Subversion Repositories Games.Chess Giants

Rev

Rev 1 | Rev 3 | 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. // handy macros
  8. #define GUIBUTTON_ENABLE(button) { if ((button).state == 0) (button).state = 1; }
  9. #define GUIBUTTON_DISABLE(button) { if ((button).state > 0) (button).state = 0; }
  10.  
  11.  
  12. // prototypes of locally used functions
  13. static void MainLoop_FindCurrentViewer (void);
  14. static void MainLoop_EvaluateGameState (void);
  15.  
  16.  
  17. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, char *lpCmdLine, int nCmdShow)
  18. {
  19.    // the entry point for any Windows program
  20.  
  21.    WNDCLASSEX wc;
  22.    MENUINFO menu_info;
  23.    MSG msg;
  24.    HBITMAP hSplashBmp;
  25.    BITMAP splash_bmp;
  26.    HDC hdc;
  27.    HMENU hMenu;
  28.    HMENU hDropDownMenu;
  29.    RECT rect;
  30.    PAINTSTRUCT ps;
  31.    HBITMAP hbmTmp;
  32.    HDC hdcMem;
  33.    float previous_time;
  34.    int frame_count;
  35.    int array_index;
  36.    bool is_success;
  37.    char *endptr;
  38.    wchar_t app_pathname[MAX_PATH];
  39.    wchar_t language_file[MAX_PATH];
  40.    struct _stat fileinfo;
  41.    HACCEL hAccelerators;
  42.    ACCEL accelerators[] =
  43.    {
  44.       {FVIRTKEY | FCONTROL, L'O',     MENUID_GAME_LOAD},
  45.       {FVIRTKEY | FCONTROL, L'S',     MENUID_GAME_SAVE},
  46.       {FVIRTKEY | FCONTROL, L'Z',     MENUID_CHESSBOARD_CANCELLASTMOVE},
  47.       {FVIRTKEY,            VK_HOME,  MENUID_CHESSBOARD_BEGINNINGOFGAME},
  48.       {FVIRTKEY,            VK_LEFT,  MENUID_CHESSBOARD_PREVIOUSMOVE},
  49.       {FVIRTKEY,            VK_RIGHT, MENUID_CHESSBOARD_NEXTMOVE},
  50.       {FVIRTKEY,            VK_END,   MENUID_CHESSBOARD_CURRENTSTATEOFGAME},
  51.       {FVIRTKEY | FCONTROL, L'G',     MENUID_CHESSBOARD_GOTOMOVE},
  52.       {FVIRTKEY,            VK_F1,    MENUID_HELP_HELP},
  53.       {FVIRTKEY,            VK_F2,    MENUID_GAME_NEWGAME},
  54.       {FVIRTKEY,            VK_F3,    MENUID_GAME_STATISTICS},
  55.       {FVIRTKEY,            VK_F4,    MENUID_GAME_OPTIONS},
  56.       {FVIRTKEY,            VK_F5,    MENUID_CHESSBOARD_TOPVIEW},
  57.       {FVIRTKEY,            VK_F6,    MENUID_CHESSBOARD_DEFAULTVIEW},
  58.       {FVIRTKEY,            VK_F7,    MENUID_CHESSBOARD_RESETVIEW},
  59.       {FVIRTKEY,            VK_UP,    MENUID_CHESSBOARD_ZOOMIN},
  60.       {FVIRTKEY,            VK_DOWN,  MENUID_CHESSBOARD_ZOOMOUT},
  61.       {FVIRTKEY | FCONTROL, VK_DOWN,  MENUID_CHESSBOARD_DISPLAYWINDOWSDESKTOP},
  62.       {FVIRTKEY,            VK_F8,    MENUID_CHESSBOARD_CHANGEAPPEARANCE},
  63.       {FVIRTKEY,            VK_F9,    MENUID_INTERNET_SHOWONLINEPLAYERS},
  64.       {FVIRTKEY,            VK_F10,   MENUID_INTERNET_SHOWSOUGHTGAMES},
  65.    };
  66.  
  67.    // save application instance
  68.    hAppInstance = hInstance;
  69.  
  70.    // find module and path names, and *IMPORTANT*, set the current working directory there
  71.    GetModuleFileName (NULL, app_pathname, WCHAR_SIZEOF (app_pathname));
  72.    GetDirectoryPath (app_pathname, app_path);
  73.    SetCurrentDirectoryW (app_path);
  74.  
  75.    // initialize stuff
  76.    terminate_everything = false;
  77.    hMainWnd = NULL;
  78.    hChatterChannelsWnd = NULL;
  79.    hGamesWnd = NULL;
  80.    hMOTDWnd = NULL;
  81.    hOpponentsWnd = NULL;
  82.    hSoughtWnd = NULL;
  83. #ifdef NDEBUG
  84.    want_framerate = false; // release mode, don't display framerate
  85. #else
  86.    want_framerate = true; // display framerate in debug mode
  87. #endif // NDEBUG
  88.    is_dialogbox_about_validated = false;
  89.    is_dialogbox_challenge_validated = false;
  90.    is_dialogbox_changeappearance_validated = false;
  91.    is_dialogbox_comment_validated = false;
  92.    is_dialogbox_endgame_validated = false;
  93.    is_dialogbox_gotomove_validated = false;
  94.    is_dialogbox_load_validated = false;
  95.    is_dialogbox_message_validated = false;
  96.    is_dialogbox_newgame_validated = false;
  97.    is_dialogbox_options_validated = false;
  98.    is_dialogbox_pawnpromotion_validated = false;
  99.    is_dialogbox_playercard_validated = false;
  100.    is_dialogbox_playerinfoname_validated = false;
  101.    is_dialogbox_quit_validated = false;
  102.    is_dialogbox_resign_validated = false;
  103.    is_dialogbox_save_validated = false;
  104.    is_dialogbox_saveposition_validated = false;
  105.    is_dialogbox_sendchallenge_validated = false;
  106.    is_dialogbox_sendseek_validated = false;
  107.    is_dialogbox_takeback_validated = false;
  108.    is_window_chat_validated = false;
  109.    is_window_chatterchannels_validated = false;
  110.    is_window_games_validated = false;
  111.    is_window_motd_validated = false;
  112.    is_window_opponents_validated = false;
  113.    is_window_sought_validated = false;
  114.    save_pathname[0] = 0;
  115.    srand ((unsigned int) time (NULL)); // initialize PRNG
  116.  
  117.    // read configuration data
  118.    Config_Load ();
  119.  
  120.    // read texts in the right language, if such a translation exists
  121.    is_success = false;
  122.    GetLocaleInfo (LOCALE_SYSTEM_DEFAULT, LOCALE_SENGLANGUAGE, os_language, WCHAR_SIZEOF (os_language));
  123.    swprintf_s (language_file, WCHAR_SIZEOF (language_file), L"data/languages/%s.ini", os_language);
  124.    is_success = LocalizedTexts_Init (language_file);
  125.  
  126.    // fallback to English if language not found
  127.    if (!is_success)
  128.       is_success = LocalizedTexts_Init (L"data/languages/English.ini");
  129.  
  130.    // consistency check: don't go further if texts aren't available
  131.    if (!is_success)
  132.       return (-1); // bomb out on error
  133.  
  134.    // create the main menu line
  135.    hMenu = CreateMenu ();
  136.    hDropDownMenu = CreateMenu (); // create the first drop-down item
  137.    AppendMenu (hDropDownMenu, MF_STRING, MENUID_GAME_NEWGAME, LOCALIZE (L"Menu_GameNewGame"));
  138.    AppendMenu (hDropDownMenu, MF_STRING, MENUID_GAME_LOAD, LOCALIZE (L"Menu_GameLoadGame"));
  139.    AppendMenu (hDropDownMenu, MF_STRING, MENUID_GAME_SETUPPOSITION, LOCALIZE (L"Menu_GameSetupPosition"));
  140.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_GAME_SAVE, LOCALIZE (L"Menu_GameSaveGame")); // initially grayed
  141.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_GAME_SAVEAS, LOCALIZE (L"Menu_GameSaveGameAs")); // initially grayed
  142.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_GAME_SAVEPOSITIONAS, LOCALIZE (L"Menu_GameSavePositionAs")); // initially grayed
  143.    AppendMenu (hDropDownMenu, MF_STRING, MENUID_GAME_RESIGN, LOCALIZE (L"Menu_GameResign"));
  144.    AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
  145.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_GAME_STATISTICS, LOCALIZE (L"Menu_GameStatistics"));
  146.    AppendMenu (hDropDownMenu, MF_STRING, MENUID_GAME_OPTIONS, LOCALIZE (L"Menu_GameOptions"));
  147.    AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
  148.    AppendMenu (hDropDownMenu, MF_STRING, MENUID_GAME_QUIT, LOCALIZE (L"Menu_GameQuit"));
  149.    AppendMenu (hMenu, MF_POPUP, (UINT) hDropDownMenu, LOCALIZE (L"Menu_Game"));
  150.    DestroyMenu (hDropDownMenu);
  151.    hDropDownMenu = CreateMenu (); // create the second drop-down item
  152.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_CHESSBOARD_SUGGESTMOVE, LOCALIZE (L"Menu_ChessboardSuggestMove")); // initially grayed
  153.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_CHESSBOARD_CANCELLASTMOVE, LOCALIZE (L"Menu_ChessboardCancelLastMove")); // initially grayed
  154.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_CHESSBOARD_COMMENTMOVE, LOCALIZE (L"Menu_ChessboardCommentMove")); // initially grayed
  155.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_CHESSBOARD_GOTOMOVE, LOCALIZE (L"Menu_ChessboardGoToMove")); // initially grayed
  156.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_CHESSBOARD_SWAPSIDES, LOCALIZE (L"Menu_ChessboardSwapSides")); // initially grayed
  157.    AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
  158.    AppendMenu (hDropDownMenu, MF_STRING, MENUID_CHESSBOARD_TOPVIEW, LOCALIZE (L"Menu_ChessboardTopView"));
  159.    AppendMenu (hDropDownMenu, MF_STRING, MENUID_CHESSBOARD_DEFAULTVIEW, LOCALIZE (L"Menu_ChessboardDefaultView"));
  160.    AppendMenu (hDropDownMenu, MF_STRING, MENUID_CHESSBOARD_RESETVIEW, LOCALIZE (L"Menu_ChessboardResetView"));
  161.    AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
  162.    AppendMenu (hDropDownMenu, MF_STRING, MENUID_CHESSBOARD_CHANGEAPPEARANCE, LOCALIZE (L"Menu_ChessboardChangeAppearance"));
  163.    if (options.want_fullscreen)
  164.    {
  165.       AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
  166.       AppendMenu (hDropDownMenu, MF_STRING, MENUID_CHESSBOARD_DISPLAYWINDOWSDESKTOP, LOCALIZE (L"Menu_ChessboardDisplayWindowsDesktop"));
  167.    }
  168.    AppendMenu (hMenu, MF_POPUP, (UINT) hDropDownMenu, LOCALIZE (L"Menu_Chessboard"));
  169.    DestroyMenu (hDropDownMenu);
  170.    hDropDownMenu = CreateMenu (); // create the third drop-down item
  171.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_SHOWONLINEPLAYERS, LOCALIZE (L"Menu_InternetShowOnlinePlayers")); // initially grayed
  172.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_SHOWSOUGHTGAMES, LOCALIZE (L"Menu_InternetShowSoughtGames")); // initially grayed
  173.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_SEEKGAME, LOCALIZE (L"Menu_InternetSeekGame")); // initially grayed
  174.    AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
  175.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_CHATTERCHANNELS, LOCALIZE (L"Menu_InternetChatterChannels")); // initially grayed
  176.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_ENTERCHATTEXT, LOCALIZE (L"Menu_InternetEnterChatText")); // initially grayed
  177.    AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
  178.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_DISPLAYPLAYERCARD, LOCALIZE (L"Menu_InternetDisplayPlayerCard")); // initially grayed
  179.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_DISPLAYYOURCARD, LOCALIZE (L"Menu_InternetDisplayYourCard")); // initially grayed
  180.    AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
  181.    AppendMenu (hDropDownMenu, MF_STRING | MF_GRAYED, MENUID_INTERNET_MOTD, LOCALIZE (L"Menu_InternetDisplayMOTD")); // initially grayed
  182.    AppendMenu (hMenu, MF_POPUP, (UINT) hDropDownMenu, LOCALIZE (L"Menu_Internet"));
  183.    DestroyMenu (hDropDownMenu);
  184.    hDropDownMenu = CreateMenu (); // create the fourth drop-down item
  185.    AppendMenu (hDropDownMenu, MF_STRING, MENUID_HELP_HELP, LOCALIZE (L"Menu_HelpDisplayHelp"));
  186.    AppendMenu (hDropDownMenu, MF_STRING, MENUID_HELP_GETCHESSGAMES, LOCALIZE (L"Menu_HelpGetChessGames"));
  187.    AppendMenu (hDropDownMenu, MF_SEPARATOR, 0, NULL);
  188.    AppendMenu (hDropDownMenu, MF_STRING, MENUID_HELP_ABOUT, LOCALIZE (L"Menu_HelpAbout"));
  189.    AppendMenu (hMenu, MF_POPUP, (UINT) hDropDownMenu, LOCALIZE (L"Menu_Help"));
  190.    DestroyMenu (hDropDownMenu);
  191.  
  192.    // create the accelerators
  193.    hAccelerators = CreateAcceleratorTable (accelerators, sizeof (accelerators) / sizeof (ACCEL));
  194.  
  195.    // register the window class, create the window and show it
  196.    memset (&wc, 0, sizeof (wc));
  197.    wc.cbSize = sizeof (wc);
  198.    wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; // double-clicks support
  199.    wc.lpfnWndProc = WindowProc_Main;
  200.    wc.hInstance = hAppInstance;
  201.    wc.hIcon = LoadIcon (hAppInstance, (wchar_t *) ICON_MAIN);
  202.    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  203.    wc.lpszClassName = PROGRAM_NAME L" WndClass";
  204.    RegisterClassEx (&wc);
  205.    if (options.want_fullscreen)
  206.       hMainWnd = CreateWindowEx (0, wc.lpszClassName, PROGRAM_NAME, WS_POPUPWINDOW,
  207.                                  0, 0, GetSystemMetrics (SM_CXSCREEN), GetSystemMetrics (SM_CYSCREEN),
  208.                                  NULL, hMenu, hAppInstance, NULL);
  209.    else
  210.    {
  211.       // in windowed mode, ensure window width and height aren't greater than screen size
  212.       if (options.window_width > GetSystemMetrics (SM_CXMAXTRACK))
  213.          options.window_width = GetSystemMetrics (SM_CXMAXTRACK);
  214.       if (options.window_height > GetSystemMetrics (SM_CYMAXTRACK))
  215.          options.window_height = GetSystemMetrics (SM_CYMAXTRACK);
  216.  
  217.       hMainWnd = CreateWindowEx (0, wc.lpszClassName, PROGRAM_NAME, WS_OVERLAPPEDWINDOW,
  218.                                  GetSystemMetrics (SM_CXSCREEN) / 2 - options.window_width / 2,
  219.                                  GetSystemMetrics (SM_CYSCREEN) / 2 - options.window_height / 2,
  220.                                  options.window_width,
  221.                                  options.window_height,
  222.                                  NULL, hMenu, hAppInstance, NULL);
  223.    }
  224.    ShowWindow (hMainWnd, nCmdShow);
  225.  
  226.    // display the splash screen
  227.    memset ((void *) &splash_bmp, 0, sizeof (splash_bmp));
  228.    hSplashBmp = (HBITMAP) LoadImage (NULL, L"data/splash.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); // load the splash bitmap
  229.    GetObject (hSplashBmp, sizeof (splash_bmp), (void *) &splash_bmp); // get a structure containing its size (among others)
  230.    hdcMem = CreateCompatibleDC (NULL); // create a device context compatible with the screen
  231.    hbmTmp = SelectBitmap (hdcMem, hSplashBmp); // select our bitmap to use in it
  232.    hdc = BeginPaint (hMainWnd, &ps); // begin painting on the main window
  233.    GetClientRect (hMainWnd, &rect);
  234.    StretchBlt (hdc, 0, 0, rect.right, rect.bottom, hdcMem, 0, 0, splash_bmp.bmWidth, splash_bmp.bmHeight, SRCCOPY); // bit blit the bitmap into it
  235.    EndPaint (hMainWnd, &ps); // end painting on the main window
  236.    SelectObject (hdcMem, hbmTmp); // restore the previous selection
  237.    DeleteDC (hdcMem); // and delete the handles to the device context
  238.    DeleteObject (hSplashBmp); // and to the bitmap that we used
  239.  
  240.    // make the menu modeless
  241.    memset (&menu_info, 0, sizeof (menu_info)); // prepare menu info structure
  242.    menu_info.cbSize = sizeof (MENUINFO);
  243.    menu_info.fMask = MIM_STYLE;
  244.    GetMenuInfo (GetMenu (hMainWnd), &menu_info); // get current style
  245.    menu_info.dwStyle |= MNS_MODELESS; // add the "modeless" flag
  246.    SetMenuInfo (GetMenu (hMainWnd), &menu_info); // and send it back
  247.  
  248.    // temporarily add the files for the font we'll use to the system resources
  249.    AddFontResourceEx (L"data/fonts/papyrus.ttf", FR_PRIVATE, 0);
  250.  
  251.    // load status icons, bitmaps and texts
  252.    handlestatus[HANDLESTATUS_AVAILABLE].icon = (HICON) LoadImage (NULL, L"data/icons/available.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
  253.    handlestatus[HANDLESTATUS_AVAILABLE].bitmap = (HBITMAP) LoadImage (NULL, L"data/status/available.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  254.    handlestatus[HANDLESTATUS_AVAILABLE].text = LOCALIZE (L"Opponents_StatusAvailable");
  255.    handlestatus[HANDLESTATUS_INGAME].icon = (HICON) LoadImage (NULL, L"data/icons/ingame.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
  256.    handlestatus[HANDLESTATUS_INGAME].bitmap = (HBITMAP) LoadImage (NULL, L"data/status/ingame.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  257.    handlestatus[HANDLESTATUS_INGAME].text = LOCALIZE (L"Opponents_StatusInGame");
  258.    handlestatus[HANDLESTATUS_INSIMULATION].icon = (HICON) LoadImage (NULL, L"data/icons/insimulation.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
  259.    handlestatus[HANDLESTATUS_INSIMULATION].bitmap = (HBITMAP) LoadImage (NULL, L"data/status/insimulation.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  260.    handlestatus[HANDLESTATUS_INSIMULATION].text = LOCALIZE (L"Opponents_StatusInSimulation");
  261.    handlestatus[HANDLESTATUS_INTOURNAMENT].icon = (HICON) LoadImage (NULL, L"data/icons/intournament.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
  262.    handlestatus[HANDLESTATUS_INTOURNAMENT].bitmap = (HBITMAP) LoadImage (NULL, L"data/status/intournament.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  263.    handlestatus[HANDLESTATUS_INTOURNAMENT].text = LOCALIZE (L"Opponents_StatusInTournament");
  264.    handlestatus[HANDLESTATUS_EXAMININGAGAME].icon = (HICON) LoadImage (NULL, L"data/icons/examiningagame.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
  265.    handlestatus[HANDLESTATUS_EXAMININGAGAME].bitmap = (HBITMAP) LoadImage (NULL, L"data/status/examiningagame.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  266.    handlestatus[HANDLESTATUS_EXAMININGAGAME].text = LOCALIZE (L"Opponents_StatusExaminingAGame");
  267.    handlestatus[HANDLESTATUS_NOTOPENFORAMATCH].icon = (HICON) LoadImage (NULL, L"data/icons/notopenforamatch.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
  268.    handlestatus[HANDLESTATUS_NOTOPENFORAMATCH].bitmap = (HBITMAP) LoadImage (NULL, L"data/status/notopenforamatch.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  269.    handlestatus[HANDLESTATUS_NOTOPENFORAMATCH].text = LOCALIZE (L"Opponents_StatusNotOpenForAMatch");
  270.    handlestatus[HANDLESTATUS_INACTIVEORBUSY].icon = (HICON) LoadImage (NULL, L"data/icons/inactiveorbusy.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
  271.    handlestatus[HANDLESTATUS_INACTIVEORBUSY].bitmap = (HBITMAP) LoadImage (NULL, L"data/status/inactiveorbusy.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  272.    handlestatus[HANDLESTATUS_INACTIVEORBUSY].text = LOCALIZE (L"Opponents_StatusInactiveOrBusy");
  273.    handlestatus[HANDLESTATUS_OFFLINE].icon = (HICON) LoadImage (NULL, L"data/icons/offline.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
  274.    handlestatus[HANDLESTATUS_OFFLINE].bitmap = (HBITMAP) LoadImage (NULL, L"data/status/offline.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  275.    handlestatus[HANDLESTATUS_OFFLINE].text = LOCALIZE (L"Opponents_StatusOffline");
  276.  
  277.    // load fonts
  278.    hFontChat = CreateFont (17, 0, 0, 0, FW_DONTCARE, false, false, false, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
  279.                            CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Comic Sans MS");
  280.  
  281.    // initialize renderer
  282.    if (!Render_Init ())
  283.       return (-1); // bomb out on error
  284.  
  285.    // load sprites
  286.    larrow_spriteindex = Render_LoadSprite (L"data/sprites/arrow-left.*");
  287.    rarrow_spriteindex = Render_LoadSprite (L"data/sprites/arrow-right.*");
  288.    chatbutton_spriteindex = Render_LoadSprite (L"data/sprites/chat.*");
  289.    gamesbutton_spriteindex = Render_LoadSprite (L"data/sprites/games.*");
  290.    peoplebutton_spriteindex = Render_LoadSprite (L"data/sprites/people.*");
  291.    sepia_spriteindex = Render_LoadSprite (L"data/sprites/sepia.*");
  292.    for (array_index = 0; array_index < 12; array_index++)
  293.       spinner_spriteindex[array_index] = Render_LoadSprite (L"data/sprites/spinner-%d.png", array_index * 30); // spinning wheel
  294.  
  295.    // load fonts
  296.    arrow_fontindex = Render_LoadFont (L"Papyrus", 20, false, false);
  297.    chat_fontindex = Render_LoadFont (L"Papyrus", 24, false, false);
  298.    players_fontindex = Render_LoadFont (L"Papyrus", 32, false, true);
  299.    centermsg_fontindex = Render_LoadFont (L"Papyrus", 48, false, true);
  300.  
  301.    // load themes, initialize a new human vs. human game and setup the scene
  302.    if (!Themes_Init ())
  303.       return (-1); // bomb out on error
  304.    Board_Init (&the_board, PLAYER_HUMAN, PLAYER_HUMAN, FENSTARTUP_STANDARDCHESS);
  305.    Scene_Init (&the_scene, &the_board);
  306.  
  307.    // has a filename been specifed on the command-line AND that file exists ?
  308.    if ((lpCmdLine != NULL) && (lpCmdLine[0] != 0))
  309.    {
  310.       while (*lpCmdLine == '"')
  311.          lpCmdLine++; // skip leading quotes
  312.       if ((endptr = strchr (lpCmdLine, '"')) != NULL)
  313.          *endptr = 0; // break the string at the first ending quote
  314.       if (_stat (lpCmdLine, &fileinfo) == 0)
  315.       {
  316.          ConvertToWideChar (load_pathname, WCHAR_SIZEOF (load_pathname), lpCmdLine); // save pathname string
  317.          is_dialogbox_load_validated = true; // and act as if the user just validated a load dialog box
  318.       }
  319.       else
  320.          DialogBox_NewGame (); // if specified filename doesn't exist, fallback to the "new game" dialog box
  321.    }
  322.    else
  323.       DialogBox_NewGame (); // when no filename is specified, display the new game dialog box
  324.  
  325.    // enter the main loop
  326.    animation_endtime = 0.0f;
  327.    sound_playtime = 0.0f;
  328.    highlight_endtime = 0.0f;
  329.    previous_time = 0.0f;
  330.    frame_count = 0;
  331.    while (!terminate_everything)
  332.    {
  333.       // see what time it is
  334.       current_time = ProcessTime ();
  335.  
  336. #ifdef DEMO
  337.       // are we in demo mode and is it time to quit ?
  338.       if (current_time > DEMO_TIMEOUT)
  339.          terminate_everything = true; // if so, send a quit message in order to break the loop
  340. #endif
  341.  
  342.       // grab the current window size
  343.       GetWindowRect (hMainWnd, &rect);
  344.       options.window_width = rect.right - rect.left;
  345.       options.window_height = rect.bottom - rect.top;
  346.  
  347.       // are we in the middle of an animation or just after it ?
  348.       if (current_time < animation_endtime + 0.5f)
  349.          the_scene.update = true; // always update during animations
  350.       else
  351.          the_scene.update |= Board_Think (&the_board); // make the board (i.e, both players) think
  352.  
  353.       MainLoop_FindCurrentViewer ();  // determine current viewer
  354.  
  355.       // see if we have a message waiting for any window in the current thread and dispatch it
  356.       while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  357.       {
  358.          // check for accelerator keystrokes from the main window
  359.          if ((msg.hwnd != hMainWnd) || !TranslateAccelerator (hMainWnd, hAccelerators, &msg))
  360.          {
  361.             TranslateMessage (&msg); // translate and dispatch all other messages
  362.             DispatchMessage (&msg);
  363.          }
  364.       }
  365.  
  366.       // handle dialog box and window return codes
  367.       if (is_dialogbox_about_validated) DialogBox_About_Validated ();
  368.       if (is_dialogbox_challenge_validated) DialogBox_Challenge_Validated ();
  369.       if (is_dialogbox_changeappearance_validated) DialogBox_ChangeAppearance_Validated ();
  370.       if (is_dialogbox_comment_validated) DialogBox_Comment_Validated ();
  371.       if (is_dialogbox_endgame_validated) DialogBox_EndGame_Validated ();
  372.       if (is_dialogbox_gotomove_validated) DialogBox_GoToMove_Validated ();
  373.       if (is_dialogbox_load_validated) DialogBox_Load_Validated ();
  374.       if (is_dialogbox_message_validated) DialogBox_Message_Validated ();
  375.       if (is_dialogbox_newgame_validated) DialogBox_NewGame_Validated ();
  376.       if (is_dialogbox_options_validated) DialogBox_Options_Validated ();
  377.       if (is_dialogbox_pawnpromotion_validated) DialogBox_PawnPromotion_Validated ();
  378.       if (is_dialogbox_playercard_validated) DialogBox_PlayerCard_Validated ();
  379.       if (is_dialogbox_playerinfoname_validated) DialogBox_PlayerInfoName_Validated ();
  380.       if (is_dialogbox_quit_validated) DialogBox_Quit_Validated ();
  381.       if (is_dialogbox_resign_validated) DialogBox_Resign_Validated ();
  382.       if (is_dialogbox_save_validated) DialogBox_Save_Validated ();
  383.       if (is_dialogbox_saveposition_validated) DialogBox_SavePosition_Validated ();
  384.       if (is_dialogbox_sendchallenge_validated) DialogBox_SendChallenge_Validated ();
  385.       if (is_dialogbox_sendseek_validated) DialogBox_SendSeek_Validated ();
  386.       if (is_dialogbox_takeback_validated) DialogBox_Takeback_Validated ();
  387.       if (is_window_chat_validated) Window_Chat_Validated ();
  388.       if (is_window_chatterchannels_validated) Window_ChatterChannels_Validated ();
  389.       if (is_window_games_validated) Window_Games_Validated ();
  390.       if (is_window_motd_validated) Window_MOTD_Validated ();
  391.       if (is_window_opponents_validated) Window_Opponents_Validated ();
  392.       if (is_window_sought_validated) Window_Sought_Validated ();
  393.  
  394.       MainLoop_EvaluateGameState (); // evaluate game state
  395.  
  396.       // is the table rotating ?
  397.       if (Player_RotateTable (&the_board.players[current_viewer], current_time - previous_time))
  398.          the_scene.update = true; // if so, update the scene
  399.  
  400.       // are we highlighting something ?
  401.       if (highlight_endtime > current_time)
  402.          the_scene.update = true; // if so, update the scene
  403.  
  404.       // else if we WERE just highlighting something, clear the hovered positions
  405.       else if (highlight_endtime + 0.1f > current_time)
  406.       {
  407.          the_board.hovered_position[0] = -1;
  408.          the_board.hovered_position[1] = -1;
  409.          the_scene.update = true; // and update the scene
  410.       }
  411.  
  412.       // if we want the game clock, update scene every second
  413.       if (options.want_clock && ((int) current_time != (int) previous_time))
  414.          the_scene.update = true;
  415.  
  416.       // if there's something in the central text buffer, update scene too
  417.       if (the_scene.gui.central_text.is_displayed)
  418.          the_scene.update = true;
  419.  
  420.       // if we have a spinning wheel, update scene too
  421.       if (the_scene.gui.want_spinwheel)
  422.          the_scene.update = true;
  423.  
  424.       // do we NOT need to update the scene ?
  425.       if (!the_scene.update)
  426.       {
  427.          // cycle through all themes and see if one of them needs to be loaded
  428.          for (array_index = 0; array_index < theme_count; array_index++)
  429.             if (!themes[array_index].is_loaded)
  430.             {
  431.                Theme_Load (&themes[array_index], false); // load a bit more of this theme
  432.                break; // and stop looping (see you next pass)
  433.             }
  434.       }
  435.  
  436.       // do we need to update the scene ?
  437.       if (the_scene.update || want_framerate)
  438.       {
  439.          Scene_Update (&the_scene, &the_board); // evaluate which parts need to be placed
  440.          Render_RenderFrame (&the_scene); // render a game frame
  441.  
  442.          the_scene.update = false; // scene no longer needs to be updated now
  443.       }
  444.  
  445.       Audio_Think (); // ensure audio is playing
  446.  
  447.       previous_time = current_time; // save previous time
  448.       if (frame_count == 100)
  449.       {
  450.          frame_count = 0; // reset frame count every 100 frames
  451.          Sleep (1); // once every 100 frames, wait a millisecond
  452.       }
  453.       else
  454.          Sleep (0); // else just allow context switching
  455.       frame_count++; // increase the frame count
  456.    }
  457.  
  458.    /////////////////////////////////////////////////////////////////////////
  459.    // at this point, we exited the main loop and we are returning to Windows
  460.  
  461.    // release scene, game, themes and shutdown Direct3D
  462.    Scene_Shutdown (&the_scene);
  463.    Board_Shutdown (&the_board);
  464.    Themes_Shutdown ();
  465.    Render_Shutdown ();
  466.  
  467.    // delete the font resources
  468.    DeleteObject (hFontChat);
  469.  
  470.    // delete the bitmap and icon ressources
  471.    for (array_index = 1; array_index < sizeof (handlestatus) / sizeof (handlestatus[0]); array_index++)
  472.    {
  473.       DeleteObject (handlestatus[array_index].bitmap);
  474.       DestroyIcon (handlestatus[array_index].icon);
  475.    }
  476.  
  477.    // unregister window class
  478.    UnregisterClass (wc.lpszClassName, hAppInstance);
  479.  
  480.    // destroy the accelerators table
  481.    if (hAccelerators)
  482.       DestroyAcceleratorTable (hAccelerators);
  483.    hAccelerators = NULL;
  484.  
  485.    // destroy the game menu
  486.    if (IsMenu (hMenu))
  487.       DestroyMenu (hMenu);
  488.    hMenu = NULL;
  489.  
  490.    // save configuration data
  491.    Config_Save ();
  492.  
  493.    // unload localized texts
  494.    LocalizedTexts_Shutdown ();
  495.  
  496.    return (0); // and return to Windows.
  497. }
  498.  
  499.  
  500. static void MainLoop_FindCurrentViewer (void)
  501. {
  502.    // helper function that tells who is the current viewer
  503.  
  504.    if ((the_board.players[COLOR_WHITE].type == PLAYER_HUMAN) && (the_board.players[COLOR_BLACK].type == PLAYER_HUMAN))
  505.       current_viewer = Board_ColorToMove (&the_board); // if both players are human, track them both
  506.    else if (the_board.players[COLOR_WHITE].type == PLAYER_HUMAN)
  507.       current_viewer = COLOR_WHITE; // else if only the white is human, track the white
  508.    else if (the_board.players[COLOR_BLACK].type == PLAYER_HUMAN)
  509.       current_viewer = COLOR_BLACK; // else if it's the black, track the black
  510.    else
  511.       current_viewer = COLOR_WHITE; // else no human in game, track just the white
  512.  
  513.    return; // finished, current viewer is known
  514. }
  515.  
  516.  
  517. static void MainLoop_EvaluateGameState (void)
  518. {
  519.    // function to evaluate the game state in the main loop when a part has just moved
  520.  
  521.    wchar_t window_title[256];
  522.    player_t *current_player;
  523.    player_t *opposite_player;
  524.    player_t *network_player;
  525.    boardmove_t *last_move;
  526.    int enabled_value;
  527.    int move_index;
  528.  
  529.    if (!the_board.reevaluate)
  530.       return; // if the board doesn't need to be reevaluated, don't do anything
  531.  
  532.    // get current and opposite players
  533.    current_player = Player_GetCurrent ();
  534.    opposite_player = Player_GetOpposite ();
  535.  
  536.    // see if we're online
  537.    network_player = Player_FindByType (PLAYER_INTERNET);
  538.  
  539.    // has the game started ?
  540.    if (the_board.move_count > 1)
  541.    {
  542.       // game has started, enable the "save" and "save as" menu options
  543.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVE, MF_ENABLED);
  544.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEAS, MF_ENABLED);
  545.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_ENABLED);
  546.  
  547.       // enable the "go to move" menu option
  548.       EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_GOTOMOVE, MF_ENABLED);
  549.  
  550.       // are we watching the last move ?
  551.       if (the_board.viewed_move == the_board.move_count - 1)
  552.       {
  553.          // get a quick acccess to the last move
  554.          last_move = &the_board.moves[the_board.move_count - 1];
  555.  
  556.          // if the current player is a human AND its opponent is a computer, allow him to ask us for a hint
  557.          if ((current_player->type == PLAYER_HUMAN) && (opposite_player->type == PLAYER_COMPUTER))
  558.             EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SUGGESTMOVE, MF_ENABLED);
  559.          else
  560.             EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SUGGESTMOVE, MF_GRAYED);
  561.  
  562.          // (if the current player is a human
  563.          //  AND (its opponent is another human AND there's at least one move played)
  564.          //       OR (its opponent is a computer AND there are at least two moves played)
  565.          //       OR (its opponent is a remote player AND we're in game AND there are at least two moves played))
  566.          // 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
  567.          if (((current_player->type == PLAYER_HUMAN)
  568.               && ((opposite_player->type == PLAYER_HUMAN)
  569.                   || ((opposite_player->type == PLAYER_COMPUTER) && (the_board.move_count > 2))
  570.                   || ((opposite_player->type == PLAYER_INTERNET) && (opposite_player->is_in_game) && (the_board.move_count > 2))))
  571.              || ((current_player->type == PLAYER_INTERNET) && (current_player->is_in_game) && (the_board.move_count > 2)))
  572.             EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_CANCELLASTMOVE, MF_ENABLED);
  573.          else
  574.             EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_CANCELLASTMOVE, MF_GRAYED);
  575.  
  576.          // is the current player in check ? (to play the right sound)
  577.          // read as: was the last move an opponent's move AND did it put us to check ?
  578.          if (last_move->is_check)
  579.          {
  580.             // is it a checkmate ? (checkmate == check + stalemate)
  581.             if (last_move->is_stalemate)
  582.             {
  583.                // display the game over dialog box
  584.                the_board.game_state = (Board_ColorToMove (&the_board) == COLOR_WHITE ? STATE_BLACKWIN_CHECKMATE : STATE_WHITEWIN_CHECKMATE);
  585.                DialogBox_EndGame ();
  586.             }
  587.          }
  588.          else
  589.          {
  590.             // is it a stalemate ?
  591.             if (last_move->is_stalemate)
  592.             {
  593.                // display the game over dialog box
  594.                the_board.game_state = STATE_DRAW_STALEMATE;
  595.                DialogBox_EndGame ();
  596.             }
  597.          }
  598.  
  599.          // have there 50 moves been played each side (i.e, 100 plies) AND is the current player human ?
  600.          if ((the_board.move_count > 100) && (current_player->type == PLAYER_HUMAN))
  601.          {
  602.             // go backwards and see when is the latest move that took an opponent's piece OR the latest pawn move
  603.             for (move_index = the_board.move_count - 1; move_index >= 0; move_index--)
  604.                if (the_board.moves[move_index].has_captured || (the_board.moves[move_index].part == PART_PAWN))
  605.                   break; // stop as soon as we find one
  606.  
  607.             // can the fifty moves draw rule be claimed AND does the current player claims it ?
  608.             if (move_index + 1 + 100 < the_board.move_count)
  609.             {
  610.                // yes. Propose it to the side that's on move
  611. // TODO: non-modal MessageBox (copy dialog_newgame.cpp and use return values)
  612.             }
  613.          }
  614.  
  615.          if (the_scene.gui.larrow.state == 0)
  616.             the_scene.gui.larrow.state = 1; // enable "back" arrow if it isn't displayed yet
  617.          if (the_scene.gui.rarrow.state != 0)
  618.             the_scene.gui.rarrow.state = 0; // disable "forward" arrow if it's already displayed
  619.  
  620.          if (the_board.game_state == STATE_PLAYING)
  621.             Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
  622.                            RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"Current"));
  623.          else if ((the_board.game_state == STATE_BLACKWIN_CHECKMATE) || (the_board.game_state == STATE_WHITEWIN_CHECKMATE))
  624.             Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
  625.                            RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_CheckMate"));
  626.          else if ((the_board.game_state == STATE_WHITEWIN_RESIGNORFORFEIT) || (the_board.game_state == STATE_BLACKWIN_RESIGNORFORFEIT))
  627.             Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
  628.                            RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_Resign"));
  629.          else if (the_board.game_state == STATE_DRAW_STALEMATE)
  630.             Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
  631.                            RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_StaleMate"));
  632.          else if (the_board.game_state == STATE_DRAW_AGREEMENT)
  633.             Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
  634.                            RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_Agreement"));
  635.          else if (the_board.game_state == STATE_DRAW_OTHER)
  636.             Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
  637.                            RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_DrawOther"));
  638.  
  639.          // enable the "comment on this move" menu option
  640.          EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_COMMENTMOVE, MF_ENABLED);
  641.       }
  642.  
  643.       // else are we watching another move, but not the beginning of the game ?
  644.       else if (the_board.viewed_move > 0)
  645.       {
  646.          if (the_scene.gui.larrow.state == 0)
  647.             the_scene.gui.larrow.state = 1; // enable "back" arrow if it isn't displayed yet
  648.          if (the_scene.gui.rarrow.state == 0)
  649.             the_scene.gui.rarrow.state = 1; // enable "forward" arrow if it isn't displayed yet
  650.          Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
  651.                         RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false,
  652.                         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")));
  653.  
  654.          // enable the "save position as" and "comment on this move" menu options
  655.          EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_ENABLED);
  656.          EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_COMMENTMOVE, MF_ENABLED);
  657.       }
  658.  
  659.       // else we must be watching the beginning of the game (no move yet)
  660.       else
  661.       {
  662.          if (the_scene.gui.larrow.state != 0)
  663.             the_scene.gui.larrow.state = 0; // disable "back" arrow if it's already displayed
  664.          if (the_scene.gui.rarrow.state == 0)
  665.             the_scene.gui.rarrow.state = 1; // enable "forward" arrow if it isn't displayed yet
  666.          Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
  667.                         RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"Beginning"));
  668.  
  669.          // disable the "save position as" and "comment on this move" menu options
  670.          EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_GRAYED);
  671.          EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_COMMENTMOVE, MF_GRAYED);
  672.       }
  673.    }
  674.    else
  675.    {
  676.       // game has not started, disable the "save", "save as" and "save position as" menu options
  677.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVE, MF_GRAYED);
  678.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEAS, MF_GRAYED);
  679.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_GRAYED);
  680.  
  681.       // also disable the "suggest move", "cancel last move", "comment move" and "go to move" menu options
  682.       EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SUGGESTMOVE, MF_GRAYED);
  683.       EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_CANCELLASTMOVE, MF_GRAYED);
  684.       EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_COMMENTMOVE, MF_GRAYED);
  685.       EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_GOTOMOVE, MF_GRAYED);
  686.  
  687.       // and disable the two arrows and the arrow text
  688.       the_scene.gui.larrow.state = 0;
  689.       the_scene.gui.rarrow.state = 0;
  690.       the_scene.gui.arrow_text.is_displayed = false;
  691.    }
  692.  
  693.    // 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
  694.    if ((current_player->type == PLAYER_HUMAN) && (opposite_player->type == PLAYER_COMPUTER))
  695.       EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SWAPSIDES, MF_ENABLED);
  696.    else
  697.       EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SWAPSIDES, MF_GRAYED);
  698.  
  699.    // update window title
  700.    if ((the_board.players[COLOR_WHITE].name[0] != 0) && (the_board.players[COLOR_BLACK].name[0] != 0))
  701.    {
  702.       swprintf_s (window_title, WCHAR_SIZEOF (window_title), L"%s %s %s - " PROGRAM_NAME, the_board.players[COLOR_WHITE].name, LOCALIZE (L"Versus"), the_board.players[COLOR_BLACK].name);
  703.       SetWindowText (hMainWnd, window_title); // update window title
  704.    }
  705.    else if (the_board.players[COLOR_WHITE].name[0] != 0)
  706.    {
  707.       swprintf_s (window_title, WCHAR_SIZEOF (window_title), L"%s - " PROGRAM_NAME, the_board.players[COLOR_WHITE].name);
  708.       SetWindowText (hMainWnd, window_title); // update window title
  709.    }
  710.  
  711.    // are we in internet mode AND are we logged in ?
  712.    if ((network_player != NULL) && network_player->is_logged_in)
  713.    {
  714.       // are we currently playing a game ?
  715.       if (network_player->is_in_game)
  716.       {
  717.          GUIBUTTON_ENABLE (the_scene.gui.chatbutton); // enable chat button if it's not enabled yet
  718.          GUIBUTTON_DISABLE (the_scene.gui.gamesbutton); // disable games button if it was enabled
  719.          GUIBUTTON_DISABLE (the_scene.gui.peoplebutton); // disable people button if it was enabled
  720.          EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_RESIGN, MF_ENABLED); // allow resigning
  721.       }
  722.       else
  723.       {
  724.          GUIBUTTON_DISABLE (the_scene.gui.chatbutton); // disable chat button if it was enabled
  725.          GUIBUTTON_ENABLE (the_scene.gui.gamesbutton); // enable games button if it's not enabled yet
  726.          GUIBUTTON_ENABLE (the_scene.gui.peoplebutton); // enable people button if it's not enabled yet
  727.          EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_RESIGN, MF_GRAYED); // disable ability to resign
  728.       }
  729.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_STATISTICS, MF_ENABLED); // enable stats
  730.  
  731.       enabled_value = MF_ENABLED; // remember to enable the internet-related menu items
  732.    }
  733.    // else it's a local or vs. computer game
  734.    else
  735.    {
  736.       GUIBUTTON_DISABLE (the_scene.gui.chatbutton); // disable chat button if it was enabled
  737.       GUIBUTTON_DISABLE (the_scene.gui.gamesbutton); // disable games button if it was enabled
  738.       GUIBUTTON_DISABLE (the_scene.gui.peoplebutton); // disable people button if it was enabled
  739.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_RESIGN, MF_ENABLED); // allow resigning
  740.       EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_STATISTICS, MF_GRAYED); // disable stats
  741.  
  742.       enabled_value = MF_GRAYED; // remember to disable the internet-related menu items
  743.    }
  744.  
  745.    EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SETUPPOSITION, !enabled_value); // note the inversion
  746.    EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_SHOWONLINEPLAYERS, enabled_value);
  747.    EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_SHOWSOUGHTGAMES, enabled_value);
  748.    EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_SEEKGAME, enabled_value);
  749.    if (!options.network.want_publicchat)
  750.    {
  751.       EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_CHATTERCHANNELS, MF_GRAYED); // always grayed if we don't want public chat
  752.       EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_ENTERCHATTEXT, MF_GRAYED);
  753.    }
  754.    else
  755.    {
  756.       EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_CHATTERCHANNELS, enabled_value); // else enabled only in internet mode
  757.       EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_ENTERCHATTEXT, enabled_value);
  758.    }
  759.    EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_DISPLAYPLAYERCARD, enabled_value);
  760.    EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_DISPLAYYOURCARD, enabled_value);
  761.    EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_MOTD, enabled_value);
  762.  
  763.    the_board.reevaluate = false; // board evaluation has been done
  764.    return; // finished, new board state is known
  765. }
  766.