Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

main.cpp

Go to the documentation of this file.
00001 /***************************************************************************************************/ 00002 /***************************************************************************************************/ 00012 /***************************************************************************************************/ 00013 /***************************************************************************************************/ 00014 00015 #pragma warning(disable: 4786) 00016 00017 /*----------------------------------------------------------------*/ 00018 #include <windows.h> 00019 #include <dxerr9.h> 00020 #include <string> 00021 #include <dinput.h> 00022 00023 #include "main.h" 00024 #include "game_manager.h" 00025 #include "errrors_display.h" 00026 00027 00028 #define BITMAP_FILE "../graphs/graphs.bmp" 00029 #define MESSAGES_FILE "../graphs/messages.bmp" 00030 #define FOND_FILE "../graphs/fond.bmp" 00031 00032 00033 //la taille de l'image bitmap a charger qui contient les sprites 00034 #define TX_BITMAP 85 00035 #define TY_BITMAP 64 00036 00037 //la taille de l'image bitmap a charger qui contient les messages 00038 #define TX_MESSAGES_BMP 276 00039 #define TY_MESSAGES_BMP 190 00040 00041 00042 00043 00044 /*---------------------------------------- globals -----------------------------------------------*/ 00045 00046 CDisplay *g_pDisplay = NULL; 00047 CSurface *g_pText = NULL; 00048 CSurface *g_pLeft_Time_Text = NULL; 00049 CSurface *g_pScore_Text = NULL; 00050 CSurface *g_pSprites = NULL; 00051 CSurface *g_pMessages = NULL; 00052 CSurface *g_pFond = NULL; 00053 00054 LPDIRECTINPUT8 g_lpDI; 00055 LPDIRECTINPUTDEVICE8 g_pKeyboard ; 00056 cGame_Manager Game_Manager; 00057 HWND hWnd; 00058 Debugger Debug_File ("output.txt"); 00059 00060 00061 00062 00063 /*---------------------------------------- prototypes --------------------------------------------*/ 00064 static bool InitDD(HWND); 00065 static void CleanUp(); 00066 static void GameLoop(); 00067 static void WINAPI DI_Term(); 00068 00069 00070 /*---------------------------------------- locals --------------------------------------------*/ 00071 static bool g_bActive = false; 00072 00073 00074 00075 00076 00077 00078 00079 00080 /*--------------------------------------------------------------------------------------------------*/ 00082 /*--------------------------------------------------------------------------------------------------*/ 00083 void WINAPI DI_Term() 00084 { 00085 if (g_lpDI) 00086 { 00087 if (g_pKeyboard) 00088 { 00089 // Always unacquire device before calling Release(). 00090 g_pKeyboard->Unacquire(); 00091 g_pKeyboard->Release(); 00092 g_pKeyboard = NULL; 00093 } 00094 g_lpDI->Release(); 00095 g_lpDI = NULL; 00096 } 00097 } 00098 00099 /*--------------------------------------------------------------------------------------------------*/ 00101 /*--------------------------------------------------------------------------------------------------*/ 00102 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, 00103 WPARAM wParam, LPARAM lParam) 00104 { 00105 switch(uMsg) 00106 { 00107 case WM_SETCURSOR: 00108 SetCursor(NULL); // unable mouse cursor 00109 return true; 00110 case WM_CREATE: 00111 if( !InitDD(hWnd) ) 00112 PostQuitMessage(0); 00113 else 00114 g_bActive=true; 00115 break; 00116 case WM_CLOSE: 00117 g_bActive=false; 00118 CleanUp(); 00119 DI_Term(); 00120 DestroyWindow(hWnd); 00121 break; 00122 case WM_DESTROY: 00123 PostQuitMessage(0); 00124 break; 00125 case WM_MOVE: 00126 g_pDisplay->UpdateBounds(); 00127 break; 00128 case WM_SIZE: 00129 g_pDisplay->UpdateBounds(); 00130 break; 00131 case WM_KEYDOWN: 00132 switch(wParam) 00133 { 00134 case VK_ESCAPE: 00135 PostQuitMessage(0); 00136 break; 00137 case VK_F1 : 00138 Game_Manager.bDisplay_Square_Areas = ! Game_Manager.bDisplay_Square_Areas; 00139 break; 00140 case VK_F2: 00141 Game_Manager.bDisplay_Ennemies_Path =! Game_Manager.bDisplay_Ennemies_Path; 00142 break; 00143 case VK_F3: 00144 Game_Manager.bDisplay_Path_Graph =! Game_Manager.bDisplay_Path_Graph; 00145 break; 00146 case VK_SPACE: 00147 Game_Manager.Try_Start(); 00148 break; 00149 case VK_F5: 00150 Game_Manager.bGame_Paused = ! Game_Manager.bGame_Paused; 00151 break; 00152 case VK_F11: 00153 if( Game_Manager.Game_State == RUNNING ) 00154 Game_Manager.Game_State = LEVEL_FINISHED; 00155 break; 00156 case VK_F12 : 00157 Game_Manager.bInvincible = !Game_Manager.bInvincible; 00158 break; 00159 } 00160 break; 00161 default: 00162 return DefWindowProc(hWnd,uMsg,wParam,lParam); 00163 break; 00164 } 00165 return 0; 00166 } 00167 00168 00169 00170 00171 00172 /*--------------------------------------------------------------------------------------------------*/ 00174 /*--------------------------------------------------------------------------------------------------*/ 00175 BOOL WINAPI DI_Init(HWND hWnd , HINSTANCE g_hinst) 00176 { 00177 HRESULT hr; 00178 00179 // Create the DirectInput object. 00180 hr = DirectInput8Create(g_hinst, DIRECTINPUT_VERSION, 00181 IID_IDirectInput8, (void**)&g_lpDI, NULL); 00182 00183 if FAILED(hr) 00184 { 00185 Display_Fatal_Error(hWnd , "BOOL WINAPI DI_Init(HWND hWnd , HINSTANCE g_hinst)" , hr); 00186 return FALSE; 00187 } 00188 00189 // Retrieve a pointer to an IDirectInputDevice8 interface 00190 hr = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_pKeyboard, NULL); 00191 if FAILED(hr) 00192 { 00193 Display_Fatal_Error(hWnd , "BOOL WINAPI DI_Init(HWND hWnd , HINSTANCE g_hinst)" , hr); 00194 DI_Term(); 00195 return FALSE; 00196 } 00197 00198 // Set the data format using the predefined keyboard data 00199 hr = g_pKeyboard->SetDataFormat(&c_dfDIKeyboard); 00200 if FAILED(hr) 00201 { 00202 Display_Fatal_Error(hWnd , "BOOL WINAPI DI_Init(HWND hWnd , HINSTANCE g_hinst)" , hr); 00203 DI_Term(); 00204 return FALSE; 00205 } 00206 00207 // Set the cooperative level 00208 hr = g_pKeyboard->SetCooperativeLevel(hWnd, 00209 DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); 00210 if FAILED(hr) 00211 { 00212 Display_Fatal_Error(hWnd , "BOOL WINAPI DI_Init(HWND hWnd , HINSTANCE g_hinst)" , hr); 00213 DI_Term(); 00214 return FALSE; 00215 } 00216 00217 // Get access to the input device. 00218 hr = g_pKeyboard->Acquire(); 00219 if FAILED(hr) 00220 { 00221 Display_Fatal_Error(hWnd , "BOOL WINAPI DI_Init(HWND hWnd , HINSTANCE g_hinst)" , hr); 00222 DI_Term(); 00223 return FALSE; 00224 } 00225 00226 return TRUE; 00227 } 00228 00229 00230 00231 00232 00233 /*--------------------------------------------------------------------------------------------------*/ 00235 /*--------------------------------------------------------------------------------------------------*/ 00236 bool InitDD(HWND hWnd) 00237 { 00238 HRESULT hr; 00239 LPDIRECTDRAWPALETTE pDDPal = NULL; 00240 std::string error_message; 00241 00242 //dd init code 00243 g_pDisplay = new CDisplay(); 00244 00245 if(FAILED( hr = g_pDisplay->CreateFullScreenDisplay(hWnd, TX_SCREEN,TY_SCREEN,BIT_DEPTH) ) ) 00246 { 00247 Display_Fatal_Error(hWnd , "CreateFullScreenDisplay(hWnd, TX_SCREEN,TY_SCREEN,BIT_DEPTH)" , hr); 00248 return false; 00249 } 00250 00251 // Create and set the palette when in palettized color 00252 if( FAILED( hr = g_pDisplay->CreatePaletteFromBitmap( &pDDPal, BITMAP_FILE ) ) ) 00253 { 00254 error_message.erase(); 00255 error_message += "g_pDisplay->CreatePaletteFromBitmap( &pDDPal, "; 00256 error_message += BITMAP_FILE ; 00257 error_message += "\n"; 00258 Display_Fatal_Error(hWnd , error_message.c_str() , hr); 00259 return false; 00260 } 00261 00262 if( FAILED( hr = g_pDisplay->SetPalette( pDDPal ) )) 00263 { 00264 error_message.erase(); 00265 error_message += "g_pDisplay->SetPalette( pDDPal )\n"; 00266 Display_Fatal_Error(hWnd , error_message.c_str() , hr); 00267 return false; 00268 } 00269 00270 SAFE_RELEASE( pDDPal ); 00271 00272 // Create a surface, and draw a bitmap resource on it. 00273 if( FAILED( hr = g_pDisplay->CreateSurfaceFromBitmap( &g_pMessages, MESSAGES_FILE , TX_MESSAGES_BMP , TY_MESSAGES_BMP ) ) ) 00274 { 00275 error_message.erase(); 00276 error_message += "g_pDisplay->CreateSurfaceFromBitmap( &g_pSprites, "; 00277 error_message += MESSAGES_FILE ; 00278 error_message += ",TX_MESSAGES_BMP , TX_MESSAGES_BMP ) ) )\n"; 00279 Display_Fatal_Error(hWnd , error_message.c_str() , hr); 00280 return false; 00281 } 00282 00283 // Create a surface, and draw a bitmap resource on it. 00284 if( FAILED( hr = g_pDisplay->CreateSurfaceFromBitmap( &g_pSprites, BITMAP_FILE , TX_BITMAP , TY_BITMAP ) ) ) 00285 { 00286 error_message.erase(); 00287 error_message += "g_pDisplay->CreateSurfaceFromBitmap( &g_pSprites, "; 00288 error_message += BITMAP_FILE ; 00289 error_message += ", TX_BITMAP , TY_BITMAP ) ) )\n"; 00290 Display_Fatal_Error(hWnd , error_message.c_str() , hr); 00291 return false; 00292 } 00293 00294 if( FAILED( hr = g_pSprites->SetColorKey( 0 ) )) 00295 { 00296 error_message.erase(); 00297 error_message += "g_pSprites->SetColorKey( 0 )\n"; 00298 Display_Fatal_Error(hWnd , error_message.c_str() , hr); 00299 return false; 00300 } 00301 00302 // Create a surface, and draw a bitmap resource on it. 00303 if( FAILED( hr = g_pDisplay->CreateSurfaceFromBitmap( &g_pFond, FOND_FILE , TX_SCREEN , TY_SCREEN ) ) ) 00304 { 00305 error_message.erase(); 00306 error_message += "g_pDisplay->CreateSurfaceFromBitmap( &g_pFond, "; 00307 error_message += FOND_FILE ; 00308 error_message += ", TX_SCREEN , TY_SCREEN ) ) )\n"; 00309 Display_Fatal_Error(hWnd , error_message.c_str() , hr); 00310 return false; 00311 } 00312 00313 // Create a surface 00314 if( FAILED( hr = g_pDisplay->CreateSurface( &g_pLeft_Time_Text, 50 , 20) ) ) 00315 { 00316 error_message.erase(); 00317 error_message += "g_pDisplay->CreateSurface( &g_pLeft_Time_Text, 50 , 20) )"; 00318 Display_Fatal_Error(hWnd , error_message.c_str() , hr); 00319 return false; 00320 } 00321 00322 // Create a surface 00323 if( FAILED( hr = g_pDisplay->CreateSurface( &g_pScore_Text, 50 , 20) ) ) 00324 { 00325 error_message.erase(); 00326 error_message += "g_pDisplay->CreateSurface( &g_pScore_Text, 50 , 20) )"; 00327 Display_Fatal_Error(hWnd , error_message.c_str() , hr); 00328 return false; 00329 } 00330 00331 return true; 00332 00333 } 00334 00335 00336 00337 /*--------------------------------------------------------------------------------------------------*/ 00339 /*--------------------------------------------------------------------------------------------------*/ 00340 void CleanUp() 00341 { 00342 SAFE_DELETE(g_pDisplay); 00343 SAFE_DELETE(g_pText); 00344 SAFE_DELETE(g_pLeft_Time_Text); 00345 SAFE_DELETE(g_pScore_Text); 00346 SAFE_DELETE(g_pSprites); 00347 SAFE_DELETE(g_pMessages); 00348 SAFE_DELETE(g_pFond); 00349 } 00350 00351 00352 00353 00354 /*--------------------------------------------------------------------------------------------------*/ 00356 /*--------------------------------------------------------------------------------------------------*/ 00357 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 00358 LPSTR lpCmdLine, int iShowCmd) 00359 { 00360 WNDCLASSEX wc; 00361 MSG lpMsg; 00362 00363 wc.cbClsExtra=0; 00364 wc.cbSize=sizeof(WNDCLASSEX); 00365 wc.cbWndExtra=0; 00366 wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); 00367 wc.hCursor=LoadCursor(NULL,IDC_ARROW); 00368 wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); 00369 wc.hIconSm=LoadIcon(NULL,IDI_APPLICATION); 00370 wc.hInstance=hInstance; 00371 wc.lpfnWndProc=WndProc; 00372 wc.lpszClassName="wc"; 00373 wc.lpszMenuName=0; 00374 wc.style=CS_HREDRAW|CS_VREDRAW; 00375 if(!RegisterClassEx(&wc)) 00376 { 00377 MessageBox(NULL,"Couldn't Register Window Class", 00378 "Window Class Registration Failure",MB_OK|MB_ICONERROR); 00379 return 0; 00380 } 00381 00382 00383 hWnd = CreateWindowEx(NULL,"wc","Libell", WS_POPUPWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,TX_SCREEN,TY_SCREEN,0,0, 00384 hInstance,0); 00385 00386 if(hWnd == NULL) 00387 { 00388 MessageBox(NULL,"Failed to Create Window","Window Creation Failure", 00389 MB_OK|MB_ICONERROR); 00390 return 0; 00391 } 00392 00393 if( ! DI_Init(hWnd , hInstance) ) 00394 { 00395 MessageBox(NULL,"Failed to Initialize Direct Input","Window Creation Failure", 00396 MB_OK|MB_ICONERROR); 00397 return 0; 00398 } 00399 00400 ShowWindow(hWnd,SW_SHOW); 00401 UpdateWindow(hWnd); 00402 00403 while(lpMsg.message != WM_QUIT) 00404 { 00405 if(PeekMessage(&lpMsg,0,0,0,PM_REMOVE)) 00406 { 00407 TranslateMessage(&lpMsg); 00408 DispatchMessage(&lpMsg); 00409 } 00410 else if(g_bActive) 00411 { 00412 Game_Manager.Main_Loop(); //la boucle principale du jeu 00413 } 00414 } 00415 return lpMsg.wParam; 00416 }

Generated on Fri May 21 19:22:37 2004 for LIBELL by doxygen 1.3.7