Subversion Repositories Games.Chess Giants

Rev

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