Advertisement
shardy

WIN32

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