-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtext-gauge.c
More file actions
302 lines (256 loc) · 8.01 KB
/
Copy pathtext-gauge.c
File metadata and controls
302 lines (256 loc) · 8.01 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* SPDX-FileCopyrightText: 2021 Samuel Cuella <samuel.cuella@gmail.com>
*
* This file is part of SoFIS - an open source EFIS
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "SDL_gpu.h"
#include "SDL_pcf.h"
#include "SDL_pixels.h"
#include "SDL_rect.h"
#include "base-gauge.h"
#include "generic-layer.h"
#include "text-gauge.h"
#include "sdl-colors.h"
#include "misc.h"
static void text_gauge_update_state(TextGauge *self, Uint32 dt);
static void text_gauge_render(TextGauge *self, Uint32 dt, RenderContext *ctx);
static void *text_gauge_dispose(TextGauge *self);
static BaseGaugeOps text_gauge_ops = {
.render = (RenderFunc)text_gauge_render,
.update_state = (StateUpdateFunc)text_gauge_update_state,
.dispose = (DisposeFunc)text_gauge_dispose
};
TextGauge *text_gauge_new(const char *value, bool outlined, int w, int h)
{
TextGauge *self;
self = calloc(1, sizeof(TextGauge));
if(self){
if(!text_gauge_init(self, value, outlined, w, h)){
return base_gauge_free(BASE_GAUGE(self));
}
}
return self;
}
TextGauge *text_gauge_init(TextGauge *self, const char *value, bool outlined, int w, int h)
{
base_gauge_init(BASE_GAUGE(self),
&text_gauge_ops,
w, h
);
if(value)
text_gauge_set_value(self, value);
self->outlined = outlined;
return self;
}
static void *text_gauge_dispose(TextGauge *self)
{
if(self->value)
free(self->value);
if(self->font.is_static)
PCF_FreeStaticFont(self->font.static_font);
else
PCF_CloseFont(self->font.font);
if(self->state.chars)
free(self->state.chars);
#if USE_SDL_GPU
if(self->buffer)
generic_layer_free(self->buffer);
#endif
return self;
}
static void text_gauge_dispose_font(TextGauge *self)
{
if(!self->font.is_static && self->font.font){
PCF_FontUnref(self->font.font);
PCF_CloseFont(self->font.font);
}else if(self->font.static_font){
/* TODO: This is confusing. There should be only one
* of unref/free
* */
PCF_StaticFontUnref(self->font.static_font);
// PCF_FreeStaticFont(self->font.static_font);
}
}
void text_gauge_set_font(TextGauge *self, PCF_Font *font)
{
text_gauge_dispose_font(self);
self->font.font = font;
PCF_FontRef(self->font.font);
self->font.is_static = false;
BASE_GAUGE(self)->dirty = true;
}
void text_gauge_set_static_font(TextGauge *self, PCF_StaticFont *font)
{
text_gauge_dispose_font(self);
PCF_StaticFontRef(font);
self->font.static_font = font;
self->font.is_static = true;
BASE_GAUGE(self)->dirty = true;
}
void text_gauge_set_color(TextGauge *self, SDL_Color color, Uint8 which)
{
if(which == TEXT_COLOR)
self->text_color = color;
else
self->bg_color = color;
BASE_GAUGE(self)->dirty = true;
}
/**
* @brief Ensure that the TextGauge can store at least
* @param size chars.
*
* TODO: Should be set_length or something that conveys the
* meaning of internal storage vs heigh/width
*
* @param self The TextGauge
* @param size the number of chars that need to be stored.
* The actual allocation will be size+1 to accomodate the
* trailing NULL byte. The internal size can be greater than
* @param size.
*
* @return True on success, false on failure.
*/
bool text_gauge_set_size(TextGauge *self, size_t size)
{
if(size+1 > self->asize){
char *tmp;
tmp = realloc(self->value, (size+1) * sizeof(char));
if(!tmp) return false;
self->asize = size+1;
self->value = tmp;
}
if(size > self->state.achars){
void *tmp;
tmp = realloc(self->state.chars, size * sizeof(PCF_StaticFontPatch));
if(!tmp) return false;
self->state.chars = tmp;
self->state.achars = size;
}
return true;
}
/*TODO: Check if the static font can display the new value ?*/
bool text_gauge_set_value(TextGauge *self, const char *value)
{
int newlen;
/*TODO: This is going to be quite mem-intesive, use a pool or something
* and resort to allocation only when needing large pools*/
newlen = strlen(value);
if(!text_gauge_set_size(self, newlen))
return false;
strcpy(self->value, value);
self->len = newlen;
self->value[self->len] = '\0';
BASE_GAUGE(self)->dirty = true;
return true;
}
/**
* @brief Sets the value of a TextGauge using a printf-like format.
*
* @param self a TextGauge
* @param size The maximum number of chars to print, excluding the NULL byte
* @param fmt A printf-like format string
* @param ... Arguments to the format
*
* @return True on success, false if all the chars couldn't be printed
*/
bool text_gauge_set_value_formatn(TextGauge *self, size_t size, const char *fmt, ...)
{
va_list ap;
int rv;
if(!text_gauge_set_size(self, size))
return false;
va_start(ap, fmt);
rv = vsnprintf(self->value, size+1, fmt, ap);
va_end(ap);
self->len = rv <= size ? rv : strlen(self->value);
BASE_GAUGE(self)->dirty = true;
return rv <= size;
}
static inline void text_gauge_static_font_update_state(TextGauge *self, Uint32 dt)
{
SDL_Rect farea;
SDL_Rect glyph, cursor;
PCF_StaticFont *sfont;
if(!self->state.chars)
return;
sfont = self->font.static_font;
farea = (SDL_Rect){
.x = 0,
.y = 0,
.w = base_gauge_w(BASE_GAUGE(self)),
.h = base_gauge_h(BASE_GAUGE(self))
};
PCF_StaticFontGetSizeRequestRect(sfont, self->value, true, &cursor);
SDLExt_RectAlign(&cursor, &farea, self->alignment);
self->state.nchars = PCF_StaticFontPreWriteString(sfont,
self->len, self->value,
true, &cursor,
self->state.achars, self->state.chars
);
}
static inline void text_gauge_regular_font_update_state(TextGauge *self, Uint32 dt)
{
if(!self->buffer){
self->buffer = generic_layer_new(base_gauge_w(BASE_GAUGE(self)), base_gauge_h(BASE_GAUGE(self)));
}
view_font_draw_text(self->buffer->canvas, NULL,
self->alignment, self->value,
self->font.font,
SDL_MapRGBA(self->buffer->canvas->format,
self->text_color.r, self->text_color.g,
self->text_color.b, self->text_color.a
),
SDL_MapRGBA(self->buffer->canvas->format,
self->bg_color.r, self->bg_color.g,
self->bg_color.b, self->bg_color.a
)
);
generic_layer_update_texture(self->buffer);
printf("redrawn backbuffer\n");
}
static void text_gauge_update_state(TextGauge *self, Uint32 dt)
{
if(!self->font.font) return; /*Wait until either font has been set*/
if(self->font.is_static){
text_gauge_static_font_update_state(self, dt);
}else{
text_gauge_regular_font_update_state(self, dt);
}
}
static inline void text_gauge_static_font_render(TextGauge *self, Uint32 dt,
RenderContext *ctx)
{
base_gauge_fill(BASE_GAUGE(self), ctx, NULL, &self->bg_color, false);
if(self->outlined)
base_gauge_draw_outline(BASE_GAUGE(self), ctx, &SDL_WHITE, NULL);
for(int i = 0; i < self->state.nchars; i++){
base_gauge_draw_static_font_patch(BASE_GAUGE(self),
ctx,
self->font.static_font,
&self->state.chars[i]
);
}
}
static inline void text_gauge_regular_font_render(TextGauge *self, Uint32 dt,
RenderContext *ctx)
{
base_gauge_fill(BASE_GAUGE(self), ctx, NULL, &self->bg_color, false);
base_gauge_blit_layer(BASE_GAUGE(self), ctx, self->buffer, NULL, NULL);
if(self->outlined)
base_gauge_draw_outline(BASE_GAUGE(self), ctx, &SDL_WHITE, NULL);
}
static void text_gauge_render(TextGauge *self, Uint32 dt, RenderContext *ctx)
{
if(!self->font.font) return; /*Wait until either font has been set*/
if(self->font.is_static){
text_gauge_static_font_render(self, dt, ctx);
}else{
text_gauge_regular_font_render(self, dt, ctx);
}
}