// main.cpp
 
 
 
#include "common.h"
 
 
 
 
 
bool Themes_Init (void)
 
{
 
   // this function (re)builds the themes array and loads all the themes' data
 
 
 
   wchar_t search_pattern[256];
 
   WIN32_FIND_DATA wfd;
 
   HANDLE hFind;
 
   int selectedtheme_index;
 
 
 
   // start by resetting the themes array
 
   themes = NULL;
 
   theme_count = 0;
 
 
 
   // the selected them isn't known yet
 
   selectedtheme_index = -1;
 
 
 
   // build the search pattern string out of the path and initiate the search from that point
 
   swprintf_s (search_pattern, WCHAR_SIZEOF (search_pattern), L"%s\\themes\\*.*", app_path);
 
   hFind = FindFirstFile (search_pattern, &wfd);
 
   if (hFind != INVALID_HANDLE_VALUE)
 
   {
 
      // start doing this...
 
      do
 
      {
 
         // is it this directory or the parent directory ?
 
         if ((wcscmp (wfd.cFileName, L".") == 0) || (wcscmp (wfd.cFileName, L"..") == 0))
 
            continue; // skip that entry
 
 
 
         // is it a directory ?
 
         if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
 
         {
 
            // reallocate space for one theme more (zero out the allocated space) and copy it in place
 
            themes = (theme_t *) SAFE_realloc (themes, theme_count, theme_count + 1, sizeof (theme_t), true);
 
            wcscpy_s (themes[theme_count].name, WCHAR_SIZEOF (themes[theme_count].name), wfd.cFileName);
 
            theme_count++; // we know now one theme more
 
 
 
            // is it the selected theme ?
 
            if (_wcsicmp (wfd.cFileName, wantedtheme_name) == 0)
 
            {
 
               Theme_LoadABitMore (&themes[theme_count - 1]); // start loading the theme data from this directory
 
               selectedtheme_index = theme_count - 1; // remember the selected theme is this one
 
            }
 
         }
 
      } while (FindNextFile (hFind, &wfd)); // ...and don't stop as long as there are files to go
 
 
 
      FindClose (hFind); // close the search handle
 
   }
 
 
 
   // consistency check: we need at least one theme
 
   if (theme_count == 0)
 
   {
 
      MessageBox (NULL, LOCALIZE (L"Error_UnableToLoadThemes"), LOCALIZE (L"FatalError"), MB_ICONERROR | MB_OK);
 
      return (false); // if none of these extensions match, bomb out
 
   }
 
 
 
   // have we found the selected theme ?
 
   if (selectedtheme_index != -1)
 
      theme = &themes[selectedtheme_index]; // found it, so link it
 
   else
 
   {
 
      Theme_LoadABitMore (&themes[0]); // not found, so start loading the first available theme
 
      theme = &themes[0]; // and link selected theme to it
 
   }
 
 
 
   // if we have a custom background specified, load it too
 
   if (custombackground_pathname[0] != 0)
 
      Background_LoadImage (&custombg, custombackground_pathname);
 
 
 
   return (true); // at least one theme has been loaded successfully, return TRUE
 
}
 
 
 
 
 
void Themes_Shutdown (void)
 
{
 
   // helper function to release the themes structures array
 
 
 
   int theme_index;
 
 
 
   // before freeing the themes array, backup our theme's name
 
   wcscpy_s (wantedtheme_name, WCHAR_SIZEOF (wantedtheme_name), theme->name);
 
 
 
   // for each theme, free its mallocated data
 
   for (theme_index = 0; theme_index < theme_count; theme_index++)
 
   {
 
      SAFE_free ((void **) &theme->description); // free its description
 
 
 
      SAFE_free ((void **) &themes[theme_index].illum.lights); // free the lights array
 
      themes[theme_index].illum.light_count = 0;
 
   }
 
 
 
   // free the themes array
 
   SAFE_free ((void **) &themes);
 
   theme_count = 0;
 
 
 
   return; // nothing to do
 
}
 
 
 
 
 
void Theme_LoadABitMore (theme_t *theme)
 
