Subversion Repositories Games.Chess Giants

Rev

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