Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _WINDOWS_H_
- #define _WINDOWS_H_
- #include <windows.h>
- #include <math.h>
- #define exitError exit(GetLastError())
- #ifndef _OPENGL_H_
- #define _OPENGL_H_
- #include <gl\gl.h>
- #endif
- #ifndef _KEYS_H_
- #define _KEYS_H_
- bool VKEY[256];
- #define VK_A 65
- #define VK_B 66
- #define VK_C 67
- #define VK_D 68
- #define VK_E 69
- #define VK_F 70
- #define VK_G 71
- #define VK_H 72
- #define VK_I 73
- #define VK_J 74
- #define VK_K 75
- #define VK_L 76
- #define VK_M 77
- #define VK_N 78
- #define VK_O 79
- #define VK_P 80
- #define VK_Q 81
- #define VK_R 82
- #define VK_S 83
- #define VK_T 84
- #define VK_U 85
- #define VK_V 86
- #define VK_W 87
- #define VK_X 88
- #define VK_Y 89
- #define VK_Z 90
- #endif
- POINT MOUSE;
- class glWindow {
- public:
- glWindow(HINSTANCE, int, const char*, const char*, int, int, int, bool);
- // glWindow(handle, min|max, window title, class name, height, width, depth, fullscreen);
- ~glWindow();
- void Frustum(GLdouble, GLdouble, GLdouble, GLdouble);
- void SwapBuffer();
- static LRESULT CALLBACK WndProcS(HWND, UINT, WPARAM, LPARAM);
- bool ACTIVE; //minimize flag
- bool FULLSCREEN;
- private:
- WNDCLASSEX hWC;
- HGLRC hRC;
- HWND hWnd;
- HDC hDC;
- HINSTANCE hInstance;
- const char* szClass;
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- void ResizeWindow(int, int); // ResizeWindow(height, width);
- };
- glWindow::glWindow(HINSTANCE f_hInstance, int nCmdShow, const char* szTitle, const char* f_szClass, int f_iWidth, int f_iHeight, int iBits, bool f_bFullscreen) {
- PIXELFORMATDESCRIPTOR pFD;
- int PixelFormat;
- RECT hRect;
- DWORD dwExStyle;
- DWORD dwStyle;
- //Creates windows class with OpenGL
- hWC.cbSize = sizeof(WNDCLASSEX);
- hWC.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
- hWC.lpfnWndProc = WndProcS;
- hWC.cbClsExtra = 0;
- hWC.cbWndExtra = 0;
- hWC.hInstance = f_hInstance;
- hInstance = f_hInstance;
- hWC.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- hWC.hCursor = LoadCursor(NULL, IDC_ARROW);
- hWC.hbrBackground = NULL;
- hWC.lpszMenuName = NULL;
- hWC.lpszClassName = f_szClass;
- szClass = f_szClass;
- hWC.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- hRect.left = (long) 0;
- hRect.right = (long) f_iWidth;
- hRect.top = (long) 0;
- hRect.bottom = (long) f_iHeight;
- //Fullscreen logic
- FULLSCREEN = f_bFullscreen;
- if(FULLSCREEN) {
- DEVMODE dmScreenSettings;
- memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
- dmScreenSettings.dmSize = sizeof(dmScreenSettings); // Size Of The Devmode Structure
- dmScreenSettings.dmPelsWidth = f_iWidth;
- dmScreenSettings.dmPelsHeight = f_iHeight;
- dmScreenSettings.dmBitsPerPel = iBits;
- dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
- if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
- if (MessageBox(NULL,"Fullscreen is not supported by\nyour video card. Use Windowed Mode instead?","ERROR",MB_YESNO|MB_ICONEXCLAMATION)==IDYES) {
- FULLSCREEN = false;
- dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
- dwStyle = WS_OVERLAPPEDWINDOW;
- }
- else { exitError; }
- }
- else {
- dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
- dwStyle=WS_POPUP; // Windows Style
- ShowCursor(FALSE); // Hide Mouse Pointer
- }
- }
- else {
- dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
- dwStyle = WS_OVERLAPPEDWINDOW;
- }
- if(!RegisterClassEx(&hWC)) { exitError; }
- hWnd = CreateWindowEx(dwExStyle,
- f_szClass,
- szTitle,
- dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
- 0, 0,
- hRect.right-hRect.left,
- hRect.bottom-hRect.top,
- NULL,
- NULL,
- f_hInstance,
- NULL);
- if(!hWnd) { MessageBox(NULL,"Cannot create window.","ERROR",MB_OK|MB_ICONSTOP); exitError; }
- if(!(hDC = GetDC(hWnd))) { MessageBox(NULL,"Cannot get DC.","ERROR",MB_OK|MB_ICONSTOP); exitError; }
- //pixel format descriptor for device context
- ZeroMemory(&pFD, sizeof(pFD));
- pFD.nSize = sizeof(pFD);
- pFD.nVersion = 1;
- pFD.dwFlags = PFD_DRAW_TO_WINDOW |
- PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
- pFD.iPixelType = PFD_TYPE_RGBA;
- pFD.cColorBits = 32;
- pFD.cDepthBits = 24;
- pFD.iLayerType = PFD_MAIN_PLANE;
- if(!SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pFD), &pFD)) { exitError; }
- if(!(hRC = wglCreateContext(hDC))) { exitError; }
- if(!wglMakeCurrent(hDC, hRC)) { exitError; }
- ShowWindow(hWnd, nCmdShow);
- SetForegroundWindow(hWnd);
- SetFocus(hWnd);
- ACTIVE = true;
- //initial opengl drawing setup
- ResizeWindow(f_iWidth, f_iHeight);
- glClearDepth(1.0f);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
- }
- glWindow::~glWindow() {
- if(FULLSCREEN) { ChangeDisplaySettings(NULL, 0); ShowCursor(TRUE); }
- if(!wglMakeCurrent(NULL, NULL)) exitError;
- if(!wglDeleteContext(hRC)) exitError;
- if( hDC && !ReleaseDC(hWnd, hDC)) exitError;
- if(hWnd && !DestroyWindow(hWnd)) exitError;
- if(!UnregisterClass(szClass,hInstance)) exitError;
- }
- //this static WndProc returns the private member WndProc
- LRESULT CALLBACK glWindow::WndProcS(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
- glWindow *wnd = 0;
- wnd = reinterpret_cast<glWindow *>(::GetWindowLong(hwnd, GWL_USERDATA));
- return wnd->WndProc(hwnd, message, wparam, lparam);
- }
- LRESULT CALLBACK glWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
- switch(uMsg) {
- case WM_KEYDOWN: {
- VKEY[wParam] = true;
- break;
- }
- case WM_KEYUP: {
- VKEY[wParam] = false;
- break;
- }
- case WM_SIZE: {
- ResizeWindow(LOWORD(lParam), HIWORD(lParam));
- break;
- }
- case WM_CLOSE: {
- PostQuitMessage(0);
- break;
- }
- default: {
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
- }
- return 0;
- }
- void glWindow::ResizeWindow(GLsizei width, GLsizei height) {
- if (height==0) { height=1; } //prevent Divide-By-Zero
- glViewport(0,0,width,height);
- glMatrixMode(GL_PROJECTION); //set up aspect ratio
- glLoadIdentity();
- Frustum(65.0f,(float) width/ (float) height, 0.1f, 1000.0f);
- glMatrixMode(GL_MODELVIEW); //switch back to model to continue drawing
- glLoadIdentity();
- }
- void glWindow::Frustum(GLdouble fovY, GLdouble aspect, GLdouble zNear, GLdouble zFar) {
- GLdouble fW, fH;
- fH = tan( fovY / 360 * PI ) * zNear;
- fW = fH * aspect;
- glFrustum( -fW, fW, -fH, fH, zNear, zFar );
- }
- void glWindow::SwapBuffer(void) {
- SwapBuffers(hDC);
- //this is pretty much only here to keep OOP consistent (hDC is private). notice there is no 's' at the end
- //of the member function
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment