Subversion Repositories Games.Chess Giants

Rev

Rev 10 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
// main.cpp
2
 
3
#include "common.h"
4
 
5
 
6
bool Themes_Init (void)
7
{
8
   // this function (re)builds the themes array and loads all the themes' data
9
 
10
   wchar_t search_pattern[256];
11
   WIN32_FIND_DATA wfd;
12
   HANDLE hFind;
13
   int selectedtheme_index;
14
 
15
   // start by resetting the themes array
16
   themes = NULL;
17
   theme_count = 0;
18
 
19
   // the selected them isn't known yet
20
   selectedtheme_index = -1;
21
 
22
   // build the search pattern string out of the path
23
   swprintf_s (search_pattern, WCHAR_SIZEOF (search_pattern), L"%s\\themes\\*.*", app_path);
24
 
25
   // initiate the search from that point
26
   hFind = FindFirstFile (search_pattern, &wfd);
27
   if (hFind != INVALID_HANDLE_VALUE)
28
   {
29
      // start doing this...
30
      do
31
      {
32
         // is it this directory or the parent directory ?
33
         if ((wcscmp (wfd.cFileName, L".") == 0) || (wcscmp (wfd.cFileName, L"..") == 0))
34
            continue; // skip that entry
35
 
36
         // is it a directory ?
37
         if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
38
         {
39
            // reallocate space for one theme more (zero out the allocated space) and copy it in place
40
            themes = (theme_t *) SAFE_realloc (themes, theme_count, theme_count + 1, sizeof (theme_t), true);
41
            wcscpy_s (themes[theme_count].name, WCHAR_SIZEOF (themes[theme_count].name), wfd.cFileName);
42
            theme_count++; // we know now one theme more
43
 
44
            // is it the selected theme ?
45
            if (_wcsicmp (wfd.cFileName, wantedtheme_name) == 0)
46
            {
47
               Theme_Load (&themes[theme_count - 1], true); // load ALL the theme data from this directory
48
               selectedtheme_index = theme_count - 1; // remember the selected theme is this one
49
            }
50
         }
51
      } while (FindNextFile (hFind, &wfd)); // ...and don't stop as long as there are files to go
52
 
53
      FindClose (hFind); // close the search handle
54
   }
55
 
56
   // consistency check (this is not the real reason for the error, but who cares)
57
   if (theme_count == 0)
58
   {
59
      MessageBox (NULL, LOCALIZE (L"Error_UnableToAddTextureD3DXCreateTextureFromFileFailed"), LOCALIZE (L"FatalError"), MB_ICONERROR | MB_OK);
60
      return (false); // if none of these extensions match, bomb out
61
   }
62
 
63
   // have we found the selected theme ?
64
   if (selectedtheme_index != -1)
65
      theme = &themes[selectedtheme_index]; // found it, so link it
66
   else
67
   {
68
      Theme_Load (&themes[0], true); // not found, so load the first available theme
69
      theme = &themes[0]; // and link selected theme to it
70
   }
71
 
72
   // if we have a custom background specified, load it too
73
   if (custombackground_pathname[0] != 0)
74
      Background_LoadImage (&custombg, custombackground_pathname);
75
 
76
   return (true); // at least one theme has been loaded successfully, return TRUE
77
}
78
 
79
 
80
void Themes_Shutdown (void)
81
{
82
   // helper function to release the themes structures array
83
 
84
   int theme_index;
85
 
86
   // before freeing the themes array, backup our theme's name
87
   wcscpy_s (wantedtheme_name, WCHAR_SIZEOF (wantedtheme_name), theme->name);
88
 
89
   // for each theme, free its mallocated data
90
   for (theme_index = 0; theme_index < theme_count; theme_index++)
91
   {
92
      SAFE_free ((void **) &theme->description); // free its description
93
 
94
      SAFE_free ((void **) &themes[theme_index].illum.lights); // free the lights array
95
      themes[theme_index].illum.light_count = 0;
96
   }
97
 
98
   // free the themes array
99
   SAFE_free ((void **) &themes);
100
   theme_count = 0;
101
 
102
   return; // nothing to do
103
}
104
 
