Hi. I was wondering if anyone could figure out why the below code doesn’t work (its merely meant to draw a triangle). I know it may bitch but I would greatly appreciate any input.
#include <windows.h> //Windows header
#include <gl\gl.h> //OpenGL32 Library Header
#include <gl\glu.h> //GLu32 Library Header
#include <gl\glaux.h> //GLaux Library Header
HGLRC hRC = NULL; //Rendering Context <-Links OpenGL calls to Device Context
HDC hDC = NULL; //Device Context
HWND hWnd = NULL; //Window Handle
HINSTANCE hInstance; //Application Instance
bool g_keys[256]; //Used for keyboard routine
bool g_active=true; //Program is not minimized
bool g_fullscreen=true; //fullscreen Mode
//****************************************************//
// F u n c t i o n P r o t o t y p e s //
//****************************************************//
//Message Processing
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//Creates the Window
int CreateGLWindow(char* title, int width, int height, int bits, bool g_fullscreen);
//Resize and Initialize the GL Window
GLvoid ReSizeGLScene(GLsizei width, GLsizei height);
//Setup for OpenGL
int InitGL(GLvoid);
//Draw the scene
int RenderGLScene(GLvoid);
//Kill the Window Correctly
GLvoid KillGLWindow(GLvoid);
//****************************************************//
// CreateGLWindow //
// Properly Create the Window //
//****************************************************//
int CreateGLWindow(char* title, int width, int height, int bits, bool g_fullscreen)
{
GLuint PixelFormat; //Pixel Format Windows finds
WNDCLASS WndClass; //Window Class
DWORD dwStyle; //Window Style
DWORD dwExStyle; //Window Extended Stye
RECT WindowRect; //Window Boundries
WindowRect.left=(long)0; //Left Boundry (w/ Long Cast)
WindowRect.right=(long)width; //Right Boundry
WindowRect.top=(long)0; //Top Boundry
WindowRect.bottom=(long)height; //Bottom Boundry
hInstance =GetModuleHandle(NULL); //Get an Instance for WIndow
WndClass.style =CS_HREDRAW|CS_VREDRAW|CS_OWNDC; //Redraw on Move and Own DC
WndClass.lpfnWndProc =(WNDPROC)WndProc; //WndProc handles Messages
WndClass.cbClsExtra =0; //No Extra Window Data
WndClass.cbWndExtra =0; //No Extra Window Data
WndClass.hInstance =hInstance; //Set the Instancee
WndClass.hIcon =LoadIcon(NULL, IDI_WINLOGO); //Default Icon
WndClass.hCursor =LoadCursor(NULL, IDC_ARROW); //Default Curson
WndClass.hbrBackground =NULL; //No Background. OGL covers it.
WndClass.lpszMenuName =NULL; //This is a game. No Menus.
WndClass.lpszClassName ="WINDOW"; //Class Name
if (!RegisterClass(&WndClass)) //Try to Register Class
{
//If Registering the Window Class Failed Report Error
MessageBox(NULL, "Failed to Register Window Class","Init Error", MB_OK|MB_ICONEXCLAMATION);
return 0; //Return Failure
}//End If
if (g_fullscreen) //If the Window is to be Fullscreen
{
DEVMODE dmScreenSettings; //Device Mode
memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); //Make sure Memory's Cleared
dmScreenSettings.dmSize=sizeof(dmScreenSettings); //Size of the DEVMODE Class
dmScreenSettings.dmPelsWidth=width; //Screen Width
dmScreenSettings.dmPelsHeight=height; //Screen Height
dmScreenSettings.dmBitsPerPel=bits; //Bits per Pixel
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
//Try to Set Mode
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
//If Mode Fails ask for Windowed Mode
if(MessageBox(NULL, "Fullscreen cannot be run. Would you like to run in Windowed mode?","g_fullscreen Error", MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
g_fullscreen=false;
//If user doesn't accept Windowed Mode
else
{
MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
return 0; //Return Failure
}
}
}
if (g_fullscreen)//If we are still in g_fullscreen
{
dwExStyle=WS_EX_APPWINDOW; //Extened Windows Style
dwStyle=WS_POPUP; //Windows Stule
ShowCursor(false); //Hide Cursor
}
else
{
dwExStyle=WS_EX_APPWINDOW|WS_EX_WINDOWEDGE; //Extended Windows Style
dwStyle=WS_OVERLAPPEDWINDOW; //Windows Style
}
AdjustWindowRectEx(&WindowRect, dwStyle, false, dwExStyle); //Adjust Window to True Requested Size
//Create Window
if (!(hWnd=CreateWindowEx(dwExStyle, // Extended Style For The Window
"WINDOW", // Class Name
title, // Window Title
dwStyle | // Defined Window Style
WS_CLIPSIBLINGS | // Required Window Style
WS_CLIPCHILDREN, // Required Window Style
0, 0, // Window Position
WindowRect.right-WindowRect.left, // Calculate Window Width
WindowRect.bottom-WindowRect.top, // Calculate Window Height
NULL, // No Parent Window
NULL, // No Menu
hInstance, // Instance
NULL)))
{
//Error creating Window
KillGLWindow();//Reset the Display
MessageBox(NULL, "Error Creating Window", "Init Error", MB_OK|MB_ICONEXCLAMATION);
return 0;//Return Error
}
//Pixel Format
static PIXELFORMATDESCRIPTOR pfd= //pfd tells Windows how we want stuff
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
bits, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
if (!(hDC=GetDC(hWnd))) // Try to get a Device Context
{
//Error
KillGLWindow(); //Reset Display
MessageBox(NULL, "Unable to Create GL Device Context", "Init Error", MB_OK|MB_ICONEXCLAMATION);
return 0; //Return error
}
if (!(PixelFormat=ChoosePixelFormat(hDC, &pfd))) //Try to Find the Pixel Format
{
//Error
KillGLWindow(); //Reset Display
MessageBox(NULL, "Unable to find suitable PixelFormat", "Init Error", MB_OK| MB_ICONEXCLAMATION);
return 0; //Return Error
}
if (!SetPixelFormat(hDC,PixelFormat, &pfd)) //Try to Set the Pixel Format
{
//Error
KillGLWindow(); //Reset Display
MessageBox(NULL, "Unable to set PixelFormat", "Init Error", MB_OK| MB_ICONEXCLAMATION);
return 0; //Return Error
}
if (!(hRC=wglCreateContext(hDC))) //Try to Create a Rendering Context
{
//Error
KillGLWindow(); //Reset Display
MessageBox(NULL, "Cannot Create GL Rendering Context", "Init Error", MB_OK| MB_ICONEXCLAMATION);
return 0; //Return Error
}
if (!wglMakeCurrent(hDC,hRC)) //Try to Activate Rending Context
{
//Error
KillGLWindow(); //Reset Display
MessageBox(NULL, "Unable to Activate GL Rendering Context", "Init Error", MB_OK| MB_ICONEXCLAMATION);
return 0; //Return Error
}
ShowWindow(hWnd, SW_SHOW); //Show the Window
SetForegroundWindow(hWnd); //Higher Priority
SetFocus(hWnd); //Keyboard Focus
ReSizeGLScene(width, height); //Resize and Perspective
if (!InitGL()) //Initialize
{
//Error
KillGLWindow(); //Reset Display
MessageBox(NULL, "Initialisation Failed", "Init Error", MB_OK| MB_ICONEXCLAMATION);
return 0; //Return Error
}
return 1; //Window successfully created, return success
}
//****************************************************//
// WndProc //
// Handles Windows Messages //
//****************************************************//
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) //Check Messages
{
case WM_ACTIVATE: //Check for g_active Message
{
if (!HIWORD(wParam)) //Check Minimize State
g_active=true; //Program is g_active
else
g_active=false; //Program isn't g_active
return 0; //Return to Message loop
}
case WM_SYSCOMMAND: //Intercept System Commands
{
switch (wParam)
{
case SC_SCREENSAVE: //Is the Screensaver Trying to g_active
return 0; //Prevent from Happening
case SC_MONITORPOWER: //Is the Monitor try to Enter Powersave
return 0; //Prevent from happening
}
break; //Prevent from falling through
}
case WM_CLOSE: //Time to close
{
PostQuitMessage(0); //Sent a Quit Message
return 0; // Go Home
}
case WM_KEYDOWN: //Key is Being Pressed
{
g_keys[wParam]=true; //Mark Key being pressed down
return 0; //Go Home
}
case WM_KEYUP: //Key Released
{
g_keys[wParam]=false; //Mark Key being NOT pressed
return 0; //Go Home
}
case WM_SIZE: //Resize Window
{
ReSizeGLScene(LOWORD(lParam), HIWORD(lParam)); //LOWORD = width HIWORD= Height
return 0; // Go Home
}
}// End switch(uMsg)
return DefWindowProc(hWnd, uMsg, wParam, lParam); //Pass Unhandled Messages to DefWindowProc
}
//****************************************************//
// ReSizeGLScene //
// Resizes the viewport for OpenGL //
//****************************************************//
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
if (height==0) //If height is Zero
height = 1; //Prevent Division by Zero
glViewport(0, 0, width, height); //Reset the Current Viewport, Set boundries.
glMatrixMode(GL_PROJECTION); //Select the Projection Matrix
glLoadIdentity(); //Reset the Projection Matrix
// 1. 2. 3. 4.
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); //Calculates the Aspect Ratio of the Window. 1: View Angle 2: Width vs. Height Ratio 3 [img]http://www.opengl.org/discussion_boards/ubb/biggrin.gif[/img]istance to Camera before Clipping 4:Furthest distance from Camera before stops Drawing.
glMatrixMode(GL_MODELVIEW); //Select the Modelview Matrix
glLoadIdentity(); //Reset the Modelview Matrix
}
//****************************************************//
// InitGL //
// Handles OpenGL Initialisation //
//****************************************************//
int InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH); //Enables Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Clear to Black Background
glClearDepth(1.0f); //Depth Buffer Setup
glEnable(GL_DEPTH_TEST); //Enable Depth Testing
glDepthFunc(GL_LEQUAL); //Type of Depth Testing
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Best Perspective Correction
return 1; //Return a success.
}
//****************************************************//
// RenderGLScene //
// Draw and Render the Scene //
//****************************************************//
int RenderGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //Clear Screen and Depth Buffer
glLoadIdentity(); //Resent Current Modelview Matrix
glBegin(GL_TRIANGLES); //Begin Drawing
// R G B
glColor3f (1.0f, 0.0f, 0.0f);
glVertex3f (0.0f, 1.0f, 0.0f);
glColor3f ( 0.0f, 1.0f, 0.0f);
glVertex3f (-1.0f,-1.0f, 0.0f);
glColor3f (0.0f, 0.0f, 1.0f);
glVertex3f (1.0f,-1.0f, 0.0f);
glEnd();
return 1;
}
//****************************************************//
// KillGLWindow //
// Properly Destroy the Window //
//****************************************************//
GLvoid KillGLWindow(GLvoid)
{
if (g_fullscreen) //If the Window is g_fullscreened
{
ChangeDisplaySettings(NULL,0); //Return to the Desktop
ShowCursor(true); //Show the cursor
}
if (hRC) //If there is a Rendering Context
{
if (!wglMakeCurrent(NULL,NULL)) //Try to Release RC and DC
//If Unreleasable then Notify Error
MessageBox(NULL,"Release of DC and RC Failed","Shutdown Error",MB_OK|MB_ICONINFORMATION);
if (!wglDeleteContext(hRC)) //Try to Deleate RC
//If Undeletable then Notify Error
MessageBox(NULL, "Release of Rendering Context Failed","Shutdown Error", MB_OK|MB_ICONINFORMATION);
hRC=NULL; //Set hRC to Null
}
if (hDC && !ReleaseDC(hWnd, hDC)) // Try to Release DC
{
MessageBox(NULL, "Release of Devicec Context Failed","Shutdown Error", MB_OK|MB_ICONINFORMATION);
hDC=NULL; //Set DC to NULL
}
if (hWnd && !DestroyWindow(hWnd)) // Try to Destroy the Window
{
MessageBox(NULL, "Release of hWnd Failed","Shutdown Error", MB_OK|MB_ICONINFORMATION);
hWnd=NULL; //Set hWnd to NULL
}
if (!UnregisterClass("WINDOW", hInstance)) //Try to Unregister Class
{
MessageBox(NULL, "Unable to Unregister Class","Shutdown Error", MB_OK|MB_ICONINFORMATION);
hInstance=NULL; //Set RC to NULL
}
}
//****************************************************//
// WinMain //
// Hmmm. //
//****************************************************//
int WINAPI WinMain(HINSTANCE hINSTANCE, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg; //Windows Message
bool done=false; //Exit Loop
//Ask which mode user would like
if (MessageBox(NULL, "Wanna run in Fullscreen Mode?", "Display Mode", MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
g_fullscreen=true; //Select g_fullscreen (I think i set this Global at the start so this is redundant)
else
g_fullscreen=false; //Select Windowed Mode (Why would anyone choose Windowed?)
if (!CreateGLWindow("Window", 800,600,32,g_fullscreen)) //Try to create window
return 0; //Quit if Window was insuccessfully created
while (!done) //Loop until done=true
{
if (PeekMessage(&msg, NULL, 0,0, PM_REMOVE)) //Any messges?
{
if (msg.message==WM_QUIT) //Time to Quit?
done=true; //End loop
else
{
TranslateMessage(&msg); //Deal with Message
DispatchMessage(&msg); //Deal with Message
}
}
else //No Messages
{
if (g_active) //If the Program is g_active (Not Minimized)
{
if (g_keys[VK_ESCAPE]) //If ESC is being Pressed
done=true;
else
{
RenderGLScene(); //Render Scene
SwapBuffers(hDC); //Swap Buffers AKA Double Buffering
}
}
}
}//End While
//If we get Here its Time to Quit
KillGLWindow(); //Destroy Window Mwhahahaha
return (msg.wParam); //Exit
}
{/CODE]