RenderText.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * 2D Game Engine
  3. * RenderText.h:
  4. * Based on pikuma.com 2D game engine in C++ and Lua course
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 27/02/2021.
  8. */
  9. #ifndef GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERTEXT_H
  10. #define GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERTEXT_H
  11. #include <SDL.h>
  12. #include <SDL_ttf.h>
  13. #include <AssetStore.h>
  14. #include <ECS.h>
  15. #include <Components/TextLabel.h>
  16. class RenderTextSystem: public System
  17. {
  18. public:
  19. RenderTextSystem()
  20. {
  21. this->requireComponent<TextLabelComponent>();
  22. }
  23. void update(SDL_Renderer *renderer, std::unique_ptr<AssetStore> &assetStore, SDL_Rect &camera)
  24. {
  25. for(auto entity: this->getSystemEntities())
  26. {
  27. const auto textLabel = entity.getComponent<TextLabelComponent>();
  28. TTF_Font *font = assetStore->getFont(textLabel.fontId);
  29. SDL_Surface *surface = TTF_RenderText_Blended(font, textLabel.text.c_str(), textLabel.fgColor);
  30. SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
  31. int labelWidth, labelHeight;
  32. SDL_QueryTexture(texture, NULL, NULL, &labelWidth, &labelHeight);
  33. SDL_Rect destRect =
  34. {
  35. static_cast<int>(textLabel.position.x - (textLabel.isFixed?0.:camera.x)),
  36. static_cast<int>(textLabel.position.y - (textLabel.isFixed?0.:camera.y)),
  37. labelWidth, labelHeight
  38. };
  39. SDL_RenderCopyEx(renderer, texture, NULL, &destRect,0, NULL, SDL_FLIP_NONE);
  40. SDL_FreeSurface(surface);
  41. SDL_DestroyTexture(texture);
  42. }
  43. }
  44. };
  45. #endif /* GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERTEXT_H */