Guest User

SDL2RenderLibrary.h and .cpp

a guest
Dec 25th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.42 KB | None | 0 0
  1. /********************************THIS IS SDL2RenderLibrary.cpp********************************/
  2. #include "SDL2RenderLibrary.h"
  3.  
  4. using namespace std;
  5.  
  6. void SDL2RenderLibrary::CEF_LibraryInit(int windowWidth, int windowHeight, char* windowTitle, char* fontFileName) {
  7.  
  8.     // Just launch the default sdl things
  9.    
  10.     if (SDL_Init(SDL_INIT_EVERYTHING) != 0 || TTF_Init() == -1)
  11.     {
  12.         throw "SDL failed to initialize.";
  13.     }
  14.    
  15.     CEV_GameWindow    = SDL_CreateWindow(windowTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth, windowHeight, SDL_WINDOW_SHOWN);
  16.     CEV_GameSurface   = SDL_GetWindowSurface(CEV_GameWindow);
  17.     CEV_KeyboardState = SDL_GetKeyboardState(NULL);
  18.     SDL_UpdateWindowSurface(CEV_GameWindow);
  19.     CEV_LastTime = 0;
  20.     CEV_CurrentFPS = 0;
  21.     CEV_DeltaMultiplier = 1;
  22.  
  23.     // Try to load a font
  24.     CEV_Font = TTF_OpenFont(fontFileName, 12);
  25.    
  26.     if (CEV_Font == NULL)
  27.     {
  28.         printf("Error: %s\n", TTF_GetError());
  29.         throw "Font failed to load";
  30.     }
  31.  
  32.     // Set up the keycodes
  33.     CEV_KeyCodes["A"] = SDL_SCANCODE_A;
  34.     CEV_KeyCodes["B"] = SDL_SCANCODE_B;
  35.     CEV_KeyCodes["C"] = SDL_SCANCODE_C;
  36.     CEV_KeyCodes["D"] = SDL_SCANCODE_D;
  37.     CEV_KeyCodes["E"] = SDL_SCANCODE_E;
  38.     CEV_KeyCodes["F"] = SDL_SCANCODE_F;
  39.     CEV_KeyCodes["G"] = SDL_SCANCODE_G;
  40.     CEV_KeyCodes["H"] = SDL_SCANCODE_H;
  41.     CEV_KeyCodes["I"] = SDL_SCANCODE_I;
  42.     CEV_KeyCodes["J"] = SDL_SCANCODE_J;
  43.     CEV_KeyCodes["K"] = SDL_SCANCODE_K;
  44.     CEV_KeyCodes["L"] = SDL_SCANCODE_L;
  45.     CEV_KeyCodes["M"] = SDL_SCANCODE_M;
  46.     CEV_KeyCodes["N"] = SDL_SCANCODE_N;
  47.     CEV_KeyCodes["O"] = SDL_SCANCODE_O;
  48.     CEV_KeyCodes["P"] = SDL_SCANCODE_P;
  49.     CEV_KeyCodes["Q"] = SDL_SCANCODE_Q;
  50.     CEV_KeyCodes["R"] = SDL_SCANCODE_R;
  51.     CEV_KeyCodes["S"] = SDL_SCANCODE_S;
  52.     CEV_KeyCodes["T"] = SDL_SCANCODE_T;
  53.     CEV_KeyCodes["U"] = SDL_SCANCODE_U;
  54.     CEV_KeyCodes["V"] = SDL_SCANCODE_V;
  55.     CEV_KeyCodes["W"] = SDL_SCANCODE_W;
  56.     CEV_KeyCodes["X"] = SDL_SCANCODE_X;
  57.     CEV_KeyCodes["Y"] = SDL_SCANCODE_Y;
  58.     CEV_KeyCodes["Z"] = SDL_SCANCODE_Z;
  59.     CEV_KeyCodes["1"] = SDL_SCANCODE_1;
  60.     CEV_KeyCodes["2"] = SDL_SCANCODE_2;
  61.     CEV_KeyCodes["3"] = SDL_SCANCODE_3;
  62.     CEV_KeyCodes["4"] = SDL_SCANCODE_4;
  63.     CEV_KeyCodes["5"] = SDL_SCANCODE_5;
  64.     CEV_KeyCodes["6"] = SDL_SCANCODE_6;
  65.     CEV_KeyCodes["7"] = SDL_SCANCODE_7;
  66.     CEV_KeyCodes["8"] = SDL_SCANCODE_8;
  67.     CEV_KeyCodes["9"] = SDL_SCANCODE_9;
  68.     CEV_KeyCodes["0"] = SDL_SCANCODE_0;
  69.     CEV_KeyCodes["-"] = SDL_SCANCODE_MINUS;
  70.     CEV_KeyCodes["="] = SDL_SCANCODE_EQUALS;
  71.     CEV_KeyCodes["`"] = SDL_SCANCODE_GRAVE;
  72.     CEV_KeyCodes["TAB"] = SDL_SCANCODE_TAB;
  73.     CEV_KeyCodes["CAPS"] = SDL_SCANCODE_CAPSLOCK;
  74.     CEV_KeyCodes["/"] = SDL_SCANCODE_SLASH;
  75.     CEV_KeyCodes["*"] = SDL_SCANCODE_KP_MULTIPLY;
  76.     CEV_KeyCodes["+"] = SDL_SCANCODE_KP_PLUS;
  77.     CEV_KeyCodes["RET"] = SDL_SCANCODE_RETURN;
  78.     CEV_KeyCodes["BS"] = SDL_SCANCODE_BACKSPACE;
  79.     CEV_KeyCodes["DEL"] = SDL_SCANCODE_DELETE;
  80.     CEV_KeyCodes["F1"] = SDL_SCANCODE_F1;
  81.     CEV_KeyCodes["F2"] = SDL_SCANCODE_F2;
  82.     CEV_KeyCodes["F3"] = SDL_SCANCODE_F3;
  83.     CEV_KeyCodes["F4"] = SDL_SCANCODE_F4;
  84.     CEV_KeyCodes["F5"] = SDL_SCANCODE_F5;
  85.     CEV_KeyCodes["F6"] = SDL_SCANCODE_F6;
  86.     CEV_KeyCodes["F7"] = SDL_SCANCODE_F7;
  87.     CEV_KeyCodes["F8"] = SDL_SCANCODE_F8;
  88.     CEV_KeyCodes["F9"] = SDL_SCANCODE_F9;
  89.     CEV_KeyCodes["F10"] = SDL_SCANCODE_F10;
  90.     CEV_KeyCodes["F11"] = SDL_SCANCODE_F11;
  91.     CEV_KeyCodes["F12"] = SDL_SCANCODE_F12;
  92.     CEV_KeyCodes[","] = SDL_SCANCODE_COMMA;
  93.     CEV_KeyCodes["."] = SDL_SCANCODE_PERIOD;
  94.     CEV_KeyCodes[";"] = SDL_SCANCODE_SEMICOLON;
  95.     CEV_KeyCodes["'"] = SDL_SCANCODE_APOSTROPHE;
  96.     CEV_KeyCodes["\\"] = SDL_SCANCODE_BACKSLASH;
  97.     CEV_KeyCodes["["] = SDL_SCANCODE_LEFTBRACKET;
  98.     CEV_KeyCodes["]"] = SDL_SCANCODE_RIGHTBRACKET;
  99.     CEV_KeyCodes["UP"] = SDL_SCANCODE_UP;
  100.     CEV_KeyCodes["DOWN"] = SDL_SCANCODE_DOWN;
  101.     CEV_KeyCodes["LEFT"] = SDL_SCANCODE_LEFT;
  102.     CEV_KeyCodes["RIGHT"] = SDL_SCANCODE_RIGHT;
  103. }
  104.  
  105. int SDL2RenderLibrary::CEF_LoadImage(char* fileName) {
  106.  
  107.     // Grab the index before inserting
  108.     int index;
  109.     index = distance(CEV_Images.begin(), CEV_Images.end());
  110.  
  111.     // Load the surface and error check it
  112.     SDL_Surface* tempSurface = SDL_LoadBMP(fileName);
  113.     if (tempSurface == NULL)
  114.     {
  115.         throw "Image failed to load.";
  116.     }
  117.  
  118.     // Load the image then return it
  119.     CEV_Images.insert(CEV_Images.end(), tempSurface);
  120.  
  121.     return index;
  122. }
  123.  
  124. void SDL2RenderLibrary::CEF_RenderImage(Real x, Real y, int imageIndex)
  125. {
  126.     // First we must create a rectangle to tell the screen where to draw it
  127.     SDL_Rect placement;
  128.  
  129.     // Fill it with the proper positions
  130.     placement.x = x;
  131.     placement.y = y;
  132.     placement.w = CEV_Images[imageIndex]->w;
  133.     placement.h = CEV_Images[imageIndex]->h;
  134.  
  135.     // Draw the actual surface
  136.     if (SDL_BlitSurface(CEV_Images[imageIndex], NULL, CEV_GameSurface, &placement) != 0)
  137.     {
  138.         throw "Failed to display image";
  139.     }
  140. }
  141.  
  142. int SDL2RenderLibrary::CEF_MakeText(string textString, Uint8 r, Uint8 b, Uint8 g)
  143. {
  144.     // Grab the index before inserting
  145.     int index;
  146.     index = distance(CEV_Images.begin(), CEV_Images.end());
  147.  
  148.     // Make a surface and render some text on it
  149.     SDL_Surface* tempSurface = TTF_RenderText_Solid(CEV_Font, textString.c_str(), {r, g, b});
  150.     if (tempSurface == NULL)
  151.     {
  152.         throw "Failed to make text";
  153.     }
  154.  
  155.     // Load the image then return it
  156.     CEV_Images.insert(CEV_Images.end(), tempSurface);
  157.  
  158.     return index;
  159. }
  160.  
  161. void SDL2RenderLibrary::CEF_RenderText(Real x, Real y, string textString, Uint8 r, Uint8 b, Uint8 g)
  162. {
  163.     // Draw the actual text
  164.     SDL_Surface* tempText = TTF_RenderText_Solid(CEV_Font, textString.c_str(), { r, g, b });
  165.    
  166.     // Where to place the text
  167.     SDL_Rect placement;
  168.  
  169.     // Fill it with the proper positions
  170.     placement.x = x;
  171.     placement.y = y;
  172.     placement.w = tempText->w;
  173.     placement.h = tempText->h;
  174.  
  175.     // Draw the actual surface
  176.     if (SDL_BlitSurface(tempText, NULL, CEV_GameSurface, &placement) != 0)
  177.     {
  178.         throw "Failed to display image";
  179.     }
  180.  
  181.     SDL_FreeSurface(tempText);
  182. }
  183.  
  184. bool SDL2RenderLibrary::CEF_UpdateGameWindow()
  185. {
  186.     bool returnVal = true;
  187.  
  188.     // This is the only display thing that must be done by the engine itself
  189.     while (SDL_PollEvent(&CEV_EventHandler))
  190.     {
  191.         if (CEV_EventHandler.type == SDL_QUIT)
  192.         {
  193.             returnVal = false;
  194.         }
  195.     }
  196.  
  197.     SDL_UpdateWindowSurface(CEV_GameWindow);
  198.     SDL_FillRect(CEV_GameSurface, NULL, 0x000000);
  199.  
  200.     // Calculate delta time
  201.     Uint32 newTime = SDL_GetTicks();
  202.     int between = newTime - CEV_LastTime;
  203.     CEV_DeltaMultiplier = 0.06 * between;
  204.  
  205.     // Calculate fps
  206.     CEV_CurrentFPS = 1000 / between;
  207.  
  208.     // Update the previous time
  209.     CEV_LastTime = newTime;
  210.  
  211.     return returnVal;
  212. }
  213.  
  214. void SDL2RenderLibrary::CEF_CleanUp()
  215. {
  216.     // Basically just loop through all images and erase them
  217.     for (int i = 0; i < CEV_Images.size(); i++)
  218.     {
  219.         SDL_FreeSurface(CEV_Images[i]);
  220.     }
  221.  
  222.     // Destroy the window itself
  223.     SDL_DestroyWindow(CEV_GameWindow);
  224.  
  225.     // Quit all sdl ttf related things
  226.     TTF_Quit();
  227.  
  228.     // Now end SDL all together
  229.     SDL_Quit();
  230. }
  231.  
  232. /********************************THIS IS SDL2RenderLibrary.h********************************/
  233. #pragma once
  234.  
  235. #include "Constants.h"
  236. #include <vector>
  237. #include <string>
  238. #include <map>
  239. extern "C"
  240. {
  241.     #include <SDL.h>
  242.     #include <SDL_ttf.h>
  243. }
  244.  
  245. class SDL2RenderLibrary {
  246.  
  247.     /***********************Variables***********************/
  248.  
  249.     // Each image is in a vector
  250.     std::vector<SDL_Surface*> CEV_Images;
  251.  
  252.     // The game window, so like everything
  253.     SDL_Window* CEV_GameWindow;
  254.  
  255.     // The window's surface to draw on
  256.     SDL_Surface* CEV_GameSurface;
  257.  
  258.     // The event handler for the game
  259.     SDL_Event CEV_EventHandler;
  260.  
  261.     // The last game time in milliseconds
  262.     unsigned int CEV_LastTime;
  263.  
  264.     // Holds the current vector font
  265.     TTF_Font* CEV_Font;
  266.  
  267. public:
  268.  
  269.     // Convert an SDL scancode to ascii
  270.     std::map<std::string, short> CEV_KeyCodes;
  271.  
  272.     const Uint8* CEV_KeyboardState;
  273.  
  274.     float CEV_CurrentFPS;
  275.  
  276.     double CEV_DeltaMultiplier;
  277.  
  278.     /***********************Functions***********************/
  279.  
  280.     // Initialise everything for the engine
  281.     void CEF_LibraryInit(int windowWidth, int windowHeight, char* windowTitle, char* fontFileName);
  282.  
  283.     // Load an image and return it's vector position
  284.     int CEF_LoadImage(char* fileName);
  285.  
  286.     // Render the image to the game surface
  287.     void CEF_RenderImage(Real x, Real y, int imageIndex);
  288.  
  289.     // Draw text on the fly
  290.     void CEF_RenderText(Real x, Real y, std::string textString, Uint8 r, Uint8 b, Uint8 g);
  291.  
  292.     // Render some text and return it's index
  293.     int CEF_MakeText(std::string textString, Uint8 r, Uint8 b, Uint8 g);
  294.  
  295.     // Update the screen
  296.     bool CEF_UpdateGameWindow();
  297.  
  298.     // Cleanup everything this class has made
  299.     void CEF_CleanUp();
  300. };
Advertisement
Add Comment
Please, Sign In to add comment