// main.cpp
 
 
 
#define DEFINE_GLOBALS
 
#include "common.h"
 
 
 
 
 
// handy macros
 
#define GUIBUTTON_ENABLE(button) { if ((button).state == 0) (button).state = 1; }
 
#define GUIBUTTON_DISABLE(button) { if ((button).state > 0) (button).state = 0; }
 
 
 
 
 
// prototypes of locally used functions
 
static void MainLoop_FindCurrentViewer (void);
 
static void MainLoop_EvaluateGameState (void);
 
 
 
 
 
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, char *lpCmdLine, int nCmdShow)
 
{
 
   // the entry point for any Windows program
 
 
 
   WNDCLASSEX wc;
 
   MENUINFO menu_info;
 
   MSG msg;
 
   HBITMAP hSplashBmp;
 
   BITMAP splash_bmp;
 
   HDC hdc;
 
   HMENU hMenu;
 
   HMENU hDropDownMenu;
 
   RECT rect;
 
   PAINTSTRUCT ps;
 
   HBITMAP hbmTmp;
 
   HDC hdcMem;
 
   float previous_time;
 
   int frame_count;
 
   int array_index;
 
   bool is_success;
 
   char *endptr;
 
   wchar_t app_pathname[MAX_PATH];
 
   wchar_t font_pathname[MAX_PATH];
 
   wchar_t temp_string[100];
 
   struct _stat fileinfo;
 
   HACCEL hAccelerators;
 
   ACCEL accelerators[] =
 
   {
 
      {FVIRTKEY | FCONTROL, L'O',     MENUID_GAME_LOAD},
 
      {FVIRTKEY | FCONTROL, L'S',     MENUID_GAME_SAVE},
 
      {FVIRTKEY | FCONTROL, L'Z',     MENUID_CHESSBOARD_CANCELLASTMOVE},
 
      {FVIRTKEY,            VK_HOME,  MENUID_CHESSBOARD_BEGINNINGOFGAME},
 
      {FVIRTKEY,            VK_LEFT,  MENUID_CHESSBOARD_PREVIOUSMOVE},
 
      {FVIRTKEY,            VK_RIGHT, MENUID_CHESSBOARD_NEXTMOVE},
 
      {FVIRTKEY,            VK_END,   MENUID_CHESSBOARD_CURRENTSTATEOFGAME},
 
      {FVIRTKEY | FCONTROL, L'G',     MENUID_CHESSBOARD_GOTOMOVE},
 
      {FVIRTKEY,            VK_F1,    MENUID_HELP_HELP},
 
      {FVIRTKEY,            VK_F2,    MENUID_GAME_NEWGAME},
 
      {FVIRTKEY,            VK_F3,    MENUID_GAME_STATISTICS},
 
      {FVIRTKEY,            VK_F4,    MENUID_GAME_OPTIONS},
 
      {FVIRTKEY,            VK_F5,    MENUID_CHESSBOARD_TOPVIEW},
 
      {FVIRTKEY,            VK_F6,    MENUID_CHESSBOARD_DEFAULTVIEW},
 
      {FVIRTKEY,            VK_F7,    MENUID_CHESSBOARD_RESETVIEW},
 
      {FVIRTKEY,            VK_UP,    MENUID_CHESSBOARD_ZOOMIN},
 
      {FVIRTKEY,            VK_DOWN,  MENUID_CHESSBOARD_ZOOMOUT},
 
      {FVIRTKEY | FCONTROL, VK_DOWN,  MENUID_CHESSBOARD_DISPLAYWINDOWSDESKTOP},
 
      {FVIRTKEY,            VK_F8,    MENUID_CHESSBOARD_CHANGEAPPEARANCE},
 
      {FVIRTKEY,            VK_F9,    MENUID_INTERNET_SHOWONLINEPLAYERS},
 
      {FVIRTKEY,            VK_F10,   MENUID_INTERNET_SHOWSOUGHTGAMES},
 
   };
 
 
 
   // save application instance
 
   hAppInstance = hInstance;
 
 
 
   // find module and path names, and *IMPORTANT*, set the current working directory there
 
   GetModuleFileName (NULL, app_pathname, WCHAR_SIZEOF (app_pathname));
 
   GetDirectoryPath (app_pathname, app_path);
 
   SetCurrentDirectoryW (app_path);
 
 
 
   // initialize stuff
 
   is_registered = false;
 
   terminate_everything = false;
 
   hMainWnd = NULL;
 
   hChatterChannelsWnd = NULL;
 
   hGamesWnd = NULL;
 
   hMOTDWnd = NULL;
 
   hOpponentsWnd = NULL;
 
   hSoughtWnd = NULL;
 
#ifdef NDEBUG
 
   want_framerate = false; // release mode, don't display framerate
 
#else
 
   want_framerate = true; // display framerate in debug mode
 
#endif // NDEBUG
 
   is_dialogbox_about_validated = false;
 
   is_dialogbox_challenge_validated = false;
 
   is_dialogbox_changeappearance_validated = false;
 
   is_dialogbox_comment_validated = false;
 
   is_dialogbox_endgame_validated = false;
 
   is_dialogbox_gotomove_validated = false;
 
   is_dialogbox_load_validated = false;
 
   is_dialogbox_message_validated = false;
 
   is_dialogbox_newgame_validated = false;
 
   is_dialogbox_options_validated = false;
 
   is_dialogbox_pawnpromotion_validated = false;
 
   is_dialogbox_playercard_validated = false;
 
   is_dialogbox_playerinfoname_validated = false;
 
   is_dialogbox_quit_validated = false;
 
   is_dialogbox_resign_validated = false;
 
   is_dialogbox_save_validated = false;
 
   is_dialogbox_saveposition_validated = false;
 
   is_dialogbox_sendchallenge_validated = false;
 
   is_dialogbox_sendseek_validated = false;
 
   is_dialogbox_takeback_validated = false;
 
   is_window_chat_validated = false;
 
   is_window_chatterchannels_validated = false;
 
   is_window_games_validated = false;
 
   is_window_motd_validated = false;
 
   is_window_opponents_validated = false;
 
   is_window_sought_validated = false;
 
   save_pathname[0] = 0;
 
   srand ((unsigned int) time (NULL)); // initialize PRNG
 
   animation_endtime = 0.0f;
 
   command_ignoretime = 0.0f;
 
   sound_playtime = 0.0f;
 
   highlight_endtime = 0.0f;
 
   previous_time = 0.0f;
 
   frame_count = 0;
 
 
 
   // read configuration data
 
   Config_Load ();
 
 
 
   // see if the program is registered
 
   is_registered = IsRegistrationCorrect (options.registration.user_email, options.registration.activation_code);
 
 
 
   // read texts in the right language, if such a translation exists
 
   is_success = false;
 
   GetLocaleInfo (LOCALE_SYSTEM_DEFAULT, LOCALE_SENGLANGUAGE, os_language, WCHAR_SIZEOF (os_language));
 
   is_success = LocalizedTexts_Init (L"%s/data/languages/%s.ini", app_path, os_language);
 
 
 
   // fallback to English if language not found
 
   if (!is_success)
 
   {
 
      wcscpy_s (os_language, WCHAR_SIZEOF (os_language), L"English");
 
      is_success = LocalizedTexts_Init (L"%s/data/languages/%s.ini", app_path, os_language);
 
   }
 
 
 
   // consistency check: don't go further if texts aren't available
 
   if (!is_success)
 
      return (-1); // bomb out on error
 
 
 
   // create the main menu line
 
   hMenu = CreateMenu ();
 
   hDropDownMenu = CreateMenu (); // create the first drop-down item
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_GAME_NEWGAME, LOCALIZE (L"Menu_GameNewGame"));
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_GAME_LOAD, LOCALIZE (L"Menu_GameLoadGame"));
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_GAME_SETUPPOSITION, LOCALIZE (L"Menu_GameSetupPosition"));
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_GAME_SAVE, LOCALIZE (L"Menu_GameSaveGame")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_GAME_SAVEAS, LOCALIZE (L"Menu_GameSaveGameAs")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_GAME_SAVEPOSITIONAS, LOCALIZE (L"Menu_GameSavePositionAs")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_GAME_RESIGN, LOCALIZE (L"Menu_GameResign"));
 
   AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_GAME_STATISTICS, LOCALIZE (L"Menu_GameStatistics"));
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_GAME_OPTIONS, LOCALIZE (L"Menu_GameOptions"));
 
   AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_GAME_QUIT, LOCALIZE (L"Menu_GameQuit"));
 
   AppendMenu (hMenu, MF_POPUP, (UINT) hDropDownMenu, LOCALIZE (L"Menu_Game"));
 
   DestroyMenu (hDropDownMenu);
 
   hDropDownMenu = CreateMenu (); // create the second drop-down item
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_CHESSBOARD_SUGGESTMOVE, LOCALIZE (L"Menu_ChessboardSuggestMove")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_CHESSBOARD_CANCELLASTMOVE, LOCALIZE (L"Menu_ChessboardCancelLastMove")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_CHESSBOARD_COMMENTMOVE, LOCALIZE (L"Menu_ChessboardCommentMove")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_CHESSBOARD_GOTOMOVE, LOCALIZE (L"Menu_ChessboardGoToMove")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_CHESSBOARD_SWAPSIDES, LOCALIZE (L"Menu_ChessboardSwapSides")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_CHESSBOARD_TOPVIEW, LOCALIZE (L"Menu_ChessboardTopView"));
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_CHESSBOARD_DEFAULTVIEW, LOCALIZE (L"Menu_ChessboardDefaultView"));
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_CHESSBOARD_RESETVIEW, LOCALIZE (L"Menu_ChessboardResetView"));
 
   AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_CHESSBOARD_CHANGEAPPEARANCE, LOCALIZE (L"Menu_ChessboardChangeAppearance"));
 
   if (options.want_fullscreen)
 
   {
 
      AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
 
      AppendMenu (hDropDownMenu, MF_STRING, MENUID_CHESSBOARD_DISPLAYWINDOWSDESKTOP, LOCALIZE (L"Menu_ChessboardDisplayWindowsDesktop"));
 
   }
 
   AppendMenu (hMenu, MF_POPUP, (UINT) hDropDownMenu, LOCALIZE (L"Menu_Chessboard"));
 
   DestroyMenu (hDropDownMenu);
 
   hDropDownMenu = CreateMenu (); // create the third drop-down item
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_SHOWONLINEPLAYERS, LOCALIZE (L"Menu_InternetShowOnlinePlayers")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_SHOWSOUGHTGAMES, LOCALIZE (L"Menu_InternetShowSoughtGames")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_SEEKGAME, LOCALIZE (L"Menu_InternetSeekGame")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_CHATTERCHANNELS, LOCALIZE (L"Menu_InternetChatterChannels")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_ENTERCHATTEXT, LOCALIZE (L"Menu_InternetEnterChatText")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_DISPLAYPLAYERCARD, LOCALIZE (L"Menu_InternetDisplayPlayerCard")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_DISPLAYYOURCARD, LOCALIZE (L"Menu_InternetDisplayYourCard")); // initially grayed
 
   AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
 
   AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_MOTD, LOCALIZE (L"Menu_InternetDisplayMOTD")); // initially grayed
 
   AppendMenu (hMenu, MF_POPUP, (UINT) hDropDownMenu, LOCALIZE (L"Menu_Internet"));
 
   DestroyMenu (hDropDownMenu);
 
   hDropDownMenu = CreateMenu (); // create the fourth drop-down item
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_HELP_HELP, LOCALIZE (L"Menu_HelpDisplayHelp"));
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_HELP_GETCHESSGAMES, LOCALIZE (L"Menu_HelpGetChessGames"));
 
   AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_HELP_ADDMODIFYVISUALTHEMES, LOCALIZE (L"Menu_HelpAddModifyThemes"));
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_HELP_ADDMODIFYENGINES, LOCALIZE (L"Menu_HelpAddModifyEngines"));
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_HELP_ADDMODIFYTRANSLATIONS, LOCALIZE (L"Menu_HelpAddModifyTranslations"));
 
   AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
 
   AppendMenu (hDropDownMenu, MF_STRING, MENUID_HELP_ABOUT, LOCALIZE (L"Menu_HelpAbout"));
 
   AppendMenu (hMenu, MF_POPUP, (UINT) hDropDownMenu, LOCALIZE (L"Menu_Help"));
 
   DestroyMenu (hDropDownMenu);
 
 
 
   // create the accelerators
 
   hAccelerators = CreateAcceleratorTable (accelerators, sizeof (accelerators) / sizeof (ACCEL));
 
 
 
   // register the window class, create the window and show it
 
   memset (&wc, 0, sizeof (wc));
 
   wc.cbSize = sizeof (wc);
 
   wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; // double-clicks support
 
   wc.lpfnWndProc = WindowProc_Main;
 
   wc.hInstance = hAppInstance;
 
   wc.hIcon = LoadIcon (hAppInstance, (wchar_t *) ICON_MAIN);
 
   wc.hCursor = LoadCursor (NULL, IDC_ARROW);
 
   wc.lpszClassName = PROGRAM_NAME L" WndClass";
 
   RegisterClassEx (&wc);
 
   swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), PROGRAM_NAME L"%s", (is_registered ? L"" : LOCALIZE (L"EvaluationMode"))); // build window title
 
   if (options.want_fullscreen)
 
      hMainWnd = CreateWindowEx (0, wc.lpszClassName, temp_string, WS_POPUPWINDOW, // temp_string holds window title
 
                                 0, 0, GetSystemMetrics (SM_CXSCREEN), GetSystemMetrics (SM_CYSCREEN),
 
                                 NULL, hMenu, hAppInstance, NULL);
 
   else
 
   {
 
      // in windowed mode, ensure window width and height aren't greater than screen size nor lower than a safe minimum
 
      if (options.window_width > GetSystemMetrics (SM_CXMAXTRACK))
 
         options.window_width = GetSystemMetrics (SM_CXMAXTRACK); // check this first in case screen size is reported incorrect
 
      if (options.window_height > GetSystemMetrics (SM_CYMAXTRACK))
 
         options.window_height = GetSystemMetrics (SM_CYMAXTRACK);
 
      if (options.window_width < 640)
 
         options.window_width = 640; // check this secondly in case screen size is reported incorrect
 
      if (options.window_height < 480)
 
         options.window_height = 480;
 
 
 
      hMainWnd = CreateWindowEx (0, wc.lpszClassName, temp_string, WS_OVERLAPPEDWINDOW, // temp_string holds window title
 
                                 GetSystemMetrics (SM_CXSCREEN) / 2 - options.window_width / 2,
 
                                 GetSystemMetrics (SM_CYSCREEN) / 2 - options.window_height / 2,
 
                                 options.window_width,
 
                                 options.window_height,
 
                                 NULL, hMenu, hAppInstance, NULL);
 
   }
 
   ShowWindow (hMainWnd, nCmdShow);
 
 
 
   // display the splash screen
 
   memset ((void *) &splash_bmp, 0, sizeof (splash_bmp));
 
   hSplashBmp = W32LoadImage (L"%s/data/splash.bmp", app_path); // load the splash bitmap
 
   GetObject (hSplashBmp, sizeof (splash_bmp), (void *) &splash_bmp); // get a structure containing its size (among others)
 
   hdcMem = CreateCompatibleDC (NULL); // create a device context compatible with the screen
 
   hbmTmp = SelectBitmap (hdcMem, hSplashBmp); // select our bitmap to use in it
 
   hdc = BeginPaint (hMainWnd, &ps); // begin painting on the main window
 
   GetClientRect (hMainWnd, &rect);
 
   StretchBlt (hdc, 0, 0, rect.right, rect.bottom, hdcMem, 0, 0, splash_bmp.bmWidth, splash_bmp.bmHeight, SRCCOPY); // bit blit the bitmap into it
 
   EndPaint (hMainWnd, &ps); // end painting on the main window
 
   SelectObject (hdcMem, hbmTmp); // restore the previous selection
 
   DeleteDC (hdcMem); // and delete the handles to the device context
 
   DeleteObject (hSplashBmp); // and to the bitmap that we used
 
 
 
   // make the menu modeless
 
   memset (&menu_info, 0, sizeof (menu_info)); // prepare menu info structure
 
   menu_info.cbSize = sizeof (MENUINFO);
 
   menu_info.fMask = MIM_STYLE;
 
   GetMenuInfo (GetMenu (hMainWnd), &menu_info); // get current style
 
   menu_info.dwStyle |= MNS_MODELESS; // add the "modeless" flag
 
   SetMenuInfo (GetMenu (hMainWnd), &menu_info); // and send it back
 
 
 
   // load status icons, bitmaps and texts
 
   handlestatus[HANDLESTATUS_AVAILABLE].icon = W32LoadIcon (L"%s/data/icons/available.ico", app_path);
 
   handlestatus[HANDLESTATUS_AVAILABLE].bitmap = W32LoadImage (L"%s/data/status/available.bmp", app_path);
 
   handlestatus[HANDLESTATUS_AVAILABLE].text = LOCALIZE (L"Opponents_StatusAvailable");
 
   handlestatus[HANDLESTATUS_INGAME].icon = W32LoadIcon (L"%s/data/icons/ingame.ico", app_path);
 
   handlestatus[HANDLESTATUS_INGAME].bitmap = W32LoadImage (L"%s/data/status/ingame.bmp", app_path);
 
   handlestatus[HANDLESTATUS_INGAME].text = LOCALIZE (L"Opponents_StatusInGame");
 
   handlestatus[HANDLESTATUS_INSIMULATION].icon = W32LoadIcon (L"%s/data/icons/insimulation.ico", app_path);
 
   handlestatus[HANDLESTATUS_INSIMULATION].bitmap = W32LoadImage (L"%s/data/status/insimulation.bmp", app_path);
 
   handlestatus[HANDLESTATUS_INSIMULATION].text = LOCALIZE (L"Opponents_StatusInSimulation");
 
   handlestatus[HANDLESTATUS_INTOURNAMENT].icon = W32LoadIcon (L"%s/data/icons/intournament.ico", app_path);
 
   handlestatus[HANDLESTATUS_INTOURNAMENT].bitmap = W32LoadImage (L"%s/data/status/intournament.bmp", app_path);
 
   handlestatus[HANDLESTATUS_INTOURNAMENT].text = LOCALIZE (L"Opponents_StatusInTournament");
 
   handlestatus[HANDLESTATUS_EXAMININGAGAME].icon = W32LoadIcon (L"%s/data/icons/examiningagame.ico", app_path);
 
   handlestatus[HANDLESTATUS_EXAMININGAGAME].bitmap = W32LoadImage (L"%s/data/status/examiningagame.bmp", app_path);
 
   handlestatus[HANDLESTATUS_EXAMININGAGAME].text = LOCALIZE (L"Opponents_StatusExaminingAGame");
 
   handlestatus[HANDLESTATUS_NOTOPENFORAMATCH].icon = W32LoadIcon (L"%s/data/icons/notopenforamatch.ico", app_path);
 
   handlestatus[HANDLESTATUS_NOTOPENFORAMATCH].bitmap = W32LoadImage (L"%s/data/status/notopenforamatch.bmp", app_path);
 
   handlestatus[HANDLESTATUS_NOTOPENFORAMATCH].text = LOCALIZE (L"Opponents_StatusNotOpenForAMatch");
 
   handlestatus[HANDLESTATUS_INACTIVEORBUSY].icon = W32LoadIcon (L"%s/data/icons/inactiveorbusy.ico", app_path);
 
   handlestatus[HANDLESTATUS_INACTIVEORBUSY].bitmap = W32LoadImage (L"%s/data/status/inactiveorbusy.bmp", app_path);
 
   handlestatus[HANDLESTATUS_INACTIVEORBUSY].text = LOCALIZE (L"Opponents_StatusInactiveOrBusy");
 
   handlestatus[HANDLESTATUS_OFFLINE].icon = W32LoadIcon (L"%s/data/icons/offline.ico", app_path);
 
   handlestatus[HANDLESTATUS_OFFLINE].bitmap = W32LoadImage (L"%s/data/status/offline.bmp", app_path);
 
   handlestatus[HANDLESTATUS_OFFLINE].text = LOCALIZE (L"Opponents_StatusOffline");
 
 
 
   // load the system fonts
 
   hFontChat = CreateFont (17, 0, 0, 0, FW_DONTCARE, false, false, false, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
 
                           CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Comic Sans MS");
 
 
 
   // before any rendering is done, it's a good idea to know what time it is
 
   current_time = ProcessTime ();
 
 
 
   // initialize renderer
 
   if (!Render_Init ())
 
      return (-1); // bomb out on error
 
 
 
   // load sprites
 
   larrow_spriteindex = Render_LoadSprite (L"%s/data/sprites/arrow-left.*", app_path);
 
   rarrow_spriteindex = Render_LoadSprite (L"%s/data/sprites/arrow-right.*", app_path);
 
   chatbutton_spriteindex = Render_LoadSprite (L"%s/data/sprites/chat.*", app_path);
 
   gamesbutton_spriteindex = Render_LoadSprite (L"%s/data/sprites/games.*", app_path);
 
   peoplebutton_spriteindex = Render_LoadSprite (L"%s/data/sprites/people.*", app_path);
 
   sepia_spriteindex = Render_LoadSprite (L"%s/data/sprites/sepia.*", app_path);
 
   for (array_index = 0; array_index < 12; array_index++)
 
      spinner_spriteindex[array_index] = Render_LoadSprite (L"%s/data/sprites/spinner-%d.png", app_path, array_index * 30); // spinning wheel
 
 
 
   // add our custom fonts to the list of available fonts for the duration of the process
 
   swprintf_s (font_pathname, WCHAR_SIZEOF (font_pathname), L"%s/data/fonts/papyrus.ttf", app_path);
 
   AddFontResourceEx (font_pathname, FR_PRIVATE, 0);
 
   // load rendered fonts
 
   arrow_fontindex = Render_LoadFont (L"Papyrus", 24, false, false);
 
   chat_fontindex = Render_LoadFont (L"Papyrus", 32, false, false);
 
   players_fontindex = Render_LoadFont (L"Papyrus", 40, false, true);
 
   centermsg_fontindex = Render_LoadFont (L"Papyrus", 54, false, true);
 
 
 
   // load themes, initialize a new human vs. human game and setup the scene
 
   if (!Themes_Init ())
 
      return (-1); // bomb out on error
 
   Board_Init (&the_board, PLAYER_HUMAN, PLAYER_HUMAN, FENSTARTUP_STANDARDCHESS);
 
   Scene_Init (&the_scene, &the_board);
 
 
 
   // has a filename been specifed on the command-line AND that file exists ?
 
   if ((lpCmdLine != NULL) && (lpCmdLine[0] != 0))
 
   {
 
      while (*lpCmdLine == '"')
 
         lpCmdLine++; // skip leading quotes
 
      if ((endptr = strchr (lpCmdLine, '"')) != NULL)
 
         *endptr = 0; // break the string at the first ending quote
 
 
 
      // is it a file that exists ?
 
      if (_stat (lpCmdLine, &fileinfo) == 0)
 
      {
 
         ConvertToWideChar (load_pathname, WCHAR_SIZEOF (load_pathname), lpCmdLine); // save pathname string
 
         is_dialogbox_load_validated = true; // and act as if the user just validated a load dialog box
 
      }
 
#ifndef NDEBUG
 
      // else is it a registration info ? if so, parse it
 
      else if ((strncmp (lpCmdLine, "/r=", 3) == 0) && ((endptr = strchr (&lpCmdLine[3], ',')) != NULL))
 
      {
 
         *endptr = 0; // break the string at the separator between user email and activation code
 
         ConvertToWideChar (temp_string, WCHAR_SIZEOF (temp_string), &lpCmdLine[3]); // read user email
 
         is_registered = IsRegistrationCorrect (temp_string, atoi (endptr + 1)); // and see whether we're registered or not
 
         DialogBox_NewGame (); // still open the "new game" dialog box
 
      }
 
#endif // !NDEBUG
 
      else
 
         DialogBox_NewGame (); // if specified filename doesn't exist, fallback to the "new game" dialog box
 
   }
 
   else
 
      DialogBox_NewGame (); // when no filename is specified, display the new game dialog box
 
 
 
   // enter the main loop
 
   while (!terminate_everything)
 
   {
 
      // see what time it is
 
      current_time = ProcessTime ();
 
 
 
      // are we in demo mode and is it time to quit ?
 
      if (!is_registered && (current_time > DEMO_TIMEOUT))
 
         DestroyWindow (hMainWnd); // if so, send a quit message in order to break the loop
 
 
 
      // grab the current window size
 
      GetWindowRect (hMainWnd, &rect);
 
      options.window_width = rect.right - rect.left;
 
      options.window_height = rect.bottom - rect.top;
 
 
 
      // are we in the middle of an animation or just after it ?
 
      if (current_time < animation_endtime + 0.5f)
 
         the_scene.update = true; // always update during animations
 
      else
 
         the_scene.update |= Board_Think (&the_board); // make the board (i.e, both players) think
 
 
 
      MainLoop_FindCurrentViewer ();  // determine current viewer
 
 
 
      // see if we have a message waiting for any window in the current thread and dispatch it
 
      while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
 
      {
 
         // check for accelerator keystrokes from the main window
 
         if ((msg.hwnd != hMainWnd) || !TranslateAccelerator (hMainWnd, hAccelerators, &msg))
 
         {
 
            TranslateMessage (&msg); // translate and dispatch all other messages
 
            DispatchMessage (&msg);
 
         }
 
      }
 
 
 
      // handle dialog box and window return codes
 
      if (is_dialogbox_about_validated) DialogBox_About_Validated ();
 
      if (is_dialogbox_challenge_validated) DialogBox_Challenge_Validated ();
 
      if (is_dialogbox_changeappearance_validated) DialogBox_ChangeAppearance_Validated ();
 
      if (is_dialogbox_comment_validated) DialogBox_Comment_Validated ();
 
      if (is_dialogbox_endgame_validated) DialogBox_EndGame_Validated ();
 
      if (is_dialogbox_gotomove_validated) DialogBox_GoToMove_Validated ();
 
      if (is_dialogbox_load_validated) DialogBox_Load_Validated ();
 
      if (is_dialogbox_message_validated) DialogBox_Message_Validated ();
 
      if (is_dialogbox_newgame_validated) DialogBox_NewGame_Validated ();
 
      if (is_dialogbox_options_validated) DialogBox_Options_Validated ();
 
      if (is_dialogbox_pawnpromotion_validated) DialogBox_PawnPromotion_Validated ();
 
      if (is_dialogbox_playercard_validated) DialogBox_PlayerCard_Validated ();
 
      if (is_dialogbox_playerinfoname_validated) DialogBox_PlayerInfoName_Validated ();
 
      if (is_dialogbox_quit_validated) DialogBox_Quit_Validated ();
 
      if (is_dialogbox_resign_validated) DialogBox_Resign_Validated ();
 
      if (is_dialogbox_save_validated) DialogBox_Save_Validated ();
 
      if (is_dialogbox_saveposition_validated) DialogBox_SavePosition_Validated ();
 
      if (is_dialogbox_sendchallenge_validated) DialogBox_SendChallenge_Validated ();
 
      if (is_dialogbox_sendseek_validated) DialogBox_SendSeek_Validated ();
 
      if (is_dialogbox_takeback_validated) DialogBox_Takeback_Validated ();
 
      if (is_window_chat_validated) Window_Chat_Validated ();
 
      if (is_window_chatterchannels_validated) Window_ChatterChannels_Validated ();
 
      if (is_window_games_validated) Window_Games_Validated ();
 
      if (is_window_motd_validated) Window_MOTD_Validated ();
 
      if (is_window_opponents_validated) Window_Opponents_Validated ();
 
      if (is_window_sought_validated) Window_Sought_Validated ();
 
 
 
      MainLoop_EvaluateGameState (); // evaluate game state
 
 
 
      // is the table rotating ?
 
      if (Player_RotateTable (&the_board.players[current_viewer], current_time - previous_time))
 
         the_scene.update = true; // if so, update the scene
 
 
 
      // are we highlighting something ?
 
      if (highlight_endtime > current_time)
 
         the_scene.update = true; // if so, update the scene
 
 
 
      // else if we WERE just highlighting something, clear the hovered positions
 
      else if (highlight_endtime + 0.1f > current_time)
 
      {
 
         the_board.hovered_position[0] = -1;
 
         the_board.hovered_position[1] = -1;
 
         the_scene.update = true; // and update the scene
 
      }
 
 
 
      // if we want the game clock, update scene every second
 
      if (options.want_clock && ((int) current_time != (int) previous_time))
 
         the_scene.update = true;
 
 
 
      // if there's something in the central text buffer, update scene too
 
      if (the_scene.gui.central_text.is_displayed)
 
         the_scene.update = true;
 
 
 
      // if we have a spinning wheel, update scene too
 
      if (the_scene.gui.want_spinwheel)
 
         the_scene.update = true;
 
 
 
      // do we NOT need to update the scene ?
 
      if (!the_scene.update)
 
      {
 
         // cycle through all themes and see if one of them needs to be loaded
 
         for (array_index = 0; array_index < theme_count; array_index++)
 
            if (!themes[array_index].is_loaded)
 
            {
 
               Theme_Load (&themes[array_index], false); // load a bit more of this theme
 
               break; // and stop looping (see you next pass)
 
            }
 
      }
 
 
 
      // do we need to update the scene ?
 
      if (the_scene.update || want_framerate)
 
      {
 
         Scene_Update (&the_scene, &the_board); // evaluate which parts need to be placed
 
         Render_RenderFrame (&the_scene); // render a game frame
 
 
 
         the_scene.update = false; // scene no longer needs to be updated now
 
      }
 
 
 
      Audio_Think (); // ensure audio is playing
 
 
 
      previous_time = current_time; // save previous time
 
      if (frame_count == 100)
 
      {
 
         frame_count = 0; // reset frame count every 100 frames
 
         Sleep (1); // once every 100 frames, wait a millisecond
 
      }
 
      else
 
         Sleep (0); // else just allow context switching
 
      frame_count++; // increase the frame count
 
   }
 
 
 
   /////////////////////////////////////////////////////////////////////////
 
   // at this point, we exited the main loop and we are returning to Windows
 
 
 
   // release scene, game, themes and shutdown Direct3D
 
   Scene_Shutdown (&the_scene);
 
   Board_Shutdown (&the_board);
 
   Themes_Shutdown ();
 
   Render_Shutdown ();
 
 
 
   // delete the font resources
 
   DeleteObject (hFontChat);
 
 
 
   // delete the bitmap and icon ressources
 
   for (array_index = 1; array_index < sizeof (handlestatus) / sizeof (handlestatus[0]); array_index++)
 
   {
 
      DeleteObject (handlestatus[array_index].bitmap);
 
      DestroyIcon (handlestatus[array_index].icon);
 
   }
 
 
 
   // unregister window class
 
   UnregisterClass (wc.lpszClassName, hAppInstance);
 
 
 
   // destroy the accelerators table
 
   if (hAccelerators)
 
      DestroyAcceleratorTable (hAccelerators);
 
   hAccelerators = NULL;
 
 
 
   // destroy the game menu
 
   if (IsMenu (hMenu))
 
      DestroyMenu (hMenu);
 
   hMenu = NULL;
 
 
 
   // are we not registered yet ?
 
   if (!is_registered)
 
      DialogBox_Registration ();
 
 
 
   // save configuration data
 
   Config_Save ();
 
 
 
   // unload localized texts
 
   LocalizedTexts_Shutdown ();
 
 
 
   return (0); // and return to Windows.
 
}
 
 
 
 
 
