Jump to content
  • Advertisement
Sign in to follow this  
MindOfCorruption97

OpenGL OpenGL application stopped working

This topic is 2027 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry to ask but for some reason my OpenGL application does not seem to work. When I try to run/debug my application it stops and shows a message that my application stopped working. I tried to find ways to fix it, but I don't know what is causing to to stop working.

 

I listed all the classes and files associated with my application.

 

MainWindow.cpp

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

#include "EditorLink.h"

const char g_szClassName[] = "Window";
BOOL quit = FALSE;

void EnableOpenGL(HWND hWnd,HDC * hDc,HGLRC * hRc);
void DisableOpenGL(HWND hWnd,HDC hDc,HGLRC hRc);

Editor Edit;

LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            quit = TRUE;
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd,msg,wParam,lParam);
    }
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR CmdLine,int nCmdShow)
{
    WNDCLASS wc;
    HWND hWnd;
    HGLRC hRc;
    HDC hDc;
    MSG msg;
    
    wc.lpszClassName = g_szClassName;
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.hInstance = hInstance;
    wc.style = CS_OWNDC;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.lpfnWndProc = WndProc;
    wc.lpszMenuName = NULL;
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hIcon = LoadIcon(NULL,NULL);
    RegisterClass(&wc);
    
    hWnd = CreateWindow(
            g_szClassName,
            "OPENGL PRACTICE",
            WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_POPUP | WS_VISIBLE,
            CW_USEDEFAULT,CW_USEDEFAULT,800,700,
            NULL,NULL,hInstance,NULL
            );
    
    EnableOpenGL(hWnd,&hDc,&hRc);
    Edit.init(hInstance);
    
    ShowWindow(hWnd,nCmdShow);
    UpdateWindow(hWnd);
    
    while(!quit)
    {
        if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            Edit.display();
            SwapBuffers(hDc);
        }
    }
    
    DisableOpenGL(hWnd,hDc,hRc);
    DestroyWindow(hWnd);
    
    return msg.wParam;
}


void EnableOpenGL(HWND hWnd,HDC * hDc,HGLRC * hRc)
{
    *hDc = GetDC(hWnd);
    int nFormat;
    
    PIXELFORMATDESCRIPTOR pfd =
    {
        sizeof(PIXELFORMATDESCRIPTOR),
        1,
        PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA,
        0,0,0,0,0,0,0,0,0,0,0,0,0,
        24,
        8,
        0,
        PFD_MAIN_PLANE,
        0,0,0,0
    };
    
    nFormat = ChoosePixelFormat(*hDc,&pfd);
    SetPixelFormat(*hDc,nFormat,&pfd);
    
    *hRc = wglCreateContext(*hDc);
    wglMakeCurrent(*hDc,*hRc);
}

void DisableOpenGL(HWND hWnd,HDC hDc,HGLRC hRc)
{
    wglMakeCurrent(NULL,NULL);
    wglDeleteContext(hRc);
    ReleaseDC(hWnd,hDc);
}

Editor.h

#include <windows.h>

#ifndef EDITORLINK_H
#define	EDITORLINK_H

class Editor
{
public:
    void init(HINSTANCE hInstance);
    void display();
private:
};

#endif

 

Editor.cpp

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

#include "EditorLink.h"
#include "Texture.h"

unsigned int tex;
Bitmap *bitmap;

void Editor::init(HINSTANCE hInstance)
{
    glClearColor(0.0,0.0,0.0,1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45,640.0/480.0,1.0,500.0);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_TEXTURE_2D);
    
    float dif[] = {1.0,1.0,1.0,1.0};
    glLightfv(GL_LIGHT0,GL_DIFFUSE,dif);
    float amb[] = {0.2,0.2,0.2,1.0};
    glLightfv(GL_LIGHT0,GL_AMBIENT,amb);
    
    tex = bitmap->loadTexture("Practice.bmp");
}

void Editor::display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    
    float pos[] = {-2.0,2.0,-3.0,1.0,};
    glLightfv(GL_LIGHT0,GL_POSITION,pos);
    
    glTranslatef(0.0,0.0,-5.0);
    
    glBindTexture(GL_TEXTURE_2D,tex);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0,1.0);
    glVertex3f(-2.0,2.0,0.0);
    glTexCoord2f(0.0,0.0);
    glVertex3f(-2.0,-2.0,0.0);
    glTexCoord2f(1.0,0.0);
    glVertex3f(2.0,-2.0,0.0);
    glTexCoord2f(1.0,1.0);
    glVertex3f(2.0,2.0,0.0);
    glEnd();
}

 

Texture.h

#include <windows.h>
#include <cstdio>

#ifndef TEXTURE_H
#define	TEXTURE_H

class Bitmap
{    
public:
    RGBQUAD getColors(){return *colors;}
    BYTE getPixelData(){return *pixelData;}
    bool getLoaded(){return loaded;}
    LONG getWidth(){return width;}
    LONG getHeight(){return height;}
    WORD getBpp(){return bpp;}
    bool loadBmp(const char * FileName);
    unsigned int loadTexture(const char * FileName);
private:
    RGBQUAD *colors;
    BYTE *pixelData;
    bool loaded;
    LONG width,height;
    WORD bpp;
    BITMAPFILEHEADER bmfh;
    BITMAPINFOHEADER bmih;
};

#endif


Texture.cpp

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

#include "Texture.h"

bool Bitmap::loadBmp(const char* FileName)
{
    int numColors,offset;
    DWORD size,diff;
    BYTE *tempPixelData;
    LONG byteWidth,padWidth;
    
    FILE *in;
    in = fopen(FileName,"rb");
    fread(&bmfh,sizeof(BITMAPFILEHEADER),1,in);
    fread(&bmih,sizeof(BITMAPINFOHEADER),1,in);
    
    numColors = 1 << bmih.biBitCount;
    
    size = bmfh.bfSize - bmfh.bfOffBits;
    tempPixelData = new BYTE[size];
    byteWidth = padWidth = (LONG)((float)width * (float)bpp/8.0);
    
    height = bmih.biHeight;
    diff = height * byteWidth;
    
    if(bmih.biBitCount == 8)
    {
        colors = new RGBQUAD[numColors];
        fread(colors,sizeof(RGBQUAD),numColors,in);
    }
    if(tempPixelData == NULL)
    {
        fclose(in);
        return false;
    }
    fread(tempPixelData,sizeof(BYTE),size,in);
    
    while(padWidth % 4 != 0)
    {
        padWidth++;
    }
    if(height > 0)
    {
        int j = size - 3;
        offset = padWidth - byteWidth;
        for(int i = 0;i < size;i += 3)
        {
            if((i + 1) % padWidth == 0)
            {
                i += offset;
            }
            *(pixelData + j + 2) = *(tempPixelData + i);
            *(pixelData + j + 1) = *(tempPixelData + i + 1);
            *(pixelData + j) = *(tempPixelData + i + 2);
            j++;
        }
    }
    else
    {
        height = height* - 1;
        offset = 0;
        do{
            memcpy((pixelData + (offset * byteWidth)),
                    (tempPixelData + (offset * byteWidth)),
                    byteWidth);
            offset++;
        }while(offset < height);
    }
}

unsigned int Bitmap::loadTexture(const char * FileName)
{
    if(loadBmp(FileName))
    {
        unsigned int id;
        glGenTextures(1,&id);
        glBindTexture(GL_TEXTURE_2D,id);
        glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,pixelData);
        
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        
        return id;
    }
}

 

Thank you, sorry to waste your time.

Also can you guys tell me what causes an application to stop working? Since I want to make sure I could prevent this from happening in the future.

Share this post


Link to post
Share on other sites
Advertisement
There are a lot of reasons why an application can stop working. Perhaps you should use your debugger and break while your program isn't responding and examine what it is doing and/or stuck on.

One thing I noticed is that your WndProc routine isn't returning a value when the default case isn't reached. However, that only applies to WM_CLOSE and WM_QUIT, so I doubt that is the issue here...

Share this post


Link to post
Share on other sites
It's a lot of code to go through, so I just skimmed it, but this looks suspicious:
Bitmap *bitmap;
...
    tex = bitmap->loadTexture("Practice.bmp");

I don't see you ever initializing bitmap. My guess is that it's an invalid pointer and you aren't setting it/allocating a Bitmap. Perhaps it shouldn't be a pointer and should just be a regular Bitmap instead of a Bitmap*? I see no reason to make it a pointer. I'm also unsure of why you made bitmap and tex global variables instead of member variables of Editor...
 
But yes, as fastcall22 said, now would be a great time to get familiar with your debugger. Edited by Cornstalks

Share this post


Link to post
Share on other sites

Well I made Tex and Bitmap no longer a global variable.

I also checked the debugger and it says the problem is this section of the code.

 if((i + 1) % padWidth == 0)
{
i += offset;
}
*(pixelData + j + 2) = *(tempPixelData + i);
*(pixelData + j + 1) = *(tempPixelData + i + 1);
*(pixelData + j) = *(tempPixelData + i + 2);
j++;

I tried deleting that part of the code and it worked but it didn't show the texture.

Any idea what could be wrong?

Share this post


Link to post
Share on other sites

Well, for one, you're never setting pixelData. This is the same problem that I said bitmap had. The thing about pointers is, if you don't give them a chunk of valid memory to point to, they're just pointing off into oblivion, and using them is a great way to crash your program. You need to allocate memory for pixelData if you're going to write to it.

Also, this:

while(padWidth % 4 != 0)
{
    padWidth++;
}

can be simplified to:

padWidth += padWidth % 4; // round up to multiple of 4


Actually, the more I look at it there's a whole bunch of problems with your code. colors and tempPixelData leak memory because you never delete [] what you new [] (there could be other leaks elsewhere; I haven't checked too thoroughly). You're mixing C with C++ when you shouldn't (for example, don't use FILEs, use std::fstreams, and use  std::copy instead of memcpy). But even if you keep using FILEs, you're not fclose()ing what you fopen().

 

And this if statement is useless:

 

if(tempPixelData == NULL)
{
    fclose(in);
    return false;
}
 
If new fails, it will throw an exception (std::bad_alloc), so there's no point in checking it for NULL.
 
Also, you should place your #includes inside of your inclusion guards (this isn't necessarily a problem, but it's "best practice").
Edited by Cornstalks

Share this post


Link to post
Share on other sites

Check first if your j and i are getting initialised to 0, everytime before you enter this pieace of code, and then look for any reason why you might be hitting an out-of-bounds failure while doing pointer arithmetic.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!