Subversion Repositories Games.Chess Giants

Rev

Rev 85 | Rev 116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
// main.cpp
2
 
3
#define DEFINE_GLOBALS
4
#include "common.h"
5
 
6
 
7
// handy macros
8
#define GUIBUTTON_ENABLE(button) { if ((button).state == 0) (button).state = 1; }
9
#define GUIBUTTON_DISABLE(button) { if ((button).state > 0) (button).state = 0; }
10
 
11
 
12
// prototypes of locally used functions
13
static void MainLoop_FindCurrentViewer (void);
14
static void MainLoop_EvaluateGameState (void);
15
 
16
 
17
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, char *lpCmdLine, int nCmdShow)
18
{
19
   // the entry point for any Windows program
20
 
21
   WNDCLASSEX wc;
22
   MENUINFO menu_info;
23
   MSG msg;
24
   HBITMAP hSplashBmp;
25
   BITMAP splash_bmp;
26
   HDC hdc;
27
   RECT rect;
28
   PAINTSTRUCT ps;
29
   HBITMAP hbmTmp;
30
   HDC hdcMem;
60 pmbaty 31
   INPUT mousemove_input;
1 pmbaty 32
   float previous_time;
60 pmbaty 33
   float screensaverwatchdog_feedtime;
1 pmbaty 34
   int frame_count;
35
   int array_index;
36
   char *endptr;
37
   wchar_t app_pathname[MAX_PATH];
11 pmbaty 38
   wchar_t font_pathname[MAX_PATH];
41 pmbaty 39
   wchar_t temp_string[100];
1 pmbaty 40
 
41
   // save application instance
42
   hAppInstance = hInstance;
43
 
44
   // find module and path names, and *IMPORTANT*, set the current working directory there
45
   GetModuleFileName (NULL, app_pathname, WCHAR_SIZEOF (app_pathname));
46
   GetDirectoryPath (app_pathname, app_path);
47
   SetCurrentDirectoryW (app_path);
48
 
49
   // initialize stuff
14 pmbaty 50
   is_registered = false;
1 pmbaty 51
   terminate_everything = false;
52
   hMainWnd = NULL;
53
   hChatterChannelsWnd = NULL;
54
   hGamesWnd = NULL;
55
   hMOTDWnd = NULL;
56
   hOpponentsWnd = NULL;
57
   hSoughtWnd = NULL;
75 pmbaty 58
   is_paused = false; // clear pause status
1 pmbaty 59
#ifdef NDEBUG
60
   want_framerate = false; // release mode, don't display framerate
61
#else
2 pmbaty 62
   want_framerate = true; // display framerate in debug mode
1 pmbaty 63
#endif // NDEBUG
64
   is_dialogbox_about_validated = false;
65
   is_dialogbox_challenge_validated = false;
66
   is_dialogbox_changeappearance_validated = false;
67
   is_dialogbox_comment_validated = false;
68
   is_dialogbox_endgame_validated = false;
69
   is_dialogbox_gotomove_validated = false;
75 pmbaty 70
   is_dialogbox_renamesides_validated = false;
1 pmbaty 71
   is_dialogbox_load_validated = false;
72
   is_dialogbox_message_validated = false;
73
   is_dialogbox_newgame_validated = false;
74
   is_dialogbox_options_validated = false;
75
   is_dialogbox_pawnpromotion_validated = false;
76
   is_dialogbox_playercard_validated = false;
77
   is_dialogbox_playerinfoname_validated = false;
78
   is_dialogbox_quit_validated = false;
79
   is_dialogbox_resign_validated = false;
80
   is_dialogbox_save_validated = false;
81
   is_dialogbox_saveposition_validated = false;
82
   is_dialogbox_sendchallenge_validated = false;
83
   is_dialogbox_sendseek_validated = false;
84
   is_dialogbox_takeback_validated = false;
85
   is_window_chat_validated = false;
86
   is_window_chatterchannels_validated = false;
87
   is_window_games_validated = false;
88
   is_window_motd_validated = false;
89
   is_window_opponents_validated = false;
90
   is_window_sought_validated = false;
91
   save_pathname[0] = 0;
92
   srand ((unsigned int) time (NULL)); // initialize PRNG
21 pmbaty 93
   animation_endtime = 0.0f;
94
   command_ignoretime = 0.0f;
95
   sound_playtime = 0.0f;
96
   highlight_endtime = 0.0f;
97
   previous_time = 0.0f;
98
   frame_count = 0;
1 pmbaty 99
 
59 pmbaty 100
   // load the texts and ensure we have at least one display language
101
   LocalizedTexts_Init ();
102
   if (language_count < 1)
30 pmbaty 103
   {
56 pmbaty 104
      MessageBox (NULL, L"Chess Giants was unable to load its data files.\nThe game cannot start.\n\nPlease reinstall this program to fix the problem.", L"Chess Giants", MB_ICONERROR | MB_OK);
1 pmbaty 105
      return (-1); // bomb out on error
56 pmbaty 106
   }
1 pmbaty 107
 
59 pmbaty 108
   // read configuration data
109
   Config_Load ();
1 pmbaty 110
 
59 pmbaty 111
   // see if the program is registered
112
   is_registered = IsRegistrationCorrect (options.registration.user_email, options.registration.activation_code);
1 pmbaty 113
 
83 pmbaty 114
   // uncomment the two lines below to test the registration code
115
   //DialogBox_Registration ();
116
   //return (0);
117
 
1 pmbaty 118
   // register the window class, create the window and show it
119
   memset (&wc, 0, sizeof (wc));
120
   wc.cbSize = sizeof (wc);
121
   wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; // double-clicks support
122
   wc.lpfnWndProc = WindowProc_Main;
123
   wc.hInstance = hAppInstance;
124
   wc.hIcon = LoadIcon (hAppInstance, (wchar_t *) ICON_MAIN);
125
   wc.hCursor = LoadCursor (NULL, IDC_ARROW);
126
   wc.lpszClassName = PROGRAM_NAME L" WndClass";
127
   RegisterClassEx (&wc);
41 pmbaty 128
   swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), PROGRAM_NAME L"%s", (is_registered ? L"" : LOCALIZE (L"EvaluationMode"))); // build window title
1 pmbaty 129
   if (options.want_fullscreen)
41 pmbaty 130
      hMainWnd = CreateWindowEx (0, wc.lpszClassName, temp_string, WS_POPUPWINDOW, // temp_string holds window title
59 pmbaty 131
                                 0, 0, GetSystemMetrics (SM_CXSCREEN), GetSystemMetrics (SM_CYSCREEN), NULL, NULL, hAppInstance, NULL);
1 pmbaty 132
   else
133
   {
3 pmbaty 134
      // in windowed mode, ensure window width and height aren't greater than screen size nor lower than a safe minimum
1 pmbaty 135
      if (options.window_width > GetSystemMetrics (SM_CXMAXTRACK))
3 pmbaty 136
         options.window_width = GetSystemMetrics (SM_CXMAXTRACK); // check this first in case screen size is reported incorrect
1 pmbaty 137
      if (options.window_height > GetSystemMetrics (SM_CYMAXTRACK))
138
         options.window_height = GetSystemMetrics (SM_CYMAXTRACK);
3 pmbaty 139
      if (options.window_width < 640)
140
         options.window_width = 640; // check this secondly in case screen size is reported incorrect
141
      if (options.window_height < 480)
142
         options.window_height = 480;
1 pmbaty 143
 
41 pmbaty 144
      hMainWnd = CreateWindowEx (0, wc.lpszClassName, temp_string, WS_OVERLAPPEDWINDOW, // temp_string holds window title
1 pmbaty 145
                                 GetSystemMetrics (SM_CXSCREEN) / 2 - options.window_width / 2,
146
                                 GetSystemMetrics (SM_CYSCREEN) / 2 - options.window_height / 2,
59 pmbaty 147
                                 options.window_width, options.window_height, NULL, NULL, hAppInstance, NULL);
1 pmbaty 148
   }
149
   ShowWindow (hMainWnd, nCmdShow);
150
 
59 pmbaty 151
   // create the main menu line, and its accelerators
152
   hMainMenu = NULL;
153
   hMainAccelerators = NULL;
154
   CreateOrUpdateApplicationMenu ();
155
 
1 pmbaty 156
   // display the splash screen
157
   memset ((void *) &splash_bmp, 0, sizeof (splash_bmp));
11 pmbaty 158
   hSplashBmp = W32LoadImage (L"%s/data/splash.bmp", app_path); // load the splash bitmap
1 pmbaty 159
   GetObject (hSplashBmp, sizeof (splash_bmp), (void *) &splash_bmp); // get a structure containing its size (among others)
160
   hdcMem = CreateCompatibleDC (NULL); // create a device context compatible with the screen
161
   hbmTmp = SelectBitmap (hdcMem, hSplashBmp); // select our bitmap to use in it
162
   hdc = BeginPaint (hMainWnd, &ps); // begin painting on the main window
163
   GetClientRect (hMainWnd, &rect);
164
   StretchBlt (hdc, 0, 0, rect.right, rect.bottom, hdcMem, 0, 0, splash_bmp.bmWidth, splash_bmp.bmHeight, SRCCOPY); // bit blit the bitmap into it
165
   EndPaint (hMainWnd, &ps); // end painting on the main window
166
   SelectObject (hdcMem, hbmTmp); // restore the previous selection
167
   DeleteDC (hdcMem); // and delete the handles to the device context
168
   DeleteObject (hSplashBmp); // and to the bitmap that we used
169
 
170
   // make the menu modeless
171
   memset (&menu_info, 0, sizeof (menu_info)); // prepare menu info structure
172
   menu_info.cbSize = sizeof (MENUINFO);
173
   menu_info.fMask = MIM_STYLE;
174
   GetMenuInfo (GetMenu (hMainWnd), &menu_info); // get current style
175
   menu_info.dwStyle |= MNS_MODELESS; // add the "modeless" flag
176
   SetMenuInfo (GetMenu (hMainWnd), &menu_info); // and send it back
177
 
178
   // load status icons, bitmaps and texts
11 pmbaty 179
   handlestatus[HANDLESTATUS_AVAILABLE].icon = W32LoadIcon (L"%s/data/icons/available.ico", app_path);
180
   handlestatus[HANDLESTATUS_AVAILABLE].bitmap = W32LoadImage (L"%s/data/status/available.bmp", app_path);
1 pmbaty 181
   handlestatus[HANDLESTATUS_AVAILABLE].text = LOCALIZE (L"Opponents_StatusAvailable");
11 pmbaty 182
   handlestatus[HANDLESTATUS_INGAME].icon = W32LoadIcon (L"%s/data/icons/ingame.ico", app_path);
183
   handlestatus[HANDLESTATUS_INGAME].bitmap = W32LoadImage (L"%s/data/status/ingame.bmp", app_path);
1 pmbaty 184
   handlestatus[HANDLESTATUS_INGAME].text = LOCALIZE (L"Opponents_StatusInGame");
11 pmbaty 185
   handlestatus[HANDLESTATUS_INSIMULATION].icon = W32LoadIcon (L"%s/data/icons/insimulation.ico", app_path);
186
   handlestatus[HANDLESTATUS_INSIMULATION].bitmap = W32LoadImage (L"%s/data/status/insimulation.bmp", app_path);
1 pmbaty 187
   handlestatus[HANDLESTATUS_INSIMULATION].text = LOCALIZE (L"Opponents_StatusInSimulation");
11 pmbaty 188
   handlestatus[HANDLESTATUS_INTOURNAMENT].icon = W32LoadIcon (L"%s/data/icons/intournament.ico", app_path);
189
   handlestatus[HANDLESTATUS_INTOURNAMENT].bitmap = W32LoadImage (L"%s/data/status/intournament.bmp", app_path);
1 pmbaty 190
   handlestatus[HANDLESTATUS_INTOURNAMENT].text = LOCALIZE (L"Opponents_StatusInTournament");
11 pmbaty 191
   handlestatus[HANDLESTATUS_EXAMININGAGAME].icon = W32LoadIcon (L"%s/data/icons/examiningagame.ico", app_path);
192
   handlestatus[HANDLESTATUS_EXAMININGAGAME].bitmap = W32LoadImage (L"%s/data/status/examiningagame.bmp", app_path);
1 pmbaty 193
   handlestatus[HANDLESTATUS_EXAMININGAGAME].text = LOCALIZE (L"Opponents_StatusExaminingAGame");
11 pmbaty 194
   handlestatus[HANDLESTATUS_NOTOPENFORAMATCH].icon = W32LoadIcon (L"%s/data/icons/notopenforamatch.ico", app_path);
195
   handlestatus[HANDLESTATUS_NOTOPENFORAMATCH].bitmap = W32LoadImage (L"%s/data/status/notopenforamatch.bmp", app_path);
1 pmbaty 196
   handlestatus[HANDLESTATUS_NOTOPENFORAMATCH].text = LOCALIZE (L"Opponents_StatusNotOpenForAMatch");
11 pmbaty 197
   handlestatus[HANDLESTATUS_INACTIVEORBUSY].icon = W32LoadIcon (L"%s/data/icons/inactiveorbusy.ico", app_path);
198
   handlestatus[HANDLESTATUS_INACTIVEORBUSY].bitmap = W32LoadImage (L"%s/data/status/inactiveorbusy.bmp", app_path);
1 pmbaty 199
   handlestatus[HANDLESTATUS_INACTIVEORBUSY].text = LOCALIZE (L"Opponents_StatusInactiveOrBusy");
11 pmbaty 200
   handlestatus[HANDLESTATUS_OFFLINE].icon = W32LoadIcon (L"%s/data/icons/offline.ico", app_path);
201
   handlestatus[HANDLESTATUS_OFFLINE].bitmap = W32LoadImage (L"%s/data/status/offline.bmp", app_path);
1 pmbaty 202
   handlestatus[HANDLESTATUS_OFFLINE].text = LOCALIZE (L"Opponents_StatusOffline");
203
 
11 pmbaty 204
   // load the system fonts
