Guest User

Untitled

a guest
Jan 13th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.27 KB | None | 0 0
  1. /*
  2.  *      This Code Was Created By Jeff Molofee 2000
  3.  *      A HUGE Thanks To Fredric Echols For Cleaning Up
  4.  *      And Optimizing This Code, Making It More Flexible!
  5.  *      If You've Found This Code Useful, Please Let Me Know.
  6.  *      Visit My Site At nehe.gamedev.net
  7.  */
  8.  
  9. #include <windows.h>        // Header File For Windows
  10. #include <gl\gl.h>          // Header File For The OpenGL32 Library
  11. #include <gl\glu.h>         // Header File For The GLu32 Library
  12. #include <gl\glaux.h>       // Header File For The Glaux Library
  13. #include <stdio.h>  
  14. #include <string>
  15. #define FlNum 2
  16. HDC         hDC=NULL;       // Private GDI Device Context
  17. HGLRC       hRC=NULL;       // Permanent Rendering Context
  18. HWND        hWnd=NULL;      // Holds Our Window Handle
  19. HINSTANCE   hInstance;      // Holds The Instance Of The Application
  20.  
  21. bool    keys[256];          // Array Used For The Keyboard Routine
  22. bool    active=TRUE;        // Window Active Flag Set To TRUE By Default
  23. bool    fullscreen=TRUE;    // Fullscreen Flag Set To Fullscreen Mode By Default
  24. bool    r = TRUE;
  25. bool    dbjump = FALSE;
  26.  
  27.  
  28. GLfloat     xPlace = 0.0f;
  29. GLfloat     yPlace = 2.0f;
  30. GLfloat     ySpeed;
  31. GLuint      nFace;
  32. GLuint      texture[4];
  33. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);   // Declaration For WndProc
  34.  
  35. class floor
  36. {
  37.     float xFl, yFl, xFl2, yFl2;
  38.     public:
  39.         void Fpaint();
  40.         floor();
  41.         floor(float, float, float, float);
  42.         bool Fcheck(float, float, float, float);
  43. };
  44. floor::floor(float xF, float xF2, float yF, float yF2)
  45. {
  46.     xFl = xF;
  47.     yFl = yF;
  48.     xFl2 = xF2;
  49.     yFl2 = yF2;
  50. }
  51.  
  52. void floor::Fpaint()
  53. {  
  54.     glTranslatef(0.0f, 0.0f,-13.0f);
  55.     glColor3f(0.0f,1.0f,0.0f);              
  56.     glBegin(GL_QUADS);                  // Start Drawing Quads
  57.         glVertex3f(xFl, yFl2, 0.0f);          
  58.         glVertex3f(xFl2, yFl2, 0.0f);          
  59.         glVertex3f(xFl2,yFl, 0.0f);          
  60.         glVertex3f(xFl,yFl, 0.0f);          
  61.     glEnd();          
  62. }
  63. bool floor::Fcheck(float xP, float xP2, float yP, float yP2)
  64. {
  65.     if( ( (yP <= yFl &&  yP2 >= yFl)  || (yP <= yFl2 &&  yP2 >= yFl2) || (yFl <= yP &&  yFl2 >= yP)  || (yFl <= yP2 &&  yFl2 >= yP2)  ) &&   ((xFl <= xP && xFl2 >= xP) || (xFl <= xP2 && xFl2 >= xP2) || (xP <= xFl && xP2 >= xFl) || (xP <= xFl2 && xP2 >= xFl2)) )
  66.     {    
  67.         return TRUE;
  68.     }
  69.     return FALSE;
  70. }
  71.  
  72.  
  73.        // declaration of a new array
  74. floor f[] = {floor(0.0f, 4.0f, -1.0f, 0.5f), floor(4.0f, 6.0f, -1.0f, 3.5f), floor(-6.0f, 6.0f, -5.0f, -2.0f)};        // access to an element of the array.
  75.  
  76. bool FlCheck(float xP, float xP2, float yP, float yP2)
  77. {
  78.     for(int i=0; i<=FlNum; i+=1)
  79.     {
  80.         if(f[i].Fcheck(xP, xP2, yP, yP2))
  81.             return TRUE;
  82.     }
  83.     return FALSE;
  84. }
  85.  
  86. void Pmove(float &xP, float &yP, float &yS)
  87. {
  88.     if(FlCheck(xPlace, xPlace+2.0f, yPlace-1.0f+yS, yPlace+1.0f+yS) && yS < 0)
  89.         while(!FlCheck(xPlace, xPlace+2.0f, yPlace-1.001f, yPlace+0.999f))
  90.         {
  91.                 yP -= 0.001;
  92.         }
  93.     else if(FlCheck(xPlace, xPlace+2.0f, yPlace-1.0f+yS, yPlace+1.0f+yS) && yS > 0)
  94.         while(!FlCheck(xPlace, xPlace+2.0f, yPlace-0.999f, yPlace+1.001f))
  95.         {
  96.                 yP -= 0.001;
  97.         }
  98.     if(FlCheck(xPlace, xPlace+2.0f, yPlace-1.001f, yPlace+0.999f))
  99.     {
  100.         yS = 0;
  101.         dbjump = TRUE;
  102.         if(keys[VK_UP] && r)
  103.         {
  104.             yS = 0.075f;
  105.             r = false;
  106.         }
  107.         nFace = 0;
  108.     }
  109.     else
  110.     {
  111.                 nFace = 1;
  112.                 yS -= 0.0025f;
  113.                 if(keys[VK_UP] && r && dbjump)
  114.                 {
  115.                     yS = 0.1f;
  116.                     r = false;
  117.                     dbjump = false;
  118.                 }
  119.     }
  120.     if(!keys[VK_UP])
  121.         r = TRUE;
  122.     if(yS != 0)
  123.         if(!FlCheck(xPlace, xPlace+2.0f, yPlace-1.0f+yS, yPlace+1.0f+yS))
  124.             yP += yS;
  125.    
  126.     if(keys[VK_RIGHT] )
  127.     {
  128.         if (!FlCheck(xP+0.05f, xP+2.0f+0.05f, yP-1.0f, yP+1.0f))
  129.             xP += 0.05f;
  130.         else if(FlCheck(xP+0.09f, xP+2.0f+0.09f, yP-1.0f, yP+1.0f))
  131.             while(!FlCheck(xP+0.01f, xP+2.0f+0.01f, yP-1.0f, yP+1.0f))
  132.             {
  133.                 xP += 0.01;
  134.             }
  135.         nFace = 2;
  136.     }
  137.     else if(keys[VK_LEFT] )
  138.     {
  139.         if (!FlCheck(xP-0.05f, xP+2.0f-0.05f, yP-1.0f, yP+1.0f))
  140.             xP -= 0.05f;
  141.         else if(FlCheck(xP-0.09f, xP+2.0f-0.09f, yP-1.0f, yP+1.0f))
  142.             while(!FlCheck(xP-0.01f, xP+2.0f-0.01f, yP-1.0f, yP+1.0f))
  143.             {
  144.                 xP -= 0.01;
  145.             }
  146.         nFace = 3;
  147.     }
  148. }
  149.  
  150. AUX_RGBImageRec *LoadBMP(char *Filename)                // Loads A Bitmap Image
  151. {
  152.     FILE *File=NULL;                                    // File Handle
  153.  
  154.     if (!Filename)                                      // Make Sure A Filename Was Given
  155.     {
  156.         return NULL;                                    // If Not Return NULL
  157.     }
  158.  
  159.     File=fopen(Filename,"r");                           // Check To See If The File Exists
  160.  
  161.     if (File)                                           // Does The File Exist?
  162.     {
  163.         fclose(File);                                   // Close The Handle
  164.         return auxDIBImageLoad(Filename);               // Load The Bitmap And Return A Pointer
  165.     }
  166.  
  167.     return NULL;                                        // If Load Failed Return NULL
  168. }
  169.  
  170. int LoadGLTextures(std::string fName, int fNum)                                 // Load Bitmaps And Convert To Textures
  171. {
  172.     int Status=FALSE;                                   // Status Indicator
  173.  
  174.     AUX_RGBImageRec *TextureImage[4];                   // Create Storage Space For The Texture
  175.  
  176.     memset(TextureImage,0,sizeof(void *)*1);            // Set The Pointer To NULL
  177.  
  178.     // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
  179.     if (TextureImage[fNum]=LoadBMP((char*)fName.c_str()))
  180.     {
  181.                 Status=TRUE;                                    // Set The Status To TRUE
  182.  
  183.                 glGenTextures(1, &texture[fNum]);                                  // Create The Texture
  184.  
  185.                 // Typical Texture Generation Using Data From The Bitmap
  186.                 glBindTexture(GL_TEXTURE_2D, texture[fNum]);
  187.                 glTexImage2D(GL_TEXTURE_2D, 0, 4, TextureImage[fNum]->sizeX, TextureImage[fNum]->sizeY, 1, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[fNum]->data);
  188.                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  189.                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  190.     }
  191.  
  192.     if (TextureImage[0])                                    // If Texture Exists
  193.     {
  194.         if (TextureImage[0]->data)                          // If Texture Image Exists
  195.         {
  196.             free(TextureImage[0]->data);                    // Free The Texture Image Memory
  197.         }
  198.  
  199.         free(TextureImage[0]);                              // Free The Image Structure
  200.     }
  201.  
  202.     return Status;                                      // Return The Status
  203. }
  204.  
  205. GLvoid ReSizeGLScene(GLsizei width, GLsizei height)     // Resize And Initialize The GL Window
  206. {
  207.     if (height==0)                                      // Prevent A Divide By Zero By
  208.     {
  209.         height=1;                                       // Making Height Equal One
  210.     }
  211.     glViewport(0,0, width,height);                      // Reset The Current Viewport
  212.  
  213.     glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
  214.     glLoadIdentity();                                   // Reset The Projection Matrix
  215.  
  216.     // Calculate The Aspect Ratio Of The Window
  217.     gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
  218.  
  219.     glMatrixMode(GL_MODELVIEW);                         // Select The Modelview Matrix
  220.     glLoadIdentity();
  221.                                         // Reset The Modelview Matrix
  222. }
  223.  
  224. int InitGL(GLvoid)                                      // All Setup For OpenGL Goes Here
  225. {
  226.     if(!LoadGLTextures(std::string("texture/Face0.bmp"), 0))
  227.        return FALSE;
  228.     if(!LoadGLTextures(std::string("texture/Face1.bmp"), 1))
  229.        return FALSE;
  230.     if(!LoadGLTextures(std::string("texture/Face2.bmp"), 2))
  231.        return FALSE;
  232.     if(!LoadGLTextures(std::string("texture/Face3.bmp"), 3))
  233.        return FALSE;
  234.    
  235.     glEnable(GL_TEXTURE_2D);
  236.     glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
  237.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);               // Black Background
  238.     glClearDepth(1.0f);                                 // Depth Buffer Setup
  239.     glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
  240.     glDepthFunc(GL_LEQUAL);                             // The Type Of Depth Testing To Do
  241.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective Calculations
  242.    
  243.     return TRUE;                                        // Initialization Went OK
  244. }
  245.  
  246. int DrawGLScene(GLvoid)                                 // Here's Where We Do All The Drawing
  247. {
  248.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
  249.         // Reset The Current Modelview Matrix  
  250.            
  251.     glLoadIdentity();
  252.     f[0].Fpaint();
  253.     glLoadIdentity();
  254.     f[1].Fpaint();
  255.     glLoadIdentity();
  256.     f[2].Fpaint();
  257.    
  258.     glLoadIdentity();
  259.     glTranslatef(0.0f, 0.0f, -13.0f);
  260.     glBindTexture(GL_TEXTURE_2D, texture[nFace]); //use texture 0
  261.     glColor3f(1.0f,1.0f,1.0f); // color white
  262.     glBegin(GL_POLYGON); //create polygon
  263.     glTexCoord2f(0.0f, 0.0f);glVertex3f( xPlace,yPlace-1.0f, 0.0f);  //down left
  264.     glTexCoord2f(0.0f,  1.0f);glVertex3f( xPlace, yPlace+1.0f, 0.0f);       //up  left  
  265.     glTexCoord2f(1.0f, 1.0f);glVertex3f( xPlace+2.0f, yPlace+1.0f, 0.0f);           //up right
  266.     glTexCoord2f(1.0f, 0.0f);glVertex3f( xPlace+2.0f, yPlace-1.0f, 0.0f);       //down right
  267.     glEnd();
  268.     Pmove(xPlace, yPlace, ySpeed);
  269.    
  270.    
  271.     return TRUE;                                        // Everything Went OK
  272. }
  273.  
  274. GLvoid KillGLWindow(GLvoid)                             // Properly Kill The Window
  275. {
  276.  
  277.     if (fullscreen)                                     // Are We In Fullscreen Mode?
  278.     {
  279.         ChangeDisplaySettings(NULL,0);                  // If So Switch Back To The Desktop
  280.         ShowCursor(TRUE);                               // Show Mouse Pointer
  281.     }
  282.  
  283.     if (hRC)                                            // Do We Have A Rendering Context?
  284.     {
  285.         if (!wglMakeCurrent(NULL,NULL))                 // Are We Able To Release The DC And RC Contexts?
  286.         {
  287.             MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  288.         }
  289.  
  290.         if (!wglDeleteContext(hRC))                     // Are We Able To Delete The RC?
  291.         {
  292.             MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  293.         }
  294.         hRC=NULL;                                       // Set RC To NULL
  295.     }
  296.  
  297.     if (hDC && !ReleaseDC(hWnd,hDC))                    // Are We Able To Release The DC
  298.     {
  299.         MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  300.         hDC=NULL;                                       // Set DC To NULL
  301.     }
  302.  
  303.     if (hWnd && !DestroyWindow(hWnd))                   // Are We Able To Destroy The Window?
  304.     {
  305.         MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  306.         hWnd=NULL;                                      // Set hWnd To NULL
  307.     }
  308.  
  309.     if (!UnregisterClass("OpenGL",hInstance))           // Are We Able To Unregister Class
  310.     {
  311.         MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  312.         hInstance=NULL;                                 // Set hInstance To NULL
  313.     }
  314. }
  315.  
  316. /*  This Code Creates Our OpenGL Window.  Parameters Are:                   *
  317.  *  title           - Title To Appear At The Top Of The Window              *
  318.  *  width           - Width Of The GL Window Or Fullscreen Mode             *
  319.  *  height          - Height Of The GL Window Or Fullscreen Mode            *
  320.  *  bits            - Number Of Bits To Use For Color (8/16/24/32)          *
  321.  *  fullscreenflag  - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE)   */
  322.  
  323.  
  324. BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
  325. {
  326.    
  327.     GLuint      PixelFormat;            // Holds The Results After Searching For A Match
  328.     WNDCLASS    wc;                     // Windows Class Structure
  329.     DWORD       dwExStyle;              // Window Extended Style
  330.     DWORD       dwStyle;                // Window Style
  331.     RECT        WindowRect;             // Grabs Rectangle Upper Left / Lower Right Values
  332.     WindowRect.left=(long)0;            // Set Left Value To 0
  333.     WindowRect.right=(long)width;       // Set Right Value To Requested Width
  334.     WindowRect.top=(long)0;             // Set Top Value To 0
  335.     WindowRect.bottom=(long)height;     // Set Bottom Value To Requested Height
  336.  
  337.    
  338.    
  339.     fullscreen=fullscreenflag;          // Set The Global Fullscreen Flag
  340.  
  341.     hInstance           = GetModuleHandle(NULL);                // Grab An Instance For Our Window
  342.     wc.style            = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;   // Redraw On Size, And Own DC For Window.
  343.     wc.lpfnWndProc      = (WNDPROC) WndProc;                    // WndProc Handles Messages
  344.     wc.cbClsExtra       = 0;                                    // No Extra Window Data
  345.     wc.cbWndExtra       = 0;                                    // No Extra Window Data
  346.     wc.hInstance        = hInstance;                            // Set The Instance
  347.     wc.hIcon            = LoadIcon(NULL, IDI_WINLOGO);          // Load The Default Icon
  348.     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);          // Load The Arrow Pointer
  349.     wc.hbrBackground    = NULL;                                 // No Background Required For GL
  350.     wc.lpszMenuName     = NULL;                                 // We Don't Want A Menu
  351.     wc.lpszClassName    = "OpenGL";                             // Set The Class Name
  352.  
  353.     if (!RegisterClass(&wc))                                    // Attempt To Register The Window Class
  354.     {
  355.         MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  356.         return FALSE;                                           // Return FALSE
  357.     }
  358.    
  359.     if (fullscreen)                                             // Attempt Fullscreen Mode?
  360.     {
  361.         DEVMODE dmScreenSettings;                               // Device Mode
  362.         memset(&dmScreenSettings,0,sizeof(dmScreenSettings));   // Makes Sure Memory's Cleared
  363.         dmScreenSettings.dmSize=sizeof(dmScreenSettings);       // Size Of The Devmode Structure
  364.         dmScreenSettings.dmPelsWidth    = width;                // Selected Screen Width
  365.         dmScreenSettings.dmPelsHeight   = height;               // Selected Screen Height
  366.         dmScreenSettings.dmBitsPerPel   = bits;                 // Selected Bits Per Pixel
  367.         dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
  368.  
  369.         // Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
  370.         if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
  371.         {
  372.             // If The Mode Fails, Offer Two Options.  Quit Or Use Windowed Mode.
  373.             if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
  374.             {
  375.                 fullscreen=FALSE;       // Windowed Mode Selected.  Fullscreen = FALSE
  376.             }
  377.             else
  378.             {
  379.                 // Pop Up A Message Box Letting User Know The Program Is Closing.
  380.                 MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
  381.                 return FALSE;                                   // Return FALSE
  382.             }
  383.         }
  384.     }
  385.  
  386.     if (fullscreen)                                             // Are We Still In Fullscreen Mode?
  387.     {
  388.         dwExStyle=WS_EX_APPWINDOW;                              // Window Extended Style
  389.         dwStyle=WS_POPUP;                                       // Windows Style
  390.         ShowCursor(FALSE);                                      // Hide Mouse Pointer
  391.     }
  392.     else
  393.     {
  394.         dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;           // Window Extended Style
  395.         dwStyle=WS_OVERLAPPEDWINDOW;                            // Windows Style
  396.     }
  397.  
  398.     AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);     // Adjust Window To True Requested Size
  399.  
  400.     // Create The Window
  401.     if (!(hWnd=CreateWindowEx(  dwExStyle,                          // Extended Style For The Window
  402.                                 "OpenGL",                           // Class Name
  403.                                 title,                              // Window Title
  404.                                 dwStyle |                           // Defined Window Style
  405.                                 WS_CLIPSIBLINGS |                   // Required Window Style
  406.                                 WS_CLIPCHILDREN,                    // Required Window Style
  407.                                 0, 0,                               // Window Position
  408.                                 WindowRect.right-WindowRect.left,   // Calculate Window Width
  409.                                 WindowRect.bottom-WindowRect.top,   // Calculate Window Height
  410.                                 NULL,                               // No Parent Window
  411.                                 NULL,                               // No Menu
  412.                                 hInstance,                          // Instance
  413.                                 NULL)))                             // Dont Pass Anything To WM_CREATE
  414.     {
  415.         KillGLWindow();                             // Reset The Display
  416.         MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  417.         return FALSE;                               // Return FALSE
  418.     }
  419.  
  420.     static  PIXELFORMATDESCRIPTOR pfd=              // pfd Tells Windows How We Want Things To Be
  421.     {
  422.         sizeof(PIXELFORMATDESCRIPTOR),              // Size Of This Pixel Format Descriptor
  423.         1,                                          // Version Number
  424.         PFD_DRAW_TO_WINDOW |                        // Format Must Support Window
  425.         PFD_SUPPORT_OPENGL |                        // Format Must Support OpenGL
  426.         PFD_DOUBLEBUFFER,                           // Must Support Double Buffering
  427.         PFD_TYPE_RGBA,                              // Request An RGBA Format
  428.         bits,                                       // Select Our Color Depth
  429.         0, 0, 0, 0, 0, 0,                           // Color Bits Ignored
  430.         0,                                          // No Alpha Buffer
  431.         0,                                          // Shift Bit Ignored
  432.         0,                                          // No Accumulation Buffer
  433.         0, 0, 0, 0,                                 // Accumulation Bits Ignored
  434.         16,                                         // 16Bit Z-Buffer (Depth Buffer)  
  435.         0,                                          // No Stencil Buffer
  436.         0,                                          // No Auxiliary Buffer
  437.         PFD_MAIN_PLANE,                             // Main Drawing Layer
  438.         0,                                          // Reserved
  439.         0, 0, 0                                     // Layer Masks Ignored
  440.     };
  441.    
  442.     if (!(hDC=GetDC(hWnd)))                         // Did We Get A Device Context?
  443.     {
  444.         KillGLWindow();                             // Reset The Display
  445.         MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  446.         return FALSE;                               // Return FALSE
  447.     }
  448.  
  449.     if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
  450.     {
  451.         KillGLWindow();                             // Reset The Display
  452.         MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  453.         return FALSE;                               // Return FALSE
  454.     }
  455.  
  456.     if(!SetPixelFormat(hDC,PixelFormat,&pfd))       // Are We Able To Set The Pixel Format?
  457.     {
  458.         KillGLWindow();                             // Reset The Display
  459.         MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  460.         return FALSE;                               // Return FALSE
  461.     }
  462.  
  463.     if (!(hRC=wglCreateContext(hDC)))               // Are We Able To Get A Rendering Context?
  464.     {
  465.         KillGLWindow();                             // Reset The Display
  466.         MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  467.         return FALSE;                               // Return FALSE
  468.     }
  469.  
  470.     if(!wglMakeCurrent(hDC,hRC))                    // Try To Activate The Rendering Context
  471.     {
  472.         KillGLWindow();                             // Reset The Display
  473.         MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  474.         return FALSE;                               // Return FALSE
  475.     }
  476.  
  477.     ShowWindow(hWnd,SW_SHOW);                       // Show The Window
  478.     SetForegroundWindow(hWnd);                      // Slightly Higher Priority
  479.     SetFocus(hWnd);                                 // Sets Keyboard Focus To The Window
  480.     ReSizeGLScene(width, height);                   // Set Up Our Perspective GL Screen
  481.  
  482.     if (!InitGL())                                  // Initialize Our Newly Created GL Window
  483.     {
  484.         KillGLWindow();                             // Reset The Display
  485.         MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  486.         return FALSE;                               // Return FALSE
  487.     }
  488.  
  489.     return TRUE;                                    // Success
  490. }
  491.  
  492. LRESULT CALLBACK WndProc(   HWND    hWnd,           // Handle For This Window
  493.                             UINT    uMsg,           // Message For This Window
  494.                             WPARAM  wParam,         // Additional Message Information
  495.                             LPARAM  lParam)         // Additional Message Information
  496. {
  497.     switch (uMsg)                                   // Check For Windows Messages
  498.     {
  499.         case WM_ACTIVATE:                           // Watch For Window Activate Message
  500.         {
  501.             if (!HIWORD(wParam))                    // Check Minimization State
  502.             {
  503.                 active=TRUE;                        // Program Is Active
  504.             }
  505.             else
  506.             {
  507.                 active=FALSE;                       // Program Is No Longer Active
  508.             }
  509.  
  510.             return 0;                               // Return To The Message Loop
  511.         }
  512.        
  513.         case WM_SYSCOMMAND:                         // Intercept System Commands
  514.         {
  515.             switch (wParam)                         // Check System Calls
  516.             {
  517.                 case SC_SCREENSAVE:                 // Screensaver Trying To Start?
  518.                 case SC_MONITORPOWER:               // Monitor Trying To Enter Powersave?
  519.                 return 0;                           // Prevent From Happening
  520.             }
  521.             break;                                  // Exit
  522.         }
  523.  
  524.         case WM_CLOSE:                              // Did We Receive A Close Message?
  525.         {
  526.             PostQuitMessage(0);                     // Send A Quit Message
  527.             return 0;                               // Jump Back
  528.         }
  529.  
  530.         case WM_KEYDOWN:                            // Is A Key Being Held Down?
  531.         {
  532.             keys[wParam] = TRUE;                    // If So, Mark It As TRUE
  533.             return 0;                               // Jump Back
  534.         }
  535.  
  536.         case WM_KEYUP:                              // Has A Key Been Released?
  537.         {
  538.             keys[wParam] = FALSE;                   // If So, Mark It As FALSE
  539.             return 0;                               // Jump Back
  540.         }
  541.  
  542.         case WM_SIZE:                               // Resize The OpenGL Window
  543.         {
  544.             ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));  // LoWord=Width, HiWord=Height
  545.             return 0;                               // Jump Back
  546.         }
  547.     }
  548.  
  549.     // Pass All Unhandled Messages To DefWindowProc
  550.     return DefWindowProc(hWnd,uMsg,wParam,lParam);
  551. }
  552.  
  553. int WINAPI WinMain( HINSTANCE   hInstance,          // Instance
  554.                     HINSTANCE   hPrevInstance,      // Previous Instance
  555.                     LPSTR       lpCmdLine,          // Command Line Parameters
  556.                     int         nCmdShow)           // Window Show State
  557. {
  558.  
  559.     MSG     msg;                                    // Windows Message Structure
  560.     BOOL    done=FALSE;                             // Bool Variable To Exit Loop
  561.  
  562.     // Ask The User Which Screen Mode They Prefer
  563.  
  564.    fullscreen=FALSE;                            // Windowed Mode
  565.    
  566.    
  567.     // Create Our OpenGL Window
  568.     if (!CreateGLWindow("Game",640,480,16,fullscreen))
  569.     {
  570.         return 0;                                   // Quit If Window Was Not Created
  571.     }
  572.  
  573.     while(!done)                                    // Loop That Runs While done=FALSE
  574.     {
  575.         if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))   // Is There A Message Waiting?
  576.         {
  577.             if (msg.message==WM_QUIT)               // Have We Received A Quit Message?
  578.             {
  579.                 done=TRUE;                          // If So done=TRUE
  580.             }
  581.             else                                    // If Not, Deal With Window Messages
  582.             {
  583.                 TranslateMessage(&msg);             // Translate The Message
  584.                 DispatchMessage(&msg);              // Dispatch The Message
  585.             }
  586.         }
  587.         else                                        // If There Are No Messages
  588.         {
  589.             // Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
  590.             if (active)                             // Program Active?
  591.             {
  592.                 if (keys[VK_ESCAPE])                // Was ESC Pressed?
  593.                 {
  594.                     done=TRUE;                      // ESC Signalled A Quit
  595.                 }
  596.                 else                                // Not Time To Quit, Update Screen
  597.                 {
  598.                     DrawGLScene();                  // Draw The Scene
  599.                     SwapBuffers(hDC);               // Swap Buffers (Double Buffering)
  600.                 }
  601.             }
  602.            
  603.             if (keys[VK_F1])                        // Is F1 Being Pressed?
  604.             {
  605.                 keys[VK_F1]=FALSE;                  // If So Make Key FALSE
  606.                 KillGLWindow();                     // Kill Our Current Window
  607.                 fullscreen=!fullscreen;             // Toggle Fullscreen / Windowed Mode
  608.                 // Recreate Our OpenGL Window
  609.                 if (!CreateGLWindow("Game",400,400,16,fullscreen))
  610.                 {
  611.                     return 0;                       // Quit If Window Was Not Created
  612.                 }
  613.             }
  614.         }
  615.     }
  616.  
  617.     // Shutdown
  618.     KillGLWindow();                                 // Kill The Window
  619.     return (msg.wParam);                            // Exit The Program
  620. }
Add Comment
Please, Sign In to add comment