forked from tsoding/zozlib.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsoding_ball.c
53 lines (46 loc) · 1.26 KB
/
tsoding_ball.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
#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 raylib_js_set_entry(void (*entry)(void));
void GameFrame()
{
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();
}
int main()
{
InitWindow(800, 600, "Hello, from WebAssembly");
SetTargetFPS(60);
int w = GetScreenWidth();
int h = GetScreenHeight();
ball_position.x = w/2;
ball_position.y = h/2;
#ifdef PLATFORM_WEB
raylib_js_set_entry(GameFrame);
#else
while (!WindowShouldClose()) {
GameFrame();
}
CloseWindow();
#endif
return 0;
}