Add proper text rendering
This commit is contained in:
parent
cf8ec294d1
commit
05ac3cb90a
77
sdlpp.hpp
77
sdlpp.hpp
@ -13,6 +13,12 @@
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
#define SDLPP_TEXT_LEFT 0x0001
|
||||
#define SDLPP_TEXT_RIGHT 0x0002
|
||||
#define SDLPP_TEXT_CENTER 0x0004
|
||||
#define SDLPP_TEXT_TOP 0x0008
|
||||
#define SDLPP_TEXT_BOTTOM 0x0010
|
||||
|
||||
namespace SDLPP {
|
||||
|
||||
int hex2num(char c);
|
||||
@ -813,6 +819,77 @@ protected:
|
||||
SDL_Rect rect;
|
||||
};
|
||||
|
||||
class TextRenderer : public RectangleRender {
|
||||
public:
|
||||
TextRenderer() = delete;
|
||||
TextRenderer( double x, double y, double w, double h, std::shared_ptr<Renderer> &r ) : RectangleRender(x, y, w, h, r) {}
|
||||
TextRenderer( double x, double y, double w, double h, std::shared_ptr<Renderer> &r, Font &font, const std::string &text, const std::string &color = "FFFFFF", const std::string &outline_color = "000000", int outline_size = -1, int flags = SDLPP_TEXT_CENTER ) : RectangleRender(x, y, w, h, r) {
|
||||
position_flags = flags;
|
||||
setText(font, text, color, outline_color, outline_size);
|
||||
}
|
||||
void setText(Font &font, const std::string &text, const std::string &color = "FFFFFF", const std::string &outline_color = "000000", int outline_size = -1) {
|
||||
setTexture(font, text, color, outline_color, outline_size);
|
||||
updateDstRect();
|
||||
}
|
||||
void setFlags(int flags) {
|
||||
position_flags = flags;
|
||||
updateDstRect();
|
||||
}
|
||||
virtual void render() override {
|
||||
if(!getHidden()) {
|
||||
if(polygon)
|
||||
polygon->render(*renderer);
|
||||
if(texture != NULL)
|
||||
SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &dst_rect);
|
||||
}
|
||||
if(hasCollisions() && renderer->getRenderColiders() && !getHidden()) {
|
||||
for(const auto &col : getCollisions())
|
||||
col->render(*renderer, colider_color);
|
||||
}
|
||||
}
|
||||
virtual void updateSizeAndPosition() override {
|
||||
RectangleRender::updateSizeAndPosition();
|
||||
updateDstRect();
|
||||
}
|
||||
private:
|
||||
void updateDstRect() {
|
||||
if(!texture)
|
||||
return;
|
||||
int text_width{}, text_height{};
|
||||
SDL_QueryTexture(texture->getTexturePtr(), NULL, NULL, &text_width, &text_height);
|
||||
if(text_width < rect.w && text_height < rect.h) {
|
||||
dst_rect.w = text_width;
|
||||
dst_rect.h = text_height;
|
||||
} else {
|
||||
double x_div = static_cast<double>(text_width)/static_cast<double>(rect.w);
|
||||
double y_div = static_cast<double>(text_height)/static_cast<double>(rect.h);
|
||||
if(x_div > y_div) {
|
||||
dst_rect.w = text_width / x_div;
|
||||
dst_rect.h = text_height / x_div;
|
||||
} else {
|
||||
dst_rect.w = text_width / y_div;
|
||||
dst_rect.h = text_height / y_div;
|
||||
}
|
||||
}
|
||||
if(!(position_flags & SDLPP_TEXT_LEFT || position_flags & SDLPP_TEXT_RIGHT)) {
|
||||
dst_rect.x = rect.x + (rect.w - dst_rect.w)/2;
|
||||
} else if(position_flags & SDLPP_TEXT_LEFT) {
|
||||
dst_rect.x = rect.x;
|
||||
} else if(position_flags & SDLPP_TEXT_RIGHT) {
|
||||
dst_rect.x = rect.x + rect.w - dst_rect.w;
|
||||
}
|
||||
if(!(position_flags & SDLPP_TEXT_TOP || position_flags & SDLPP_TEXT_BOTTOM)) {
|
||||
dst_rect.y = rect.y + (rect.h - dst_rect.h)/2;
|
||||
} else if(position_flags & SDLPP_TEXT_TOP) {
|
||||
dst_rect.y = rect.y;
|
||||
} else if(position_flags & SDLPP_TEXT_BOTTOM) {
|
||||
dst_rect.y = rect.y + rect.h - dst_rect.h;
|
||||
}
|
||||
}
|
||||
int position_flags = 0;
|
||||
SDL_Rect dst_rect{};
|
||||
};
|
||||
|
||||
class CircleRender : public RenderObject {
|
||||
public:
|
||||
CircleRender() = delete;
|
||||
|
Loading…
Reference in New Issue
Block a user