Main Page   Namespace List   Compound List   File List   Compound Members   File Members  

main.cpp

Go to the documentation of this file.
00001 // ~~~ OpenGL & Win32 ~~~
00002 // Tutorial No.1 - Template
00003 // Alan Baylis 2001
00004 
00005 #include <windows.h>    // Header file for Windows
00006 #include "shared.h"
00007 #include "general.h"
00008 #include "vector.h"
00009 #include "vertex.h"
00010 #include "quat.h"
00011 #include "matrix.h"
00012 #include "texture.h"
00013 #include "locmath.h"    // Included Math functions (specific to OpenGL)
00014 #include "polygon.h"
00015 #include "mmgr.h"
00016 #include "resource.rh"  // Resources header file
00017 
00018 //Windows
00019 static HGLRC hRC;       // Permanent Rendering Context
00020 static HDC hDC;         // Private GDI Device Context
00021 HWND hWnd;              // Storage For Window Handle
00022 RECT screen;            // RECT struct for the screen dimensions
00023 PAINTSTRUCT ps;         // PAINTSTRUCT for WM_PAINT message handler
00024 
00025 // Input
00026 bool key[256];          // Array used for keyboard input
00027 bool released_key[256]; // Array used for toggled keyboard input
00028 
00029 // Math
00030 float pi = 3.141592;
00031 float radian = pi / 180;
00032 
00033 // Texture
00034 TEXTURE * texture = new TEXTURE[1];
00035 
00036 // Cube polygons
00037 POLYGON * polygon = new POLYGON[12];
00038 
00039 void InitGL(int Width, int Height)    // This is called after the main window is created
00040 {
00041     SetGLProperties();
00042     SetGLMaterial();
00043     SetGLLighting();
00044     SetGLView(Width, Height);
00045     SetGLScene(polygon);
00046     SetGLTexture(texture);
00047 }
00048 
00049 void ReSizeGLScene(int Width, int Height)
00050 {
00051     SetGLView(Width, Height);
00052 }
00053 
00054 void DrawGLScene(void)
00055 {
00056     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00057     DrawGrid();
00058     DrawCube(polygon, texture);
00059     DrawSphere();
00060     DrawCone();
00061 }
00062 
00063 LRESULT CALLBACK WndProc( HWND    hWnd,
00064                           UINT    message,
00065                           WPARAM  wParam,
00066                           LPARAM  lParam )
00067 {
00068     GLuint    PixelFormat;
00069     static    PIXELFORMATDESCRIPTOR pfd=
00070     {
00071         sizeof(PIXELFORMATDESCRIPTOR),   // Size Of This Pixel Format Descriptor
00072         1,                               // Version Number (?)
00073         PFD_DRAW_TO_WINDOW |             // Format Must Support Window
00074         PFD_SUPPORT_OPENGL |             // Format Must Support OpenGL
00075         PFD_DOUBLEBUFFER,                // Must Support Double Buffering
00076         PFD_TYPE_RGBA,                   // Request An RGBA Format
00077         16,                              // Select A 16Bit Color Depth
00078         0, 0, 0, 0, 0, 0,                // Color Bits Ignored (?)
00079         0,                               // No Alpha Buffer
00080         0,                               // Shift Bit Ignored (?)
00081         0,                               // No Accumulation Buffer
00082         0, 0, 0, 0,                      // Accumulation Bits Ignored (?)
00083         16,                              // 16Bit Z-Buffer (Depth Buffer)
00084         0,                               // No Stencil Buffer
00085         0,                               // No Auxiliary Buffer (?)
00086         PFD_MAIN_PLANE,                  // Main Drawing Layer
00087         0,                               // Reserved (?)
00088         0, 0, 0                          // Layer Masks Ignored (?)
00089     };
00090 
00091 
00092     switch (message)                // Tells Windows We Want To Check The Message
00093     {
00094         case WM_CREATE:
00095             hDC = GetDC(hWnd);      // Gets A Device Context For The Window
00096 
00097             PixelFormat = ChoosePixelFormat(hDC, &pfd); // Finds The Closest Match To The Pixel Format We Set Above
00098 
00099             if (!PixelFormat)
00100             {
00101                 MessageBox(0,"Can't Find A Suitable PixelFormat.","Error",MB_OK|MB_ICONERROR);
00102                 PostQuitMessage(0);      // This Sends A 'Message' Telling The Program To Quit
00103                 break;                   // Prevents The Rest Of The Code From Running
00104             }
00105 
00106             if(!SetPixelFormat(hDC,PixelFormat,&pfd))
00107             {
00108                 MessageBox(0,"Can't Set The PixelFormat.","Error",MB_OK|MB_ICONERROR);
00109                 PostQuitMessage(0);
00110                 break;
00111             }
00112 
00113             hRC = wglCreateContext(hDC);
00114             if(!hRC)
00115             {
00116                 MessageBox(0,"Can't Create A GL Rendering Context.","Error",MB_OK|MB_ICONERROR);
00117                 PostQuitMessage(0);
00118                 break;
00119             }
00120 
00121             if(!wglMakeCurrent(hDC, hRC))
00122             {
00123                 MessageBox(0,"Can't activate GLRC.","Error",MB_OK|MB_ICONERROR);
00124                 PostQuitMessage(0);
00125                 break;
00126             }
00127             InitGL(screen.right, screen.bottom);
00128         break;
00129 
00130         case WM_SYSCOMMAND:                          // Intercept System Commands
00131         {
00132             switch (wParam)                          // Check System Calls
00133             {
00134                 case SC_SCREENSAVE:                  // Screensaver Trying To Start?
00135                 case SC_MONITORPOWER:                // Monitor Trying To Enter Powersave?
00136                     return 0;                        // Prevent From Happening
00137             }
00138             break;                                   // Exit
00139         }
00140 
00141 // This WM_PAINT msg handler will be required when dialog windows are used
00142 /*
00143         case WM_PAINT:
00144             BeginPaint(hWnd,&ps);
00145             DrawGLScene();
00146             glFlush();
00147             SwapBuffers(hDC);
00148             EndPaint(hWnd,&ps);
00149         break;
00150 */
00151         case WM_DESTROY:
00152         case WM_CLOSE:
00153             delete[] texture;
00154             delete[] polygon;
00155             
00156             ChangeDisplaySettings(NULL, 0);
00157             wglMakeCurrent(hDC,NULL);
00158             wglDeleteContext(hRC);
00159             ReleaseDC(hWnd,hDC);
00160             PostQuitMessage(0);
00161         break;
00162 
00163         case WM_KEYDOWN:
00164             key[wParam] = TRUE;
00165         break;
00166 
00167         case WM_KEYUP:
00168             key[wParam] = FALSE;
00169         break;
00170 
00171         case WM_SIZE:
00172             ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
00173         break;
00174 
00175         default:
00176             return (DefWindowProc(hWnd, message, wParam, lParam));
00177     }
00178     return (0);
00179 }
00180 
00181 int WINAPI WinMain( HINSTANCE    hInstance,
00182                     HINSTANCE    hPrevInstance,
00183                     LPSTR        lpCmdLine,
00184                     int          nCmdShow)
00185 {
00186     MSG        msg;        // Windows Message Structure
00187     WNDCLASSEX    wc;      // Windows Class Structure Used To Set Up The Type Of Window
00188 
00189     GetWindowRect(GetDesktopWindow(), &screen);
00190     wc.cbSize           = sizeof(WNDCLASSEX);
00191     wc.style            = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_SAVEBITS;
00192     wc.lpfnWndProc      = (WNDPROC) WndProc;
00193     wc.cbClsExtra       = 0;
00194     wc.cbWndExtra       = 0;
00195     wc.hInstance        = hInstance;
00196     wc.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
00197     wc.hIconSm          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
00198     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
00199     wc.hbrBackground    = NULL;
00200     wc.lpszMenuName     = NULL;
00201     wc.lpszClassName    = "OpenGL WinClass";
00202 
00203 
00204     if(!RegisterClassEx(&wc))
00205     {
00206         MessageBox(0,"Failed To Register The Window Class.","Error",MB_OK|MB_ICONERROR);
00207         return FALSE;
00208     }
00209 
00210     hWnd = CreateWindowEx(
00211     WS_EX_LEFT,
00212     "OpenGL WinClass",
00213     "OpenGL & Win32 Tutorial No.1", // Title Appearing At The Top Of The Window
00214 
00215     WS_MAXIMIZE |
00216     WS_CLIPCHILDREN |
00217     WS_CLIPSIBLINGS |
00218     WS_POPUPWINDOW |
00219     WS_VISIBLE,
00220     0, 0,                          // The Position Of The Window On The Screen
00221     screen.right, screen.bottom,   // The Width And Height Of The WIndow
00222     NULL,
00223     NULL,
00224     hInstance,
00225     NULL);
00226 
00227     if(!hWnd)
00228     {
00229         MessageBox(0,"Window Creation Error.","Error",MB_OK|MB_ICONERROR);
00230         return FALSE;
00231     }
00232 
00233     DEVMODE dmScreenSettings;                              // Developer Mode
00234 
00235     memset(&dmScreenSettings, 0, sizeof(DEVMODE));         // Clear Room To Store Settings
00236     dmScreenSettings.dmSize        = sizeof(DEVMODE);      // Size Of The Devmode Structure
00237     dmScreenSettings.dmPelsWidth    = screen.right;        // Screen Width
00238     dmScreenSettings.dmPelsHeight    = screen.bottom;      // Screen Height
00239     dmScreenSettings.dmFields    = DM_PELSWIDTH | DM_PELSHEIGHT; // Pixel Mode
00240     ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN); // Switch To Full Screen
00241 
00242     ShowWindow(hWnd, SW_SHOW);
00243     UpdateWindow(hWnd);
00244     SetFocus(hWnd);
00245     wglMakeCurrent(hDC,hRC);
00246 
00247     while (1)
00248     {
00249         // Process All Messages
00250         while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
00251         {
00252             if (GetMessage(&msg, NULL, 0, 0))
00253             {
00254                 TranslateMessage(&msg);
00255                 DispatchMessage(&msg);
00256             }
00257             else
00258             {
00259                 return TRUE;
00260             }
00261         }
00262 
00263         DrawGLScene();    // Draw The Scene
00264         glFlush();        // Block the program until cached rendering commands are complete
00265         SwapBuffers(hDC); // Swap Screen Buffers
00266 
00267         if (key[VK_ESCAPE])
00268             SendMessage(hWnd,WM_CLOSE,0,0);
00269     }
00270 }

Generated on Fri Dec 23 05:22:19 2005 for Template by doxygen1.2.15