Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Grid.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: Grid.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Implementation of the Grid class
13 */
14 
15 #include "stdafx.h"
16 #include "Magic.h"
17 
18 #include "MagicDoc.h"
19 #include "Grid.h"
20 
21 #include "ActiveWindow.h"
22 #include "Color.h"
23 #include "Polygon.h"
24 #include "Scene.h"
25 #include "Screen.h"
26 #include "Solid.h"
27 #include "Video.h"
28 
29 #ifdef _DEBUG
30 #define new DEBUG_NEW
31 #undef THIS_FILE
32 static char THIS_FILE[] = __FILE__;
33 #endif
34 
35 const int MAJOR_COUNT = 64;
36 
37 // +--------------------------------------------------------------------+
38 
40  : x_major(0), y_major(0), x_minor(0), y_minor(0),
41  bmp_plan(0), bmp_front(0), bmp_side(0)
42 {
43  strcpy(name, "Grid");
44 
45  plane = GRID_XZ; // plan
46 
47  show = true;
48  show_ref = true;
49  snap = true;
50  minor = true;
51 
52  x_size = 4;
53  y_size = 4;
54  x_skip = 4;
55  y_skip = 4;
56 
57  x_major = new Vec3[2*MAJOR_COUNT];
58  y_major = new Vec3[2*MAJOR_COUNT];
59 }
60 
62 {
63  delete [] x_major;
64  delete [] y_major;
65  delete [] x_minor;
66  delete [] y_minor;
67 }
68 
69 // +--------------------------------------------------------------------+
70 
71 const char*
73 {
74  if (bmp_plan)
75  return bmp_plan->GetFilename();
76 
77  return "";
78 }
79 
80 void
81 Grid::SetReferencePlan(const char* fname)
82 {
83  bmp_plan = 0;
84 
85  if (fname && *fname)
86  LoadTexture(fname, bmp_plan);
87 }
88 
89 // +--------------------------------------------------------------------+
90 
91 const char*
93 {
94  if (bmp_front)
95  return bmp_front->GetFilename();
96 
97  return "";
98 }
99 
100 void
101 Grid::SetReferenceFront(const char* fname)
102 {
103  bmp_front = 0;
104 
105  if (fname && *fname)
106  LoadTexture(fname, bmp_front);
107 }
108 
109 // +--------------------------------------------------------------------+
110 
111 const char*
113 {
114  if (bmp_side)
115  return bmp_side->GetFilename();
116 
117  return "";
118 }
119 
120 void
121 Grid::SetReferenceSide(const char* fname)
122 {
123  bmp_side = 0;
124 
125  if (fname && *fname)
126  LoadTexture(fname, bmp_side);
127 }
128 
129 // +--------------------------------------------------------------------+
130 
131 void
132 Grid::Render(Video* video, DWORD flags)
133 {
134  Vec3 origin[4];
135 
136  for (int i = 0; i < 4; i++)
137  origin[i] = loc;
138 
139  const float EXTENT = (float) MAJOR_COUNT * 64.0f;
140 
141  switch (plane) {
142  case GRID_XY: // FRONT
143  origin[0].x += -EXTENT;
144  origin[1].x += EXTENT;
145  origin[2].y += -EXTENT;
146  origin[3].y += EXTENT;
147 
148  for (int i = 0; i < 2*MAJOR_COUNT; i += 2) {
149  float x = (float) (i-MAJOR_COUNT) * 64.0f;
150  float y = (float) (i-MAJOR_COUNT) * 64.0f;
151 
152  x_major[i ] = loc + Vec3(x, -EXTENT, 0.0f);
153  x_major[i+1] = loc + Vec3(x, EXTENT, 0.0f);
154  y_major[i ] = loc + Vec3(-EXTENT, y, 0.0f);
155  y_major[i+1] = loc + Vec3( EXTENT, y, 0.0f);
156  }
157 
158  break;
159 
160  case GRID_XZ: // PLAN
161  origin[0].x += -EXTENT;
162  origin[1].x += EXTENT;
163  origin[2].z += -EXTENT;
164  origin[3].z += EXTENT;
165 
166  for (int i = 0; i < 2*MAJOR_COUNT; i += 2) {
167  float x = (float) (i-MAJOR_COUNT) * 64.0f;
168  float z = (float) (i-MAJOR_COUNT) * 64.0f;
169 
170  x_major[i ] = loc + Vec3(x, 0.0f, -EXTENT);
171  x_major[i+1] = loc + Vec3(x, 0.0f, EXTENT);
172  y_major[i ] = loc + Vec3(-EXTENT, 0.0f, z);
173  y_major[i+1] = loc + Vec3( EXTENT, 0.0f, z);
174  }
175 
176  break;
177 
178  case GRID_YZ: // SIDE
179  origin[0].y += -EXTENT;
180  origin[1].y += EXTENT;
181  origin[2].z += -EXTENT;
182  origin[3].z += EXTENT;
183 
184  for (int i = 0; i < 2*MAJOR_COUNT; i += 2) {
185  float y = (float) (i-MAJOR_COUNT) * 64.0f;
186  float z = (float) (i-MAJOR_COUNT) * 64.0f;
187 
188  x_major[i ] = loc + Vec3(0.0f, y, -EXTENT);
189  x_major[i+1] = loc + Vec3(0.0f, y, EXTENT);
190  y_major[i ] = loc + Vec3(0.0f, -EXTENT, z);
191  y_major[i+1] = loc + Vec3(0.0f, EXTENT, z);
192  }
193 
194  break;
195  }
196 
197  RenderReference(video);
198 
200  video->SetRenderState(Video::LIGHTING_ENABLE, FALSE);
201  video->SetRenderState(Video::Z_ENABLE, TRUE);
203 
206  video->DrawLines( 2, origin, Color::DarkGray, 1);
207 }
208 
209 // +--------------------------------------------------------------------+
210 
211 void
213 {
214  if (!show_ref) return;
215 
216  Bitmap* bmp = 0;
217 
218  switch (plane) {
219  case GRID_XY: bmp = bmp_front; break;
220  case GRID_XZ: bmp = bmp_plan; break;
221  case GRID_YZ: bmp = bmp_side; break;
222  }
223 
224  if (!bmp) return;
225 
226  Material mtl;
227  VertexSet vset(4);
228  Poly poly;
229  Vec3 nrm;
230 
231  float vx = (float) bmp->Width();
232  float vy = (float) bmp->Height();
233  float vz = -100.0f;
234 
235  if (vx <= 256 || vy <= 256) {
236  vx *= 2.0f;
237  vy *= 2.0f;
238  }
239 
240  switch (plane) {
241  case GRID_XY: // FRONT
242  nrm = Vec3(0.0f, 0.0f, 1.0f);
243  vset.loc[0] = loc + Vec3(-vx, -vy, vz);
244  vset.loc[1] = loc + Vec3( vx, -vy, vz);
245  vset.loc[2] = loc + Vec3( vx, vy, vz);
246  vset.loc[3] = loc + Vec3(-vx, vy, vz);
247  break;
248 
249  case GRID_XZ: // PLAN
250  nrm = Vec3(0.0f, 1.0f, 0.0f);
251  vset.loc[0] = loc + Vec3(-vx, vz, vy);
252  vset.loc[1] = loc + Vec3( vx, vz, vy);
253  vset.loc[2] = loc + Vec3( vx, vz, -vy);
254  vset.loc[3] = loc + Vec3(-vx, vz, -vy);
255  break;
256 
257  case GRID_YZ: // SIDE
258  nrm = Vec3(1.0f, 0.0f, 0.0f);
259  vset.loc[0] = loc + Vec3( vz, -vx, -vy);
260  vset.loc[1] = loc + Vec3( vz, vx, -vy);
261  vset.loc[2] = loc + Vec3( vz, vx, vy);
262  vset.loc[3] = loc + Vec3( vz, -vx, vy);
263  break;
264 
265  default:
266  return;
267  }
268 
269  mtl.Ka = Color::White;
270  mtl.Kd = Color::Black;
271  mtl.tex_diffuse = bmp;
272 
273  vset.nrm[0] = nrm;
274  vset.nrm[1] = nrm;
275  vset.nrm[2] = nrm;
276  vset.nrm[3] = nrm;
277 
278  vset.tu[0] = 0.0f;
279  vset.tv[0] = 0.0f;
280  vset.tu[1] = 1.0f;
281  vset.tv[1] = 0.0f;
282  vset.tu[2] = 1.0f;
283  vset.tv[2] = 1.0f;
284  vset.tu[3] = 0.0f;
285  vset.tv[3] = 1.0f;
286 
287  poly.nverts = 4;
288  poly.verts[0] = 0;
289  poly.verts[1] = 1;
290  poly.verts[2] = 2;
291  poly.verts[3] = 3;
292  poly.material = &mtl;
293  poly.vertex_set = &vset;
294 
297  video->SetRenderState(Video::Z_ENABLE, FALSE);
298  video->SetRenderState(Video::Z_WRITE_ENABLE, FALSE);
299  video->SetAmbient(Color::White);
300 
301  video->DrawPolys(1, &poly);
302 }
303 
304 // +----------------------------------------------------------------------+
305 
306 CPoint&
307 Grid::Snap(CPoint& p)
308 {
309  p.x = snapto(p.x, x_size);
310  p.y = snapto(p.y, y_size);
311 
312  return p;
313 }
314 
315 // +----------------------------------------------------------------------+
316 
317 int
318 Grid::snapto(int i, int dim)
319 {
320  if (!snap)
321  return i;
322 
323  int n = i + dim/2;
324  int m = n % dim;
325 
326  return (n - m);
327 }
328 
329 // +----------------------------------------------------------------------+
330 
331 void
332 Grid::SetSize(int x, int y)
333 {
334  x_size = x;
335  y_size = y ? y : x;
336 }
337 
338 // +----------------------------------------------------------------------+
339 
340 void
341 Grid::SetSkip(int x, int y)
342 {
343  x_skip = x;
344  y_skip = y ? y : x;
345 }
346