forked from tsoding/sowon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
394 lines (340 loc) · 12.5 KB
/
main.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
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
#define _CRT_SECURE_NO_WARNINGS
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <SDL.h>
#include "./digits.h"
#define FPS 60
//#define DELTA_TIME (1.0f / FPS)
#define SPRITE_CHAR_WIDTH (300 / 2)
#define SPRITE_CHAR_HEIGHT (380 / 2)
#define CHAR_WIDTH (300 / 2)
#define CHAR_HEIGHT (380 / 2)
#define CHARS_COUNT 8
#define TEXT_WIDTH (CHAR_WIDTH * CHARS_COUNT)
#define TEXT_HEIGHT (CHAR_HEIGHT)
#define WIGGLE_COUNT 3
#define WIGGLE_DURATION (0.40f / WIGGLE_COUNT)
#define COLON_INDEX 10
#define MAIN_COLOR_R 220
#define MAIN_COLOR_G 220
#define MAIN_COLOR_B 220
#define PAUSE_COLOR_R 220
#define PAUSE_COLOR_G 120
#define PAUSE_COLOR_B 120
#define BACKGROUND_COLOR_R 24
#define BACKGROUND_COLOR_G 24
#define BACKGROUND_COLOR_B 24
#define SCALE_FACTOR 0.15f
void secc(int code)
{
if (code < 0) {
fprintf(stderr, "SDL pooped itself: %s\n", SDL_GetError());
abort();
}
}
void *secp(void *ptr)
{
if (ptr == NULL) {
fprintf(stderr, "SDL pooped itself: %s\n", SDL_GetError());
abort();
}
return ptr;
}
SDL_Surface *load_png_file_as_surface()
{
SDL_Surface* image_surface =
secp(SDL_CreateRGBSurfaceFrom(
png,
(int) png_width,
(int) png_height,
32,
(int) png_width * 4,
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000));
return image_surface;
}
SDL_Texture *load_png_file_as_texture(SDL_Renderer *renderer)
{
SDL_Surface *image_surface = load_png_file_as_surface();
return secp(SDL_CreateTextureFromSurface(renderer, image_surface));
}
void render_digit_at(SDL_Renderer *renderer, SDL_Texture *digits, size_t digit_index,
size_t wiggle_index, int *pen_x, int *pen_y, float user_scale, float fit_scale)
{
const int effective_digit_width = (int) floorf((float) CHAR_WIDTH * user_scale * fit_scale);
const int effective_digit_height = (int) floorf((float) CHAR_HEIGHT * user_scale * fit_scale);
const SDL_Rect src_rect = {
(int) (digit_index * SPRITE_CHAR_WIDTH),
(int) (wiggle_index * SPRITE_CHAR_HEIGHT),
SPRITE_CHAR_WIDTH,
SPRITE_CHAR_HEIGHT
};
const SDL_Rect dst_rect = {
*pen_x,
*pen_y,
effective_digit_width,
effective_digit_height
};
SDL_RenderCopy(renderer, digits, &src_rect, &dst_rect);
*pen_x += effective_digit_width;
}
void initial_pen(SDL_Window *window, int *pen_x, int *pen_y, float user_scale, float *fit_scale)
{
int w, h;
SDL_GetWindowSize(window, &w, &h);
float text_aspect_ratio = (float) TEXT_WIDTH / (float) TEXT_HEIGHT;
float window_aspect_ratio = (float) w / (float) h;
if(text_aspect_ratio > window_aspect_ratio) {
*fit_scale = (float) w / (float) TEXT_WIDTH;
} else {
*fit_scale = (float) h / (float) TEXT_HEIGHT;
}
const int effective_digit_width = (int) floorf((float) CHAR_WIDTH * user_scale * *fit_scale);
const int effective_digit_height = (int) floorf((float) CHAR_HEIGHT * user_scale * *fit_scale);
*pen_x = w / 2 - effective_digit_width * CHARS_COUNT / 2;
*pen_y = h / 2 - effective_digit_height / 2;
}
typedef enum {
MODE_ASCENDING = 0,
MODE_COUNTDOWN,
MODE_CLOCK,
} Mode;
float parse_time(const char *time)
{
float result = 0.0f;
while (*time) {
char *endptr = NULL;
float x = strtof(time, &endptr);
if (time == endptr) {
fprintf(stderr, "`%s` is not a number\n", time);
exit(1);
}
switch (*endptr) {
case '\0':
case 's': result += x; break;
case 'm': result += x * 60.0f; break;
case 'h': result += x * 60.0f * 60.0f; break;
default:
fprintf(stderr, "`%c` is an unknown time unit\n", *endptr);
exit(1);
}
time = endptr;
if (*time) time += 1;
}
return result;
}
typedef struct {
Uint32 frame_delay;
float dt;
Uint64 last_time;
} FpsDeltaTime;
FpsDeltaTime make_fpsdeltatime(const Uint32 fps_cap)
{
return (FpsDeltaTime){
.frame_delay=(1000 / fps_cap),
.dt=0.0f,
.last_time=SDL_GetPerformanceCounter(),
};
}
void frame_start(FpsDeltaTime *fpsdt)
{
const Uint64 now = SDL_GetPerformanceCounter();
const Uint64 elapsed = now - fpsdt->last_time;
fpsdt->dt = ((float)elapsed) / ((float)SDL_GetPerformanceFrequency());
// printf("FPS: %f | dt %f\n", 1.0 / fpsdt->dt, fpsdt->dt);
fpsdt->last_time = now;
}
void frame_end(FpsDeltaTime *fpsdt)
{
const Uint64 now = SDL_GetPerformanceCounter();
const Uint64 elapsed = now - fpsdt->last_time;
const Uint32 cap_frame_end = (Uint32) ((((float)elapsed) * 1000.0f) / ((float)SDL_GetPerformanceFrequency()));
if (cap_frame_end < fpsdt->frame_delay) {
SDL_Delay((fpsdt->frame_delay - cap_frame_end) );
}
}
#define TITLE_CAP 256
int main(int argc, char **argv)
{
Mode mode = MODE_ASCENDING;
float displayed_time = 0.0f;
int paused = 0;
int exit_after_countdown = 0;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-p") == 0) {
paused = 1;
} else if (strcmp(argv[i], "-e") == 0) {
exit_after_countdown = 1;
} else if (strcmp(argv[i], "clock") == 0) {
mode = MODE_CLOCK;
} else {
mode = MODE_COUNTDOWN;
displayed_time = parse_time(argv[i]);
}
}
secc(SDL_Init(SDL_INIT_VIDEO));
SDL_Window *window =
secp(SDL_CreateWindow(
"sowon",
0, 0, TEXT_WIDTH, TEXT_HEIGHT,
SDL_WINDOW_RESIZABLE));
SDL_Renderer *renderer =
secp(SDL_CreateRenderer(
window, -1,
SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED));
secc(SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"));
SDL_Texture *digits = load_png_file_as_texture(renderer);
secc(SDL_SetTextureColorMod(digits, MAIN_COLOR_R, MAIN_COLOR_G, MAIN_COLOR_B));
if (paused) {
secc(SDL_SetTextureColorMod(digits, PAUSE_COLOR_R, PAUSE_COLOR_G, PAUSE_COLOR_B));
} else {
secc(SDL_SetTextureColorMod(digits, MAIN_COLOR_R, MAIN_COLOR_G, MAIN_COLOR_B));
}
int quit = 0;
size_t wiggle_index = 0;
float wiggle_cooldown = WIGGLE_DURATION;
float user_scale = 1.0f;
char prev_title[TITLE_CAP];
FpsDeltaTime fps_dt = make_fpsdeltatime(FPS);
while (!quit) {
frame_start(&fps_dt);
// INPUT BEGIN //////////////////////////////
SDL_Event event = {0};
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT: {
quit = 1;
} break;
case SDL_KEYDOWN: {
switch (event.key.keysym.sym) {
case SDLK_SPACE: {
paused = !paused;
if (paused) {
secc(SDL_SetTextureColorMod(digits, PAUSE_COLOR_R, PAUSE_COLOR_G, PAUSE_COLOR_B));
} else {
secc(SDL_SetTextureColorMod(digits, MAIN_COLOR_R, MAIN_COLOR_G, MAIN_COLOR_B));
}
} break;
case SDLK_KP_PLUS:
case SDLK_EQUALS: {
user_scale += SCALE_FACTOR * user_scale;
} break;
case SDLK_KP_MINUS:
case SDLK_MINUS: {
user_scale -= SCALE_FACTOR * user_scale;
} break;
case SDLK_KP_0:
case SDLK_0: {
user_scale = 1.0f;
} break;
case SDLK_F5: {
displayed_time = 0.0f;
paused = 0;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-p") == 0) {
paused = 1;
} else {
displayed_time = parse_time(argv[i]);
}
}
if (paused) {
secc(SDL_SetTextureColorMod(digits, PAUSE_COLOR_R, PAUSE_COLOR_G, PAUSE_COLOR_B));
} else {
secc(SDL_SetTextureColorMod(digits, MAIN_COLOR_R, MAIN_COLOR_G, MAIN_COLOR_B));
}
} break;
case SDLK_F11: {
Uint32 window_flags;
secc(window_flags = SDL_GetWindowFlags(window));
if(window_flags & SDL_WINDOW_FULLSCREEN_DESKTOP) {
secc(SDL_SetWindowFullscreen(window, 0));
} else {
secc(SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP));
}
} break;
}
} break;
case SDL_MOUSEWHEEL: {
if (SDL_GetModState() & KMOD_CTRL) {
if (event.wheel.y > 0) {
user_scale += SCALE_FACTOR * user_scale;
} else if (event.wheel.y < 0) {
user_scale -= SCALE_FACTOR * user_scale;
}
}
} break;
default: {}
}
}
// INPUT END //////////////////////////////
// RENDER BEGIN //////////////////////////////
SDL_SetRenderDrawColor(renderer, BACKGROUND_COLOR_R, BACKGROUND_COLOR_G, BACKGROUND_COLOR_B, 255);
SDL_RenderClear(renderer);
{
int pen_x, pen_y;
float fit_scale = 1.0;
initial_pen(window, &pen_x, &pen_y, user_scale, &fit_scale);
const size_t t = (size_t) ceilf(fmaxf(displayed_time, 0.0f));
// TODO: support amount of hours >99
const size_t hours = t / 60 / 60;
render_digit_at(renderer, digits, hours / 10, wiggle_index % WIGGLE_COUNT, &pen_x, &pen_y, user_scale, fit_scale);
render_digit_at(renderer, digits, hours % 10, (wiggle_index + 1) % WIGGLE_COUNT, &pen_x, &pen_y, user_scale, fit_scale);
render_digit_at(renderer, digits, COLON_INDEX, wiggle_index % WIGGLE_COUNT, &pen_x, &pen_y, user_scale, fit_scale);
const size_t minutes = t / 60 % 60;
render_digit_at(renderer, digits, minutes / 10, (wiggle_index + 2) % WIGGLE_COUNT, &pen_x, &pen_y, user_scale, fit_scale);
render_digit_at(renderer, digits, minutes % 10, (wiggle_index + 3) % WIGGLE_COUNT, &pen_x, &pen_y, user_scale, fit_scale);
render_digit_at(renderer, digits, COLON_INDEX, (wiggle_index + 1) % WIGGLE_COUNT, &pen_x, &pen_y, user_scale, fit_scale);
const size_t seconds = t % 60;
render_digit_at(renderer, digits, seconds / 10, (wiggle_index + 4) % WIGGLE_COUNT, &pen_x, &pen_y, user_scale, fit_scale);
render_digit_at(renderer, digits, seconds % 10, (wiggle_index + 5) % WIGGLE_COUNT, &pen_x, &pen_y, user_scale, fit_scale);
char title[TITLE_CAP];
snprintf(title, sizeof(title), "%02zu:%02zu:%02zu - sowon", hours, minutes, seconds);
if (strcmp(prev_title, title) != 0) {
SDL_SetWindowTitle(window, title);
}
memcpy(title, prev_title, TITLE_CAP);
}
SDL_RenderPresent(renderer);
// RENDER END //////////////////////////////
// UPDATE BEGIN //////////////////////////////
if (wiggle_cooldown <= 0.0f) {
wiggle_index++;
wiggle_cooldown = WIGGLE_DURATION;
}
wiggle_cooldown -= fps_dt.dt;
if (!paused) {
switch (mode) {
case MODE_ASCENDING: {
displayed_time += fps_dt.dt;
} break;
case MODE_COUNTDOWN: {
if (displayed_time > 1e-6) {
displayed_time -= fps_dt.dt;
} else {
displayed_time = 0.0f;
if (exit_after_countdown) {
SDL_Quit();
return 0;
}
}
} break;
case MODE_CLOCK: {
time_t t = time(NULL);
struct tm *tm = localtime(&t);
displayed_time = tm->tm_sec
+ tm->tm_min * 60.0f
+ tm->tm_hour * 60.0f * 60.0f;
} break;
}
}
// UPDATE END //////////////////////////////
frame_end(&fps_dt);
}
SDL_Quit();
return 0;
}