Skip to content

Commit

Permalink
Ready. Set. Go!
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Feb 3, 2024
0 parents commit d300c4e
Show file tree
Hide file tree
Showing 9 changed files with 8,919 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
all: wasm/game.wasm

wasm/game.wasm: game.c
clang --target=wasm32 -I./include --no-standard-libraries -Wl,--no-entry -Wl,--allow-undefined -Wl,--export=game_init -Wl,--export=game_frame -Wl,--export=game_over -o wasm/game.wasm game.c -DPLATFORM_WEB
58 changes: 58 additions & 0 deletions game.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <raylib.h>
#include <raymath.h>

#define BALL_RADIUS 100.0
#define GRAVITY 1000.0

static Vector2 ball_position = {0};
static Vector2 ball_velocity = {200, 200};

void game_frame()
{
BeginDrawing();
ClearBackground((Color){20, 20, 20, 255});
float dt = GetFrameTime();
ball_velocity.y += GRAVITY*dt;
float x = ball_position.x + ball_velocity.x*dt;
if (x - BALL_RADIUS < 0.0 || x + BALL_RADIUS >= GetScreenWidth()) {
ball_velocity.x *= -1.0f;
} else {
ball_position.x = x;
}
float y = ball_position.y + ball_velocity.y*dt;
if (y - BALL_RADIUS < 0.0 || y + BALL_RADIUS >= GetScreenHeight()) {
ball_velocity.y *= -1.0f;
} else {
ball_position.y = y;
}
DrawCircleV(ball_position, BALL_RADIUS, RED);
EndDrawing();
}

void game_init()
{
InitWindow(800, 600, "Hello, from WebAssembly");
SetTargetFPS(60);

int w = GetScreenWidth();
int h = GetScreenHeight();
ball_position.x = w/2;
ball_position.y = h/2;
}

void game_over()
{
CloseWindow();
}

#ifndef PLATFORM_WEB
int main()
{
game_init();
while (!WindowShouldClose()) {
game_frame();
}
game_over();
return 0;
}
#endif
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 we 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 d300c4e

Please sign in to comment.