Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************THIS IS SDL2RenderLibrary.cpp********************************/
- #include "SDL2RenderLibrary.h"
- using namespace std;
- void SDL2RenderLibrary::CEF_LibraryInit(int windowWidth, int windowHeight, char* windowTitle, char* fontFileName) {
- // Just launch the default sdl things
- if (SDL_Init(SDL_INIT_EVERYTHING) != 0 || TTF_Init() == -1)
- {
- throw "SDL failed to initialize.";
- }
- CEV_GameWindow = SDL_CreateWindow(windowTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth, windowHeight, SDL_WINDOW_SHOWN);
- CEV_GameSurface = SDL_GetWindowSurface(CEV_GameWindow);
- CEV_KeyboardState = SDL_GetKeyboardState(NULL);
- SDL_UpdateWindowSurface(CEV_GameWindow);
- CEV_LastTime = 0;
- CEV_CurrentFPS = 0;
- CEV_DeltaMultiplier = 1;
- // Try to load a font
- CEV_Font = TTF_OpenFont(fontFileName, 12);
- if (CEV_Font == NULL)
- {
- printf("Error: %s\n", TTF_GetError());
- throw "Font failed to load";
- }
- // Set up the keycodes
- CEV_KeyCodes["A"] = SDL_SCANCODE_A;
- CEV_KeyCodes["B"] = SDL_SCANCODE_B;
- CEV_KeyCodes["C"] = SDL_SCANCODE_C;
- CEV_KeyCodes["D"] = SDL_SCANCODE_D;
- CEV_KeyCodes["E"] = SDL_SCANCODE_E;
- CEV_KeyCodes["F"] = SDL_SCANCODE_F;
- CEV_KeyCodes["G"] = SDL_SCANCODE_G;
- CEV_KeyCodes["H"] = SDL_SCANCODE_H;
- CEV_KeyCodes["I"] = SDL_SCANCODE_I;
- CEV_KeyCodes["J"] = SDL_SCANCODE_J;
- CEV_KeyCodes["K"] = SDL_SCANCODE_K;
- CEV_KeyCodes["L"] = SDL_SCANCODE_L;
- CEV_KeyCodes["M"] = SDL_SCANCODE_M;
- CEV_KeyCodes["N"] = SDL_SCANCODE_N;
- CEV_KeyCodes["O"] = SDL_SCANCODE_O;
- CEV_KeyCodes["P"] = SDL_SCANCODE_P;
- CEV_KeyCodes["Q"] = SDL_SCANCODE_Q;
- CEV_KeyCodes["R"] = SDL_SCANCODE_R;
- CEV_KeyCodes["S"] = SDL_SCANCODE_S;
- CEV_KeyCodes["T"] = SDL_SCANCODE_T;
- CEV_KeyCodes["U"] = SDL_SCANCODE_U;
- CEV_KeyCodes["V"] = SDL_SCANCODE_V;
- CEV_KeyCodes["W"] = SDL_SCANCODE_W;
- CEV_KeyCodes["X"] = SDL_SCANCODE_X;
- CEV_KeyCodes["Y"] = SDL_SCANCODE_Y;
- CEV_KeyCodes["Z"] = SDL_SCANCODE_Z;
- CEV_KeyCodes["1"] = SDL_SCANCODE_1;
- CEV_KeyCodes["2"] = SDL_SCANCODE_2;
- CEV_KeyCodes["3"] = SDL_SCANCODE_3;
- CEV_KeyCodes["4"] = SDL_SCANCODE_4;
- CEV_KeyCodes["5"] = SDL_SCANCODE_5;
- CEV_KeyCodes["6"] = SDL_SCANCODE_6;
- CEV_KeyCodes["7"] = SDL_SCANCODE_7;
- CEV_KeyCodes["8"] = SDL_SCANCODE_8;
- CEV_KeyCodes["9"] = SDL_SCANCODE_9;
- CEV_KeyCodes["0"] = SDL_SCANCODE_0;
- CEV_KeyCodes["-"] = SDL_SCANCODE_MINUS;
- CEV_KeyCodes["="] = SDL_SCANCODE_EQUALS;
- CEV_KeyCodes["`"] = SDL_SCANCODE_GRAVE;
- CEV_KeyCodes["TAB"] = SDL_SCANCODE_TAB;
- CEV_KeyCodes["CAPS"] = SDL_SCANCODE_CAPSLOCK;
- CEV_KeyCodes["/"] = SDL_SCANCODE_SLASH;
- CEV_KeyCodes["*"] = SDL_SCANCODE_KP_MULTIPLY;
- CEV_KeyCodes["+"] = SDL_SCANCODE_KP_PLUS;
- CEV_KeyCodes["RET"] = SDL_SCANCODE_RETURN;
- CEV_KeyCodes["BS"] = SDL_SCANCODE_BACKSPACE;
- CEV_KeyCodes["DEL"] = SDL_SCANCODE_DELETE;
- CEV_KeyCodes["F1"] = SDL_SCANCODE_F1;
- CEV_KeyCodes["F2"] = SDL_SCANCODE_F2;
- CEV_KeyCodes["F3"] = SDL_SCANCODE_F3;
- CEV_KeyCodes["F4"] = SDL_SCANCODE_F4;
- CEV_KeyCodes["F5"] = SDL_SCANCODE_F5;
- CEV_KeyCodes["F6"] = SDL_SCANCODE_F6;
- CEV_KeyCodes["F7"] = SDL_SCANCODE_F7;
- CEV_KeyCodes["F8"] = SDL_SCANCODE_F8;
- CEV_KeyCodes["F9"] = SDL_SCANCODE_F9;
- CEV_KeyCodes["F10"] = SDL_SCANCODE_F10;
- CEV_KeyCodes["F11"] = SDL_SCANCODE_F11;
- CEV_KeyCodes["F12"] = SDL_SCANCODE_F12;
- CEV_KeyCodes[","] = SDL_SCANCODE_COMMA;
- CEV_KeyCodes["."] = SDL_SCANCODE_PERIOD;
- CEV_KeyCodes[";"] = SDL_SCANCODE_SEMICOLON;
- CEV_KeyCodes["'"] = SDL_SCANCODE_APOSTROPHE;
- CEV_KeyCodes["\\"] = SDL_SCANCODE_BACKSLASH;
- CEV_KeyCodes["["] = SDL_SCANCODE_LEFTBRACKET;
- CEV_KeyCodes["]"] = SDL_SCANCODE_RIGHTBRACKET;
- CEV_KeyCodes["UP"] = SDL_SCANCODE_UP;
- CEV_KeyCodes["DOWN"] = SDL_SCANCODE_DOWN;
- CEV_KeyCodes["LEFT"] = SDL_SCANCODE_LEFT;
- CEV_KeyCodes["RIGHT"] = SDL_SCANCODE_RIGHT;
- }
- int SDL2RenderLibrary::CEF_LoadImage(char* fileName) {
- // Grab the index before inserting
- int index;
- index = distance(CEV_Images.begin(), CEV_Images.end());
- // Load the surface and error check it
- SDL_Surface* tempSurface = SDL_LoadBMP(fileName);
- if (tempSurface == NULL)
- {
- throw "Image failed to load.";
- }
- // Load the image then return it
- CEV_Images.insert(CEV_Images.end(), tempSurface);
- return index;
- }
- void SDL2RenderLibrary::CEF_RenderImage(Real x, Real y, int imageIndex)
- {
- // First we must create a rectangle to tell the screen where to draw it
- SDL_Rect placement;
- // Fill it with the proper positions
- placement.x = x;
- placement.y = y;
- placement.w = CEV_Images[imageIndex]->w;
- placement.h = CEV_Images[imageIndex]->h;
- // Draw the actual surface
- if (SDL_BlitSurface(CEV_Images[imageIndex], NULL, CEV_GameSurface, &placement) != 0)
- {
- throw "Failed to display image";
- }
- }
- int SDL2RenderLibrary::CEF_MakeText(string textString, Uint8 r, Uint8 b, Uint8 g)
- {
- // Grab the index before inserting
- int index;
- index = distance(CEV_Images.begin(), CEV_Images.end());
- // Make a surface and render some text on it
- SDL_Surface* tempSurface = TTF_RenderText_Solid(CEV_Font, textString.c_str(), {r, g, b});
- if (tempSurface == NULL)
- {
- throw "Failed to make text";
- }
- // Load the image then return it
- CEV_Images.insert(CEV_Images.end(), tempSurface);
- return index;
- }
- void SDL2RenderLibrary::CEF_RenderText(Real x, Real y, string textString, Uint8 r, Uint8 b, Uint8 g)
- {
- // Draw the actual text
- SDL_Surface* tempText = TTF_RenderText_Solid(CEV_Font, textString.c_str(), { r, g, b });
- // Where to place the text
- SDL_Rect placement;
- // Fill it with the proper positions
- placement.x = x;
- placement.y = y;
- placement.w = tempText->w;
- placement.h = tempText->h;
- // Draw the actual surface
- if (SDL_BlitSurface(tempText, NULL, CEV_GameSurface, &placement) != 0)
- {
- throw "Failed to display image";
- }
- SDL_FreeSurface(tempText);
- }
- bool SDL2RenderLibrary::CEF_UpdateGameWindow()
- {
- bool returnVal = true;
- // This is the only display thing that must be done by the engine itself
- while (SDL_PollEvent(&CEV_EventHandler))
- {
- if (CEV_EventHandler.type == SDL_QUIT)
- {
- returnVal = false;
- }
- }
- SDL_UpdateWindowSurface(CEV_GameWindow);
- SDL_FillRect(CEV_GameSurface, NULL, 0x000000);
- // Calculate delta time
- Uint32 newTime = SDL_GetTicks();
- int between = newTime - CEV_LastTime;
- CEV_DeltaMultiplier = 0.06 * between;
- // Calculate fps
- CEV_CurrentFPS = 1000 / between;
- // Update the previous time
- CEV_LastTime = newTime;
- return returnVal;
- }
- void SDL2RenderLibrary::CEF_CleanUp()
- {
- // Basically just loop through all images and erase them
- for (int i = 0; i < CEV_Images.size(); i++)
- {
- SDL_FreeSurface(CEV_Images[i]);
- }
- // Destroy the window itself
- SDL_DestroyWindow(CEV_GameWindow);
- // Quit all sdl ttf related things
- TTF_Quit();
- // Now end SDL all together
- SDL_Quit();
- }
- /********************************THIS IS SDL2RenderLibrary.h********************************/
- #pragma once
- #include "Constants.h"
- #include <vector>
- #include <string>
- #include <map>
- extern "C"
- {
- #include <SDL.h>
- #include <SDL_ttf.h>
- }
- class SDL2RenderLibrary {
- /***********************Variables***********************/
- // Each image is in a vector
- std::vector<SDL_Surface*> CEV_Images;
- // The game window, so like everything
- SDL_Window* CEV_GameWindow;
- // The window's surface to draw on
- SDL_Surface* CEV_GameSurface;
- // The event handler for the game
- SDL_Event CEV_EventHandler;
- // The last game time in milliseconds
- unsigned int CEV_LastTime;
- // Holds the current vector font
- TTF_Font* CEV_Font;
- public:
- // Convert an SDL scancode to ascii
- std::map<std::string, short> CEV_KeyCodes;
- const Uint8* CEV_KeyboardState;
- float CEV_CurrentFPS;
- double CEV_DeltaMultiplier;
- /***********************Functions***********************/
- // Initialise everything for the engine
- void CEF_LibraryInit(int windowWidth, int windowHeight, char* windowTitle, char* fontFileName);
- // Load an image and return it's vector position
- int CEF_LoadImage(char* fileName);
- // Render the image to the game surface
- void CEF_RenderImage(Real x, Real y, int imageIndex);
- // Draw text on the fly
- void CEF_RenderText(Real x, Real y, std::string textString, Uint8 r, Uint8 b, Uint8 g);
- // Render some text and return it's index
- int CEF_MakeText(std::string textString, Uint8 r, Uint8 b, Uint8 g);
- // Update the screen
- bool CEF_UpdateGameWindow();
- // Cleanup everything this class has made
- void CEF_CleanUp();
- };
Advertisement
Add Comment
Please, Sign In to add comment