Subversion Repositories Games.Chess Giants

Rev

Rev 192 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. // main.cpp
  2.  
  3. #define DEFINE_GLOBALS
  4. #include "common.h"
  5.  
  6.  
  7. // prototypes of locally used functions
  8. static void MainLoop_FindCurrentViewer (void);
  9. static void MainLoop_EvaluateGameState (void);
  10.  
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, char *lpCmdLine, int nCmdShow)
  13. {
  14.    // the entry point for any Windows program
  15.  
  16.    WNDCLASSEX wc;
  17.    MENUINFO menu_info;
  18.    MSG msg;
  19.    HBITMAP hSplashBmp;
  20.    BITMAP splash_bmp;
  21.    HDC hdc;
  22.    RECT rect;
  23.    PAINTSTRUCT ps;
  24.    HBITMAP hbmTmp;
  25.    HDC hdcMem;
  26.    HKEY hRegistryKey;
  27.    INPUT mousemove_input;
  28.    float previous_time;
  29.    float screensaverwatchdog_feedtime;
  30.    int frame_count;
  31.    int array_index;
  32.    char *endptr;
  33.    wchar_t app_pathname[MAX_PATH];
  34.    wchar_t font_pathname[MAX_PATH];
  35.    wchar_t temp_string[MAX_PATH];
  36.    DWORD ascii_buffersize;
  37.    WSADATA wsa_data;
  38.  
  39.    // does an InstallerPath registry key exist? if so, it means we've just been installed
  40.    ascii_buffersize = sizeof (temp_string);
  41.    if ((RegOpenKeyEx (HKEY_CURRENT_USER, L"SOFTWARE\\Chess Giants", 0, KEY_READ | KEY_SET_VALUE, &hRegistryKey) == 0)
  42.        && (RegQueryValueEx (hRegistryKey, L"InstallerPath", 0, NULL, (BYTE *) temp_string, &ascii_buffersize) == 0))
  43.    {
  44.       temp_string[ascii_buffersize / sizeof (wchar_t)] = 0; // terminate the string ourselves (strings in the registry MAY have no null terminator)
  45.  
  46.       // delete the installer
  47.       DeleteFile (temp_string);
  48.  
  49.       // delete the registry value and close the registry key
  50.       RegDeleteValue (hRegistryKey, L"InstallerPath");
  51.       RegCloseKey (hRegistryKey);
  52.    }
  53.  
  54.    // save application instance
  55.    hAppInstance = hInstance;
  56.  
  57. #ifdef NO_REGISTRATION
  58. #else // !NO_REGISTRATION
  59.    // does another Chess Giants instance exist ? If so, bring it to the front (note: allow this in debug mode)
  60.    {
  61.       HWND hOtherWnd = FindWindow (PROGRAM_NAME L" WndClass", NULL);
  62.       if (IsWindow (hOtherWnd))
  63.       {
  64.          ShowWindow (hOtherWnd, SW_SHOW);
  65.          BringWindowToTop (hOtherWnd);
  66.          SetForegroundWindow (hOtherWnd);
  67.          SetFocus (hOtherWnd);
  68.          return (-1);
  69.       }
  70.    }
  71. #endif // NO_REGISTRATION
  72.  
  73.    // find module and path names, and *IMPORTANT*, set the current working directory there
  74.    GetModuleFileName (NULL, app_pathname, WCHAR_SIZEOF (app_pathname));
  75.    GetDirectoryPath (app_pathname, app_path);
  76.    SetCurrentDirectoryW (app_path);
  77.  
  78.    // see which Windows kernel we have by looking at kernel32.dll's file version. No error checking as this cannot fail.
  79.    {
  80.       uint8_t raw_version_data[256 + sizeof (VS_FIXEDFILEINFO)];
  81.       VS_FIXEDFILEINFO *version_data;
  82.       UINT version_data_size;
  83.       GetFileVersionInfo (L"kernel32.dll", NULL, sizeof (raw_version_data), raw_version_data);
  84.       VerQueryValue (raw_version_data, L"\\", (void **) &version_data, &version_data_size);
  85.       kernel32_version = (((uint64_t) ((version_data->dwFileVersionMS >> 16) & 0xffff)) << 48)
  86.                        | (((uint64_t) ((version_data->dwFileVersionMS >>  0) & 0xffff)) << 32)
  87.                        | (((uint64_t) ((version_data->dwFileVersionLS >> 16) & 0xffff)) << 16)
  88.                        | (((uint64_t) ((version_data->dwFileVersionLS >>  0) & 0xffff)) <<  0);
  89.    }
  90.  
  91.    // see if we have VERY little memory, in which case complain
  92.    // TODO
  93.  
  94.    // initialize stuff
  95.    terminate_everything = false;
  96.    hMainWnd = NULL;
  97.    hChatterChannelsWnd = NULL;
  98.    hGamesWnd = NULL;
  99.    hMOTDWnd = NULL;
  100.    hOpponentsWnd = NULL;
  101.    hSoughtWnd = NULL;
  102.    is_paused = false; // clear pause status
  103. #ifdef NO_REGISTRATION
  104.    want_framerate = IsDebuggerPresent (); // display framerate in debug mode
  105. #else // !NO_REGISTRATION
  106.    is_registered = false;
  107.    dont_nag = false;
  108.    want_framerate = false; // release mode, don't display framerate
  109. #endif // NO_REGISTRATION
  110.    is_dialogbox_displayed = false;
  111.    is_dialogbox_about_validated = false;
  112.    is_dialogbox_challenge_validated = false;
  113.    is_dialogbox_changeappearance_validated = false;
  114.    is_dialogbox_comment_validated = false;
  115.    is_dialogbox_endgame_validated = false;
  116.    is_dialogbox_gotomove_validated = false;
  117.    is_dialogbox_renamesides_validated = false;
  118.    is_dialogbox_load_validated = false;
  119.    is_dialogbox_message_validated = false;
  120.    is_dialogbox_newgame_validated = false;
  121.    is_dialogbox_options_validated = false;
  122.    is_dialogbox_pawnpromotion_validated = false;
  123.    is_dialogbox_playercard_validated = false;
  124.    is_dialogbox_playerinfoname_validated = false;
  125.    is_dialogbox_quit_validated = false;
  126.    is_dialogbox_resign_validated = false;
  127.    is_dialogbox_save_validated = false;
  128.    is_dialogbox_saveposition_validated = false;
  129.    is_dialogbox_sendchallenge_validated = false;
  130.    is_dialogbox_sendseek_validated = false;
  131.    is_dialogbox_takeback_validated = false;
  132.    is_window_chat_validated = false;
  133.    is_window_chatterchannels_validated = false;
  134.    is_window_games_validated = false;
  135.    is_window_motd_validated = false;
  136.    is_window_opponents_validated = false;
  137.    is_window_sought_validated = false;
  138.    save_pathname[0] = 0;
  139.    srand ((unsigned int) time (NULL)); // initialize PRNG
  140.    animation_endtime = 0.0f;
  141.    command_ignoretime = 0.0f;
  142.    highlight_endtime = 0.0f;
  143.    previous_time = 0.0f;
  144.    frame_count = 0;
  145.  
  146.    // load the texts and ensure we have at least one display language
  147.    LocalizedTexts_Init ();
  148.    if (language_count < 1)
  149.    {
  150.       MessageBox (NULL, L"Chess Giants was unable to load its data files.\nThe game cannot start.\n\nPlease reinstall this program to fix the problem.", L"Chess Giants", MB_ICONERROR | MB_OK);
  151.       return (-1); // bomb out on error
  152.    }
  153.  
  154.    // initialize WinSock. God knows why it's necessary.
  155.    if (WSAStartup (MAKEWORD (2, 2), &wsa_data) != 0)
  156.    {
  157.       MessageBox (NULL, LOCALIZE (L"Error_NetworkInitializationFailed"), LOCALIZE (L"ImportantMessage"), MB_ICONERROR | MB_OK);
  158.       return (-1); // bomb out if network couldn't be initialized
  159.    }
  160.  
  161.    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  162.    // TODO: measure amount of RAM. If < 1Gb total or < 500 Gb free, warn that some chess engines might refuse to run. //
  163.    // FIXME: best thing would be to auto-adjust engine RAM usage according to what's available ?                      //
  164.    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  165.  
  166.    // read configuration data
  167.    Config_Load ();
  168.  
  169.    // see if the program is registered
  170. #ifndef NO_REGISTRATION
  171.    is_registered = IsRegistrationCorrect (options.registration.user_email, options.registration.activation_code);
  172.  
  173.    // is it not ? if so, try to read alternate registration data from the registry
  174.    if (!is_registered && (RegOpenKeyEx (HKEY_CURRENT_USER, L"SOFTWARE\\Chess Giants", 0, KEY_READ, &hRegistryKey) == 0))
  175.    {
  176.       ascii_buffersize = sizeof (options.registration.user_email); // in bytes
  177.       if (RegQueryValueEx (hRegistryKey, L"UserEmail", 0, NULL, (BYTE *) options.registration.user_email, &ascii_buffersize) == 0)
  178.          options.registration.user_email[ascii_buffersize / sizeof (wchar_t)] = 0; // terminate the string ourselves (strings in the registry MAY have no null terminator)
  179.       ascii_buffersize = sizeof (options.registration.activation_code); // in bytes
  180.       if (RegQueryValueEx (hRegistryKey, L"ActivationCode", 0, NULL, (BYTE *) &options.registration.activation_code, &ascii_buffersize) != 0)
  181.          options.registration.activation_code = 0; // if we can't read the activation code DWORD properly, reset it
  182.       RegCloseKey (hRegistryKey); // once we've read the data we were interested in, close the registry key
  183.  
  184.       // now check again if we're registered
  185.       is_registered = IsRegistrationCorrect (options.registration.user_email, options.registration.activation_code);
  186.    }
  187. #endif // !NO_REGISTRATION
  188.  
  189.    // if we're not registered, display a dialog box to invite the user to enter his donor email to unlock the program
  190.    //if (!is_registered)
  191.    //{
  192.    //   DialogBox_Registration ();
  193.    //}
  194.    //DialogBox_Registration ();
  195.    //return (0);
  196.  
  197.    // register the window class, create the window and show it
  198.    memset (&wc, 0, sizeof (wc));
  199.    wc.cbSize = sizeof (wc);
  200.    wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; // double-clicks support
  201.    wc.lpfnWndProc = WindowProc_Main;
  202.    wc.hInstance = hAppInstance;
  203.    wc.hIcon = LoadIcon (hAppInstance, (wchar_t *) ICON_MAIN);
  204.    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  205.    wc.lpszClassName = PROGRAM_NAME L" WndClass";
  206.    RegisterClassEx (&wc);
  207.    swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), PROGRAM_NAME L"%s",
  208. #ifndef NO_REGISTRATION
  209.                (is_registered ?
  210. #endif // !NO_REGISTRATION
  211.                L""
  212. #ifndef NO_REGISTRATION
  213.                : LOCALIZE (L"EvaluationMode"))
  214. #endif // !NO_REGISTRATION
  215.    ); // build window title
  216.    if (options.want_fullscreen)
  217.       hMainWnd = CreateWindowEx (0, wc.lpszClassName, temp_string, WS_POPUPWINDOW, // temp_string holds window title
  218.                                  0, 0, GetSystemMetrics (SM_CXSCREEN), GetSystemMetrics (SM_CYSCREEN), NULL, NULL, hAppInstance, NULL);
  219.    else
  220.    {
  221.       // in windowed mode, ensure window width and height aren't greater than screen size nor lower than a safe minimum
  222.       if (options.window_width > GetSystemMetrics (SM_CXMAXTRACK))
  223.          options.window_width = GetSystemMetrics (SM_CXMAXTRACK); // check this first in case screen size is reported incorrect
  224.       if (options.window_height > GetSystemMetrics (SM_CYMAXTRACK))
  225.          options.window_height = GetSystemMetrics (SM_CYMAXTRACK);
  226.       if (options.window_width < 640)
  227.          options.window_width = 640; // check this secondly in case screen size is reported incorrect
  228.       if (options.window_height < 480)
  229.          options.window_height = 480;
  230.  
  231.       hMainWnd = CreateWindowEx (0, wc.lpszClassName, temp_string, WS_OVERLAPPEDWINDOW, // temp_string holds window title
  232.                                  GetSystemMetrics (SM_CXSCREEN) / 2 - options.window_width / 2,
  233.                                  GetSystemMetrics (SM_CYSCREEN) / 2 - options.window_height / 2,
  234.                                  options.window_width, options.window_height, NULL, NULL, hAppInstance, NULL);
  235.    }
  236.    ShowWindow (hMainWnd, (options.want_maximized ? SW_SHOWMAXIMIZED : nCmdShow)); // show it maximized if it was closed so
  237.  
  238.    // create the main menu line, and its accelerators
  239.    hMainMenu = NULL;
  240.    hMainAccelerators = NULL;
  241.    CreateOrUpdateApplicationMenu ();
  242.  
  243.    // display the splash screen (uglily first, using GDI functions)
  244.    memset ((void *) &splash_bmp, 0, sizeof (splash_bmp));
  245.    hSplashBmp = W32LoadImage (L"%s/data/splash.bmp", app_path); // load the splash bitmap
  246.    GetObject (hSplashBmp, sizeof (splash_bmp), (void *) &splash_bmp); // get a structure containing its size (among others)
  247.    hdcMem = CreateCompatibleDC (NULL); // create a device context compatible with the screen
  248.    hbmTmp = SelectBitmap (hdcMem, hSplashBmp); // select our bitmap to use in it
  249.    hdc = BeginPaint (hMainWnd, &ps); // begin painting on the main window
  250.    GetClientRect (hMainWnd, &rect);
  251.    StretchBlt (hdc, 0, 0, rect.right, rect.bottom, hdcMem, 0, 0, splash_bmp.bmWidth, splash_bmp.bmHeight, SRCCOPY); // bit blit the bitmap into it
  252.    EndPaint (hMainWnd, &ps); // end painting on the main window
  253.    SelectObject (hdcMem, hbmTmp); // restore the previous selection
  254.    DeleteDC (hdcMem); // and delete the handles to the device context
  255.    DeleteObject (hSplashBmp); // and to the bitmap that we used
  256.  
  257.    // make the menu modeless
  258.    memset (&menu_info, 0, sizeof (menu_info)); // prepare menu info structure
  259.    menu_info.cbSize = sizeof (MENUINFO);
  260.    menu_info.fMask = MIM_STYLE;
  261.    GetMenuInfo (GetMenu (hMainWnd), &menu_info); // get current style
  262.    menu_info.dwStyle |= MNS_MODELESS; // add the "modeless" flag
  263.    SetMenuInfo (GetMenu (hMainWnd), &menu_info); // and send it back
  264.  
  265.    // load status icons, bitmaps and texts
  266.    handlestatus[HANDLESTATUS_AVAILABLE].icon = W32LoadIcon (L"%s/data/icons/available.ico", app_path);
  267.    handlestatus[HANDLESTATUS_AVAILABLE].bitmap = W32LoadImage (L"%s/data/status/available.bmp", app_path);
  268.    handlestatus[HANDLESTATUS_AVAILABLE].text = LOCALIZE (L"Opponents_StatusAvailable");
  269.    handlestatus[HANDLESTATUS_INGAME].icon = W32LoadIcon (L"%s/data/icons/ingame.ico", app_path);
  270.    handlestatus[HANDLESTATUS_INGAME].bitmap = W32LoadImage (L"%s/data/status/ingame.bmp", app_path);
  271.    handlestatus[HANDLESTATUS_INGAME].text = LOCALIZE (L"Opponents_StatusInGame");
  272.    handlestatus[HANDLESTATUS_INSIMULATION].icon = W32LoadIcon (L"%s/data/icons/insimulation.ico", app_path);
  273.    handlestatus[HANDLESTATUS_INSIMULATION].bitmap = W32LoadImage (L"%s/data/status/insimulation.bmp", app_path);
  274.    handlestatus[HANDLESTATUS_INSIMULATION].text = LOCALIZE (L"Opponents_StatusInSimulation");
  275.    handlestatus[HANDLESTATUS_INTOURNAMENT].icon = W32LoadIcon (L"%s/data/icons/intournament.ico", app_path);
  276.    handlestatus[HANDLESTATUS_INTOURNAMENT].bitmap = W32LoadImage (L"%s/data/status/intournament.bmp", app_path);
  277.    handlestatus[HANDLESTATUS_INTOURNAMENT].text = LOCALIZE (L"Opponents_StatusInTournament");
  278.    handlestatus[HANDLESTATUS_EXAMININGAGAME].icon = W32LoadIcon (L"%s/data/icons/examiningagame.ico", app_path);
  279.    handlestatus[HANDLESTATUS_EXAMININGAGAME].bitmap = W32LoadImage (L"%s/data/status/examiningagame.bmp", app_path);
  280.    handlestatus[HANDLESTATUS_EXAMININGAGAME].text = LOCALIZE (L"Opponents_StatusExaminingAGame");
  281.    handlestatus[HANDLESTATUS_NOTOPENFORAMATCH].icon = W32LoadIcon (L"%s/data/icons/notopenforamatch.ico", app_path);
  282.    handlestatus[HANDLESTATUS_NOTOPENFORAMATCH].bitmap = W32LoadImage (L"%s/data/status/notopenforamatch.bmp", app_path);
  283.    handlestatus[HANDLESTATUS_NOTOPENFORAMATCH].text = LOCALIZE (L"Opponents_StatusNotOpenForAMatch");
  284.    handlestatus[HANDLESTATUS_INACTIVEORBUSY].icon = W32LoadIcon (L"%s/data/icons/inactiveorbusy.ico", app_path);
  285.    handlestatus[HANDLESTATUS_INACTIVEORBUSY].bitmap = W32LoadImage (L"%s/data/status/inactiveorbusy.bmp", app_path);
  286.    handlestatus[HANDLESTATUS_INACTIVEORBUSY].text = LOCALIZE (L"Opponents_StatusInactiveOrBusy");
  287.    handlestatus[HANDLESTATUS_OFFLINE].icon = W32LoadIcon (L"%s/data/icons/offline.ico", app_path);
  288.    handlestatus[HANDLESTATUS_OFFLINE].bitmap = W32LoadImage (L"%s/data/status/offline.bmp", app_path);
  289.    handlestatus[HANDLESTATUS_OFFLINE].text = LOCALIZE (L"Opponents_StatusOffline");
  290.  
  291.    // load the system fonts
  292.    hFontChat = CreateFont (17, 0, 0, 0, FW_DONTCARE, false, false, false, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
  293.                            CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Comic Sans MS");
  294.  
  295.    // before any rendering is done, it's a good idea to know what time it is
  296.    current_time = ProcessTime ();
  297.    stoppage_time = 0;
  298.    screensaverwatchdog_feedtime = current_time + 50.0f; // feed screensaver watchdog every 50 seconds
  299.  
  300.    // initialize audio and renderer, and display a cleaner version of the splash screen this time :)
  301.    if (!Audio_Init () || !Render_Init (L"%s/data/splash.bmp", app_path))
  302.       return (-1); // bomb out on error
  303.  
  304.    // load sprites
  305.    llarrow_spriteindex = Render_LoadSprite (L"%s/data/sprites/arrow-leftmost.png", app_path);
  306.    larrow_spriteindex = Render_LoadSprite (L"%s/data/sprites/arrow-left.png", app_path);
  307.    rarrow_spriteindex = Render_LoadSprite (L"%s/data/sprites/arrow-right.png", app_path);
  308.    rrarrow_spriteindex = Render_LoadSprite (L"%s/data/sprites/arrow-rightmost.png", app_path);
  309.    newgamebutton_spriteindex = Render_LoadSprite (L"%s/data/sprites/newgame.png", app_path);
  310.    opengamebutton_spriteindex = Render_LoadSprite (L"%s/data/sprites/opengame.png", app_path);
  311.    chatbutton_spriteindex = Render_LoadSprite (L"%s/data/sprites/chat.png", app_path);
  312.    gamesbutton_spriteindex = Render_LoadSprite (L"%s/data/sprites/games.png", app_path);
  313.    peoplebutton_spriteindex = Render_LoadSprite (L"%s/data/sprites/people.png", app_path);
  314.    sepia_spriteindex = Render_LoadSprite (L"%s/data/sprites/sepia.png", app_path);
  315.    for (array_index = 0; array_index < 12; array_index++)
  316.       spinner_spriteindex[array_index] = Render_LoadSprite (L"%s/data/sprites/spinner-%d.png", app_path, array_index * 30); // spinning wheel
  317.  
  318.    // add our custom fonts to the list of available fonts for the duration of the process
  319.    swprintf_s (font_pathname, WCHAR_SIZEOF (font_pathname), L"%s/data/fonts/papyrus.ttf", app_path);
  320.    AddFontResourceEx (font_pathname, FR_PRIVATE, 0);
  321.  
  322.    // load themes, initialize a new human vs. human game and setup the scene
  323.    if (!Themes_Init ())
  324.       return (-1); // bomb out on error
  325.    Board_Init (&the_board, PLAYER_HUMAN, PLAYER_HUMAN, L"standard", FENSTARTUP_STANDARDCHESS);
  326.    Scene_Init (&the_scene, &the_board);
  327.  
  328.    // has a filename been specifed on the command-line AND that file exists ?
  329.    if ((lpCmdLine != NULL) && (lpCmdLine[0] != 0))
  330.    {
  331.       while (*lpCmdLine == '"')
  332.          lpCmdLine++; // skip leading quotes
  333.       if ((endptr = strchr (lpCmdLine, '"')) != NULL)
  334.          *endptr = 0; // break the string at the first ending quote
  335.  
  336.       // is it a file that exists ? (don't use stat() which is broken on WinXP)
  337.       if (_access (lpCmdLine, 0) == 0)
  338.       {
  339.          ConvertToWideChar (load_pathname, WCHAR_SIZEOF (load_pathname), lpCmdLine); // save pathname string
  340.          is_dialogbox_load_validated = true; // and act as if the user just validated a load dialog box
  341.       }
  342. #ifndef NO_REGISTRATION
  343.       // else is it a registration info ? if so, parse it
  344.       else if ((strncmp (lpCmdLine, "/r=", 3) == 0) && ((endptr = strchr (&lpCmdLine[3], ',')) != NULL))
  345.       {
  346.          *endptr = 0; // break the string at the separator between user email and activation code
  347.          ConvertToWideChar (temp_string, WCHAR_SIZEOF (temp_string), &lpCmdLine[3]); // read user email
  348.          is_registered = IsRegistrationCorrect (temp_string, atoi (endptr + 1)); // and see whether we're registered or not
  349.       }
  350. #endif // !NO_REGISTRATION
  351.    }
  352.  
  353.    // TODO: this + offline statistics
  354.  
  355.    // enter the main loop
  356.    while (!terminate_everything)
  357.    {
  358.       // see what time it is
  359.       current_time = ProcessTime ();
  360.       if (is_paused)
  361.          stoppage_time += (current_time - previous_time); // if we're paused, increase stoppage time
  362.  
  363.       // is it time to feed the screensaver watchdog ? (to prevent it from starting)
  364.       if (screensaverwatchdog_feedtime < current_time)
  365.       {
  366.          mousemove_input.type = INPUT_MOUSE;
  367.          memset (&mousemove_input.mi, 0, sizeof (mousemove_input.mi)); // blank out struct = no move at all :)
  368.          mousemove_input.mi.dwFlags = MOUSEEVENTF_MOVE;
  369.          SendInput (1, &mousemove_input, sizeof (mousemove_input)); // send a fake mouse move input event
  370.  
  371.          screensaverwatchdog_feedtime = current_time + 50.0f; // feed screensaver watchdog again in 50 seconds
  372.       }
  373.  
  374. #ifndef NO_REGISTRATION
  375.       // are we in demo mode and is it time to quit ?
  376.       if (!is_registered && (current_time > DEMO_TIMEOUT))
  377.          DestroyWindow (hMainWnd); // if so, send a quit message in order to break the loop
  378. #endif // !NO_REGISTRATION
  379.  
  380.       // are we in the middle of an animation or just after it ?
  381.       if (current_time < animation_endtime + 0.5f)
  382.          the_scene.update = true; // always update during animations
  383.       else
  384.          the_scene.update |= Board_Think (&the_board); // make the board (i.e, both players) think
  385.  
  386.       MainLoop_FindCurrentViewer ();  // determine current viewer
  387.  
  388.       // see if we have a message waiting for any window in the current thread and dispatch it
  389.       while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  390.       {
  391.          // check for accelerator keystrokes from the main window
  392.          if ((msg.hwnd != hMainWnd) || !TranslateAccelerator (hMainWnd, hMainAccelerators, &msg))
  393.          {
  394.             TranslateMessage (&msg); // translate and dispatch all other messages
  395.             DispatchMessage (&msg);
  396.          }
  397.       }
  398.  
  399.       // handle dialog box and window return codes
  400.       if (is_dialogbox_about_validated) DialogBox_About_Validated ();
  401.       if (is_dialogbox_challenge_validated) DialogBox_Challenge_Validated ();
  402.       if (is_dialogbox_changeappearance_validated) DialogBox_ChangeAppearance_Validated ();
  403.       if (is_dialogbox_comment_validated) DialogBox_Comment_Validated ();
  404.       if (is_dialogbox_endgame_validated) DialogBox_EndGame_Validated ();
  405.       if (is_dialogbox_gotomove_validated) DialogBox_GoToMove_Validated ();
  406.       if (is_dialogbox_renamesides_validated) DialogBox_RenameSides_Validated ();
  407.       if (is_dialogbox_load_validated) DialogBox_Load_Validated ();
  408.       if (is_dialogbox_message_validated) DialogBox_Message_Validated ();
  409.       if (is_dialogbox_newgame_validated) DialogBox_NewGame_Validated ();
  410.       if (is_dialogbox_options_validated) DialogBox_Options_Validated ();
  411.       if (is_dialogbox_pawnpromotion_validated) DialogBox_PawnPromotion_Validated ();
  412.       if (is_dialogbox_playercard_validated) DialogBox_PlayerCard_Validated ();
  413.       if (is_dialogbox_playerinfoname_validated) DialogBox_PlayerInfoName_Validated ();
  414.       if (is_dialogbox_quit_validated) DialogBox_Quit_Validated ();
  415.       if (is_dialogbox_resign_validated) DialogBox_Resign_Validated ();
  416.       if (is_dialogbox_save_validated) DialogBox_Save_Validated ();
  417.       if (is_dialogbox_saveposition_validated) DialogBox_SavePosition_Validated ();
  418.       if (is_dialogbox_sendchallenge_validated) DialogBox_SendChallenge_Validated ();
  419.       if (is_dialogbox_sendseek_validated) DialogBox_SendSeek_Validated ();
  420.       if (is_dialogbox_takeback_validated) DialogBox_Takeback_Validated ();
  421.       if (is_window_chat_validated) Window_Chat_Validated ();
  422.       if (is_window_chatterchannels_validated) Window_ChatterChannels_Validated ();
  423.       if (is_window_games_validated) Window_Games_Validated ();
  424.       if (is_window_motd_validated) Window_MOTD_Validated ();
  425.       if (is_window_opponents_validated) Window_Opponents_Validated ();
  426.       if (is_window_sought_validated) Window_Sought_Validated ();
  427.  
  428.       MainLoop_EvaluateGameState (); // evaluate game state
  429.  
  430.       // is the table rotating ?
  431.       if (Player_RotateTable (&the_board.players[current_viewer], current_time - previous_time))
  432.          the_scene.update = true; // if so, update the scene
  433.  
  434.       // are we highlighting something ?
  435.       if (highlight_endtime > current_time)
  436.          the_scene.update = true; // if so, update the scene
  437.  
  438.       // else if we WERE just highlighting something, clear the hovered positions
  439.       else if (highlight_endtime + 0.1f > current_time)
  440.       {
  441.          the_board.hovered_position[0] = -1;
  442.          the_board.hovered_position[1] = -1;
  443.          the_scene.update = true; // and update the scene
  444.       }
  445.  
  446.       // if we want the game clock, update scene every second
  447.       if (options.want_clock && ((int) current_time != (int) previous_time))
  448.          the_scene.update = true;
  449.  
  450.       // if there's something in the central text buffer, update scene too
  451.       if (the_scene.gui.central_text.is_displayed)
  452.          the_scene.update = true;
  453.  
  454.       // if we have a spinning wheel, update scene too
  455.       if (the_scene.gui.want_spinwheel)
  456.          the_scene.update = true;
  457.  
  458.       // is our theme not loaded yet ?
  459.       if (!theme->is_loaded)
  460.          Theme_LoadABitMore (theme); // load a bit more of it
  461.  
  462.       // else do we NOT need to update the scene ?
  463.       else if (!the_scene.update)
  464.       {
  465.          // cycle through all themes and see if one of them needs to be loaded
  466.          for (array_index = 0; array_index < theme_count; array_index++)
  467.             if (!themes[array_index].is_loaded)
  468.             {
  469.                Theme_LoadABitMore (&themes[array_index]); // load a bit more of this theme
  470.                break; // and stop looping (see you next pass)
  471.             }
  472.       }
  473.  
  474.       // either we need to update the scene, or we want to do it continuously
  475.       else
  476.       {
  477.          Scene_Update (&the_scene, &the_board); // evaluate which parts need to be placed
  478.          Render_RenderFrame (&the_scene); // render a game frame
  479.  
  480.          the_scene.update = false; // scene no longer needs to be updated now
  481.       }
  482.  
  483.       Audio_Think (); // ensure audio is playing
  484.          frame_count = 0; // reset frame count every 100 frames
  485.          Sleep (1); // once every 100 frames, wait a millisecond
  486.  
  487.       previous_time = current_time; // save previous time
  488.       if (frame_count == 100)
  489.       {
  490.       }
  491.       else
  492.          Sleep (0); // else just allow context switching
  493.       frame_count++; // increase the frame count
  494.    }
  495.  
  496.    /////////////////////////////////////////////////////////////////////////
  497.    // at this point, we exited the main loop and we are returning to Windows
  498.  
  499.    // release scene, game, themes and shutdown renderer and audio
  500.    Scene_Shutdown (&the_scene);
  501.    Board_Shutdown (&the_board);
  502.    Themes_Shutdown ();
  503.    Render_Shutdown ();
  504.    Audio_Shutdown ();
  505.  
  506.    // delete the font resources
  507.    DeleteObject (hFontChat);
  508.  
  509.    // delete the bitmap and icon ressources
  510.    for (array_index = 1; array_index < sizeof (handlestatus) / sizeof (handlestatus[0]); array_index++)
  511.    {
  512.       DeleteObject (handlestatus[array_index].bitmap);
  513.       DestroyIcon (handlestatus[array_index].icon);
  514.    }
  515.  
  516.    // unregister window class
  517.    UnregisterClass (wc.lpszClassName, hAppInstance);
  518.  
  519.    // destroy the accelerators table
  520.    if (hMainAccelerators)
  521.       DestroyAcceleratorTable (hMainAccelerators);
  522.    hMainAccelerators = NULL;
  523.  
  524.    // destroy the application menu object
  525.    if (IsMenu (hMainMenu))
  526.       DestroyMenu (hMainMenu);
  527.    hMainMenu = NULL;
  528.  
  529.    // are we not registered yet AND have we NOT been told to don't nag?
  530. #ifndef NO_REGISTRATION
  531.    if (!is_registered && !dont_nag)
  532.       DialogBox_Registration ();
  533. #endif // !NO_REGISTRATION
  534.  
  535.    // save configuration data
  536.    Config_Save ();
  537.  
  538.    // unload localized texts
  539.    LocalizedTexts_Shutdown ();
  540.  
  541.    // shutdown WinSock
  542.    WSACleanup ();
  543.  
  544.    ExitProcess (0); // it looks like this is needed to terminate reluctant Chess Giants instances...
  545.    return (0); // and return to Windows.
  546. }
  547.  
  548.  
  549. static void MainLoop_FindCurrentViewer (void)
  550. {
  551.    // helper function that tells who is the current viewer
  552.  
  553.    if ((the_board.players[COLOR_WHITE].type == PLAYER_HUMAN) && (the_board.players[COLOR_BLACK].type == PLAYER_HUMAN))
  554.       current_viewer = Board_ColorToMove (&the_board); // if both players are human, track them both
  555.    else if (the_board.players[COLOR_WHITE].type == PLAYER_HUMAN)
  556.       current_viewer = COLOR_WHITE; // else if only the white is human, track the white
  557.    else if (the_board.players[COLOR_BLACK].type == PLAYER_HUMAN)
  558.       current_viewer = COLOR_BLACK; // else if it's the black, track the black
  559.    else
  560.       current_viewer = COLOR_WHITE; // else no human in game, track just the white
  561.  
  562.    return; // finished, current viewer is known
  563. }
  564.  
  565.  
  566. static void MainLoop_EvaluateGameState (void)
  567. {
  568.    // function to evaluate the game state in the main loop when a part has just moved
  569.  
  570.    static wchar_t window_title[256];
  571.  
  572.    player_t *current_player;
  573.    player_t *opposite_player;
  574.    player_t *network_player;
  575.    boardmove_t *last_move;
  576.    int enabled_value;
  577.    int move_index;
  578.  
  579.    // get current and opposite players, and see if we're online
  580.    current_player = Player_GetCurrent ();
  581.    opposite_player = Player_GetOpposite ();
  582.    network_player = Player_FindByType (PLAYER_INTERNET);
  583.  
  584.    // if no dialog box is in focus AND the view distance and pitch is lower than the minimum, enable the "new game" and "open game" buttons
  585.    if (!is_dialogbox_displayed && (network_player == NULL) && (current_distance == CLOSEUP_VIEW_DISTANCE) && (current_pitch == CLOSEUP_VIEW_PITCH))
  586.    {
  587.       GUIBUTTON_ENABLE (the_scene.gui.newgamebutton);
  588.       GUIBUTTON_ENABLE (the_scene.gui.opengamebutton);
  589.    }
  590.    else
  591.    {
  592.       GUIBUTTON_DISABLE (the_scene.gui.newgamebutton);
  593.       GUIBUTTON_DISABLE (the_scene.gui.opengamebutton);
  594.    }
  595.  
  596.    if (!the_board.reevaluate)
  597.       return; // if the board doesn't need to be reevaluated, don't do anything
  598.  
  599.    // has the game started ?
  600.    if (the_board.move_count > 1)
  601.    {
  602.       // game has started, enable the "save" and "save as" menu options
  603.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVE, MF_ENABLED);
  604.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEAS, MF_ENABLED);
  605.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_ENABLED);
  606.  
  607.       // disable the start position setup mode
  608.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SETUPPOSITION, MF_GRAYED);
  609.  
  610.       // enable the "go to move" menu option
  611.       EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_GOTOMOVE, MF_ENABLED);
  612.  
  613.       // are we watching the last move ?
  614.       if (the_board.viewed_move == the_board.move_count - 1)
  615.       {
  616.          // get a quick acccess to the last move
  617.          last_move = &the_board.moves[the_board.move_count - 1];
  618.  
  619.          // if the current player is a human AND its opponent is a computer, allow him to ask us for a hint
  620.          if ((current_player->type == PLAYER_HUMAN) && (opposite_player->type == PLAYER_COMPUTER))
  621.             EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_SUGGESTMOVE, MF_ENABLED);
  622.          else
  623.             EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_SUGGESTMOVE, MF_GRAYED);
  624.  
  625.          // (if the current player is a human
  626.          //  AND (its opponent is another human AND there's at least one move played)
  627.          //       OR (its opponent is a computer AND there are at least two moves played)
  628.          //       OR (its opponent is a remote player AND we're in game AND there are at least two moves played))
  629.          // 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
  630.          if (((current_player->type == PLAYER_HUMAN)
  631.               && ((opposite_player->type == PLAYER_HUMAN)
  632.                   || ((opposite_player->type == PLAYER_COMPUTER) && (the_board.move_count > 2))
  633.                   || ((opposite_player->type == PLAYER_INTERNET) && (opposite_player->is_in_game) && (the_board.move_count > 2))))
  634.              || ((current_player->type == PLAYER_INTERNET) && (current_player->is_in_game) && (the_board.move_count > 2)))
  635.             EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_CANCELLASTMOVE, MF_ENABLED);
  636.          else
  637.             EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_CANCELLASTMOVE, MF_GRAYED);
  638.  
  639.          // is the current player in check ? (to play the right sound)
  640.          // read as: was the last move an opponent's move AND did it put us to check ?
  641.          if (last_move->is_check)
  642.          {
  643.             // is it a checkmate ? (checkmate == check + stalemate)
  644.             if (last_move->is_stalemate)
  645.             {
  646.                // display the game over dialog box
  647.                the_board.game_state = (Board_ColorToMove (&the_board) == COLOR_WHITE ? STATE_BLACKWIN_CHECKMATE : STATE_WHITEWIN_CHECKMATE);
  648.                DialogBox_EndGame ();
  649.             }
  650.          }
  651.          else
  652.          {
  653.             // is it a stalemate ?
  654.             if (last_move->is_stalemate)
  655.             {
  656.                // display the game over dialog box
  657.                the_board.game_state = STATE_DRAW_STALEMATE;
  658.                DialogBox_EndGame ();
  659.             }
  660.          }
  661.  
  662.          // have there 50 moves been played each side (i.e, 100 plies) AND is the current player human ?
  663.          if ((the_board.move_count > 100) && (current_player->type == PLAYER_HUMAN))
  664.          {
  665.             // go backwards and see when is the latest move that took an opponent's piece OR the latest pawn move
  666.             for (move_index = the_board.move_count - 1; move_index >= 0; move_index--)
  667.                if (the_board.moves[move_index].has_captured || (the_board.moves[move_index].part == PART_PAWN))
  668.                   break; // stop as soon as we find one
  669.  
  670.             // can the fifty moves draw rule be claimed AND does the current player claims it ?
  671.             if (move_index + 1 + 100 < the_board.move_count)
  672.             {
  673.                // yes. Propose it to the side that's on move
  674. // TODO: non-modal MessageBox (copy dialog_newgame.cpp and use return values)
  675.             }
  676.          }
  677.  
  678.          GUIBUTTON_ENABLE (the_scene.gui.llarrow); // enable "jump to beginning" arrow if it isn't displayed yet
  679.          GUIBUTTON_ENABLE (the_scene.gui.larrow); // enable "back" arrow if it isn't displayed yet
  680.          GUIBUTTON_DISABLE (the_scene.gui.rarrow); // disable "forward" arrow if it's already displayed
  681.          GUIBUTTON_DISABLE (the_scene.gui.rrarrow); // disable "jump to end" arrow if it's already displayed
  682.  
  683.          if (the_board.game_state == STATE_PLAYING)
  684.             Scene_UpdateText (&the_scene.gui.arrow_text, RGBA_TO_RGBACOLOR (255, 255, 255, 191), DURATION_INFINITE, false, LOCALIZE (is_paused ? L"Paused" : L"Current"));
  685.          else if ((the_board.game_state == STATE_BLACKWIN_CHECKMATE) || (the_board.game_state == STATE_WHITEWIN_CHECKMATE))
  686.             Scene_UpdateText (&the_scene.gui.arrow_text, RGBA_TO_RGBACOLOR (255, 255, 255, 191), DURATION_INFINITE, false, L"%s\n%s", LOCALIZE (L"EndGame_CheckMate"), LOCALIZE (L"EndGame_Title"));
  687.          else if ((the_board.game_state == STATE_WHITEWIN_RESIGNORFORFEIT) || (the_board.game_state == STATE_BLACKWIN_RESIGNORFORFEIT))
  688.             Scene_UpdateText (&the_scene.gui.arrow_text, RGBA_TO_RGBACOLOR (255, 255, 255, 191), DURATION_INFINITE, false, L"%s\n%s", LOCALIZE (L"EndGame_Resign"), LOCALIZE (L"EndGame_Title"));
  689.          else if (the_board.game_state == STATE_DRAW_STALEMATE)
  690.             Scene_UpdateText (&the_scene.gui.arrow_text, RGBA_TO_RGBACOLOR (255, 255, 255, 191), DURATION_INFINITE, false, L"%s\n%s", LOCALIZE (L"EndGame_StaleMate"), LOCALIZE (L"EndGame_Title"));
  691.          else if (the_board.game_state == STATE_DRAW_AGREEMENT)
  692.             Scene_UpdateText (&the_scene.gui.arrow_text, RGBA_TO_RGBACOLOR (255, 255, 255, 191), DURATION_INFINITE, false, L"%s\n%s", LOCALIZE (L"EndGame_Agreement"), LOCALIZE (L"EndGame_Title"));
  693.          else if (the_board.game_state == STATE_DRAW_OTHER)
  694.             Scene_UpdateText (&the_scene.gui.arrow_text, RGBA_TO_RGBACOLOR (255, 255, 255, 191), DURATION_INFINITE, false, L"%s\n%s", LOCALIZE (L"EndGame_DrawOther"), LOCALIZE (L"EndGame_Title"));
  695.  
  696.          // enable the "comment on this move" menu option
  697.          EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_COMMENTMOVE, MF_ENABLED);
  698.       }
  699.  
  700.       // else are we watching another move, but not the beginning of the game ?
  701.       else if (the_board.viewed_move > 0)
  702.       {
  703.          GUIBUTTON_ENABLE (the_scene.gui.llarrow); // enable "jump to beginning" arrow if it isn't displayed yet
  704.          GUIBUTTON_ENABLE (the_scene.gui.larrow); // enable "back" arrow if it isn't displayed yet
  705.          GUIBUTTON_ENABLE (the_scene.gui.rarrow); // enable "forward" arrow if it isn't displayed yet
  706.          GUIBUTTON_ENABLE (the_scene.gui.rrarrow); // enable "jump to end" arrow if it isn't displayed yet
  707.          Scene_UpdateText (&the_scene.gui.arrow_text, RGBA_TO_RGBACOLOR (255, 255, 255, 191), DURATION_INFINITE, 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")));
  708.  
  709.          // hints are not usable when watching the game history
  710.          EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_SUGGESTMOVE, MF_GRAYED);
  711.  
  712.          // enable the "save position as" and "comment on this move" menu options
  713.          EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_ENABLED);
  714.          EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_COMMENTMOVE, MF_ENABLED);
  715.       }
  716.  
  717.       // else we must be watching the beginning of the game (no move yet)
  718.       else
  719.       {
  720.          GUIBUTTON_DISABLE (the_scene.gui.llarrow); // disable "jump to beginning" arrow if it's already displayed
  721.          GUIBUTTON_DISABLE (the_scene.gui.larrow); // disable "back" arrow if it's already displayed
  722.          GUIBUTTON_ENABLE (the_scene.gui.rarrow); // enable "forward" arrow if it isn't displayed yet
  723.          GUIBUTTON_ENABLE (the_scene.gui.rrarrow); // enable "jump to end" arrow if it isn't displayed yet
  724.          Scene_UpdateText (&the_scene.gui.arrow_text, RGBA_TO_RGBACOLOR (255, 255, 255, 191), DURATION_INFINITE, false, LOCALIZE (L"Beginning"));
  725.  
  726.          // hints are not usable when watching the game history
  727.          EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_SUGGESTMOVE, MF_GRAYED);
  728.  
  729.          // disable the "save position as" and "comment on this move" menu options
  730.          EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_GRAYED);
  731.          EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_COMMENTMOVE, MF_GRAYED);
  732.       }
  733.    }
  734.    else
  735.    {
  736.       // game has not started, disable the "save", "save as" and "save position as" menu options
  737.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVE, MF_GRAYED);
  738.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEAS, MF_GRAYED);
  739.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_GRAYED);
  740.  
  741.       // if we are NOT on Internet play AND no longer in closeup mode, enable the start position setup mode
  742.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SETUPPOSITION, ((network_player == NULL) && (current_distance != CLOSEUP_VIEW_DISTANCE) && (current_pitch != CLOSEUP_VIEW_PITCH) ? MF_ENABLED : MF_GRAYED));
  743.  
  744.       // if the current player is a human AND its opponent is a computer, allow him to ask us for a hint
  745.       if ((current_player->type == PLAYER_HUMAN) && (opposite_player->type == PLAYER_COMPUTER))
  746.          EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_SUGGESTMOVE, MF_ENABLED);
  747.       else
  748.          EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_SUGGESTMOVE, MF_GRAYED);
  749.  
  750.       // disable the "cancel last move", "comment move" and "go to move" menu options
  751.       EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_CANCELLASTMOVE, MF_GRAYED);
  752.       EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_COMMENTMOVE, MF_GRAYED);
  753.       EnableMenuItem (GetMenu (hMainWnd), MENUID_MOVE_GOTOMOVE, MF_GRAYED);
  754.  
  755.       is_paused = false; // clear pause status (we can only pause a game when it's been started)
  756.  
  757.       // and disable the navigation arrows and the arrow text
  758.       GUIBUTTON_DISABLE (the_scene.gui.llarrow);
  759.       GUIBUTTON_DISABLE (the_scene.gui.larrow);
  760.       GUIBUTTON_DISABLE (the_scene.gui.rarrow);
  761.       GUIBUTTON_DISABLE (the_scene.gui.rrarrow);
  762.       the_scene.gui.arrow_text.is_displayed = false;
  763.    }
  764.  
  765.    // 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
  766.    if ((current_player->type == PLAYER_HUMAN) && (opposite_player->type == PLAYER_COMPUTER))
  767.       EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SWAPSIDES, MF_ENABLED);
  768.    else
  769.       EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SWAPSIDES, MF_GRAYED);
  770.  
  771.    // no matter whether the game started or not, enable players renaming only if we are NOT in internet mode
  772.    EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_RENAMESIDES, (network_player == NULL ? MF_ENABLED : MF_GRAYED));
  773.  
  774.    // update window title
  775.    if ((the_board.players[COLOR_WHITE].name[0] != 0) && (the_board.players[COLOR_BLACK].name[0] != 0))
  776.    {
  777.       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,
  778. #ifndef NO_REGISTRATION
  779.                   (is_registered ?
  780. #endif // !NO_REGISTRATION
  781.                   L""
  782. #ifndef NO_REGISTRATION
  783.                   : LOCALIZE (L"EvaluationMode"))
  784. #endif // !NO_REGISTRATION
  785.       );
  786.       SetWindowText (hMainWnd, window_title); // update window title
  787.    }
  788.    else if (the_board.players[COLOR_WHITE].name[0] != 0)
  789.    {
  790.       swprintf_s (window_title, WCHAR_SIZEOF (window_title), L"%s - " PROGRAM_NAME L"%s", the_board.players[COLOR_WHITE].name,
  791. #ifndef NO_REGISTRATION
  792.                   (is_registered ?
  793. #endif // !NO_REGISTRATION
  794.                   L""
  795. #ifndef NO_REGISTRATION
  796.                   : LOCALIZE (L"EvaluationMode"))
  797. #endif // !NO_REGISTRATION
  798.       );
  799.       SetWindowText (hMainWnd, window_title); // update window title
  800.    }
  801.  
  802.    // are we in internet mode AND are we logged in ?
  803.    if ((network_player != NULL) && network_player->is_logged_in)
  804.    {
  805.       // are we currently playing a game ?
  806.       if (network_player->is_in_game)
  807.       {
  808.          GUIBUTTON_ENABLE (the_scene.gui.chatbutton); // enable chat button if it's not enabled yet
  809.          GUIBUTTON_DISABLE (the_scene.gui.gamesbutton); // disable games button if it was enabled
  810.          GUIBUTTON_DISABLE (the_scene.gui.peoplebutton); // disable people button if it was enabled
  811.          EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_RESIGN, ((the_board.game_state == STATE_PLAYING) && (the_board.move_count > 1) ? MF_ENABLED : MF_GRAYED)); // allow resigning if the game has started AND it's not terminated)
  812.       }
  813.       else
  814.       {
  815.          GUIBUTTON_DISABLE (the_scene.gui.chatbutton); // disable chat button if it was enabled
  816.          GUIBUTTON_ENABLE (the_scene.gui.gamesbutton); // enable games button if it's not enabled yet
  817.          GUIBUTTON_ENABLE (the_scene.gui.peoplebutton); // enable people button if it's not enabled yet
  818.          EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_RESIGN, MF_GRAYED); // disable ability to resign
  819.       }
  820.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_PAUSE, MF_DISABLED); // disallow pause
  821.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_STATISTICS, MF_ENABLED); // enable stats
  822.  
  823.       enabled_value = MF_ENABLED; // remember to enable the internet-related menu items
  824.    }
  825.    // else it's a local or vs. computer game
  826.    else
  827.    {
  828.       GUIBUTTON_DISABLE (the_scene.gui.chatbutton); // disable chat button if it was enabled
  829.       GUIBUTTON_DISABLE (the_scene.gui.gamesbutton); // disable games button if it was enabled
  830.       GUIBUTTON_DISABLE (the_scene.gui.peoplebutton); // disable people button if it was enabled
  831.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_PAUSE, ((the_board.game_state == STATE_PLAYING) && (the_board.move_count > 1) ? MF_ENABLED : MF_GRAYED)); // allow pause if the game has started
  832.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_RESIGN, ((the_board.game_state == STATE_PLAYING) && (the_board.move_count > 1) ? MF_ENABLED : MF_GRAYED)); // allow resigning if the game has started AND it's not terminated)
  833.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_STATISTICS, MF_GRAYED); // disable stats
  834.  
  835.       enabled_value = MF_GRAYED; // remember to disable the internet-related menu items
  836.    }
  837.  
  838.    EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_SHOWONLINEPLAYERS, enabled_value);
  839.    EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_SHOWSOUGHTGAMES, enabled_value);
  840.    EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_SEEKGAME, enabled_value);
  841.    if (!options.network.want_publicchat)
  842.    {
  843.       EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_CHATTERCHANNELS, MF_GRAYED); // always grayed if we don't want public chat
  844.       EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_ENTERCHATTEXT, MF_GRAYED);
  845.    }
  846.    else
  847.    {
  848.       EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_CHATTERCHANNELS, enabled_value); // else enabled only in internet mode
  849.       EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_ENTERCHATTEXT, enabled_value);
  850.    }
  851.    EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_DISPLAYPLAYERCARD, enabled_value);
  852.    EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_DISPLAYYOURCARD, enabled_value);
  853.    EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_MOTD, enabled_value);
  854.  
  855.    the_board.reevaluate = false; // board evaluation has been done
  856.    return; // finished, new board state is known
  857. }
  858.