1 pmbaty 205
   hFontChat = CreateFont (17, 0, 0, 0, FW_DONTCARE, false, false, false, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
206
                           CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Comic Sans MS");
207
 
21 pmbaty 208
   // before any rendering is done, it's a good idea to know what time it is
209
   current_time = ProcessTime ();
75 pmbaty 210
   stoppage_time = 0;
60 pmbaty 211
   screensaverwatchdog_feedtime = current_time + 50.0f; // feed screensaver watchdog every 50 seconds
21 pmbaty 212
 
1 pmbaty 213
   // initialize renderer
214
   if (!Render_Init ())
215
      return (-1); // bomb out on error
216
 
217
   // load sprites
11 pmbaty 218
   larrow_spriteindex = Render_LoadSprite (L"%s/data/sprites/arrow-left.*", app_path);
219
   rarrow_spriteindex = Render_LoadSprite (L"%s/data/sprites/arrow-right.*", app_path);
220
   chatbutton_spriteindex = Render_LoadSprite (L"%s/data/sprites/chat.*", app_path);
221
   gamesbutton_spriteindex = Render_LoadSprite (L"%s/data/sprites/games.*", app_path);
222
   peoplebutton_spriteindex = Render_LoadSprite (L"%s/data/sprites/people.*", app_path);
223
   sepia_spriteindex = Render_LoadSprite (L"%s/data/sprites/sepia.*", app_path);
1 pmbaty 224
   for (array_index = 0; array_index < 12; array_index++)
11 pmbaty 225
      spinner_spriteindex[array_index] = Render_LoadSprite (L"%s/data/sprites/spinner-%d.png", app_path, array_index * 30); // spinning wheel
1 pmbaty 226
 
11 pmbaty 227
   // add our custom fonts to the list of available fonts for the duration of the process
228
   swprintf_s (font_pathname, WCHAR_SIZEOF (font_pathname), L"%s/data/fonts/papyrus.ttf", app_path);
229
   AddFontResourceEx (font_pathname, FR_PRIVATE, 0);
230
   // load rendered fonts
18 pmbaty 231
   arrow_fontindex = Render_LoadFont (L"Papyrus", 24, false, false);
232
   chat_fontindex = Render_LoadFont (L"Papyrus", 32, false, false);
233
   players_fontindex = Render_LoadFont (L"Papyrus", 40, false, true);
234
   centermsg_fontindex = Render_LoadFont (L"Papyrus", 54, false, true);
1 pmbaty 235
 
236
   // load themes, initialize a new human vs. human game and setup the scene
237
   if (!Themes_Init ())
238
      return (-1); // bomb out on error
239
   Board_Init (&the_board, PLAYER_HUMAN, PLAYER_HUMAN, FENSTARTUP_STANDARDCHESS);
240
   Scene_Init (&the_scene, &the_board);
241
 
242
   // has a filename been specifed on the command-line AND that file exists ?
243
   if ((lpCmdLine != NULL) && (lpCmdLine[0] != 0))
244
   {
245
      while (*lpCmdLine == '"')
246
         lpCmdLine++; // skip leading quotes
247
      if ((endptr = strchr (lpCmdLine, '"')) != NULL)
248
         *endptr = 0; // break the string at the first ending quote
41 pmbaty 249
 
87 pmbaty 250
      // is it a file that exists ? (don't use stat() which is broken on WinXP)
251
      if (_access (lpCmdLine, 0) == 0)
50 pmbaty 252
      {
253
         ConvertToWideChar (load_pathname, WCHAR_SIZEOF (load_pathname), lpCmdLine); // save pathname string
254
         is_dialogbox_load_validated = true; // and act as if the user just validated a load dialog box
255
      }
41 pmbaty 256
#ifndef NDEBUG
50 pmbaty 257
      // else is it a registration info ? if so, parse it
258
      else if ((strncmp (lpCmdLine, "/r=", 3) == 0) && ((endptr = strchr (&lpCmdLine[3], ',')) != NULL))
41 pmbaty 259
      {
260
         *endptr = 0; // break the string at the separator between user email and activation code
261
         ConvertToWideChar (temp_string, WCHAR_SIZEOF (temp_string), &lpCmdLine[3]); // read user email
262
         is_registered = IsRegistrationCorrect (temp_string, atoi (endptr + 1)); // and see whether we're registered or not
263
         DialogBox_NewGame (); // still open the "new game" dialog box
264
      }
265
#endif // !NDEBUG
1 pmbaty 266
      else
267
         DialogBox_NewGame (); // if specified filename doesn't exist, fallback to the "new game" dialog box
268
   }
269
   else
270
      DialogBox_NewGame (); // when no filename is specified, display the new game dialog box
271
 
272
   // enter the main loop
273
   while (!terminate_everything)
274
   {
275
      // see what time it is
276
      current_time = ProcessTime ();
75 pmbaty 277
      if (is_paused)
278
         stoppage_time += (current_time - previous_time); // if we're paused, increase stoppage time
1 pmbaty 279
 
60 pmbaty 280
      // is it time to feed the screensaver watchdog ? (to prevent it from starting)
281
      if (screensaverwatchdog_feedtime < current_time)
282
      {
283
         mousemove_input.type = INPUT_MOUSE;
284
         memset (&mousemove_input.mi, 0, sizeof (mousemove_input.mi)); // blank out struct = no move at all :)
285
         mousemove_input.mi.dwFlags = MOUSEEVENTF_MOVE;
286
         SendInput (1, &mousemove_input, sizeof (mousemove_input)); // send a fake mouse move input event
287
 
288
         screensaverwatchdog_feedtime = current_time + 50.0f; // feed screensaver watchdog again in 50 seconds
289
      }
290
 
1 pmbaty 291
      // are we in demo mode and is it time to quit ?
14 pmbaty 292
      if (!is_registered && (current_time > DEMO_TIMEOUT))
18 pmbaty 293
         DestroyWindow (hMainWnd); // if so, send a quit message in order to break the loop
1 pmbaty 294
 
295
      // grab the current window size
296
      GetWindowRect (hMainWnd, &rect);
297
      options.window_width = rect.right - rect.left;
298
      options.window_height = rect.bottom - rect.top;
299
 
300
      // are we in the middle of an animation or just after it ?
301
      if (current_time < animation_endtime + 0.5f)
302
         the_scene.update = true; // always update during animations
303
      else
304
         the_scene.update |= Board_Think (&the_board); // make the board (i.e, both players) think
305
 
306
      MainLoop_FindCurrentViewer ();  // determine current viewer
307
 
308
      // see if we have a message waiting for any window in the current thread and dispatch it
309
      while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
310
      {
311
         // check for accelerator keystrokes from the main window
59 pmbaty 312
         if ((msg.hwnd != hMainWnd) || !TranslateAccelerator (hMainWnd, hMainAccelerators, &msg))
1 pmbaty 313
         {
314
            TranslateMessage (&msg); // translate and dispatch all other messages
315
            DispatchMessage (&msg);
316
         }
317
      }
318
 
319
      // handle dialog box and window return codes
320
      if (is_dialogbox_about_validated) DialogBox_About_Validated ();
321
      if (is_dialogbox_challenge_validated) DialogBox_Challenge_Validated ();
322
      if (is_dialogbox_changeappearance_validated) DialogBox_ChangeAppearance_Validated ();
323
      if (is_dialogbox_comment_validated) DialogBox_Comment_Validated ();
324
      if (is_dialogbox_endgame_validated) DialogBox_EndGame_Validated ();
325
      if (is_dialogbox_gotomove_validated) DialogBox_GoToMove_Validated ();
75 pmbaty 326
      if (is_dialogbox_renamesides_validated) DialogBox_RenameSides_Validated ();
1 pmbaty 327
      if (is_dialogbox_load_validated) DialogBox_Load_Validated ();
328
      if (is_dialogbox_message_validated) DialogBox_Message_Validated ();
329
      if (is_dialogbox_newgame_validated) DialogBox_NewGame_Validated ();
330
      if (is_dialogbox_options_validated) DialogBox_Options_Validated ();
331
      if (is_dialogbox_pawnpromotion_validated) DialogBox_PawnPromotion_Validated ();
332
      if (is_dialogbox_playercard_validated) DialogBox_PlayerCard_Validated ();
333
      if (is_dialogbox_playerinfoname_validated) DialogBox_PlayerInfoName_Validated ();
334
      if (is_dialogbox_quit_validated) DialogBox_Quit_Validated ();
335
      if (is_dialogbox_resign_validated) DialogBox_Resign_Validated ();
336
      if (is_dialogbox_save_validated) DialogBox_Save_Validated ();
337
      if (is_dialogbox_saveposition_validated) DialogBox_SavePosition_Validated ();
338
      if (is_dialogbox_sendchallenge_validated) DialogBox_SendChallenge_Validated ();
339
      if (is_dialogbox_sendseek_validated) DialogBox_SendSeek_Validated ();
340
      if (is_dialogbox_takeback_validated) DialogBox_Takeback_Validated ();
341
      if (is_window_chat_validated) Window_Chat_Validated ();
342
      if (is_window_chatterchannels_validated) Window_ChatterChannels_Validated ();
343
      if (is_window_games_validated) Window_Games_Validated ();
344
      if (is_window_motd_validated) Window_MOTD_Validated ();
345
      if (is_window_opponents_validated) Window_Opponents_Validated ();
346
      if (is_window_sought_validated) Window_Sought_Validated ();
347
 
348
      MainLoop_EvaluateGameState (); // evaluate game state
349
 
350
      // is the table rotating ?
351
      if (Player_RotateTable (&the_board.players[current_viewer], current_time - previous_time))
352
         the_scene.update = true; // if so, update the scene
353
 
354
      // are we highlighting something ?
355
      if (highlight_endtime > current_time)
356
         the_scene.update = true; // if so, update the scene
357
 
358
      // else if we WERE just highlighting something, clear the hovered positions
359
      else if (highlight_endtime + 0.1f > current_time)
360
      {
361
         the_board.hovered_position[0] = -1;
362
         the_board.hovered_position[1] = -1;
363
         the_scene.update = true; // and update the scene
364
      }
365
 
366
      // if we want the game clock, update scene every second
367
      if (options.want_clock && ((int) current_time != (int) previous_time))
368
         the_scene.update = true;
369
 
370
      // if there's something in the central text buffer, update scene too
371
      if (the_scene.gui.central_text.is_displayed)
372
         the_scene.update = true;
373
 
374
      // if we have a spinning wheel, update scene too
375
      if (the_scene.gui.want_spinwheel)
376
         the_scene.update = true;
377
 
85 pmbaty 378
      // is our theme not loaded yet ?
379
      if (!theme->is_loaded)
380
         Theme_LoadABitMore (theme); // load a bit more of it
381
 
382
      // else do we NOT need to update the scene ?
383
      else if (!(the_scene.update || want_framerate))
1 pmbaty 384
      {
385
         // cycle through all themes and see if one of them needs to be loaded
386
         for (array_index = 0; array_index < theme_count; array_index++)
387
            if (!themes[array_index].is_loaded)
388
            {
85 pmbaty 389
               Theme_LoadABitMore (&themes[array_index]); // load a bit more of this theme
1 pmbaty 390
               break; // and stop looping (see you next pass)
391
            }
392
      }
393
 
85 pmbaty 394
      // either we need to update the scene, or we want to do it continuously
395
      else
1 pmbaty 396
      {
397
         Scene_Update (&the_scene, &the_board); // evaluate which parts need to be placed
398
         Render_RenderFrame (&the_scene); // render a game frame
399
 
400
         the_scene.update = false; // scene no longer needs to be updated now
401
      }
402
 
403
      Audio_Think (); // ensure audio is playing
404
 
405
      previous_time = current_time; // save previous time
406
      if (frame_count == 100)
407
      {
408
         frame_count = 0; // reset frame count every 100 frames
409
         Sleep (1); // once every 100 frames, wait a millisecond
410
      }
411
      else
412
         Sleep (0); // else just allow context switching
413
      frame_count++; // increase the frame count
414
   }
415
 
416
   /////////////////////////////////////////////////////////////////////////
417
   // at this point, we exited the main loop and we are returning to Windows
418
 
419
   // release scene, game, themes and shutdown Direct3D
420
   Scene_Shutdown (&the_scene);
421
   Board_Shutdown (&the_board);
422
   Themes_Shutdown ();
423
   Render_Shutdown ();
424
 
425
   // delete the font resources
426
   DeleteObject (hFontChat);
427
 
428
   // delete the bitmap and icon ressources
429
   for (array_index = 1; array_index < sizeof (handlestatus) / sizeof (handlestatus[0]); array_index++)
430
   {
431
      DeleteObject (handlestatus[array_index].bitmap);
432
      DestroyIcon (handlestatus[array_index].icon);
433
   }
434
 
435
   // unregister window class
436
   UnregisterClass (wc.lpszClassName, hAppInstance);
437
 
438
   // destroy the accelerators table
59 pmbaty 439
   if (hMainAccelerators)
440
      DestroyAcceleratorTable (hMainAccelerators);
441
   hMainAccelerators = NULL;
1 pmbaty 442
 
59 pmbaty 443
   // destroy the application menu object
444
   if (IsMenu (hMainMenu))
445
      DestroyMenu (hMainMenu);
446
   hMainMenu = NULL;
1 pmbaty 447
 
14 pmbaty 448
   // are we not registered yet ?
449
   if (!is_registered)
450
      DialogBox_Registration ();
451
 
1 pmbaty 452
   // save configuration data
453
   Config_Save ();
454
 
455
   // unload localized texts
456
   LocalizedTexts_Shutdown ();
457
 
458
   return (0); // and return to Windows.
459
}
460
 
461
 
462
static void MainLoop_FindCurrentViewer (void)
463
{
464
   // helper function that tells who is the current viewer
465
 
466
   if ((the_board.players[COLOR_WHITE].type == PLAYER_HUMAN) && (the_board.players[COLOR_BLACK].type == PLAYER_HUMAN))
467
      current_viewer = Board_ColorToMove (&the_board); // if both players are human, track them both
468
   else if (the_board.players[COLOR_WHITE].type == PLAYER_HUMAN)
469
      current_viewer = COLOR_WHITE; // else if only the white is human, track the white
470
   else if (the_board.players[COLOR_BLACK].type == PLAYER_HUMAN)
471
      current_viewer = COLOR_BLACK; // else if it's the black, track the black
472
   else
473
      current_viewer = COLOR_WHITE; // else no human in game, track just the white
474
 
475
   return; // finished, current viewer is known
476
}
477
 
478
 
479
static void MainLoop_EvaluateGameState (void)
480
{
481
   // function to evaluate the game state in the main loop when a part has just moved
482
 
41 pmbaty 483
   static wchar_t window_title[256];
484
 
1 pmbaty 485
   player_t *current_player;
486
   player_t *opposite_player;
487
   player_t *network_player;
488
   boardmove_t *last_move;
489
   int enabled_value;
490
   int move_index;
491
 
492
   if (!the_board.reevaluate)
493
      return; // if the board doesn't need to be reevaluated, don't do anything
494
 
495
   // get current and opposite players
496
   current_player = Player_GetCurrent ();
497
   opposite_player = Player_GetOpposite ();
498
 
499
   // see if we're online
500
   network_player = Player_FindByType (PLAYER_INTERNET);
501
 
502
   // has the game started ?
503
   if (the_board.move_count > 1)
504
   {
505
      // game has started, enable the "save" and "save as" menu options
506
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVE, MF_ENABLED);
507
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEAS, MF_ENABLED);
508
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_ENABLED);
509
 
510
      // enable the "go to move" menu option
511
      EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_GOTOMOVE, MF_ENABLED);
512
 
513
      // are we watching the last move ?
514
      if (the_board.viewed_move == the_board.move_count - 1)
515
      {
516
         // get a quick acccess to the last move
517
         last_move = &the_board.moves[the_board.move_count - 1];
518
 
50 pmbaty 519
         // if the current player is a human AND its opponent is a computer, allow him to ask us for a hint
520
         if ((current_player->type == PLAYER_HUMAN) && (opposite_player->type == PLAYER_COMPUTER))
1 pmbaty 521
            EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SUGGESTMOVE, MF_ENABLED);
522
         else
523
            EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SUGGESTMOVE, MF_GRAYED);
524
 
525
         // (if the current player is a human
526
         //  AND (its opponent is another human AND there's at least one move played)
527
         //       OR (its opponent is a computer AND there are at least two moves played)
528
         //       OR (its opponent is a remote player AND we're in game AND there are at least two moves played))
529
         // OR the current player is a remote player AND we're in game AND there are at least two moves played), allow him to cancel move
530
         if (((current_player->type == PLAYER_HUMAN)
531
              && ((opposite_player->type == PLAYER_HUMAN)
532
                  || ((opposite_player->type == PLAYER_COMPUTER) && (the_board.move_count > 2))
533
                  || ((opposite_player->type == PLAYER_INTERNET) && (opposite_player->is_in_game) && (the_board.move_count > 2))))
534
             || ((current_player->type == PLAYER_INTERNET) && (current_player->is_in_game) && (the_board.move_count > 2)))
535
            EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_CANCELLASTMOVE, MF_ENABLED);
536
         else
537
            EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_CANCELLASTMOVE, MF_GRAYED);
538
 
539
         // is the current player in check ? (to play the right sound)
540
         // read as: was the last move an opponent's move AND did it put us to check ?
541
         if (last_move->is_check)
542
         {
543
            // is it a checkmate ? (checkmate == check + stalemate)
544
            if (last_move->is_stalemate)
545
            {
546
               // display the game over dialog box
547
               the_board.game_state = (Board_ColorToMove (&the_board) == COLOR_WHITE ? STATE_BLACKWIN_CHECKMATE : STATE_WHITEWIN_CHECKMATE);
548
               DialogBox_EndGame ();
549
            }
550
         }
551
         else
552
         {
553
            // is it a stalemate ?
554
            if (last_move->is_stalemate)
555
            {
556
               // display the game over dialog box
557
               the_board.game_state = STATE_DRAW_STALEMATE;
558
               DialogBox_EndGame ();
559
            }
560
         }
561
 
562
         // have there 50 moves been played each side (i.e, 100 plies) AND is the current player human ?
563
         if ((the_board.move_count > 100) && (current_player->type == PLAYER_HUMAN))
564
         {
565
            // go backwards and see when is the latest move that took an opponent's piece OR the latest pawn move
566
            for (move_index = the_board.move_count - 1; move_index >= 0; move_index--)
567
               if (the_board.moves[move_index].has_captured || (the_board.moves[move_index].part == PART_PAWN))
568
                  break; // stop as soon as we find one
569
 
570
            // can the fifty moves draw rule be claimed AND does the current player claims it ?
571
            if (move_index + 1 + 100 < the_board.move_count)
572
            {
573
               // yes. Propose it to the side that's on move
574
// TODO: non-modal MessageBox (copy dialog_newgame.cpp and use return values)
575
            }
576
         }
577
 
578
         if (the_scene.gui.larrow.state == 0)
579
            the_scene.gui.larrow.state = 1; // enable "back" arrow if it isn't displayed yet
580
         if (the_scene.gui.rarrow.state != 0)
581
            the_scene.gui.rarrow.state = 0; // disable "forward" arrow if it's already displayed
582
 
583
         if (the_board.game_state == STATE_PLAYING)
584
            Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
75 pmbaty 585
                           RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (is_paused ? L"Paused" : L"Current"));
1 pmbaty 586
         else if ((the_board.game_state == STATE_BLACKWIN_CHECKMATE) || (the_board.game_state == STATE_WHITEWIN_CHECKMATE))
587
            Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
588
                           RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_CheckMate"));
589
         else if ((the_board.game_state == STATE_WHITEWIN_RESIGNORFORFEIT) || (the_board.game_state == STATE_BLACKWIN_RESIGNORFORFEIT))
590
            Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
591
                           RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_Resign"));
592
         else if (the_board.game_state == STATE_DRAW_STALEMATE)
593
            Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
594
                           RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_StaleMate"));
595
         else if (the_board.game_state == STATE_DRAW_AGREEMENT)
596
            Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
597
                           RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_Agreement"));
598
         else if (the_board.game_state == STATE_DRAW_OTHER)
599
            Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
600
                           RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"EndGame_DrawOther"));
601
 
602
         // enable the "comment on this move" menu option
603
         EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_COMMENTMOVE, MF_ENABLED);
604
      }
605
 
606
      // else are we watching another move, but not the beginning of the game ?
607
      else if (the_board.viewed_move > 0)
608
      {
609
         if (the_scene.gui.larrow.state == 0)
610
            the_scene.gui.larrow.state = 1; // enable "back" arrow if it isn't displayed yet
611
         if (the_scene.gui.rarrow.state == 0)
612
            the_scene.gui.rarrow.state = 1; // enable "forward" arrow if it isn't displayed yet
613
         Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
614
                        RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false,
18 pmbaty 615
                        L"%s %d\n%s", LOCALIZE (L"Move"), (the_board.viewed_move + 1) / 2, (the_board.viewed_move % 2 ? LOCALIZE (L"Games_White"): LOCALIZE (L"Games_Black")));
1 pmbaty 616
 
617
         // enable the "save position as" and "comment on this move" menu options
618
         EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_ENABLED);
619
         EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_COMMENTMOVE, MF_ENABLED);
620
      }
621
 
622
      // else we must be watching the beginning of the game (no move yet)
623
      else
624
      {
625
         if (the_scene.gui.larrow.state != 0)
626
            the_scene.gui.larrow.state = 0; // disable "back" arrow if it's already displayed
627
         if (the_scene.gui.rarrow.state == 0)
628
            the_scene.gui.rarrow.state = 1; // enable "forward" arrow if it isn't displayed yet
629
         Scene_SetText (&the_scene.gui.arrow_text, 3.3f, 5.0f, -1, ALIGN_CENTER, ALIGN_TOP, ALIGN_CENTER, arrow_fontindex,
630
                        RGBACOLOR_SETALPHA (options.clock_color, 0x7f), 999999.0f, false, LOCALIZE (L"Beginning"));
631
 
632
         // disable the "save position as" and "comment on this move" menu options
633
         EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_GRAYED);
634
         EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_COMMENTMOVE, MF_GRAYED);
635
      }
636
   }
637
   else
638
   {
639
      // game has not started, disable the "save", "save as" and "save position as" menu options
640
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVE, MF_GRAYED);
641
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEAS, MF_GRAYED);
642
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SAVEPOSITIONAS, MF_GRAYED);
643
 
52 pmbaty 644
      // if the current player is a human AND its opponent is a computer, allow him to ask us for a hint
645
      if ((current_player->type == PLAYER_HUMAN) && (opposite_player->type == PLAYER_COMPUTER))
646
         EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SUGGESTMOVE, MF_ENABLED);
647
      else
648
         EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SUGGESTMOVE, MF_GRAYED);
649
 
650
      // disable the "cancel last move", "comment move" and "go to move" menu options
1 pmbaty 651
      EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_CANCELLASTMOVE, MF_GRAYED);
652
      EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_COMMENTMOVE, MF_GRAYED);
653
      EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_GOTOMOVE, MF_GRAYED);
654
 
75 pmbaty 655
      is_paused = false; // clear pause status (we can only pause a game when it's been started)
656
 
1 pmbaty 657
      // and disable the two arrows and the arrow text
658
      the_scene.gui.larrow.state = 0;
659
      the_scene.gui.rarrow.state = 0;
660
      the_scene.gui.arrow_text.is_displayed = false;
661
   }
662
 
663
   // no matter whether the game started or not, if the current player is a human AND its opponent is a computer, allow him to swap sides
664
   if ((current_player->type == PLAYER_HUMAN) && (opposite_player->type == PLAYER_COMPUTER))
665
      EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SWAPSIDES, MF_ENABLED);
666
   else
667
      EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_SWAPSIDES, MF_GRAYED);
668
 
75 pmbaty 669
   // no matter whether the game started or not, enable players renaming only if we are NOT in internet mode
670
   EnableMenuItem (GetMenu (hMainWnd), MENUID_CHESSBOARD_RENAMESIDES, (network_player == NULL ? MF_ENABLED : MF_GRAYED));
671
 
1 pmbaty 672
   // update window title
673
   if ((the_board.players[COLOR_WHITE].name[0] != 0) && (the_board.players[COLOR_BLACK].name[0] != 0))
674
   {
14 pmbaty 675
      swprintf_s (window_title, WCHAR_SIZEOF (window_title), L"%s %s %s - " PROGRAM_NAME L"%s", the_board.players[COLOR_WHITE].name, LOCALIZE (L"Versus"), the_board.players[COLOR_BLACK].name, (is_registered ? L"" : LOCALIZE (L"EvaluationMode")));
1 pmbaty 676
      SetWindowText (hMainWnd, window_title); // update window title
677
   }
678
   else if (the_board.players[COLOR_WHITE].name[0] != 0)
679
   {
14 pmbaty 680
      swprintf_s (window_title, WCHAR_SIZEOF (window_title), L"%s - " PROGRAM_NAME L"%s", the_board.players[COLOR_WHITE].name, (is_registered ? L"" : LOCALIZE (L"EvaluationMode")));
1 pmbaty 681
      SetWindowText (hMainWnd, window_title); // update window title
682
   }
683
 
684
   // are we in internet mode AND are we logged in ?
685
   if ((network_player != NULL) && network_player->is_logged_in)
686
   {
687
      // are we currently playing a game ?
688
      if (network_player->is_in_game)
689
      {
690
         GUIBUTTON_ENABLE (the_scene.gui.chatbutton); // enable chat button if it's not enabled yet
691
         GUIBUTTON_DISABLE (the_scene.gui.gamesbutton); // disable games button if it was enabled
692
         GUIBUTTON_DISABLE (the_scene.gui.peoplebutton); // disable people button if it was enabled
693
         EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_RESIGN, MF_ENABLED); // allow resigning
694
      }
695
      else
696
      {
697
         GUIBUTTON_DISABLE (the_scene.gui.chatbutton); // disable chat button if it was enabled
698
         GUIBUTTON_ENABLE (the_scene.gui.gamesbutton); // enable games button if it's not enabled yet
699
         GUIBUTTON_ENABLE (the_scene.gui.peoplebutton); // enable people button if it's not enabled yet
700
         EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_RESIGN, MF_GRAYED); // disable ability to resign
701
      }
75 pmbaty 702
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_PAUSE, MF_DISABLED); // disallow pause
1 pmbaty 703
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_STATISTICS, MF_ENABLED); // enable stats
704
 
705
      enabled_value = MF_ENABLED; // remember to enable the internet-related menu items
706
   }
707
   // else it's a local or vs. computer game
708
   else
709
   {
710
      GUIBUTTON_DISABLE (the_scene.gui.chatbutton); // disable chat button if it was enabled
711
      GUIBUTTON_DISABLE (the_scene.gui.gamesbutton); // disable games button if it was enabled
712
      GUIBUTTON_DISABLE (the_scene.gui.peoplebutton); // disable people button if it was enabled
75 pmbaty 713
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_PAUSE, (the_board.move_count > 1 ? MF_ENABLED : MF_GRAYED)); // allow pause if the game has started
714
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_RESIGN, (the_board.move_count > 1 ? MF_ENABLED : MF_GRAYED)); // allow resigning if the game has started
1 pmbaty 715
      EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_STATISTICS, MF_GRAYED); // disable stats
716
 
717
      enabled_value = MF_GRAYED; // remember to disable the internet-related menu items
718
   }
719
 
720
   EnableMenuItem (GetMenu (hMainWnd), MENUID_GAME_SETUPPOSITION, !enabled_value); // note the inversion
721
   EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_SHOWONLINEPLAYERS, enabled_value);
722
   EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_SHOWSOUGHTGAMES, enabled_value);
723
   EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_SEEKGAME, enabled_value);
724
   if (!options.network.want_publicchat)
725
   {
726
      EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_CHATTERCHANNELS, MF_GRAYED); // always grayed if we don't want public chat
727
      EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_ENTERCHATTEXT, MF_GRAYED);
728
   }
729
   else
730
   {
731
      EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_CHATTERCHANNELS, enabled_value); // else enabled only in internet mode
732
      EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_ENTERCHATTEXT, enabled_value);
733
   }
734
   EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_DISPLAYPLAYERCARD, enabled_value);
735
   EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_DISPLAYYOURCARD, enabled_value);
736
   EnableMenuItem (GetMenu (hMainWnd), MENUID_INTERNET_MOTD, enabled_value);
737
 
738
   the_board.reevaluate = false; // board evaluation has been done
739
   return; // finished, new board state is known
740
}