-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_sdl.c
84 lines (67 loc) · 2 KB
/
example_sdl.c
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
#include <SDL.h>
#include <stdbool.h>
// Use the ARGB pixel format
#define PNTR_PIXELFORMAT_ARGB
// Support the default font - not really needed here
#define PNTR_SUPPORT_DEFAULT_FONT
// This must be loaded first
#define PNTR_IMPLEMENTATION
#include "pntr.h"
// load pntr-font
#define PNTR_FONT_IMPLEMENTATION
#include "pntr-font.h"
// this is just for web demo
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
SDL_Window* window;
SDL_Surface* screen;
SDL_Surface* surface;
pntr_image* canvas;
bool shouldClose = false;
pntr_font* fontBeleive;
pntr_font* fontWintersong;
void mainloop() {
SDL_Event event;
while (SDL_PollEvent(&event) != 0) {
switch (event.type) {
case SDL_QUIT:
shouldClose = true;
break;
}
}
if (shouldClose) {
SDL_FreeSurface(surface);
// Unload
// TODO
SDL_DestroyWindow(window);
SDL_Quit();
} else {
// Update
pntr_draw_text(canvas, fontBeleive, "'Believe It' TTF Font Example", 20, 50);
pntr_draw_text(canvas, fontWintersong, "'Winter Song' TTF Font Example", 20, 80);
SDL_BlitSurface(surface, NULL, screen, NULL);
SDL_UpdateWindowSurface(window);
}
}
int main() {
// load beleiveit.ttf with 20px size
fontBeleive = pntr_load_font("fonts/beleiveit.ttf");
// load wintersong.ttf with 30px size
fontWintersong = pntr_load_font("fonts/wintersong.ttf");
// this is all regular pntr/SDL stuff
canvas = pntr_new_image(400, 225);
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
window = SDL_CreateWindow("pntr-font: Examples - SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, canvas->width, canvas->height, SDL_WINDOW_SHOWN);
screen = SDL_GetWindowSurface(window);
surface = SDL_CreateRGBSurfaceWithFormatFrom((void*)canvas->data, canvas->width, canvas->height, 8, canvas->pitch, SDL_PIXELFORMAT_ARGB8888);
// this is a cross-platrom way to support emscripten/native
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(mainloop, 0, 1);
#else
while (!shouldClose) {
mainloop();
SDL_Delay(16);
}
#endif
}