🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Lesson 1 problems

Started by
8 comments, last by GameDev.net 17 years, 9 months ago
Hi i have typed out lesson 1, 3 different times and have gotten 3 different resolts, and as far as i can tell, i have written each one word for word using dev c++. the first time i typed out lesson 1 every think compiles perfectly. the seccond time i typed it out, it all compiled fine except it came up with the "Window Creation Error." message. then it closed. the third time i typed it out (i deleted the above code and retyped) everything compiles fine except when i click anywear on the title bar or try to resize the window it closes instantly. i have been over and over the code and cannot find why it is doing this. I suspect the problem might lie in ReSizeGLScene(width, height) function. Here is the code for the hole thing. maby there is just something im not doing. dev c++ project opeions under the parameters tap for compiler type -D__GNUWIN32__ -W -DWIN32 -DNDEBUG -D_WINDOWS -D_MBCS for the liker type -lopengl32 -lglu32 -lglaux -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid -lodbc32 -lodbccp32 or in code::blocks create a new OpenGL application project and just cut and paste the code. and it will compile.

#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>

HGLRC hRC = NULL;
HDC hDC = NULL;
HINSTANCE hInstance;
HWND hWnd = NULL;

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

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//------------------------------------------------------------------------------
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
    if(height == 0)
    {
        height = 1;
    }
    
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f);
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
//------------------------------------------------------------------------------
int InitGL(GLvoid)
{
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, .0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    
    return true;
    
}
//------------------------------------------------------------------------------
int DrawGLScene(GLvoid)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    return TRUE;
}
//------------------------------------------------------------------------------
GLvoid KillGLWindow(GLvoid)
{
    if(fullscreen)
    {
        ChangeDisplaySettings(NULL,0);
        ShowCursor(true);
    }
    
    if(hRC)
    {
        if(!wglMakeCurrent(NULL, NULL))
        {
            MessageBox(NULL, "Release of DC and RC FAILED.", "ERROR!",
                       MB_OK | MB_ICONINFORMATION);
        }
        
        
        if (!wglDeleteContext(hRC))
        {
            MessageBox(NULL,"Release of the rendering context failed", "ERROR!",
                       MB_OK | MB_ICONINFORMATION);                
        }
        hRC = NULL;
    }
    if(hRC && !ReleaseDC(hWnd,hDC))
    {
        MessageBox(NULL,"Release of device context failed", "ERROR!",
                   MB_OK | MB_ICONINFORMATION);
        hDC = NULL;
    }
        
    if(hWnd && !DestroyWindow(hWnd))
    {
        MessageBox(NULL,"Could not release hWnd", "ERROR!",
                    MB_OK | MB_ICONINFORMATION);
        hWnd = NULL;
    }
        
    if(!UnregisterClass("OpenGL", hInstance))
    {
        MessageBox(NULL,"Could nor Unregester class", "ERROR!",
                    MB_OK | MB_ICONINFORMATION);  
        hInstance = NULL;
    }
    
}
//------------------------------------------------------------------------------
bool CreateGLWindow(char * title, int width, int height, int bits, bool fullscreenFlag)
{
    GLuint PixelFormat;
    
    WNDCLASS wc;
    
    DWORD dwExStyle;
    DWORD dwStyle;
    
    RECT WindowRect;
    WindowRect.left = (long)0;
    WindowRect.right = (long)width;
    WindowRect.top = (long)0;
    WindowRect.bottom = (long)height;
    
    fullscreen = fullscreenFlag;
    
    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 = "OpenGl";
    
    if(!RegisterClass(&wc))
    {
        MessageBox(NULL,"Failed To Register The Window Class.","ERROR",
                   MB_OK|MB_ICONEXCLAMATION);
        return false;
    }
    
    if(fullscreen)
    {
        DEVMODE dmScreenSettings;
        memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
        dmScreenSettings.dmSize = sizeof(dmScreenSettings);
        dmScreenSettings.dmPelsWidth = width;
        dmScreenSettings.dmPelsHeight = height;
        dmScreenSettings.dmBitsPerPel = bits;
        dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
        
        if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) !=DISP_CHANGE_SUCCESSFUL)
        {
            if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?",
                            "NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
			{
                fullscreen = false;
            }
            else
            {
                MessageBox(NULL,"Program Will Now Close.","ERROR",
                           MB_OK|MB_ICONSTOP);
                return false;
            }
        }
    }
    if(fullscreen)
    {
        dwExStyle = WS_EX_APPWINDOW;
        dwStyle = WS_POPUP;
        ShowCursor(true);
    }
    else
    {
        dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
        dwStyle = WS_OVERLAPPEDWINDOW;
    }
    
    AdjustWindowRectEx(&WindowRect, dwStyle, false, dwExStyle);
    
    if(!(hWnd = CreateWindowEx(  dwExStyle,
                                "OpenGL",
                                title,
                                WS_CLIPSIBLINGS |
                                WS_CLIPCHILDREN |
                                dwStyle,
                                0, 0,
                                WindowRect.right - WindowRect.left,
                                WindowRect.bottom - WindowRect.top,
                                NULL,
                                NULL,
                                hInstance,
                                NULL)))
    {
        KillGLWindow();
        MessageBox(NULL,"Window Creation Error.","ERROR",
                   MB_OK|MB_ICONEXCLAMATION);
        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
    };
    
    if(!(hDC = GetDC(hWnd)))
    {
        KillGLWindow();
        MessageBox(NULL,"Unable to create a GL device Context","ERROR",
                   MB_OK|MB_ICONEXCLAMATION);
        return false;        
    }
    
    if(!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
    {
        KillGLWindow();
        MessageBox(NULL, "Cant find a sutable pixel format", "ERROR!",
                   MB_OK | MB_ICONEXCLAMATION);
        return false;
    }
    
    if(!SetPixelFormat(hDC, PixelFormat, &pfd))
    {
        KillGLWindow();
        MessageBox(NULL, "Canot set the  pixel format", "ERROR!",
                   MB_OK | MB_ICONEXCLAMATION);
        return false;
    }
    
    if(!(hRC = wglCreateContext(hDC)))
    {
        KillGLWindow();
        MessageBox(NULL, "Canot create a GL Rendering context.", "ERROR!",
                   MB_OK | MB_ICONEXCLAMATION);
        return false;
    }
    
    if(!wglMakeCurrent(hDC,hRC))
    {
        KillGLWindow();
        MessageBox(NULL, "Canot activate the rendering context", "ERROR!",
                   MB_OK | MB_ICONEXCLAMATION);
        return false;
    }
    
    ShowWindow(hWnd, SW_SHOW);
    SetForegroundWindow(hWnd);
    SetFocus(hWnd);
    ReSizeGLScene(width, height);
    
    if(!InitGL())
    {
        KillGLWindow();
        MessageBox(NULL, "Initalisation failed", "ERROR!",
                   MB_OK | MB_ICONEXCLAMATION);
        return false;
    }
    return true;
}
//------------------------------------------------------------------------------
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:
        {
            keys[wParam] = true;
            return 0;
        }
        
        case WM_KEYUP:
        {
            keys[wParam] = false;
            return 0;
        }
        
        case WM_SIZE:
        {
            ReSizeGLScene(LOWORD(lParam), HIWORD(lParam));
            return 0;
        }
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//------------------------------------------------------------------------------
int WINAPI WinMain( HINSTANCE   hInstance,
                    HINSTANCE   hPrevInstance,
                    LPSTR       lpCmdLine,
                    int         nCmdShow)
{
    MSG msg;
    bool done = false;
    
    if(MessageBox(NULL, "Would you like to start in Fullscreen mode?", "Start Fullscreen?",
                  MB_YESNO | MB_ICONQUESTION)==IDYES)
    {
        fullscreen = true;
    }
    
    if(!CreateGLWindow("Open GL Window", 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("Open GL Window", 640, 480, 16, fullscreen))
                {
                    return 0;
                }
                
            }
        }
    }
    
    KillGLWindow();
    return (msg.message);
}



thanks people. this may even help you improve your debugging skills. unless its a simple thing. lol [Edited by - aaroncox_123 on August 29, 2006 2:33:20 AM]
Advertisement
Using 'break' after a 'case' is always a good idea...
I did wondered about that, when i went through his tutorial the first time. but it unfortunatly it didnt make a difference to the compiled porgram.

switch(uMsg)    {        case WM_ACTIVATE:        {            if(!HIWORD(wParam))            {                active = true;            }            else            {                active = false;            }            return 0;            break;        }                case WM_SYSCOMMAND:        {            switch(wParam)            {                case SC_SCREENSAVE:                case SC_MONITORPOWER:                {                    return 0;                }                break;            }        }                case WM_CLOSE:        {            PostQuitMessage(0);            return 0;            break;        }                case WM_KEYDOWN:        {            keys[wParam] = true;            return 0;            break;        }                case WM_KEYUP:        {            keys[wParam] = false;            return 0;            break;        }                case WM_SIZE:        {            ReSizeGLScene(LOWORD(lParam), HIWORD(lParam));            return 0;            break;        }    }


that is what you were getting at?
Look at 'WM_SYSCOMMAND' and what it does when 'wParam' isn't handled...
Adding the break after return is not necessary. The return statement will cause the function to end as soon as it is executed. What that means is that the break statement will never be called, and is therefore unnecessary.

Have you tried downloading the source code for lesson one, instead of trying to manually type it in? It sounds like your inconsistent results are coming from that. Assuming your project is set up the same for each of the three times, you should get the same results if the source code is the same.
Quote: Original post by Halibut
Adding the break after return is not necessary. The return statement will cause the function to end as soon as it is executed. What that means is that the break statement will never be called, and is therefore unnecessary.


True, but you should allways have a break at the end of every case, not because it is allways needed, but because missing breaks causes more bugs than you really want to know about, a lost break can even kill people. :)

Quote: True, but you should allways have a break at the end of every case, not because it is allways needed, but because missing breaks causes more bugs than you really want to know about, a lost break can even kill people. :)


That is true that it is good practice to have a break at the end of every case. A lot of problems can be prevented by practicing good programming techniques. I was just trying give an explanation of why adding the breaks won't fix the problem.
Quote: Original post by lc_overlordTrue, but you should allways have a break at the end of every case, not because it is allways needed, but because missing breaks causes more bugs than you really want to know about, a lost break can even kill people. :)


With the case of a break after a return, it definately is not needed! Placing it there will generate a load of warnings stating that you have un-reachable code. That imo is a *bad* programming practice...

Anyhow, To re-iterate TrueTom's point...
        case WM_SYSCOMMAND:        {            switch(wParam)            {                case SC_SCREENSAVE:                case SC_MONITORPOWER:                {                    return 0;                }                break;            }        }        break;
Hi. I am also having a problem with the first tutorial. I copied and pasted the source code and it compiled without errors. However, when I try to build the .exe file I get a error message.

"
-------------------Configuration: windoh - Win32 Debug--------------------
Linking...
LINK : fatal error LNK1104: cannot open file "and.obj"
Error executing link.exe.

windoh.exe - 1 error(s), 0 warning(s)
"

I'm stuck at this point and any help would be much appreciated. I've redone the project several times to no avail. Thank you.

I should have done a more thorough search before posting. Problem fixed. My apologies.

This topic is closed to new replies.

Advertisement