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.






