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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#include "microui.h"
#include "renderer.h"
#define expect(x) do { \
if (!(x)) { \
fprintf(stderr, "Fatal error: %s:%d: assertion '%s' failed\n", \
__FILE__, __LINE__, #x); \
abort(); \
} \
} while (0)
enum window {
WIDTH = 640,
HEIGHT = 480,
WINFLAGS = SDL_WINDOW_RESIZABLE,
RENDERFLAGS = SDL_RENDERER_PRESENTVSYNC,
};
enum {
ICONLIST_SIZE = 32,
CANVASLIST_SIZE = 4,
};
enum { CIRCLE_RADIUS = 16 };
enum rgb {
RGBA_DEPTH = 4*4,
RGB_RED = 0xFu << 3,
RGB_GREEN = 0xFu << 2,
RGB_BLUE = 0xFu << 1,
RGBA_BYTES_PER_PIXEL = RGBA_DEPTH / 8,
};
static const char FONT[] = "font/P052-Roman.ttf";
enum font { FONTSIZE = 14, };
static const mu_Color bg = {255, 255, 255, 255};
static const char button_map[256] = {
[SDL_BUTTON_LEFT & 0xff] = MU_MOUSE_LEFT,
[SDL_BUTTON_RIGHT & 0xff] = MU_MOUSE_RIGHT,
[SDL_BUTTON_MIDDLE & 0xff] = MU_MOUSE_MIDDLE,
};
static const char key_map[256] = {
[SDLK_LSHIFT & 0xff] = MU_KEY_SHIFT,
[SDLK_RSHIFT & 0xff] = MU_KEY_SHIFT,
[SDLK_LCTRL & 0xff] = MU_KEY_CTRL,
[SDLK_RCTRL & 0xff] = MU_KEY_CTRL,
[SDLK_LALT & 0xff] = MU_KEY_ALT,
[SDLK_RALT & 0xff] = MU_KEY_ALT,
[SDLK_RETURN & 0xff] = MU_KEY_RETURN,
[SDLK_BACKSPACE & 0xff] = MU_KEY_BACKSPACE,
};
typedef struct {
SDL_Surface *bg, *fg, *dst;
int icon_id;
} Canvas;
static void print_info(void);
static int draw_circle(void);
static int text_width(mu_Font mufont, const char *str, int len);
static int text_height(mu_Font mufont);
static void handle_event(SDL_Event e, mu_Context *ctx);
static void clear(void);
static void render_command(mu_Command *cmd);
static void clip(mu_Rect rect);
static void draw_rect(mu_Rect rect, mu_Color color);
static void draw_text(mu_Font font, mu_Vec2 pos, mu_Color color, const char *str);
static void draw_icon(int id, mu_Rect r);
static void free_canvas(Canvas c);
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
mu_stack(SDL_Texture *, ICONLIST_SIZE) icon_list;
mu_stack(Canvas, CANVASLIST_SIZE) canvas_list;
SDL_Surface *circle;
/* Initialize the window and renderer. Returns non-zero on error. */
int
r_init(mu_Context *ctx, const char *title) {
icon_list.idx = 0;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "%s\n", SDL_GetError());
return 1;
}
window = SDL_CreateWindow(title,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
WIDTH, HEIGHT,
WINFLAGS);
if (!window) {
fprintf(stderr, "%s\n", SDL_GetError());
return 1;
}
renderer = SDL_CreateRenderer(window, -1, RENDERFLAGS);
if (!renderer) {
fprintf(stderr, "%s\n", SDL_GetError());
return 1;
}
if (TTF_Init() != 0) {
fprintf(stderr, "%s\n", SDL_GetError());
return 1;
}
TTF_Font *font = TTF_OpenFont(FONT, FONTSIZE);
if (!font) {
fprintf(stderr, "Failed to open font %s\n", FONT);
return 1;
}
ctx->style->font = font;
print_info();
ctx->text_width = text_width;
ctx->text_height = text_height;
if (draw_circle() != 0) {
return 1;
}
return 0;
}
void
r_free(void) {
while (icon_list.idx > 0) {
r_remove_icon(icon_list.idx-1);
}
while (canvas_list.idx > 0) {
r_remove_canvas(canvas_list.idx-1);
}
SDL_FreeSurface(circle);
}
static void
print_info(void) {
SDL_RendererInfo info;
if (SDL_GetRendererInfo(renderer, &info) != 0) {
fprintf(stderr, "%s\n", SDL_GetError());
return;
}
printf("Using renderer %s\n", info.name);
fflush(stdout);
}
static int
draw_circle(void) {
int x, y;
uint8_t *p;
circle = SDL_CreateRGBSurface(0, 2*CIRCLE_RADIUS, 2*CIRCLE_RADIUS, RGBA_DEPTH, 0, 0, 0, 0);
if (!circle) {
fprintf(stderr, "%s\n", SDL_GetError());
return 1;
}
for (y = 0; y < circle->h; y++) {
for (x = 0; x < circle->w; x++) {
p = (uint8_t *) circle->pixels + y*circle->pitch + x*RGBA_BYTES_PER_PIXEL;
p[0] = RGB_RED;
}
}
return 0;
/* TODO */
}
static int
text_width(mu_Font font, const char *str, int len) {
if (!str || !*str) { return 0; }
int w = 0;
int c = 0;
if (TTF_MeasureUTF8(font, str, INT_MAX, &w, &c) != 0) {
w = 0;
}
return w;
}
static int
text_height(mu_Font font) {
return TTF_FontHeight(font);
}
void
r_input(mu_Context *ctx) {
SDL_Event e;
while (SDL_PollEvent(&e)) {
handle_event(e, ctx);
}
}
static void
handle_event(SDL_Event e, mu_Context *ctx) {
switch (e.type) {
case SDL_QUIT: {
TTF_CloseFont(ctx->style->font);
TTF_Quit();
SDL_Quit();
exit(EXIT_SUCCESS);
}
break; case SDL_MOUSEMOTION: {
mu_input_mousemove(ctx, e.motion.x, e.motion.y);
}
break; case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: {
int b = button_map[e.button.button & 0xff];
if (b && e.type == SDL_MOUSEBUTTONDOWN) {
mu_input_mousedown(ctx, e.button.x, e.button.y, b);
}
if (b && e.type == SDL_MOUSEBUTTONUP) {
mu_input_mouseup(ctx, e.button.x, e.button.y, b);
}
}
break; case SDL_MOUSEWHEEL: {
mu_input_scroll(ctx, 0, e.wheel.y * -30);
}
break; case SDL_KEYDOWN: case SDL_KEYUP: {
int c = key_map[e.key.keysym.sym & 0xff];
if (c && e.type == SDL_KEYDOWN) {
mu_input_keydown(ctx, c);
}
if (c && e.type == SDL_KEYUP) {
mu_input_keyup(ctx, c);
}
}
break; case SDL_TEXTINPUT: {
mu_input_text(ctx, e.text.text);
}
}
}
void
r_render(mu_Context *ctx) {
clear();
mu_Command *cmd = NULL;
while (mu_next_command(ctx, &cmd)) {
render_command(cmd);
}
SDL_RenderPresent(renderer);
}
static void
clear(void) {
if (SDL_SetRenderDrawColor(renderer, bg.r, bg.g, bg.b, bg.a) != 0) {
fprintf(stderr, "%s", SDL_GetError());
}
if (SDL_RenderClear(renderer) != 0) {
fprintf(stderr, "%s", SDL_GetError());
}
}
static void
render_command(mu_Command *cmd) {
switch (cmd->type) {
case MU_COMMAND_CLIP: {
clip(cmd->clip.rect);
}
break; case MU_COMMAND_RECT: {
draw_rect(cmd->rect.rect, cmd->rect.color);
}
break; case MU_COMMAND_TEXT: {
draw_text(cmd->text.font, cmd->text.pos, cmd->text.color, cmd->text.str);
}
break; case MU_COMMAND_ICON: {
draw_icon(cmd->icon.id, cmd->icon.rect);
}
}
}
static void
clip(mu_Rect rect) {
SDL_Rect r = {rect.x, rect.y, rect.w, rect.h};
if (SDL_RenderSetClipRect(renderer, &r) != 0) {
fprintf(stderr, "%s\n", SDL_GetError());
}
}
static void
draw_rect(mu_Rect rect, mu_Color color) {
if (SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a) != 0) {
fprintf(stderr, "%s\n", SDL_GetError());
}
SDL_Rect r = {rect.x, rect.y, rect.w, rect.h};
if (SDL_RenderFillRect(renderer, &r) != 0) {
fprintf(stderr, "%s\n", SDL_GetError());
}
}
static void
draw_text(mu_Font font, mu_Vec2 pos, mu_Color color, const char *str) {
if (!str || !*str) { return; }
SDL_Color sdl_color = {color.r, color.g, color.b, color.a};
SDL_Surface *surface = TTF_RenderUTF8_Blended(font, str, sdl_color);
if (!surface) {
fprintf(stderr, "%s\n", TTF_GetError());
return;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
fprintf(stderr, "%s\n", SDL_GetError());
SDL_FreeSurface(surface);
return;
}
int descent = TTF_FontDescent(font);
if (descent > 0) { /* v-align fonts with positive descent. */
pos.y -= descent;
}
SDL_Rect dst = {pos.x, pos.y, surface->w, surface->h};
if (SDL_RenderCopy(renderer, texture, NULL, &dst) != 0) {
fprintf(stderr, "%s\n", SDL_GetError());
}
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
static void
draw_icon(int id, mu_Rect r) {
SDL_Rect rect;
expect(id >= 0 && id < icon_list.idx);
rect = (SDL_Rect) {r.x, r.y, r.w, r.h};
if (SDL_RenderCopy(renderer, icon_list.items[id], NULL, &rect) != 0) {
fprintf(stderr, "%s\n", SDL_GetError());
}
}
void
r_get_window_size(int *w, int*h) {
if (SDL_GetRendererOutputSize(renderer, w, h) != 0) {
fprintf(stderr, "%s\n", SDL_GetError());
}
}
/* Load an image and add it to the list of icons. Returns the id of the icon, or -1 on error. */
int
r_add_icon(const char *path) {
SDL_Surface *surface;
expect(icon_list.idx < ICONLIST_SIZE);
surface = IMG_Load(path);
if (!surface) {
fprintf(stderr, "failed to load %s: %s\n", path, SDL_GetError());
return -1;
}
icon_list.items[icon_list.idx] = SDL_CreateTextureFromSurface(renderer, surface);
if (!icon_list.items[icon_list.idx]) {
fprintf(stderr, "%s\n", SDL_GetError());
SDL_FreeSurface(surface);
return -1;
}
SDL_FreeSurface(surface);
return icon_list.idx++;
}
/* Remove the icon with the specified id from the icons list. */
void
r_remove_icon(int id) {
SDL_Texture **dst, **src;
size_t size;
expect(id >= 0 && id < icon_list.idx);
SDL_DestroyTexture(icon_list.items[id]);
dst = icon_list.items + id;
src = icon_list.items + id + 1;
size = (icon_list.idx - id - 1) * sizeof(*icon_list.items);
memmove(dst, src, size);
icon_list.idx--;
}
void
r_get_icon_size(int id, int *w, int *h) {
expect(id >= 0 && id < icon_list.idx);
*w = *h = 0;
if (SDL_QueryTexture(icon_list.items[id], NULL, NULL, w, h) != 0) {
fprintf(stderr, "%s\n", SDL_GetError());
}
}
/* Create a canvas with the background loaded from an image file.
* Returns the id of the canvas, or -1 on error. */
int
r_add_canvas(const char *bg_img_path) {
Canvas *c;
expect(canvas_list.idx < CANVASLIST_SIZE);
c = &canvas_list.items[canvas_list.idx];
c->bg = IMG_Load(bg_img_path);
if (!c->bg) {
fprintf(stderr, "failed to load %s: %s\n", bg_img_path, SDL_GetError());
return -1;
}
c->fg = SDL_CreateRGBSurface(0, c->bg->w, c->bg->h, RGBA_DEPTH, 0, 0, 0, 0);
if (!c->fg) {
fprintf(stderr, "%s\n", SDL_GetError());
SDL_FreeSurface(c->bg);
return -1;
}
c->dst = SDL_CreateRGBSurface(0, c->bg->w, c->bg->h, RGBA_DEPTH, 0, 0, 0, 0);
if (!c->dst) {
fprintf(stderr, "%s\n", SDL_GetError());
SDL_FreeSurface(c->bg);
SDL_FreeSurface(c->fg);
return -1;
}
return canvas_list.idx++;
}
void
r_remove_canvas(int id) {
Canvas *dst, *src;
size_t size;
expect(id >= 0 && id < canvas_list.idx);
free_canvas(canvas_list.items[id]);
dst = canvas_list.items + id;
src= canvas_list.items + id + 1;
size = (canvas_list.idx - id - 1) * sizeof(*canvas_list.items);
memmove(dst, src, size);
canvas_list.idx--;
}
static void
free_canvas(Canvas c) {
SDL_FreeSurface(c.bg);
SDL_FreeSurface(c.fg);
SDL_FreeSurface(c.dst);
}
|