Advertisement
shardy

GLUT

Mar 26th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.94 KB | None | 0 0
  1.     #include <windows.h>        // Header File For Windows
  2.     #include <stdlib.h>
  3.     #include <stdio.h>
  4.     #include <gl\gl.h>          // Header File For The OpenGL32 Library
  5.     #include <gl\glu.h>         // Header File For The GLu32 Library
  6.     #include <GL/glut.h>
  7.     //#include <GL\glaux.h>       // or The Glaux Library
  8.  
  9.     #include "glaux.h"
  10.     #include <iostream>
  11.     #include <fstream>
  12.  
  13.     #define MAP_SIZEX 20
  14.     #define MAP_SIZEY 20
  15.       float offsetx = 0;
  16.       float offsety = 0;
  17.  
  18.     GLuint texture[2];
  19.  
  20.     char map[MAP_SIZEX][MAP_SIZEY] = {
  21.       {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  39.       {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  40.       {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},                    
  41.     };
  42.     void draw_tiles();
  43.  
  44.     bool    keys[256];          // Array Used For The Keyboard Routine
  45.     bool    active=TRUE;        // Window Active Flag Set To TRUE By Default
  46.     bool    fullscreen=TRUE;    // Fullscreen Flag Set To Fullscreen Mode By Default
  47.  
  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.  
  126.         // OFFSETS are how much the camera moves by when moves.
  127.         // try setting offsets to how much the character moves form input of keys instead and the character should also control the amount of movement.
  128.  
  129.         glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x+offsetx), float(y + offsety), 0.0f);
  130.         glTexCoord2f(1.0f, 0.0f); glVertex3f(float(x + 1 + offsetx), float(y + offsety), 0.0f);
  131.         glTexCoord2f(1.0f, 1.0f); glVertex3f(float(x + 1 + offsetx), float(y + 1 + offsety), 0.0f);
  132.         glTexCoord2f(0.0f, 1.0f); glVertex3f(float(x + offsetx), float(y + 1 + offsety), 0.0f);
  133.         glEnd();
  134.  
  135.         }
  136.       }
  137.     }
  138.  
  139.     GLvoid ReSizeGLScene(GLsizei width, GLsizei height)     // Resize And Initialize The GL Window
  140.     {
  141.         if (height==0)                                      // Prevent A Divide By Zero By
  142.         {
  143.             height=1;                                       // Making Height Equal One
  144.         }
  145.  
  146.         glViewport(0,0,width,height);                       // Reset The Current Viewport
  147.  
  148.         glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
  149.         glLoadIdentity();                                   // Reset The Projection Matrix
  150.  
  151.         // Calculate The Aspect Ratio Of The Window
  152.         gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,20.0f);
  153.  
  154.         glMatrixMode(GL_MODELVIEW);                         // Select The Modelview Matrix
  155.         glLoadIdentity();                                   // Reset The Modelview Matrix
  156.     }
  157.  
  158.     int init(GLvoid)                                // All Setup For OpenGL Goes Here
  159.     {
  160.         if (!LoadGLTextures())                          // Jump To Texture Loading Routine ( NEW )
  161.         {
  162.             return FALSE;                           // If Texture Didn't Load Return FALSE ( NEW )
  163.         }
  164.  
  165.         glEnable(GL_TEXTURE_2D);                        // Enable Texture Mapping ( NEW )
  166.         glShadeModel(GL_SMOOTH);                        // Enable Smooth Shading
  167.         glClearColor(0.0f, 0.0f, 0.0f, 0.5f);                   // Black Background
  168.         glClearDepth(1.0f);                         // Depth Buffer Setup
  169.         glEnable(GL_DEPTH_TEST);                        // Enables Depth Testing
  170.         glDepthFunc(GL_LEQUAL);                         // The Type Of Depth Testing To Do
  171.         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);          // Really Nice Perspective Calculations
  172.         return TRUE;                                // Initialization Went OK
  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;
  183.     }
  184.                
  185.     void display(void)
  186.     {
  187.         glClear(GL_COLOR_BUFFER_BIT);
  188.             DrawGLScene();
  189.             //ReSizeGLScene(640,480);
  190.         glutSwapBuffers();
  191.     }
  192.            
  193.     void mySKeyboard(int key, int x, int y)
  194.     {
  195.         switch(key){
  196.         case GLUT_KEY_LEFT:  offsetx += 0.04f; break;
  197.         case GLUT_KEY_RIGHT: offsetx -= 0.04f; break;
  198.         case GLUT_KEY_UP:    offsety -= 0.04f; break;
  199.         case GLUT_KEY_DOWN:  offsety += 0.04f; break;
  200.         default: break;}
  201.     }
  202.  
  203.     void update(){ glutPostRedisplay();}
  204.  
  205.     void main (int argc, char** argv)
  206.     {
  207.         glutInit(&argc, argv);
  208.         glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  209.         glutInitWindowPosition(100, 100);
  210.         glutInitWindowSize(640, 480);
  211.         glutCreateWindow("BasicPlatform");
  212.         init();
  213.         glutDisplayFunc(display);
  214.         glutReshapeFunc(ReSizeGLScene);
  215.         glutSpecialFunc(mySKeyboard);
  216.         glutIdleFunc(update);
  217.         glutMainLoop();
  218.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement