Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. Renderer.cpp
  2.  
  3. #include "Renderer.h"
  4. #include <iostream>
  5.  
  6. Renderer::Renderer()
  7. {
  8. // --- INIT ---
  9. if (SDL_Init(SDL_INIT_EVERYTHING) != 0) throw "No es pot inicialitzar SDL subsystems";
  10.  
  11. // --- WINDOW ---
  12. m_window = SDL_CreateWindow("SDL...", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  13. if (m_window == nullptr) throw "No es pot inicialitzar SDL_Window";
  14.  
  15. // --- RENDERER ---
  16. m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  17. if (m_renderer == nullptr) throw "No es pot inicialitzar SDL_Renderer";
  18.  
  19. //Initialize renderer color
  20. SDL_SetRenderDrawColor(m_renderer, 255, 255, 255, 255);
  21.  
  22. //Initialize PNG loading
  23. const Uint8 imgFlags{ IMG_INIT_PNG | IMG_INIT_JPG };
  24. if (!(IMG_Init(imgFlags) & imgFlags)) throw "Error: SDL_imageinit";
  25.  
  26. // ---- TTF ----
  27. if (TTF_Init() != 0) throw"No es pot inicialitzar SDL_ttf";
  28.  
  29.  
  30.  
  31. };
  32.  
  33.  
  34. Renderer::~Renderer()
  35. {
  36. for (auto &t : m_textureData) SDL_DestroyTexture(t.second), t.second = nullptr;
  37. for (auto &f : m_fontData) TTF_CloseFont(f.second), f.second = nullptr;
  38. SDL_DestroyRenderer(m_renderer);
  39. m_renderer = nullptr;
  40. SDL_DestroyWindow(m_window);
  41. m_window = nullptr;
  42.  
  43. IMG_Quit();
  44. TTF_Quit();
  45. SDL_Quit();
  46.  
  47. };
  48.  
  49. void Renderer::Clear() { SDL_RenderClear(m_renderer); };
  50.  
  51. void Renderer::Render() { SDL_RenderPresent(m_renderer); };
  52.  
  53. void Renderer::LoadFont(Font font) {
  54. TTF_Font *ttfFont{ TTF_OpenFont(font.path.c_str(), font.size) };
  55. if (ttfFont == nullptr) throw"No espot inicialitzar TTF_Font";
  56. m_fontData[font.id] = ttfFont;
  57. };
  58.  
  59. void Renderer::LoadTexture(const std::string &id, const std::string &path) {
  60. SDL_Texture *texture{ IMG_LoadTexture(m_renderer, path.c_str()) };
  61. if (texture == nullptr) throw "No s'han pogut crear les textures"; //Falla porque texture es nullptr
  62. m_textureData[id] = texture;
  63. if (m_textureData[0] == nullptr) throw "Algo fue mal al crear la textura";
  64. };
  65.  
  66. void Renderer::LoadTextureText(const std::string &fontId, Text text) {
  67. SDL_Surface *tmpSurf = TTF_RenderText_Blended(m_fontData[fontId], text.text.c_str(), SDL_Color{ text.color.r, text.color.g, text.color.b,text.color.a });
  68. if (tmpSurf == nullptr) throw "Unable to create the SDL text surface";
  69. SDL_Texture *texture{ SDL_CreateTextureFromSurface(m_renderer, tmpSurf) };
  70. m_textureData[text.id] = texture;
  71.  
  72. };
  73.  
  74. Vector2 Renderer::GetTextureSize(const std::string &id) {
  75. int w; int h;
  76. SDL_QueryTexture(m_textureData[id], NULL, NULL, &w, &h);
  77. return { w, h };
  78. };
  79.  
  80. void Renderer::PushImage(const std::string &id, const SDL_Rect &rect) {
  81. SDL_RenderCopy(m_renderer, m_textureData[id], nullptr, &rect);
  82. };
  83.  
  84. void Renderer::PushSprite(const std::string &id, const SDL_Rect &rectSprite, const SDL_Rect &rectPos) {
  85. SDL_RenderCopy(m_renderer, m_textureData[id], &rectSprite, &rectPos);
  86. }
  87.  
  88. void Renderer::PushRotateSprite(const std::string & id, const SDL_Rect & rectSprite, const SDL_Rect & rectPos, float angle) {
  89. SDL_Point center = { rectPos.w / 2, rectPos.h / 2 };
  90. SDL_RenderCopyEx(m_renderer, m_textureData[id], &rectSprite, &rectPos, angle, &center, SDL_FLIP_NONE);
  91. }
  92.  
  93. void Renderer::SetRendererDrawColor(int r, int g, int b)
  94. {
  95. SDL_SetRenderDrawColor(m_renderer, r, g, b, 255);
  96. }
  97.  
  98.  
  99.  
  100.  
  101. Renderer* Renderer::renderer = nullptr;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement