Rev 18 | Rev 116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | // render.cpp |
2 | |||
3 | // thanks ChaotikMind for optimizing the engine a bit :) |
||
4 | |||
5 | #include "common.h" |
||
6 | |||
7 | |||
8 | // note: DirectX requires a C++ compiler. |
||
9 | #include "DirectX9/Include/d3d9.h" |
||
10 | #include "DirectX9/Include/d3dx9.h" |
||
11 | |||
12 | // include the Direct3D library files |
||
13 | #pragma comment (lib, "DirectX9/Lib/x86/d3d9.lib") |
||
14 | #pragma comment (lib, "DirectX9/Lib/x86/d3dx9.lib") |
||
15 | |||
16 | |||
17 | // define this to display framerate |
||
18 | #define WANT_FRAMERATE |
||
19 | |||
20 | |||
21 | // handy macro to print a chatter channel reply |
||
22 | #define PRINT_CCREPLY(ccreply) \ |
||
23 | { \ |
||
24 | Render_wprintf (rect.right, initial_height - combined_height, initial_width - rect.right, ALIGN_LEFT, ALIGN_TOP, ALIGN_LEFT, \ |
||
25 | chat_fontindex, (ccreply)->color, &rect, \ |
||
26 | L"["); \ |
||
27 | Render_wprintf (rect.right, initial_height - combined_height, initial_width - rect.right, ALIGN_LEFT, ALIGN_TOP, ALIGN_LEFT, \ |
||
28 | chat_fontindex, RGBA_TO_RGBACOLOR (159, 159, 159, RGBACOLOR_ALPHA ((ccreply)->color)), &rect, \ |
||
29 | (ccreply)->channelname); \ |
||
30 | Render_wprintf (rect.right, initial_height - combined_height, initial_width - rect.right, ALIGN_LEFT, ALIGN_TOP, ALIGN_LEFT, \ |
||
31 | chat_fontindex, (ccreply)->color, &rect, \ |
||
32 | L"] "); /* closing bracket + non-breakable space */ \ |
||
33 | Render_wprintf (rect.right, initial_height - combined_height, initial_width - rect.right, ALIGN_LEFT, ALIGN_TOP, ALIGN_LEFT, \ |
||
34 | chat_fontindex, RGBA_TO_RGBACOLOR (159, 159, 159, RGBACOLOR_ALPHA ((ccreply)->color)), &rect, \ |
||
35 | (ccreply)->nickname); \ |
||
36 | Render_wprintf (rect.right, initial_height - combined_height, initial_width - rect.right, ALIGN_LEFT, ALIGN_TOP, ALIGN_LEFT, \ |
||
37 | chat_fontindex, (ccreply)->color, &rect, \ |
||
38 | L": "); /* colon + non-breakable space */ \ |
||
39 | if ((ccreply)->text != NULL) \ |
||
40 | Render_wprintf (rect.right, initial_height - combined_height, initial_width - rect.right, ALIGN_LEFT, ALIGN_TOP, ALIGN_LEFT, \ |
||
41 | chat_fontindex, (ccreply)->color, &rect, \ |
||
42 | (ccreply)->text); \ |
||
43 | } |
||
44 | |||
45 | |||
46 | // handy macro to draw a GUI button |
||
47 | #define DRAW_BUTTON_IF_NEEDED(button) \ |
||
48 | { \ |
||
49 | if ((button).state != 0) \ |
||
50 | Render_DrawSprite (&sprites[(button).sprite_index], (button).left, (button).top, (button).width, (button).height, ((button).state == 2 ? 0xFF : 0x7F)); \ |
||
51 | } |
||
52 | |||
53 | |||
54 | // handy macro to draw a GUI text |
||
55 | #define DRAW_TEXT_IF_NEEDED(text) \ |
||
56 | { \ |
||
57 | if ((text).is_displayed) \ |
||
58 | { \ |
||
59 | Render_wprintf ((int) ((text).xpos_percent * (float) initial_width) / 100, (int) ((text).ypos_percent * (float) initial_height) / 100, \ |
||
60 | (int) ((text).maxwidth_percent * (float) initial_width) / 100, (text).horizontal_align, (text).vertical_align, (text).text_align, (text).font_index, \ |
||
61 | ((text).want_fade ? \ |
||
62 | RGBACOLOR_SETALPHA ((text).color, \ |
||
63 | (((text).appear_time + (text).disappear_time) * 0.5f > current_time ? \ |
||
64 | /* fading in */ (int) FadeFloat (0, RGBACOLOR_ALPHA ((text).color), (text).appear_time, (text).appear_time + 0.5f) : \ |
||
65 | /* fading out */ (int) FadeFloat (RGBACOLOR_ALPHA ((text).color), 0, (text).disappear_time - 3.0f, (text).disappear_time))) : \ |
||
66 | (text).color), \ |
||
67 | NULL, (text).buffer); \ |
||
68 | if ((text).disappear_time < current_time) \ |
||
69 | (text).is_displayed = false; \ |
||
70 | } \ |
||
71 | } |
||
72 | |||
73 | |||
74 | #pragma pack(push,1) |
||
75 | |||
76 | // definition for a vector |
||
77 | typedef struct vector_s |
||
78 | { |
||
79 | float x; // X component |
||
80 | float y; // Y component |
||
81 | float z; // Z component |
||
82 | } vector_t; |
||
83 | |||
84 | |||
85 | // definition for a texture coordinates pair |
||
86 | typedef struct texcoord_s |
||
87 | { |
||
88 | float u; // X coordinate of the texture point this vertex corresponds to |
||
89 | float v; // Y coordinate of the texture point this vertex corresponds to |
||
90 | } texcoord_t; |
||
91 | |||
92 | |||
93 | // definition for a vertex (must be in that order for Direct3D) |
||
94 | typedef struct vertex_s |
||
95 | { |
||
96 | vector_t position; // position in space |
||
97 | vector_t normal; // coordinates of the unary normal vector of the plane this vertex is on (for illumination) |
||
98 | texcoord_t texcoord; // coordinates of the texture point this vertex corresponds to |
||
99 | } vertex_t; |
||
100 | |||
101 | #pragma pack(pop) |
||
102 | |||
103 | |||
104 | // definition for a reflected object (qsort array element to sort reflections by distance) |
||
105 | typedef struct reflectedobject_s |
||
106 | { |
||
107 | sceneobject_t *object; // pointer to the scene object |
||
108 | float distance; // distance to viewer camera |
||
109 | } reflectedobject_t; |
||
110 | |||
111 | |||
112 | // definition for a material (light reflection type) |
||
113 | typedef struct material_s |
||
114 | { |
||
115 | wchar_t name[32]; // material name |
||
116 | float ambient; // ambient reflection value ranging from 0 to 1 |
||
117 | float diffuse; // diffuse reflection value ranging from 0 to 1 |
||
118 | float emissive; // emissive reflection value ranging from 0 to 1 |
||
119 | float specular; // specular reflection value ranging from 0 to 1 |
||
120 | float shininess; // shininess (specular factor) |
||
121 | float transparency; // transparency value ranging from 0 (fully transparent) to 1 (opaque) |
||
122 | } material_t; |
||
123 | |||
124 | |||
125 | // definition for a mesh |
||
126 | typedef struct mesh_s |
||
127 | { |
||
128 | unsigned long hash; // basic content hash, to avoid duplicates |
||
129 | unsigned long vertex_format; |
||
130 | IDirect3DVertexBuffer9 *d3dvertices; // handled opaquely by Direct3D |
||
131 | int vertice_size; |
||
132 | int vertice_count; |
||
133 | bool is_indexed; // set to TRUE if this mesh has an index buffer |
||
134 | IDirect3DIndexBuffer9 *d3dindices; // handled opaquely by Direct3D |
||
135 | int indice_size; |
||
136 | int indice_count; |
||
137 | } mesh_t; |
||
138 | |||
139 | |||
140 | // definition for a texture |
||
141 | typedef struct texture_s |
||
142 | { |
||
143 | unsigned long hash; // basic content hash, to avoid duplicates |
||
144 | int width; |
||
145 | int height; |
||
146 | IDirect3DTexture9 *texture; |
||
147 | } texture_t; |
||
148 | |||
149 | |||
150 | // definition for a font |
||
151 | typedef struct font_s |
||
152 | { |
||
153 | unsigned long pathname_hash; |
||
154 | ID3DXFont *font; |
||
155 | } font_t; |
||
156 | |||
157 | |||
158 | // definition for a sprite |
||
159 | typedef struct sprite_s |
||
160 | { |
||
161 | unsigned long hash; // basic content hash, to avoid duplicates |
||
162 | ID3DXSprite *sprite; |
||
163 | int texture_index; |
||
164 | } sprite_t; |
||
165 | |||
166 | |||
167 | // global variables used in this module only |
||
168 | static IDirect3D9 *d3d = NULL; // our Direct3D interface |
||
169 | static IDirect3DDevice9 *d3ddev = NULL; // the device class |
||
170 | |||
171 | static material_t *materials = NULL; |
||
172 | static int material_count = 0; |
||
173 | static texture_t *textures = NULL; |
||
174 | static int texture_count = 0; |
||
175 | static mesh_t *meshes = NULL; |
||
176 | static int mesh_count = 0; |
||
177 | static font_t *fonts = NULL; |
||
178 | static int font_count = 0; |
||
179 | static sprite_t *sprites = NULL; |
||
180 | static int sprite_count = 0; |
||
181 | static const float fov_value = 45.0f; // field of view width, in degrees |
||
182 | static const float viewdist_near = 1.0f; // nearest view plane distance |
||
183 | static const float viewdist_far = 200.0f; // farthest view plane distance |
||
6 | pmbaty | 184 | static int initial_width = 0; // initial width of the render surface, in pixels (typically equal to the max displayable size) |
185 | static int initial_height = 0; // initial height of the render surface, in pixels (typically equal to the max displayable size) |
||
1 | pmbaty | 186 | static float current_width = 0.0f; // current width of the client area on which the render surface is rendered, in pixels |
187 | static float current_height = 0.0f; // current height of the client area on which the render surface is rendered, in pixels |
||
188 | static D3DCOLOR ambient_light; |
||
189 | static vector_t camera_position; |
||
190 | static const vector_t scene_center = { 0.0f, 0.0f, 0.0f }; |
||
191 | static const vector_t upwards_direction = { 0.0f, 0.0f, 1.0f }; |
||
192 | static int best_supported_filter; |
||
5 | pmbaty | 193 | static bool is_fsaa_supported = false; |
1 | pmbaty | 194 | |
195 | static wchar_t printf_buffer[0xffff]; |
||
196 | |||
197 | |||
198 | // prototypes of functions used in this module only |
||
199 | static bool Render_LoadMesh_X (mesh_t *mesh, const wchar_t *xfile_pathname); |
||
200 | static bool Render_LoadMesh_Obj (mesh_t *mesh, const wchar_t *objfile_pathname); |
||
201 | static void Render_DrawSceneObjectReflection (sceneobject_t *sceneobject); |
||
202 | static void Render_DrawSceneObject (sceneobject_t *sceneobject); |
||
203 | static void Render_DrawSceneTile (sceneobject_t *sceneobject); |
||
204 | static void Render_DrawSprite (sprite_t *sprite, float x_percent, float y_percent, float width_percent, float height_percent, int alpha); |
||
205 | static void Render_GetTextBoundaries (int max_width, int font_id, wchar_t *text, RECT *rect); |
||
206 | static void Render_wprintf (int x, int y, int max_width, int horiz_align, int vert_align, int text_align, int font_id, unsigned long color_rgba, RECT *out_rect, const wchar_t *fmt, ...); |
||
207 | static float DistanceToCamera (float x, float y, float z); |
||
208 | static float FadeFloat (float from, float to, float start_time, float end_time); |
||
209 | static unsigned long HashString (const wchar_t *string_buffer); |
||
210 | static unsigned long HashFile (const wchar_t *file_pathname); |
||
211 | static void ResolveWildcard (wchar_t *file_pathname, wchar_t *extensions_separated_by_bars); |
||
212 | static int SortReflectedObjects (const void *object1, const void *object2); |
||
213 | |||
214 | |||
215 | bool Render_Init (void) |
||
216 | { |
||
217 | // this function sets up and initializes Direct3D |
||
218 | |||
219 | static wchar_t *default_materialname = L"default"; |
||
220 | |||
221 | D3DCAPS9 device_capabilities; |
||
222 | unsigned long behaviour_flags; |
||
5 | pmbaty | 223 | unsigned long multisample_quality_count; |
1 | pmbaty | 224 | D3DPRESENT_PARAMETERS d3dpp; |
2 | pmbaty | 225 | wchar_t errorfile_path[MAX_PATH]; |
1 | pmbaty | 226 | wchar_t line_buffer[256]; |
227 | material_t material; |
||
2 | pmbaty | 228 | HRESULT ret; |
1 | pmbaty | 229 | RECT rect; |
6 | pmbaty | 230 | RECT viewport_rect; |
1 | pmbaty | 231 | FILE *fp; |
232 | |||
233 | // create the Direct3D interface |
||
234 | d3d = Direct3DCreate9 (D3D_SDK_VERSION); |
||
235 | |||
236 | // get hardware capabilities |
||
237 | if (FAILED (d3d->GetDeviceCaps (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &device_capabilities))) |
||
238 | { |
||
239 | MessageBox (NULL, LOCALIZE (L"Error_CouldNotCreateD3DDevGetDeviceCapsFailed"), LOCALIZE (L"FatalError"), MB_ICONERROR | MB_OK); |
||
11 | pmbaty | 240 | terminate_everything = true; // this is a fatal error |
1 | pmbaty | 241 | return (false); |
242 | } |
||
243 | |||
244 | // grab info from that and adjust our D3D settings |
||
245 | best_supported_filter = (device_capabilities.RasterCaps & D3DPRASTERCAPS_ANISOTROPY ? D3DTEXF_ANISOTROPIC : D3DTEXF_LINEAR); |
||
246 | behaviour_flags = (device_capabilities.VertexProcessingCaps != 0 ? D3DCREATE_HARDWARE_VERTEXPROCESSING : D3DCREATE_SOFTWARE_VERTEXPROCESSING); |
||
247 | |||
5 | pmbaty | 248 | // see if simple (non-maskable) full-scene antialiasing is supported and in how many quality flavors |
249 | if (SUCCEEDED (d3d->CheckDeviceMultiSampleType (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, false, D3DMULTISAMPLE_NONMASKABLE, &multisample_quality_count))) |
||
250 | is_fsaa_supported = true; // remember FSAA is supported |
||
1 | pmbaty | 251 | |
252 | memset (&d3dpp, 0, sizeof (d3dpp)); // clear out the struct for use |
||
253 | d3dpp.Windowed = true; // always windowed (because we can't display dialog boxes in fullscreen mode) |
||
254 | d3dpp.BackBufferCount = 1; |
||
255 | d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames |
||
256 | d3dpp.hDeviceWindow = hMainWnd; // set the window to be used by Direct3D |
||
257 | d3dpp.EnableAutoDepthStencil = true; // enable Z-buffer and stencil buffer |
||
258 | d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; // D3DFMT_D15S1 15 bits should be enough to store each pixel's Z depth |
||
5 | pmbaty | 259 | if (is_fsaa_supported) |
260 | { |
||
261 | d3dpp.MultiSampleType = D3DMULTISAMPLE_NONMASKABLE; // use multisampling (full scene antialiasing) if supported |
||
262 | d3dpp.MultiSampleQuality = multisample_quality_count - 1; // use the best available multisample quality |
||
263 | } |
||
264 | else |
||
265 | { |
||
266 | d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; // do not use multisampling (full scene antialiasing) |
||
267 | d3dpp.MultiSampleQuality = 0; |
||
268 | } |
||
1 | pmbaty | 269 | |
6 | pmbaty | 270 | // get main window's current rectangle and full screen size |
271 | GetWindowRect (hMainWnd, &rect); |
||
272 | |||
273 | // create a device class using this information and the info from the d3dpp stuct. Resize the window to the max displayable |
||
274 | // size before creating the DirectX device, so as to guarantee the viewport will never be upscaled in case of window resize. |
||
275 | MoveWindow (hMainWnd, rect.left, rect.top, GetSystemMetrics (SM_CXSCREEN), GetSystemMetrics (SM_CYSCREEN), false); |
||
2 | pmbaty | 276 | if (FAILED (ret = d3d->CreateDevice (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hMainWnd, behaviour_flags, &d3dpp, &d3ddev))) |
1 | pmbaty | 277 | { |
278 | MessageBox (NULL, LOCALIZE (L"Error_CouldNotCreateD3DDevCreateDeviceFailed"), LOCALIZE (L"FatalError"), MB_ICONERROR | MB_OK); |
||
2 | pmbaty | 279 | |
280 | // on error, write a log file on the user's desktop |
||
281 | errorfile_path[0] = 0; |
||
282 | SHGetSpecialFolderPath (NULL, errorfile_path, CSIDL_DESKTOP, true); |
||
283 | wcscat_s (errorfile_path, WCHAR_SIZEOF (errorfile_path), L"\\Chess Giants error.log"); |
||
284 | _wfopen_s (&fp, errorfile_path, L"w, ccs=UNICODE"); |
||
285 | if (fp != NULL) |
||
286 | { |
||
287 | fwprintf (fp, L"device_capabilities.DeviceType = %d\n", device_capabilities.DeviceType); |
||
288 | fwprintf (fp, L"device_capabilities.AdapterOrdinal = %ul\n", device_capabilities.AdapterOrdinal); |
||
289 | fwprintf (fp, L"device_capabilities.Caps = %ul\n", device_capabilities.Caps); |
||
290 | fwprintf (fp, L"device_capabilities.Caps2 = %ul\n", device_capabilities.Caps2); |
||
291 | fwprintf (fp, L"device_capabilities.Caps3 = %ul\n", device_capabilities.Caps3); |
||
292 | fwprintf (fp, L"device_capabilities.PresentationIntervals = %ul\n", device_capabilities.PresentationIntervals); |
||
293 | fwprintf (fp, L"device_capabilities.CursorCaps = %ul\n", device_capabilities.CursorCaps); |
||
294 | fwprintf (fp, L"device_capabilities.DevCaps = %ul\n", device_capabilities.DevCaps); |
||
295 | fwprintf (fp, L"device_capabilities.PrimitiveMiscCaps = %ul\n", device_capabilities.PrimitiveMiscCaps); |
||
296 | fwprintf (fp, L"device_capabilities.RasterCaps = %ul\n", device_capabilities.RasterCaps); |
||
297 | fwprintf (fp, L"device_capabilities.ZCmpCaps = %ul\n", device_capabilities.ZCmpCaps); |
||
298 | fwprintf (fp, L"device_capabilities.SrcBlendCaps = %ul\n", device_capabilities.SrcBlendCaps); |
||
299 | fwprintf (fp, L"device_capabilities.DestBlendCaps = %ul\n", device_capabilities.DestBlendCaps); |
||
300 | fwprintf (fp, L"device_capabilities.AlphaCmpCaps = %ul\n", device_capabilities.AlphaCmpCaps); |
||
301 | fwprintf (fp, L"device_capabilities.ShadeCaps = %ul\n", device_capabilities.ShadeCaps); |
||
302 | fwprintf (fp, L"device_capabilities.TextureCaps = %ul\n", device_capabilities.TextureCaps); |
||
303 | fwprintf (fp, L"device_capabilities.TextureFilterCaps = %ul\n", device_capabilities.TextureFilterCaps); |
||
304 | fwprintf (fp, L"device_capabilities.CubeTextureFilterCaps = %ul\n", device_capabilities.CubeTextureFilterCaps); |
||
305 | fwprintf (fp, L"device_capabilities.VolumeTextureFilterCaps = %ul\n", device_capabilities.VolumeTextureFilterCaps); |
||
306 | fwprintf (fp, L"device_capabilities.TextureAddressCaps = %ul\n", device_capabilities.TextureAddressCaps); |
||
307 | fwprintf (fp, L"device_capabilities.VolumeTextureAddressCaps = %ul\n", device_capabilities.VolumeTextureAddressCaps); |
||
308 | fwprintf (fp, L"device_capabilities.LineCaps = %ul\n", device_capabilities.LineCaps); |
||
309 | fwprintf (fp, L"device_capabilities.MaxTextureWidth = %ul\n", device_capabilities.MaxTextureWidth); |
||
310 | fwprintf (fp, L"device_capabilities.MaxTextureHeight = %ul\n", device_capabilities.MaxTextureHeight); |
||
311 | fwprintf (fp, L"device_capabilities.MaxVolumeExtent = %ul\n", device_capabilities.MaxVolumeExtent); |
||
312 | fwprintf (fp, L"device_capabilities.MaxTextureRepeat = %ul\n", device_capabilities.MaxTextureRepeat); |
||
313 | fwprintf (fp, L"device_capabilities.MaxTextureAspectRatio = %ul\n", device_capabilities.MaxTextureAspectRatio); |
||
314 | fwprintf (fp, L"device_capabilities.MaxAnisotropy = %ul\n", device_capabilities.MaxAnisotropy); |
||
315 | fwprintf (fp, L"device_capabilities.MaxVertexW = %f\n", device_capabilities.MaxVertexW); |
||
316 | fwprintf (fp, L"device_capabilities.GuardBandLeft = %f\n", device_capabilities.GuardBandLeft); |
||
317 | fwprintf (fp, L"device_capabilities.GuardBandTop = %f\n", device_capabilities.GuardBandTop); |
||
318 | fwprintf (fp, L"device_capabilities.GuardBandRight = %f\n", device_capabilities.GuardBandRight); |
||
319 | fwprintf (fp, L"device_capabilities.GuardBandBottom = %f\n", device_capabilities.GuardBandBottom); |
||
320 | fwprintf (fp, L"device_capabilities.ExtentsAdjust = %f\n", device_capabilities.ExtentsAdjust); |
||
321 | fwprintf (fp, L"device_capabilities.StencilCaps = %ul\n", device_capabilities.StencilCaps); |
||
322 | fwprintf (fp, L"device_capabilities.FVFCaps = %ul\n", device_capabilities.FVFCaps); |
||
323 | fwprintf (fp, L"device_capabilities.TextureOpCaps = %ul\n", device_capabilities.TextureOpCaps); |
||
324 | fwprintf (fp, L"device_capabilities.MaxTextureBlendStages = %ul\n", device_capabilities.MaxTextureBlendStages); |
||
325 | fwprintf (fp, L"device_capabilities.MaxSimultaneousTextures = %ul\n", device_capabilities.MaxSimultaneousTextures); |
||
326 | fwprintf (fp, L"device_capabilities.VertexProcessingCaps = %ul\n", device_capabilities.VertexProcessingCaps); |
||
327 | fwprintf (fp, L"device_capabilities.MaxActiveLights = %ul\n", device_capabilities.MaxActiveLights); |
||
328 | fwprintf (fp, L"device_capabilities.MaxUserClipPlanes = %ul\n", device_capabilities.MaxUserClipPlanes); |
||
329 | fwprintf (fp, L"device_capabilities.MaxVertexBlendMatrices = %ul\n", device_capabilities.MaxVertexBlendMatrices); |
||
330 | fwprintf (fp, L"device_capabilities.MaxVertexBlendMatrixIndex = %ul\n", device_capabilities.MaxVertexBlendMatrixIndex); |
||
331 | fwprintf (fp, L"device_capabilities.MaxPointSize = %f\n", device_capabilities.MaxPointSize); |
||
332 | fwprintf (fp, L"device_capabilities.MaxPrimitiveCount = %ul\n", device_capabilities.MaxPrimitiveCount); |
||
333 | fwprintf (fp, L"device_capabilities.MaxVertexIndex = %ul\n", device_capabilities.MaxVertexIndex); |
||
334 | fwprintf (fp, L"device_capabilities.MaxStreams = %ul\n", device_capabilities.MaxStreams); |
||
335 | fwprintf (fp, L"device_capabilities.MaxStreamStride = %ul\n", device_capabilities.MaxStreamStride); |
||
336 | fwprintf (fp, L"device_capabilities.VertexShaderVersion = %ul\n", device_capabilities.VertexShaderVersion); |
||
337 | fwprintf (fp, L"device_capabilities.MaxVertexShaderConst = %ul\n", device_capabilities.MaxVertexShaderConst); |
||
338 | fwprintf (fp, L"device_capabilities.PixelShaderVersion = %ul\n", device_capabilities.PixelShaderVersion); |
||
339 | fwprintf (fp, L"device_capabilities.PixelShader1xMaxValue = %f\n", device_capabilities.PixelShader1xMaxValue); |
||
340 | fwprintf (fp, L"device_capabilities.DevCaps2 = %ul\n", device_capabilities.DevCaps2); |
||
341 | fwprintf (fp, L"device_capabilities.MaxNpatchTessellationLevel = %f\n", device_capabilities.MaxNpatchTessellationLevel); |
||
342 | fwprintf (fp, L"device_capabilities.Reserved5 = %ul\n", device_capabilities.Reserved5); |
||
343 | fwprintf (fp, L"device_capabilities.MasterAdapterOrdinal = %ul\n", device_capabilities.MasterAdapterOrdinal); |
||
344 | fwprintf (fp, L"device_capabilities.AdapterOrdinalInGroup = %ul\n", device_capabilities.AdapterOrdinalInGroup); |
||
345 | fwprintf (fp, L"device_capabilities.NumberOfAdaptersInGroup = %ul\n", device_capabilities.NumberOfAdaptersInGroup); |
||
346 | fwprintf (fp, L"device_capabilities.DeclTypes = %ul\n", device_capabilities.DeclTypes); |
||
347 | fwprintf (fp, L"device_capabilities.NumSimultaneousRTs = %ul\n", device_capabilities.NumSimultaneousRTs); |
||
348 | fwprintf (fp, L"device_capabilities.StretchRectFilterCaps = %ul\n", device_capabilities.StretchRectFilterCaps); |
||
349 | fwprintf (fp, L"device_capabilities.VS20Caps.Caps = %ul\n", device_capabilities.VS20Caps.Caps); |
||
350 | fwprintf (fp, L"device_capabilities.VS20Caps.DynamicFlowControlDepth = %d\n", device_capabilities.VS20Caps.DynamicFlowControlDepth); |
||
351 | fwprintf (fp, L"device_capabilities.VS20Caps.NumTemps = %d\n", device_capabilities.VS20Caps.NumTemps); |
||
352 | fwprintf (fp, L"device_capabilities.VS20Caps.StaticFlowControlDepth = %d\n", device_capabilities.VS20Caps.StaticFlowControlDepth); |
||
353 | fwprintf (fp, L"device_capabilities.PS20Caps.Caps = %ul\n", device_capabilities.PS20Caps.Caps); |
||
354 | fwprintf (fp, L"device_capabilities.PS20Caps.DynamicFlowControlDepth = %d\n", device_capabilities.PS20Caps.DynamicFlowControlDepth); |
||
355 | fwprintf (fp, L"device_capabilities.PS20Caps.NumTemps = %d\n", device_capabilities.PS20Caps.NumTemps); |
||
356 | fwprintf (fp, L"device_capabilities.PS20Caps.StaticFlowControlDepth = %d\n", device_capabilities.PS20Caps.StaticFlowControlDepth); |
||
357 | fwprintf (fp, L"device_capabilities.PS20Caps.NumInstructionSlots = %d\n", device_capabilities.PS20Caps.NumInstructionSlots); |
||
358 | fwprintf (fp, L"device_capabilities.VertexTextureFilterCaps = %ul\n", device_capabilities.VertexTextureFilterCaps); |
||
359 | fwprintf (fp, L"device_capabilities.MaxVShaderInstructionsExecuted = %ul\n", device_capabilities.MaxVShaderInstructionsExecuted); |
||
360 | fwprintf (fp, L"device_capabilities.MaxPShaderInstructionsExecuted = %ul\n", device_capabilities.MaxPShaderInstructionsExecuted); |
||
361 | fwprintf (fp, L"device_capabilities.MaxVertexShader30InstructionSlots = %ul\n", device_capabilities.MaxVertexShader30InstructionSlots); |
||
362 | fwprintf (fp, L"device_capabilities.MaxPixelShader30InstructionSlots = %ul\n", device_capabilities.MaxPixelShader30InstructionSlots); |
||
363 | fwprintf (fp, L"========\n"); |
||
364 | fwprintf (fp, L"(guessed) behaviour_flags = %ul\n", behaviour_flags); |
||
365 | fwprintf (fp, L"========\n"); |
||
5 | pmbaty | 366 | fwprintf (fp, L"d3dpp.MultiSampleType = %d\n", d3dpp.MultiSampleType); |
367 | fwprintf (fp, L"d3dpp.MultiSampleQuality = %d\n", d3dpp.MultiSampleQuality); |
||
368 | fwprintf (fp, L"========\n"); |
||
2 | pmbaty | 369 | fwprintf (fp, L"d3d->CreateDevice() returned %d\n", (int) ret); |
370 | fclose (fp); |
||
371 | |||
372 | MessageBox (NULL, LOCALIZE (L"Error_GameCouldNotStartPleaseSendLogToAuthor"), LOCALIZE (L"Information"), MB_ICONINFORMATION | MB_OK); |
||
373 | } |
||
374 | else |
||
375 | MessageBox (NULL, LOCALIZE (L"Error_CouldNotWriteToLogFile"), LOCALIZE (L"FatalError"), MB_ICONERROR | MB_OK); |
||
376 | |||
11 | pmbaty | 377 | terminate_everything = true; // this is a fatal error |
1 | pmbaty | 378 | return (false); |
379 | } |
||
380 | |||
6 | pmbaty | 381 | // save the viewport's width and height |
382 | GetClientRect (hMainWnd, &viewport_rect); |
||
383 | initial_width = viewport_rect.right; |
||
384 | initial_height = viewport_rect.bottom; |
||
1 | pmbaty | 385 | |
6 | pmbaty | 386 | // now resize the window to its original size |
387 | MoveWindow (hMainWnd, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, false); |
||
388 | |||
1 | pmbaty | 389 | // set the texture parameters |
390 | d3ddev->SetSamplerState (0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); // wrap textures around their edges |
||
391 | d3ddev->SetSamplerState (0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); |
||
392 | d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); // and modulate their alpha with the material's alpha |
||
393 | d3ddev->SetTextureStageState (0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE); |
||
394 | d3ddev->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); |
||
395 | |||
396 | // enable the Z buffer |
||
397 | d3ddev->SetRenderState (D3DRS_ZENABLE, true); |
||
398 | |||
399 | // disable the stencil buffer |
||
400 | d3ddev->SetRenderState (D3DRS_STENCILENABLE, false); |
||
401 | |||
402 | // normalize the face normals (if we don't, scaling will cause problems with lighting computations) |
||
403 | d3ddev->SetRenderState (D3DRS_NORMALIZENORMALS, true); |
||
404 | |||
405 | // turn on alpha blending |
||
406 | d3ddev->SetRenderState (D3DRS_ALPHABLENDENABLE, true); |
||
407 | d3ddev->SetRenderState (D3DRS_BLENDOP, D3DBLENDOP_ADD); |
||
408 | d3ddev->SetRenderState (D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); |
||
409 | d3ddev->SetRenderState (D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); |
||
410 | |||
411 | // use all of the materials' light reflection properties |
||
412 | d3ddev->SetRenderState (D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL); |
||
413 | d3ddev->SetRenderState (D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL); |
||
414 | d3ddev->SetRenderState (D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL); |
||
415 | d3ddev->SetRenderState (D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL); |
||
416 | d3ddev->SetRenderState (D3DRS_COLORVERTEX, false); |
||
417 | |||
418 | // enable 3D lighting |
||
419 | d3ddev->SetRenderState (D3DRS_LIGHTING, true); |
||
420 | |||
421 | // open and parse the materials file and build the materials list |
||
422 | materials = NULL; |
||
423 | material_count = 0; |
||
424 | _wfopen_s (&fp, L"materials.cfg", L"r, ccs=UNICODE"); |
||
425 | if (fp != NULL) |
||
426 | { |
||
427 | // read line per line... |
||
428 | while (fgetws (line_buffer, WCHAR_SIZEOF (line_buffer), fp) != NULL) |
||
429 | { |
||
430 | // can we read a complete material line ? |
||
431 | if (swscanf_s (line_buffer, L"\"%[^\"]\" %f %f %f %f %f %f", material.name, WCHAR_SIZEOF (material.name), &material.ambient, &material.diffuse, &material.emissive, &material.specular, &material.shininess, &material.transparency) == 7) |
||
432 | { |
||
433 | materials = (material_t *) SAFE_realloc (materials, material_count, material_count + 1, sizeof (material_t), false); |
||
434 | memcpy (&materials[material_count], &material, sizeof (material_t)); |
||
435 | material_count++; // if so, append this new material to the materials array |
||
436 | } |
||
437 | } |
||
438 | fclose (fp); // finished, close the file |
||
439 | } |
||
440 | materials = (material_t *) SAFE_realloc (materials, material_count, material_count + 1, sizeof (material_t), false); |
||
441 | wcscpy_s (materials[material_count].name, WCHAR_SIZEOF (materials[material_count].name), default_materialname); |
||
442 | materials[material_count].ambient = 1.0f; |
||
443 | materials[material_count].diffuse = 1.0f; |
||
444 | materials[material_count].emissive = 0.0f; |
||
445 | materials[material_count].specular = 0.0f; |
||
446 | materials[material_count].shininess = 0.0f; |
||
447 | materials[material_count].transparency = 1.0f; |
||
448 | material_count++; // append a default material at the end of the array and we're all set |
||
449 | |||
450 | return (true); // finished |
||
451 | } |
||
452 | |||
453 | |||
454 | void Render_Shutdown (void) |
||
455 | { |
||
456 | // this function shuts down the Direct3D interfaces and releases the Direct3D COM objects |
||
457 | |||
458 | int array_index; |
||
459 | |||
460 | // close and release font data |
||
461 | SAFE_free ((void **) &fonts); |
||
462 | font_count = 0; |
||
463 | |||
464 | // close and release sprite data |
||
465 | SAFE_free ((void **) &sprites); |
||
466 | sprite_count = 0; |
||
467 | |||
468 | // close and release meshes data |
||
469 | if (meshes != NULL) |
||
470 | { |
||
471 | for (array_index = 0; array_index < mesh_count; array_index++) |
||
472 | { |
||
473 | if (meshes[array_index].d3dindices != NULL) |
||
474 | meshes[array_index].d3dindices->Release (); |
||
475 | meshes[array_index].d3dindices = NULL; |
||
476 | |||
477 | if (meshes[array_index].d3dvertices != NULL) |
||
478 | meshes[array_index].d3dvertices->Release (); |
||
479 | meshes[array_index].d3dvertices = NULL; |
||
480 | } |
||
481 | } |
||
482 | meshes = NULL; |
||
483 | mesh_count = 0; |
||
484 | |||
485 | // close and release texture data |
||
486 | if (textures != NULL) |
||
487 | { |
||
488 | for (array_index = 0; array_index < texture_count; array_index++) |
||
489 | if (textures[array_index].texture != NULL) |
||
490 | textures[array_index].texture->Release (); |
||
491 | SAFE_free ((void **) &textures); |
||
492 | } |
||
493 | texture_count = 0; |
||
494 | |||
495 | // close and release materials data |
||
496 | SAFE_free ((void **) &materials); |
||
497 | material_count = 0; |
||
498 | |||
499 | // close and release the 3D device |
||
500 | if (d3ddev != NULL) |
||
501 | d3ddev->Release (); |
||
502 | d3ddev = NULL; |
||
503 | |||
504 | // close and release Direct3D |
||
505 | if (d3d != NULL) |
||
506 | d3d->Release (); |
||
507 | d3d = NULL; |
||
508 | |||
509 | return; // finished |
||
510 | } |
||
511 | |||
512 | |||
513 | void Render_RenderFrame (scene_t *scene) |
||
514 | { |
||
515 | // this is the function used to render a single frame |
||
516 | |||
517 | static int framerate_value = 0; |
||
518 | static int framerate_count = 0; |
||
519 | static float framerate_time = 0; |
||
520 | |||
521 | D3DXMATRIX scaling_matrix; |
||
522 | D3DXMATRIX translation_matrix; |
||
523 | D3DXMATRIX view_matrix; // the view transform matrix |
||
524 | D3DXMATRIX projection_matrix; // the projection transform matrix |
||
525 | D3DLIGHT9 dxlight; |
||
526 | RECT rect; |
||
527 | float angle; |
||
528 | float sin_pitch; |
||
529 | float sin_yaw; |
||
530 | float cos_pitch; |
||
531 | float cos_yaw; |
||
532 | int light_index; |
||
533 | int object_index; |
||
534 | int cchistory_index; |
||
535 | int cchistory_index2; |
||
536 | int combined_width; |
||
537 | int combined_height; |
||
538 | light_t *light; |
||
539 | ccreply_t *ccreply; |
||
540 | sceneobject_t *sceneobject; |
||
541 | reflectedobject_t *reflectedobjects; // mallocated |
||
542 | int reflectedobject_count; |
||
543 | reflectedobject_t *otherobjects; // mallocated |
||
544 | int otherobject_count; |
||
545 | |||
11 | pmbaty | 546 | if (terminate_everything) |
547 | return; // consistency check |
||
548 | |||
1 | pmbaty | 549 | // get the device view port and save the actual width and height |
550 | GetClientRect (hMainWnd, &rect); |
||
551 | current_width = (float) rect.right; // they may differ from window width and window height |
||
552 | current_height = (float) rect.bottom; // because of title bars, menus, borders, etc. |
||
553 | |||
554 | // clear the back buffer, the Z buffer and the stencil buffer |
||
555 | d3ddev->Clear (0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_XRGB (0, 0, 0), 1.0f, 0); |
||
556 | d3ddev->BeginScene (); // begins the 3D scene |
||
557 | |||
558 | //////////////////////////////////////// |
||
559 | // Setup scene lights |
||
560 | |||
561 | // There are three types of light : ambient, diffuse and specular. |
||
562 | // Diffuse lights can be of type directional (sun), point (bulb) or spot (flashlight). |
||
563 | |||
564 | // The attenuation formula for point lights is : |
||
565 | // Atten = 1 / (att0 + (att1 * d) + (att2 * d²)) |
||
566 | |||
567 | // In spot lights, Phi is the outer cone angle. Theta is the inner cone angle. |
||
568 | |||
569 | // set the default lighting color |
||
570 | ambient_light = RGBACOLOR_TO_ARGBCOLOR (RGBACOLOR_FULLALPHA (theme->illum.ambient_light)); |
||
571 | d3ddev->SetRenderState (D3DRS_AMBIENT, ambient_light); |
||
572 | |||
573 | // for each light... |
||
574 | for (light_index = 0; light_index < theme->illum.light_count; light_index++) |
||
575 | { |
||
576 | light = &theme->illum.lights[light_index]; // quick access to light |
||
577 | |||
578 | memset (&dxlight, 0, sizeof (dxlight)); // clear out the dxlight struct for use |
||
579 | |||
580 | // set its type |
||
581 | if (light->type == LIGHT_DIRECTIONAL) |
||
582 | dxlight.Type = D3DLIGHT_DIRECTIONAL; // directional light (e.g, sun) |
||
583 | else if (light->type == LIGHT_POINT) |
||
584 | dxlight.Type = D3DLIGHT_POINT; // point light (e.g, light bulb) |
||
585 | else if (light->type == LIGHT_SPOT) |
||
586 | dxlight.Type = D3DLIGHT_SPOT; // spot light (e.g, flash light) |
||
587 | else |
||
588 | { |
||
589 | d3ddev->LightEnable (light_index, false); // unknown light ; turn off light #index |
||
590 | continue; // and proceed to the next one |
||
591 | } |
||
592 | |||
593 | // set its parameters |
||
594 | dxlight.Diffuse = D3DXCOLOR (RGBACOLOR_TO_ARGBCOLOR (RGBACOLOR_FULLALPHA (light->color))); |
||
595 | dxlight.Specular = D3DXCOLOR (0xffffffff); |
||
596 | dxlight.Position.x = light->pos_x; |
||
597 | dxlight.Position.y = light->pos_y; |
||
598 | dxlight.Position.z = light->pos_z; |
||
599 | dxlight.Direction.x = light->direction_x; |
||
600 | dxlight.Direction.y = light->direction_y; |
||
601 | dxlight.Direction.z = light->direction_z; |
||
602 | dxlight.Range = light->range; // light won't be computed after this distance |
||
603 | dxlight.Attenuation0 = light->attenuation_constant; // constant attenuation, see formula |
||
604 | dxlight.Attenuation1 = light->attenuation_proportional; // proportional attenuation, see formula |
||
605 | dxlight.Attenuation2 = light->attenuation_square; // square attenuation, see formula |
||
606 | dxlight.Phi = light->cone_outer * TO_RADIANS; // outer spot cone |
||
607 | dxlight.Theta = light->cone_inner * TO_RADIANS; // inner spot cone |
||
608 | |||
609 | d3ddev->SetLight (light_index, &dxlight); // send the light struct properties to light #index |
||
610 | d3ddev->LightEnable (light_index, true); // turn on light #index |
||
611 | } |
||
612 | |||
613 | //////////////////////////////// |
||
614 | // View transform |
||
615 | |||
616 | // compute the sine and cosine of the pitch component |
||
617 | angle = current_pitch * TO_RADIANS; |
||
618 | sin_pitch = sinf (angle); |
||
619 | cos_pitch = cosf (angle); |
||
620 | |||
621 | // compute the sine and cosine of the yaw component |
||
622 | angle = current_yaw * TO_RADIANS; |
||
623 | sin_yaw = sinf (angle); |
||
624 | cos_yaw = cosf (angle); |
||
625 | |||
626 | // build the camera position |
||
627 | camera_position.x = (float) -(cos_pitch * cos_yaw) * current_distance; |
||
628 | camera_position.y = (float) -(cos_pitch * sin_yaw) * current_distance; |
||
629 | camera_position.z = (float) sin_pitch * current_distance; |
||
630 | |||
631 | // set up a view matrix |
||
632 | D3DXMatrixLookAtLH (&view_matrix, |
||
633 | (D3DXVECTOR3 *) &camera_position, // camera position |
||
634 | (D3DXVECTOR3 *) &scene_center, // look-at position |
||
635 | (D3DXVECTOR3 *) &upwards_direction); // up direction |
||
636 | |||
637 | // tell Direct3D about our matrix |
||
638 | d3ddev->SetTransform (D3DTS_VIEW, &view_matrix); |
||
639 | |||
640 | ///////////////////////////////////// |
||
641 | // Projection transform |
||
642 | |||
643 | // set up a projection matrix |
||
644 | D3DXMatrixPerspectiveFovLH (&projection_matrix, |
||
645 | fov_value * TO_RADIANS, // field of view width |
||
646 | current_width / current_height, // aspect ratio |
||
647 | viewdist_near, viewdist_far); // view plane distances |
||
648 | |||
649 | // tell Direct3D about our matrix |
||
650 | d3ddev->SetTransform (D3DTS_PROJECTION, &projection_matrix); |
||
651 | |||
652 | ///////////////////////////////////// |
||
653 | // End of the transforms |
||
654 | |||
655 | // if we want it, enable specular lighting |
||
656 | d3ddev->SetRenderState (D3DRS_SPECULARENABLE, options.want_specularlighting); |
||
657 | |||
658 | // turn on texture filtering if needed |
||
659 | if (options.want_filtering) |
||
660 | { |
||
661 | d3ddev->SetSamplerState (0, D3DSAMP_MINFILTER, best_supported_filter); |
||
662 | d3ddev->SetSamplerState (0, D3DSAMP_MAGFILTER, best_supported_filter); |
||
663 | d3ddev->SetSamplerState (0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); |
||
664 | |||
665 | // turn on fullscene antialiasing only if capable |
||
5 | pmbaty | 666 | d3ddev->SetRenderState (D3DRS_MULTISAMPLEANTIALIAS, is_fsaa_supported); |
1 | pmbaty | 667 | } |
668 | else |
||
669 | { |
||
670 | d3ddev->SetSamplerState (0, D3DSAMP_MINFILTER, D3DTEXF_NONE); |
||
671 | d3ddev->SetSamplerState (0, D3DSAMP_MAGFILTER, D3DTEXF_NONE); |
||
672 | d3ddev->SetSamplerState (0, D3DSAMP_MIPFILTER, D3DTEXF_NONE); |
||
673 | |||
674 | // turn off fullscene antialiasing |
||
675 | d3ddev->SetRenderState (D3DRS_MULTISAMPLEANTIALIAS, false); |
||
676 | } |
||
677 | |||
678 | ///////////////////////////////////////////////////////////// |
||
679 | // draw the background elements. No need for a Z buffer here. |
||
680 | |||
681 | d3ddev->SetRenderState (D3DRS_ZENABLE, false); // disable depth testing |
||
682 | |||
683 | // draw the background sprite, if any |
||
684 | if (scene->background_spriteindex >= 0) |
||
685 | Render_DrawSprite (&sprites[scene->background_spriteindex], 0.0f, 0.0f, 100.0f, 100.0f, 0xFF); |
||
686 | |||
687 | // draw the table border |
||
688 | Render_DrawSceneObject (&scene->objects[0]); |
||
689 | Render_DrawSceneObject (&scene->objects[1]); |
||
690 | |||
691 | // draw the table and build the stencil buffer at the same time |
||
692 | d3ddev->SetRenderState (D3DRS_STENCILENABLE, true); // enable the stencil buffer (i.e. the "frame" for drawing table reflections) |
||
693 | d3ddev->SetRenderState (D3DRS_STENCILFUNC, D3DCMP_ALWAYS); // instruct how to fill it |
||
694 | d3ddev->SetRenderState (D3DRS_STENCILPASS, D3DSTENCILOP_INCRSAT); // instruct how to fill it |
||
695 | Render_DrawSceneObject (&scene->objects[2]); // draw the table squares in the stencil buffer |
||
696 | d3ddev->SetRenderState (D3DRS_STENCILENABLE, false); // finished drawing the stencil buffer |
||
697 | |||
698 | d3ddev->SetRenderState (D3DRS_ZENABLE, true); // re-enable depth testing |
||
699 | |||
700 | //////////////////////////////////////////////////////////// |
||
701 | // draw the scene objects and their reflections on the table |
||
702 | |||
703 | // start with the reflections if we want them, and if the table does it |
||
704 | if (options.want_reflections && (theme->reflection_alpha > 0)) |
||
705 | { |
||
706 | // build an array of reflected objects with their distances |
||
707 | reflectedobjects = NULL; |
||
708 | reflectedobject_count = 0; |
||
709 | otherobjects = NULL; |
||
710 | otherobject_count = 0; |
||
711 | |||
712 | // cycle through all parts and see which ones need to be reflected |
||
713 | for (object_index = 3; object_index < scene->object_count; object_index++) |
||
714 | { |
||
715 | sceneobject = &scene->objects[object_index]; // quick access to scene object |
||
716 | |||
717 | // is this object a mesh AND it's above the ground ? |
||
718 | if ((sceneobject->mesh_index != -1) && (sceneobject->z > 0)) |
||
719 | { |
||
720 | // yes it is. It thus needs to be reflected, so add it to the list |
||
721 | reflectedobjects = (reflectedobject_t *) SAFE_realloc (reflectedobjects, reflectedobject_count, reflectedobject_count + 1, sizeof (reflectedobject_t), false); |
||
722 | reflectedobjects[reflectedobject_count].object = sceneobject; // save scene object |
||
723 | reflectedobjects[reflectedobject_count].distance = DistanceToCamera (sceneobject->x, sceneobject->y, sceneobject->z); |
||
724 | reflectedobject_count++; // we have now one object more to reflect |
||
725 | } |
||
726 | else |
||
727 | { |
||
728 | // no it's not. It doesn't need to be reflected, so add it to the other list |
||
729 | otherobjects = (reflectedobject_t *) SAFE_realloc (otherobjects, otherobject_count, otherobject_count + 1, sizeof (reflectedobject_t), false); |
||
730 | otherobjects[otherobject_count].object = sceneobject; // save scene object |
||
731 | otherobjects[otherobject_count].distance = 0; |
||
732 | otherobject_count++; // we have now one object more to reflect |
||
733 | } |
||
734 | } |
||
735 | |||
736 | // now sort them from farthest to closest and draw them in this order |
||
737 | qsort (reflectedobjects, reflectedobject_count, sizeof (reflectedobject_t), SortReflectedObjects); |
||
738 | for (object_index = 0; object_index < reflectedobject_count; object_index++) |
||
739 | Render_DrawSceneObjectReflection (reflectedobjects[object_index].object); // draw the reflection |
||
740 | for (object_index = 0; object_index < reflectedobject_count; object_index++) |
||
741 | Render_DrawSceneObject (reflectedobjects[object_index].object); // and draw the objects afterwards |
||
742 | for (object_index = 0; object_index < otherobject_count; object_index++) |
||
743 | Render_DrawSceneObject (otherobjects[object_index].object); // finally, draw the non-reflected objects |
||
744 | |||
745 | SAFE_free ((void **) &reflectedobjects); // and free the reflected objects array |
||
746 | SAFE_free ((void **) &otherobjects); // and the non-reflected objects array |
||
747 | } |
||
748 | else |
||
749 | for (object_index = 3; object_index < scene->object_count; object_index++) |
||
750 | Render_DrawSceneObject (&scene->objects[object_index]); // else if no reflections, draw the objects, the Z-buffer will sort them |
||
751 | |||
752 | // draw the overlay texture if required |
||
753 | if (scene->overlay_spriteindex >= 0) |
||
754 | Render_DrawSprite (&sprites[scene->overlay_spriteindex], 0.0f, 0.0f, 100.0f, 100.0f, 0x4F); |
||
755 | |||
756 | /////////////// |
||
757 | // draw the GUI |
||
758 | |||
759 | // draw the arrows |
||
760 | DRAW_BUTTON_IF_NEEDED (scene->gui.larrow); // left arrow |
||
761 | DRAW_BUTTON_IF_NEEDED (scene->gui.rarrow); // right arrow |
||
762 | DRAW_TEXT_IF_NEEDED (scene->gui.arrow_text); // arrow text |
||
763 | |||
764 | if (want_framerate) |
||
765 | Render_wprintf (initial_width - 10, 10, 62, ALIGN_RIGHT, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex, D3DCOLOR_RGBA (255, 255, 255, 255), NULL, |
||
766 | L"%d textures\n" |
||
767 | L"%d meshes\n" |
||
768 | L"%d fonts\n" |
||
769 | L"%d sprites\n" |
||
770 | L"%d fps", texture_count, mesh_count, font_count, sprite_count, framerate_value); |
||
771 | |||
772 | // draw the other GUI buttons |
||
773 | DRAW_BUTTON_IF_NEEDED (scene->gui.chatbutton); // chat button |
||
774 | DRAW_BUTTON_IF_NEEDED (scene->gui.gamesbutton); // games button |
||
775 | DRAW_BUTTON_IF_NEEDED (scene->gui.peoplebutton); // people button |
||
776 | |||
777 | // does the parts pick line need to be displayed ? |
||
778 | if (scene->gui.is_partspick_displayed) |
||
779 | { |
||
780 | #define PARTSIZE_PCT (100.0f / 13.0f) |
||
781 | |||
782 | if (scene->gui.partspick_selectedpart == 'P') Render_DrawSprite (&sprites[theme->lastmovetarget_spriteindex], 0.0f * PARTSIZE_PCT, 0.0f, PARTSIZE_PCT, 11.0f, 0xFF); |
||
783 | else if (scene->gui.partspick_selectedpart == 'R') Render_DrawSprite (&sprites[theme->lastmovetarget_spriteindex], 1.0f * PARTSIZE_PCT, 0.0f, PARTSIZE_PCT, 11.0f, 0xFF); |
||
784 | else if (scene->gui.partspick_selectedpart == 'N') Render_DrawSprite (&sprites[theme->lastmovetarget_spriteindex], 2.0f * PARTSIZE_PCT, 0.0f, PARTSIZE_PCT, 11.0f, 0xFF); |
||
785 | else if (scene->gui.partspick_selectedpart == 'B') Render_DrawSprite (&sprites[theme->lastmovetarget_spriteindex], 3.0f * PARTSIZE_PCT, 0.0f, PARTSIZE_PCT, 11.0f, 0xFF); |
||
786 | else if (scene->gui.partspick_selectedpart == 'Q') Render_DrawSprite (&sprites[theme->lastmovetarget_spriteindex], 4.0f * PARTSIZE_PCT, 0.0f, PARTSIZE_PCT, 11.0f, 0xFF); |
||
787 | else if (scene->gui.partspick_selectedpart == 'K') Render_DrawSprite (&sprites[theme->lastmovetarget_spriteindex], 5.0f * PARTSIZE_PCT, 0.0f, PARTSIZE_PCT, 11.0f, 0xFF); |
||
788 | else if (scene->gui.partspick_selectedpart == ' ') Render_DrawSprite (&sprites[theme->lastmovetarget_spriteindex], 6.0f * PARTSIZE_PCT, 0.0f, PARTSIZE_PCT, 11.0f, 0xFF); |
||
789 | else if (scene->gui.partspick_selectedpart == 'k') Render_DrawSprite (&sprites[theme->lastmovetarget_spriteindex], 7.0f * PARTSIZE_PCT, 0.0f, PARTSIZE_PCT, 11.0f, 0xFF); |
||
790 | else if (scene->gui.partspick_selectedpart == 'q') Render_DrawSprite (&sprites[theme->lastmovetarget_spriteindex], 8.0f * PARTSIZE_PCT, 0.0f, PARTSIZE_PCT, 11.0f, 0xFF); |
||
791 | else if (scene->gui.partspick_selectedpart == 'b') Render_DrawSprite (&sprites[theme->lastmovetarget_spriteindex], 9.0f * PARTSIZE_PCT, 0.0f, PARTSIZE_PCT, 11.0f, 0xFF); |
||
792 | else if (scene->gui.partspick_selectedpart == 'n') Render_DrawSprite (&sprites[theme->lastmovetarget_spriteindex], 10.0f * PARTSIZE_PCT, 0.0f, PARTSIZE_PCT, 11.0f, 0xFF); |
||
793 | else if (scene->gui.partspick_selectedpart == 'r') Render_DrawSprite (&sprites[theme->lastmovetarget_spriteindex], 11.0f * PARTSIZE_PCT, 0.0f, PARTSIZE_PCT, 11.0f, 0xFF); |
||
794 | else if (scene->gui.partspick_selectedpart == 'p') Render_DrawSprite (&sprites[theme->lastmovetarget_spriteindex], 12.0f * PARTSIZE_PCT, 0.0f, PARTSIZE_PCT, 11.0f, 0xFF); |
||
795 | |||
796 | Render_DrawSprite (&sprites[theme->flatsprites[COLOR_WHITE][PART_PAWN]], 0.0f * PARTSIZE_PCT, 0, PARTSIZE_PCT, 11.0f, (((scene->gui.partspick_hoveredpart == 'P') || (scene->gui.partspick_selectedpart == 'P')) ? 0xFF : 0x7F)); // white pawn |
||
797 | Render_DrawSprite (&sprites[theme->flatsprites[COLOR_WHITE][PART_ROOK]], 1.0f * PARTSIZE_PCT, 0, PARTSIZE_PCT, 11.0f, (((scene->gui.partspick_hoveredpart == 'R') || (scene->gui.partspick_selectedpart == 'R')) ? 0xFF : 0x7F)); // white rook |
||
798 | Render_DrawSprite (&sprites[theme->flatsprites[COLOR_WHITE][PART_KNIGHT]], 2.0f * PARTSIZE_PCT, 0, PARTSIZE_PCT, 11.0f, (((scene->gui.partspick_hoveredpart == 'N') || (scene->gui.partspick_selectedpart == 'N')) ? 0xFF : 0x7F)); // white knight |
||
799 | Render_DrawSprite (&sprites[theme->flatsprites[COLOR_WHITE][PART_BISHOP]], 3.0f * PARTSIZE_PCT, 0, PARTSIZE_PCT, 11.0f, (((scene->gui.partspick_hoveredpart == 'B') || (scene->gui.partspick_selectedpart == 'B')) ? 0xFF : 0x7F)); // white bishop |
||
800 | Render_DrawSprite (&sprites[theme->flatsprites[COLOR_WHITE][PART_QUEEN]], 4.0f * PARTSIZE_PCT, 0, PARTSIZE_PCT, 11.0f, (((scene->gui.partspick_hoveredpart == 'Q') || (scene->gui.partspick_selectedpart == 'Q')) ? 0xFF : 0x7F)); // white queen |
||
801 | Render_DrawSprite (&sprites[theme->flatsprites[COLOR_WHITE][PART_KING]], 5.0f * PARTSIZE_PCT, 0, PARTSIZE_PCT, 11.0f, (((scene->gui.partspick_hoveredpart == 'K') || (scene->gui.partspick_selectedpart == 'K')) ? 0xFF : 0x7F)); // white king |
||
802 | Render_DrawSprite (&sprites[theme->lastmovesource_spriteindex], 6.0f * PARTSIZE_PCT, 0, PARTSIZE_PCT, 11.0f, (((scene->gui.partspick_hoveredpart == ' ') || (scene->gui.partspick_selectedpart == ' ')) ? 0xFF : 0x7F)); // erase mark |
||
803 | Render_DrawSprite (&sprites[theme->flatsprites[COLOR_BLACK][PART_KING]], 7.0f * PARTSIZE_PCT, 0, PARTSIZE_PCT, 11.0f, (((scene->gui.partspick_hoveredpart == 'k') || (scene->gui.partspick_selectedpart == 'k')) ? 0xFF : 0x7F)); // black king |
||
804 | Render_DrawSprite (&sprites[theme->flatsprites[COLOR_BLACK][PART_QUEEN]], 8.0f * PARTSIZE_PCT, 0, PARTSIZE_PCT, 11.0f, (((scene->gui.partspick_hoveredpart == 'q') || (scene->gui.partspick_selectedpart == 'q')) ? 0xFF : 0x7F)); // black queen |
||
805 | Render_DrawSprite (&sprites[theme->flatsprites[COLOR_BLACK][PART_BISHOP]], 9.0f * PARTSIZE_PCT, 0, PARTSIZE_PCT, 11.0f, (((scene->gui.partspick_hoveredpart == 'b') || (scene->gui.partspick_selectedpart == 'b')) ? 0xFF : 0x7F)); // black bishop |
||
806 | Render_DrawSprite (&sprites[theme->flatsprites[COLOR_BLACK][PART_KNIGHT]], 10.0f * PARTSIZE_PCT, 0, PARTSIZE_PCT, 11.0f, (((scene->gui.partspick_hoveredpart == 'n') || (scene->gui.partspick_selectedpart == 'n')) ? 0xFF : 0x7F)); // black knight |
||
807 | Render_DrawSprite (&sprites[theme->flatsprites[COLOR_BLACK][PART_ROOK]], 11.0f * PARTSIZE_PCT, 0, PARTSIZE_PCT, 11.0f, (((scene->gui.partspick_hoveredpart == 'r') || (scene->gui.partspick_selectedpart == 'r')) ? 0xFF : 0x7F)); // black rook |
||
808 | Render_DrawSprite (&sprites[theme->flatsprites[COLOR_BLACK][PART_PAWN]], 12.0f * PARTSIZE_PCT, 0, PARTSIZE_PCT, 11.0f, (((scene->gui.partspick_hoveredpart == 'p') || (scene->gui.partspick_selectedpart == 'p')) ? 0xFF : 0x7F)); // black pawn |
||
809 | |||
810 | #undef PARTSIZE_PCT |
||
811 | } |
||
812 | |||
813 | // draw GUI texts |
||
814 | DRAW_TEXT_IF_NEEDED (scene->gui.comment_text); // move comments |
||
815 | DRAW_TEXT_IF_NEEDED (scene->gui.history_text); // game history |
||
816 | DRAW_TEXT_IF_NEEDED (scene->gui.clock_text); // game clock |
||
817 | |||
818 | // draw the chatter channels text |
||
819 | Render_GetTextBoundaries (-1, chat_fontindex, L"a", &rect); |
||
820 | combined_height = rect.bottom; // get a string's height |
||
821 | |||
822 | // cycle through all the chat strings... |
||
823 | ccreply = NULL; |
||
824 | for (cchistory_index = 0; cchistory_index < scene->gui.cchistory_count; cchistory_index++) |
||
825 | { |
||
826 | ccreply = &scene->gui.cchistory[cchistory_index]; // quick access to CC reply |
||
827 | if ((ccreply->text[0] != 0) && (ccreply->arrival_time + 60.0f > current_time)) |
||
828 | break; // break at the first one that we should display |
||
829 | } |
||
830 | |||
831 | // have we some to display ? |
||
832 | if (cchistory_index < scene->gui.cchistory_count) |
||
833 | { |
||
834 | // first, get the remaining text's combined height |
||
835 | for (cchistory_index2 = cchistory_index; cchistory_index2 < scene->gui.cchistory_count; cchistory_index2++) |
||
836 | { |
||
837 | ccreply = &scene->gui.cchistory[cchistory_index2]; // quick access to CC reply |
||
838 | |||
839 | combined_width = 10; |
||
840 | Render_GetTextBoundaries (initial_width - combined_width, chat_fontindex, L"[] : ", &rect); |
||
841 | combined_width += rect.right; |
||
842 | Render_GetTextBoundaries (initial_width - combined_width, chat_fontindex, ccreply->channelname, &rect); |
||
843 | combined_width += rect.right; |
||
844 | Render_GetTextBoundaries (initial_width - combined_width, chat_fontindex, ccreply->nickname, &rect); |
||
845 | combined_width += rect.right; |
||
846 | Render_GetTextBoundaries (initial_width - combined_width, chat_fontindex, ccreply->text, &rect); |
||
847 | combined_height += rect.bottom; // add this string's height |
||
848 | } |
||
849 | |||
850 | // now for each string remaining... |
||
851 | for (; cchistory_index < scene->gui.cchistory_count; cchistory_index++) |
||
852 | { |
||
853 | ccreply = &scene->gui.cchistory[cchistory_index]; // quick access to CC reply |
||
854 | rect.right = 10; |
||
855 | PRINT_CCREPLY (ccreply); // print CC reply on screen |
||
856 | combined_height -= (rect.bottom - rect.top); // draw it from top of zone to bottom |
||
857 | } |
||
858 | } |
||
859 | |||
860 | // are we online ? |
||
861 | if (Player_FindByType (PLAYER_INTERNET) != NULL) |
||
862 | { |
||
863 | ccreply = &scene->gui.entered_ccreply; |
||
864 | if (!scene->gui.is_entering_text) |
||
865 | ccreply->color = RGBACOLOR_SETALPHA (ccreply->color, RGBACOLOR_ALPHA (ccreply->color) / 6); // if there's no text being entered, fade CC reply a lot |
||
866 | |||
867 | rect.right = 10; |
||
868 | PRINT_CCREPLY (ccreply); // print CC reply on screen |
||
869 | } |
||
870 | |||
871 | // print GUI texts |
||
872 | DRAW_TEXT_IF_NEEDED (scene->gui.turn_text); // player turn's text |
||
873 | DRAW_TEXT_IF_NEEDED (scene->gui.central_text); // central notification zone text |
||
874 | |||
875 | // if needed, print the spinning wheel |
||
876 | if (scene->gui.want_spinwheel) |
||
877 | Render_DrawSprite (&sprites[spinner_spriteindex[(int) (10.0f * current_time) % 12]], 47.0f, 46.0f, 6.0f, 8.0f, 255); |
||
878 | |||
879 | // are we in demo mode ? if so, display the program name as a watermark |
||
14 | pmbaty | 880 | if (!is_registered) |
18 | pmbaty | 881 | Render_wprintf (initial_width / 2, initial_height * 3 / 5, -1, ALIGN_CENTER, ALIGN_CENTER, ALIGN_CENTER, chat_fontindex, |
882 | D3DCOLOR_RGBA (255, 255, 255, 255), |
||
14 | pmbaty | 883 | NULL, PROGRAM_NAME L"%s - " PROGRAM_URL L"\n- %d:%02d -", LOCALIZE (L"EvaluationMode"), (int) (DEMO_TIMEOUT - current_time) / 60, (int) (DEMO_TIMEOUT - current_time) % 60); |
1 | pmbaty | 884 | |
885 | // end 3D rendering on the back buffer |
||
886 | ////////////////////////////////////// |
||
887 | |||
888 | d3ddev->EndScene (); // ends the 3D scene |
||
889 | d3ddev->Present (NULL, NULL, NULL, NULL); // displays the created frame on the screen |
||
890 | |||
891 | // update the frame rate |
||
892 | if (framerate_time < current_time) |
||
893 | { |
||
894 | framerate_value = framerate_count; |
||
895 | framerate_count = 0; |
||
896 | framerate_time = current_time + 1.0f; |
||
897 | } |
||
898 | framerate_count++; // one frame more elapsed |
||
899 | |||
900 | return; // finished |
||
901 | } |
||
902 | |||
903 | |||
904 | int Render_LoadMesh (const wchar_t *fmt, ...) |
||
905 | { |
||
906 | // this function appends a new mesh in the global meshes buffer and returns its index |
||
907 | |||
908 | static wchar_t meshfile_pathname[MAX_PATH]; |
||
909 | unsigned long hash; |
||
910 | mesh_t *mesh; |
||
911 | int mesh_index; |
||
912 | va_list argptr; |
||
913 | |||
914 | // concatenate all the arguments in one string |
||
915 | va_start (argptr, fmt); |
||
916 | wvsprintf (meshfile_pathname, fmt, argptr); |
||
917 | va_end (argptr); |
||
918 | |||
919 | // resolve wildcards and get content hash |
||
920 | ResolveWildcard (meshfile_pathname, L".obj"); |
||
921 | hash = HashFile (meshfile_pathname); |
||
922 | |||
923 | // now cycle through all our loaded meshes and see if it's already loaded |
||
924 | for (mesh_index = 0; mesh_index < mesh_count; mesh_index++) |
||
925 | if (meshes[mesh_index].hash == hash) |
||
926 | return (mesh_index); // if we can find it, return its index so as not to load it twice |
||
927 | |||
928 | // reallocate space to hold one mesh more |
||
929 | meshes = (mesh_t *) SAFE_realloc (meshes, mesh_count, mesh_count + 1, sizeof (mesh_t), false); |
||
930 | mesh = &meshes[mesh_count]; // quick access to the mesh we'll be working on |
||
931 | |||
932 | // load the mesh |
||
933 | if (!Render_LoadMesh_Obj (mesh, meshfile_pathname)) |
||
934 | return (-1); // bomb out on error |
||
935 | |||
936 | mesh->hash = hash; // save the hash |
||
937 | mesh_count++; // we know now one mesh more |
||
938 | return (mesh_count - 1); // return its index |
||
939 | } |
||
940 | |||
941 | |||
942 | int Render_LoadTexture (const wchar_t *fmt, ...) |
||
943 | { |
||
944 | // this function appends a new texture in the global textures buffer and returns its index |
||
945 | |||
946 | D3DSURFACE_DESC texture_description; |
||
947 | wchar_t texturefile_pathname[MAX_PATH]; |
||
948 | static wchar_t *filename; |
||
949 | unsigned long hash; |
||
950 | int texture_index; |
||
951 | va_list argptr; |
||
952 | |||
953 | // concatenate all the arguments in one string |
||
954 | va_start (argptr, fmt); |
||
955 | wvsprintf (texturefile_pathname, fmt, argptr); |
||
956 | va_end (argptr); |
||
957 | |||
958 | // resolve wildcards and get content hash |
||
959 | ResolveWildcard (texturefile_pathname, L".dds|.jpg|.jpeg|.png|.tga|.bmp"); |
||
960 | hash = HashFile (texturefile_pathname); |
||
961 | |||
962 | // now cycle through all our loaded textures and see if it's already loaded |
||
963 | for (texture_index = 0; texture_index < texture_count; texture_index++) |
||
964 | if (textures[texture_index].hash == hash) |
||
965 | return (texture_index); // if we can find it, return its index so as not to load it twice |
||
966 | |||
967 | // reallocate space to hold one texture more |
||
968 | textures = (texture_t *) SAFE_realloc (textures, texture_count, texture_count + 1, sizeof (texture_t), false); |
||
969 | |||
970 | // ask Direct3D to prepare texture data |
||
971 | if (FAILED (D3DXCreateTextureFromFile (d3ddev, texturefile_pathname, &textures[texture_count].texture))) |
||
972 | { |
||
973 | MessageBox (NULL, LOCALIZE (L"Error_UnableToAddTextureD3DXCreateTextureFromFileFailed"), LOCALIZE (L"FatalError"), MB_ICONERROR | MB_OK); |
||
11 | pmbaty | 974 | terminate_everything = true; // this is a fatal error |
1 | pmbaty | 975 | return (-1); // bomb out on error |
976 | } |
||
977 | |||
978 | // get info on the newly loaded texture such as size etc. |
||
979 | textures[texture_count].texture->GetLevelDesc (0, &texture_description); |
||
980 | textures[texture_count].width = texture_description.Width; // save texture width (as loaded) |
||
981 | textures[texture_count].height = texture_description.Height; // save texture height (as loaded) |
||
982 | textures[texture_count].hash = hash; // save its hash |
||
983 | texture_count++; // we know now one texture more |
||
984 | return (texture_count - 1); // return its index |
||
985 | } |
||
986 | |||
987 | |||
988 | int Render_LoadFont (const wchar_t *font_name, int font_size, bool is_bold, bool is_italic) |
||
989 | { |
||
990 | // this function appends a new font in the global fonts buffer and returns its index |
||
991 | |||
992 | unsigned long pathname_hash; |
||
993 | int font_index; |
||
994 | |||
995 | // first, get the hash of the requested pathname (include font size and weight parameters) |
||
996 | pathname_hash = HashString (font_name); |
||
997 | pathname_hash += 3 * (unsigned long) font_size + 2 * (unsigned long) is_bold + (unsigned long) is_italic; |
||
998 | |||
999 | // now cycle through all our loaded fonts and see if it's already loaded |
||
1000 | for (font_index = 0; font_index < font_count; font_index++) |
||
1001 | if (fonts[font_index].pathname_hash == pathname_hash) |
||
1002 | return (font_index); // if we can find it, return its index so as not to load it twice |
||
1003 | |||
1004 | // reallocate space to hold one font more |
||
1005 | fonts = (font_t *) SAFE_realloc (fonts, font_count, font_count + 1, sizeof (font_t), false); |
||
1006 | |||
1007 | // create a Direct3D font object and record font data |
||
1008 | if (FAILED (D3DXCreateFont (d3ddev, font_size, // font height |
||
12 | pmbaty | 1009 | 2 * font_size / 5, // font width |
1 | pmbaty | 1010 | (is_bold ? FW_BOLD : FW_NORMAL), // font weight (bold, etc) |
1011 | 0, // miplevels |
||
1012 | is_italic, // is italic |
||
1013 | DEFAULT_CHARSET, // charset |
||
1014 | OUT_DEFAULT_PRECIS, // precision |
||
1015 | CLEARTYPE_QUALITY, // font quality (antialiased or not) |
||
1016 | DEFAULT_PITCH | FF_DONTCARE, // font family |
||
1017 | font_name, // font name |
||
1018 | &fonts[font_count].font))) // and a pointer that will receive the font |
||
1019 | { |
||
1020 | MessageBox (NULL, LOCALIZE (L"Error_UnableToAddFontD3DXCreateFontFailed"), LOCALIZE (L"FatalError"), MB_ICONERROR | MB_OK); |
||
11 | pmbaty | 1021 | terminate_everything = true; // this is a fatal error |
1 | pmbaty | 1022 | return (-1); // bomb out on error |
1023 | } |
||
1024 | |||
1025 | fonts[font_count].pathname_hash = pathname_hash; // save its hash |
||
1026 | font_count++; // we know now one font more |
||
1027 | return (font_count - 1); // return its index |
||
1028 | } |
||
1029 | |||
1030 | |||
1031 | int Render_LoadSprite (const wchar_t *fmt, ...) |
||
1032 | { |
||
1033 | // this function appends a new sprite in the global sprites buffer and returns its index |
||
1034 | |||
1035 | wchar_t spritefile_pathname[MAX_PATH]; |
||
1036 | unsigned long hash; |
||
1037 | int sprite_index; |
||
1038 | va_list argptr; |
||
1039 | |||
1040 | // concatenate all the arguments in one string |
||
1041 | va_start (argptr, fmt); |
||
1042 | wvsprintf (spritefile_pathname, fmt, argptr); |
||
1043 | va_end (argptr); |
||
1044 | |||
1045 | // resolve wildcards and get content hash |
||
1046 | ResolveWildcard (spritefile_pathname, L".dds|.jpg|.jpeg|.png|.tga|.bmp"); |
||
1047 | hash = HashFile (spritefile_pathname); |
||
1048 | |||
1049 | // now cycle through all our loaded sprites and see if it's already loaded |
||
1050 | for (sprite_index = 0; sprite_index < sprite_count; sprite_index++) |
||
1051 | if (sprites[sprite_index].hash == hash) |
||
1052 | return (sprite_index); // if we can find it, return its index so as not to load it twice |
||
1053 | |||
1054 | // reallocate space to hold one sprite more |
||
1055 | sprites = (sprite_t *) SAFE_realloc (sprites, sprite_count, sprite_count + 1, sizeof (sprite_t), false); |
||
1056 | |||
1057 | // ask Direct3D to prepare texture data |
||
1058 | if (FAILED (D3DXCreateSprite (d3ddev, &sprites[sprite_count].sprite))) |
||
1059 | { |
||
1060 | MessageBox (NULL, LOCALIZE (L"Error_UnableToAddSpriteD3DXCreateSpriteFailed"), LOCALIZE (L"FatalError"), MB_ICONERROR | MB_OK); |
||
11 | pmbaty | 1061 | terminate_everything = true; // this is a fatal error |
1 | pmbaty | 1062 | return (-1); // bomb out on error |
1063 | } |
||
1064 | sprites[sprite_count].texture_index = Render_LoadTexture (spritefile_pathname); // register and save sprite texture |
||
1065 | sprites[sprite_count].hash = hash; // save its hash |
||
1066 | |||
1067 | sprite_count++; // we know now one sprite more |
||
1068 | return (sprite_count - 1); // return its index |
||
1069 | } |
||
1070 | |||
1071 | |||
1072 | int Render_MaterialIndexOf (const wchar_t *material_name) |
||
1073 | { |
||
1074 | // this function returns the index of the material in the global materials array which has the specified name |
||
1075 | |||
1076 | int material_index; |
||
1077 | |||
1078 | // cycle through all materials and look whether one with the specified name exists |
||
1079 | for (material_index = 0; material_index < material_count; material_index++) |
||
1080 | if (_wcsicmp (materials[material_index].name, material_name) == 0) |
||
1081 | return (material_index); // if we find one, return its index |
||
1082 | |||
1083 | return (material_count - 1); // else return the index of the last material in list, which is the default material |
||
1084 | } |
||
1085 | |||
1086 | |||
1087 | void Render_MouseToFloor (short mouse_x, short mouse_y, float *floor_x, float *floor_y) |
||
1088 | { |
||
1089 | // this function converts a mouse coordinates into floor coordinates by doing vector |
||
1090 | // projection on the floor plane from the eyepoint of the camera |
||
1091 | |||
1092 | static D3DXPLANE floor_plane; |
||
1093 | static bool is_planefound = false; |
||
1094 | |||
1095 | D3DXMATRIX projection_matrix; |
||
1096 | D3DXMATRIX view_matrix; |
||
1097 | D3DXMATRIX invertedview_matrix; |
||
1098 | float mouse_pitch; |
||
1099 | float mouse_yaw; |
||
1100 | vector_t v_lookat; |
||
1101 | vector_t v_intersection; |
||
1102 | |||
1103 | // find the floor plane (only do it once) |
||
1104 | if (!is_planefound) |
||
1105 | { |
||
1106 | D3DXPlaneFromPointNormal (&floor_plane, (D3DXVECTOR3 *) &scene_center, (D3DXVECTOR3 *) &upwards_direction); |
||
1107 | is_planefound = true; // once and for all, as this plane will never change |
||
1108 | } |
||
1109 | |||
1110 | // get the current projection and view matrices, and invert the view matrix |
||
1111 | d3ddev->GetTransform (D3DTS_PROJECTION, &projection_matrix); |
||
1112 | d3ddev->GetTransform (D3DTS_VIEW, &view_matrix); |
||
1113 | D3DXMatrixInverse (&invertedview_matrix, NULL, &view_matrix); |
||
1114 | |||
1115 | // convert the mouse coordinates to relative pitch and yaw |
||
1116 | mouse_pitch = (((mouse_x * 2.0f) / current_width) - 1) / projection_matrix._11; |
||
1117 | mouse_yaw = -(((mouse_y * 2.0f) / current_height) - 1) / projection_matrix._22; |
||
1118 | |||
1119 | // now build a matrix that will describe the mouse direction vector, add it to the camera position, and make it 200 times longer |
||
1120 | v_lookat.x = camera_position.x + (mouse_pitch * invertedview_matrix._11 + mouse_yaw * invertedview_matrix._21 + invertedview_matrix._31) * 200.0f; |
||
1121 | v_lookat.y = camera_position.y + (mouse_pitch * invertedview_matrix._12 + mouse_yaw * invertedview_matrix._22 + invertedview_matrix._32) * 200.0f; |
||
1122 | v_lookat.z = camera_position.z + (mouse_pitch * invertedview_matrix._13 + mouse_yaw * invertedview_matrix._23 + invertedview_matrix._33) * 200.0f; |
||
1123 | |||
1124 | // and the intersection point with our ray |
||
1125 | D3DXPlaneIntersectLine ((D3DXVECTOR3 *) &v_intersection, &floor_plane, (D3DXVECTOR3 *) &camera_position, (D3DXVECTOR3 *) &v_lookat); |
||
1126 | |||
1127 | // now fill the return values |
||
1128 | *floor_x = v_intersection.x; |
||
1129 | *floor_y = v_intersection.y; |
||
1130 | |||
1131 | return; // finished |
||
1132 | } |
||
1133 | |||
1134 | |||
1135 | bool Render_IsMouseInBox (short mouse_x, short mouse_y, float x_percent, float y_percent, float width_percent, float height_percent) |
||
1136 | { |
||
1137 | // helper function that returns whether the mouse coordinates are inside a given square |
||
1138 | |||
1139 | float mousex_percent; |
||
1140 | float mousey_percent; |
||
1141 | |||
1142 | // compute mouse coordinates in percents |
||
1143 | mousex_percent = (float) (mouse_x * 100) / current_width; |
||
1144 | mousey_percent = (float) (mouse_y * 100) / current_height; |
||
1145 | |||
1146 | return ((mousex_percent >= x_percent) && (mousex_percent <= x_percent + width_percent) |
||
1147 | && (mousey_percent >= y_percent) && (mousey_percent <= y_percent + height_percent)); |
||
1148 | } |
||
1149 | |||
1150 | |||
1151 | static bool Render_LoadMesh_Obj (mesh_t *mesh, const wchar_t *objfile_pathname) |
||
1152 | { |
||
1153 | // this function loads a mesh from a Wavefront Object file (.obj) |
||
1154 | |||
1155 | #define OBJ_INCREASE_OR_RESIZE(count,maxcount,increment,arrayptr,type,erase) { \ |
||
1156 | (count)++; \ |
||
1157 | if ((count) == (maxcount)) \ |
||
1158 | { \ |
||
1159 | (arrayptr) = (type *) SAFE_realloc ((arrayptr), (maxcount), (maxcount) + (increment), sizeof (type), erase); \ |
||
1160 | (maxcount) += (increment); \ |
||
1161 | } \ |
||
1162 | } |
||
1163 | #define OBJ_CONVERT_INDEX(element,current_element_count) if ((element) < 0) (element) = (current_element_count) + (element); else (element)--; |
||
1164 | #define OBJ_GET_EXISTING_INDEX_OR_APPEND_VERTEX(uniquevertex) \ |
||
1165 | { \ |
||
1166 | hash = &hashtable[*((unsigned long *) &obj.vs[(uniquevertex).iv].x) & 0xFF]; \ |
||
1167 | for (vertex_index = 0; vertex_index < hash->count; vertex_index++) \ |
||
1168 | if ((memcmp (&vertices[hash->indices[vertex_index]].position, &obj.vs[(uniquevertex).iv], sizeof (vector_t)) == 0) \ |
||
1169 | && (memcmp (&vertices[hash->indices[vertex_index]].normal, &obj.ns[(uniquevertex).in], sizeof (vector_t)) == 0) \ |
||
1170 | && (memcmp (&vertices[hash->indices[vertex_index]].texcoord, &obj.tcs[(uniquevertex).itc], sizeof (texcoord_t)) == 0)) \ |
||
1171 | break; \ |
||
1172 | if (vertex_index == hash->count) \ |
||
1173 | { \ |
||
1174 | vertex_index = mesh->vertice_count; \ |
||
1175 | current_vertex = &vertices[vertex_index]; \ |
||
1176 | memset (current_vertex, 0, sizeof (vertex_t)); \ |
||
1177 | memcpy (¤t_vertex->position, &obj.vs[(uniquevertex).iv], sizeof (vector_t)); \ |
||
1178 | if ((uniquevertex).in > -1) \ |
||
1179 | memcpy (¤t_vertex->normal, &obj.ns[(uniquevertex).in], sizeof (vector_t)); \ |
||
1180 | if ((uniquevertex).itc > -1) \ |
||
1181 | memcpy (¤t_vertex->texcoord, &obj.tcs[(uniquevertex).itc], sizeof (texcoord_t)); \ |
||
1182 | hash = &hashtable[*((unsigned long *) ¤t_vertex->normal.x) & 0xFF]; \ |
||
1183 | hash->indices = (long *) SAFE_realloc (hash->indices, hash->count, hash->count + 1, sizeof (long), false); \ |
||
1184 | hash->indices[hash->count] = vertex_index; \ |
||
1185 | hash->count++; \ |
||
1186 | mesh->vertice_count++; \ |
||
1187 | } \ |
||
1188 | else \ |
||
1189 | vertex_index = hash->indices[vertex_index]; \ |
||
1190 | } |
||
1191 | |||
1192 | typedef struct obj_uniquevertex_s { long iv, in, itc; } obj_uniquevertex_t; |
||
1193 | typedef struct obj_face_s { obj_uniquevertex_t v1, v2, v3; } obj_face_t; |
||
1194 | typedef struct obj_hashbucket_s { int count; long *indices; /* mallocated */ } obj_hashbucket_t; |
||
1195 | typedef struct objfile_s |
||
1196 | { |
||
1197 | vector_t *vs; long v_count; long v_maxcount; // array mallocated to v_maxcount |
||
1198 | vector_t *ns; long n_count; long n_maxcount; // array mallocated to n_maxcount |
||
1199 | texcoord_t *tcs; long tc_count; long tc_maxcount; // array mallocated to tc_maxcount |
||
1200 | obj_face_t *fs; long f_count; long f_maxcount; // array mallocated to f_maxcount |
||
1201 | } objfile_t; |
||
1202 | |||
1203 | static obj_hashbucket_t hashtable[256]; |
||
1204 | objfile_t obj; |
||
1205 | obj_hashbucket_t *hash; |
||
1206 | obj_face_t *f; |
||
1207 | vertex_t *vertices; // mallocated |
||
1208 | unsigned long *indices; // mallocated |
||
1209 | vertex_t *current_vertex; |
||
81 | pmbaty | 1210 | unsigned long file_size; |
1 | pmbaty | 1211 | char *filedata; // mallocated |
1212 | char *fileptr; |
||
1213 | int vertex_index; |
||
1214 | int array_index; |
||
1215 | void *ptr_to; |
||
1216 | FILE *fp; |
||
1217 | |||
1218 | // open the mesh file and read it as a whole |
||
1219 | _wfopen_s (&fp, objfile_pathname, L"rb"); |
||
1220 | if (fp == NULL) |
||
1221 | return (false); // bomb out on error |
||
81 | pmbaty | 1222 | fseek (fp, 0, SEEK_END); // seek at end of file... |
1223 | file_size = ftell (fp); // ...read file length... |
||
1224 | fseek (fp, 0, SEEK_SET); // and rewind |
||
1225 | filedata = (char *) SAFE_malloc (file_size, sizeof (char), false); // mallocate space for data |
||
1226 | fread (filedata, file_size, 1, fp); // read file as a whole |
||
1 | pmbaty | 1227 | fclose (fp); // file is read, close it |
1228 | |||
1229 | // allocate space for an arbitrary amount of vertices, texture coordinates, normals and faces |
||
1230 | memset (&obj, 0, sizeof (obj)); |
||
1231 | obj.v_maxcount = 10000; obj.vs = (vector_t *) SAFE_malloc (obj.v_maxcount, sizeof (vector_t), false); |
||
1232 | obj.n_maxcount = 10000; obj.ns = (vector_t *) SAFE_malloc (obj.n_maxcount, sizeof (vector_t), false); |
||
1233 | obj.tc_maxcount = 10000; obj.tcs = (texcoord_t *) SAFE_malloc (obj.tc_maxcount, sizeof (texcoord_t), false); |
||
1234 | obj.f_maxcount = 5000; obj.fs = (obj_face_t *) SAFE_malloc (obj.f_maxcount, sizeof (obj_face_t), true); // zero out the faces array (IMPORTANT !) |
||
1235 | |||
1236 | // read file line per line... |
||
1237 | fileptr = filedata - 1; // start parsing line after line |
||
1238 | while (fileptr != NULL) |
||
1239 | { |
||
1240 | fileptr++; // skip the line feed (or reach the first character, if it's the first pass) |
||
1241 | |||
1242 | // is it a vertex-related line ? |
||
1243 | if (fileptr[0] == L'v') |
||
1244 | { |
||
1245 | // is it a vertex, a normal or a texture coordinate ? |
||
1246 | if ((fileptr[1] == L' ') && (sscanf_s (&fileptr[2], "%f %f %f", &obj.vs[obj.v_count].x, &obj.vs[obj.v_count].y, &obj.vs[obj.v_count].z) == 3)) |
||
1247 | OBJ_INCREASE_OR_RESIZE (obj.v_count, obj.v_maxcount, 10000, obj.vs, vector_t, false) // one vertex more has been read |
||
1248 | else if ((fileptr[1] == L'n') && (sscanf_s (&fileptr[3], "%f %f %f", &obj.ns[obj.n_count].x, &obj.ns[obj.n_count].y, &obj.ns[obj.n_count].z) == 3)) |
||
1249 | OBJ_INCREASE_OR_RESIZE (obj.n_count, obj.n_maxcount, 10000, obj.ns, vector_t, false) // one normal more has been read |
||
1250 | else if ((fileptr[1] == L't') && (sscanf_s (&fileptr[3], "%f %f", &obj.tcs[obj.tc_count].u, &obj.tcs[obj.tc_count].v) == 2)) |
||
1251 | OBJ_INCREASE_OR_RESIZE (obj.tc_count, obj.tc_maxcount, 10000, obj.tcs, texcoord_t, false) // one texture coordinate more has been read |
||
1252 | } |
||
1253 | |||
1254 | // else is it a face-related line ? |
||
1255 | else if (fileptr[0] == L'f') |
||
1256 | { |
||
1257 | // get a quick pointer to current face (note: it's been already blanked out by malloc()) |
||
1258 | f = &obj.fs[obj.f_count]; |
||
1259 | |||
1260 | // is it a face with normals, a face without normals or a face without normals and texture coordinates ? |
||
1261 | if ((sscanf_s (&fileptr[2], "%d/%d/%d %d/%d/%d %d/%d/%d", &f->v1.iv, &f->v1.itc, &f->v1.in, &f->v2.iv, &f->v2.itc, &f->v2.in, &f->v3.iv, &f->v3.itc, &f->v3.in) == 9) |
||
1262 | || (sscanf_s (&fileptr[2], "%d/%d %d/%d %d/%d", &f->v3.iv, &f->v3.itc, &f->v2.iv, &f->v2.itc, &f->v3.iv, &f->v3.itc) == 6) |
||
1263 | || (sscanf_s (&fileptr[2], "%d %d %d", &f->v3.iv, &f->v2.iv, &f->v3.iv) == 6)) |
||
1264 | { |
||
1265 | OBJ_CONVERT_INDEX (f->v1.iv, obj.v_count); |
||
1266 | OBJ_CONVERT_INDEX (f->v1.in, obj.n_count); // if no normal could be read, its index will be converted from 0 to -1 |
||
1267 | OBJ_CONVERT_INDEX (f->v1.itc, obj.tc_count); // if no texcoord could be read, its index will be converted from 0 to -1 |
||
1268 | OBJ_CONVERT_INDEX (f->v2.iv, obj.v_count); |
||
1269 | OBJ_CONVERT_INDEX (f->v2.in, obj.n_count); // if no normal could be read, its index will be converted from 0 to -1 |
||
1270 | OBJ_CONVERT_INDEX (f->v2.itc, obj.tc_count); // if no texcoord could be read, its index will be converted from 0 to -1 |
||
1271 | OBJ_CONVERT_INDEX (f->v3.iv, obj.v_count); |
||
1272 | OBJ_CONVERT_INDEX (f->v3.in, obj.n_count); // if no normal could be read, its index will be converted from 0 to -1 |
||
1273 | OBJ_CONVERT_INDEX (f->v3.itc, obj.tc_count); // if no texcoord could be read, its index will be converted from 0 to -1 |
||
1274 | OBJ_INCREASE_OR_RESIZE (obj.f_count, obj.f_maxcount, 5000, obj.fs, obj_face_t, true) // one face more has been read |
||
1275 | } |
||
1276 | } |
||
1277 | |||
1278 | fileptr = strchr (fileptr, '\n'); // proceed to next line |
||
1279 | } |
||
1280 | |||
1281 | // now build our final vertex and index list |
||
1282 | vertices = (vertex_t *) SAFE_malloc (3 * obj.f_count, sizeof (vertex_t), false); // mallocate for the max number of vertices we can have |
||
1283 | indices = (unsigned long *) SAFE_malloc (3 * obj.f_count, sizeof (unsigned long), false); // mallocate for the right amount of indices |
||
1284 | |||
1285 | // t3h mighty l00p ^^ (builds vertex and index buffers) |
||
1286 | memset (hashtable, 0, sizeof (hashtable)); // wipe out the hashtable |
||
1287 | mesh->vertice_count = 0; // start with an unoptimized list |
||
1288 | for (array_index = 0; array_index < obj.f_count; array_index++) |
||
1289 | { |
||
1290 | f = &obj.fs[array_index]; // quick access to current face |
||
1291 | OBJ_GET_EXISTING_INDEX_OR_APPEND_VERTEX (f->v1); |
||
1292 | indices[3 * array_index + 0] = vertex_index; |
||
1293 | OBJ_GET_EXISTING_INDEX_OR_APPEND_VERTEX (f->v2); |
||
1294 | indices[3 * array_index + 1] = vertex_index; |
||
1295 | OBJ_GET_EXISTING_INDEX_OR_APPEND_VERTEX (f->v3); |
||
1296 | indices[3 * array_index + 2] = vertex_index; |
||
1297 | } |
||
1298 | |||
1299 | // now create a correctly-sized DirectX vertex buffer and populate it |
||
1300 | mesh->vertex_format = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1; |
||
1301 | mesh->vertice_size = sizeof (vertex_t); |
||
1302 | d3ddev->CreateVertexBuffer (mesh->vertice_count * mesh->vertice_size, // length |
||
1303 | D3DUSAGE_DYNAMIC, // usage |
||
1304 | mesh->vertex_format, // vertex format |
||
1305 | D3DPOOL_DEFAULT, // pool type |
||
1306 | &mesh->d3dvertices, // pointer to the vertex buffer pointer |
||
1307 | NULL); // shared handle |
||
1308 | mesh->d3dvertices->Lock (0, mesh->vertice_count * mesh->vertice_size, &ptr_to, D3DLOCK_DISCARD); |
||
1309 | memcpy (ptr_to, vertices, mesh->vertice_count * mesh->vertice_size); |
||
1310 | mesh->d3dvertices->Unlock (); |
||
1311 | |||
1312 | // create a correctly-sized DirectX index buffer and populate it |
||
1313 | mesh->is_indexed = true; // remember that we're building an index buffer |
||
1314 | mesh->indice_count = obj.f_count * 3; |
||
1315 | mesh->indice_size = (mesh->indice_count <= (int) USHRT_MAX ? 2 : 4); |
||
1316 | d3ddev->CreateIndexBuffer (mesh->indice_count * mesh->indice_size, // length |
||
1317 | D3DUSAGE_DYNAMIC, // usage |
||
1318 | (mesh->indice_size == 2 ? D3DFMT_INDEX16 : D3DFMT_INDEX32), // format (here, 16 or 32-bit) |
||
1319 | D3DPOOL_DEFAULT, // pool type |
||
1320 | &mesh->d3dindices, // pointer to the index buffer pointer |
||
1321 | NULL); // shared handle |
||
1322 | mesh->d3dindices->Lock (0, mesh->indice_count * mesh->indice_size, &ptr_to, D3DLOCK_DISCARD); |
||
1323 | if (mesh->indice_size == 2) |
||
1324 | for (array_index = 0; array_index < mesh->indice_count; array_index++) |
||
1325 | ((unsigned short *) ptr_to)[array_index] = (unsigned short) indices[array_index]; |
||
1326 | else |
||
1327 | memcpy (ptr_to, indices, mesh->indice_count * mesh->indice_size); |
||
1328 | mesh->d3dindices->Unlock (); |
||
1329 | |||
1330 | // finished, free the temporary objects |
||
1331 | for (array_index = 0; array_index < sizeof (hashtable) / sizeof (obj_hashbucket_t); array_index++) |
||
1332 | SAFE_free ((void **) &hashtable[array_index].indices); |
||
1333 | SAFE_free ((void **) &indices); |
||
1334 | SAFE_free ((void **) &vertices); |
||
1335 | SAFE_free ((void **) &obj.vs); |
||
1336 | SAFE_free ((void **) &obj.ns); |
||
1337 | SAFE_free ((void **) &obj.tcs); |
||
1338 | SAFE_free ((void **) &obj.fs); |
||
1339 | SAFE_free ((void **) &filedata); |
||
1340 | |||
1341 | return (true); // Wavefront Object successfully loaded, return TRUE |
||
1342 | |||
1343 | #undef OBJ_GET_EXISTING_INDEX_OR_APPEND_VERTEX |
||
1344 | #undef OBJ_CONVERT_INDEX |
||
1345 | #undef OBJ_INCREASE_OR_RESIZE |
||
1346 | } |
||
1347 | |||
1348 | |||
1349 | static void Render_DrawSceneObjectReflection (sceneobject_t *sceneobject) |
||
1350 | { |
||
1351 | // fast helper to draw a mesh at a specified location with certain pitch and yaw angles |
||
1352 | |||
1353 | D3DXMATRIX rotation_matrix; |
||
1354 | D3DXMATRIX translation_matrix; |
||
1355 | D3DXMATRIX reflect_matrix; |
||
1356 | D3DXMATRIX scaling_matrix; |
||
1357 | material_t *material; |
||
1358 | D3DMATERIAL9 d3dmaterial; |
||
1359 | D3DXPLANE plane; |
||
1360 | mesh_t *mesh; |
||
1361 | mesh_t *tile_mesh; |
||
1362 | float alpha; |
||
1363 | |||
1364 | // draw the reflection below this mesh |
||
1365 | |||
1366 | // quick access to meshes |
||
1367 | mesh = &meshes[sceneobject->mesh_index]; |
||
1368 | tile_mesh = &meshes[theme->tile_meshindex]; |
||
1369 | |||
1370 | // set the world transform at location |
||
1371 | D3DXPlaneFromPointNormal (&plane, (D3DXVECTOR3 *) &scene_center, (D3DXVECTOR3 *) &upwards_direction); |
||
1372 | D3DXMatrixReflect (&reflect_matrix, &plane); |
||
1373 | D3DXMatrixRotationYawPitchRoll (&rotation_matrix, -sceneobject->pitch * TO_RADIANS, 0.0f, -sceneobject->yaw * TO_RADIANS); |
||
1374 | D3DXMatrixTranslation (&translation_matrix, sceneobject->x, sceneobject->y, -sceneobject->z); |
||
1375 | D3DXMatrixScaling (&scaling_matrix, sceneobject->scale, sceneobject->scale, 1.0f); |
||
1376 | |||
1377 | // tell Direct3D about our matrix |
||
1378 | d3ddev->SetTransform (D3DTS_WORLD, &(reflect_matrix * scaling_matrix * rotation_matrix * translation_matrix)); |
||
1379 | |||
1380 | d3ddev->SetRenderState (D3DRS_STENCILENABLE, true); // enable the stencil buffer |
||
1381 | d3ddev->SetRenderState (D3DRS_STENCILFUNC, D3DCMP_LESS); // instruct how to fill the stencil buffer |
||
1382 | d3ddev->SetRenderState (D3DRS_STENCILPASS, D3DSTENCILOP_KEEP); // instruct how to fill the stencil buffer |
||
1383 | |||
1384 | // set the texture for this mesh |
||
1385 | if (sceneobject->texture_index != -1) |
||
1386 | d3ddev->SetTexture (0, textures[sceneobject->texture_index].texture); |
||
1387 | else |
||
1388 | d3ddev->SetTexture (0, NULL); |
||
1389 | |||
1390 | // adjust the light reflection properties by setting the material |
||
1391 | if (sceneobject->material_index != -1) |
||
1392 | material = &materials[sceneobject->material_index]; // use the specified scene object material |
||
1393 | else |
||
1394 | material = &materials[material_count - 1]; // scene object material unspecified, use default material |
||
1395 | alpha = theme->reflection_alpha / 256.0f; |
||
1396 | d3dmaterial.Ambient = D3DXCOLOR (material->ambient, material->ambient, material->ambient, material->transparency * alpha); // Alpha value not used according to SDK |
||
1397 | d3dmaterial.Diffuse = D3DXCOLOR (material->diffuse, material->diffuse, material->diffuse, material->transparency * alpha); |
||
1398 | d3dmaterial.Emissive = D3DXCOLOR (material->emissive, material->emissive, material->emissive, material->transparency * alpha); // Alpha value not used according to SDK |
||
1399 | d3dmaterial.Specular = D3DXCOLOR (material->specular, material->specular, material->specular, material->transparency * alpha); // Alpha value not used according to SDK |
||
1400 | d3dmaterial.Power = material->shininess; |
||
1401 | d3ddev->SetMaterial (&d3dmaterial); |
||
1402 | |||
1403 | // draw the mesh subset |
||
1404 | d3ddev->SetStreamSource (0, mesh->d3dvertices, 0, sizeof (vertex_t)); |
||
1405 | d3ddev->SetFVF (mesh->vertex_format); |
||
1406 | d3ddev->SetRenderState (D3DRS_CULLMODE, D3DCULL_CW); // draw the faces backwards |
||
1407 | if (mesh->is_indexed) |
||
1408 | { |
||
1409 | d3ddev->SetIndices (mesh->d3dindices); |
||
1410 | d3ddev->DrawIndexedPrimitive (D3DPT_TRIANGLELIST, 0, 0, mesh->vertice_count, 0, mesh->indice_count / 3); |
||
1411 | } |
||
1412 | else |
||
1413 | d3ddev->DrawPrimitive (D3DPT_TRIANGLELIST, 0, mesh->vertice_count / 3); |
||
1414 | |||
1415 | d3ddev->SetRenderState (D3DRS_STENCILENABLE, false); // and disable the stencil buffer |
||
1416 | |||
1417 | // now draw the simple shadow below this mesh |
||
1418 | |||
1419 | // grab the tools we need in hand |
||
1420 | d3ddev->SetRenderState (D3DRS_ZENABLE, false); // disable the Z buffer |
||
1421 | d3ddev->SetRenderState (D3DRS_AMBIENT, D3DCOLOR_XRGB (255, 255, 255)); // raise ambient light |
||
1422 | |||
1423 | // position the simple shadow sprite |
||
1424 | D3DXMatrixScaling (&scaling_matrix, max (sceneobject->simpleshadow_size, sceneobject->z / 5.0f), max (sceneobject->simpleshadow_size, sceneobject->z / 5.0f), 0.0f); |
||
1425 | D3DXMatrixTranslation (&translation_matrix, sceneobject->x, sceneobject->y, 0.0f); |
||
1426 | d3ddev->SetTransform (D3DTS_WORLD, &(scaling_matrix * translation_matrix)); |
||
1427 | |||
1428 | // adjust the light reflection properties by setting the material |
||
1429 | material = &materials[material_count - 1]; // use the default material |
||
1430 | d3dmaterial.Ambient = D3DXCOLOR (material->ambient, material->ambient, material->ambient, material->transparency); |
||
1431 | d3dmaterial.Diffuse = D3DXCOLOR (material->diffuse, material->diffuse, material->diffuse, material->transparency); |
||
1432 | d3dmaterial.Emissive = D3DXCOLOR (material->emissive, material->emissive, material->emissive, material->transparency); |
||
1433 | d3dmaterial.Specular = D3DXCOLOR (material->specular, material->specular, material->specular, material->transparency); |
||
1434 | d3dmaterial.Power = material->shininess; |
||
1435 | d3ddev->SetMaterial (&d3dmaterial); |
||
1436 | |||
1437 | d3ddev->SetTexture (0, textures[theme->shadow_textureindex].texture); // select the texture we want |
||
1438 | |||
1439 | // and then draw it |
||
1440 | d3ddev->SetStreamSource (0, tile_mesh->d3dvertices, 0, sizeof (vertex_t)); // set stream source |
||
1441 | d3ddev->SetFVF (tile_mesh->vertex_format); // select which vertex format we are using |
||
1442 | d3ddev->SetRenderState (D3DRS_CULLMODE, D3DCULL_CW); // draw the faces backwards |
||
1443 | if (tile_mesh->is_indexed) |
||
1444 | { |
||
1445 | d3ddev->SetIndices (tile_mesh->d3dindices); |
||
1446 | d3ddev->DrawIndexedPrimitive (D3DPT_TRIANGLELIST, 0, 0, tile_mesh->vertice_count, 0, tile_mesh->indice_count / 3); |
||
1447 | } |
||
1448 | else |
||
1449 | d3ddev->DrawPrimitive (D3DPT_TRIANGLELIST, 0, tile_mesh->vertice_count / 3); |
||
1450 | |||
1451 | // finished, reset ambient light to its previous value and enable the Z buffer back |
||
1452 | d3ddev->SetRenderState (D3DRS_AMBIENT, ambient_light); |
||
1453 | d3ddev->SetRenderState (D3DRS_ZENABLE, true); |
||
1454 | |||
1455 | return; // finished |
||
1456 | } |
||
1457 | |||
1458 | |||
1459 | static void Render_DrawSceneObject (sceneobject_t *sceneobject) |
||
1460 | { |
||
1461 | // fast helper to draw a mesh at a specified location with certain pitch and yaw angles |
||
1462 | |||
1463 | D3DXMATRIX rotation_matrix; |
||
1464 | D3DXMATRIX translation_matrix; |
||
1465 | D3DXMATRIX scaling_matrix; |
||
1466 | material_t *material; |
||
1467 | D3DMATERIAL9 d3dmaterial; |
||
1468 | mesh_t *mesh; |
||
1469 | |||
1470 | // is this object a tile (i.e, it has no mesh) ? |
||
1471 | if (sceneobject->mesh_index == -1) |
||
1472 | { |
||
1473 | Render_DrawSceneTile (sceneobject); // then draw it as a tile instead |
||
1474 | return; // and return |
||
1475 | } |
||
1476 | |||
1477 | // quick access to mesh |
||
1478 | mesh = &meshes[sceneobject->mesh_index]; |
||
1479 | |||
1480 | // set the world transform at location |
||
1481 | D3DXMatrixRotationYawPitchRoll (&rotation_matrix, sceneobject->pitch * TO_RADIANS, 0.0f, sceneobject->yaw * TO_RADIANS); |
||
1482 | D3DXMatrixTranslation (&translation_matrix, sceneobject->x, sceneobject->y, sceneobject->z); |
||
1483 | D3DXMatrixScaling (&scaling_matrix, sceneobject->scale, sceneobject->scale, 1.0f); |
||
1484 | |||
1485 | // tell Direct3D about our matrix |
||
1486 | d3ddev->SetTransform (D3DTS_WORLD, &(scaling_matrix * rotation_matrix * translation_matrix)); |
||
1487 | |||
1488 | // set the texture for this mesh |
||
1489 | if (sceneobject->texture_index != -1) |
||
1490 | d3ddev->SetTexture (0, textures[sceneobject->texture_index].texture); |
||
1491 | else |
||
1492 | d3ddev->SetTexture (0, NULL); |
||
1493 | |||
1494 | // adjust the light reflection properties by setting the material |
||
1495 | if (sceneobject->material_index != -1) |
||
1496 | material = &materials[sceneobject->material_index]; |
||
1497 | else |
||
1498 | material = &materials[material_count - 1]; |
||
1499 | d3dmaterial.Ambient = D3DXCOLOR (material->ambient, material->ambient, material->ambient, material->transparency); |
||
1500 | d3dmaterial.Diffuse = D3DXCOLOR (material->diffuse, material->diffuse, material->diffuse, material->transparency); |
||
1501 | d3dmaterial.Emissive = D3DXCOLOR (material->emissive, material->emissive, material->emissive, material->transparency); |
||
1502 | d3dmaterial.Specular = D3DXCOLOR (material->specular, material->specular, material->specular, material->transparency); |
||
1503 | d3dmaterial.Power = material->shininess; |
||
1504 | d3ddev->SetMaterial (&d3dmaterial); |
||
1505 | |||
1506 | // draw the mesh subset |
||
1507 | d3ddev->SetStreamSource (0, mesh->d3dvertices, 0, sizeof (vertex_t)); |
||
1508 | d3ddev->SetFVF (mesh->vertex_format); |
||
1509 | |||
1510 | // is there transparency on this mesh ? |
||
1511 | if (material->transparency < 1) |
||
1512 | { |
||
1513 | d3ddev->SetRenderState (D3DRS_CULLMODE, D3DCULL_CW); // draw the back faces |
||
1514 | if (mesh->is_indexed) |
||
1515 | { |
||
1516 | d3ddev->SetIndices (mesh->d3dindices); |
||
1517 | d3ddev->DrawIndexedPrimitive (D3DPT_TRIANGLELIST, 0, 0, mesh->vertice_count, 0, mesh->indice_count / 3); |
||
1518 | } |
||
1519 | else |
||
1520 | d3ddev->DrawPrimitive (D3DPT_TRIANGLELIST, 0, mesh->vertice_count / 3); |
||
1521 | } |
||
1522 | |||
1523 | // now draw the front faces |
||
1524 | d3ddev->SetRenderState (D3DRS_CULLMODE, D3DCULL_CCW); // draw the front faces |
||
1525 | if (mesh->is_indexed) |
||
1526 | { |
||
1527 | d3ddev->SetIndices (mesh->d3dindices); |
||
1528 | d3ddev->DrawIndexedPrimitive (D3DPT_TRIANGLELIST, 0, 0, mesh->vertice_count, 0, mesh->indice_count / 3); |
||
1529 | } |
||
1530 | else |
||
1531 | d3ddev->DrawPrimitive (D3DPT_TRIANGLELIST, 0, mesh->vertice_count / 3); |
||
1532 | |||
1533 | return; // finished |
||
1534 | } |
||
1535 | |||
1536 | |||
1537 | static void Render_DrawSceneTile (sceneobject_t *sceneobject) |
||
1538 | { |
||
1539 | // fast helper to draw a tile (i.e, an object that doesn't have a mesh) at a specified location |
||
1540 | |||
1541 | D3DXMATRIX rotation_matrix; |
||
1542 | D3DXMATRIX translation_matrix; |
||
1543 | D3DXMATRIX scaling_matrix; |
||
1544 | material_t *material; |
||
1545 | D3DMATERIAL9 d3dmaterial; |
||
1546 | mesh_t *tile_mesh; |
||
1547 | |||
1548 | tile_mesh = &meshes[theme->tile_meshindex]; // quick access to tile mesh |
||
1549 | |||
1550 | // grab the tools we need in hand |
||
1551 | d3ddev->SetRenderState (D3DRS_AMBIENT, D3DCOLOR_RGBA (0xFF, 0xFF, 0xFF, 0xFF)); // raise light |
||
1552 | |||
1553 | // set the world transform at location |
||
1554 | D3DXMatrixRotationYawPitchRoll (&rotation_matrix, sceneobject->pitch * TO_RADIANS, 0.0f, sceneobject->yaw * TO_RADIANS); |
||
1555 | D3DXMatrixTranslation (&translation_matrix, sceneobject->x, sceneobject->y, sceneobject->z); |
||
1556 | D3DXMatrixScaling (&scaling_matrix, sceneobject->scale, sceneobject->scale, 1.0f); |
||
1557 | |||
1558 | // tell Direct3D about our matrix |
||
1559 | d3ddev->SetTransform (D3DTS_WORLD, &(scaling_matrix * rotation_matrix * translation_matrix)); |
||
1560 | |||
1561 | // adjust the light reflection properties by setting the material |
||
1562 | material = &materials[material_count - 1]; // use the default material for tiles |
||
1563 | d3dmaterial.Ambient = D3DXCOLOR (material->ambient, material->ambient, material->ambient, material->ambient); |
||
1564 | d3dmaterial.Diffuse = D3DXCOLOR (material->diffuse, material->diffuse, material->diffuse, material->diffuse); |
||
1565 | d3dmaterial.Emissive = D3DXCOLOR (material->emissive, material->emissive, material->emissive, material->emissive); |
||
1566 | d3dmaterial.Specular = D3DXCOLOR (material->specular, material->specular, material->specular, material->specular); |
||
1567 | d3dmaterial.Power = material->shininess; |
||
1568 | d3ddev->SetMaterial (&d3dmaterial); |
||
1569 | |||
1570 | // select the texture we want |
||
1571 | d3ddev->SetTexture (0, textures[sceneobject->texture_index].texture); |
||
1572 | |||
1573 | // and then draw it |
||
1574 | d3ddev->SetStreamSource (0, tile_mesh->d3dvertices, 0, sizeof (vertex_t)); // set stream source |
||
1575 | d3ddev->SetFVF (tile_mesh->vertex_format); // select which vertex format we are using |
||
1576 | d3ddev->SetRenderState (D3DRS_CULLMODE, D3DCULL_CW); // draw the back faces |
||
1577 | if (tile_mesh->is_indexed) |
||
1578 | { |
||
1579 | d3ddev->SetIndices (tile_mesh->d3dindices); |
||
1580 | d3ddev->DrawIndexedPrimitive (D3DPT_TRIANGLELIST, 0, 0, tile_mesh->vertice_count, 0, tile_mesh->indice_count / 3); |
||
1581 | } |
||
1582 | else |
||
1583 | d3ddev->DrawPrimitive (D3DPT_TRIANGLELIST, 0, tile_mesh->vertice_count / 3); |
||
1584 | |||
1585 | // finished, reset ambient light to its previous value |
||
1586 | d3ddev->SetRenderState (D3DRS_AMBIENT, ambient_light); |
||
1587 | |||
1588 | return; // finished |
||
1589 | } |
||
1590 | |||
1591 | |||
1592 | static void Render_DrawSprite (sprite_t *sprite, float x_percent, float y_percent, float width_percent, float height_percent, int alpha) |
||
1593 | { |
||
1594 | // fast helper to draw a sprite at a specified location with certain parameters |
||
1595 | |||
1596 | D3DXMATRIX scaling_matrix; |
||
1597 | texture_t *texture; |
||
1598 | float scale_x; |
||
1599 | float scale_y; |
||
1600 | |||
1601 | texture = &textures[sprite->texture_index]; // quick access to sprite's texture |
||
1602 | |||
1603 | scale_x = (width_percent * (float) initial_width) / (float) (100 * texture->width); |
||
1604 | scale_y = (height_percent * (float) initial_height) / (float) (100 * texture->height); |
||
1605 | |||
1606 | // start rendering the sprite (an optimized version would draw all sprites in a row...) |
||
1607 | sprite->sprite->Begin (D3DXSPRITE_ALPHABLEND); |
||
1608 | |||
1609 | // scale and position the sprite |
||
1610 | D3DXMatrixTransformation2D (&scaling_matrix, // output matrix |
||
1611 | NULL, // scaling center |
||
1612 | 0.0f, // scaling rotation |
||
1613 | &D3DXVECTOR2 (scale_x, scale_y), // scaling ratio |
||
1614 | &D3DXVECTOR2 (0, 0), // rotation center |
||
1615 | 0.0f, // rotation |
||
1616 | &D3DXVECTOR2 (x_percent * (float) initial_width / 100.0f, y_percent * (float) initial_height / 100.0f)); // translation |
||
1617 | sprite->sprite->SetTransform (&scaling_matrix); // tell the sprite about the scaling and position transform |
||
1618 | |||
1619 | // now draw the sprite with the specified alpha and finish rendering |
||
1620 | sprite->sprite->Draw (texture->texture, NULL, NULL, NULL, D3DCOLOR_ARGB (alpha, 255, 255, 255)); |
||
1621 | sprite->sprite->End (); |
||
1622 | |||
1623 | return; // finished |
||
1624 | } |
||
1625 | |||
1626 | |||
1627 | static void Render_GetTextBoundaries (int max_width, int font_id, wchar_t *text, RECT *rect) |
||
1628 | { |
||
1629 | // this function computes and returns the size of the rectangle the specified text will fit into. Note that text may be modified |
||
1630 | // to insert new lines if it doesn't fit in a single line. |
||
1631 | |||
1632 | int char_index; |
||
1633 | int length; |
||
1634 | int optimal_length; |
||
1635 | bool have_split; |
||
1636 | |||
1637 | // blank out the output rectangle |
||
1638 | memset (rect, 0, sizeof (RECT)); |
||
1639 | |||
1640 | // ask direct3D to compute the text size a first time |
||
1641 | fonts[font_id].font->DrawText (NULL, text, -1, rect, DT_CALCRECT, D3DCOLOR (0)); |
||
1642 | |||
1643 | // if max width is not set, set it to viewport width |
||
1644 | if (max_width < 0) |
||
1645 | max_width = initial_width; // then use it to compute the real max width |
||
1646 | |||
1647 | // do we need more than one line ? |
||
1648 | if (rect->right > max_width) |
||
1649 | { |
||
1650 | // see how many lines we need and compute the optimal length of one line |
||
1651 | length = wcslen (text); |
||
1652 | optimal_length = length / (1 + rect->right / (int) max_width); |
||
1653 | have_split = false; |
||
1654 | for (char_index = optimal_length; char_index < length; char_index++) |
||
1655 | if (iswspace (text[char_index])) |
||
1656 | { |
||
1657 | text[char_index] = L'\n'; // interpolate linefeeds into string |
||
1658 | have_split = true; // remember string has been split |
||
1659 | char_index += optimal_length; |
||
1660 | } |
||
1661 | |||
1662 | // and ask direct3D to compute the text size again |
||
1663 | fonts[font_id].font->DrawText (NULL, text, -1, rect, DT_CALCRECT, D3DCOLOR (0)); |
||
1664 | } |
||
1665 | |||
1666 | return; // finished |
||
1667 | } |
||
1668 | |||
1669 | |||
1670 | static void Render_wprintf (int x, int y, int max_width, int horiz_align, int vert_align, int text_align, int font_id, unsigned long color_rgba, RECT *out_rect, const wchar_t *fmt, ...) |
||
1671 | { |
||
1672 | // this function displays text on the Direct3D interface according to the given parameters. X and Y are the base coordinates of |
||
1673 | // the text's bounding rectangle. Max_width is the maximum allowed width of this rectangle before wrapping words on a new line. |
||
1674 | // Horiz_align and vert_align are the alignment parameters of the RECTANGLE relatively to X and Y. Text_align is the alignment of |
||
1675 | // the TEXT inside this rectangle (meaning, you can have right-aligned text in a rectangle that is centered on a point). Font_id |
||
1676 | // and color.alphargb define the font and color of the text. Out_rect, if filled, will point to a RECT structure describing the text's |
||
1677 | // bounding rectangle, after any word wrapping corrections have been made. Fmt is a format string containing the text itself, |
||
1678 | // printf-style. |
||
1679 | |||
1680 | va_list argptr; |
||
1681 | RECT rect; |
||
1682 | int left; |
||
1683 | int top; |
||
1684 | |||
1685 | // concatenate all the arguments in one string |
||
1686 | va_start (argptr, fmt); |
||
1687 | wvsprintf (printf_buffer, fmt, argptr); |
||
1688 | va_end (argptr); |
||
1689 | |||
1690 | // get the text boundaries |
||
1691 | Render_GetTextBoundaries (max_width, font_id, printf_buffer, &rect); |
||
1692 | |||
1693 | // horizontal alignment |
||
1694 | if (horiz_align == ALIGN_LEFT) |
||
1695 | left = x; |
||
1696 | else if (horiz_align == ALIGN_RIGHT) |
||
1697 | left = x - rect.right; |
||
1698 | else |
||
1699 | left = x - rect.right / 2; |
||
1700 | |||
1701 | // vertical alignment |
||
1702 | if (vert_align == ALIGN_TOP) |
||
1703 | top = y; |
||
1704 | else if (vert_align == ALIGN_BOTTOM) |
||
1705 | top = y - rect.bottom; |
||
1706 | else |
||
1707 | top = y - rect.bottom / 2; |
||
1708 | |||
1709 | // now reposition our rectangle correctly acording to alignment |
||
1710 | OffsetRect (&rect, left, top); |
||
1711 | |||
1712 | // and draw the text |
||
1713 | if (text_align == ALIGN_LEFT) |
||
1714 | fonts[font_id].font->DrawText (NULL, printf_buffer, -1, &rect, DT_LEFT, RGBACOLOR_TO_ARGBCOLOR (color_rgba)); |
||
1715 | else if (horiz_align == ALIGN_RIGHT) |
||
1716 | fonts[font_id].font->DrawText (NULL, printf_buffer, -1, &rect, DT_RIGHT, RGBACOLOR_TO_ARGBCOLOR (color_rgba)); |
||
1717 | else |
||
1718 | fonts[font_id].font->DrawText (NULL, printf_buffer, -1, &rect, DT_CENTER, RGBACOLOR_TO_ARGBCOLOR (color_rgba)); |
||
1719 | |||
1720 | // do we want the output rectangle ? |
||
1721 | if (out_rect != NULL) |
||
1722 | memcpy (out_rect, &rect, sizeof (rect)); // if so, copy it in the given variable |
||
1723 | |||
1724 | return; // finished |
||
1725 | } |
||
1726 | |||
1727 | |||
1728 | static float DistanceToCamera (float x, float y, float z) |
||
1729 | { |
||
1730 | // this function computes the distance of the point at coordinates x,y,z to the camera |
||
1731 | |||
1732 | vector_t displacement; |
||
1733 | |||
1734 | // compute displacement... |
||
1735 | displacement.x = x - camera_position.x; |
||
1736 | displacement.y = y - camera_position.y; |
||
1737 | displacement.z = z - camera_position.z; |
||
1738 | |||
1739 | // ...and then Pythagores in 3D |
||
1740 | return (sqrtf (displacement.x * displacement.x + displacement.y * displacement.y + displacement.z * displacement.z)); |
||
1741 | } |
||
1742 | |||
1743 | |||
1744 | static float FadeFloat (float from, float to, float start_time, float end_time) |
||
1745 | { |
||
1746 | // helper function to return a progressive variation between from and to based on time |
||
1747 | |||
1748 | if (end_time < current_time) |
||
1749 | return (to); |
||
1750 | |||
1751 | // base + (variation) * ( fraction of completion ) |
||
1752 | return (from + (to - from) * (current_time - start_time) / (end_time - start_time)); |
||
1753 | } |
||
1754 | |||
1755 | |||
1756 | static unsigned long HashString (const wchar_t *string_buffer) |
||
1757 | { |
||
1758 | // super fast string hash function, code courtesy of |
||
1759 | // http://www.azillionmonkeys.com/qed/hash.html |
||
1760 | |||
1761 | unsigned long length; |
||
1762 | unsigned long hash; |
||
1763 | unsigned long tmp; |
||
1764 | int remaining; |
||
1765 | |||
1766 | // first, get the string length and start with this as a hash value |
||
1767 | length = wcslen (string_buffer) * sizeof (wchar_t); |
||
1768 | hash = length; |
||
1769 | |||
1770 | // figure out how many bytes there will remain after 32-bit processing |
||
1771 | remaining = length & 3; |
||
1772 | length >>= 2; |
||
1773 | |||
1774 | // main loop, process 32-bit blocks |
||
1775 | for ( ; length > 0; length--) |
||
1776 | { |
||
1777 | hash += *((const unsigned short *) string_buffer); |
||
1778 | tmp = ((*((const unsigned short *) (string_buffer + 2))) << 11) ^ hash; |
||
1779 | hash = (hash << 16) ^ tmp; |
||
1780 | string_buffer += 2 * sizeof (unsigned short); |
||
1781 | hash += hash >> 11; |
||
1782 | } |
||
1783 | |||
1784 | // handle the remaining bytes |
||
1785 | if (remaining == 3) |
||
1786 | { |
||
1787 | hash += *((const unsigned short *) string_buffer); |
||
1788 | hash ^= hash << 16; |
||
1789 | hash ^= string_buffer[sizeof (unsigned short)] << 18; |
||
1790 | hash += hash >> 11; |
||
1791 | } |
||
1792 | else if (remaining == 2) |
||
1793 | { |
||
1794 | hash += *((const unsigned short *) string_buffer); |
||
1795 | hash ^= hash << 11; |
||
1796 | hash += hash >> 17; |
||
1797 | } |
||
1798 | else if (remaining == 1) |
||
1799 | { |
||
1800 | hash += *string_buffer; |
||
1801 | hash ^= hash << 10; |
||
1802 | hash += hash >> 1; |
||
1803 | } |
||
1804 | |||
1805 | // force "avalanching" of final 127 bits |
||
1806 | hash ^= hash << 3; |
||
1807 | hash += hash >> 5; |
||
1808 | hash ^= hash << 4; |
||
1809 | hash += hash >> 17; |
||
1810 | hash ^= hash << 25; |
||
1811 | hash += hash >> 6; |
||
1812 | |||
1813 | return (hash); // finished, return the hash value |
||
1814 | } |
||
1815 | |||
1816 | |||
1817 | static unsigned long HashFile (const wchar_t *file_pathname) |
||
1818 | { |
||
1819 | // super fast file content pseudo-hash function |
||
1820 | |||
1821 | unsigned short value; |
||
81 | pmbaty | 1822 | unsigned long file_size; |
1 | pmbaty | 1823 | FILE *fp; |
1824 | |||
1825 | // open the file |
||
1826 | _wfopen_s (&fp, file_pathname, L"rb"); |
||
1827 | if (fp == NULL) |
||
1828 | return ((unsigned long) time (NULL)); // if file can't be open, return a random number |
||
1829 | |||
81 | pmbaty | 1830 | // seek at end of file, read file size, and rewind |
1831 | fseek (fp, 0, SEEK_END); |
||
1832 | file_size = ftell (fp); |
||
1833 | fseek (fp, 0, SEEK_SET); |
||
1834 | |||
1 | pmbaty | 1835 | // seek at 2/3 of file size (if file is small enough, return only its content) |
81 | pmbaty | 1836 | if (file_size >= 4) |
1 | pmbaty | 1837 | { |
81 | pmbaty | 1838 | fseek (fp, file_size * 2 / 3, SEEK_SET); // seek at 2/3 of file |
1 | pmbaty | 1839 | fread (&value, 2, 1, fp); // and read a word here |
1840 | } |
||
81 | pmbaty | 1841 | else if (file_size >= 2) |
1 | pmbaty | 1842 | fread (&value, 2, 1, fp); |
81 | pmbaty | 1843 | else if (file_size == 1) |
1 | pmbaty | 1844 | value = fgetc (fp); |
1845 | else |
||
1846 | value = 0; |
||
1847 | |||
1848 | // finished, close the file |
||
1849 | fclose (fp); |
||
1850 | |||
1851 | // and return a hash composed of the 16 lower bits of the file size and the value we read |
||
81 | pmbaty | 1852 | return ((unsigned long) (file_size << 16) | (unsigned long) value); |
1 | pmbaty | 1853 | } |
1854 | |||
1855 | |||
1856 | static void ResolveWildcard (wchar_t *file_pathname, wchar_t *extensions_separated_by_bars) |
||
1857 | { |
||
1858 | // this function resolves a pathname ending with .* by testing with various possible |
||
1859 | // file extensions until one of the files formed that way is found to exist. |
||
1860 | |||
1861 | static wchar_t extension_list[256]; // needs to be modifiable for strtok() |
||
1862 | wchar_t *current_extension; |
||
1863 | wchar_t *wcstok_context; |
||
1864 | int length; |
||
1865 | |||
1866 | wcscpy_s (extension_list, WCHAR_SIZEOF (extension_list), extensions_separated_by_bars); |
||
1867 | length = wcslen (file_pathname); // get pathname length |
||
1868 | |||
1869 | // does the pathname we want NOT end with a wildcard ? |
||
1870 | if ((length < 2) || (wcscmp (&file_pathname[length - 2], L".*") != 0)) |
||
1871 | return; // no need to resolve anything |
||
1872 | |||
1873 | // test each extension and see if a corresponding file exists |
||
1874 | current_extension = wcstok_s (extension_list, L"|", &wcstok_context); |
||
1875 | while (current_extension != NULL) |
||
1876 | { |
||
1877 | if (*current_extension == L'.') |
||
1878 | current_extension++; // if current extension starts with a dot, skip it |
||
81 | pmbaty | 1879 | |
1 | pmbaty | 1880 | wcscpy_s (&file_pathname[length - 1], wcslen (current_extension) + 1, current_extension); |
81 | pmbaty | 1881 | if (_waccess (file_pathname, 0) == 0) |
1 | pmbaty | 1882 | return; // found a file with this extension |
1883 | current_extension = wcstok_s (NULL, L"|", &wcstok_context); |
||
1884 | } |
||
1885 | |||
1886 | wcscpy_s (&file_pathname[length - 1], 2, L"*"); |
||
1887 | return; // if none of these extensions match, put the wildcard back and return |
||
1888 | } |
||
1889 | |||
1890 | |||
1891 | static int SortReflectedObjects (const void *object1, const void *object2) |
||
1892 | { |
||
1893 | // callback function used by qsort() when sorting the reflected objects according to their distance to the viewer |
||
1894 | |||
1895 | return ((int) (1000.0f * (((reflectedobject_t *) object2)->distance - ((reflectedobject_t *) object1)->distance))); |
||
1896 | } |