forked from TheSuperHackers/GeneralsGameCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenCursor.cpp
More file actions
383 lines (328 loc) · 10 KB
/
ScreenCursor.cpp
File metadata and controls
383 lines (328 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
** Command & Conquer Renegade(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/***********************************************************************************************
*** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
***********************************************************************************************
* *
* Project Name : W3DView *
* *
* $Archive:: /Commando/Code/Tools/W3DView/ScreenCursor.cpp $Modtime:: $*
* *
* $Revision:: 8 $*
* *
*---------------------------------------------------------------------------------------------*
* Functions: *
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#include "StdAfx.h"
#include "ScreenCursor.h"
#include "Utils.h"
#include "ww3d.h"
#include "vertmaterial.h"
#include "shader.h"
#include "scene.h"
#include "rinfo.h"
#include "texture.h"
#include "dx8wrapper.h"
#include "dx8vertexbuffer.h"
#include "dx8indexbuffer.h"
#include "sortingrenderer.h"
///////////////////////////////////////////////////////////////////
//
// ScreenCursorClass
//
///////////////////////////////////////////////////////////////////
ScreenCursorClass::ScreenCursorClass (void)
: m_ScreenPos (0, 0),
m_pTexture (NULL),
m_pVertMaterial (NULL),
m_Width (0),
m_Height (0),
m_hWnd (NULL)
{
Initialize ();
return ;
}
///////////////////////////////////////////////////////////////////
//
// ScreenCursorClass
//
///////////////////////////////////////////////////////////////////
ScreenCursorClass::ScreenCursorClass (const ScreenCursorClass &src)
: m_ScreenPos (0, 0),
m_pTexture (NULL),
m_hWnd (NULL),
m_pVertMaterial (NULL),
m_Width (0),
m_Height (0),
RenderObjClass (src)
{
Initialize ();
return ;
}
///////////////////////////////////////////////////////////////////
//
// ~ScreenCursorClass
//
///////////////////////////////////////////////////////////////////
ScreenCursorClass::~ScreenCursorClass (void)
{
REF_PTR_RELEASE (m_pTexture);
REF_PTR_RELEASE (m_pVertMaterial);
return ;
}
///////////////////////////////////////////////////////////////////
//
// Initialize
//
///////////////////////////////////////////////////////////////////
void
ScreenCursorClass::Initialize (void)
{
REF_PTR_RELEASE(m_pVertMaterial);
// Create default vertex material
m_pVertMaterial = NEW_REF( VertexMaterialClass, ());
m_pVertMaterial->Set_Diffuse (1.0F, 1.0F, 1.0F);
m_pVertMaterial->Set_Emissive (0.0F, 0.0F, 0.0F);
m_pVertMaterial->Set_Specular (1.0F, 1.0F, 1.0F);
m_pVertMaterial->Set_Ambient (1.0F, 1.0F, 1.0F);
m_Triangles[0].I = 0;
m_Triangles[0].J = 1;
m_Triangles[0].K = 2;
m_Triangles[1].I = 1;
m_Triangles[1].J = 2;
m_Triangles[1].K = 3;
m_Normals[0].X = 0;
m_Normals[0].Y = 0;
m_Normals[0].Z = -1;
m_Normals[1].X = 0;
m_Normals[1].Y = 0;
m_Normals[1].Z = -1;
m_Normals[2].X = 0;
m_Normals[2].Y = 0;
m_Normals[2].Z = -1;
m_Normals[3].X = 0;
m_Normals[3].Y = 0;
m_Normals[3].Z = -1;
m_UVs[0].X = 0;
m_UVs[0].Y = 0;
m_UVs[1].X = 1.0F;
m_UVs[1].Y = 0;
m_UVs[2].X = 0;
m_UVs[2].Y = 1.0F;
m_UVs[3].X = 1.0F;
m_UVs[3].Y = 1.0F;
return ;
}
///////////////////////////////////////////////////////////////////
//
// Set_Texture
//
///////////////////////////////////////////////////////////////////
void
ScreenCursorClass::Set_Texture (TextureClass *texture)
{
REF_PTR_SET (m_pTexture, texture);
// Find the dimensions of the texture:
if (m_pTexture != NULL) {
m_Width = m_pTexture->Get_Width();
m_Height = m_pTexture->Get_Height();
}
return ;
}
///////////////////////////////////////////////////////////////////
//
// On_Frame_Update
//
///////////////////////////////////////////////////////////////////
void
ScreenCursorClass::On_Frame_Update (void)
{
//
// Get the current cursor position in screen coords
//
POINT point = { 0 };
::GetCursorPos (&point);
if (m_hWnd != NULL) {
//
// Normalize the screen position
//
RECT rect = { 0 };
::GetClientRect (m_hWnd, &rect);
::ScreenToClient (m_hWnd, &point);
m_ScreenPos.X = ((float)point.x) / ((float)rect.right);
m_ScreenPos.Y = ((float)point.y) / ((float)rect.bottom);
} else {
//
// Normalize the screen position
//
m_ScreenPos.X = ((float)point.x) / ((float)::GetSystemMetrics (SM_CXSCREEN));
m_ScreenPos.Y = ((float)point.y) / ((float)::GetSystemMetrics (SM_CYSCREEN));
}
//
// Determine the current display resolution
//
int screen_cx = 0;
int screen_cy = 0;
int bits = 0;
bool windowed = false;
WW3D::Get_Device_Resolution (screen_cx, screen_cy, bits, windowed);
//
// Calculate the 3D position
//
float normal_width = ((float)m_Width) / (float)screen_cx;
float normal_height = ((float)m_Height) / (float)screen_cy;
float x_pos = floor(m_ScreenPos.X * ((float)screen_cx) + 0.5F) / ((float)screen_cx);
float y_pos = floor(m_ScreenPos.Y * ((float)screen_cy) + 0.5F) / ((float)screen_cy);
float z_pos = 0;
//
// Convert the 3D position to normalized 'view' coords
//
float x_max = ((x_pos + normal_width) * 2) - 1;
float y_max = 1 - ((y_pos + normal_height) * 2);
x_pos = (x_pos * 2) - 1;
y_pos = 1 - (y_pos * 2);
z_pos = 0;
//
// Build the verticies from the position and extents
//
m_Verticies[0].X = x_pos;
m_Verticies[0].Y = y_pos;
m_Verticies[0].Z = z_pos;
m_Verticies[1].X = x_max;
m_Verticies[1].Y = y_pos;
m_Verticies[1].Z = z_pos;
m_Verticies[2].X = x_pos;
m_Verticies[2].Y = y_max;
m_Verticies[2].Z = z_pos;
m_Verticies[3].X = x_max;
m_Verticies[3].Y = y_max;
m_Verticies[3].Z = z_pos;
return ;
}
///////////////////////////////////////////////////////////////////
//
// Render
//
///////////////////////////////////////////////////////////////////
void
ScreenCursorClass::Render (RenderInfoClass &rinfo)
{
const int VERTEX_COUNT = 4;
const int FACE_COUNT = 2;
/*
** Dump the vertices into the dynamic sorting vertex buffer.
*/
DynamicVBAccessClass vbaccess(BUFFER_TYPE_DYNAMIC_SORTING,dynamic_fvf_type,VERTEX_COUNT);
{
DynamicVBAccessClass::WriteLockClass lock(&vbaccess);
VertexFormatXYZNDUV2* vb=lock.Get_Formatted_Vertex_Array();
for (int i=0; i<VERTEX_COUNT; i++) {
// Locations
vb->x=m_Verticies[i].X;
vb->y=m_Verticies[i].Y;
vb->z=m_Verticies[i].Z;
// Normals
vb->nx=m_Normals[i].X;
vb->ny=m_Normals[i].Y;
vb->nz=m_Normals[i].Z;
// UV coordinates
vb->u1=m_UVs[i].X;
vb->v1=m_UVs[i].Y;
vb++;
}
}
/*
** Dump the faces into the dynamic sorting index buffer.
*/
DynamicIBAccessClass ibaccess(BUFFER_TYPE_DYNAMIC_SORTING,FACE_COUNT*3);
{
DynamicIBAccessClass::WriteLockClass lock(&ibaccess);
unsigned short * indices = lock.Get_Index_Array();
for (int i=0; i<FACE_COUNT; i++) {
indices[3*i+0] = m_Triangles[i][0];
indices[3*i+1] = m_Triangles[i][1];
indices[3*i+2] = m_Triangles[i][2];
}
}
/*
** Apply the shader and material
*/
DX8Wrapper::Set_Material(m_pVertMaterial);
DX8Wrapper::Set_Shader(ShaderClass::_PresetATestBlend2DShader);
DX8Wrapper::Set_Texture(0,m_pTexture);
DX8Wrapper::Set_Vertex_Buffer(vbaccess);
DX8Wrapper::Set_Index_Buffer(ibaccess,0);
SphereClass sphere;
Get_Obj_Space_Bounding_Sphere(sphere);
SortingRendererClass::Insert_Triangles(
sphere,
0,
FACE_COUNT*3,
0,
VERTEX_COUNT*2);
return ;
}
//////////////////////////////////////////////////////////////
//
// Get_Obj_Space_Bounding_Sphere
//
//////////////////////////////////////////////////////////////
void
ScreenCursorClass::Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const
{
sphere.Center = Get_Transform().Get_Translation();
sphere.Radius = max (m_Width, m_Height);
}
//////////////////////////////////////////////////////////////
//
// Get_Obj_Space_Bounding_Box
//
//////////////////////////////////////////////////////////////
void
ScreenCursorClass::Get_Obj_Space_Bounding_Box(AABoxClass & box) const
{
Matrix3D transform = Get_Transform ();
box.Center = transform.Get_Translation ();
box.Extent.Set(0.1F, m_Width, m_Height);
}
//////////////////////////////////////////////////////////////
//
// Notify_Added
//
//////////////////////////////////////////////////////////////
void
ScreenCursorClass::Notify_Added (SceneClass * scene)
{
if (scene != NULL) {
scene->Register (this, SceneClass::ON_FRAME_UPDATE);
}
return ;
}
//////////////////////////////////////////////////////////////
//
// Notify_Removed
//
//////////////////////////////////////////////////////////////
void
ScreenCursorClass::Notify_Removed (SceneClass * scene)
{
if (scene != NULL) {
scene->Unregister (this, SceneClass::ON_FRAME_UPDATE);
}
return ;
}