Rev 172 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line | 
|---|---|---|---|
| 1 | pmbaty | 1 | // dialog_resign.cpp | 
| 2 | |||
| 3 | #include "../common.h" | ||
| 4 | |||
| 5 | |||
| 6 | // dialog template | ||
| 7 | #define THIS_DIALOG DIALOG_RESIGN | ||
| 8 | |||
| 9 | |||
| 10 | // prototypes of local functions | ||
| 11 | static void StartThread_ThisDialog (void *thread_parms); | ||
| 12 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam); | ||
| 136 | pmbaty | 13 | static int resign_type; | 
| 1 | pmbaty | 14 | |
| 15 | |||
| 136 | pmbaty | 16 | void DialogBox_Resign (int type) | 
| 1 | pmbaty | 17 | { | 
| 18 |    // helper function to fire up the modeless dialog box | ||
| 19 | |||
| 136 | pmbaty | 20 | resign_type = type; // save the resign type for later use | 
| 140 | pmbaty | 21 | is_dialogbox_displayed = true; | 
| 19 | pmbaty | 22 | _beginthread (StartThread_ThisDialog, 0, NULL); // fire up a new thread to display the dialog box | 
| 1 | pmbaty | 23 | |
| 24 | return; // return as soon as the thread is fired up | ||
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | void DialogBox_Resign_Validated (void) | ||
| 29 | { | ||
| 30 |    // callback function called by the main game thread when the dialog box is validated | ||
| 31 | |||
| 32 | player_t *network_player; | ||
| 33 | |||
| 34 |    // remember this callback is no longer to be called | ||
| 35 | is_dialogbox_resign_validated = false; | ||
| 36 | |||
| 37 |    // see if we're playing against a remote opponent | ||
| 38 | network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player | ||
| 39 | |||
| 40 |    // is it an online game ? | ||
| 41 | if (network_player != NULL) | ||
| 42 | Player_SendBuffer_Add (network_player, 1000, L"resign\n"); // if so, send a resign command | ||
| 43 | |||
| 44 |    // else we're playing locally (either vs. another human or vs. computer) | ||
| 45 |    else | ||
| 46 |    { | ||
| 47 | Scene_Shutdown (&the_scene); // release scene | ||
| 48 | Board_Shutdown (&the_board); // release chess game | ||
| 172 | pmbaty | 49 | Board_Init (&the_board, PLAYER_HUMAN, PLAYER_HUMAN, L"standard", FENSTARTUP_STANDARDCHESS); // prepare a new human vs human game (by default) | 
| 1 | pmbaty | 50 | Scene_Init (&the_scene, &the_board); // initialize scene | 
| 51 | SetWindowText (hMainWnd, PROGRAM_NAME); // update window title | ||
| 52 | |||
| 136 | pmbaty | 53 | if (resign_type == RESIGNTYPE_NEWGAME) | 
| 54 | DialogBox_NewGame (); // show the new game dialog box | ||
| 55 | else if (resign_type == RESIGNTYPE_LOADGAME) | ||
| 56 | DialogBox_Load (); // show the open dialog box | ||
| 1 | pmbaty | 57 |    } | 
| 58 | |||
| 59 | the_board.reevaluate = true; // evaluate the new board | ||
| 60 | the_scene.update = true; // update scene | ||
| 61 | |||
| 62 | return; // finished, we have resigned from game | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | static void StartThread_ThisDialog (void *thread_parms) | ||
| 67 | { | ||
| 68 |    // this function runs in a separate thread, for that's the only way (seemingly) | ||
| 69 |    // to implement a non-modal message box using the Common Controls library. | ||
| 70 | |||
| 71 |    // display the dialog box | ||
| 72 | if (DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog) == 1) | ||
| 73 | is_dialogbox_resign_validated = true; | ||
| 140 | pmbaty | 74 | is_dialogbox_displayed = false; | 
| 1 | pmbaty | 75 | |
| 124 | pmbaty | 76 | the_board.reevaluate = true; // refresh the GUI buttons if needed | 
| 1 | pmbaty | 77 | return; // _endthread() implied | 
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam) | ||
| 82 | { | ||
| 83 |    // message handler for the dialog box | ||
| 84 | |||
| 85 | unsigned short wParam_hiword; | ||
| 86 | unsigned short wParam_loword; | ||
| 87 | |||
| 88 |    // filter out the commonly used message values | ||
| 89 | wParam_hiword = HIWORD (wParam); | ||
| 90 | wParam_loword = LOWORD (wParam); | ||
| 91 | |||
| 92 |    // have we just fired up this window ? | ||
| 93 | if (message == WM_INITDIALOG) | ||
| 94 |    { | ||
| 95 |       // center the window | ||
| 96 | CenterWindow (hWnd, hMainWnd); | ||
| 97 | |||
| 98 |       // set dialog icons (small one for title bar & big one for task manager) | ||
| 99 | SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN))); | ||
| 100 | SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN))); | ||
| 101 | |||
| 102 |       // set window title and control texts | ||
| 103 | SetWindowText (hWnd, LOCALIZE (L"Quit_Title")); | ||
| 104 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_RESIGN_QUESTION), LOCALIZE (L"Resign_Question")); | ||
| 105 | SetWindowText (GetDlgItem (hWnd, BUTTON_GIVEUP), LOCALIZE (L"Resign_GiveUp")); | ||
| 106 | SetWindowText (GetDlgItem (hWnd, BUTTON_BACKTOGAME), LOCALIZE (L"Resign_BackToGame")); | ||
| 107 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_RESIGN_STATUSBAR), LOCALIZE (L"Resign_StatusBar")); | ||
| 108 | |||
| 171 | pmbaty | 109 |       // play the "important notification" sound | 
| 185 | pmbaty | 110 | Audio_PlaySoundAtCenter (SOUNDTYPE_IMPORTANT); // play at the center of the board | 
| 19 | pmbaty | 111 | |
| 1 | pmbaty | 112 |       // convert the status bar message to a hyperlink | 
| 113 | ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_RESIGN_STATUSBAR)); | ||
| 114 |    } | ||
| 115 | |||
| 116 |    // else did we click the close button on the title bar ? | ||
| 117 | else if (message == WM_CLOSE) | ||
| 118 | EndDialog (hWnd, 0); // close the dialog box | ||
| 119 | |||
| 120 |    // else did we take action on one of the controls ? | ||
| 121 | else if (message == WM_COMMAND) | ||
| 122 |    { | ||
| 123 |       // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key) | ||
| 124 | if (wParam_loword == IDCANCEL) | ||
| 125 | EndDialog (hWnd, 0); // close the dialog box | ||
| 126 | |||
| 127 |       // else was it the quit button ? | ||
| 128 | else if (wParam_loword == BUTTON_GIVEUP) | ||
| 129 | EndDialog (hWnd, 1); // close the dialog box and return a success code | ||
| 130 | |||
| 131 |       // else was it the back to game button ? | ||
| 132 | else if (wParam_loword == BUTTON_BACKTOGAME) | ||
| 133 | EndDialog (hWnd, 0); // close the dialog box | ||
| 134 | |||
| 135 |       // else was it the status bar hyperlink ? | ||
| 136 | else if (wParam_loword == STATICTEXT_RESIGN_STATUSBAR) | ||
| 137 | ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized | ||
| 138 |    } | ||
| 139 | |||
| 140 |    // call the default dialog message processing function to keep things going | ||
| 141 | return (false); | ||
| 142 | } |