static void MainLoop_FindCurrentViewer (void)
 
{
 
   // helper function that tells who is the current viewer
 
 
 
   if ((the_board.players[COLOR_WHITE].type == PLAYER_HUMAN) && (the_board.players[COLOR_BLACK].type == PLAYER_HUMAN))
 
      current_viewer = Board_ColorToMove (&the_board); // if both players are human, track them both
 
   else if (the_board.players[COLOR_WHITE].type == PLAYER_HUMAN)
 
      current_viewer = COLOR_WHITE; // else if only the white is human, track the white
 
   else if (the_board.players[COLOR_BLACK].type == PLAYER_HUMAN)
 
      current_viewer = COLOR_BLACK; // else if it's the black, track the black
 
   else
 
      current_viewer = COLOR_WHITE; // else no human in game, track just the white
 
 
 
   return; // finished, current viewer is known
 
}
 
 
 
 
 
static void MainLoop_EvaluateGameState (void)
 
{
 
   // function to evaluate the game state in the main loop when a part has just moved
 
 
 
   static wchar_t window_title[256];
 
 
 
   player_t *current_player;
 
   player_t *opposite_player;
 
   player_t *network_player;
 
   boardmove_t *last_move;
 
   int enabled_value;
 
   int move_index;
 
 
 
   if (!the_board.reevaluate)
 
      return; // if the board doesn't need to be reevaluated, don't do anything
 
 
 
   // get current and opposite players
 
   current_player = Player_GetCurrent ();
 
   opposite_player = Player_GetOpposite ();
 
 
 
   // see if we're online
 
   network_player = Player_FindByType (PLAYER_INTERNET);
 
 
 
   // has the game started ?
 
   if (the_board.move_count > 1)
 
   {
 
      // game has started, enable the "save" and "save as" menu options
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVE, MF_ENABLED);
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEAS, MF_ENABLED);
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_ENABLED);
 
 
 
      // enable the "go to move" menu option
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_GOTOMOVE, MF_ENABLED);
 
 
 
      // are we watching the last move ?
 
      if (the_board.viewed_move == the_board.move_count - 1)
 
      {
 
         // get a quick acccess to the last move
 
         last_move = &the_board.moves[the_board.move_count - 1];
 
 
 
         // if the current player is a human AND its opponent is a computer, allow him to ask us for a hint
 
         if ((current_player->type == PLAYER_HUMAN) && (opposite_player->type == PLAYER_COMPUTER))
 
            EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SUGGESTMOVE, MF_ENABLED);
 
         else
 
            EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SUGGESTMOVE, MF_GRAYED);
 
 
 
         // (if the current player is a human
 
         //  AND (its opponent is another human AND there's at least one move played)
 
         //       OR (its opponent is a computer AND there are at least two moves played)
 
         //       OR (its opponent is a remote player AND we're in game AND there are at least two moves played))
 
         // OR the current player is a remote player AND we're in game AND there are at least two moves played), allow him to cancel move
 
         if (((current_player->type == PLAYER_HUMAN)
 
              && ((opposite_player->type == PLAYER_HUMAN)
 
                  || ((opposite_player->type == PLAYER_COMPUTER) && (the_board.move_count > 2))
 
                  || ((opposite_player->type == PLAYER_INTERNET) && (opposite_player->is_in_game) && (the_board.move_count > 2))))
 
             || ((current_player->type == PLAYER_INTERNET) && (current_player->is_in_game) && (the_board.move_count > 2)))
 
            EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_CANCELLASTMOVE, MF_ENABLED);
 
         else
 
            EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_CANCELLASTMOVE, MF_GRAYED);
 
 
 
         // is the current player in check ? (to play the right sound)
 
         // read as: was the last move an opponent's move AND did it put us to check ?
 
         if (last_move->is_check)
 
         {
 
            // is it a checkmate ? (checkmate == check + stalemate)
 
            if (last_move->is_stalemate)
 
            {
 
               // display the game over dialog box
 
               the_board.game_state = (Board_ColorToMove (&the_board) == COLOR_WHITE ? STATE_BLACKWIN_CHECKMATE : STATE_WHITEWIN_CHECKMATE);
 
               DialogBox_EndGame ();
 
            }
 
         }
 
         else
 
         {
 
            // is it a stalemate ?
 
            if (last_move->is_stalemate)
 
            {
 
               // display the game over dialog box
 
               the_board.game_state = STATE_DRAW_STALEMATE;
 
               DialogBox_EndGame ();
 
            }
 
         }
 
 
 
         // have there 50 moves been played each side (i.e, 100 plies) AND is the current player human ?
 
         if ((the_board.move_count > 100) && (current_player->type == PLAYER_HUMAN))
 
         {
 
            // go backwards and see when is the latest move that took an opponent's piece OR the latest pawn move
 
            for (move_index = the_board.move_count - 1; move_index >= 0; move_index--)
 
               if (the_board.moves[move_index].has_captured || (the_board.moves[move_index].part == PART_PAWN))
 
                  break; // stop as soon as we find one
 
 
 
            // can the fifty moves draw rule be claimed AND does the current player claims it ?
 
            if (move_index + 1 + 100 < the_board.move_count)
 
            {
 
               // yes. Propose it to the side that's on move
 
// TODO: non-modal MessageBox (copy dialog_newgame.cpp and use return values)
 
            }
 
         }
 
 
 
         if (the_scene.gui.larrow.state == 0)
 
            the_scene.gui.larrow.state = 1; // enable "back" arrow if it isn't displayed yet
 
         if (the_scene.gui.rarrow.state != 0)
 
            the_scene.gui.rarrow.state = 0; // disable "forward" arrow if it's already displayed
 
 
 
         if (the_board.game_state == STATE_PLAYING)
 
            Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
 
                           RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"Current"));
 
         else if ((the_board.game_state == STATE_BLACKWIN_CHECKMATE) || (the_board.game_state == STATE_WHITEWIN_CHECKMATE))
 
            Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
 
                           RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_CheckMate"));
 
         else if ((the_board.game_state == STATE_WHITEWIN_RESIGNORFORFEIT) || (the_board.game_state == STATE_BLACKWIN_RESIGNORFORFEIT))
 
            Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
 
                           RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_Resign"));
 
         else if (the_board.game_state == STATE_DRAW_STALEMATE)
 
            Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
 
                           RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_StaleMate"));
 
         else if (the_board.game_state == STATE_DRAW_AGREEMENT)
 
            Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
 
                           RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_Agreement"));
 
         else if (the_board.game_state == STATE_DRAW_OTHER)
 
            Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
 
                           RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_DrawOther"));
 
 
 
         // enable the "comment on this move" menu option
 
         EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_COMMENTMOVE, MF_ENABLED);
 
      }
 
 
 
      // else are we watching another move, but not the beginning of the game ?
 
      else if (the_board.viewed_move > 0)
 
      {
 
         if (the_scene.gui.larrow.state == 0)
 
            the_scene.gui.larrow.state = 1; // enable "back" arrow if it isn't displayed yet
 
         if (the_scene.gui.rarrow.state == 0)
 
            the_scene.gui.rarrow.state = 1; // enable "forward" arrow if it isn't displayed yet
 
         Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
 
                        RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, 
 
                        L"%s %d\n%s", LOCALIZE (L"Move"), (the_board.viewed_move + 1) / 2, (the_board.viewed_move % 2 ? LOCALIZE (L"Games_White"): LOCALIZE (L"Games_Black")));
 
 
 
         // enable the "save position as" and "comment on this move" menu options
 
         EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_ENABLED);
 
         EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_COMMENTMOVE, MF_ENABLED);
 
      }
 
 
 
      // else we must be watching the beginning of the game (no move yet)
 
      else
 
      {
 
         if (the_scene.gui.larrow.state != 0)
 
            the_scene.gui.larrow.state = 0; // disable "back" arrow if it's already displayed
 
         if (the_scene.gui.rarrow.state == 0)
 
            the_scene.gui.rarrow.state = 1; // enable "forward" arrow if it isn't displayed yet
 
         Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
 
                        RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"Beginning"));
 
 
 
         // disable the "save position as" and "comment on this move" menu options
 
         EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_GRAYED);
 
         EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_COMMENTMOVE, MF_GRAYED);
 
      }
 
   }
 
   else
 
   {
 
      // game has not started, disable the "save", "save as" and "save position as" menu options
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVE, MF_GRAYED);
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEAS, MF_GRAYED);
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_GRAYED);
 
 
 
      // if the current player is a human AND its opponent is a computer, allow him to ask us for a hint
 
      if ((current_player->type == PLAYER_HUMAN) && (opposite_player->type == PLAYER_COMPUTER))
 
         EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SUGGESTMOVE, MF_ENABLED);
 
      else
 
         EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SUGGESTMOVE, MF_GRAYED);
 
 
 
      // disable the "cancel last move", "comment move" and "go to move" menu options
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_CANCELLASTMOVE, MF_GRAYED);
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_COMMENTMOVE, MF_GRAYED);
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_GOTOMOVE, MF_GRAYED);
 
 
 
      // and disable the two arrows and the arrow text
 
      the_scene.gui.larrow.state = 0;
 
      the_scene.gui.rarrow.state = 0;
 
      the_scene.gui.arrow_text.is_displayed = false;
 
   }
 
 
 
   // no matter whether the game started or not, if the current player is a human AND its opponent is a computer, allow him to swap sides
 
   if ((current_player->type == PLAYER_HUMAN) && (opposite_player->type == PLAYER_COMPUTER))
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SWAPSIDES, MF_ENABLED);
 
   else
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SWAPSIDES, MF_GRAYED);
 
 
 
   // update window title
 
   if ((the_board.players[COLOR_WHITE].name[0] != 0) && (the_board.players[COLOR_BLACK].name[0] != 0))
 
   {
 
      swprintf_s (window_title, WCHAR_SIZEOF (window_title), L"%s %s %s - " PROGRAM_NAME L"%s", the_board.players[COLOR_WHITE].name, LOCALIZE (L"Versus"), the_board.players[COLOR_BLACK].name, (is_registered ? L"" : LOCALIZE (L"EvaluationMode")));
 
      SetWindowText (hMainWnd, window_title); // update window title
 
   }
 
   else if (the_board.players[COLOR_WHITE].name[0] != 0)
 
   {
 
      swprintf_s (window_title, WCHAR_SIZEOF (window_title), L"%s - " PROGRAM_NAME L"%s", the_board.players[COLOR_WHITE].name, (is_registered ? L"" : LOCALIZE (L"EvaluationMode")));
 
      SetWindowText (hMainWnd, window_title); // update window title
 
   }
 
 
 
   // are we in internet mode AND are we logged in ?
 
   if ((network_player != NULL) && network_player->is_logged_in)
 
   {
 
      // are we currently playing a game ?
 
      if (network_player->is_in_game)
 
      {
 
         GUIBUTTON_ENABLE (the_scene.gui.chatbutton); // enable chat button if it's not enabled yet
 
         GUIBUTTON_DISABLE (the_scene.gui.gamesbutton); // disable games button if it was enabled
 
         GUIBUTTON_DISABLE (the_scene.gui.peoplebutton); // disable people button if it was enabled
 
         EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_RESIGN, MF_ENABLED); // allow resigning
 
      }
 
      else
 
      {
 
         GUIBUTTON_DISABLE (the_scene.gui.chatbutton); // disable chat button if it was enabled
 
         GUIBUTTON_ENABLE (the_scene.gui.gamesbutton); // enable games button if it's not enabled yet
 
         GUIBUTTON_ENABLE (the_scene.gui.peoplebutton); // enable people button if it's not enabled yet
 
         EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_RESIGN, MF_GRAYED); // disable ability to resign
 
      }
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_STATISTICS, MF_ENABLED); // enable stats
 
 
 
      enabled_value = MF_ENABLED; // remember to enable the internet-related menu items
 
   }
 
   // else it's a local or vs. computer game
 
   else
 
   {
 
      GUIBUTTON_DISABLE (the_scene.gui.chatbutton); // disable chat button if it was enabled
 
      GUIBUTTON_DISABLE (the_scene.gui.gamesbutton); // disable games button if it was enabled
 
      GUIBUTTON_DISABLE (the_scene.gui.peoplebutton); // disable people button if it was enabled
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_RESIGN, MF_ENABLED); // allow resigning
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_STATISTICS, MF_GRAYED); // disable stats
 
 
 
      enabled_value = MF_GRAYED; // remember to disable the internet-related menu items
 
   }
 
 
 
   EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SETUPPOSITION, !enabled_value); // note the inversion
 
   EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_SHOWONLINEPLAYERS, enabled_value);
 
   EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_SHOWSOUGHTGAMES, enabled_value);
 
   EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_SEEKGAME, enabled_value);
 
   if (!options.network.want_publicchat)
 
   {
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_CHATTERCHANNELS, MF_GRAYED); // always grayed if we don't want public chat
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_ENTERCHATTEXT, MF_GRAYED);
 
   }
 
   else
 
   {
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_CHATTERCHANNELS, enabled_value); // else enabled only in internet mode
 
      EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_ENTERCHATTEXT, enabled_value);
 
   }
 
   EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_DISPLAYPLAYERCARD, enabled_value);
 
   EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_DISPLAYYOURCARD, enabled_value);
 
   EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_MOTD, enabled_value);
 
 
 
   the_board.reevaluate = false; // board evaluation has been done
 
   return; // finished, new board state is known
 
}