Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WINDOWS_LEAN_AND_MEAN
- #include <Windows.h>
- #undef WINDOWS_LEAN_AND_MEAN
- #include <GL/gl.h>
- #include <assert.h>
- #include <iostream>
- #include "glext.h"
- #include "wglext.h"
- //////////////////////////////////////////////////////////////////////////
- // TEST OF OPENGL-DEBUG-CONTEXT
- // to compile, switch unicode -> multi-byte and add link-input: OpenGL32.lib
- //////////////////////////////////////////////////////////////////////////
- #define INIT_ENTRY_POINT2( FUNCTYPE, FUNCNAME ) {\
- FUNCNAME = reinterpret_cast<FUNCTYPE>( wglGetProcAddress( #FUNCNAME ) ); \
- if(!FUNCNAME) { std::cerr << (#FUNCNAME) << " not initialized" << std::endl; } }
- GLboolean queryGlExtension( char *extName );
- GLboolean queryGlExtension( char *extName );
- LRESULT WINAPI WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
- HWND CreateOpenGLWindow( char* title, int x, int y, int width, int height );
- void init();
- void __stdcall DebugCallback(
- GLenum,
- GLenum,
- GLuint,
- GLenum,
- GLsizei,
- const GLchar*,
- GLvoid* )
- {
- __debugbreak();
- }
- int main() {
- init();
- // note: cause gl-error
- glEnable( GL_BLEND_EQUATION_ALPHA );
- return 0;
- }
- void init() {
- HDC m_hDC; // device context
- HGLRC m_hRC; // opengl context
- HWND m_hWnd; // window
- m_hWnd = CreateOpenGLWindow( "OpenGL", 0, 0, 640, 360 );
- if( m_hWnd == NULL) {
- exit(1);
- }
- m_hDC = GetDC(m_hWnd);
- m_hRC = wglCreateContext(m_hDC);
- wglMakeCurrent(m_hDC, m_hRC);
- PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = 0;
- INIT_ENTRY_POINT2( PFNWGLCREATECONTEXTATTRIBSARBPROC, wglCreateContextAttribsARB );
- // Clean up and delete old GL context
- wglMakeCurrent(NULL, NULL);
- wglDeleteContext( m_hRC );
- // Create a new (optionally debug/core/compatibility) context
- const int contextAttribs[] = { WGL_CONTEXT_FLAGS_ARB,
- WGL_CONTEXT_DEBUG_BIT_ARB,
- 0 };
- m_hRC = wglCreateContextAttribsARB( m_hDC, 0, contextAttribs );
- if ( m_hRC == NULL )
- {
- int err = GetLastError();
- std::cerr << err << std::endl;
- assert( false && "catastrophy!" ); // note: gently handle error
- }
- wglMakeCurrent( m_hDC, m_hRC );
- ShowWindow( m_hWnd, SW_SHOWNORMAL );
- if( !queryGlExtension( "GL_ARB_debug_output" )) {
- std::cerr << "# WARNING: GL_ARB_debug_output not supported" << std::endl;
- }
- PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackARB = 0;
- INIT_ENTRY_POINT2( PFNGLDEBUGMESSAGECALLBACKARBPROC, glDebugMessageCallbackARB );
- glDebugMessageCallbackARB( DebugCallback, 0 );
- glEnable( GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB );
- }
- //////////////////////////////////////////////////////////////////////////
- GLboolean queryGlExtension( char *extName ) {
- char *p = (char *) glGetString(GL_EXTENSIONS);
- assert( p );
- char *end = p + strlen(p);
- while (p < end) {
- size_t n = strcspn(p, " ");
- if( (strlen(extName)==n) && (strncmp(extName,p,n)==0) ) {
- return GL_TRUE;
- }
- p += (n + 1);
- }
- return GL_FALSE;
- }
- PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = 0;
- GLboolean queryWglExtension( char *extName ) {
- if ( !wglGetExtensionsStringARB ) {
- INIT_ENTRY_POINT2( PFNWGLGETEXTENSIONSSTRINGARBPROC, wglGetExtensionsStringARB );
- }
- char const *p = static_cast<char const *>( wglGetExtensionsStringARB( wglGetCurrentDC() ) );
- assert( p );
- char const *end = p + strlen(p);
- while (p < end) {
- size_t n = strcspn(p, " ");
- if ((strlen(extName)==n) && (strncmp(extName,p,n)==0)) {
- return GL_TRUE;
- }
- p += (n + 1);
- }
- return GL_FALSE;
- }
- LRESULT WINAPI WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- switch(uMsg) {
- case WM_SIZE:
- PostMessage(hWnd, WM_PAINT, 0, 0);
- return 0;
- case WM_CHAR:
- switch (wParam) {
- case 'q': case 'Q': exit(0); break;
- }
- case WM_KEYDOWN:
- switch ( wParam )
- {
- case VK_ESCAPE: PostQuitMessage(0); return 0;
- }
- return 0;
- case WM_CLOSE:
- exit(0);
- }
- return DefWindowProc( hWnd, uMsg, wParam, lParam );
- }
- HWND CreateOpenGLWindow( char* title, int x, int y, int width, int height )
- {
- WNDCLASS wc;
- static HINSTANCE hInstance = 0;
- // only register the window class once - use hInstance as a flag
- if (!hInstance) {
- hInstance = GetModuleHandle(NULL);
- wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
- wc.lpfnWndProc = (WNDPROC)WindowProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = NULL;
- wc.lpszMenuName = NULL;
- wc.lpszClassName = "OpenGL";
- //wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- if (!RegisterClass(&wc)) {
- MessageBox(NULL, "RegisterClass() failed: Cannot register window class.", "Error", MB_OK);
- return NULL;
- }
- }
- DWORD dwExStyle; // Window Extended Style
- DWORD dwStyle; // Window Style
- dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
- dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
- HWND hWnd = CreateWindowEx( dwExStyle,
- "OpenGL",
- title,
- dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
- x, y,
- width, height,
- NULL,
- NULL,
- hInstance,
- NULL);
- if (hWnd == NULL) {
- MessageBox(NULL, "CreateWindow() failed: Cannot create a window.", "Error", MB_OK);
- return NULL;
- }
- HDC hDC = GetDC(hWnd);
- PIXELFORMATDESCRIPTOR pfd;
- memset(&pfd, 0, 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.cStencilBits = 8;
- pfd.cAlphaBits = 0;
- pfd.cAccumBits = 0;
- pfd.cAccumRedBits = 0;
- pfd.cAccumGreenBits = 0;
- pfd.cAccumBlueBits = 0;
- int pf = ChoosePixelFormat(hDC, &pfd);
- if (pf == 0) {
- std::cerr << "ChoosePixelFormat() failed: Cannot find a suitable pixel format." << std::endl;
- return 0;
- }
- if (SetPixelFormat(hDC, pf, &pfd) == FALSE) {
- std::cerr << "ChoosePixelFormat() failed: Cannot find a suitable pixel format." << std::endl;
- return 0;
- }
- DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
- ReleaseDC(hWnd, hDC);
- return hWnd;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement