Help! What's wrong with this code?

Please help me find the error in this code. I’m new to opengl and wrote this following nehe’s tutorial. When I compile it in Borland C++ builder 5.0 it doesn’t draw the triangle and the plane. When I compile in Dev-C++ 5 it says that the app has encountered an error and needs to be closed.

I downloaded and compiled the VC++6 source file and it compiled well on both compilers.

//---------------------------------------------------------------------------

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

//---------------------------------------------------------------------------
// Global variables

HGLRC hRC;
HDC hDC;
HWND main;
HINSTANCE hInstance;

bool fullscreen = false;
bool active = false;
bool keys[256];

const char classname = “StandartClass”;

//---------------------------------------------------------------------------
// Function declarations

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

//---------------------------------------------------------------------------
// All functions except for WinMain
//---------------------------------------------------------------------------

GLvoid ResizeGLWindow(GLsizei width, GLsizei height)
{
if (height == 0) height = 1;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(0.45f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

//---------------------------------------------------------------------------

int InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.7f, 0.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return true;
}

//---------------------------------------------------------------------------

GLvoid DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f, 0.0f, -6.0f);
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd();
glTranslatef(3.0f, 0.0f, 0.0f);
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd();
}

//---------------------------------------------------------------------------

GLvoid KillGLWindow(GLvoid)
{
if (fullscreen)
{
ChangeDisplaySettings(NULL, 0);
ShowCursor(true);
}
if(hRC)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRC);
}
hRC = NULL;
if(hDC && !ReleaseDC(main, hDC)) hDC = NULL;
if(main && !DestroyWindow(main)) main = NULL;
if(!UnregisterClass(classname, hInstance))hInstance = NULL;

}

//---------------------------------------------------------------------------

bool CreateGLWindow(const char * title, UINT width, UINT height, UINT bits, bool fullscreenflag)
{
GLuint PixelFormat;
WNDCLASS wc;
DWORD dwStyle;
DWORD dwExStyle;

    RECT WndRect;
            WndRect.left = 0;
            WndRect.top = 0;
            WndRect.right = width;
            WndRect.bottom = height;

    fullscreen = fullscreenflag;

    hInstance = GetModuleHandle(NULL);

    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.lpfnWndProc = WndProc;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = classname;
    wc.hInstance = hInstance;
    wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);

    RegisterClass(&wc);

    if(fullscreen)
    {
            DEVMODE dmScreenSettings;
            memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
            dmScreenSettings.dmSize = sizeof(dmScreenSettings);
            dmScreenSettings.dmBitsPerPel = bits;
            dmScreenSettings.dmPelsWidth = width;
            dmScreenSettings.dmPelsHeight = height;
            dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
            if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) fullscreen = false;
    }

    if (fullscreen)
    {
            dwExStyle = WS_EX_APPWINDOW;
            dwStyle = WS_POPUP;
            ShowCursor(false);
    }
    else
    {
            dwStyle = WS_OVERLAPPED;
            dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
            AdjustWindowRectEx(&WndRect, dwStyle, false, dwExStyle);
    };

    main = CreateWindowEx(dwExStyle, classname, title, dwStyle, 0, 0, width, height, NULL, NULL, hInstance, NULL);
    if(!main)
    {
            KillGLWindow();
            return false;
    }
    static PIXELFORMATDESCRIPTOR pfd =
    {
            sizeof(PIXELFORMATDESCRIPTOR),
            1,
            PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA,
            bits,
            0, 0, 0, 0, 0, 0,
            0,
            0,
            0,
            0, 0, 0, 0,
            16,
            0,
            0,
            PFD_MAIN_PLANE,
            0,
            0, 0, 0
    };
    hDC = GetDC(main);
    if(!hDC)
    {
            KillGLWindow();
            return false;
    };
    PixelFormat = ChoosePixelFormat(hDC, &pfd);
    if(!PixelFormat)
    {
            KillGLWindow();
            return false;
    };
    if(!SetPixelFormat(hDC, PixelFormat, &pfd))
    {
            KillGLWindow();
            return false;
    };
    hRC = wglCreateContext(hDC);
    if(!hRC)
    {
            KillGLWindow();
            return false;
    };
    if(!wglMakeCurrent(hDC, hRC))
    {
            KillGLWindow();
            return false;
    };
    ShowWindow(main, 1);
    SetForegroundWindow(main);
    SetFocus(main);
    ResizeGLWindow(width, height);

    if(!InitGL())
    {
            KillGLWindow();
            return false;
    }
    return true;

}

//---------------------------------------------------------------------------

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
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:
{
keys[wParam] = true;
return 0;
}
case WM_KEYUP:
{
keys[wParam] = false;
return 0;
}
case WM_SIZE:
{
ResizeGLWindow(LOWORD(lParam), HIWORD(lParam));
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}

//---------------------------------------------------------------------------

WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
bool done;
MSG msg;

    if(MessageBox(NULL, "Do you want to run the application in fullscreen mode (recommended)?", "Freescape 2002 :: Question", MB_YESNO | MB_ICONQUESTION)==IDYES)fullscreen = true;
    if(!CreateGLWindow("Freescape 2002", 640, 480, 16, fullscreen))
    {
            return 0;
    };

    while(!done)
    {
            if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                    if(msg.message==WM_QUIT)
                    {
                            done = true;
                    }
                    else
                    {
                            TranslateMessage(&msg);
                            DispatchMessage(&msg);
                    }
            }
            else
            {
                    if(active)
                    {
                            if (keys[VK_ESCAPE])
                            {
                                    done = true;
                            }
                            else
                            {
                                    DrawGLScene();
                                    SwapBuffers(hDC);
                            }
                    }
                    if(keys[VK_F1])
                    {
                            keys[VK_F1] = false;
                            KillGLWindow();
                            fullscreen = !fullscreen;
                            if(!CreateGLWindow("Freescape 2002", 640, 480, 16, fullscreen))
                            {
                                    return 0;
                            };
                    }
            }
    };

    KillGLWindow();
    return msg.wParam;

}
//---------------------------------------------------------------------------

what exatly did the error say?

The Dev-C++ error said "filename.exe has encountered a problem and needs to close. We are sorry for the inconvenience.

If you were in the middle of something, the information you were working on might be lost.

Please tell Microsoft about this problem.
We have created an error report that you can send to us. We will tret this report as confidential and anonymous.

To see what data this error report contains, click here"

the typical winXP error.

You should familiarise yourself with the use of a “debugger”, and have the debugger tell you exactly where in your program the crash occurs, what the state of the variables are, etc.

Without that info, a static reading of several pages of code is a pretty difficult way to track down a bug.

Hi !

Make sure you compile it with debug information enabled (-g) and then run it with the debugger (gdb application.e from the command line), when gdb is up and running, type “run” and press enter, the application will stop at the line where it crashes and give you a line number and filename where the problem was, you can alos use “bt” enter to see a complete backtrace of what it was doing before the crash.

Mikael