{
 
   // this function loads a particular theme out of a theme subdirectory's files
 
 
 
   #define THEME_LOAD_STEP(id) if (theme->load_index == (id))
 
   #define THEME_LOAD_TEXTURE(varname,texture_name) \
 
   { \
 
      theme->varname = Render_LoadTexture (L"%s/themes/%s/" texture_name L".*", app_path, theme->name); \
 
      if (theme->varname == -1) \
 
         MessageBox (NULL, LOCALIZE (L"Error_UnableToAddTextureD3DXCreateTextureFromFileFailed"), LOCALIZE (L"FatalError"), MB_ICONERROR | MB_OK); \
 
   }
 
   #define THEME_LOAD_MESH(varname,mesh_name) \
 
   { \
 
      theme->varname = Render_LoadMesh (L"%s/themes/%s/" mesh_name L".*", app_path, theme->name); \
 
      if (theme->varname == -1) \
 
         MessageBox (NULL, LOCALIZE (L"Error_UnableToAddMeshD3DXLoadMeshFromXFailed"), LOCALIZE (L"FatalError"), MB_ICONERROR | MB_OK); \
 
   }
 
   #define THEME_LOAD_SPRITE(varname,sprite_name) \
 
   { \
 
      theme->varname = Render_LoadSprite (L"%s/themes/%s/" sprite_name L".*", app_path, theme->name); \
 
      if (theme->varname == -1) \
 
         MessageBox (NULL, LOCALIZE (L"Error_UnableToAddSpriteD3DXCreateSpriteFailed"), LOCALIZE (L"FatalError"), MB_ICONERROR | MB_OK); \
 
   }
 
   #define THEME_LOAD_PART(part_id,part_name) \
 
   { \
 
      theme->part_meshes[part_id] = -1; \
 
      if (options.want_hiquality) \
 
         theme->part_meshes[part_id] = Render_LoadMesh (L"%s/themes/%s/" part_name L"-hidef.*", app_path, theme->name); \
 
      if (theme->part_meshes[part_id] == -1) \
 
         theme->part_meshes[part_id] = Render_LoadMesh (L"%s/themes/%s/" part_name L".*", app_path, theme->name); \
 
      if (theme->part_meshes[part_id] == -1) \
 
         MessageBox (NULL, LOCALIZE (L"Error_UnableToAddMeshD3DXLoadMeshFromXFailed"), LOCALIZE (L"FatalError"), MB_ICONERROR | MB_OK); \
 
   }
 
 
 
   unsigned long file_size;
 
   char *desc_buffer;
 
   wchar_t filename[256];
 
   wchar_t section_name[32];
 
   wchar_t value[128];
 
   light_t light;
 
   void *inifile;
 
   int light_index;
 
   int light_count;
 
   int red;
 
   int green;
 
   int blue;
 
   FILE *fp;
 
 
 
   // allocate space for and load its description
 
   swprintf_s (filename, WCHAR_SIZEOF (filename), L"%s/themes/%s/readme.txt", app_path, theme->name);
 
   _wfopen_s (&fp, filename, L"rb");
 
   if (fp != NULL)
 
   {
 
      fseek (fp, 0, SEEK_END); // seek at end of file
 
      file_size = ftell (fp); // get file size
 
      fseek (fp, 0, SEEK_SET); // and rewind
 
      if (file_size > 0)
 
      {
 
         desc_buffer = (char *) SAFE_malloc (file_size, sizeof (char), false);
 
         fread (desc_buffer, file_size, 1, fp); // read file in whole in ASCII form
 
         fclose (fp); // close it and then convert it to wide char
 
         theme->description = (wchar_t *) SAFE_malloc (file_size, sizeof (wchar_t), false);
 
         ConvertToWideChar (theme->description, file_size, desc_buffer);
 
         SAFE_free ((void **) &desc_buffer); // we no longer need the ASCII buffer
 
      }
 
      else
 
         theme->description = NULL; // this theme has no description
 
      fclose (fp);
 
   }
 
 
 
   // load its background image
 
   THEME_LOAD_STEP (0) Background_LoadImage (&theme->bg, L"%s/themes/%s/background/background.*", app_path, theme->name);
 
 
 
   // load board meshes
 
   THEME_LOAD_STEP (1) THEME_LOAD_MESH (board_meshindex, L"models/board");
 
   THEME_LOAD_STEP (2) THEME_LOAD_MESH (table_meshindex, L"models/table");
 
   THEME_LOAD_STEP (3) THEME_LOAD_MESH (trim_meshindex, L"models/trim");
 
   THEME_LOAD_STEP (4) THEME_LOAD_MESH (tile_meshindex, L"models/tile");
 
 
 
   // load part meshes
 
   THEME_LOAD_STEP (5) THEME_LOAD_PART (PART_KING, L"models/king");
 
   THEME_LOAD_STEP (6) THEME_LOAD_PART (PART_QUEEN, L"models/queen");
 
   THEME_LOAD_STEP (7) THEME_LOAD_PART (PART_BISHOP, L"models/bishop");
 
   THEME_LOAD_STEP (8) THEME_LOAD_PART (PART_KNIGHT, L"models/knight");
 
   THEME_LOAD_STEP (9) THEME_LOAD_PART (PART_ROOK, L"models/rook");
 
   THEME_LOAD_STEP (10) THEME_LOAD_PART (PART_PAWN, L"models/pawn");
 
 
 
   // load part textures
 
   THEME_LOAD_STEP (11) THEME_LOAD_TEXTURE (part_colors[COLOR_BLACK].texture, L"models/part-black");
 
   THEME_LOAD_STEP (12) THEME_LOAD_TEXTURE (part_colors[COLOR_WHITE].texture, L"models/part-white");
 
 
 
   // load board, table, grid and trim textures
 
   THEME_LOAD_STEP (13) THEME_LOAD_TEXTURE (board_texture, L"models/board");
 
   THEME_LOAD_STEP (14) THEME_LOAD_TEXTURE (table_texture, L"models/table");
 
   THEME_LOAD_STEP (15) THEME_LOAD_TEXTURE (trim_texture, L"models/trim");
 
   THEME_LOAD_STEP (16) THEME_LOAD_TEXTURE (grid_texture, L"models/grid");
 
 
 
   // load overlay textures
 
   THEME_LOAD_STEP (17) THEME_LOAD_TEXTURE (shadow_textureindex, L"shadow");
 
   THEME_LOAD_STEP (18) THEME_LOAD_TEXTURE (hovered_textureindex, L"hovered");
 
   THEME_LOAD_STEP (19) THEME_LOAD_TEXTURE (check_textureindex, L"check");
 
   THEME_LOAD_STEP (20) THEME_LOAD_TEXTURE (threat_textureindex, L"takeable");
 
   THEME_LOAD_STEP (21) THEME_LOAD_TEXTURE (lastmovesource_textureindex, L"lastmovesource");
 
   THEME_LOAD_STEP (22) THEME_LOAD_TEXTURE (lastmovetarget_textureindex, L"lastmovetarget");
 
   THEME_LOAD_STEP (23) THEME_LOAD_TEXTURE (selected_textureindex, L"selected");
 
   THEME_LOAD_STEP (24) THEME_LOAD_TEXTURE (possiblemove_textureindex, L"possiblemove");
 
   THEME_LOAD_STEP (25) THEME_LOAD_TEXTURE (takeable_textureindex, L"takeable");
 
 
 
   // load sprites
 
   THEME_LOAD_STEP (26) THEME_LOAD_SPRITE (flatsprites[COLOR_WHITE][PART_PAWN], L"parts/pawn-white");
 
   THEME_LOAD_STEP (27) THEME_LOAD_SPRITE (flatsprites[COLOR_WHITE][PART_ROOK], L"parts/rook-white");
 
   THEME_LOAD_STEP (28) THEME_LOAD_SPRITE (flatsprites[COLOR_WHITE][PART_KNIGHT], L"parts/knight-white");
 
   THEME_LOAD_STEP (29) THEME_LOAD_SPRITE (flatsprites[COLOR_WHITE][PART_BISHOP], L"parts/bishop-white");
 
   THEME_LOAD_STEP (30) THEME_LOAD_SPRITE (flatsprites[COLOR_WHITE][PART_QUEEN], L"parts/queen-white");
 
   THEME_LOAD_STEP (31) THEME_LOAD_SPRITE (flatsprites[COLOR_WHITE][PART_KING], L"parts/king-white");
 
   THEME_LOAD_STEP (32) THEME_LOAD_SPRITE (flatsprites[COLOR_BLACK][PART_PAWN], L"parts/pawn-black");
 
   THEME_LOAD_STEP (33) THEME_LOAD_SPRITE (flatsprites[COLOR_BLACK][PART_ROOK], L"parts/rook-black");
 
   THEME_LOAD_STEP (34) THEME_LOAD_SPRITE (flatsprites[COLOR_BLACK][PART_KNIGHT], L"parts/knight-black");
 
   THEME_LOAD_STEP (35) THEME_LOAD_SPRITE (flatsprites[COLOR_BLACK][PART_BISHOP], L"parts/bishop-black");
 
   THEME_LOAD_STEP (36) THEME_LOAD_SPRITE (flatsprites[COLOR_BLACK][PART_QUEEN], L"parts/queen-black");
 
   THEME_LOAD_STEP (37) THEME_LOAD_SPRITE (flatsprites[COLOR_BLACK][PART_KING], L"parts/king-black");
 
   THEME_LOAD_STEP (38) THEME_LOAD_SPRITE (lastmovesource_spriteindex, L"lastmovesource");
 
   THEME_LOAD_STEP (39) THEME_LOAD_SPRITE (lastmovetarget_spriteindex, L"lastmovetarget");
 
 
 
   // load the flat pieces textures
 
   THEME_LOAD_STEP (40) THEME_LOAD_TEXTURE (flattextures[COLOR_WHITE][PART_PAWN], L"parts/pawn-white");
 
   THEME_LOAD_STEP (41) THEME_LOAD_TEXTURE (flattextures[COLOR_WHITE][PART_ROOK], L"parts/rook-white");
 
   THEME_LOAD_STEP (42) THEME_LOAD_TEXTURE (flattextures[COLOR_WHITE][PART_KNIGHT], L"parts/knight-white");
 
   THEME_LOAD_STEP (43) THEME_LOAD_TEXTURE (flattextures[COLOR_WHITE][PART_BISHOP], L"parts/bishop-white");
 
   THEME_LOAD_STEP (44) THEME_LOAD_TEXTURE (flattextures[COLOR_WHITE][PART_QUEEN], L"parts/queen-white");
 
   THEME_LOAD_STEP (45) THEME_LOAD_TEXTURE (flattextures[COLOR_WHITE][PART_KING], L"parts/king-white");
 
   THEME_LOAD_STEP (46) THEME_LOAD_TEXTURE (flattextures[COLOR_BLACK][PART_PAWN], L"parts/pawn-black");
 
   THEME_LOAD_STEP (47) THEME_LOAD_TEXTURE (flattextures[COLOR_BLACK][PART_ROOK], L"parts/rook-black");
 
   THEME_LOAD_STEP (48) THEME_LOAD_TEXTURE (flattextures[COLOR_BLACK][PART_KNIGHT], L"parts/knight-black");
 
   THEME_LOAD_STEP (49) THEME_LOAD_TEXTURE (flattextures[COLOR_BLACK][PART_BISHOP], L"parts/bishop-black");
 
   THEME_LOAD_STEP (50) THEME_LOAD_TEXTURE (flattextures[COLOR_BLACK][PART_QUEEN], L"parts/queen-black");
 
   THEME_LOAD_STEP (51) THEME_LOAD_TEXTURE (flattextures[COLOR_BLACK][PART_KING], L"parts/king-black");
 
 
 
   //////////////////////////////
 
   // read scene lights from file
 
 
 
   THEME_LOAD_STEP (52)
 
   {
 
      theme->illum.lights = NULL; // no lights for the scene so far
 
      theme->illum.light_count = 0; // reset the known lights count
 
 
 
      // open the INI file
 
      swprintf_s (filename, WCHAR_SIZEOF (filename), L"%s/themes/%s/lights.ini", app_path, theme->name);
 
      inifile = INIFile_LoadINIFile (filename);
 
 
 
      // read the global light values and the parts illumination values
 
      swscanf_s (INIFile_ReadEntryAsString (inifile, L"global", L"ambient light", L"50, 50, 50"), L"%d , %d , %d", &red, &green, &blue);
 
      theme->illum.ambient_light = RGB_TO_RGBACOLOR (red, green, blue);
 
      theme->board_material = Render_MaterialIndexOf (INIFile_ReadEntryAsString (inifile, L"global", L"board material", L"default"));
 
      theme->table_material = Render_MaterialIndexOf (INIFile_ReadEntryAsString (inifile, L"global", L"table material", L"default"));
 
      theme->trim_material = -1; // TODO: make customizable
 
      theme->reflection_alpha = (unsigned char) INIFile_ReadEntryAsLong (inifile, L"global", L"table reflection alpha", 50);
 
      theme->part_colors[COLOR_BLACK].material = Render_MaterialIndexOf (INIFile_ReadEntryAsString (inifile, L"global", L"black material", L"default"));
 
      theme->part_colors[COLOR_WHITE].material = Render_MaterialIndexOf (INIFile_ReadEntryAsString (inifile, L"global", L"white material", L"default"));
 
 
 
      // figure out the number of lights in this file
 
      light_count = INIFile_GetNumberOfSections (inifile) - 1; // deduce the [global] section
 
 
 
      // for each light section...
 
      for (light_index = 0; light_index < light_count; light_index++)
 
      {
 
         swprintf_s (section_name, WCHAR_SIZEOF (section_name), L"light %d", light_index + 1); // build section name
 
         memset (&light, 0, sizeof (light)); // wipe out temporary light structure
 
 
 
         // get this light parameters
 
         wcscpy_s (value, WCHAR_SIZEOF (value), INIFile_ReadEntryAsString (inifile, section_name, L"type", L""));
 
         if (_wcsicmp (value, L"directional") == 0)
 
            light.type = LIGHT_DIRECTIONAL;
 
         else if (_wcsicmp (value, L"point") == 0)
 
            light.type = LIGHT_POINT;
 
         else if (_wcsicmp (value, L"spot") == 0)
 
            light.type = LIGHT_SPOT;
 
         else
 
            continue; // on invalid data, skip this section
 
         swscanf_s (INIFile_ReadEntryAsString (inifile, section_name, L"color", L"0, 0, 0"), L"%d , %d , %d", &red, &green, &blue);
 
         light.color = RGB_TO_RGBACOLOR (red, green, blue);
 
         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);
 
         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);
 
         light.range = (float) INIFile_ReadEntryAsDouble (inifile, section_name, L"range", 0.0);
 
         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);
 
         swscanf_s (INIFile_ReadEntryAsString (inifile, section_name, L"cone", L"0.0, 0.0"), L"%f , %f", &light.cone_inner, &light.cone_outer);
 
 
 
         // reallocate the lights array to hold one light more
 
         theme->illum.lights = (light_t *) SAFE_realloc (theme->illum.lights, theme->illum.light_count, theme->illum.light_count + 1, sizeof (light_t), false);
 
         memcpy (&theme->illum.lights[theme->illum.light_count], &light, sizeof (light_t)); // copy light in place
 
         theme->illum.light_count++; // we know now one light more
 
      }
 
 
 
      // close the INI file
 
      INIFile_FreeINIFile (inifile);
 
   }
 
 
 
   theme->load_index++; // this bit has been loaded
 
   if (theme->load_index == 53)
 
      theme->is_loaded = true; // when everything has been loaded, remember this theme is now usable
 
 
 
   return; // finished, theme data is loaded a bit more
 
 
 
   #undef THEME_LOAD_PART
 
   #undef THEME_LOAD_SPRITE
 
   #undef THEME_LOAD_MESH
 
   #undef THEME_LOAD_TEXTURE
 
   #undef THEME_LOAD_STEP
 
}
 
 
 
 
 