105
 
106
void Theme_Load (theme_t *theme, bool want_all)
107
{
108
   // this function loads a particular theme out of a theme subdirectory's files
109
 
110
   struct _stat fileinfo;
111
   char *desc_buffer;
112
   wchar_t filename[256];
113
   wchar_t section_name[32];
114
   wchar_t value[128];
115
   light_t light;
116
   void *inifile;
117
   int light_index;
118
   int light_count;
119
   int red;
120
   int green;
121
   int blue;
122
   FILE *fp;
123
 
124
   // allocate space for and load its description
125
   swprintf_s (filename, WCHAR_SIZEOF (filename), L"themes/%s/readme.txt", theme->name);
126
   if (_wstat (filename, &fileinfo) == 0)
127
   {
128
      desc_buffer = (char *) SAFE_malloc (fileinfo.st_size, sizeof (char), false);
129
      _wfopen_s (&fp, filename, L"rb");
130
      fread (desc_buffer, fileinfo.st_size, 1, fp); // read file in whole in ASCII form
131
      fclose (fp); // close it and then convert it to wide char
132
      theme->description = (wchar_t *) SAFE_malloc (fileinfo.st_size, sizeof (wchar_t), false);
133
      ConvertToWideChar (theme->description, fileinfo.st_size, desc_buffer);
134
      SAFE_free ((void **) &desc_buffer); // we no longer need the ASCII buffer
135
   }
136
   else
137
      theme->description = NULL; // this theme has no description
138
 
139
   // load its background image
140
   if ((theme->load_index == 0) || (want_all && (theme->load_index < 1)))
141
   {
142
      swprintf_s (filename, WCHAR_SIZEOF (filename), L"themes/%s/background/background.*", theme->name);
143
      Background_LoadImage (&theme->bg, filename);
144
   }
145
 
146
   // load board meshes
147
   if ((theme->load_index == 1) || (want_all && (theme->load_index < 2)))
148
      theme->board_meshindex = Render_LoadMesh (L"themes/%s/models/board.*", theme->name);
149
   if ((theme->load_index == 2) || (want_all && (theme->load_index < 3)))
150
      theme->table_meshindex = Render_LoadMesh (L"themes/%s/models/table.*", theme->name);
151
   if ((theme->load_index == 3) || (want_all && (theme->load_index < 4)))
152
      theme->trim_meshindex = Render_LoadMesh (L"themes/%s/models/trim.*", theme->name);
153
   if ((theme->load_index == 4) || (want_all && (theme->load_index < 5)))
154
      theme->tile_meshindex = Render_LoadMesh (L"themes/%s/models/tile.*", theme->name);
155
 
156
   // load part meshes
157
   if ((theme->load_index == 5) || (want_all && (theme->load_index < 6)))
158
      theme->part_meshes[PART_KING] = Render_LoadMesh (L"themes/%s/models/king.*", theme->name);
159
   if ((theme->load_index == 6) || (want_all && (theme->load_index < 7)))
160
      theme->part_meshes[PART_QUEEN] = Render_LoadMesh (L"themes/%s/models/queen.*", theme->name);
161
   if ((theme->load_index == 7) || (want_all && (theme->load_index < 8)))
162
      theme->part_meshes[PART_BISHOP] = Render_LoadMesh (L"themes/%s/models/bishop.*", theme->name);
163
   if ((theme->load_index == 8) || (want_all && (theme->load_index < 9)))
164
      theme->part_meshes[PART_KNIGHT] = Render_LoadMesh (L"themes/%s/models/knight.*", theme->name);
165
   if ((theme->load_index == 9) || (want_all && (theme->load_index < 10)))
166
      theme->part_meshes[PART_ROOK] = Render_LoadMesh (L"themes/%s/models/rook.*", theme->name);
167
   if ((theme->load_index == 10) || (want_all && (theme->load_index < 11)))
168
      theme->part_meshes[PART_PAWN] = Render_LoadMesh (L"themes/%s/models/pawn.*", theme->name);
169
 
170
   // load part textures
171
   if ((theme->load_index == 11) || (want_all && (theme->load_index < 12)))
172
      theme->part_colors[COLOR_BLACK].texture = Render_LoadTexture (L"themes/%s/models/part-black.*", theme->name);
173
   if ((theme->load_index == 12) || (want_all && (theme->load_index < 13)))
174
      theme->part_colors[COLOR_WHITE].texture = Render_LoadTexture (L"themes/%s/models/part-white.*", theme->name);
175
 
176
   // load board, table, grid and trim textures
177
   if ((theme->load_index == 13) || (want_all && (theme->load_index < 14)))
178
      theme->board_texture = Render_LoadTexture (L"themes/%s/models/board.*", theme->name);
179
   if ((theme->load_index == 14) || (want_all && (theme->load_index < 15)))
180
      theme->table_texture = Render_LoadTexture (L"themes/%s/models/table.*", theme->name);
181
   if ((theme->load_index == 15) || (want_all && (theme->load_index < 16)))
182
      theme->trim_texture = Render_LoadTexture (L"themes/%s/models/trim.*", theme->name);
183
   if ((theme->load_index == 16) || (want_all && (theme->load_index < 17)))
184
      theme->grid_texture = Render_LoadTexture (L"themes/%s/models/grid.*", theme->name);
185
 
186
   // load overlay textures
187
   if ((theme->load_index == 17) || (want_all && (theme->load_index < 18)))
188
      theme->shadow_textureindex = Render_LoadTexture (L"themes/%s/shadow.*", theme->name);
189
   if ((theme->load_index == 18) || (want_all && (theme->load_index < 19)))
190
      theme->hovered_textureindex = Render_LoadTexture (L"themes/%s/hovered.*", theme->name);
191
   if ((theme->load_index == 19) || (want_all && (theme->load_index < 20)))
192
      theme->check_textureindex = Render_LoadTexture (L"themes/%s/check.*", theme->name);
193
   if ((theme->load_index == 20) || (want_all && (theme->load_index < 21)))
194
      theme->threat_textureindex = Render_LoadTexture (L"themes/%s/takeable.*", theme->name);
195
   if ((theme->load_index == 21) || (want_all && (theme->load_index < 22)))
196
      theme->lastmovesource_textureindex = Render_LoadTexture (L"themes/%s/lastmovesource.*", theme->name);
197
   if ((theme->load_index == 22) || (want_all && (theme->load_index < 23)))
198
      theme->lastmovetarget_textureindex = Render_LoadTexture (L"themes/%s/lastmovetarget.*", theme->name);
199
   if ((theme->load_index == 23) || (want_all && (theme->load_index < 24)))
200
      theme->selected_textureindex = Render_LoadTexture (L"themes/%s/selected.*", theme->name);
201
   if ((theme->load_index == 24) || (want_all && (theme->load_index < 25)))
202
      theme->possiblemove_textureindex = Render_LoadTexture (L"themes/%s/possiblemove.*", theme->name);
203
   if ((theme->load_index == 25) || (want_all && (theme->load_index < 26)))
204
      theme->takeable_textureindex = Render_LoadTexture (L"themes/%s/takeable.*", theme->name);
205
 
206
   // load sprites
207
   if ((theme->load_index == 26) || (want_all && (theme->load_index < 27)))
208
      theme->flatsprites[COLOR_WHITE][PART_PAWN] = Render_LoadSprite (L"themes/%s/parts/pawn-white.*", theme->name);
209
   if ((theme->load_index == 27) || (want_all && (theme->load_index < 28)))
210
      theme->flatsprites[COLOR_WHITE][PART_ROOK] = Render_LoadSprite (L"themes/%s/parts/rook-white.*", theme->name);
211
   if ((theme->load_index == 28) || (want_all && (theme->load_index < 29)))
212
      theme->flatsprites[COLOR_WHITE][PART_KNIGHT] = Render_LoadSprite (L"themes/%s/parts/knight-white.*", theme->name);
213
   if ((theme->load_index == 29) || (want_all && (theme->load_index < 30)))
214
      theme->flatsprites[COLOR_WHITE][PART_BISHOP] = Render_LoadSprite (L"themes/%s/parts/bishop-white.*", theme->name);
215
   if ((theme->load_index == 30) || (want_all && (theme->load_index < 31)))
216
      theme->flatsprites[COLOR_WHITE][PART_QUEEN] = Render_LoadSprite (L"themes/%s/parts/queen-white.*", theme->name);
217
   if ((theme->load_index == 31) || (want_all && (theme->load_index < 32)))
218
      theme->flatsprites[COLOR_WHITE][PART_KING] = Render_LoadSprite (L"themes/%s/parts/king-white.*", theme->name);
219
   if ((theme->load_index == 32) || (want_all && (theme->load_index < 33)))
220
      theme->flatsprites[COLOR_BLACK][PART_PAWN] = Render_LoadSprite (L"themes/%s/parts/pawn-black.*", theme->name);
221
   if ((theme->load_index == 33) || (want_all && (theme->load_index < 34)))
222
      theme->flatsprites[COLOR_BLACK][PART_ROOK] = Render_LoadSprite (L"themes/%s/parts/rook-black.*", theme->name);
223
   if ((theme->load_index == 34) || (want_all && (theme->load_index < 35)))
224
      theme->flatsprites[COLOR_BLACK][PART_KNIGHT] = Render_LoadSprite (L"themes/%s/parts/knight-black.*", theme->name);
225
   if ((theme->load_index == 35) || (want_all && (theme->load_index < 36)))
226
      theme->flatsprites[COLOR_BLACK][PART_BISHOP] = Render_LoadSprite (L"themes/%s/parts/bishop-black.*", theme->name);
227
   if ((theme->load_index == 36) || (want_all && (theme->load_index < 37)))
228
      theme->flatsprites[COLOR_BLACK][PART_QUEEN] = Render_LoadSprite (L"themes/%s/parts/queen-black.*", theme->name);
229
   if ((theme->load_index == 37) || (want_all && (theme->load_index < 38)))
230
      theme->flatsprites[COLOR_BLACK][PART_KING] = Render_LoadSprite (L"themes/%s/parts/king-black.*", theme->name);
231
   if ((theme->load_index == 38) || (want_all && (theme->load_index < 39)))
232
      theme->lastmovesource_spriteindex = Render_LoadSprite (L"themes/%s/lastmovesource.*", theme->name);
233
   if ((theme->load_index == 39) || (want_all && (theme->load_index < 40)))
234
      theme->lastmovetarget_spriteindex = Render_LoadSprite (L"themes/%s/lastmovetarget.*", theme->name);
235
 
236
   // load the flat pieces textures
237
   if ((theme->load_index == 40) || (want_all && (theme->load_index < 41)))
238
      theme->flattextures[COLOR_WHITE][PART_PAWN] = Render_LoadTexture (L"themes/%s/parts/pawn-white.*", theme->name);
239
   if ((theme->load_index == 41) || (want_all && (theme->load_index < 42)))
240
      theme->flattextures[COLOR_WHITE][PART_ROOK] = Render_LoadTexture (L"themes/%s/parts/rook-white.*", theme->name);
241
   if ((theme->load_index == 42) || (want_all && (theme->load_index < 43)))
242
      theme->flattextures[COLOR_WHITE][PART_KNIGHT] = Render_LoadTexture (L"themes/%s/parts/knight-white.*", theme->name);
243
   if ((theme->load_index == 43) || (want_all && (theme->load_index < 44)))
244
      theme->flattextures[COLOR_WHITE][PART_BISHOP] = Render_LoadTexture (L"themes/%s/parts/bishop-white.*", theme->name);
245
   if ((theme->load_index == 44) || (want_all && (theme->load_index < 45)))
246
      theme->flattextures[COLOR_WHITE][PART_QUEEN] = Render_LoadTexture (L"themes/%s/parts/queen-white.*", theme->name);
247
   if ((theme->load_index == 45) || (want_all && (theme->load_index < 46)))
248
      theme->flattextures[COLOR_WHITE][PART_KING] = Render_LoadTexture (L"themes/%s/parts/king-white.*", theme->name);
249
   if ((theme->load_index == 46) || (want_all && (theme->load_index < 47)))
250
      theme->flattextures[COLOR_BLACK][PART_PAWN] = Render_LoadTexture (L"themes/%s/parts/pawn-black.*", theme->name);
251
   if ((theme->load_index == 47) || (want_all && (theme->load_index < 48)))
252
      theme->flattextures[COLOR_BLACK][PART_ROOK] = Render_LoadTexture (L"themes/%s/parts/rook-black.*", theme->name);
253
   if ((theme->load_index == 48) || (want_all && (theme->load_index < 49)))
254
      theme->flattextures[COLOR_BLACK][PART_KNIGHT] = Render_LoadTexture (L"themes/%s/parts/knight-black.*", theme->name);
255
   if ((theme->load_index == 49) || (want_all && (theme->load_index < 50)))
256
      theme->flattextures[COLOR_BLACK][PART_BISHOP] = Render_LoadTexture (L"themes/%s/parts/bishop-black.*", theme->name);
257
   if ((theme->load_index == 50) || (want_all && (theme->load_index < 51)))
258
      theme->flattextures[COLOR_BLACK][PART_QUEEN] = Render_LoadTexture (L"themes/%s/parts/queen-black.*", theme->name);
259
   if ((theme->load_index == 51) || (want_all && (theme->load_index < 52)))
260
      theme->flattextures[COLOR_BLACK][PART_KING] = Render_LoadTexture (L"themes/%s/parts/king-black.*", theme->name);
261
 
262
   //////////////////////////////
263
   // read scene lights from file
264
 
265
   if ((theme->load_index == 52) || (want_all && (theme->load_index < 53)))
266
   {
267
      theme->illum.lights = NULL; // no lights for the scene so far
268
      theme->illum.light_count = 0; // reset the known lights count
269
 
270
      // open the INI file
271
      swprintf_s (filename, WCHAR_SIZEOF (filename), L"themes/%s/lights.ini", theme->name);
272
      inifile = INIFile_LoadINIFile (filename);
273
 
274
      // read the global light values and the parts illumination values
275
      swscanf_s (INIFile_ReadEntryAsString (inifile, L"global", L"ambient light", L"50, 50, 50"), L"%d , %d , %d", &red, &green, &blue);
276
      theme->illum.ambient_light = RGB_TO_RGBACOLOR (red, green, blue);
277
      theme->board_material = Render_MaterialIndexOf (INIFile_ReadEntryAsString (inifile, L"global", L"board material", L"default"));
278
      theme->table_material = Render_MaterialIndexOf (INIFile_ReadEntryAsString (inifile, L"global", L"table material", L"default"));
279
      theme->trim_material = -1; // TODO: make customizable
280
      theme->reflection_alpha = (unsigned char) INIFile_ReadEntryAsLong (inifile, L"global", L"table reflection alpha", 50);
281
      theme->part_colors[COLOR_BLACK].material = Render_MaterialIndexOf (INIFile_ReadEntryAsString (inifile, L"global", L"black material", L"default"));
282
      theme->part_colors[COLOR_WHITE].material = Render_MaterialIndexOf (INIFile_ReadEntryAsString (inifile, L"global", L"white material", L"default"));
283
 
284
      // figure out the number of lights in this file
285
      light_count = INIFile_GetNumberOfSections (inifile) - 1; // deduce the [global] section
286
 
287
      // for each light section...
288
      for (light_index = 0; light_index < light_count; light_index++)
289
      {
290
         swprintf_s (section_name, WCHAR_SIZEOF (section_name), L"light %d", light_index + 1); // build section name
291
         memset (&light, 0, sizeof (light)); // wipe out temporary light structure
292
 
293
         // get this light parameters
294
         wcscpy_s (value, WCHAR_SIZEOF (value), INIFile_ReadEntryAsString (inifile, section_name, L"type", L""));
295
         if (_wcsicmp (value, L"directional") == 0)
296
            light.type = LIGHT_DIRECTIONAL;
297
         else if (_wcsicmp (value, L"point") == 0)
298
            light.type = LIGHT_POINT;
299
         else if (_wcsicmp (value, L"spot") == 0)
300
            light.type = LIGHT_SPOT;
301
         else
302
            continue; // on invalid data, skip this section
303
         swscanf_s (INIFile_ReadEntryAsString (inifile, section_name, L"color", L"0, 0, 0"), L"%d , %d , %d", &red, &green, &blue);
304
         light.color = RGB_TO_RGBACOLOR (red, green, blue);
305
         swscanf_s (INIFile_ReadEntryAsString (inifile, section_name, L"source", L"0.0, 0.0, 0.0"), L"%f , %f , %f", &light.pos_x, &light.pos_y, &light.pos_z);
306
         swscanf_s (INIFile_ReadEntryAsString (inifile, section_name, L"direction", L"0.0, 0.0, 0.0"), L"%f , %f , %f", &light.direction_x, &light.direction_y, &light.direction_z);
307
         light.range = (float) INIFile_ReadEntryAsDouble (inifile, section_name, L"range", 0.0);
308
         swscanf_s (INIFile_ReadEntryAsString (inifile, section_name, L"attenuation", L"0.0, 0.0, 0.0"), L"%f , %f , %f", &light.attenuation_constant, &light.attenuation_proportional, &light.attenuation_square);
309
         swscanf_s (INIFile_ReadEntryAsString (inifile, section_name, L"cone", L"0.0, 0.0"), L"%f , %f", &light.cone_inner, &light.cone_outer);
310
 
311
         // reallocate the lights array to hold one light more
312
         theme->illum.lights = (light_t *) SAFE_realloc (theme->illum.lights, theme->illum.light_count, theme->illum.light_count + 1, sizeof (light_t), false);
313
         memcpy (&theme->illum.lights[theme->illum.light_count], &light, sizeof (light_t)); // copy light in place
314
         theme->illum.light_count++; // we know now one light more
315
      }
316
 
317
      // close the INI file
318
      INIFile_FreeINIFile (inifile);
319
   }
320
 
321
   theme->load_index++; // this bit has been loaded
322
   if (theme->load_index == 53)
323
      theme->is_loaded = true; // when everything has been loaded, remember this theme is now usable
324
 
325
   return; // finished, theme data is loaded a bit more
326
}
327
 
328
 
329
void Background_LoadImage (backgroundsprite_t *bg, const wchar_t *image_pathname)
330
{
331
   // this function loads the specified background image for the specified theme
332
 
333
   int width;
334
   int height;
335
 
336
   // does the background file look like an image we can comprehend ? try to get its width and height
337
   if (GetImageSize (image_pathname, &width, &height))
338
   {
339
      // yes it is, so load it as our background and round its size to the highest multiple of two
340
      bg->sprite_width = 2;
341
      while (bg->sprite_width < width)
342
         bg->sprite_width *= 2; // save them rounded to the next power of two
343
      bg->sprite_height = 2;
344
      while (bg->sprite_height < height)
345
         bg->sprite_height *= 2; // save them rounded to the next power of two
346
 
347
      bg->sprite_index = Render_LoadSprite (image_pathname); // load texture as sprite
348
   }
349
   else
350
   {
351
      // no it's not, so load a default texture
352
      bg->sprite_index = Render_LoadSprite (L"data/notexture.png");
353
      bg->sprite_width = 2;
354
      bg->sprite_height = 2;
355
   }
356
 
357
   return; // finished, theme background image is loaded
358
}