PDA

View Full Version : Crisis!!?!?!?!?



08-12-2002, 08:10 AM
I have been try to get a scaled down framework of one that I download from the internet. I think there is some kind of leak because I am only able to compile, link and run the program once, because the second time i try to link it says that is cannot use the exe file. The source is just ment to create a blank framework, but It does not seem to do anything. Please someone taqke the following code and see if you can get it working for me. Much appreciated.

The source is heavily commented as I am still very new to the whole OGL thing.

//GL and Standard Header for C++
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include <math.h>
//

//Windows Variables
HGLRC hRC=NULL; //Permanent Rendering Context for OpegGL
HDC hDC=NULL; //Permanent GDI Device Context for Window
HWND hWnd=NULL; //Handle for the Window
HINSTANCE hInstance; //The Instance for the Application
//

//Normal Variables
bool key[256]; //Array Element for Each Key on the Keyboard
bool active=true;
//


//Prototype for Main Windows Procedure
LRESULT CALLBACK WndProc(HWND ,
UINT ,
WPARAM,
LPARAM
);

//Procedure to Set the Size of The GL Window
GLvoid GLResize(GLsizei width, GLsizei height)
{
if (height == 0) {height = 1;}
if (width == 0) {width = 1;}


//Set GL viewport to Entire Window Screen
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION); //Select Projection Matrix
glLoadIdentity(); //Reset Projection Matrix


//Setup the Perspective Settings (Angle, Gradient, MinDepth, MaxDepth
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);

glMatrixMode(GL_MODELVIEW); //Select Model View Matrix
glLoadIdentity(); //Reset Model View Matrix

}
//

int InitGL(GLvoid) //OpenGL Setup
{

glShadeModel(GL_SMOOTH); //Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Set Background Colour

//Depth Buffer Setup
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_EQUAL);
//

//Perspective Calculations
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

return true;
}
//

//Procedure to Draw Everything in OpenGL
int GLDraw(GLvoid)
{

//Clear the Screen and the Depth Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Reset the Model View Matrix
glLoadIdentity();


/*


ALL GL DRAWING IN HERE

*/

return true;


}
//

//Get Rid of the OpenGL Window
GLvoid KillGLWindow(GLvoid)
{

if (hRC)
{
//Release DC and RC
wglMakeCurrent(NULL, NULL);
//Delete Rendering Context
wglDeleteContext(hRC);
//Set RC to NULL
hRC=NULL;
}

//Release the DC
ReleaseDC(hWnd, hDC);
hDC=NULL;

//Destroy the Windw
DestroyWindow(hWnd);
hWnd=NULL;

//Unregister the Class With Windows
UnregisterClass("OpenGL", hInstance);
hInstance=NULL;
}
//


//Creates the OpenGL Window
BOOL CreateGLWindow(char* title, int width, int height, int bits)
{

//Holds Results After Searching for A PDF
GLuint PixelFormat;
//Windows Class Object
WNDCLASS wc;
//Window Style
DWORD dwStyle;
//Window Extended Style
DWORD dwExStyle;

//Initializes a Rectangle Objecy
RECT WindowRect;
//Set the Coords of the Window
WindowRect.left =(long)0;
WindowRect.right =(long)width;
WindowRect.top =(long)0;
WindowRect.bottom =(long)height;

//Get an Instance for our Window
hInstance = GetModuleHandle(NULL);

//Setup the wc object
//On Move Horizontal Redraw, Vertical Redraw, Own DC
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
//Procedure to Handle Message (Prototype)
wc.lpfnWndProc = (WNDPROC)WndProc;
//No Extra Window Data
wc.cbClsExtra =0;
//No Extra Window Data
wc.cbClsExtra;
//Set the Instance
wc.hInstance = hInstance;
//Setup the Window Icon
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
// Load The Arrow Pointer
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
//Load a background | OpenGL Doesn't need one
wc.hbrBackground = NULL;
//Load the Menu Name | OpenGL Doesn't need one
wc.lpszMenuName = NULL;
//Name the Class for refernec
wc.lpszClassName = "OpenGL";

//Register the Class with Windows
RegisterClass(&wc);

//Load Window Style
dwStyle = WS_OVERLAPPEDWINDOW;
//Load Extended Window Style
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;

//Adjust the Window We Are Creating
AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);

//Create the Window
// PARAM ( Extended Window Style, Class Name, Required Window Style
//That Allows Overdraw, Stlye, Window Position, Dimensions, Parent? Menu?,
//Instance, Optional Extra Pass
hWnd = CreateWindowEx( dwExStyle,
"OpenGL",
title,
WS_CLIPSIBLINGS |
WS_CLIPCHILDREN |
dwStyle,
0, 0,
WindowRect.right - WindowRect.left,
WindowRect.bottom - WindowRect.top,
NULL,
NULL,
hInstance,
NULL);

//Dictates the sort of thing we will need for OpenGL and then see
//If windows has the resources for it
static PIXELFORMATDESCRIPTOR pfd =
{
//Size of the Structure
sizeof(PIXELFORMATDESCRIPTOR),
//Version Number
1,
//Required Features
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
//Colour Format
PFD_TYPE_RGBA,
//Colour Depth
bits,
//Colour bits depths ignored
0, 0, 0, 0,
//Alpha Buffer? | NO
0,
//Shift Bit Accept? | NO
0,
//Accumulation (slow) Buffer? | NO
0,
//Accumulation Buffer Bit depths ignored | NONE
0, 0, 0, 0,
//Bit Depth of Z-Depth Buffer
16,
//Stencil Buffer? | NO
0,
//Auxillary Buffer? | NO
0,
//Draw to Where
PFD_MAIN_PLANE,
//Reserved!
0,
//Layer Masks Ignored? | NONE
0, 0, 0,
};


//Get a device context
hDC = GetDC(hWnd);
//Get a matching PFD
PixelFormat = ChoosePixelFormat(hDC, &pfd);
//Set Pixel Format
SetPixelFormat(hDC, PixelFormat, &pfd);
//Get a rendering context
hRC = wglCreateContext(hDC);
//Activate the rendering context
wglMakeCurrent(hDC, hRC);


//Show the OpenGL Window
//Show the Window
ShowWindow(hWnd, SW_SHOW);
//Givew slightly higher priority
SetForegroundWindow(hWnd);
//Set Keyboard Focus to Window
SetFocus(hWnd);
//Set up the Perspective Gl Screen
GLResize(width, height);

//Initialize our new Opengl Window
InitGL();


//SUCCESS!
return true;
}

//Procedure to deal with all the window messages
LRESULT CALLBACK WndProc( HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)

{
switch (uMsg) //Check for a windows message
{

case WM_ACTIVATE:
{
//Check if window in minimized
if (HIWORD(wParam))
{
active = true;
}
else
{
active = false;
}
return 0;
}


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:
{
GLResize(LOWORD(lParam), HIWORD(lParam));
return 0;
}
}

//Return All Unhandled Commands
return DefWindowProc(hWnd, uMsg, wParam, lParam);

}
//

//ENTRY POINT OF THE WINDOWS APPLICATION//
/////////////////////////////

int WINAPI WinMain( HINSTANCE hInstance, // Instance
HINSTANCE hPrevInstance, // Previous Instance
LPSTR lpCmdLine, // Command Line Parameters
int nCmdShow) // Window Show State
{

MSG msg; // Windows Message Structure
BOOL done=FALSE; // Bool Variable To Exit Loop

// Create Our OpenGL Window
CreateGLWindow("OpenGL Framework",640,480,16);

while(!done) // Loop That Runs Until done=TRUE
{

if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
{

if (msg.message==WM_QUIT) // Have We Received A Quit Message?
{
done=TRUE; // If So done=TRUE
}
else // If Not, Deal With Window Messages
{

TranslateMessage(&msg); // Translate The Message
DispatchMessage(&msg); // Dispatch The Message
}
}
else // If There Are No Messages
{


// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
if (active) // Program Active?
{
if (key[VK_ESCAPE]) // Was ESC Pressed?
{
done=TRUE; // ESC Signalled A Quit
}
else // Not Time To Quit, Update Screen
{

GLDraw(); // Draw The Scene
SwapBuffers(hDC); // Swap Buffers (Double Buffering)
}
}

if (key[VK_F1]) // Is F1 Being Pressed?
{
key[VK_F1]=FALSE; // If So Make Key FALSE
KillGLWindow(); // Kill Our Current Window

// Recreate Our OpenGL Window
if (!CreateGLWindow("NeHe's OpenGL Framework",640,480,16))
{
return 0; // Quit If Window Was Not Created
}
}
}
}

// Shutdown
KillGLWindow(); // Kill The Window
return (msg.wParam); // Exit The Program
}


THANK YOU!!!!!

08-12-2002, 11:35 AM
What compiler/IDE nare you using. I had a similar problem using Dev C++ (which is I quite liked otherwise) which left the exe protected somehow everytime I ran/linked it. Have you tried deleting the exe and then re-linking?

Robbo
08-13-2002, 04:07 AM
If your exe is still in the process list, it obviously cannot be written to by the linker. Bring up task manager and kill it there.

In other words: application is not shutting down correctly.

08-13-2002, 05:21 AM
Thanks but....
There is a problem in the code, once it has compiled and linked the program executes then terminates immidiately or just doesn't execute at all I can't tell. Can someone copy the source above and see if they can get the framework to run. I was using vc++ 6

Allan Walton
08-13-2002, 05:54 AM
Hi there,

I had this very problem just the other night. i cant remember how i fixed it off the top of my head but i'll look tonight when i get home and let you know tomorrow.

i can remember it's something to do with the quit message not being handled correctly (at least it was in my case because i was shuting the window down and freeing the Hdc. the next time the program comes round to exit the Hdc is invalid (this may be total rubbish but it was definetly something like this)

as a hint, if you have boundschecker, debug it with that and you'll get an error message about an invalid context

sorry if i'm talking rubbish but i really cant remember right now - i'll reply again tomorrow.

08-13-2002, 06:39 AM
Thanks man, appreciate the help. Do you think that the reason that the framework is immidiately closing the framework. Await your post eagerly!!

Allan Walton
08-13-2002, 07:34 AM
I've had a quick look at trying to get your code working and couldnt do it. all i can tell you is that your "hWnd" variable which is returned from CreateWindowEx is invalid / NULL and that's why your program is dying immediately (at least on my computer)

if you want a really good tutorial about windows / GL and such like go to gametutorials.com. that's where i learned how to do the windows stuff and Nehe's is quite different

good luck anyway.

Allan