//----------------------------------------------------------------- // Game Engine Object // C++ Source - GameEngine.cpp //----------------------------------------------------------------- //----------------------------------------------------------------- // Include Files //----------------------------------------------------------------- #include "GameEngine.h" #include "Shlobj.h" //----------------------------------------------------------------- // Static Variable Initialization //----------------------------------------------------------------- GameEngine *GameEngine::m_pGameEngine = NULL; //----------------------------------------------------------------- // Windows Functions //----------------------------------------------------------------- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MSG msg; static int iTickTrigger = 0; int iTickCount; int iRestCounter=0; if (GameInitialize(hInstance)) { // Initialize the game engine if (!GameEngine::GetEngine()->Initialize(iCmdShow)) return FALSE; // Enter the main message loop HACCEL hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)106); while ( GetMessage(&msg, NULL, 0, 0) ) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { // Process the message if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } { // Make sure the game engine isn't sleeping if (!GameEngine::GetEngine()->GetSleep()) { // Check the tick count to see if a game cycle has elapsed iTickCount = GetTickCount(); if (iTickCount > iTickTrigger) { iRestCounter++; iTickTrigger = iTickCount + GameEngine::GetEngine()->GetFrameDelay(); //HandleKeys(); GameCycle(); if(iRestCounter==10){ //GameEngine::GetEngine()->SetSleep(true); iRestCounter=0; } } } } } //GameEngine::GetEngine()->SetLast(); return (int)msg.wParam; } // End the game GameEnd(); return TRUE; } LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam) { // Route all Windows messages to the game engine return GameEngine::GetEngine()->HandleEvent(hWindow, msg, wParam, lParam); } //----------------------------------------------------------------- // GameEngine Constructor(s)/Destructor //----------------------------------------------------------------- GameEngine::GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass, LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth, int iHeight) { // Set the member variables for the game engine m_pGameEngine = this; m_hInstance = hInstance; m_hWindow = NULL; if (lstrlen(szWindowClass) > 0) lstrcpy(m_szWindowClass, szWindowClass); if (lstrlen(szTitle) > 0) lstrcpy(m_szTitle, szTitle); m_wIcon = wIcon; m_wSmallIcon = wSmallIcon; m_iWidth = iWidth; m_iHeight = iHeight; m_iFrameDelay = 50; // 20 FPS default m_bSleep = TRUE; bIsMenuExpanded=false; menuSystemOpened=false; menuSystemMouseDown=false; WhoOpened=0; bMouseDown=false; } GameEngine::~GameEngine() { } //----------------------------------------------------------------- // Game Engine General Methods //----------------------------------------------------------------- BOOL GameEngine::Initialize(int iCmdShow) { WNDCLASSEX wndclass; // Create the window class for the main window wndclass.cbSize = sizeof(wndclass); wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = m_hInstance; wndclass.hIcon = LoadIcon(m_hInstance, MAKEINTRESOURCE(GetIcon())); wndclass.hIconSm = LoadIcon(m_hInstance, MAKEINTRESOURCE(GetSmallIcon())); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = m_szWindowClass; // Register the window class if (!RegisterClassEx(&wndclass)){ return FALSE; } // Calculate the window size and position based upon the game size int cyScreen=GetSystemMetrics(SM_CYSCREEN); int iWindowWidth = m_iWidth + GetSystemMetrics(SM_CXFIXEDFRAME) * 2, iWindowHeight = m_iHeight + GetSystemMetrics(SM_CYFIXEDFRAME) * 2 + ((cyScreen>600)?GetSystemMetrics(SM_CYCAPTION):0); if (wndclass.lpszMenuName != NULL) iWindowHeight += GetSystemMetrics(SM_CYMENU); int iXWindowPos = (GetSystemMetrics(SM_CXSCREEN) - iWindowWidth) / 2, iYWindowPos = (GetSystemMetrics(SM_CYSCREEN) - iWindowHeight) / 2; // Create the window if(cyScreen<600) MessageBox(m_hWindow,"The Application works better in 800*600 or more resolution","Info",MB_OK); m_hWindow = CreateWindow(m_szWindowClass, m_szTitle, WS_POPUPWINDOW | ((cyScreen>600)?(WS_CAPTION | WS_MINIMIZEBOX|WS_CLIPCHILDREN):(int)0), iXWindowPos, iYWindowPos, iWindowWidth, iWindowHeight, NULL, NULL, m_hInstance, NULL); if (!m_hWindow) return FALSE; // Show and update the window ShowWindow(m_hWindow, iCmdShow); UpdateWindow(m_hWindow); *szReadingFile=0; // set the folder char szPrimarayPath[128]; if(!SHGetSpecialFolderPath( m_hWindow, szPrimarayPath, CSIDL_PERSONAL , 0)){return FALSE; } wsprintf(szProfilePath,"%s\\Organic Chemistry",szPrimarayPath); //MessageBox(m_hWindow,szPath,"Error!",MB_OK); SECURITY_ATTRIBUTES securityAttributes ={sizeof(SECURITY_ATTRIBUTES),NULL,FALSE}; CreateDirectory(szProfilePath,&securityAttributes); GetSettings(); char szPath[128]; // set the font if(!SHGetSpecialFolderPath( m_hWindow, szPath, CSIDL_FONTS, 0)){ MessageBox(m_hWindow,"Your font folder cant be identified! Please install the sinhala font manually..","Error!",MB_OK); return TRUE; //though continue } int ilength=strlen(szPath); szPath[ilength+1]='/0'; char szFileName[128]; wsprintf(szFileName,"%s\\DL-Paras.ttf",szPath); HANDLE hFile = CreateFile(szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); if(hFile==INVALID_HANDLE_VALUE){ SHFILEOPSTRUCT shFileOp={m_hWindow, FO_COPY, "DL-Paras.ttf", szPath, FOF_SIMPLEPROGRESS , ilength, NULL,NULL}; ShellExecute(GameEngine::GetEngine()->GetWindow(), "explore", szPath, NULL, NULL, SW_MINIMIZE); if(SHFileOperation(&shFileOp)) MessageBox(m_hWindow,"The Program cannot Install the Sinhala font! Please do it manually..","Error!",MB_OK); } CloseHandle(hFile); return TRUE; } LRESULT GameEngine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam) { // Route Windows messages to game engine member functions switch (msg) { case WM_CREATE: // Set the game window and start the game SetWindow(hWindow); GameStart(hWindow); return 0; case WM_ACTIVATE: // Activate/deactivate the game and update the Sleep status if (wParam != WA_INACTIVE) { GameActivate(hWindow); SetSleep(false); } else { GameDeactivate(hWindow); SetSleep(TRUE); } return 0; case WM_PAINT: HDC hDC; PAINTSTRUCT ps; hDC = BeginPaint(hWindow, &ps); // Paint the game GamePaint(hDC); EndPaint(hWindow, &ps); return 0; case WM_DESTROY: // End the game and exit the application GameEnd(); PostQuitMessage(0); return 0; case WM_LBUTTONDOWN: // Handle left mouse button press MouseButtonDown(LOWORD(lParam), HIWORD(lParam), TRUE); return 0; case WM_LBUTTONUP: // Handle left mouse button release MouseButtonUp(LOWORD(lParam), HIWORD(lParam), true); return 0; case WM_RBUTTONDOWN: // Handle right mouse button press MouseButtonDown(LOWORD(lParam), HIWORD(lParam), FALSE); return 0; case WM_RBUTTONUP: // Handle right mouse button release MouseButtonUp(LOWORD(lParam), HIWORD(lParam), false); return 0; case WM_MOUSEMOVE: // Handle mouse movement MouseMove(LOWORD(lParam), HIWORD(lParam)); return 0; } return DefWindowProc(hWindow, msg, wParam, lParam); } void GameEngine::ErrorQuit(LPTSTR szErrorMsg) { MessageBox(GetWindow(), szErrorMsg, TEXT("Critical Error"), MB_OK | MB_ICONERROR); PostQuitMessage(0); } void GameEngine::GetSettings() { char szFileName[128]; wsprintf(szFileName,"%s\\Config.txt",szProfilePath); HANDLE hFile = CreateFile(szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); if(hFile==INVALID_HANDLE_VALUE){ isEnglish=true; isSoundOn=true; bSaveRecords=true; bSaveLastView=true; strcpy(szStudentName,"New Student"); strcpy(szLastExercise,"AlkaneRevision1"); strcpy(szLastLesson,"intro"); *szRecordString=0; CloseHandle(hFile); //MessageBox(GetWindow(),"Hello","hi",MB_OK); SetSettings(); GetSettings(); return; } DWORD dwBytesRead; if (!(ReadFile(hFile, &isEnglish, 1, &dwBytesRead, NULL)&& ReadFile(hFile, &isSoundOn, 1, &dwBytesRead, NULL)&& ReadFile(hFile, &bSaveRecords, 1, &dwBytesRead, NULL)&& ReadFile(hFile, &bSaveLastView, 1, &dwBytesRead, NULL)&& ReadFile(hFile,szStudentName,128,&dwBytesRead,NULL)&& ReadFile(hFile,szLastExercise,128,&dwBytesRead,NULL)&& ReadFile(hFile,szLastLesson,128,&dwBytesRead,NULL)&& ReadFile(hFile,szRecordString,1024,&dwBytesRead,NULL))) { // Something went wrong, so close the file handle MessageBox(GetWindow(),"Error in Reading Data","Error",MB_OK); CloseHandle(hFile); return ; } //MessageBox(GetWindow(),szLastLesson,"Error",MB_OK); CloseHandle(hFile); } bool GameEngine::SetSettings() { char szFileName[128]; wsprintf(szFileName,"%s\\Config.txt",szProfilePath); HANDLE hFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); /* if(hFile==INVALID_HANDLE_VALUE){ CloseHandle(hFile); //SetSttings(); return false; }*/ DWORD dwBytesWritten; if (!(WriteFile(hFile, &isEnglish, 1, &dwBytesWritten, NULL)&& WriteFile(hFile, &isSoundOn, 1, &dwBytesWritten, NULL)&& WriteFile(hFile, &bSaveRecords, 1, &dwBytesWritten, NULL)&& WriteFile(hFile, &bSaveLastView, 1, &dwBytesWritten, NULL)&& WriteFile(hFile,szStudentName,128,&dwBytesWritten,NULL)&& WriteFile(hFile,szLastExercise,128,&dwBytesWritten,NULL)&& WriteFile(hFile,szLastLesson,128,&dwBytesWritten,NULL)&& WriteFile(hFile,szRecordString,1024,&dwBytesWritten,NULL))) { // Something went wrong, so close the file handle MessageBox(GetWindow(),"Error in Writing data","Error!",MB_OK); CloseHandle(hFile); return false; } //GetSettings(); CloseHandle(hFile); return true; } void GameEngine::SetValuesForSettings(bool isEnglish, bool isSoundOn, bool bSaveRecords, bool bSaveLastView, char *szStudentName) { GameEngine::isEnglish=isEnglish; GameEngine::isSoundOn=isSoundOn; GameEngine::bSaveRecords=bSaveRecords; GameEngine::bSaveLastView=bSaveLastView; strcpy(GameEngine::szStudentName,szStudentName); SetSettings(); }