// // FUNCTION: InitInstance(HINSTANCE, int) // // PURPOSE: Saves instance handle and creates main window // // COMMENTS: // // In this function, we save the instance handle in a global variable and // create and display the main program window. // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { //HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, ///*WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME| //WS_MINIMIZEBOX|WS_CLIPSIBLINGS|WS_CLIPCHILDREN*/ WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, SCREEN_WIDTH + GetSystemMetrics(SM_CXFIXEDFRAME) * 2 + GetSystemMetrics(SM_CXBORDER) * 2 , SCREEN_HEIGHT + GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYMENU) + GetSystemMetrics(SM_CYFIXEDFRAME) * 2 + GetSystemMetrics(SM_CYBORDER) * 2, NULL, NULL, hInstance, NULL); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); ///////////////////////////////////////////////////////////////////////// if(!InitOGLES()) //OpenGL ES Initialization { MessageBoxA(hWnd,"OpenGL ES init error.","Error",MB_OK | MB_ICONERROR); return false; } return TRUE; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSEX wc; hInst = hInstance; bool done = false; HBRUSH hbrColorKey = CreateSolidBrush(g_KeyColor); if (hWnd = FindWindow(appName, appName)) { return 0; } wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC) WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hIcon = LoadIcon(NULL, NULL); wc.hCursor = LoadCursor(NULL, NULL); wc.hbrBackground = hbrColorKey; wc.lpszMenuName = NULL; wc.lpszClassName = appName; wc.hInstance = hInstance; wc.hIconSm = LoadIcon(NULL, NULL); if (!RegisterClassEx(&wc)) { return false; } hWnd = CreateWindow(appName, appName, WS_POPUP | /*WS_CLIPCHILDREN | WS_CLIPSIBLINGS |*/ WS_VISIBLE | /*WS_OVERLAPPED |*/ WS_CAPTION | WS_SYSMENU | /*WS_THICKFRAME |*/ WS_MINIMIZEBOX | WS_MAXIMIZEBOX, x, y, w, h, NULL, NULL, hInst, NULL); if (!hWnd) { return false; } if (!InitOGLES()) { MessageBox(hWnd, "OpenGL ES init error.", "Error", MB_OK | MB_ICONERROR); return false; } // Bring the window to front, focus it and refresh it SetWindowText(hWnd, appName); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); game.init(w, h); while (!done) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { switch (msg.message) { case WM_MOUSEMOVE: Construct::Input::setPointerState(GET_X_LPARAM(msg.lParam), GET_Y_LPARAM(msg.lParam)); break; case WM_LBUTTONDOWN: Construct::Input::setButtonState(Construct::Input::B_LEFT, true); break; case WM_LBUTTONUP: Construct::Input::setButtonState(Construct::Input::B_LEFT, false); break; case WM_RBUTTONDOWN: Construct::Input::setButtonState(Construct::Input::B_RIGHT, true); break; case WM_RBUTTONUP: Construct::Input::setButtonState(Construct::Input::B_RIGHT, false); break; case WM_QUIT: done = true; break; case WM_KEYDOWN: switch (msg.wParam) { case VK_LEFT: Construct::Input::setKeyState(Construct::Input::K_LEFT, true); break; case VK_RIGHT: Construct::Input::setKeyState(Construct::Input::K_RIGHT, true); break; case VK_UP: Construct::Input::setKeyState(Construct::Input::K_UP, true); break; case VK_RETURN: Construct::Input::setKeyState(Construct::Input::K_RETURN, true); break; } break; case WM_KEYUP: switch (msg.wParam) { case VK_LEFT: Construct::Input::setKeyState(Construct::Input::K_LEFT, false); break; case VK_RIGHT: Construct::Input::setKeyState(Construct::Input::K_RIGHT, false); break; case VK_UP: Construct::Input::setKeyState(Construct::Input::K_UP, false); break; case VK_RETURN: Construct::Input::setKeyState(Construct::Input::K_RETURN, false); break; } break; default: TranslateMessage(&msg); DispatchMessage(&msg); break; } } Render(); } //_CrtDumpMemoryLeaks(); DestroyWindow(hWnd); UnregisterClass(appName, hInst); return 0; }
/*This is the WinMain function. Here we will create the rendering window, initialize OpenGL ES, write the message loop, and, at the end, clean all and release all used resources*/ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { MSG msg; //This is the message variable for the message loop WNDCLASS wc; /*This structure will hold some init values for our window*/ hInst = hInstance; // Initialization of our global variable bool done = FALSE; /*This block of code is to ensure that the user only can run one instance of the application. First we search for a window with the same class name, if found, we will focus it and return*/ if(hWnd = FindWindow(szAppName, szAppName)) { /* Set focus to foremost child window. The "| 0x01" is used to bring any owned windows to the foreground and activate them.*/ SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001)); return 0; } wc.style = CS_HREDRAW | CS_VREDRAW; /*Will force a redraw if the window is resized, both horizontally or vertically*/ wc.lpfnWndProc = (WNDPROC) WndProc; /*this is a function pointer, to tell the OS what function should call when a message needs to be processed*/ wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(hInstance, NULL);//Load default icon wc.hCursor = 0; // Default cursor wc.hbrBackground = 0; //We don't care about the background color wc.lpszMenuName = NULL; //This application does not have a menu wc.lpszClassName = szAppName; /*Important, here we must fill the application class name (the class name is not the same than the caption of the window, but many times they are the same)*/ //Before creating the window, we must register this new window class if(!RegisterClass(&wc)) return FALSE; hWnd=CreateWindow(szAppName, //Class Name szAppName, //Caption string WS_VISIBLE,//Window style CW_USEDEFAULT,CW_USEDEFAULT,//Starting [x,y] pos. CW_USEDEFAULT, CW_USEDEFAULT, //Width and height NULL, NULL, //Parent window and menu handle hInst, NULL); /*Instance handle. Custom value to pass in the creation with the WM_CREATE message*/ if(!hWnd) return FALSE; if(!InitOGLES()) return FALSE; //OpenGL ES Initialization //Bring the window to front, focus it and refresh it ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); //Message Loop while(!done) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if(msg.message==WM_QUIT) done=TRUE; else { TranslateMessage(&msg); DispatchMessage(&msg); } } else Render(); } //Clean up all Clean(); return 0; }