-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo.c
347 lines (287 loc) · 10.2 KB
/
demo.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
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
/*
MIT No Attribution
Copyright (c) 2019-2023 Mika Tuupola
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-cut-
SPDX-License-Identifier: MIT-0
*/
#include <time.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include "hagl_hal.h"
#include "hagl.h"
#include "aps.h"
#include "font6x9.h"
#include "backend.h"
hagl_backend_t *backend;
static aps_instance_t pps;
static const uint64_t MS_PER_FRAME_60_FPS = 1000 / 60;
uint32_t
pps_callback(uint32_t interval, void *param)
{
printf("Primitives per second: %f\n", *(float *)param);
return interval;
}
void
grid_test()
{
uint16_t red = hagl_color(backend, 255, 0, 0);
uint16_t green = hagl_color(backend, 0, 255, 0);
uint16_t blue = hagl_color(backend, 0, 0, 255);
hagl_draw_rectangle(backend, 0, 0, backend->width - 1, backend->height - 1, red);
hagl_draw_hline(backend, 0, 0, backend->width - 1, green);
hagl_draw_hline(backend, 0, backend->height - 1, backend->width - 1, green);
hagl_draw_vline(backend, 0, 0, backend->height - 1, green);
hagl_draw_vline(backend, backend->width - 1, 0, backend->height - 1, green);
for (int16_t x = 16; x < backend->width; x += 16) {
hagl_draw_vline(backend, x, 0, backend->height - 1, red);
}
for (int16_t y = 16; y < backend->height; y += 16) {
hagl_draw_hline(backend, 0, y, backend->width - 1, red);
}
}
void
polygon_demo()
{
int16_t x0 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y0 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x1 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y1 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x2 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y2 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x3 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y3 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x4 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y4 = (rand() % 280) - 20; /* -20 ... 260 */
uint16_t colour = rand() % 0xffff;
int16_t vertices[10] = {x0, y0, x1, y1, x2, y2, x3, y3, x4, y4};
hagl_draw_polygon(backend, 5, vertices, colour);
}
void
fill_polygon_demo()
{
int16_t x0 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y0 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x1 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y1 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x2 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y2 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x3 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y3 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x4 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y4 = (rand() % 280) - 20; /* -20 ... 260 */
uint16_t colour = rand() % 0xffff;
int16_t vertices[10] = {x0, y0, x1, y1, x2, y2, x3, y3, x4, y4};
hagl_fill_polygon(backend, 5, vertices, colour);
}
void
circle_demo()
{
int16_t x0 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y0 = (rand() % 280) - 20; /* -20 ... 260 */
uint16_t r = (rand() % 40);
uint16_t colour = rand() % 0xffff;
hagl_draw_circle(backend, x0, y0, r, colour);
}
void
fill_circle_demo()
{
int16_t x0 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y0 = (rand() % 280) - 20; /* -20 ... 260 */
uint16_t r = (rand() % 40);
uint16_t colour = rand() % 0xffff;
hagl_fill_circle(backend, x0, y0, r, colour);
}
void
line_demo()
{
int16_t x0 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y0 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x1 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y1 = (rand() % 280) - 20; /* -20 ... 260 */
uint16_t colour = rand() % 0xffff;
hagl_draw_line(backend, x0, y0, x1, y1, colour);
}
void
rectangle_demo()
{
int16_t x0 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y0 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x1 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y1 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x2 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y2 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x3 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y3 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x4 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y4 = (rand() % 280) - 20; /* -20 ... 260 */
uint16_t colour = rand() % 0xffff;
hagl_draw_rectangle(backend, x0, y0, x1, y1, colour);
}
void
fill_rectangle_demo()
{
int16_t x0 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y0 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x1 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y1 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x2 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y2 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x3 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y3 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x4 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y4 = (rand() % 280) - 20; /* -20 ... 260 */
uint16_t colour = rand() % 0xffff;
hagl_fill_rectangle(backend, x0, y0, x1, y1, colour);
}
void
put_character_demo()
{
int16_t x0 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y0 = (rand() % 280) - 20; /* -20 ... 260 */
uint16_t colour = rand() % 0xffff;
wchar_t ascii = rand() % 127;
hagl_put_char(backend, ascii, x0, y0, colour, font6x9);
}
void
put_text_demo()
{
int16_t x0 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y0 = (rand() % 280) - 20; /* -20 ... 260 */
uint16_t colour = rand() % 0xffff;
hagl_put_text(backend, L"YO! MTV raps.", x0, y0, colour, font6x9);
}
void
put_pixel_demo()
{
int16_t x0 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y0 = (rand() % 280) - 20; /* -20 ... 260 */
uint16_t colour = rand() % 0xffff;
hagl_put_pixel(backend, x0, y0, colour);
}
void
triangle_demo()
{
int16_t x0 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y0 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x1 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y1 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x2 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y2 = (rand() % 280) - 20; /* -20 ... 260 */
uint16_t colour = rand() % 0xffff;
hagl_draw_triangle(backend, x0, y0, x1, y1, x2, y2, colour);
}
void
fill_triangle_demo()
{
int16_t x0 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y0 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x1 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y1 = (rand() % 280) - 20; /* -20 ... 260 */
int16_t x2 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y2 = (rand() % 280) - 20; /* -20 ... 260 */
uint16_t colour = rand() % 0xffff;
hagl_fill_triangle(backend, x0, y0, x1, y1, x2, y2, colour);
}
void
blit_xywh_demo()
{
hagl_bitmap_t bitmap;
bitmap.buffer = (uint8_t *) malloc(6 * 9 * sizeof(hagl_color_t));
int16_t x0 = (rand() % 360) - 20; /* -20 ... 340 */
int16_t y0 = (rand() % 280) - 20; /* -20 ... 260 */
uint16_t colour = rand() % 0xffff;
wchar_t ascii = rand() % 127;
if (0 == hagl_get_glyph(backend, ascii, colour, &bitmap, font6x9)) {
hagl_blit_xywh(backend, x0, y0, 24, 36, &bitmap);
}
}
void
rgb_demo()
{
uint16_t red = hagl_color(backend, 255, 0, 0);
uint16_t green = hagl_color(backend, 0, 255, 0);
uint16_t blue = hagl_color(backend, 0, 0, 255);
int16_t x0 = 0;
int16_t x1 = backend->width / 3;
int16_t x2 = 2 * x1;
hagl_fill_rectangle(backend, x0, 0, x1 - 1, backend->height, red);
hagl_fill_rectangle(backend, x1, 0, x2 - 1, backend->height, green);
hagl_fill_rectangle(backend, x2, 0, backend->width, backend->height, blue);
}
int
main()
{
backend = hagl_init();
srand(time(0));
uint32_t pps_delay = 2000; /* 0.5 fps */
uint16_t current_demo = 0;
float current_pps = 0.0; /* primitives per second */
SDL_TimerID pps_id;
pps_id = SDL_AddTimer(pps_delay, pps_callback, &pps.current);
bool quit = false;
SDL_Event event;
void (*demo[15])();
demo[0] = rgb_demo;
demo[1] = put_character_demo;
demo[2] = put_pixel_demo;
demo[3] = fill_triangle_demo;
demo[4] = triangle_demo;
demo[5] = fill_rectangle_demo;
demo[6] = rectangle_demo;
demo[7] = line_demo;
demo[8] = circle_demo;
demo[9] = fill_circle_demo;
demo[10] = polygon_demo;
demo[11] = fill_polygon_demo;
demo[12] = put_text_demo;
demo[13] = blit_xywh_demo;
demo[14] = grid_test;
printf("\n");
printf("Press any key to change demo.\n");
printf("Press ESC to quit.\n\n");
aps_init(&pps);
uint32_t start = SDL_GetTicks();
while (!quit) {
(*demo[current_demo])();
aps_update(&pps, 1);
uint32_t end = SDL_GetTicks();
int32_t delta = MS_PER_FRAME_60_FPS - (end - start);
if (delta < 0) {
hagl_flush(backend);
start = SDL_GetTicks();
}
if (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
if (event.type == SDL_KEYDOWN) {
if (SDLK_ESCAPE == event.key.keysym.sym) {
quit = true;
} else {
aps_reset(&pps);
hagl_clear(backend);
current_demo = (current_demo + 1) % 15;
}
}
}
}
SDL_RemoveTimer(pps_id);
hagl_close(backend);
return 0;
}