Skip to content

Commit

Permalink
Add Raylib "Platform"
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Feb 9, 2024
1 parent 36172ce commit 9448a21
Show file tree
Hide file tree
Showing 13 changed files with 8,863 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
game.o
sdl_main
raylib_main
*.swp
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ set -xe

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

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
31 changes: 24 additions & 7 deletions game.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "./game.h"

// #define FEATURE_DYNAMIC_CAMERA
// #define FEATURE_DEV
#define FEATURE_DEV

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

typedef enum {
ALIGN_LEFT,
ALIGN_RIGHT,
ALIGN_CENTER,
} Align;

static void fill_text_aligned(i32 x, i32 y, const char *text, u32 size, u32 color, Align align)
{
u32 width = platform_text_width(text, size);
switch (align) {
case ALIGN_LEFT: break;
case ALIGN_CENTER: x -= width/2; break;
case ALIGN_RIGHT: x -= width; break;
}
platform_fill_text(x, y, text, size, color);
}

static u32 rand(void)
{
static u64 rand_state = 0;
Expand Down Expand Up @@ -578,26 +595,26 @@ void game_render(void)
background_render();
egg_render();
snake_render();
platform_fill_text(SCORE_PADDING, SCORE_PADDING, game.score_buffer, SCORE_FONT_SIZE, SCORE_FONT_COLOR, ALIGN_LEFT);
fill_text_aligned(SCORE_PADDING, SCORE_PADDING, game.score_buffer, SCORE_FONT_SIZE, SCORE_FONT_COLOR, ALIGN_LEFT);
}
break;

case STATE_PAUSE: {
background_render();
egg_render();
snake_render();
platform_fill_text(SCORE_PADDING, SCORE_PADDING, game.score_buffer, SCORE_FONT_SIZE, SCORE_FONT_COLOR, ALIGN_LEFT);
fill_text_aligned(SCORE_PADDING, SCORE_PADDING, game.score_buffer, SCORE_FONT_SIZE, SCORE_FONT_COLOR, ALIGN_LEFT);
// TODO: "Pause", "Game Over" are not centered vertically
platform_fill_text(game.width/2, game.height/2, "Pause", PAUSE_FONT_SIZE, PAUSE_FONT_COLOR, ALIGN_CENTER);
fill_text_aligned(game.width/2, game.height/2, "Pause", PAUSE_FONT_SIZE, PAUSE_FONT_COLOR, ALIGN_CENTER);
}
break;

case STATE_GAMEOVER: {
background_render();
egg_render();
dead_snake_render();
platform_fill_text(SCORE_PADDING, SCORE_PADDING, game.score_buffer, SCORE_FONT_SIZE, SCORE_FONT_COLOR, ALIGN_LEFT);
platform_fill_text(game.width/2, game.height/2, "Game Over", GAMEOVER_FONT_SIZE, GAMEOVER_FONT_COLOR, ALIGN_CENTER);
fill_text_aligned(SCORE_PADDING, SCORE_PADDING, game.score_buffer, SCORE_FONT_SIZE, SCORE_FONT_COLOR, ALIGN_LEFT);
fill_text_aligned(game.width/2, game.height/2, "Game Over", GAMEOVER_FONT_SIZE, GAMEOVER_FONT_COLOR, ALIGN_CENTER);
}
break;

Expand All @@ -607,7 +624,7 @@ void game_render(void)
}

#ifdef FEATURE_DEV
platform_fill_text(game.width - SCORE_PADDING, SCORE_PADDING, "Dev", SCORE_FONT_SIZE, SCORE_FONT_COLOR, ALIGN_RIGHT);
fill_text_aligned(game.width - SCORE_PADDING, SCORE_PADDING, "Dev", SCORE_FONT_SIZE, SCORE_FONT_COLOR, ALIGN_RIGHT);
Rect rect = { .w = COLS*CELL_SIZE, .h = ROWS*CELL_SIZE };
stroke_rect(rect, 0xFF0000FF);
#endif
Expand Down
12 changes: 3 additions & 9 deletions game.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef GAME_H_
#define GAME_H_
#define GAME_H_

#include <stddef.h>

Expand All @@ -10,16 +10,10 @@ typedef int i32;
typedef int b32;
typedef float f32;

typedef enum {
ALIGN_LEFT,
ALIGN_RIGHT,
ALIGN_CENTER,
} Align;

void platform_fill_rect(i32 x, i32 y, i32 w, i32 h, u32 color);
void platform_stroke_rect(i32 x, i32 y, i32 w, i32 h, u32 color);
void platform_fill_text(i32 x, i32 y, const char *text, u32 size, u32 color, Align align);
void platform_stroke_line(i32 x1, i32 y1, i32 x2, i32 y2, u32 color);
void platform_fill_text(i32 x, i32 y, const char *text, u32 size, u32 color);
u32 platform_text_width(const char *text, u32 size);
void platform_panic(const char *file_path, i32 line, const char *message);
void platform_log(const char *message);

Expand Down
Binary file modified game.wasm
Binary file not shown.
17 changes: 17 additions & 0 deletions include/math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Phony math.h. Since we are compiling with --no-standard-libraries raymath.h can't find math.h.
// But it only needs it for few function definitions. So we've put those definitions here.
#ifndef MATH_H_
#define MATH_H_
float floorf(float);
float fabsf(float);
double fabs(double);
float fmaxf(float, float);
float fminf(float, float);
float sqrtf(float);
float atan2f(float, float);
float cosf(float);
float sinf(float);
float acosf(float);
float asinf(float);
double tan(double);
#endif // MATH_H_
Loading

0 comments on commit 9448a21

Please sign in to comment.