-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixelore_draw.c
More file actions
147 lines (119 loc) · 4.19 KB
/
pixelore_draw.c
File metadata and controls
147 lines (119 loc) · 4.19 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Pixelore Easy and light pixel art editor
Copyright (C) 2022-2023 SolindekDev
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <pixelore_draw.h>
#include <pixelore_window.h>
#include <pixelore_font_manager.h>
#include <pixelore_types.h>
#include <pixelore_config.h>
#include <pixelore_app.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
__GLOBAL__ i32 font_size = 16;
void set_font_size(i32 size)
{
font_size = size;
}
vec2_t draw_text(window_t* win, i32 x, i32 y, str format, ...)
{
va_list arg;
va_start(arg, format);
vec2_t vec2 = draw_text_va(win, x, y, format, arg);
va_end(arg);
return vec2;
}
vec2_t draw_text_va(window_t* win, i32 x, i32 y, str format, va_list va)
{
i8 buffer[2048];
vsprintf(buffer, format, va);
SDL_Color colorfg = { APP_FONT_COLOR() };
SDL_Surface* surface = TTF_RenderText_Blended_Wrapped(get_default_font(font_size), buffer, colorfg, window_get_width(win) - x);
SDL_Texture* texture = SDL_CreateTextureFromSurface(win->sdl_renderer, surface);
i32 font_w = 0;
i32 font_h = 0;
SDL_QueryTexture(texture, NULL, NULL, &font_w, &font_h);
SDL_Rect rect = { x, y, font_w, font_h };
SDL_RenderCopy(win->sdl_renderer, texture, NULL, &rect);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
vec2_t vec2 = { font_w, font_h };
return vec2;
}
vec2_t get_size_text(window_t* win, i32 x, i32 y, str format, ...)
{
va_list arg;
va_start(arg, format);
vec2_t vec2 = get_size_text_va(win, x, y, format, arg);
va_end(arg);
return vec2;
}
vec2_t get_size_text_va(window_t* win, i32 x, i32 y, str format, va_list va)
{
i8 buffer[2048];
vsprintf(buffer, format, va);
SDL_Color colorfg = { APP_FONT_COLOR() };
SDL_Surface* surface = TTF_RenderText_Blended_Wrapped(get_default_font(font_size), buffer, colorfg, window_get_width(win) - x);
SDL_Texture* texture = SDL_CreateTextureFromSurface(win->sdl_renderer, surface);
i32 font_w = 0;
i32 font_h = 0;
SDL_QueryTexture(texture, NULL, NULL, &font_w, &font_h);
SDL_Rect rect = { x, y, font_w, font_h };
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
vec2_t vec2 = { font_w, font_h };
return vec2;
}
SDL_Texture* load_image(window_t* win, str path)
{
SDL_Surface* surface = IMG_Load(path);
if (surface == NULL)
{
printf("Unable to open %s", path);
return NULL;
}
SDL_Texture* texture = SDL_CreateTextureFromSurface(win->sdl_renderer, surface);
SDL_FreeSurface(surface);
return texture;
}
void draw_image(window_t* win, i32 x, i32 y, SDL_Texture* image)
{
i32 image_w = 0;
i32 image_h = 0;
SDL_QueryTexture(image, NULL, NULL, &image_w, &image_h);
SDL_Rect rect = { x, y, image_w, image_h };
SDL_RenderCopy(win->sdl_renderer, image, NULL, &rect);
}
void draw_image_resize(window_t* win, i32 x, i32 y, i32 image_w, i32 image_h, SDL_Texture* image)
{
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = image_w;
rect.h = image_h;
SDL_RenderCopy(win->sdl_renderer, image, NULL, &rect);
}
void draw_rect(window_t* win, i32 x, i32 y, i32 w, i32 h, color_t color)
{
SDL_SetRenderDrawColor(win->sdl_renderer, color.r, color.g, color.b, color.a);
SDL_Rect rect = { x, y, w, h };
SDL_RenderFillRect(win->sdl_renderer, &rect);
}
void draw_rect_outline(window_t* win, i32 x, i32 y, i32 w, i32 h, color_t color)
{
SDL_SetRenderDrawColor(win->sdl_renderer, color.r, color.g, color.b, color.a);
SDL_Rect rect = { x, y, w, h };
SDL_RenderDrawRect(win->sdl_renderer, &rect);
}