1
1
#include "./game.h"
2
2
3
3
// #define FEATURE_DYNAMIC_CAMERA
4
- // #define FEATURE_DEV
4
+ #define FEATURE_DEV
5
5
6
6
#define STB_SPRINTF_IMPLEMENTATION
7
7
#include "stb_sprintf.h"
@@ -52,6 +52,23 @@ static void platform_assert(const char *file, i32 line, b32 cond, const char *me
52
52
#define RAND_A 6364136223846793005ULL
53
53
#define RAND_C 1442695040888963407ULL
54
54
55
+ typedef enum {
56
+ ALIGN_LEFT ,
57
+ ALIGN_RIGHT ,
58
+ ALIGN_CENTER ,
59
+ } Align ;
60
+
61
+ static void fill_text_aligned (i32 x , i32 y , const char * text , u32 size , u32 color , Align align )
62
+ {
63
+ u32 width = platform_text_width (text , size );
64
+ switch (align ) {
65
+ case ALIGN_LEFT : break ;
66
+ case ALIGN_CENTER : x -= width /2 ; break ;
67
+ case ALIGN_RIGHT : x -= width ; break ;
68
+ }
69
+ platform_fill_text (x , y , text , size , color );
70
+ }
71
+
55
72
static u32 rand (void )
56
73
{
57
74
static u64 rand_state = 0 ;
@@ -578,26 +595,26 @@ void game_render(void)
578
595
background_render ();
579
596
egg_render ();
580
597
snake_render ();
581
- platform_fill_text (SCORE_PADDING , SCORE_PADDING , game .score_buffer , SCORE_FONT_SIZE , SCORE_FONT_COLOR , ALIGN_LEFT );
598
+ fill_text_aligned (SCORE_PADDING , SCORE_PADDING , game .score_buffer , SCORE_FONT_SIZE , SCORE_FONT_COLOR , ALIGN_LEFT );
582
599
}
583
600
break ;
584
601
585
602
case STATE_PAUSE : {
586
603
background_render ();
587
604
egg_render ();
588
605
snake_render ();
589
- platform_fill_text (SCORE_PADDING , SCORE_PADDING , game .score_buffer , SCORE_FONT_SIZE , SCORE_FONT_COLOR , ALIGN_LEFT );
606
+ fill_text_aligned (SCORE_PADDING , SCORE_PADDING , game .score_buffer , SCORE_FONT_SIZE , SCORE_FONT_COLOR , ALIGN_LEFT );
590
607
// TODO: "Pause", "Game Over" are not centered vertically
591
- platform_fill_text (game .width /2 , game .height /2 , "Pause" , PAUSE_FONT_SIZE , PAUSE_FONT_COLOR , ALIGN_CENTER );
608
+ fill_text_aligned (game .width /2 , game .height /2 , "Pause" , PAUSE_FONT_SIZE , PAUSE_FONT_COLOR , ALIGN_CENTER );
592
609
}
593
610
break ;
594
611
595
612
case STATE_GAMEOVER : {
596
613
background_render ();
597
614
egg_render ();
598
615
dead_snake_render ();
599
- platform_fill_text (SCORE_PADDING , SCORE_PADDING , game .score_buffer , SCORE_FONT_SIZE , SCORE_FONT_COLOR , ALIGN_LEFT );
600
- platform_fill_text (game .width /2 , game .height /2 , "Game Over" , GAMEOVER_FONT_SIZE , GAMEOVER_FONT_COLOR , ALIGN_CENTER );
616
+ fill_text_aligned (SCORE_PADDING , SCORE_PADDING , game .score_buffer , SCORE_FONT_SIZE , SCORE_FONT_COLOR , ALIGN_LEFT );
617
+ fill_text_aligned (game .width /2 , game .height /2 , "Game Over" , GAMEOVER_FONT_SIZE , GAMEOVER_FONT_COLOR , ALIGN_CENTER );
601
618
}
602
619
break ;
603
620
@@ -607,7 +624,7 @@ void game_render(void)
607
624
}
608
625
609
626
#ifdef FEATURE_DEV
610
- platform_fill_text (game .width - SCORE_PADDING , SCORE_PADDING , "Dev" , SCORE_FONT_SIZE , SCORE_FONT_COLOR , ALIGN_RIGHT );
627
+ fill_text_aligned (game .width - SCORE_PADDING , SCORE_PADDING , "Dev" , SCORE_FONT_SIZE , SCORE_FONT_COLOR , ALIGN_RIGHT );
611
628
Rect rect = { .w = COLS * CELL_SIZE , .h = ROWS * CELL_SIZE };
612
629
stroke_rect (rect , 0xFF0000FF );
613
630
#endif
0 commit comments