// dialog_newgame.cpp
#include "../common.h"
// dialog template
#define THIS_DIALOG DIALOG_NEWGAME
// game type definitons
#define GAMETYPE_ONLINE 1
#define GAMETYPE_COMPUTER 2
#define GAMETYPE_TWOPLAYERS 3
// global variables used in this module only
static int newgame_type = 0;
static char ascii_hostname[256];
// prototypes of local functions
static void StartThread_ThisDialog (void *thread_parms);
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
void DialogBox_NewGame (void)
{
// helper function to fire up the modeless dialog box
// HACK to prevent the player to click and block the view angles while the slide-in is not finished
command_ignoretime = current_time + 2.0f; // allow 2 seconds
_beginthread (StartThread_ThisDialog, 0, NULL); // so fire up a new one
the_scene.gui.want_spinwheel = true; // start spinning wheel
return; // return as soon as the thread is fired up
}
void DialogBox_NewGame_Validated (void)
{
// callback function called by the main game thread when the dialog box is validated
// remember this callback is no longer to be called
is_dialogbox_newgame_validated = false;
Scene_Shutdown (&the_scene); // release scene
Board_Shutdown (&the_board); // release chess game
if (newgame_type == GAMETYPE_ONLINE)
{
Board_Init (&the_board, PLAYER_HUMAN, PLAYER_INTERNET, FENSTARTUP_STANDARDCHESS); // we want an online opponent
the_board.game_state = STATE_UNKNOWN; // game does NOT start immediately
}
else if (newgame_type == GAMETYPE_COMPUTER)
{
Board_Init (&the_board, PLAYER_HUMAN, PLAYER_COMPUTER, FENSTARTUP_STANDARDCHESS); // we want a computer opponent
the_board.game_state = STATE_PLAYING; // game starts immediately
}
else
{
Board_Init (&the_board, PLAYER_HUMAN, PLAYER_HUMAN, FENSTARTUP_STANDARDCHESS); // we want a human opponent
the_board.game_state = STATE_PLAYING; // game starts immediately
DialogBox_RenameSides (); // pop up the opponents naming dialog box
}
Scene_Init (&the_scene, &the_board); // initialize scene
SetWindowText (hMainWnd, PROGRAM_NAME); // update window title
the_board.reevaluate = true; // evaluate the new board
the_scene.update = true; // update scene
return; // finished, a new game of the chosen type is being fired up
}
static void StartThread_ThisDialog (void *thread_parms)
{
// this function runs in a separate thread, for that's the only way (seemingly)
// to implement a non-modal message box using the Common Controls library.
// display the dialog box
newgame_type = DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), hMainWnd, DialogProc_ThisDialog);
if (newgame_type > 0)
is_dialogbox_newgame_validated = true;
the_board.reevaluate = true; // refresh the GUI buttons if needed
return; // _endthread() implied
}
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
{
// message handler for the dialog box
unsigned long connection_state;
unsigned short wParam_hiword;
unsigned short wParam_loword;
// filter out the commonly used message values
wParam_hiword = HIWORD (wParam);
wParam_loword = LOWORD (wParam);
// have we just fired up this window ?
if (message == WM_INITDIALOG)
{
// center the window
CenterWindow (hWnd, hMainWnd);
// set dialog icons (small one for title bar & big one for task manager)
SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
// set window title and control texts
SetWindowText (hWnd, LOCALIZE (L"NewGame_Title"));
Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_QUESTION), LOCALIZE (L"NewGame_Question"));
// if computer is connected to a network, enable online mode, else don't.
SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), LOCALIZE (L"NewGame_Online"));
if (InternetGetConnectedState (&connection_state, 0)
&& (ConvertTo7BitASCII (ascii_hostname, sizeof (ascii_hostname), options.network.server_address), gethostbyname (ascii_hostname) != NULL))
{
Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), LOCALIZE (L"NewGame_OnlineDescription"));
EnableWindow (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), true);
EnableWindow (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), true);
}
else
{
Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), LOCALIZE (L"Error_NetworkInitializationFailed"));
EnableWindow (GetDlgItem (hWnd, BUTTON_NEWGAME_ONLINE), false);
EnableWindow (GetDlgItem (hWnd, STATICTEXT_NEWGAME_ONLINEDESCRIPTION), false);
}
SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_COMPUTER), LOCALIZE (L"NewGame_Computer"));
Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_COMPUTERDESCRIPTION), LOCALIZE (L"NewGame_ComputerDescription"));
SetWindowText (GetDlgItem (hWnd, BUTTON_NEWGAME_HUMAN), LOCALIZE (L"NewGame_Human"));
Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_HUMANDESCRIPTION), LOCALIZE (L"NewGame_HumanDescription"));
Static_SetText (GetDlgItem (hWnd, STATICTEXT_NEWGAME_STATUSBAR), LOCALIZE (L"NewGame_StatusBar"));
// convert the status bar message to a hyperlink
ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_NEWGAME_STATUSBAR));
// and stop the center message display
the_scene.gui.want_spinwheel = false;
}
// else did we click the close button on the title bar or hit the escape key ?
else if (message == WM_CLOSE)
EndDialog (hWnd, 0); // close the dialog box
// else did we take action on one of the controls ?
else if (message == WM_COMMAND)
{
// did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
if (wParam_loword == IDCANCEL)
EndDialog (hWnd, 0); // close the dialog box
// else was it the new online game button ?
else if (wParam_loword == BUTTON_NEWGAME_ONLINE)
EndDialog (hWnd, GAMETYPE_ONLINE); // close the dialog box and return a "new online game" code
// else was it the new game versus computer button ?
else if (wParam_loword == BUTTON_NEWGAME_COMPUTER)
EndDialog (hWnd, GAMETYPE_COMPUTER); // close the dialog box and return a "new game versus computer" code
// else was it the new game versus human button ?
else if (wParam_loword == BUTTON_NEWGAME_HUMAN)
EndDialog (hWnd, GAMETYPE_TWOPLAYERS); // close the dialog box and return a "new game versus human" code
// else was it the status bar hyperlink ?
else if (wParam_loword == STATICTEXT_NEWGAME_STATUSBAR)
ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
}
// call the default dialog message processing function to keep things going
return (false);
}