I decided to post this problem in a new thread. I have a simple program in OpenGL and it doesn't work. What happens is that a transparent window forms and when you hit the 'X' it says that the Release of the Device Context and Rendering Context failed. Here is the code:

3dH.h:

#ifndef h3dH_H
#define h3dH_H
#include <windows.h>
#include <windowsx.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\GLAUX.h>
#define ERR(X,Y) MessageBox(NULL,Y,X,MB_OK|MB_ICONEXCLAMATION)
#define QUESTION(X,Y) MessageBox(NULL,Y,X,MB_YESNO|MB_ICONEXCLAMATION)
#define MESSAGE(X) PeekMessage(&X,NULL,0,0,PM_REMOVE)
#define Main() WINAPI WinMain(HINSTANCE hInstance,HINSTANCE prevInstance,LPSTR cmdline,int cmdshow)
typedef void(*DrawFunc)(bool *);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
bool KEY[256];
int mouse_x;
int mouse_y;
int deltascroll;
bool active;
bool fullscreen;
HDC hDC;
HGLRC hRC;
HWND hWnd;
HINSTANCE hInstance;
char *classname;
void SetMousePos(int x, int y);
void Resize(int w, int h);
void Resize(int w, int h, double vangle);
void Resize(int w, int h, double vnear, double vfar);
void Resize(int w, int h, double vangle, double vnear, double vfar);
bool Init();
bool Draw();
bool Draw(DrawFunc draw);
void Step();
void Kill();
bool Create(char *title, int width, int height, int bpp, bool fscreen);
bool Create(char *title, int width, int height, int bpp, bool fscreen, int posx, int posy);
bool Create(char *title, int width, int height, int bpp, bool fscreen, char *cname);
bool Create(char *title, int width, int height, int bpp, bool fscreen, int posx, int posy, char *cname);

void SetMousePos(int x, int y)
{
    SetCursorPos(x,y);
    POINT mpos;
    GetCursorPos(&mpos);
    mouse_x=mpos.x;
    mouse_y=mpos.y;
}
void Resize(int width, int height, double vangle, double vnear, double vfar)
{
    if (height==0)
        height=1;
    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(vangle,(float)width/(float)height,vnear,vfar);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void Resize(int width, int height, double vnear, double vfar)
{
    Resize(width,height,45.0f,vnear,vfar);
}
void Resize(int width, int height, double vangle)
{
    Resize(width,height,vangle,0.1f,100.0f);
}
void Resize(int width, int height)
{
    Resize(width,height,45.0f,0.1f,100.0f);
}
bool Init()
{
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f,0.0f,0.0f,0.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
    return true;
}
bool Draw()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    return true;
}
bool Draw(DrawFunc draw)
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    bool ret=true;
    draw(&ret);
    return ret;
}
void Step()
{
    SwapBuffers(hDC);
}
void Kill()
{
    if (fullscreen)
    {
        ChangeDisplaySettings(NULL,0);
        ShowCursor(TRUE);
    }
    if (hRC)
    {
        if (!wglMakeCurrent(NULL,NULL))
            ERR("Fatal Shutdown Error!","Release of Device context and Rendering context failed.");
        if (!wglDeleteContext(hRC))
            ERR("Fatal Shutdown Error!","Release of Renderinc context failed.");
        hRC=NULL;
    }
    if (hDC&&!ReleaseDC(hWnd,hDC))
    {
        ERR("Fatal Shutdown Error!","Release of Device context failed.");
        hDC=NULL;
    }
    if (hWnd&&!DestroyWindow(hWnd))
    {
        ERR("Fatal Shutdown Error!","Release of Window failed.");
        hWnd=NULL;
    }
    if (!UnregisterClass(classname,hInstance))
    {
        ERR("Fatal Shutdown Error!","Release of Windows class failed.");
        hInstance=NULL;
    }
}
bool Create(char *title,int width,int height,int bpp, bool fscreen, int posx, int posy, char *cname)
{
    unsigned int PixelFormat;
    WNDCLASS wc;
    DWORD dwExStyle;
    DWORD dwStyle;
    RECT WindowRect;
    WindowRect.left=(long)0;
    WindowRect.top=(long)0;
    WindowRect.right=(long)width;
    WindowRect.bottom=(long)height;
    fullscreen=fscreen;
    classname=cname;
    hInstance=GetModuleHandle(NULL);
    wc.style=CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
    wc.lpfnWndProc=(WNDPROC)WndProc;
    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=classname;
    if (!RegisterClass(&wc))
    {
        ERR("Fatal Error!","Failed to register the Window class.");
        return false;
    }
    if (fullscreen)
    {
        DEVMODE dmScreenSettings;
        memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
        dmScreenSettings.dmSize=sizeof(dmScreenSettings);
        dmScreenSettings.dmPelsWidth=width;
        dmScreenSettings.dmPelsHeight=height;
        dmScreenSettings.dmBitsPerPel=bpp;
        dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSHEIGHT|DM_PELSWIDTH;
        if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
        {
            if (QUESTION("Fullscreen failed!","Fullscreen mode is unavailable, continue in window?")==IDYES)
            {
                fullscreen=false;
            }
            else
            {
                ERR("Fatal Error!","The program will now close.");
                return false;
            }
        }
    }
    if (fullscreen)
    {
        dwExStyle=WS_EX_APPWINDOW;
        dwStyle=WS_POPUP;
        ShowCursor(false);
    }
    else
    {
        dwExStyle=WS_EX_APPWINDOW|WS_EX_WINDOWEDGE;
        dwStyle=WS_OVERLAPPEDWINDOW;
    }
    AdjustWindowRectEx(&WindowRect,dwStyle,FALSE,dwExStyle);
    if (!(hWnd=CreateWindowEx(dwExStyle,classname,title,WS_CLIPSIBLINGS|WS_CLIPCHILDREN|dwStyle,posx,posy,WindowRect.right-WindowRect.left,WindowRect.bottom-WindowRect.top,NULL,NULL,hInstance,NULL)))
    {
        Kill();
        ERR("Fatal Error!","Failed to create a window.");
        return false;
    }
    static PIXELFORMATDESCRIPTOR pfd={sizeof(PIXELFORMATDESCRIPTOR),1,PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,PFD_TYPE_RGBA,bpp,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,PFD_MAIN_PLANE,0,0,0,0};
    if (!(hDC=GetDC(hWnd)))
    {
        Kill();
        ERR("Fatal Error!","Failed to create a device context.");
        return false;
    }
    if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))
    {
        Kill();
        ERR("Fatal Error!","Cannot find an acceptable pixel format.");
        return false;
    }
    if (!SetPixelFormat(hDC,PixelFormat,&pfd))
    {
        Kill();
        ERR("Fatal Error!","Cannot see the pixel format.");
        return false;
    }
    if (!(hRC=wglCreateContext(hDC)))
    {
        Kill();
        ERR("Fatal Error!","Failed to create a rendering context.");
        return false;
    }
    ShowWindow(hWnd,SW_SHOW);
    SetForegroundWindow(hWnd);
    SetFocus(hWnd);
    Resize(width,height);
    if (!Init())
    {
        Kill();
        ERR("Fatal Error!","Faileld to initialize OpenGL.");
        return false;
    }
    return true;
}
bool Create(char *title,int width,int height,int bpp, bool fscreen, int posx, int posy)
{
    return Create(title,width,height,bpp,fscreen,posx,posy,"OpenGL Default Window Class");
}
bool Create(char *title,int width,int height,int bpp, bool fscreen, char *cname)
{
    return Create(title,width,height,bpp,fscreen,0,0,cname);
}
bool Create(char *title,int width,int height,int bpp, bool fscreen)
{
    return Create(title,width,height,bpp,fscreen,0,0,"OpenGL Default Window Class");
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT umsg,WPARAM wparam,LPARAM lparam)
{
    switch (umsg)
    {
        case WM_ACTIVATE:
        if (!HIWORD(wparam))
            active=TRUE;
        else
            active=FALSE;
        return 0;
        case WM_SYSCOMMAND:
        switch (wparam)
        {
            case SC_SCREENSAVE:
            case SC_MONITORPOWER:
            return 0;
        }
        break;
        case WM_CLOSE:
        PostQuitMessage(0);
        return 0;
        case WM_KEYDOWN:
        KEY[wparam]=TRUE;
        return 0;
        case WM_KEYUP:
        KEY[wparam]=FALSE;
        return 0;
        case WM_SIZE:
        Resize(LOWORD(lparam),HIWORD(lparam));
        return 0;
        case WM_MOUSEMOVE:
        mouse_x=GET_X_LPARAM(lparam);
        mouse_y=GET_Y_LPARAM(lparam);
        return 0;
        case WM_MOUSEWHEEL:
        deltascroll=GET_WHEEL_DELTA_WPARAM(wparam);
        return 0;
    }
    return DefWindowProc(hwnd,umsg,wparam,lparam);
}
#ifndef ERROR_FREE
char *LastError()
{
    char *ret;
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,0,GetLastError(),0,(LPSTR)&ret,0,0);
    return ret;
}
#endif
#endif

test1.cpp:

#include "3dH.h"
void DrawMethodSimpleTriangle(bool *in)
{
    glBegin(GL_TRIANGLES);
        glVertex3f(0.0f,1.0f,0.0f);
        glVertex3f(-1.0f,-1.0f,0.0f);
        glVertex3f(1.0f,-1.0f,0.0f);
    glEnd();
}
int Main()
{
    MSG msg;
    bool done=false;
    if (!Create("TEST",640,480,32,false))
    {
        ERR("NOCREATE!","NOCREATE!");
        return 0;
    }
    while (!done)
    {
        if (MESSAGE(msg))
        {
            if (msg.message==WM_QUIT)
            {
                done=true;
            }
            else
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
        else
        {
            if ((active&&!Draw())||KEY[VK_ESCAPE])
            {
                done=true;
            }
            else
            {
                Step();
            }
        }
    }
    ERR("ERR",LastError());
    Kill();
    ERR("ERR",LastError());

    return (msg.wParam);
}

Recommended Answers

All 3 Replies

You should play around with the pixelformatdescriptor. This is often a source of weird errors because when it cannot be realized, the OS chooses a closely matching pfd and sometimes that pfd is crap and gives very weird results. What parameters did you try? Most modern computers work on a 32 bpp, but that means that the ColorBits should be 24 and the AlphaBits should be 8 (or 0). You set it to 32 bits on the ColorBits field (while NeHe sets it to 16, for older systems). If you are seeing a transparent screen, it is possible that the pixelformat gets selected wrong due to a poor choice of parameters.

*FACEPALM*
I forgot these lines in my header:

if (!wglMakeCurrent(hDC,hRC))
    {
        Kill();
        ERR("Fatal Error!","Failed to create a rendering context.");
        return false;
    }

Since I never make the current, I can't unmake current in Kill() and the screen is messed up. Thanks anyways!
*STILL FACEPALMING*

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.