void Background_LoadImage (backgroundsprite_t *bg, const wchar_t *fmt, ...)
 
{
 
   // this function loads the specified background image for the specified theme
 
 
 
   static wchar_t bgimage_pathname[MAX_PATH];
 
   va_list argptr;
 
   int width;
 
   int height;
 
 
 
   // concatenate all the arguments in one string
 
   va_start (argptr, fmt);
 
   wvsprintf (bgimage_pathname, fmt, argptr);
 
   va_end (argptr);
 
 
 
   // does the background file look like an image we can comprehend ? try to get its width and height
 
   if (GetImageSize (bgimage_pathname, &width, &height))
 
   {
 
      // yes it is, so load it as our background and round its size to the highest multiple of two
 
      bg->sprite_width = 2;
 
      while (bg->sprite_width < width)
 
         bg->sprite_width *= 2; // save them rounded to the next power of two
 
      bg->sprite_height = 2;
 
      while (bg->sprite_height < height)
 
         bg->sprite_height *= 2; // save them rounded to the next power of two
 
 
 
      bg->sprite_index = Render_LoadSprite (bgimage_pathname); // load texture as sprite
 
   }
 
   else
 
   {
 
      // no it's not, so load a default texture
 
      bg->sprite_index = Render_LoadSprite (L"%s/data/notexture.png", app_path);
 
      bg->sprite_width = 2;
 
      bg->sprite_height = 2;
 
   }
 
 
 
   return; // finished, theme background image is loaded
 
}