Guest User

Untitled

a guest
Jul 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.16 KB | None | 0 0
  1. /** Includes **/
  2. #include "include/graphics.h"
  3.  
  4. /** Datum **/
  5. int programStatus=0;
  6. GLuint Tex;
  7.  
  8.  
  9.  
  10. /** Functions **/
  11. void GraphicsInit(void)
  12. {
  13.     Tex = loadBMP("bit.bmp");
  14.     printf("loaded bit.bmp as texture #%d\n",Tex);
  15.     const int  
  16.         windowWidth = 800,
  17.         windowHeight = 600;
  18.        
  19.     if (glfwInit() != GL_TRUE)
  20.         GraphicsShutDown(1);
  21.    
  22.     // 800 x 600, 16 bit color, no depth, alpha or stencil buffers, windowed
  23.     if (glfwOpenWindow(windowWidth, windowHeight, 8, 8, 8, 0, 0, 0, GLFW_WINDOW)
  24.     != GL_TRUE)
  25.         GraphicsShutDown(1);
  26.     glfwSetWindowTitle("Chase Hero!!!");
  27.     glClearColor(0, 0, 0, 0);
  28.     glMatrixMode(GL_PROJECTION);
  29.     glOrtho(0, 512, 512, 0, 0, 1); // Define 2D Coordinates, 512 x 512, no depth
  30.     glfwSetKeyCallback(keyboardCallback); // Callback for keyboard stuff
  31. }
  32.  
  33. void GraphicsShutDown(int return_code)
  34. {
  35.     glfwTerminate();
  36.     exit(return_code);
  37. }
  38.  
  39. void GraphicsMainLoop(void)
  40. {
  41.     // Draw the figure
  42.     Draw();
  43.     // Swap back and front buffers
  44.     glfwSwapBuffers();
  45. }
  46.  
  47. void GLFWCALL keyboardCallback(int key, int action)
  48. {
  49.     if(key == GLFW_KEY_ESC && action == GLFW_PRESS)
  50.         programStatus = 1;
  51.     else if(key == GLFW_KEY_LEFT && action == GLFW_PRESS)
  52.         programStatus = 2;
  53.     else if(key == GLFW_KEY_RIGHT && action == GLFW_PRESS)
  54.         programStatus = 3;
  55.     else if(key == GLFW_KEY_UP && action == GLFW_PRESS)
  56.         programStatus = 4;
  57.     else if(key == GLFW_KEY_DOWN && action == GLFW_PRESS)
  58.         programStatus = 5;
  59.     else
  60.         programStatus = 0;
  61. }
  62.  
  63. void Draw(void)
  64. {
  65.     int i, j;
  66.     glClear(GL_COLOR_BUFFER_BIT);   // Clear all the buffer!
  67.     for(i=0; i<10; i++)
  68.     {
  69.         for(j=0; j<10; j++)
  70.         {
  71.             glEnable(GL_TEXTURE_2D);
  72.             glBindTexture(GL_TEXTURE_2D, Tex);
  73.             glBegin(GL_QUADS);
  74.             {
  75.                 if((i+j)&1)
  76.                     glColor3f(1,1,1);
  77.                 else if(programStatus == 5)
  78.                     glColor3f(1,0,0);
  79.                 else if(programStatus == 4)
  80.                     glColor3f(0,0,1);
  81.                 else if(programStatus == 3)
  82.                     glColor3f(0,1,1);
  83.                 else if(programStatus == 2)
  84.                     glColor3f(1,1,0);
  85.                 else
  86.                     glColor3f(0,1,0);
  87.                 glTexCoord2f(0, 0);    
  88.                 glVertex2d(i*32+128,j*32+128);
  89.                 glTexCoord2f(1, 0);
  90.                 glVertex2d(i*32+160,j*32+128);
  91.                 glTexCoord2f(1, 1);
  92.                 glVertex2d(i*32+160,j*32+160);
  93.                 glTexCoord2f(0, 1);
  94.                 glVertex2d(i*32+128,j*32+160);
  95.                
  96.             }
  97.             glEnd();
  98.             glDisable(GL_TEXTURE_2D);
  99.         }
  100.     }
  101. }
  102.  
  103. GLuint loadBMP(const char * imagepath)
  104. {
  105.     /** Bitmap File layout **/
  106.     unsigned char header[54];   // Bitmap has 54 byte header
  107.     unsigned int dataPos;       // Position of the actual data
  108.     unsigned int width, height; // Sizes
  109.     unsigned int imageSize;     // width*height*3 (24 bit color)
  110.     unsigned char * data;       // Actual RGB data
  111.    
  112.     /** Loading the bitmap data into memory **/
  113.     FILE * file = fopen(imagepath,"rb");
  114.     if(!file)
  115.     {
  116.         fprintf(stderr, "404 - File %s not found", imagepath);
  117.         return 0;
  118.     }
  119.     if(fread(header, 1, 54, file) != 54) // If the header isn't there
  120.     {
  121.         fprintf(stderr, "%s not vaild BMP data", imagepath);    
  122.         return 0;
  123.     }
  124.     if(header[0]!='B'||header[1]!='M')
  125.     {
  126.         fprintf(stderr, "%s not vaild BMP data", imagepath);
  127.         return 0;
  128.     }
  129.     dataPos     =   *(int*)&(header[0x0A]);
  130.     imageSize   =   *(int*)&(header[0x22]);
  131.     width       =   *(int*)&(header[0x12]);
  132.     height      =   *(int*)&(header[0x16]);
  133.    
  134.     // Some BMP files are misformatted, lazy software, time to guess shit.
  135.     if(imageSize == 0)                  // Image size missing
  136.         imageSize = width * height * 3; // Assume 24 Bit Color
  137.     if(dataPos == 0)                    // No pointer to the actual pixel data
  138.         dataPos = 54;                   // Data usually is right after header
  139.     // NOTE: Those four lines are precautionary checks only.
  140.    
  141.     data = (unsigned char*)malloc(imageSize); // reserve memory for bitmap data.
  142.     fread(data, 1, imageSize, file);    // Read actual RGB data.
  143.     fclose(file);                       // Done reading the file, close it.
  144.    
  145.     /** Actual GL stuff here **/
  146.    
  147.     // Create one OpenGL approved Texture
  148.     GLuint textureID;
  149.     glGenTextures(1, &textureID);
  150.    
  151.     // Bind the texture to a number
  152.     glBindTexture(GL_TEXTURE_2D, textureID);
  153.     printf("%d: %d x %d\n", textureID, width, height);
  154.     // Pass the image to VRAM
  155.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  156.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  157.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  158.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  159.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB8, GL_UNSIGNED_BYTE, data);
  160.     free(data);
  161.     return textureID;
  162. }
Add Comment
Please, Sign In to add comment