Advertisement
Guest User

SDL repro

a guest
Aug 6th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include "SDL.h"
  2. #undef main
  3. #include "SDL_ttf.h"
  4.  
  5. #include <string>
  6.  
  7. class Texture
  8. {
  9. public:
  10.     Texture(SDL_Renderer* renderer) : Renderer(renderer) { }
  11.  
  12.     void loadFromText(std::string text, SDL_Color color, TTF_Font* font)
  13.     {
  14.         SDL_Texture* newTexture = nullptr;
  15.  
  16.         SDL_Surface* textSurface = TTF_RenderText_Solid(font, text.c_str(), color);
  17.  
  18.         if (textSurface != nullptr)
  19.         {
  20.             newTexture = SDL_CreateTextureFromSurface(Renderer, textSurface);
  21.  
  22.             if (newTexture != nullptr)
  23.             {
  24.                 Width = (float)textSurface->w;
  25.                 Height = (float)textSurface->h;
  26.             }
  27.         }
  28.  
  29.         texture = newTexture;
  30.         SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
  31.     }
  32.  
  33.     void Render(float x, float y)
  34.     {
  35.         SDL_Rect rect{ (int)x, (int)y, (int)Width, (int)Height };
  36.         SDL_RenderCopy(Renderer, texture, nullptr, &rect);
  37.     }
  38. private:
  39.     SDL_Renderer* Renderer;
  40.     SDL_Texture* texture;
  41.     float Width, Height;
  42. };
  43.  
  44. int main()
  45. {
  46.     SDL_Init(SDL_INIT_EVERYTHING);
  47.     TTF_Init();
  48.  
  49.     bool quit = false;
  50.     SDL_Event e;
  51.  
  52.     SDL_Window* window = SDL_CreateWindow("TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
  53.     SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  54.  
  55.     TTF_Font* font = TTF_OpenFont("Resource/Gui/Font/RussoOneRegular.ttf", 16);
  56.  
  57.     Texture t(renderer);
  58.     t.loadFromText("TEST TEXT", SDL_Color{ 255, 0, 0, 255 }, font);
  59.  
  60.     while (!quit)
  61.     {
  62.         while (SDL_PollEvent(&e) != 0)
  63.         {
  64.             switch (e.type)
  65.             {
  66.                 case SDL_QUIT:
  67.                     quit = true;
  68.                     break;
  69.             }
  70.         }
  71.  
  72.         SDL_RenderClear(renderer);
  73.  
  74.         t.Render(0.0f, 0.0f);
  75.  
  76.         SDL_RenderPresent(renderer);
  77.     }
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement