Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Selection.cpp
Go to the documentation of this file.
1 /* Project Magic 2.0
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Magic.exe
6  FILE: Selection.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Classes for rendering solid meshes of polygons
13 */
14 
15 #include "stdafx.h"
16 #include "Magic.h"
17 #include "Selection.h"
18 #include "ModelView.h"
19 #include "Projector.h"
20 
21 #include "Solid.h"
22 #include "Scene.h"
23 #include "Bitmap.h"
24 
25 #include <stdio.h>
26 
27 // +--------------------------------------------------------------------+
28 
30  : model(0), model_view(0)
31 {
32  strcpy_s(name, "Selection");
33 }
34 
35 // +--------------------------------------------------------------------+
36 
38 {
39 }
40 
41 // +--------------------------------------------------------------------+
42 
43 void
44 Selection::Render(Video* video, DWORD flags)
45 {
46  if (video && flags == Graphic::RENDER_ALPHA) {
47  ListIter<Poly> iter = polys;
48  while (++iter) {
49  Poly* p = iter.value();
50  Vec3 vloc[16];
51 
52  if (p->nverts >= 3) {
53  for (int i = 0; i < p->nverts; i++) {
54  int n = (i==p->nverts-1) ? 0 : i+1;
55 
56  vloc[2*i+0] = loc + p->vertex_set->loc[ p->verts[i] ];
57  vloc[2*i+1] = loc + p->vertex_set->loc[ p->verts[n] ];
58  }
59 
60  video->SetRenderState(Video::Z_ENABLE, FALSE);
61  video->DrawLines(p->nverts, vloc, Color(255,255,128));
62  }
63  }
64 
65  if (model) {
66  for (size_t i = 0; i < verts.size(); i++) {
67  DWORD value = verts[i];
68  WORD index = (WORD) (value >> 16);
69  WORD vert = (WORD) (value & 0xffff);
70 
71  if (index < model->NumSurfaces()) {
72  Surface* s = model->GetSurfaces()[index];
73  Vec3 v = loc + s->GetVertexSet()->loc[vert];
74 
75  if (model_view) {
76  CPoint p = model_view->ProjectPoint(v);
77  float handle[16];
78  int x1 = p.x-1;
79  int y1 = p.y-1;
80  int x2 = p.x+1;
81  int y2 = p.y+1;
82 
83  handle[ 0] = (float) x1;
84  handle[ 1] = (float) y1;
85  handle[ 2] = (float) x2;
86  handle[ 3] = (float) y1;
87 
88  handle[ 4] = (float) x2;
89  handle[ 5] = (float) y1;
90  handle[ 6] = (float) x2;
91  handle[ 7] = (float) y2;
92 
93  handle[ 8] = (float) x2;
94  handle[ 9] = (float) y2;
95  handle[10] = (float) x1;
96  handle[11] = (float) y2;
97 
98  handle[12] = (float) x1;
99  handle[13] = (float) y2;
100  handle[14] = (float) x1;
101  handle[15] = (float) y1;
102 
103  video->DrawScreenLines(4, handle, Color(255,255,128));
104  }
105 
106  else {
107  Vec3 vloc[8];
108 
109  vloc[0] = v + Vec3(-1.0f, -1.0f, 0.0f);
110  vloc[1] = v + Vec3( 1.0f, -1.0f, 0.0f);
111  vloc[2] = v + Vec3( 1.0f, -1.0f, 0.0f);
112  vloc[3] = v + Vec3( 1.0f, 1.0f, 0.0f);
113  vloc[4] = v + Vec3( 1.0f, 1.0f, 0.0f);
114  vloc[5] = v + Vec3(-1.0f, 1.0f, 0.0f);
115  vloc[6] = v + Vec3(-1.0f, 1.0f, 0.0f);
116  vloc[7] = v + Vec3(-1.0f, -1.0f, 0.0f);
117 
118  video->SetRenderState(Video::Z_ENABLE, FALSE);
119  video->DrawLines(4, vloc, Color(255,255,128));
120  }
121  }
122  }
123  }
124 
125  video->UseMaterial(0);
126  }
127 }
128 
129 // +--------------------------------------------------------------------+
130 
131 void
133 {
134  if (!polys.contains(p))
135  polys.append(p);
136 }
137 
138 void
140 {
141  polys.remove(p);
142 }
143 
144 bool
146 {
147  return polys.contains(p);
148 }
149 
150 void
151 Selection::AddVert(WORD s, WORD v)
152 {
153  DWORD value = (s << 16) | v;
154 
155  bool contains = false;
156  for (auto vi = verts.begin(); vi != verts.end(); ++vi) {
157  if (*vi == value) {
158  contains = true;
159  break;
160  }
161  }
162 
163  if (!contains)
164  verts.push_back(value);
165 }
166 
167 void
168 Selection::RemoveVert(WORD s, WORD v)
169 {
170  DWORD value = (s << 16) | v;
171 
172  for (auto vi = verts.begin(); vi != verts.end(); ++vi) {
173  if (*vi == value) {
174  verts.erase(vi);
175  return;
176  }
177  }
178 }
179 
180 bool
181 Selection::Contains(WORD s, WORD v) const
182 {
183  DWORD value = (s << 16) | v;
184 
185  for (auto vi = verts.begin(); vi != verts.end(); ++vi) {
186  if (*vi == value) {
187  return true;
188  }
189  }
190 
191  return false;
192 }