Subversion Repositories Games.Chess Giants

Rev

Rev 21 | Rev 25 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. // window_main.cpp
  2.  
  3. #include "../common.h"
  4.  
  5.  
  6. // global variables used in this module only
  7. static wchar_t folder_path[MAX_PATH];
  8.  
  9.  
  10. // prototypes of local functions
  11. static bool Button_IsHovered (guibutton_t *button, int gui_x, int gui_y);
  12. static bool Button_UpdateHoverState (guibutton_t *button, int gui_x, int gui_y);
  13.  
  14.  
  15. LRESULT CALLBACK WindowProc_Main (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
  16. {
  17.    // this is the main message handler for the program
  18.  
  19.    static wchar_t fen_string[128];
  20.    static char selectable_parts[14] = "PRNBQK kqbnrp";
  21.    static int prevgui_x = 0;
  22.    static int prevgui_y = 0;
  23.    static bool ctrl_pressed = false;
  24.    static bool rbutton_pushed = false;
  25.  
  26.    unsigned short wParam_hiword;
  27.    unsigned short wParam_loword;
  28.    int prev_hovered_position[2];
  29.    player_t *current_player;
  30.    player_t *opposite_player;
  31.    player_t *local_player;
  32.    player_t *remote_player;
  33.    ccreply_t *entered_ccreply;
  34.    boardmove_t *current_move;
  35.    float board_x;
  36.    float board_y;
  37.    int gui_x;
  38.    int gui_y;
  39.    int line;
  40.    int column;
  41.    int index_line;
  42.    int index_column;
  43.    int part_index;
  44.    int part_color;
  45.    int viewer_index;
  46.  
  47.    // filter out the commonly used message values
  48.    wParam_hiword = HIWORD (wParam);
  49.    wParam_loword = LOWORD (wParam);
  50.  
  51.    ////////////////////////////////////////////////////////////////////////////////////////////////
  52.    // has the window just been fired up ?
  53.    if (message == WM_CREATE)
  54.    {
  55.       the_scene.gui.is_entering_text = false; // we are NOT entering text yet
  56.  
  57.       // call the default window message processing function to keep things going
  58.       return (DefWindowProc (hWnd, message, wParam, lParam));
  59.    }
  60.  
  61.    ////////////////////////////////////////////////////////////////////////////////////////////////
  62.    // else have we clicked on the close button ?
  63.    else if (message == WM_CLOSE)
  64.    {
  65.       if (the_board.game_state == STATE_PLAYING)
  66.          DialogBox_Quit (); // if a game has started, ask for confirmation
  67.       else
  68.          is_dialogbox_quit_validated = true; // if game hasn't started yet, quit without question
  69.  
  70.       return (0); // don't let Windows do the default processing on this message
  71.    }
  72.  
  73.    ////////////////////////////////////////////////////////////////////////////////////////////////
  74.    // else is the user closing its session OR the window is being destroyed ?
  75.    else if ((message == WM_QUERYENDSESSION) || (message == WM_ENDSESSION) || (message == WM_DESTROY))
  76.       terminate_everything = true; // suicide (for some reason, PostQuitMessage() is unreliable !?)
  77.  
  78.    /////////////////
  79.    // MENU EVENTS //
  80.    /////////////////
  81.  
  82.    ///////////////
  83.    // menu command
  84.    else if (message == WM_COMMAND)
  85.    {
  86.       // game menu, new game
  87.       if (wParam_loword == MENUID_GAME_NEWGAME)
  88.       {
  89.          if ((the_board.game_state == STATE_PLAYING) && (the_board.move_count > 1))
  90.             DialogBox_Resign (); // if a game is playing, ask to resign first
  91.          else
  92.             DialogBox_NewGame (); // game not started yet, show the new game dialog box
  93.       }
  94.  
  95.       // game menu, setup start position
  96.       else if (wParam_loword == MENUID_GAME_SETUPPOSITION)
  97.       {
  98.          if ((the_board.game_state == STATE_PLAYING) && (the_board.move_count > 1))
  99.             DialogBox_Resign (); // if a game is playing, ask to resign first
  100.          else
  101.          {
  102.             current_move = &the_board.moves[0]; // quick access to start move
  103.             memset (&current_move->slots, 0, sizeof (current_move->slots)); // erase all slots
  104.  
  105.             the_board.game_state = STATE_SETUPPOSITION; // game not started yet, enter board setup mode
  106.             the_board.reevaluate = true; // evaluate board again
  107.  
  108.             // display the "please choose start position" phrase in the middle of the screen
  109.             Scene_SetText (&the_scene.gui.central_text, 50.0f, 50.0f, -1, ALIGN_CENTER, ALIGN_CENTER, ALIGN_CENTER, centermsg_fontindex, RGBA_TO_RGBACOLOR (255, 255, 255, 191),
  110.                            999999.0f, true, LOCALIZE (L"SetupMode"));
  111.             the_scene.gui.partspick_selectedpart = ' '; // no selected part yet
  112.             the_scene.update = true; // and update the 3D scene
  113.          }
  114.       }
  115.  
  116.       // game menu, load game
  117.       else if (wParam_loword == MENUID_GAME_LOAD)
  118.          DialogBox_Load (); // fire up the load dialog box
  119.  
  120.       // game menu, save game
  121.       else if (wParam_loword == MENUID_GAME_SAVE)
  122.       {
  123.          if (save_pathname[0] != 0)
  124.             is_dialogbox_save_validated = true; // if the filename is known, save it directly
  125.          else
  126.             DialogBox_Save (); // else fire up the save dialog box
  127.       }
  128.  
  129.       // game menu, save as
  130.       else if (wParam_loword == MENUID_GAME_SAVEAS)
  131.          DialogBox_Save (); // fire up the save dialog box
  132.  
  133.       // game menu, save position as
  134.       else if (wParam_loword == MENUID_GAME_SAVEPOSITIONAS)
  135.          DialogBox_SavePosition (); // fire up the save position dialog box
  136.  
  137.       // game menu, resign
  138.       else if (wParam_loword == MENUID_GAME_RESIGN)
  139.          DialogBox_Resign (); // if a game has started, ask for confirmation
  140.  
  141.       // game menu, statistics (stats are only available in online mode)
  142.       else if (wParam_loword == MENUID_GAME_STATISTICS)
  143.       {
  144.          local_player = Player_FindByType (PLAYER_HUMAN);
  145.          if (local_player != NULL)
  146.             PlayerCard_FindOrCreate (local_player->name); // display player's own player card
  147.       }
  148.  
  149.       // game menu, options
  150.       else if (wParam_loword == MENUID_GAME_OPTIONS)
  151.          DialogBox_Options (); // fire up the options dialog box
  152.  
  153.       // game menu, quit
  154.       else if (wParam_loword == MENUID_GAME_QUIT)
  155.       {
  156.          if (the_board.game_state == STATE_PLAYING)
  157.             DialogBox_Quit (); // if a game has started, ask for confirmation
  158.          else
  159.             is_dialogbox_quit_validated = true; // if game hasn't started yet, quit without question
  160.       }
  161.  
  162.       // chessboard menu, suggest move
  163.       else if (wParam_loword == MENUID_CHESSBOARD_SUGGESTMOVE)
  164.          the_board.players[current_viewer].wants_hint = true; // remember this player wants a hint
  165.  
  166.       // chessboard menu, cancel last move
  167.       else if (wParam_loword == MENUID_CHESSBOARD_CANCELLASTMOVE)
  168.          the_board.players[current_viewer].wants_cancel = true; // remember this player wants to cancel his last move
  169.  
  170.       // chessboard menu, comment move
  171.       else if (wParam_loword == MENUID_CHESSBOARD_COMMENTMOVE)
  172.          DialogBox_Comment (); // fire up the comment dialog box
  173.  
  174.       // chessboard menu, go to move
  175.       else if (wParam_loword == MENUID_CHESSBOARD_GOTOMOVE)
  176.          DialogBox_GoToMove (); // fire up the go to move dialog box
  177.  
  178.       // chessboard menu, swap sides
  179.       else if (wParam_loword == MENUID_CHESSBOARD_SWAPSIDES)
  180.          the_board.want_playerswap = true; // remember board sides are to be swapped
  181.  
  182.       // chessboard menu, beginning of game
  183.       else if (wParam_loword == MENUID_CHESSBOARD_BEGINNINGOFGAME)
  184.       {
  185.          the_board.viewed_move = 0; // enter view mode and go to the beginning of the game
  186.          Audio_PlaySound (SOUNDTYPE_CLICK); // make a click sound
  187.          the_board.reevaluate = true; // evaluate board again
  188.       }
  189.  
  190.       // chessboard menu, previous move (only if arrow is enabled)
  191.       else if ((wParam_loword == MENUID_CHESSBOARD_PREVIOUSMOVE) && (the_scene.gui.larrow.state != 0))
  192.       {
  193.          the_board.viewed_move--; // enter view mode and go back one move
  194.          Audio_PlaySound (SOUNDTYPE_CLICK); // make a click sound
  195.          the_board.reevaluate = true; // evaluate board again
  196.       }
  197.  
  198.       // chessboard menu, next move (only if arrow is enabled)
  199.       else if ((wParam_loword == MENUID_CHESSBOARD_NEXTMOVE) && (the_scene.gui.rarrow.state != 0))
  200.       {
  201.          the_board.viewed_move++; // enter view mode and go forward one move
  202.          Audio_PlaySound (SOUNDTYPE_CLICK); // make a click sound
  203.          the_board.reevaluate = true; // evaluate board again
  204.       }
  205.  
  206.       // chessboard menu, current state of game
  207.       else if (wParam_loword == MENUID_CHESSBOARD_CURRENTSTATEOFGAME)
  208.       {
  209.          the_board.viewed_move = the_board.move_count - 1; // enter view mode and go to the current state of the game
  210.          Audio_PlaySound (SOUNDTYPE_CLICK); // make a click sound
  211.          the_board.reevaluate = true; // evaluate board again
  212.       }
  213.  
  214.       // chessboard menu, top view
  215.       else if (wParam_loword == MENUID_CHESSBOARD_TOPVIEW)
  216.       {
  217.          // cycle through both players and change their view angles EXCEPT the opponent if he's human
  218.          for (viewer_index = 0; viewer_index < 2; viewer_index++)
  219.             if ((the_board.players[viewer_index].type == PLAYER_COMPUTER)
  220.                 || (the_board.players[viewer_index].type == PLAYER_INTERNET)
  221.                 || (viewer_index == current_viewer))
  222.             {
  223.                the_board.players[viewer_index].view_pitch = 89.0f;
  224.                the_board.players[viewer_index].view_yaw = (current_viewer == COLOR_BLACK ? 90.0f : -90.0f);
  225.                the_board.players[viewer_index].view_distance = 58.0f;
  226.             }
  227.       }
  228.  
  229.       // chessboard menu, default view
  230.       else if (wParam_loword == MENUID_CHESSBOARD_DEFAULTVIEW)
  231.       {
  232.          // cycle through both players and change their view angles EXCEPT the opponent if he's human
  233.          for (viewer_index = 0; viewer_index < 2; viewer_index++)
  234.             if ((the_board.players[viewer_index].type == PLAYER_COMPUTER)
  235.                 || (the_board.players[viewer_index].type == PLAYER_INTERNET)
  236.                 || (viewer_index == current_viewer))
  237.             {
  238.                the_board.players[viewer_index].view_pitch = 55.0f;
  239.                the_board.players[viewer_index].view_yaw = (current_viewer == COLOR_BLACK ? 90.0f : -90.0f);
  240.                the_board.players[viewer_index].view_distance = 70.0f;
  241.             }
  242.       }
  243.  
  244.       // chessboard menu, reset view
  245.       else if (wParam_loword == MENUID_CHESSBOARD_RESETVIEW)
  246.       {
  247.          // cycle through both players and change their view angles EXCEPT the opponent if he's human
  248.          for (viewer_index = 0; viewer_index < 2; viewer_index++)
  249.             if ((the_board.players[viewer_index].type == PLAYER_COMPUTER)
  250.                 || (the_board.players[viewer_index].type == PLAYER_INTERNET)
  251.                 || (viewer_index == current_viewer))
  252.             {
  253.                the_board.players[viewer_index].view_pitch = the_board.players[current_viewer].custom_pitch;
  254.                the_board.players[viewer_index].view_yaw = the_board.players[current_viewer].custom_yaw;
  255.                the_board.players[viewer_index].view_distance = the_board.players[current_viewer].custom_distance;
  256.             }
  257.       }
  258.  
  259.       // chessboard menu, zoom in
  260.       else if (wParam_loword == MENUID_CHESSBOARD_ZOOMIN)
  261.       {
  262.          // scroll up & save this as the new custom distance
  263.          the_board.players[current_viewer].view_distance = max (48.0f, the_board.players[current_viewer].view_distance - 2.0f);
  264.          the_board.players[current_viewer].custom_distance = the_board.players[current_viewer].view_distance;
  265.          the_scene.update = true; // update the 3D scene
  266.       }
  267.  
  268.       // chessboard menu, zoom out
  269.       else if (wParam_loword == MENUID_CHESSBOARD_ZOOMOUT)
  270.       {
  271.          // scroll down & save this as the new custom distance
  272.          the_board.players[current_viewer].view_distance = min (100.0f, the_board.players[current_viewer].view_distance + 2.0f);
  273.          the_board.players[current_viewer].custom_distance = the_board.players[current_viewer].view_distance;
  274.          the_scene.update = true; // update the 3D scene
  275.       }
  276.  
  277.       // chessboard menu, change appearance
  278.       else if (wParam_loword == MENUID_CHESSBOARD_CHANGEAPPEARANCE)
  279.          DialogBox_ChangeAppearance ();
  280.  
  281.       // chessboard menu, display windows desktop
  282.       else if (wParam_loword == MENUID_CHESSBOARD_DISPLAYWINDOWSDESKTOP)
  283.          ShowWindow (hWnd, SW_MINIMIZE);
  284.  
  285.       // internet menu, show online players
  286.       else if (wParam_loword == MENUID_INTERNET_SHOWONLINEPLAYERS)
  287.          Window_Opponents ();
  288.  
  289.       // internet menu, show sought games
  290.       else if (wParam_loword == MENUID_INTERNET_SHOWSOUGHTGAMES)
  291.          Window_Sought ();
  292.  
  293.       // internet menu, seek game
  294.       else if (wParam_loword == MENUID_INTERNET_SEEKGAME)
  295.          DialogBox_SendSeek ();
  296.  
  297.       // internet menu, chatter channels
  298.       else if (wParam_loword == MENUID_INTERNET_CHATTERCHANNELS)
  299.          Window_ChatterChannels ();
  300.  
  301.       // internet menu, enter chat text
  302.       else if (wParam_loword == MENUID_INTERNET_ENTERCHATTEXT)
  303.          PostMessage (hWnd, WM_CHAR, L' ', 0); // emulate a space bar hit
  304.  
  305.       // internet menu, display player card
  306.       else if (wParam_loword == MENUID_INTERNET_DISPLAYPLAYERCARD)
  307.          DialogBox_PlayerInfoName ();
  308.  
  309.       // internet menu, display your card
  310.       else if (wParam_loword == MENUID_INTERNET_DISPLAYYOURCARD)
  311.       {
  312.          local_player = Player_FindByType (PLAYER_HUMAN);
  313.          if (local_player != NULL)
  314.             PlayerCard_FindOrCreate (local_player->name); // display player's own player card
  315.       }
  316.  
  317.       // internet menu, MOTD
  318.       else if (wParam_loword == MENUID_INTERNET_MOTD)
  319.          Window_MOTD ();
  320.  
  321.       // help menu, help
  322.       else if (wParam_loword == MENUID_HELP_HELP)
  323.          ShellExecute (NULL, L"open", L"Chess Giants.html", NULL, NULL, SW_MAXIMIZE); // fire up the help file
  324.  
  325.       // help menu, get chess games
  326.       else if (wParam_loword == MENUID_HELP_GETCHESSGAMES)
  327.          ShellExecute (NULL, L"open", L"http://www.chessgames.com", NULL, NULL, SW_MAXIMIZE); // fire up the browser
  328.  
  329.       // help menu, add/modify visual themes
  330.       else if (wParam_loword == MENUID_HELP_ADDMODIFYVISUALTHEMES)
  331.       {
  332.          swprintf_s (folder_path, WCHAR_SIZEOF (folder_path), L"%s\\themes", app_path);
  333.          ShellExecute (NULL, L"open", folder_path, NULL, NULL, SW_SHOWNORMAL); // fire up Windows Explorer
  334.       }
  335.  
  336.       // help menu, add/modify translations
  337.       else if (wParam_loword == MENUID_HELP_ADDMODIFYTRANSLATIONS)
  338.       {
  339.          swprintf_s (folder_path, WCHAR_SIZEOF (folder_path), L"%s\\data\\languages", app_path);
  340.          ShellExecute (NULL, L"open", folder_path, NULL, NULL, SW_SHOWNORMAL); // fire up Windows Explorer
  341.       }
  342.  
  343.       // help menu, about
  344.       else if (wParam_loword == MENUID_HELP_ABOUT)
  345.          DialogBox_About (); // show the About dialog box
  346.  
  347.       // call the default window message processing function to keep things going
  348.       return (DefWindowProc (hWnd, message, wParam, lParam));
  349.    }
  350.  
  351.    /////////////////////////////////////////////////////
  352.    // ctrl key press or release (while not in animation)
  353.    else if ((message == WM_KEYDOWN) && (wParam == VK_CONTROL) && (animation_endtime + 1.0f < current_time))
  354.    {
  355.       ctrl_pressed = true; // remember the ctrl key is pressed
  356.  
  357.       // call the default window message processing function to keep things going
  358.       return (DefWindowProc (hWnd, message, wParam, lParam));
  359.    }
  360.    else if ((message == WM_KEYUP) && (wParam == VK_CONTROL) && (animation_endtime + 1.0f < current_time))
  361.    {
  362.       ctrl_pressed = false; // remember the ctrl key is released
  363.       rbutton_pushed = false; // remember button is released
  364.       the_scene.update = true; // update the 3D scene
  365.  
  366.       // call the default window message processing function to keep things going
  367.       return (DefWindowProc (hWnd, message, wParam, lParam));
  368.    }
  369.  
  370.    //////////////////
  371.    // MOUSE EVENTS //
  372.    //////////////////
  373.  
  374.    ////////////////////////////////////////////////////////////////////////////////////////////////
  375.    // left mouse button push
  376.    else if (message == WM_LBUTTONDOWN)
  377.    {
  378.       // are we in animation OR are mouse commands NOT allowed ?
  379.       if ((animation_endtime + 1.0f >= current_time) || (command_ignoretime >= current_time))
  380.          return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default message proc to keep things going
  381.  
  382.       // is the ctrl key pressed (emulates a right click) ?
  383.       if (ctrl_pressed)
  384.       {
  385.          prevgui_x = GET_X_LPARAM (lParam); // remember mouse coordinates
  386.          prevgui_y = GET_Y_LPARAM (lParam);
  387.  
  388.          // if we click something, stop moving the table immediately
  389.  
  390.          // cycle through both players and change their view angles EXCEPT the opponent if he's human
  391.          for (viewer_index = 0; viewer_index < 2; viewer_index++)
  392.             if ((the_board.players[viewer_index].type == PLAYER_COMPUTER)
  393.                 || (the_board.players[viewer_index].type == PLAYER_INTERNET)
  394.                 || (viewer_index == current_viewer))
  395.             {
  396.                the_board.players[viewer_index].view_pitch = current_pitch;
  397.                the_board.players[viewer_index].view_yaw = current_yaw;
  398.             }
  399.  
  400.          rbutton_pushed = true; // remember button is clicked
  401.       }
  402.  
  403.       // call the default window message processing function to keep things going
  404.       return (DefWindowProc (hWnd, message, wParam, lParam));
  405.    }
  406.  
  407.    ////////////////////////////////////////////////////////////////////////////////////////////////
  408.    // left mouse button release
  409.    else if (message == WM_LBUTTONUP)
  410.    {
  411.       // are we in animation OR are mouse commands NOT allowed ?
  412.       if ((animation_endtime + 1.0f >= current_time) || (command_ignoretime >= current_time))
  413.          return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default message proc to keep things going
  414.  
  415.       // is the ctrl key pressed (emulates a right click) ?
  416.       if (ctrl_pressed)
  417.       {
  418.          rbutton_pushed = false; // remember button is released
  419.          the_scene.update = true; // update the 3D scene
  420.  
  421.          // call the default window message processing function to keep things going
  422.          return (DefWindowProc (hWnd, message, wParam, lParam));
  423.       }
  424.  
  425.       prevgui_x = GET_X_LPARAM (lParam); // remember mouse coordinates
  426.       prevgui_y = GET_Y_LPARAM (lParam);
  427.  
  428.       // get mouse coordinates
  429.       gui_x = GET_X_LPARAM (lParam);
  430.       gui_y = GET_Y_LPARAM (lParam);
  431.  
  432.       // is the left arrow displayed AND is the mouse hovering it ?
  433.       if ((the_scene.gui.larrow.state != 0) && Button_IsHovered (&the_scene.gui.larrow, gui_x, gui_y))
  434.          SendMessage (hWnd, WM_COMMAND, MENUID_CHESSBOARD_PREVIOUSMOVE, NULL); // send a "previous move" event
  435.  
  436.       // is the right arrow displayed AND is the mouse hovering it ?
  437.       if ((the_scene.gui.rarrow.state != 0) && Button_IsHovered (&the_scene.gui.rarrow, gui_x, gui_y))
  438.          SendMessage (hWnd, WM_COMMAND, MENUID_CHESSBOARD_NEXTMOVE, NULL); // send a "next move" event
  439.  
  440.       // is the chat button displayed AND is the mouse hovering it ?
  441.       if ((the_scene.gui.chatbutton.state != 0) && Button_IsHovered (&the_scene.gui.chatbutton, gui_x, gui_y))
  442.       {
  443.          opposite_player = Player_FindByType (PLAYER_INTERNET); // get a hand on the remote player
  444.  
  445.          // find or create the corresponding interlocutor structure and fire it up
  446.          if (opposite_player != NULL)
  447.             Interlocutor_FindOrCreate (opposite_player->name);
  448.       }
  449.  
  450.       // is the games button displayed AND is the mouse hovering it ?
  451.       if ((the_scene.gui.gamesbutton.state != 0) && Button_IsHovered (&the_scene.gui.gamesbutton, gui_x, gui_y))
  452.          Window_Sought (); // if so, display the sought games window
  453.  
  454.       // is the people button displayed AND is the mouse hovering it ?
  455.       if ((the_scene.gui.peoplebutton.state != 0) && Button_IsHovered (&the_scene.gui.peoplebutton, gui_x, gui_y))
  456.          Window_Opponents (); // if so, display the opponents window
  457.  
  458.       // is the parts selection line displayed AND is the mouse anywhere near it ?
  459.       if (the_scene.gui.is_partspick_displayed && Render_IsMouseInBox (gui_x, gui_y, 0.0f, 0.0f, 100.0f, 11.0f))
  460.       {
  461.          // for each selectable part, if the mouse is on it, mark it as selected
  462.          for (part_index = 0; part_index < 13; part_index++)
  463.             if (Render_IsMouseInBox (gui_x, gui_y, part_index * (100.0f / 13.0f), 0, 100.0f / 13.0f, 11.0f))
  464.             {
  465.                the_scene.gui.partspick_selectedpart = selectable_parts[part_index]; // mark it as selected
  466.                Audio_PlaySound (SOUNDTYPE_CLICK); // make a click sound
  467.                break; // no need to search further if one selection was found
  468.             }
  469.       }
  470.  
  471.       // get current player and see if we're online
  472.       current_player = Player_GetCurrent ();
  473.       remote_player = Player_FindByType (PLAYER_INTERNET);
  474.  
  475.       // if we are not allowed to select anything, don't even try
  476.       if ((current_player->type != PLAYER_HUMAN) || (the_board.viewed_move != the_board.move_count - 1)
  477.           || ((remote_player != NULL) && !remote_player->is_in_game))
  478.       {
  479.          the_scene.update = true; // update the 3D scene
  480.  
  481.          // call the default window message processing function to keep things going
  482.          return (DefWindowProc (hWnd, message, wParam, lParam));
  483.       }
  484.  
  485.       // it's a single click. The user clicked on a square.
  486.  
  487.       // figure out the coordinates on table
  488.       Render_MouseToFloor (gui_x, gui_y, &board_x, &board_y);
  489.  
  490.       // translate them to board coordinates
  491.       the_board.hovered_position[0] = (int) floor ((20.0f - board_y) / 5.0f);
  492.       the_board.hovered_position[1] = 7 - (int) floor ((20.0f - board_x) / 5.0f);
  493.       highlight_endtime = 0; // stop highlighting anything
  494.  
  495.       // if it's outside the table, end the job
  496.       if (!IS_VALID_POSITION (the_board.hovered_position))
  497.       {
  498.          the_scene.update = true; // update the 3D scene
  499.  
  500.          // call the default window message processing function to keep things going
  501.          return (DefWindowProc (hWnd, message, wParam, lParam));
  502.       }
  503.  
  504.       // we clicked a valid slot on the table
  505.  
  506.       current_move = &the_board.moves[the_board.viewed_move]; // quick access to current move
  507.  
  508.       ///////////////////////////////////
  509.       // are we in parts placement mode ?
  510.       if (the_board.game_state == STATE_SETUPPOSITION)
  511.       {
  512.          // quick access to line and column
  513.          line = the_board.hovered_position[0];
  514.          column = the_board.hovered_position[1];
  515.  
  516.          // figure out the color of the part we are placing
  517.          if (the_scene.gui.partspick_selectedpart == tolower (the_scene.gui.partspick_selectedpart))
  518.             part_color = COLOR_BLACK; // black color
  519.          else
  520.             part_color = COLOR_WHITE; // white color
  521.  
  522.          // are we erasing a king ? if so, replace it at its default location
  523.          if ((current_move->slots[line][column].part == PART_KING) && (current_move->slots[line][column].color == COLOR_BLACK)
  524.              && (the_scene.gui.partspick_selectedpart != 'k'))
  525.          {
  526.             // is it ALREADY the king's default location ? if so, give up
  527.             if ((line == 7) && (column == 4))
  528.             {
  529.                Audio_PlaySound (SOUNDTYPE_ILLEGALMOVE); // play the "illegal move" sound
  530.                return (DefWindowProc (hWnd, message, wParam, lParam)); // call the default window message proc
  531.             }
  532.  
  533.             Move_SetSlot (current_move, 7, 4, COLOR_BLACK, PART_KING); // replace black king
  534.          }
  535.          else if ((current_move->slots[line][column].part == PART_KING) && (current_move->slots[line][column].color == COLOR_WHITE)
  536.                   && (the_scene.gui.partspick_selectedpart != 'K'))
  537.          {
  538.             // is it ALREADY the king's default location ? if so, give up
  539.             if ((line == 0) && (column == 4))
  540.             {
  541.                Audio_PlaySound (SOUNDTYPE_ILLEGALMOVE); // play the "illegal move" sound
  542.                return (DefWindowProc (hWnd, message, wParam, lParam)); // call the default window message proc
  543.             }
  544.  
  545.             Move_SetSlot (current_move, 0, 4, COLOR_WHITE, PART_KING); // replace white king
  546.          }
  547.  
  548.          // place the selected part on the board
  549.          if (tolower (the_scene.gui.partspick_selectedpart) == 'p')
  550.             Move_SetSlot (current_move, line, column, part_color, PART_PAWN); // pawn
  551.          else if (tolower (the_scene.gui.partspick_selectedpart) == 'r')
  552.             Move_SetSlot (current_move, line, column, part_color, PART_ROOK); // rook
  553.          else if (tolower (the_scene.gui.partspick_selectedpart) == 'n')
  554.             Move_SetSlot (current_move, line, column, part_color, PART_KNIGHT); // knight
  555.          else if (tolower (the_scene.gui.partspick_selectedpart) == 'b')
  556.             Move_SetSlot (current_move, line, column, part_color, PART_BISHOP); // bishop
  557.          else if (tolower (the_scene.gui.partspick_selectedpart) == 'q')
  558.             Move_SetSlot (current_move, line, column, part_color, PART_QUEEN); // queen
  559.          else if (tolower (the_scene.gui.partspick_selectedpart) == 'k')
  560.          {
  561.             // parse the board for other kings of the same color and erase them
  562.             for (index_line = 0; index_line < 8; index_line++)
  563.                for (index_column = 0; index_column < 8; index_column++)
  564.                   if ((current_move->slots[index_line][index_column].color == part_color)
  565.                       && (current_move->slots[index_line][index_column].part == PART_KING))
  566.                      memset (&current_move->slots[index_line][index_column], 0, sizeof (boardslot_t)); // erase this slot
  567.  
  568.             Move_SetSlot (current_move, line, column, part_color, PART_KING); // king
  569.          }
  570.          else
  571.             Move_SetSlot (current_move, line, column, 0, PART_NONE); // no part
  572.  
  573.          // figure out which sound to play
  574.          if (the_scene.gui.partspick_selectedpart != ' ')
  575.             Audio_PlaySound (SOUNDTYPE_MOVE); // play a move sound when placing a part
  576.  
  577.          the_scene.update = true; // update the 3D scene
  578.  
  579.          // call the default window message processing function to keep things going
  580.          return (DefWindowProc (hWnd, message, wParam, lParam));
  581.       }
  582.       // end of parts placement mode
  583.       //////////////////////////////
  584.  
  585.       // does a selection NOT exist yet ?
  586.       if (!IS_VALID_POSITION (the_board.selected_position))
  587.       {
  588.          // is there a selectable part at this location ?
  589.          if ((current_move->slots[the_board.hovered_position[0]][the_board.hovered_position[1]].part != PART_NONE)
  590.             && (current_move->slots[the_board.hovered_position[0]][the_board.hovered_position[1]].color == Board_ColorToMove (&the_board)))
  591.          {
  592.             // mark the selected position as selected (and remember it)
  593.             the_board.selected_position[0] = the_board.hovered_position[0];
  594.             the_board.selected_position[1] = the_board.hovered_position[1];
  595.          }
  596.  
  597.          the_scene.update = true; // update the 3D scene
  598.  
  599.          // call the default window message processing function to keep things going
  600.          return (DefWindowProc (hWnd, message, wParam, lParam));
  601.       }
  602.  
  603.       // a selection exists already
  604.  
  605.       // is it the slot that was previously selected ? (i.e, user wants to "unselect" it)
  606.       if ((the_board.hovered_position[0] == the_board.selected_position[0]) && (the_board.hovered_position[1] == the_board.selected_position[1]))
  607.       {
  608.          // forget the selected position
  609.          the_board.selected_position[0] = -1;
  610.          the_board.selected_position[1] = -1;
  611.  
  612.          the_scene.update = true; // update the 3D scene
  613.  
  614.          // call the default window message processing function to keep things going
  615.          return (DefWindowProc (hWnd, message, wParam, lParam));
  616.       }
  617.  
  618.       // else is it another part of the same color ? (i.e, user wants to change the part he selected)
  619.       else if ((current_move->slots[the_board.hovered_position[0]][the_board.hovered_position[1]].part != PART_NONE)
  620.                && (current_move->slots[the_board.hovered_position[0]][the_board.hovered_position[1]].color == Board_ColorToMove (&the_board)))
  621.       {
  622.          // mark the selected position as selected (and remember it)
  623.          the_board.selected_position[0] = the_board.hovered_position[0];
  624.          the_board.selected_position[1] = the_board.hovered_position[1];
  625.  
  626.          the_scene.update = true; // update the 3D scene
  627.  
  628.          // call the default window message processing function to keep things going
  629.          return (DefWindowProc (hWnd, message, wParam, lParam));
  630.       }
  631.  
  632.       // else is it a possible move ?
  633.       else if ((current_move->slots[the_board.hovered_position[0]][the_board.hovered_position[1]].flags & FLAG_POSSIBLEMOVE)
  634.                || (current_move->slots[the_board.hovered_position[0]][the_board.hovered_position[1]].flags & FLAG_TAKEABLE))
  635.       {
  636.          // are we in check after the move ? (FIXME: call EP version of this func for en passant moves)
  637.          if (Move_IsColorInCheckAfterTestMove (current_move, the_board.selected_position[0], the_board.selected_position[1], the_board.hovered_position[0], the_board.hovered_position[1], Board_ColorToMove (&the_board)))
  638.          {
  639.             Audio_PlaySound (SOUNDTYPE_ILLEGALMOVE); // play the "illegal move" sound
  640.  
  641.             the_scene.update = true; // update the 3D scene
  642.  
  643.             // call the default window message processing function to keep things going
  644.             return (DefWindowProc (hWnd, message, wParam, lParam));
  645.          }
  646.  
  647.          ////////////////////
  648.          // movement is valid
  649.  
  650.          // do it
  651.          Board_AppendMove (&the_board, the_board.selected_position[0], the_board.selected_position[1], the_board.hovered_position[0], the_board.hovered_position[1], PART_NONE, NULL);
  652.          current_move = &the_board.moves[the_board.move_count - 1]; // update current move pointer
  653.  
  654.          // are we in internet mode ?
  655.          if (remote_player != NULL)
  656.             the_board.reevaluate = false; // if so, don't reevaluate the board yet, let the server do it
  657.  
  658.          // was it a pawn being promoted ? if so, display the dialog box and wait for the reply
  659.          if ((current_move->slots[the_board.hovered_position[0]][the_board.hovered_position[1]].part == PART_PAWN)
  660.              && ((the_board.hovered_position[0] == 0) || (the_board.hovered_position[0] == 7)))
  661.             DialogBox_PawnPromotion (); // display the pawn promotion dialog box
  662.  
  663.          // else it was a normal move
  664.          else
  665.          {
  666.             Board_SetSelectedAndHovered (&the_board, -1, -1, -1, -1); // forget the hovered and selected positions
  667.             the_board.has_playerchanged = true; // and switch players
  668.          }
  669.  
  670.          the_scene.update = true; // update the 3D scene
  671.          animation_endtime = current_time + ANIMATION_DURATION; // play animation now
  672.          sound_playtime = current_time + ANIMATION_DURATION - 0.1f; // play sound near the end of animation
  673.  
  674.          // call the default window message processing function to keep things going
  675.          return (DefWindowProc (hWnd, message, wParam, lParam));
  676.       }
  677.  
  678.       // else it's another location
  679.  
  680.       Audio_PlaySound (SOUNDTYPE_ILLEGALMOVE); // play the "illegal move" sound
  681.  
  682.       the_scene.update = true; // update the 3D scene
  683.  
  684.       // call the default window message processing function to keep things going
  685.       return (DefWindowProc (hWnd, message, wParam, lParam));
  686.    }
  687.  
  688.    ////////////////////////////////////////////////////////////////////////////////////////////////
  689.    // right mouse button push
  690.    else if (message == WM_RBUTTONDOWN)
  691.    {
  692.       // are we in animation OR are mouse commands NOT allowed ?
  693.       if ((animation_endtime + 1.0f >= current_time) || (command_ignoretime >= current_time))
  694.          return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default message proc to keep things going
  695.  
  696.       prevgui_x = GET_X_LPARAM (lParam); // remember mouse coordinates
  697.       prevgui_y = GET_Y_LPARAM (lParam);
  698.  
  699.       // if we click something, stop moving the table immediately
  700.  
  701.       // cycle through both players and change their view angles EXCEPT the opponent if he's human
  702.       for (viewer_index = 0; viewer_index < 2; viewer_index++)
  703.          if ((the_board.players[viewer_index].type == PLAYER_COMPUTER)
  704.                || (the_board.players[viewer_index].type == PLAYER_INTERNET)
  705.                || (viewer_index == current_viewer))
  706.          {
  707.             the_board.players[viewer_index].view_pitch = current_pitch;
  708.             the_board.players[viewer_index].view_yaw = current_yaw;
  709.          }
  710.  
  711.       rbutton_pushed = true; // remember button is clicked
  712.  
  713.       // call the default window message processing function to keep things going
  714.       return (DefWindowProc (hWnd, message, wParam, lParam));
  715.    }
  716.  
  717.    ////////////////////////////////////////////////////////////////////////////////////////////////
  718.    // right mouse button release
  719.    else if (message == WM_RBUTTONUP)
  720.    {
  721.       // are we in animation OR are mouse commands NOT allowed ?
  722.       if ((animation_endtime + 1.0f >= current_time) || (command_ignoretime >= current_time))
  723.          return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default message proc to keep things going
  724.  
  725.       rbutton_pushed = false; // remember button is released
  726.       the_scene.update = true; // update the 3D scene
  727.  
  728.       // call the default window message processing function to keep things going
  729.       return (DefWindowProc (hWnd, message, wParam, lParam));
  730.    }
  731.  
  732.    ////////////////////////////////////////////////////////////////////////////////////////////////
  733.    // left mouse button DOUBLE-click
  734.    else if (message == WM_LBUTTONDBLCLK)
  735.    {
  736.       // are we in animation OR are mouse commands NOT allowed ?
  737.       if ((animation_endtime + 1.0f >= current_time) || (command_ignoretime >= current_time))
  738.          return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default message proc to keep things going
  739.  
  740.       // get mouse coordinates
  741.       gui_x = GET_X_LPARAM (lParam);
  742.       gui_y = GET_Y_LPARAM (lParam);
  743.  
  744.       // are we in game and did we click the move comments area ?
  745.       if ((the_board.game_state >= STATE_PLAYING) && (the_board.viewed_move > 0) && Render_IsMouseInBox (gui_x, gui_y, 10.0f, 0.0f, 80.0f, 10.0f))
  746.       {
  747.          DialogBox_Comment (); // fire up the comment dialog box
  748.          return (DefWindowProc (hWnd, message, wParam, lParam)); // call the default window message processing function to keep things going
  749.       }
  750.  
  751.       // call the default window message processing function to keep things going
  752.       return (DefWindowProc (hWnd, message, wParam, lParam));
  753.    }
  754.  
  755.    ////////////////////////////////////////////////////////////////////////////////////////////////
  756.    // mouse move (while not in animation)
  757.    else if (message == WM_MOUSEMOVE)
  758.    {
  759.       // are we in animation OR are mouse commands NOT allowed ?
  760.       if ((animation_endtime + 1.0f >= current_time) || (command_ignoretime >= current_time))
  761.          return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default message proc to keep things going
  762.  
  763.       // get mouse coordinates
  764.       gui_x = GET_X_LPARAM (lParam);
  765.       gui_y = GET_Y_LPARAM (lParam);
  766.  
  767.       // handle button update status
  768.       the_scene.update |= Button_UpdateHoverState (&the_scene.gui.larrow, gui_x, gui_y);
  769.       the_scene.update |= Button_UpdateHoverState (&the_scene.gui.rarrow, gui_x, gui_y);
  770.       the_scene.update |= Button_UpdateHoverState (&the_scene.gui.chatbutton, gui_x, gui_y);
  771.       the_scene.update |= Button_UpdateHoverState (&the_scene.gui.gamesbutton, gui_x, gui_y);
  772.       the_scene.update |= Button_UpdateHoverState (&the_scene.gui.peoplebutton, gui_x, gui_y);
  773.  
  774.       // see if we're online
  775.       remote_player = Player_FindByType (PLAYER_INTERNET);
  776.  
  777.       // are we online AND do we NOT have the right to select anything ?
  778.       if ((remote_player != NULL) && !remote_player->is_in_game)
  779.          return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default window message processing function to keep things going
  780.  
  781.       // is the parts selection line displayed AND is the mouse anywhere in it ?
  782.       if (the_scene.gui.is_partspick_displayed && Render_IsMouseInBox (gui_x, gui_y, 0.0f, 0.0f, 100.0f, 11.0f))
  783.       {
  784.          // for each selectable part...
  785.          for (part_index = 0; part_index < 13; part_index++)
  786.          {
  787.             // is the mouse hovering it ?
  788.             if (Render_IsMouseInBox (gui_x, gui_y, part_index * (100.0f / 13.0f), 0, 100.0f / 13.0f, 11.0f))
  789.             {
  790.                // was it NOT hovered before ?
  791.                if (the_scene.gui.partspick_hoveredpart != selectable_parts[part_index])
  792.                {
  793.                   the_scene.gui.partspick_hoveredpart = selectable_parts[part_index]; // mark it as hovered
  794.                   the_scene.update = true; // update the scene
  795.                }
  796.             }
  797.  
  798.             // else was it hovered before ?
  799.             else if (the_scene.gui.partspick_hoveredpart == selectable_parts[part_index])
  800.             {
  801.                the_scene.gui.partspick_hoveredpart = 0; // clear the hovered part
  802.                the_scene.update = true; // update the scene
  803.             }
  804.          }
  805.       }
  806.       else
  807.          the_scene.gui.partspick_hoveredpart = 0; // clear the hovered part
  808.  
  809.       // get current and opposite players
  810.       current_player = Player_GetCurrent ();
  811.       opposite_player = Player_GetOpposite ();
  812.  
  813.       // if right button was clicked, compute new pitch and yaw
  814.       if (rbutton_pushed)
  815.       {
  816.          // cycle through both players and change their view angles EXCEPT the opponent if he's human
  817.          for (viewer_index = 0; viewer_index < 2; viewer_index++)
  818.             if ((the_board.players[viewer_index].type == PLAYER_COMPUTER)
  819.                 || (the_board.players[viewer_index].type == PLAYER_INTERNET)
  820.                 || (viewer_index == current_viewer))
  821.             {
  822.                the_board.players[viewer_index].view_pitch += (gui_y - prevgui_y) * 0.3f;
  823.                if (the_board.players[viewer_index].view_pitch < 10.0f)
  824.                   the_board.players[viewer_index].view_pitch = 10.0f; // wrap angles around so that they
  825.                if (the_board.players[viewer_index].view_pitch > 89.0f)
  826.                   the_board.players[viewer_index].view_pitch = 89.0f; // stay in the [10, 89] bounds
  827.  
  828.                the_board.players[viewer_index].view_yaw += (gui_x - prevgui_x) * 0.3f;
  829.                the_board.players[viewer_index].view_yaw = WrapAngle (the_board.players[viewer_index].view_yaw);
  830.  
  831.                // save these as the new custom angles
  832.                the_board.players[viewer_index].custom_pitch = the_board.players[viewer_index].view_pitch;
  833.                the_board.players[viewer_index].custom_yaw = the_board.players[viewer_index].view_yaw;
  834.  
  835.                // when moving the table around, jump to ideal angles immediately
  836.                current_pitch = the_board.players[viewer_index].view_pitch;
  837.                current_yaw = the_board.players[viewer_index].view_yaw;
  838.             }
  839.  
  840.          the_scene.update = true; // button was clicked, update the 3D scene
  841.       }
  842.  
  843.       // else it's just the mouse wandering around ; have we the right to select something ?
  844.       else if ((the_board.viewed_move == the_board.move_count - 1) && (current_player->type == PLAYER_HUMAN) && (highlight_endtime < current_time))
  845.       {
  846.          // save the old positions
  847.          prev_hovered_position[0] = the_board.hovered_position[0];
  848.          prev_hovered_position[1] = the_board.hovered_position[1];
  849.  
  850.          // figure out the coordinates on table
  851.          Render_MouseToFloor (gui_x, gui_y, &board_x, &board_y);
  852.  
  853.          // translate them to board coordinates
  854.          the_board.hovered_position[0] = (int) floor ((20.0f - board_y) / 5.0f);
  855.          the_board.hovered_position[1] = 7 - (int) floor ((20.0f - board_x) / 5.0f);
  856.  
  857.          // do they differ from last time ?
  858.          if ((the_board.hovered_position[0] != prev_hovered_position[0]) || (the_board.hovered_position[1] != prev_hovered_position[1]))
  859.             the_scene.update = true; // if so, update scene
  860.       }
  861.  
  862.       // has the user the right to leave a command AND is there no comment yet ?
  863.       if ((the_board.game_state >= STATE_PLAYING) && (the_board.viewed_move > 0)
  864.           && ((the_board.moves[the_board.viewed_move].comment == NULL) || (the_board.moves[the_board.viewed_move].comment[0] == 0)))
  865.       {
  866.          // is the mouse above the comments zone ? if so, display a dimmed hint text
  867.          if (Render_IsMouseInBox (gui_x, gui_y, 30.0f, 0.0f, 40.0f, 10.0f))
  868.          {
  869.             if (!the_scene.gui.comment_text.is_displayed)
  870.             {
  871.                Scene_SetText (&the_scene.gui.comment_text, 50.0f, 5.0f, 40.0f, ALIGN_CENTER, ALIGN_CENTER, ALIGN_LEFT, chat_fontindex, RGBA_TO_RGBACOLOR (255, 255, 255, 127), 999999.0f, false, LOCALIZE (L"DoubleClickToEnterComment"));
  872.                the_scene.update = true; // and update the scene
  873.             }
  874.          }
  875.          else
  876.          {
  877.             if (the_scene.gui.comment_text.is_displayed)
  878.             {
  879.                the_scene.gui.comment_text.is_displayed = false; // if not, erase the hint text
  880.                the_scene.update = true; // and update the scene
  881.             }
  882.          }
  883.       }
  884.  
  885.       // remember these coordinates for next time
  886.       prevgui_x = gui_x;
  887.       prevgui_y = gui_y;
  888.  
  889.       // call the default window message processing function to keep things going
  890.       return (DefWindowProc (hWnd, message, wParam, lParam));
  891.    }
  892.  
  893.    ////////////////////////////////////////////////////////////////////////////////////////////////
  894.    // mouse scroll
  895.    else if (message == WM_MOUSEWHEEL)
  896.    {
  897.       // are we in animation OR are mouse commands NOT allowed ?
  898.       if ((animation_endtime + 1.0f >= current_time) || (command_ignoretime >= current_time))
  899.          return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default message proc to keep things going
  900.  
  901.       // see if we're online
  902.       remote_player = Player_FindByType (PLAYER_INTERNET);
  903.  
  904.       // are we online AND do we NOT have the right to select anything ?
  905.       if ((remote_player != NULL) && !remote_player->is_in_game)
  906.          return (DefWindowProc (hWnd, message, wParam, lParam)); // if so, call the default window message processing function to keep things going
  907.  
  908.       // scroll up / scroll down ?
  909.       if (GET_WHEEL_DELTA_WPARAM (wParam) > 0)
  910.          the_board.players[current_viewer].view_distance = max (48.0f, the_board.players[current_viewer].view_distance - 2.0f);
  911.       else if (GET_WHEEL_DELTA_WPARAM (wParam) < 0)
  912.          the_board.players[current_viewer].view_distance = min (100.0f, the_board.players[current_viewer].view_distance + 2.0f);
  913.  
  914.       // save this as the new custom distance
  915.       the_board.players[current_viewer].custom_distance = the_board.players[current_viewer].view_distance;
  916.  
  917.       // when moving the table around, jump to ideal angles immediately
  918.       current_distance = the_board.players[current_viewer].view_distance;
  919.  
  920.       the_scene.update = true; // update the 3D scene
  921.  
  922.       // call the default window message processing function to keep things going
  923.       return (DefWindowProc (hWnd, message, wParam, lParam));
  924.    }
  925.  
  926.    ////////////////////////////////////////////////////////////////////////////////////////////////
  927.    // keyboard release
  928.    else if (message == WM_CHAR)
  929.    {
  930.       // are we in position setup mode ?
  931.       if (the_board.game_state == STATE_SETUPPOSITION)
  932.       {
  933.          // is it the enter key ? if so, exit setup mode and start playing
  934.          if (wParam == L'\r')
  935.          {
  936.             current_move = &the_board.moves[the_board.viewed_move]; // quick access to current move
  937.  
  938.             // (in)validate the castling positions
  939.             if ((current_move->slots[0][0].color == COLOR_WHITE) && (current_move->slots[0][0].part == PART_ROOK)
  940.                 && (current_move->slots[0][4].color == COLOR_WHITE) && (current_move->slots[0][4].part == PART_KING))
  941.                current_move->sides[COLOR_WHITE].longcastle_allowed = true; // white castling queenside allowed
  942.             else
  943.                current_move->sides[COLOR_WHITE].longcastle_allowed = false; // white castling queenside no longer possible
  944.             if ((current_move->slots[0][7].color == COLOR_WHITE) && (current_move->slots[0][7].part == PART_ROOK)
  945.                 && (current_move->slots[0][4].color == COLOR_WHITE) && (current_move->slots[0][4].part == PART_KING))
  946.                current_move->sides[COLOR_WHITE].shortcastle_allowed = true; // white castling kingside allowed
  947.             else
  948.                current_move->sides[COLOR_WHITE].shortcastle_allowed = false; // white castling kingside no longer possible
  949.             if ((current_move->slots[7][0].color == COLOR_BLACK) && (current_move->slots[7][0].part == PART_ROOK)
  950.                 && (current_move->slots[7][4].color == COLOR_BLACK) && (current_move->slots[7][4].part == PART_KING))
  951.                current_move->sides[COLOR_BLACK].longcastle_allowed = true; // white castling queenside allowed
  952.             else
  953.                current_move->sides[COLOR_BLACK].longcastle_allowed = false; // white castling queenside no longer possible
  954.             if ((current_move->slots[7][7].color == COLOR_BLACK) && (current_move->slots[7][7].part == PART_ROOK)
  955.                 && (current_move->slots[7][4].color == COLOR_BLACK) && (current_move->slots[7][4].part == PART_KING))
  956.                current_move->sides[COLOR_BLACK].shortcastle_allowed = true; // white castling kingside allowed
  957.             else
  958.                current_move->sides[COLOR_BLACK].shortcastle_allowed = false; // white castling kingside no longer possible
  959.  
  960.             current_move->color = COLOR_BLACK; // so that game starts with white
  961.  
  962.             // validate this board in Forsyth-Edwards Notation and reset it
  963.             Move_DescribeInFEN (current_move);
  964.             wcscpy_s (fen_string, WCHAR_SIZEOF (fen_string), current_move->fen_string); // have a copy of fen string
  965.             Board_Reset (&the_board, fen_string);
  966.  
  967.             the_board.game_state = STATE_PLAYING; // start the game now
  968.             the_board.reevaluate = true; // evaluate board again
  969.  
  970.             the_scene.gui.central_text.disappear_time = current_time + 1.0f; // fade out help text now (FIXME: ugly)
  971.             the_scene.update = true; // update the 3D scene
  972.  
  973.             // call the default window message processing function to keep things going
  974.             return (DefWindowProc (hWnd, message, wParam, lParam));
  975.          }
  976.       }
  977.  
  978.       // else are we in internet mode ?
  979.       else if (Player_FindByType (PLAYER_INTERNET) != NULL)
  980.       {
  981.          entered_ccreply = &the_scene.gui.entered_ccreply; // quick access to entered ccreply
  982.          local_player = Player_FindByType (PLAYER_HUMAN); // quick access to local player
  983.          if (local_player == NULL)
  984.             return (DefWindowProc (hWnd, message, wParam, lParam)); // theoretically impossible condition, but better be sure
  985.          if (selected_chatterchannel == NULL)
  986.             return (DefWindowProc (hWnd, message, wParam, lParam)); // theoretically impossible condition, but better be sure
  987.  
  988.          // are we NOT entering text yet ?
  989.          if (!the_scene.gui.is_entering_text)
  990.          {
  991.             // is it the space bar ?
  992.             if (wParam == L' ')
  993.             {
  994.                the_scene.gui.is_entering_text = true; // remember we are entering text
  995.  
  996.                // reset the entered text buffer
  997.                entered_ccreply->text = (wchar_t *) SAFE_malloc (1, sizeof (wchar_t), false);
  998.                entered_ccreply->text[0] = 0; // only the null terminator will fit
  999.                entered_ccreply->text_length = 0; // and set its length to zero
  1000.             }
  1001.          }
  1002.  
  1003.          // else we are currently in text entering mode
  1004.          else
  1005.          {
  1006.             // is the typed character a printable character ?
  1007.             if (iswprint (wParam))
  1008.             {
  1009.                // if so, reallocate space in the typed buffer (include null terminator)
  1010.                entered_ccreply->text = (wchar_t *) SAFE_realloc (entered_ccreply->text, entered_ccreply->text_length + 1, entered_ccreply->text_length + 1 + 1, sizeof (wchar_t), false);
  1011.                swprintf_s (&entered_ccreply->text[entered_ccreply->text_length], 1 + 1, L"%c", wParam); // append character
  1012.                entered_ccreply->text_length++; // buffer holds now one character more
  1013.             }
  1014.  
  1015.             // else is the typed character a backspace AND is there text to erase ?
  1016.             else if ((wParam == 0x08) && (entered_ccreply->text_length > 0))
  1017.             {
  1018.                // if so, reallocate space in the typed buffer (include null terminator)
  1019.                entered_ccreply->text = (wchar_t *) SAFE_realloc (entered_ccreply->text, entered_ccreply->text_length + 1, entered_ccreply->text_length + 1 - 1, sizeof (wchar_t), false);
  1020.                entered_ccreply->text[entered_ccreply->text_length - 1] = 0; // backspace, delete one character
  1021.                entered_ccreply->text_length--; // buffer holds now one character less
  1022.             }
  1023.  
  1024.             // else is the typed character the escape key ?
  1025.             else if (wParam == 0x1b)
  1026.             {
  1027.                SAFE_free ((void **) &entered_ccreply->text); // reset the entered text buffer
  1028.                entered_ccreply->text_length = 0; // and set its length to zero
  1029.                the_scene.gui.is_entering_text = false; // and exit from the text entering mode
  1030.             }
  1031.  
  1032.             // else is the typed character the enter key ?
  1033.             else if (wParam == 0x0d)
  1034.                the_scene.gui.is_entering_text = false; // enter, exit from the text entering mode (this will validate our reply)
  1035.  
  1036.             // else it's an illegal character
  1037.             else
  1038.                Audio_PlaySound (SOUNDTYPE_ILLEGALMOVE); // illegal character, beep the illegal move sound
  1039.          }
  1040.  
  1041.          the_scene.update = true; // update the 3D scene
  1042.  
  1043.          // call the default window message processing function to keep things going
  1044.          return (DefWindowProc (hWnd, message, wParam, lParam));
  1045.       }
  1046.    }
  1047.  
  1048.    ////////////////////////////////////////////////////////////////////////////////////////////////
  1049.    // mouse move outside the window
  1050.    else if (message == WM_NCMOUSEMOVE)
  1051.    {
  1052.       rbutton_pushed = false; // remember right button is released
  1053.  
  1054.       // call the default window message processing function to keep things going
  1055.       return (DefWindowProc (hWnd, message, wParam, lParam));
  1056.    }
  1057.  
  1058.    ////////////////////////////////////////////////////////////////////////////////////////////////
  1059.    // window repaint
  1060.    else if (message == WM_PAINT)
  1061.    {
  1062.       the_scene.update = true; // update the 3D scene
  1063.  
  1064.       // call the default window message processing function to keep things going
  1065.       return (DefWindowProc (hWnd, message, wParam, lParam));
  1066.    }
  1067.  
  1068.    // call the default window message processing function to keep things going
  1069.    return (DefWindowProc (hWnd, message, wParam, lParam));
  1070. }
  1071.  
  1072.  
  1073. static bool Button_IsHovered (guibutton_t *button, int gui_x, int gui_y)
  1074. {
  1075.    // handy wrapper that returns whether a particular GUI button is hovered when the mouse is at the given coordinates
  1076.  
  1077.    return (Render_IsMouseInBox (gui_x, gui_y, button->left, button->top, button->width, button->height));
  1078. }
  1079.  
  1080.  
  1081. static bool Button_UpdateHoverState (guibutton_t *button, int gui_x, int gui_y)
  1082. {
  1083.    // this function updates the hover state of a GUI button, and returns TRUE if the
  1084.    // scene needs to be updated, FALSE otherwise.
  1085.  
  1086.    // is the button displayed ?
  1087.    if (button->state != 0)
  1088.    {
  1089.       // is the mouse hovering it ?
  1090.       if (Render_IsMouseInBox (gui_x, gui_y, button->left, button->top, button->width, button->height))
  1091.       {
  1092.          // was it NOT hovered before ?
  1093.          if (button->state != 2)
  1094.          {
  1095.             button->state = 2; // mark it as hovered
  1096.             return (true); // return TRUE so as to update the scene
  1097.          }
  1098.       }
  1099.  
  1100.       // else was it hovered before ?
  1101.       else if (button->state == 2)
  1102.       {
  1103.          button->state = 1; // else mark it as not hovered
  1104.          return (true); // return TRUE so as to update the scene
  1105.       }
  1106.    }
  1107.  
  1108.    return (false); // no need to update the scene
  1109. }
  1110.