-
Notifications
You must be signed in to change notification settings - Fork 1
/
sui_sdl_renderer.h
81 lines (68 loc) · 2.4 KB
/
sui_sdl_renderer.h
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
71
72
73
74
75
76
77
78
79
80
81
#ifndef SUI_SDL_RENDERER_H_
#define SUI_SDL_RENDERER_H_
#include "sui.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
static SDL_Renderer *sui_sdl_renderer;
static TTF_Font *sui_sdl_font;
void sui_sdl_init_ctx(sui_ctx *ctx);
void sui_sdl_start_event();
void sui_sdl_handle_event(sui_ctx *ctx, SDL_Event ev);
void sui_sdl_end_event();
void sui_sdl_init(SDL_Renderer *renderer, TTF_Font *font);
void sui_sdl_get_text_size(const char *text, int *w, int *h);
void sui_sdl_draw_rect(sui_ctx *ctx, int x, int y, int w, int h,
sui_rgba color);
void sui_sdl_draw_text(sui_ctx *ctx, const char *text, int x, int y,
sui_rgba color);
#endif /* SUI_SDL_RENDERER_H_ */
#ifdef SUI_SDL_RENDERER_IMPLEMENTATION
void sui_sdl_init_ctx(sui_ctx *ctx) {
ctx->draw_rect = sui_sdl_draw_rect;
ctx->draw_text = sui_sdl_draw_text;
ctx->get_text_size = sui_sdl_get_text_size;
}
void sui_sdl_start_event() {}
void sui_sdl_end_event() {}
void sui_sdl_init(SDL_Renderer *renderer, TTF_Font *font) {
sui_sdl_renderer = renderer;
sui_sdl_font = font;
}
void sui_sdl_draw_rect(sui_ctx *ctx, int x, int y, int w, int h,
sui_rgba color) {
(void)ctx;
SDL_SetRenderDrawColor(sui_sdl_renderer, color.r, color.g, color.b,
color.a);
SDL_Rect rect = {x, y, w, h};
SDL_RenderFillRect(sui_sdl_renderer, &rect);
}
void sui_sdl_draw_text(sui_ctx *ctx, const char *text, int x, int y,
sui_rgba color) {
(void)ctx;
SDL_Color sdlcolor = {color.r, color.g, color.b, color.a};
SDL_Surface *surface = TTF_RenderUTF8_Blended(sui_sdl_font, text, sdlcolor);
SDL_Texture *texture =
SDL_CreateTextureFromSurface(sui_sdl_renderer, surface);
SDL_Rect rect = {x, y, surface->w, surface->h};
SDL_RenderCopy(sui_sdl_renderer, texture, NULL, &rect);
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
}
void sui_sdl_handle_event(sui_ctx *ctx, SDL_Event ev) {
switch (ev.type) {
case SDL_MOUSEMOTION:
ctx->mouse_x = ev.motion.x;
ctx->mouse_y = ev.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
ctx->mouse_pressed = 1;
break;
case SDL_MOUSEBUTTONUP:
ctx->mouse_pressed = 0;
break;
}
}
void sui_sdl_get_text_size(const char *text, int *w, int *h) {
TTF_SizeText(sui_sdl_font, text, w, h);
}
#endif /* SUI_SDL_RENDERER_IMPLEMENTATION */