commit 54e0a086a136ac517b25dd247d3861182237c0ee
parent a95103b1fb47458c53198b192fe0e66885be7c2a
Author: Lou Woell <lou.woell@posteo.de>
Date: Fri, 2 Feb 2024 20:22:10 +0100
General Cleanup
Diffstat:
| M | makefile | | | 3 | ++- |
| M | pong.c | | | 259 | ++++++++++++++++++++++++++++++++++++++++++++++--------------------------------- |
2 files changed, 154 insertions(+), 108 deletions(-)
diff --git a/makefile b/makefile
@@ -10,6 +10,7 @@ BIN = pong
# Only used for WASM compilation
RAYLIB_PATH = ~/Programming/raylib/
+EMSCRIPTEN_PATH = /etc/profile.d/emscripten.sh
$(BIN): $(OBJS)
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
@@ -19,4 +20,4 @@ $(BIN): $(OBJS)
.PHONY: web
web:
- source /etc/profile.d/emscripten.sh && emcc -o $(BIN).html pong.c -Wall $(RAYLIB_PATH)/src/libraylib.a -I. -I $(RAYLIB_PATH)/src -I $(RAYLIB_PATH)/src/external -L. -L $(RAYLIB_PATH)/src -s USE_GLFW=3 -s ASYNCIFY --shell-file $(RAYLIB_PATH)/src/minshell.html -DPLATFORM_WEB -s GL_ENABLE_GET_PROC_ADDRESS
+ source $(EMSCRIPTEN_PATH) && emcc -o $(BIN).html pong.c -Wall -Os $(RAYLIB_PATH)/src/libraylib.a -I. -I $(RAYLIB_PATH)/src -I $(RAYLIB_PATH)/src/external -L. -L $(RAYLIB_PATH)/src -s USE_GLFW=3 --shell-file $(RAYLIB_PATH)/src/minshell.html -DPLATFORM_WEB -s GL_ENABLE_GET_PROC_ADDRESS
diff --git a/pong.c b/pong.c
@@ -3,103 +3,117 @@
#include <math.h>
#include <raylib.h>
+#if defined(PLATFORM_WEB)
+ #include <emscripten/emscripten.h>
+#endif
+
// Color Scheme of my Website
-#define DAY_COLOR CLITERAL(Color) { 238, 114, 241, 255}
-#define NIGHT_COLOR CLITERAL(Color) { 33, 32, 44, 255}
+#define DAY_COLOR CLITERAL(Color) { 238, 114, 241, 255}
+#define NIGHT_COLOR CLITERAL(Color) { 33, 32, 44, 255}
// Alternative Neutral Colors
-/* #define DAY_COLOR SKYBLUE */
-/* #define NIGHT_COLOR DARKBLUE */
+/* #define DAY_COLOR SKYBLUE */
+/* #define NIGHT_COLOR DARKBLUE */
-#define HEIGHT 600
-#define WIDTH 600
+#define SQUARE_SIZE 25
-#define SQUARE_SIZE 25
+#define MAX_RECS_X 24
+#define MAX_RECS_Y 24
-#define MAX_RECS_X WIDTH/SQUARE_SIZE
-#define MAX_RECS_Y HEIGHT/SQUARE_SIZE
+#define FONTSIZE 30
+#define PAUSE_TEXT "PAUSED"
-#define FONTSIZE 30
-#define PAUSE_TEXT "PAUSED"
+#define HEIGHT SQUARE_SIZE * MAX_RECS_Y
+#define WIDTH SQUARE_SIZE * MAX_RECS_X
typedef struct {
Vector2 Position;
Vector2 Speed;
- float Radius;
- Color Color;
+ float Radius;
+ Color Color;
} bBall;
typedef struct {
+ int squareSize;
Rectangle recs[MAX_RECS_X * MAX_RECS_Y];
- Color colors[MAX_RECS_X * MAX_RECS_Y];
+ Color colors[MAX_RECS_X * MAX_RECS_Y];
} Board;
typedef struct {
+ int framesCounter;
+ bool pause;
Board board;
bBall DayBall;
bBall NightBall;
} Game;
-void MakeBoard(Board *board) {
+Game game = {0};
+int pause_text_width;
+
+void MakeBoard(Board *board)
+{
for (int y = 0; y < MAX_RECS_Y; y++)
{
for (int x = 0; x < MAX_RECS_X; x++)
{
- board->recs[y*MAX_RECS_X + x].x = SQUARE_SIZE/2.0f + SQUARE_SIZE*x;
- board->recs[y*MAX_RECS_X + x].y = SQUARE_SIZE/2.0f + SQUARE_SIZE*y;
- board->recs[y*MAX_RECS_X + x].width = SQUARE_SIZE;
- board->recs[y*MAX_RECS_X + x].height = SQUARE_SIZE;
- if (MAX_RECS_X/2 > x) {
+ board->recs[y * MAX_RECS_X + x].x = SQUARE_SIZE / 2.0f + SQUARE_SIZE * x;
+ board->recs[y * MAX_RECS_X + x].y = SQUARE_SIZE / 2.0f + SQUARE_SIZE * y;
+ board->recs[y * MAX_RECS_X + x].width = SQUARE_SIZE;
+ board->recs[y * MAX_RECS_X + x].height = SQUARE_SIZE;
+ if (MAX_RECS_X / 2 > x)
board->colors[y * MAX_RECS_X + x] = DAY_COLOR;
- }
- else {
- board->colors[y * MAX_RECS_X + x] = NIGHT_COLOR;
- }
+ else
+ board->colors[y * MAX_RECS_X + x] = NIGHT_COLOR;
}
}
}
-void DrawBoard(Board *board) {
- for (int i = 0; i < MAX_RECS_X * MAX_RECS_Y; i++) {
- DrawRectanglePro(
- board->recs[i],
- (Vector2){board->recs[i].width / 2, board->recs[i].height / 2},
- 0,
- board->colors[i]);
- }
+void DrawBoard(Board *board)
+{
+ for (int i = 0; i < MAX_RECS_X * MAX_RECS_Y; i++)
+ {
+ DrawRectanglePro(
+ board->recs[i],
+ (Vector2){
+ board->recs[i].width / 2,
+ board->recs[i].height / 2
+ },
+ 0,
+ board->colors[i]);
+ }
}
int RandomOffset(int offset)
{
return GetRandomValue(-(offset), (offset));
-}
+}
-bool coloreq(Color first, Color second) {
- if (first.r == second.r &&
- first.g == second.g &&
- first.b == second.b &&
- first.a == second.a) return true;
+bool coloreq(Color first, Color second)
+{
+ if (first.r == second.r &&
+ first.g == second.g &&
+ first.b == second.b &&
+ first.a == second.a) return true;
return false;
}
-void flipColor(Color *tile) {
- if (coloreq(*tile, DAY_COLOR)) {
+void flipColor(Color *tile)
+{
+ if (coloreq(*tile, DAY_COLOR))
*tile = NIGHT_COLOR;
- } else {
+ else
*tile = DAY_COLOR;
- }
}
-void MakeBouncingBall(bBall * ball, Color color, float startx, float starty) {
-
- float xspeed = 12.5;
- float yspeed = 12.5;
+void MakeBouncingBall(bBall *ball, Color color, float startx, float starty)
+{
+ float xspeed = SQUARE_SIZE/2.0f - 1;
+ float yspeed = SQUARE_SIZE/2.0f - 1;
- if (startx > WIDTH / 2.0) {
+ if (startx > WIDTH / 2.0f)
xspeed *= -1;
- } else {
+ else
yspeed *= -1;
- }
ball->Position = (Vector2){ startx, starty};
ball->Speed = (Vector2){ xspeed, yspeed };
@@ -107,100 +121,131 @@ void MakeBouncingBall(bBall * ball, Color color, float startx, float starty) {
ball->Color = color;
}
-void BouncingBallPosition(bBall *ball, Color *color_array) {
+void BouncingBallPosition(bBall *ball, Board *board)
+{
ball->Position.x += ball->Speed.x;
ball->Position.y += ball->Speed.y;
-
+
// Check walls collision
if ((ball->Position.x >= (WIDTH - ball->Radius)) ||
(ball->Position.x <= ball->Radius)) ball->Speed.x *= -1.0f;
if ((ball->Position.y >= (HEIGHT - ball->Radius)) ||
(ball->Position.y <= ball->Radius)) ball->Speed.y *= -1.0f;
- // Check Colour collision
- for (double angle = 0; angle < (2 * PI); angle += (PI / 4)) {
-
- int i = floor((ball->Position.x + cos(angle) * ball->Radius) / SQUARE_SIZE);
- int j = floor((ball->Position.y + sin(angle) * ball->Radius) / SQUARE_SIZE);
+ // Check Colour collision
+ for (double angle = 0; angle < (2 * PI); angle += (PI / 4))
+ {
- if (i >= 0 && i < MAX_RECS_X && j >= 0 && j < MAX_RECS_Y) {
+ int i = floor((ball->Position.x + cos(angle) * ball->Radius) /
+ SQUARE_SIZE);
+ int j = floor((ball->Position.y + sin(angle) * ball->Radius) /
+ SQUARE_SIZE);
- int k = j * MAX_RECS_X + i;
+ if (i >= 0 && i < MAX_RECS_X && j >= 0 && j < MAX_RECS_Y)
+ {
- if (coloreq(color_array[k], ball->Color)) {
- flipColor(&(color_array[k]));
- // Determine bounce direction based on the angle
- if (fabs(cos(angle)) > fabs(sin(angle))) {
- ball->Speed.x *= -1;
- } else {
- ball->Speed.y *= -1;
- }
- }
+ int k = j * MAX_RECS_X + i;
+
+ if (coloreq(board->colors[k], ball->Color))
+ {
+ flipColor(&(board->colors[k]));
+ // Determine bounce direction based on the angle
+ if (fabs(cos(angle)) > fabs(sin(angle)))
+ ball->Speed.x *= -1;
+ else
+ ball->Speed.y *= -1;
+ }
+ }
}
- }
}
-void DrawBouncingBall(bBall * ball) {
- /* Draw Bouncing Ball */
- DrawCircleV(ball->Position, ball->Radius, ball->Color);
+void DrawBouncingBall(bBall *ball)
+{
+ /* Draw Bouncing Ball */
+ DrawCircleV(ball->Position, ball->Radius, ball->Color);
}
-void SetGame(Game *game) {
- MakeBoard(&game->board);
- MakeBouncingBall(&game->DayBall,
+void SetGame()
+{
+ MakeBoard(&game.board);
+ MakeBouncingBall(&game.DayBall,
DAY_COLOR,
- (WIDTH / 4.0f) * 3 + RandomOffset(WIDTH / 4),
+ (WIDTH / 4.0f) * 3 +
+ RandomOffset(WIDTH / 4),
HEIGHT/2.0f + RandomOffset(HEIGHT/2));
- MakeBouncingBall(&game->NightBall,
+ MakeBouncingBall(&game.NightBall,
NIGHT_COLOR,
- (WIDTH / 4.0f) + RandomOffset(WIDTH / 4),
+ (WIDTH / 4.0f) +
+ RandomOffset(WIDTH / 4),
HEIGHT/2.0f + RandomOffset(HEIGHT/2));
}
-void DrawGame(Game *game) {
- DrawBoard(&game->board);
- DrawBouncingBall(&game->DayBall);
- DrawBouncingBall(&game->NightBall);
-}
+void DrawGame()
+{
+ BeginDrawing();
-int main() {
+ ClearBackground(RAYWHITE);
- const char * title = "Pong Wars";
+ DrawBoard(&game.board);
+ DrawBouncingBall(&game.DayBall);
+ DrawBouncingBall(&game.NightBall);
- InitWindow(WIDTH,HEIGHT, title);
+ // On pause, we draw a blinking message
+ if (game.pause && ((game.framesCounter/30)%2))
+ {
+ DrawText(PAUSE_TEXT,
+ (WIDTH - pause_text_width)/2,
+ HEIGHT/3,
+ FONTSIZE,
+ WHITE);
+ }
- bool pause = 0;
- int framesCounter = 0;
+ EndDrawing();
+}
- const int pause_text_width = MeasureText(PAUSE_TEXT, FONTSIZE);
+void handleInput()
+{
+ // Press P to Pause
+ if (IsKeyPressed(KEY_P)) game.pause = !game.pause;
+ // Press R to reset
+ if (IsKeyPressed(KEY_R)) SetGame();
+}
- Game game = {0};
- SetGame(&game);
+void UpdateDrawFrame(void)
+{
+ handleInput();
- SetTargetFPS(60);
- while(!WindowShouldClose())
+ if (!game.pause)
{
- // Press P to Pause
- if (IsKeyPressed(KEY_P)) pause = !pause;
+ BouncingBallPosition(&game.DayBall, &game.board);
+ BouncingBallPosition(&game.NightBall, &game.board);
+ }
+ else game.framesCounter++;
- // Press R to reset
- if (IsKeyPressed(KEY_R)) SetGame(&game);
+ DrawGame();
+}
- if (!pause) {
- BouncingBallPosition(&game.DayBall, game.board.colors);
- BouncingBallPosition(&game.NightBall, game.board.colors);
- }
- else framesCounter++;
+int main()
+{
+ const char * title = "Pong Wars";
+
+ SetConfigFlags(FLAG_VSYNC_HINT);
+ InitWindow(WIDTH,HEIGHT, title);
- BeginDrawing();
- ClearBackground(RAYWHITE);
- DrawGame(&game);
+ // needs initialized Window
+ pause_text_width = MeasureText(PAUSE_TEXT, FONTSIZE);
- // On pause, we draw a blinking message
- if (pause && ((framesCounter/30)%2)) DrawText(PAUSE_TEXT, (WIDTH - pause_text_width)/2, HEIGHT/3, FONTSIZE, WHITE);
+ SetGame();
- EndDrawing();
+#if defined(PLATFORM_WEB)
+ emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
+#else
+ SetTargetFPS(60);
+ while (!WindowShouldClose())
+ {
+ UpdateDrawFrame();
}
+#endif
CloseWindow();
return 0;