Subversion Repositories Games.Chess Giants

Rev

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

  1. // dialog_newgame.cpp
  2.  
  3. #include "../common.h"
  4.  
  5.  
  6. // dialog template
  7. #define THIS_DIALOG DIALOG_NEWGAME
  8.  
  9.  
  10. // game type definitons
  11. #define GAMETYPE_ONLINE 1
  12. #define GAMETYPE_COMPUTER 2
  13. #define GAMETYPE_TWOPLAYERS 3
  14.  
  15.  
  16. // global variables used in this module only
  17. static int newgame_type = 0;
  18. static char ascii_hostname[256];
  19.  
  20.  
  21. // prototypes of local functions
  22. static void StartThread_ThisDialog (void *thread_parms);
  23. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
  24.  
  25.  
  26. void DialogBox_NewGame (void)
  27. {
  28.    // helper function to fire up the modeless dialog box
  29.  
  30.    // HACK to prevent the player to click and block the view angles while the slide-in is not finished
  31.    command_ignoretime = current_time + 2.0f; // allow 2 seconds
  32.  
  33.    _beginthread (StartThread_ThisDialog, 0, NULL); // so fire up a new one
  34.  
  35.    the_scene.gui.want_spinwheel = true; // start spinning wheel
  36.  
  37.    return; // return as soon as the thread is fired up
  38. }
  39.  
  40.  
  41. void DialogBox_NewGame_Validated (void)
  42. {
  43.    // callback function called by the main game thread when the dialog box is validated
  44.  
  45.    // remember this callback is no longer to be called
  46.    is_dialogbox_newgame_validated = false;
  47.  
  48.    Scene_Shutdown (&the_scene); // release scene
  49.    Board_Shutdown (&the_board); // release chess game
  50.  
  51.    if (newgame_type == GAMETYPE_ONLINE)
  52.    {
  53.       Board_Init (&the_board, PLAYER_HUMAN, PLAYER_INTERNET, FENSTARTUP_STANDARDCHESS); // we want an online opponent
  54.       the_board.game_state = STATE_UNKNOWN; // game does NOT start immediately
  55.    }
  56.    else if (newgame_type == GAMETYPE_COMPUTER)
  57.    {
  58.       Board_Init (&the_board, PLAYER_HUMAN, PLAYER_COMPUTER, FENSTARTUP_STANDARDCHESS); // we want a computer opponent
  59.       the_board.game_state = STATE_PLAYING; // game starts immediately
  60.    }
  61.    else
  62.    {
  63.       Board_Init (&the_board, PLAYER_HUMAN, PLAYER_HUMAN, FENSTARTUP_STANDARDCHESS); // we want a human opponent
  64.       the_board.game_state = STATE_PLAYING; // game starts immediately
  65.       DialogBox_RenameSides (); // pop up the opponents naming dialog box
  66.    }
  67.    Scene_Init (&the_scene, &the_board); // initialize scene
  68.  
  69.    SetWindowText (hMainWnd, PROGRAM_NAME); // update window title
  70.    the_board.reevaluate = true; // evaluate the new board
  71.    the_scene.update = true; // update scene
  72.  
  73.    return; // finished, a new game of the chosen type is being fired up
  74. }
  75.  
  76.  
  77. static void StartThread_ThisDialog (void *thread_parms)
  78. {
  79.    // this function runs in a separate thread, for that's the only way (seemingly)
  80.    // to implement a non-modal message box using the Common Controls library.
  81.  
  82.    // display the dialog box
  83.    newgame_type = DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog);
  84.    if (newgame_type > 0)
  85.       is_dialogbox_newgame_validated = true;
  86.  
  87.    return; // _endthread() implied
  88. }
  89.  
  90.  
  91. static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
  92. {
  93.    // message handler for the dialog box
  94.  
  95.    unsigned long connection_state;
  96.    unsigned short wParam_hiword;
  97.    unsigned short wParam_loword;
  98.  
  99.    // filter out the commonly used message values
  100.    wParam_hiword = HIWORD (wParam);
  101.    wParam_loword = LOWORD (wParam);
  102.  
  103.    // have we just fired up this window ?
  104.    if (message == WM_INITDIALOG)
  105.    {
  106.       // center the window
  107.       CenterWindow (hWnd, hMainWnd);
  108.  
  109.       // set dialog icons (small one for title bar & big one for task manager)
  110.       SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
  111.       SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
  112.  
  113.       // set window title and control texts
  114.       SetWindowText (hWnd, LOCALIZE (L"NewGame_Title"));
  115.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_QUESTION), LOCALIZE (L"NewGame_Question"));
  116.  
  117.       // if computer is connected to a network, enable online mode, else don't.
  118.       SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), LOCALIZE (L"NewGame_Online"));
  119.       if (InternetGetConnectedState (&connection_state, 0)
  120.           && (ConvertTo7BitASCII (ascii_hostname, sizeof (ascii_hostname), options.network.server_address), gethostbyname (ascii_hostname) != NULL))
  121.       {
  122.          Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), LOCALIZE (L"NewGame_OnlineDescription"));
  123.          EnableWindow (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), true);
  124.          EnableWindow (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), true);
  125.       }
  126.       else
  127.       {
  128.          Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), LOCALIZE (L"Error_NetworkInitializationFailed"));
  129.          EnableWindow (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), false);
  130.          EnableWindow (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), false);
  131.       }
  132.  
  133.       SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_COMPUTER), LOCALIZE (L"NewGame_Computer"));
  134.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_COMPUTERDESCRIPTION), LOCALIZE (L"NewGame_ComputerDescription"));
  135.  
  136.       SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_HUMAN), LOCALIZE (L"NewGame_Human"));
  137.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_HUMANDESCRIPTION), LOCALIZE (L"NewGame_HumanDescription"));
  138.  
  139.       Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_STATUSBAR), LOCALIZE (L"NewGame_StatusBar"));
  140.  
  141.       // convert the status bar message to a hyperlink
  142.       ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_NEWGAME_STATUSBAR));
  143.  
  144.       // and stop the center message display
  145.       the_scene.gui.want_spinwheel = false;
  146.    }
  147.  
  148.    // else did we click the close button on the title bar or hit the escape key ?
  149.    else if (message == WM_CLOSE)
  150.       EndDialog (hWnd, 0); // close the dialog box
  151.  
  152.    // else did we take action on one of the controls ?
  153.    else if (message == WM_COMMAND)
  154.    {
  155.       // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
  156.       if (wParam_loword == IDCANCEL)
  157.          EndDialog (hWnd, 0); // close the dialog box
  158.  
  159.       // else was it the new online game button ?
  160.       else if (wParam_loword == BUTTON_NEWGAME_ONLINE)
  161.          EndDialog (hWnd, GAMETYPE_ONLINE); // close the dialog box and return a "new online game" code
  162.  
  163.       // else was it the new game versus computer button ?
  164.       else if (wParam_loword == BUTTON_NEWGAME_COMPUTER)
  165.          EndDialog (hWnd, GAMETYPE_COMPUTER); // close the dialog box and return a "new game versus computer" code
  166.  
  167.       // else was it the new game versus human button ?
  168.       else if (wParam_loword == BUTTON_NEWGAME_HUMAN)
  169.          EndDialog (hWnd, GAMETYPE_TWOPLAYERS); // close the dialog box and return a "new game versus human" code
  170.  
  171.       // else was it the status bar hyperlink ?
  172.       else if (wParam_loword == STATICTEXT_NEWGAME_STATUSBAR)
  173.          ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
  174.    }
  175.  
  176.    // call the default dialog message processing function to keep things going
  177.    return (false);
  178. }
  179.