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