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