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