Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

OpenGL Version Check Windows Visual Studio No Dependencies

, 2 Oct 2013
Rate this:
Please Sign up or sign in to vote.
This fully copy pasteable program shows the version of OpenGL your Windows OS supports.

Introduction

This fully copy pasteable program shows the version of OpenGL your Windows OS supports. It is useful because you don't need to include any libraries or external dependencies other than the ones that Windows already contains. This tip is aimed for Visual Studio users. It creates an OpenGL rendering context, and displays the version number of your supported OpenGL version in a MessageBox for you.

Background

I wrote the same code snippet here, but I thought it would be good to put on CodeProject as well in case someone just quickly wanted to find out their OpenGL version:

Using the Code

Create a new project in Visual Studio, choose Win32 project, choose empty project. Add a new file, main.cpp, copy paste the following code into it and press F5 to run, you should see the version number your Windows OS supports in a message box popup.

#include <windows.h>
#include <GL/GL.h>

#pragma comment (lib, "opengl32.lib")

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, 
             __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{
    MSG msg          = {0};
    WNDCLASS wc      = {0}; 
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
    wc.lpszClassName = L"oglversionchecksample";
    wc.style = CS_OWNDC;
    if( !RegisterClass(&wc) )
        return 1;
    CreateWindowW(wc.lpszClassName,L"openglversioncheck", 
            WS_OVERLAPPEDWINDOW|WS_VISIBLE,0,0,640,480,0,0,hInstance,0); 

    while( GetMessage( &msg, NULL, 0, 0 ) > 0 )
        DispatchMessage( &msg );

    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case WM_CREATE:
        {
        PIXELFORMATDESCRIPTOR pfd =
        {
            sizeof(PIXELFORMATDESCRIPTOR),
            1,
            PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,    //Flags
            PFD_TYPE_RGBA,            //The kind of framebuffer. RGBA or palette.
            32,                        //Colordepth of the framebuffer.
            0, 0, 0, 0, 0, 0,
            0,
            0,
            0,
            0, 0, 0, 0,
            24,                        //Number of bits for the depthbuffer
            8,                        //Number of bits for the stencilbuffer
            0,                        //Number of Aux buffers in the framebuffer.
            PFD_MAIN_PLANE,
            0,
            0, 0, 0
        };

        HDC ourWindowHandleToDeviceContext = GetDC(hWnd);

        int  letWindowsChooseThisPixelFormat;
        letWindowsChooseThisPixelFormat = ChoosePixelFormat(ourWindowHandleToDeviceContext, &pfd); 
        SetPixelFormat(ourWindowHandleToDeviceContext,letWindowsChooseThisPixelFormat, &pfd);

        HGLRC ourOpenGLRenderingContext = wglCreateContext(ourWindowHandleToDeviceContext);
        wglMakeCurrent (ourWindowHandleToDeviceContext, ourOpenGLRenderingContext);

        MessageBoxA(0,(char*) glGetString(GL_VERSION), "OPENGL VERSION",0);

        wglDeleteContext(ourOpenGLRenderingContext);
        PostQuitMessage(0);
        }
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;

}

Points of Interest

It was interesting to note that just simply calling wgl commands was not enough, and the window had to be created at least in WM_CREATE until the version checkup would work.

History

  • 3.10.2013 + Fixed the OpenGL Wikipedia link missing one letter in the < A > tag
  • 2.10.2013 + First version submitted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jani Mäkinen
Software Developer Bugbear Entertainment
Finland Finland
I've programmed since the 90's QBasic times and love programming tight and slick code.
 
Here are some links for my 'off the clock' programming: http://www.gfx0.com
 
Specialties: C, C++, Game Programming
 
I was born in the middle of Finland into a small farming town. Moved to Helsinki, then to Germany and am now back in Helsinki programming as my dayjob in the Game Industry Smile | :)
Follow on   Twitter   LinkedIn

Comments and Discussions

 
QuestionConverting to VB.NET PinmemberN. Henrik Lauridsen4-Oct-13 1:10 
AnswerRe: Converting to VB.NET [modified] PinmemberJani Mäkinen5-Oct-13 10:02 
GeneralMy vote of 5 Pinmemberlittlewater2-Oct-13 14:07 
GeneralRe: My vote of 5 PinmemberJani Mäkinen2-Oct-13 22:18 
GeneralRe: My vote of 5 Pinmemberlittlewater8-Oct-13 20:35 
GeneralRe: My vote of 5 PinmemberJani Mäkinen9-Oct-13 21:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 3 Oct 2013
Article Copyright 2013 by Jani Mäkinen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid