Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.54 KB | None | 0 0
  1. // GLUT/Open_GL Single buffer example using some basic primitives in 2D
  2. #include "stdafx.h"
  3. #include <stdarg.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define _USE_MATH_DEFINES // Required to access M_PI
  8. #include <math.h>         // Required to access sin() and cos()  
  9.  
  10. #include <freeglut.h>
  11.  
  12. // Function prototype for loading texture method
  13. GLuint glmLoadTextureBMP(char *);
  14.  
  15. // Global variables
  16. int sprite_x_position = 320;
  17. bool direction = true;
  18. int frame=1;
  19.  
  20. static void display(void)
  21. {
  22.     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  23.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  24.  
  25.     glEnable(GL_TEXTURE_2D);     // Enable use of texture uv mapping
  26.     glDisable(GL_DEPTH_TEST);    // Depth testing not required (2D only 1 sprite)
  27.     glDisable(GL_LIGHTING);   // Do not include lighting (yet)
  28.     glEnable(GL_BLEND);       // Enable Alpha blending of textures
  29.                               // Screen pixel=(Exisiting screen pixel*(1-Alpha)) + (New pixel * Alpha)
  30.                               // Transparent pixels RGBA = (0,0,0,0) Image pixels = (R,G,B,1)
  31.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  32.     glBegin(GL_QUADS);
  33.     if (direction)
  34.     {
  35.         glTexCoord2f(frame / 10.0, 0);
  36.         glVertex2i(sprite_x_position, 200);
  37.         glTexCoord2f((frame + 1) / 10.0, 0);
  38.         glVertex2i(sprite_x_position + 150, 200);
  39.         glTexCoord2f((frame + 1) / 10.0, 1);
  40.         glVertex2i(sprite_x_position + 150, 350);
  41.         glTexCoord2f(frame / 10.0, 1);
  42.         glVertex2i(sprite_x_position, 350);
  43.     }
  44.     else
  45.     {
  46.         glTexCoord2f((frame + 1) / 10.0, 0);
  47.         glVertex2i(sprite_x_position, 200);
  48.         glTexCoord2f((frame) / 10.0, 0);
  49.         glVertex2i(sprite_x_position + 150, 200);
  50.         glTexCoord2f((frame) / 10.0, 1);
  51.         glVertex2i(sprite_x_position + 150, 350);
  52.         glTexCoord2f((frame + 1) / 10.0, 1);
  53.         glVertex2i(sprite_x_position, 350);
  54.     }
  55.  
  56.     glEnd();
  57.     glutSwapBuffers();
  58. }
  59.  
  60. GLuint glmLoadTextureBMP(char * fname)
  61. {
  62.     wchar_t* wString = new wchar_t[256]; // Convert char[] string to LPSTR/wchar
  63.     MultiByteToWideChar(CP_ACP, 0, fname, -1, wString, 256);
  64.     HANDLE hBitMap = LoadImage(0, wString, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  65.  
  66.     BITMAP bitmap;
  67.     GetObject(hBitMap, sizeof(BITMAP), &bitmap);
  68.     int size = bitmap.bmHeight*bitmap.bmWidth*((bitmap.bmBitsPixel / 8) + 1);
  69.     BYTE *lpBits = new BYTE[size];
  70.     int PaddedWidth = (bitmap.bmWidth + 3) & (~3); // Round width up to next multiple of 4
  71.     GetBitmapBits((HBITMAP)hBitMap, size, lpBits);
  72.     BYTE r, g, b, a;
  73.     int templ, tempr;
  74.  
  75.     for (int j = 0; j<size; j += 4) // Magenta RGBA=(255,0,255) tunrs transparent
  76.     {
  77.         if ((lpBits[j + 2] == 255) && (lpBits[j + 1] == 0) && (lpBits[j] == 255))
  78.         {
  79.             /*Red*/lpBits[j + 0] = 0; /*Green*/lpBits[j + 1] = 0; /*Blue*/lpBits[j + 2] = 0; /*Alpha*/lpBits[j + 3] = 0;
  80.         }
  81.         else
  82.         {
  83.             // Reverse BGRA to RGBA and force A=255 (Alpha 100%)
  84.             /*Red*/r = lpBits[j + 0]; /*Green*/g = lpBits[j + 1]; /*Blue*/b = lpBits[j + 2]; /*Alpha*/a = lpBits[j + 3];
  85.             a = 255; // 100% of the sprite (0% of the background)
  86.             /*Red*/lpBits[j + 0] = b; /*Green*/lpBits[j + 1] = g; /*Blue*/lpBits[j + 2] = r; /*Alpha*/lpBits[j + 3] = a;
  87.         }
  88.     }
  89.  
  90.     BYTE rgb;  // Flip texture vertical (inefficient but only done on initalisation)
  91.     for (int j = 0; j<bitmap.bmHeight / 2; j++)
  92.     {
  93.         for (int i = 0; i<PaddedWidth; i++)
  94.         {
  95.             for (int k = 0; k<4; k++)
  96.             {
  97.                 templ = 4 * (i + (j*PaddedWidth));    // Address of pixel at top
  98.                 tempr = 4 * (i + ((bitmap.bmHeight - j - 1)*PaddedWidth));  // Address of pixel at bottom
  99.                 rgb = lpBits[tempr + k];
  100.                 lpBits[tempr + k] = lpBits[templ + k];
  101.                 lpBits[templ + k] = rgb;
  102.             }
  103.         }
  104.     }
  105.     GLuint textureID;
  106.     glGenTextures(1, &textureID);  // Create 1 texture
  107.                                    // "Bind" the newly created texture address with an ID
  108.     glBindTexture(GL_TEXTURE_2D, textureID);
  109.     // Turn on and configure texture mapping, texture copied into OpenGL/Graphics card
  110.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap.bmWidth, bitmap.bmHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)lpBits);
  111.     // Magnification filter (texel larger than the pixel)
  112.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  113.     // Minification filter (texel smaller than the pixel) _
  114.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  115.     // Free system memory used to store texture (it is now been transfered to the graphics card)
  116.     delete[]lpBits;
  117.     return textureID;
  118. }
  119.  
  120. static void idle()
  121. {
  122.     //sprite_x_position += 2;
  123.     //if (sprite_x_position>600) sprite_x_position = 0;
  124.     glutPostRedisplay(); // Request redraw...    
  125. }
  126.  
  127. static void qwerty_keys(unsigned char key, int x, int y)
  128. {
  129.     switch (key)
  130.     {
  131.     case 'a':  sprite_x_position -= 5;
  132.         frame++; if (frame>4) frame = 1;
  133.         direction = true;
  134.         break;
  135.  
  136.     case 'd':  sprite_x_position += 5;
  137.         frame++; if (frame>4) frame = 1;
  138.         direction = false;
  139.         break;
  140.  
  141.     case 'q': glutLeaveMainLoop(); break; // (Stop!)
  142.     default: break;
  143.     }
  144.     glutPostRedisplay(); // Send message to redraw screen
  145. }
  146.  
  147.  
  148. int _tmain(int argc, char** argv) // Entry point of program
  149. {      
  150.     glutInit(&argc, argv);         // Start glut
  151.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  152.     glutInitWindowSize(640, 480);   // Open a window at (10,10) with size (640x480) called
  153.     glutInitWindowPosition(10, 10);
  154.     glutCreateWindow("Sprite based game");
  155.     glLoadIdentity();
  156.     glMatrixMode(GL_PROJECTION);
  157.     gluOrtho2D(0.0, 640.0, 0.0, 480.0);  // Map OpenGL to Screen crds 1:1, ignore Z, 2D (X,Y)
  158.     glutDisplayFunc(display);
  159.  
  160.     glmLoadTextureBMP("Spritesheet.bmp");
  161.     glutKeyboardFunc(qwerty_keys);
  162.     glutIdleFunc(idle);
  163.     glutMainLoop();             // Start Glut main loop, exit via break
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement