Subversion Repositories Games.Chess Giants

Rev

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