Jump to content
  • Advertisement
  • 07/25/99 06:10 PM
    Sign in to follow this  

    Example of the Minimal Win32 & OpenGL program

    Graphics and GPU Programming

    Myopic Rhino
    /* An example of the minimal Win32 & OpenGL program. It only works in
    16 bit color modes or higher (since it doesn't create a
    palette). */

    #include /* must include this before GL/gl.h */
    #include /* OpenGL header file */
    #include /* OpenGL utilities header file */

    /* rotate a triangle around */
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex2i(0, 1);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex2i(-1, -1);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex2i(1, -1);

    WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    static PAINTSTRUCT ps;

    switch(uMsg) {
    case WM_PAINT:
    BeginPaint(hWnd, &ps);
    EndPaint(hWnd, &ps);
    return 0;

    case WM_SIZE:
    glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
    PostMessage(hWnd, WM_PAINT, 0, 0);
    return 0;

    case WM_CHAR:
    switch (wParam) {
    case 27: /* ESC key */
    return 0;

    case WM_CLOSE:
    return 0;

    return DefWindowProc(hWnd, uMsg, wParam, lParam);

    CreateOpenGLWindow(char* title, int x, int y, int width, int height,
    BYTE type, DWORD flags)
    int pf;
    HDC hDC;
    HWND hWnd;
    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_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";

    if (!RegisterClass(&wc)) {
    MessageBox(NULL, "RegisterClass() failed: "
    "Cannot register window class.", "Error", MB_OK);
    return NULL;

    hWnd = CreateWindow("OpenGL", title, WS_OVERLAPPEDWINDOW |
    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 = GetDC(hWnd);

    /* there is no guarantee that the contents of the stack that become
    the pfd are zeroed, therefore _make sure_ to clear these bits. */
    memset(&pfd, 0, sizeof(pfd));
    pfd.nSize = sizeof(pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | flags;
    pfd.iPixelType = type;
    pfd.cColorBits = 32;

    pf = ChoosePixelFormat(hDC, &pfd);
    if (pf == 0) {
    MessageBox(NULL, "ChoosePixelFormat() failed: "
    "Cannot find a suitable pixel format.", "Error", MB_OK);
    return 0;

    if (SetPixelFormat(hDC, pf, &pfd) == FALSE) {
    MessageBox(NULL, "SetPixelFormat() failed: "
    "Cannot set format specified.", "Error", MB_OK);
    return 0;

    DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);

    ReleaseDC(hWnd, hDC);

    return hWnd;

    int APIENTRY
    WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst,
    LPSTR lpszCmdLine, int nCmdShow)
    HDC hDC; /* device context */
    HGLRC hRC; /* opengl context */
    HWND hWnd; /* window */
    MSG msg; /* message */

    hWnd = CreateOpenGLWindow("minimal", 0, 0, 256, 256, PFD_TYPE_RGBA, 0);
    if (hWnd == NULL)

    hDC = GetDC(hWnd);
    hRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC, hRC);

    ShowWindow(hWnd, nCmdShow);

    while(GetMessage(&msg, hWnd, 0, 0)) {

    wglMakeCurrent(NULL, NULL);
    ReleaseDC(hWnd, hDC);

    return msg.wParam;

      Report Article
    Sign in to follow this  

    User Feedback

    Create an account or sign in to leave a review

    You need to be a member in order to leave a review

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    There are no reviews to display.

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!