forked from tsweeney256/Pokedex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgui_sdlbackend.cpp
70 lines (58 loc) · 1.9 KB
/
imgui_sdlbackend.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <SDL.h>
#include "imgui_sdlbackend.hpp"
#include "text.hpp"
#include "sprite.hpp"
namespace imgui {
SDLRenderBackend::SDLRenderBackend(SDL_Renderer *renderer) :m_renderer(renderer)
{
m_font = TTF_OpenFont("assets/unifont-7.0.06.ttf", 16);
}
void SDLRenderBackend::drawRect(int x, int y, int w, int h, const SDL_Color color)
{
SDL_Rect rect = {x, y, w, h};
uint8_t r, g, b, a;
SDL_GetRenderDrawColor(m_renderer, &r, &g, &b, &a);
SDL_SetRenderDrawColor(m_renderer, color.r, color.g, color.b, color.a);
SDL_RenderFillRect(m_renderer, &rect);
SDL_SetRenderDrawColor(m_renderer, r, g, b, a);
}
void SDLRenderBackend::drawText(int x, int y, int w, SDL_Color color, const std::string &text)
{
SDL_Texture *textTexture = getTextRenderTexture(m_renderer, m_font, text, color, w);
SDL_Texture *textShadowTexture = getTextRenderTexture(m_renderer, m_font, text, {0, 0, 0, 255}, w);
SDL_SetTextureAlphaMod(textShadowTexture, 90);
// render the shadow
{
Sprite sprite(textShadowTexture);
sprite.setPosition(x+2, y+2);
SDL_Rect dest = sprite.rect();
SDL_RenderCopyEx(m_renderer, textShadowTexture, nullptr, &dest,
sprite.angle(), nullptr, SDL_FLIP_NONE);
}
// render the text
{
Sprite sprite(textTexture);
sprite.setPosition(x, y);
SDL_Rect dest = sprite.rect();
SDL_RenderCopyEx(m_renderer, textTexture, nullptr, &dest,
sprite.angle(), nullptr, SDL_FLIP_NONE);
}
// TODO: create texture cache
SDL_DestroyTexture(textTexture);
SDL_DestroyTexture(textShadowTexture);
}
void SDLRenderBackend::drawTexture(int x, int y, int w, int h, SDL_Texture *texture)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
dest.w = w;
dest.h = h;
SDL_RenderCopyEx(m_renderer, texture, nullptr, &dest,
0, nullptr, SDL_FLIP_NONE);
}
void SDLRenderBackend::setRenderTarget(SDL_Texture *texture)
{
SDL_SetRenderTarget(m_renderer, texture);
}
} // namespace imgui