Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
Added background and base rendering, minor changes, got a few TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
xfnty committed Jun 27, 2023
1 parent d58fac4 commit e5031af
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 73 deletions.
File renamed without changes
File renamed without changes
Binary file removed assets/pipe-red.png
Binary file not shown.
File renamed without changes
File renamed without changes
2 changes: 1 addition & 1 deletion src/engine/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ bool _engine_init(engine_t* engine, int argc, const char** argv) {
debug_attach_signal_handler();

SetTraceLogCallback(_raylib_log_callback);
InitWindow(800, 450, "Pong");
InitWindow(640, 480, "Flappy Bird");
SetWindowState(FLAG_WINDOW_RESIZABLE);
SetExitKey(0);
SetTargetFPS(60);
Expand Down
2 changes: 1 addition & 1 deletion src/game/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ bool game_init(game_t* game) {
.was_initialized = true,
.is_running = true,
.state = (game_state_t) { 0 },
.canvas = LoadRenderTexture(160, 100),
.canvas = LoadRenderTexture(288, 512),
};

while (!IsTextureReady(game->canvas.texture));
Expand Down
97 changes: 97 additions & 0 deletions src/game/states/gameplay.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "game/states/gameplay.h"

#include <assert.h>
#include <math.h>

#include <raylib.h>

#include "system/defines.h"
#include "system/strid.h"
#define SCOPE_NAME "gameplay"
#include "system/logging.h"

#include "game/game.h"


const float pipe_speed = 100;
const float space_between_pipes = 100;

typedef struct gameplay_state_s {
Texture2D bg_day;
Texture2D base;
Texture2D pipe;
float pipe_window_Ys[2]; // 0 means no pipe, gets shifted right to left
} gameplay_state_t;


static void draw_pipes(gameplay_state_t* gameplay, game_t* game, update_context_t ctx);
static void draw_base(gameplay_state_t* gameplay, game_t* game, update_context_t ctx);

static void _gameplay_state_enter(game_state_t* state, game_t* game) {
state->data = malloc(sizeof(gameplay_state_t));
assert(state->data);
gameplay_state_t* gameplay = (gameplay_state_t*)state->data;

*gameplay = (gameplay_state_t) {0};

gameplay->bg_day = LoadTexture("assets/background-day.png");
assert(IsTextureReady(gameplay->bg_day));
gameplay->base = LoadTexture("assets/base.png");
assert(IsTextureReady(gameplay->base));
gameplay->pipe = LoadTexture("assets/pipe.png");
assert(IsTextureReady(gameplay->pipe));
}

static void _gameplay_state_update(game_state_t* state, game_t* game, update_context_t ctx) {
gameplay_state_t* gameplay = (gameplay_state_t*)state->data;

if (IsKeyPressed(KEY_ESCAPE))
game->is_running = false;

ClearBackground(WHITE);
DrawTexture(gameplay->bg_day, 0, 0, WHITE);

draw_base(gameplay, game, ctx);
}

static void _gameplay_state_exit(game_state_t* state, game_t* game) {
free(state->data);
}

game_state_t gameplay_state_create() {
return (game_state_t) {
.name = strid_get_strid(SCOPE_NAME),
.data = NULL,
.on_enter = _gameplay_state_enter,
.on_update = _gameplay_state_update,
.on_exit = _gameplay_state_exit
};
}

void draw_pipes(gameplay_state_t* gameplay, game_t* game, update_context_t ctx) {
const int max_pipe_count = STACKARRAY_SIZE(gameplay->pipe_window_Ys);
for (int i = max_pipe_count - 1; i >= 0; i--) {
if (!gameplay->pipe_window_Ys[i])
continue;

// float pipe_y =
// game->canvas.texture.width
// - (game->canvas.texture.width / (float)max_pipe_count * i);

// NOTE: max_pipe_count = (int)(game->canvas.texture.width / space_between_pipes)
}
}

void draw_base(gameplay_state_t* gameplay, game_t* game, update_context_t ctx) {
// FIXME: flickering

float shift = (GetTime() - (int)GetTime()) * pipe_speed;
for (int i = 0; gameplay->base.width * i - shift <= game->canvas.texture.width; i++) {
DrawTexture(
gameplay->base,
gameplay->base.width * i - shift,
game->canvas.texture.height - gameplay->base.height,
WHITE
);
}
}
10 changes: 10 additions & 0 deletions src/game/states/gameplay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef GAME_STATES_GAMEPLAY_H
#define GAME_STATES_GAMEPLAY_H


typedef struct game_state_s game_state_t;

game_state_t gameplay_state_create();


#endif
32 changes: 17 additions & 15 deletions src/game/states/intro.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <game/states/intro.h>
#include "game/states/intro.h"

#include <assert.h>
#include <math.h>
Expand All @@ -10,7 +10,7 @@
#include "system/logging.h"

#include "game/game.h"
#include "game/states/placeholder.h"
#include "game/states/gameplay.h"


typedef struct intro_state_s {
Expand All @@ -30,21 +30,23 @@ static void _intro_state_enter(game_state_t* state, game_t* game) {
static void _intro_state_update(game_state_t* state, game_t* game, update_context_t ctx) {
intro_state_t* intro = (intro_state_t*)state->data;

float progress = (GetTime() - intro->start_time) / 1.5;
if (GetKeyPressed() != 0 || progress >= 1)
game_switch_state(game, placeholder_state_create());
// FIXME: font and rectangle size

// float progress = (GetTime() - intro->start_time) / 1.5;
// if (GetKeyPressed() != 0 || progress >= 1)
game_switch_state(game, gameplay_state_create());

int size = 40;
Rectangle rect = (Rectangle) {
.x = (game->canvas.texture.width - size) / 2.0f,
.y = (game->canvas.texture.height - size) / 2.0f,
.width = size,
.height = size
};
// int size = 40;
// Rectangle rect = (Rectangle) {
// .x = (game->canvas.texture.width - size) / 2.0f,
// .y = (game->canvas.texture.height - size) / 2.0f,
// .width = size,
// .height = size
// };

DrawText("Made with", 0, (game->canvas.texture.height) / 2 - 9, 8, WHITE);
DrawRectangleLinesEx(rect, 2, WHITE);
DrawText(TextSubtext("raylib", 0, 6 * fmin(1, progress / 0.15)), rect.x + 8, rect.y + 27, 8, WHITE);
// DrawText("Made with", 0, (game->canvas.texture.height) / 2 - 9, 8, WHITE);
// DrawRectangleLinesEx(rect, 2, WHITE);
// DrawText(TextSubtext("raylib", 0, 6 * fmin(1, progress / 0.15)), rect.x + 8, rect.y + 27, 8, WHITE);
}

static void _intro_state_exit(game_state_t* state, game_t* game) {
Expand Down
46 changes: 0 additions & 46 deletions src/game/states/placeholder.c

This file was deleted.

10 changes: 0 additions & 10 deletions src/game/states/placeholder.h

This file was deleted.

0 comments on commit e5031af

Please sign in to comment.