Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Device.h
- #pragma once
- #include <Windows.h>
- typedef unsigned int GLenum;
- namespace NMGL
- {
- void PrintGLError(GLenum status);
- class Device
- {
- public:
- Device();
- ~Device();
- bool Init(HWND window);
- bool SwapBuffers();
- private:
- Device(Device const&);
- Device& operator=(Device const&);
- HWND window;
- HDC deviceContext;
- HGLRC renderingContext;
- };
- }
- // Device.cpp
- #include "Device.h"
- #include <gl\glew.h>
- #include <gl\wglew.h>
- #include <gl\GL.h>
- #pragma comment(lib, "opengl32.lib")
- #pragma comment(lib, "glu32.lib")
- #pragma comment(lib, "glew32.lib")
- namespace
- {
- int kOpenGLAttribs[] =
- {
- WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
- WGL_CONTEXT_MINOR_VERSION_ARB, 2,
- WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
- 0
- };
- } // namespace
- namespace NMGL
- {
- void PrintGLError(GLenum status)
- {
- status = glGetError();
- GLubyte const * error;
- error = new GLubyte[512];
- error = ::gluErrorString(status);
- OutputDebugStringA((LPCSTR)error);
- delete[] error;
- }
- Device::Device() : window(NULL), deviceContext(NULL), renderingContext(NULL)
- {
- }
- Device::~Device()
- {
- ::wglMakeCurrent(deviceContext,0);
- ::wglDeleteContext(renderingContext);
- ::ReleaseDC(window, deviceContext);
- }
- bool Device::Init(HWND window)
- {
- // TODO: assert window is valid, make sure get a valid device context also
- this->window = window;
- deviceContext = ::GetDC(this->window);
- PIXELFORMATDESCRIPTOR pfd = { 0 };
- pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
- pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
- pfd.iPixelType = PFD_TYPE_RGBA;
- pfd.cColorBits = 32;
- pfd.cDepthBits = 32;
- pfd.iLayerType = PFD_MAIN_PLANE;
- int pixelFormat = ::ChoosePixelFormat(deviceContext, &pfd);
- if(pixelFormat == 0)
- {
- return false;
- }
- if(SetPixelFormat(deviceContext, pixelFormat, &pfd) == FALSE)
- {
- return false;
- }
- HGLRC tempContext = ::wglCreateContext(deviceContext);
- ::wglMakeCurrent(deviceContext, tempContext);
- GLenum result = glewInit();
- if(result != GLEW_OK)
- {
- return false;
- }
- if(wglewIsSupported("WGL_ARB_create_context") == 1)
- {
- renderingContext = ::wglCreateContextAttribsARB(deviceContext, NULL, kOpenGLAttribs);
- ::wglMakeCurrent(NULL, NULL);
- ::wglDeleteContext(tempContext);
- ::wglMakeCurrent(deviceContext, renderingContext);
- }
- else
- {
- renderingContext = tempContext;
- }
- return true;
- }
- bool Device::SwapBuffers()
- {
- return ::SwapBuffers(deviceContext) == TRUE;
- }
- } // NMGL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement