Advertisement
Guest User

Untitled

a guest
May 30th, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. // Device.h
  2.  
  3. #pragma once
  4.  
  5. #include <Windows.h>
  6.  
  7. typedef unsigned int GLenum;
  8.  
  9. namespace NMGL
  10. {
  11.  
  12. void PrintGLError(GLenum status);
  13.  
  14. class Device
  15. {
  16. public:
  17.     Device();
  18.     ~Device();
  19.     bool Init(HWND window);
  20.     bool SwapBuffers();
  21.  
  22. private:
  23.     Device(Device const&);
  24.     Device& operator=(Device const&);
  25.  
  26.     HWND window;
  27.     HDC deviceContext;
  28.     HGLRC renderingContext;
  29. };
  30.  
  31. }
  32.  
  33. // Device.cpp
  34.  
  35. #include "Device.h"
  36.  
  37. #include <gl\glew.h>
  38. #include <gl\wglew.h>
  39. #include <gl\GL.h>
  40.  
  41. #pragma comment(lib, "opengl32.lib")
  42. #pragma comment(lib, "glu32.lib")
  43. #pragma comment(lib, "glew32.lib")
  44.  
  45. namespace
  46. {
  47.  
  48. int kOpenGLAttribs[] =
  49. {
  50.     WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  51.     WGL_CONTEXT_MINOR_VERSION_ARB, 2,
  52.     WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
  53.     0
  54. };
  55.  
  56. } // namespace
  57.  
  58. namespace NMGL
  59. {
  60.  
  61. void PrintGLError(GLenum status)
  62. {
  63.     status = glGetError();
  64.     GLubyte const * error;
  65.     error = new GLubyte[512];
  66.     error = ::gluErrorString(status);
  67.     OutputDebugStringA((LPCSTR)error);
  68.     delete[] error;
  69. }
  70.  
  71. Device::Device() : window(NULL), deviceContext(NULL), renderingContext(NULL)
  72. {
  73. }
  74.  
  75. Device::~Device()
  76. {
  77.     ::wglMakeCurrent(deviceContext,0);
  78.     ::wglDeleteContext(renderingContext);
  79.     ::ReleaseDC(window, deviceContext);
  80. }
  81.  
  82. bool Device::Init(HWND window)
  83. {
  84.     // TODO: assert window is valid, make sure get a valid device context also
  85.     this->window = window;
  86.     deviceContext = ::GetDC(this->window);
  87.  
  88.     PIXELFORMATDESCRIPTOR pfd = { 0 };
  89.     pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  90.     pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
  91.     pfd.iPixelType = PFD_TYPE_RGBA;
  92.     pfd.cColorBits = 32;
  93.     pfd.cDepthBits = 32;
  94.     pfd.iLayerType = PFD_MAIN_PLANE;
  95.  
  96.     int pixelFormat = ::ChoosePixelFormat(deviceContext, &pfd);
  97.     if(pixelFormat == 0)
  98.     {
  99.         return false;
  100.     }
  101.  
  102.     if(SetPixelFormat(deviceContext, pixelFormat, &pfd) == FALSE)
  103.     {
  104.         return false;
  105.     }
  106.  
  107.     HGLRC tempContext = ::wglCreateContext(deviceContext);
  108.     ::wglMakeCurrent(deviceContext, tempContext);
  109.  
  110.     GLenum result = glewInit();
  111.     if(result != GLEW_OK)
  112.     {
  113.         return false;
  114.     }
  115.  
  116.     if(wglewIsSupported("WGL_ARB_create_context") == 1)
  117.     {
  118.         renderingContext = ::wglCreateContextAttribsARB(deviceContext, NULL, kOpenGLAttribs);
  119.         ::wglMakeCurrent(NULL, NULL);
  120.         ::wglDeleteContext(tempContext);
  121.         ::wglMakeCurrent(deviceContext, renderingContext);
  122.     }
  123.     else
  124.     {
  125.         renderingContext = tempContext;
  126.     }
  127.  
  128.     return true;
  129. }
  130.  
  131. bool Device::SwapBuffers()
  132. {
  133.     return ::SwapBuffers(deviceContext) == TRUE;
  134. }
  135.  
  136. } // NMGL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement