00001
00007 #include "Windows.h"
00008
00009
00010
00011
00012
00013
00014 LRESULT CALLBACK DlgProcSetup(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
00015 static HWND hRes;
00016 static HWND hFullscreen;
00017
00018 switch (msg) {
00019 case WM_INITDIALOG:
00020 hRes = GetDlgItem(hWndDlg, IDC_RESOLUTION);
00021
00022
00023 ComboBox_AddString(hRes, "640x480");
00024
00025 ComboBox_AddString(hRes, "800x600");
00026
00027 ComboBox_AddString(hRes, "1024x768");
00028
00029 ComboBox_SetCurSel(hRes, 1);
00030
00031 hFullscreen = GetDlgItem(hWndDlg,IDC_FULLSCREEN);
00032 Button_SetCheck(hFullscreen, true);
00033
00034 return true;
00035
00036 case WM_COMMAND:
00037 switch (wParam) {
00038 case IDOK:
00039 switch (ComboBox_GetCurSel(hRes)) {
00048 case 0:
00049 g_fullscreenWidth = 640;
00050 g_fullscreenHeight = 480;
00051 g_bitsPerPixel = 32;
00052
00053 break;
00062 case 1:
00063 g_fullscreenWidth = 800;
00064 g_fullscreenHeight = 600;
00065 g_bitsPerPixel = 32;
00066
00067 break;
00076 case 2:
00077 g_fullscreenWidth = 1024;
00078 g_fullscreenHeight = 768;
00079 g_bitsPerPixel = 32;
00080
00081 break;
00082
00083 }
00090 g_windowLeft = 10;
00091 g_windowRight = g_fullscreenWidth + g_windowLeft;
00092 g_windowTop = 10;
00093 g_windowBottom = g_fullscreenHeight + g_windowTop;
00094
00095 if ((char)Button_GetCheck(hFullscreen))
00096 g_fullscreen = true;
00097 else
00098 g_fullscreen = false;
00099
00100 EndDialog(hWndDlg, 0);
00101 return true;
00102
00103 case IDCANCEL:
00104 g_running = false;
00105 PostQuitMessage(WM_QUIT);
00106 return true;
00107 }
00108 break;
00109 }
00110 return false;
00111 }
00112
00113
00114
00115
00116 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
00117 switch (message) {
00118 case WM_KEYDOWN:
00119 app->keyDown(wParam);
00120 return 0;
00121
00122 case WM_KEYUP:
00123 app->setKey(wParam, false);
00124 return 0;
00125
00126 case WM_CHAR:
00127 app->handleChar(wParam);
00128 return 0;
00129
00130 case WM_CREATE:
00131 ShowWindow(hWnd, SW_SHOW);
00132 return 0;
00133
00134 case WM_TOGGLEFULLSCREEN:
00135 g_toggleFullscreen = true;
00136 PostMessage(hWnd, WM_QUIT, 0, 0);
00137 return 0;
00138
00139 case WM_SIZE:
00140 switch (wParam) {
00141 case SIZE_MINIMIZED:
00142 g_visible = false;
00143 return 0;
00144
00145 case SIZE_MAXIMIZED:
00146 g_visible = true;
00147 app->setViewport(g_width = LOWORD(lParam), g_height = HIWORD(lParam));
00148 return 0;
00149
00150 case SIZE_RESTORED:
00151 g_visible = true;
00152 app->setViewport(g_width = LOWORD(lParam), g_height = HIWORD(lParam));
00153 return 0;
00154 }
00155 break;
00156
00157 case WM_WINDOWPOSCHANGED:
00158 if (!g_toggleFullscreen && !g_fullscreen) {
00159 g_windowLeft = ((LPWINDOWPOS)lParam)->x;
00160 g_windowRight = ((LPWINDOWPOS)lParam)->x + ((LPWINDOWPOS)lParam)->cx;
00161 g_windowTop = ((LPWINDOWPOS)lParam)->y;
00162 g_windowBottom = ((LPWINDOWPOS)lParam)->y + ((LPWINDOWPOS)lParam)->cy;
00163 }
00164 g_middleX = ((LPWINDOWPOS)lParam)->x + ((LPWINDOWPOS)lParam)->cx / 2;
00165 g_middleY = ((LPWINDOWPOS)lParam)->y + ((LPWINDOWPOS)lParam)->cy / 2;
00166 break;
00167
00168 case WM_CLOSE:
00169 g_running = false;
00170 PostMessage(hWnd, WM_QUIT, 0, 0);
00171 return 0;
00172
00173 case WM_TIMER:
00174 char title[50];
00175 sprintf(title, "DeFacto [FPS:%d]", (int)g_fps);
00176 SetWindowText(hWnd, title);
00177 return 0;
00178
00179
00180 case WM_LBUTTONDOWN:
00181 app->setMouseButton(MOUSE_LEFT, true);
00182 break;
00183
00184 case WM_LBUTTONUP:
00185 app->setMouseButton(MOUSE_LEFT, false);
00186 break;
00187
00188 case WM_RBUTTONDOWN:
00189 app->setMouseButton(MOUSE_RIGHT, true);
00190 break;
00191
00192 case WM_RBUTTONUP:
00193 app->setMouseButton(MOUSE_RIGHT, false);
00194 break;
00195
00196 default:
00197 break;
00198
00199 }
00200
00201 return (DefWindowProc(hWnd, message, wParam, lParam));
00202 }
00203
00204
00205
00206
00207 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
00208 WNDCLASSEX wc;
00209 MSG msg;
00210 bool isMessagePumpActive;
00211 bool loaded;
00212 Timer timer;
00213
00214 LOG_FILE("Log.txt");
00215
00216 initGlobals();
00217
00218 wc.cbSize = sizeof(WNDCLASSEX);
00219 wc.style = CS_HREDRAW | CS_VREDRAW;
00220 wc.lpfnWndProc = (WNDPROC)WndProc;
00221 wc.cbClsExtra = 0;
00222 wc.cbWndExtra = 0;
00223 wc.hInstance = hInstance;
00224 wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
00225 wc.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_TARGET));
00226 wc.hbrBackground = NULL;
00227 wc.lpszMenuName = NULL;
00228 wc.lpszClassName = "3DGameClass";
00229 wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
00230
00231 if (!RegisterClassEx(&wc)) {
00232 return 0;
00233 }
00234
00235 DialogBox(hInstance, MAKEINTRESOURCE(IDD_SETUP), HWND_DESKTOP, reinterpret_cast<DLGPROC>(DlgProcSetup));
00236
00237
00238 if (!g_fullscreen) {
00239 g_fullscreen = false;
00240 g_middleX = g_windowLeft + (g_windowRight-g_windowLeft)/2;
00241 g_middleY = g_windowTop + (g_windowBottom-g_windowTop)/2;
00242 }
00243 else {
00244 g_middleX = g_fullscreenWidth/2;
00245 g_middleY = g_fullscreenHeight/2;
00246 }
00247
00248 if (!app->initialize()) {
00249 LOG_ERROR(("Error initializing"));
00250 MessageBox(HWND_DESKTOP, "Error initializing (see log for more information)", "Error", MB_OK | MB_ICONEXCLAMATION);
00251 return 0;
00252 }
00253
00254 timer.init();
00255 while (g_running) {
00256 if (!app->createWindow(hInstance)) {
00257 LOG_ERROR(("Error creating window"));
00258 MessageBox(HWND_DESKTOP, "Error creating window (see log for more information)", "Error", MB_OK | MB_ICONEXCLAMATION);
00259 g_running = false;
00260 }
00261 else {
00262 if (!(loaded = app->load())) {
00263 LOG_ERROR(("Error loading"));
00264 MessageBox(HWND_DESKTOP, "Error loading (see log for more information)", "Error", MB_OK | MB_ICONEXCLAMATION);
00265 PostMessage(app->hWnd, WM_CLOSE, 0, 0);
00266 }
00267
00268
00269
00270
00271
00272
00273 isMessagePumpActive = true;
00274 while (isMessagePumpActive) {
00275 if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE) != 0) {
00276 if (msg.message != WM_QUIT) {
00277 TranslateMessage(&msg);
00278 DispatchMessage(&msg);
00279 }
00280 else
00281 isMessagePumpActive = false;
00282 }
00283 else {
00284
00285 timer.update();
00286 g_frameTime = timer.getFrameTime() * 1000.0f;
00287 g_time += g_frameTime;
00288 g_fps = timer.getFPS();
00289 g_frames++;
00290
00291 app->checkMouse();
00292 app->update();
00293
00294
00295 if (!g_visible) {
00296 WaitMessage();
00297 }
00298 else {
00299 app->drawFrame();
00300
00301 }
00302 }
00303 }
00304
00305 if (loaded) {
00306 app->unload();
00307 }
00308 }
00309
00310 app->destroyWindow();
00311
00312 if (g_toggleFullscreen) {
00313 g_fullscreen = !g_fullscreen;
00314 g_toggleFullscreen = false;
00315 }
00316
00317 }
00318
00319 app->deInitialize();
00320 delete app;
00321
00322 LOG_SUCCESS(("Average FPS: %d", (int)((float)g_frames/g_time*1000.0f)));
00323 LOG_SUCCESS(("Exiting..."));
00324 LOG_CLOSE;
00325 return msg.wParam;
00326 }