Skip to content

Commit 9448a21

Browse files
committed
Add Raylib "Platform"
1 parent 36172ce commit 9448a21

File tree

13 files changed

+8863
-40
lines changed

13 files changed

+8863
-40
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
game.o
22
sdl_main
3+
raylib_main
34
*.swp

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ set -xe
44

55
clang -Wall -Wextra -Wswitch-enum -c game.c
66
clang -Wall -Wextra -Wswitch-enum -o sdl_main sdl_main.c game.o -lSDL2 -lSDL2_ttf -lm
7+
clang -Wall -Wextra -Wswitch-enum -I./include/ -o raylib_main raylib_main.c game.o -L./lib/ -lraylib -lm
78

89
clang -Os -fno-builtin -Wall -Wextra -Wswitch-enum --target=wasm32 --no-standard-libraries -Wl,--export=game_init -Wl,--export=game_render -Wl,--export=game_update -Wl,--export=game_info -Wl,--export=game_keydown -Wl,--no-entry -Wl,--allow-undefined -o game.wasm game.c

game.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "./game.h"
22

33
// #define FEATURE_DYNAMIC_CAMERA
4-
// #define FEATURE_DEV
4+
#define FEATURE_DEV
55

66
#define STB_SPRINTF_IMPLEMENTATION
77
#include "stb_sprintf.h"
@@ -52,6 +52,23 @@ static void platform_assert(const char *file, i32 line, b32 cond, const char *me
5252
#define RAND_A 6364136223846793005ULL
5353
#define RAND_C 1442695040888963407ULL
5454

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+
5572
static u32 rand(void)
5673
{
5774
static u64 rand_state = 0;
@@ -578,26 +595,26 @@ void game_render(void)
578595
background_render();
579596
egg_render();
580597
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);
582599
}
583600
break;
584601

585602
case STATE_PAUSE: {
586603
background_render();
587604
egg_render();
588605
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);
590607
// 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);
592609
}
593610
break;
594611

595612
case STATE_GAMEOVER: {
596613
background_render();
597614
egg_render();
598615
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);
601618
}
602619
break;
603620

@@ -607,7 +624,7 @@ void game_render(void)
607624
}
608625

609626
#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);
611628
Rect rect = { .w = COLS*CELL_SIZE, .h = ROWS*CELL_SIZE };
612629
stroke_rect(rect, 0xFF0000FF);
613630
#endif

game.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#ifndef GAME_H_
2-
#define GAME_H_
2+
#define GAME_H_
33

44
#include <stddef.h>
55

@@ -10,16 +10,10 @@ typedef int i32;
1010
typedef int b32;
1111
typedef float f32;
1212

13-
typedef enum {
14-
ALIGN_LEFT,
15-
ALIGN_RIGHT,
16-
ALIGN_CENTER,
17-
} Align;
18-
1913
void platform_fill_rect(i32 x, i32 y, i32 w, i32 h, u32 color);
2014
void platform_stroke_rect(i32 x, i32 y, i32 w, i32 h, u32 color);
21-
void platform_fill_text(i32 x, i32 y, const char *text, u32 size, u32 color, Align align);
22-
void platform_stroke_line(i32 x1, i32 y1, i32 x2, i32 y2, u32 color);
15+
void platform_fill_text(i32 x, i32 y, const char *text, u32 size, u32 color);
16+
u32 platform_text_width(const char *text, u32 size);
2317
void platform_panic(const char *file_path, i32 line, const char *message);
2418
void platform_log(const char *message);
2519

game.wasm

868 Bytes
Binary file not shown.

include/math.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Phony math.h. Since we are compiling with --no-standard-libraries raymath.h can't find math.h.
2+
// But it only needs it for few function definitions. So we've put those definitions here.
3+
#ifndef MATH_H_
4+
#define MATH_H_
5+
float floorf(float);
6+
float fabsf(float);
7+
double fabs(double);
8+
float fmaxf(float, float);
9+
float fminf(float, float);
10+
float sqrtf(float);
11+
float atan2f(float, float);
12+
float cosf(float);
13+
float sinf(float);
14+
float acosf(float);
15+
float asinf(float);
16+
double tan(double);
17+
#endif // MATH_H_

0 commit comments

Comments
 (0)