12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- /*
- * 2D Game Engine
- * RenderText.h:
- * Based on pikuma.com 2D game engine in C++ and Lua course
- * Copyright (c) 2021 986-Studio. All rights reserved.
- *
- * Created by Manoël Trapier on 27/02/2021.
- */
- #ifndef GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERTEXT_H
- #define GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERTEXT_H
- #include <SDL.h>
- #include <SDL_ttf.h>
- #include <AssetStore.h>
- #include <ECS.h>
- #include <Components/TextLabel.h>
- class RenderTextSystem: public System
- {
- public:
- RenderTextSystem()
- {
- this->requireComponent<TextLabelComponent>();
- }
- void update(SDL_Renderer *renderer, std::unique_ptr<AssetStore> &assetStore, SDL_Rect &camera)
- {
- for(auto entity: this->getSystemEntities())
- {
- const auto textLabel = entity.getComponent<TextLabelComponent>();
- TTF_Font *font = assetStore->getFont(textLabel.fontId);
- SDL_Surface *surface = TTF_RenderText_Blended(font, textLabel.text.c_str(), textLabel.fgColor);
- SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
- int labelWidth, labelHeight;
- SDL_QueryTexture(texture, NULL, NULL, &labelWidth, &labelHeight);
- SDL_Rect destRect =
- {
- static_cast<int>(textLabel.position.x - (textLabel.isFixed?0.:camera.x)),
- static_cast<int>(textLabel.position.y - (textLabel.isFixed?0.:camera.y)),
- labelWidth, labelHeight
- };
- SDL_RenderCopyEx(renderer, texture, NULL, &destRect,0, NULL, SDL_FLIP_NONE);
- SDL_FreeSurface(surface);
- SDL_DestroyTexture(texture);
- }
- }
- };
- #endif /* GAMEENGINE_SOURCE_INCLUDE_SYSTEMS_RENDERTEXT_H */
|