Subversion Repositories Games.Chess Giants

Rev

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

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