cleanup
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <stb_ds.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "player.h"
|
||||
#include "raymath.h"
|
||||
#include "sprite.h"
|
||||
#include "texture_manager.h"
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "game_defs.h"
|
||||
#include "arena.h"
|
||||
#include "raylib.h"
|
||||
#include "sprite.h"
|
||||
#include "texture_manager.h"
|
||||
|
||||
// forward decls
|
||||
typedef struct Player Player;
|
||||
typedef struct Sprite Sprite;
|
||||
|
||||
#define ENTITY_INITAL_CAP 128
|
||||
|
||||
#define ENEMY_SPEED 20
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "sprite.h"
|
||||
#include "utils/defs.h"
|
||||
|
||||
#define GAME_NAME "Colossus March"
|
||||
@@ -16,9 +15,3 @@ typedef struct GameContext {
|
||||
} GameContext;
|
||||
|
||||
extern GameContext g_ctx;
|
||||
|
||||
typedef struct Player {
|
||||
Sprite* sprite;
|
||||
Vector2 pos;
|
||||
float walk_speed;
|
||||
} Player;
|
||||
|
||||
9
src/player.h
Normal file
9
src/player.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "sprite.h"
|
||||
|
||||
typedef struct Player {
|
||||
Sprite* sprite;
|
||||
Vector2 pos;
|
||||
float walk_speed;
|
||||
} Player;
|
||||
@@ -5,10 +5,11 @@
|
||||
#include "menu.h"
|
||||
|
||||
static Screen active_screen = Game;
|
||||
static Screen pending_screen = -1;
|
||||
|
||||
void screen_switch_to(Screen screen)
|
||||
static void switch_screen()
|
||||
{
|
||||
if (screen != active_screen) {
|
||||
if (pending_screen != -1 && active_screen != pending_screen) {
|
||||
switch (active_screen) {
|
||||
case Intro:
|
||||
intro_screen_cleanup();
|
||||
@@ -21,7 +22,8 @@ void screen_switch_to(Screen screen)
|
||||
break;
|
||||
}
|
||||
|
||||
active_screen = screen;
|
||||
active_screen = pending_screen;
|
||||
pending_screen = -1;
|
||||
|
||||
switch (active_screen) {
|
||||
case Intro:
|
||||
@@ -34,6 +36,11 @@ void screen_switch_to(Screen screen)
|
||||
}
|
||||
}
|
||||
|
||||
void screen_request_switch(Screen screen)
|
||||
{
|
||||
pending_screen = screen;
|
||||
}
|
||||
|
||||
void screen_init()
|
||||
{
|
||||
switch (active_screen) {
|
||||
@@ -51,6 +58,7 @@ void screen_init()
|
||||
|
||||
void screen_update()
|
||||
{
|
||||
switch_screen();
|
||||
switch (active_screen) {
|
||||
case Intro: {
|
||||
intro_screen_update();
|
||||
|
||||
@@ -6,7 +6,7 @@ typedef enum Screen {
|
||||
Game
|
||||
} Screen;
|
||||
|
||||
void screen_switch_to(Screen screen);
|
||||
void screen_request_switch(Screen screen);
|
||||
void screen_init();
|
||||
void screen_update();
|
||||
void screen_draw();
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
|
||||
#include "entity.h"
|
||||
#include "game_defs.h"
|
||||
#include "pool.h"
|
||||
#include "player.h"
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
#include "ring_buffer.h"
|
||||
#include "screens.h"
|
||||
#include "sprite.h"
|
||||
#include "texture_manager.h"
|
||||
@@ -31,7 +32,7 @@ static bool game_over = false;
|
||||
static Arena screen_arena;
|
||||
static Arena frame_arena;
|
||||
|
||||
static Pool bullet_pool;
|
||||
static RingBuffer bullet_pool;
|
||||
static Texture2D bullet_texture;
|
||||
|
||||
void update_camera()
|
||||
@@ -67,7 +68,7 @@ void process_input(float dt)
|
||||
|
||||
if (IsKeyPressed(KEY_SPACE)) {
|
||||
Bullet b = {.sprite = new_bullet(), .pos = player.pos};
|
||||
pool_push(&bullet_pool, &b);
|
||||
ring_buffer_push(&bullet_pool, &b);
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KEY_P)) {
|
||||
@@ -75,7 +76,7 @@ void process_input(float dt)
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KEY_F10)) {
|
||||
screen_switch_to(Menu);
|
||||
screen_request_switch(Menu);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +85,7 @@ void game_screen_init()
|
||||
screen_arena = arena_init(MB(5));
|
||||
frame_arena = arena_init(KB(10));
|
||||
|
||||
bullet_pool = pool_init(sizeof(Bullet), 10, &screen_arena);
|
||||
bullet_pool = ring_buffer_init(sizeof(Bullet), 10, &screen_arena);
|
||||
|
||||
tm = texture_manager_init();
|
||||
em = entity_manager_init();
|
||||
@@ -128,6 +129,9 @@ void game_screen_init()
|
||||
|
||||
void game_screen_update()
|
||||
{
|
||||
// cleaning frame dependent memory
|
||||
arena_reset(&frame_arena);
|
||||
|
||||
if (game_over) {
|
||||
if (IsKeyPressed(KEY_SPACE)) {
|
||||
game_over = false;
|
||||
@@ -167,18 +171,16 @@ void game_screen_draw()
|
||||
|
||||
// Draw Bullets
|
||||
for (size i = 0; i < bullet_pool.len; i++) {
|
||||
Bullet* b = pool_get(&bullet_pool, i);
|
||||
Bullet* b = ring_buffer_get(&bullet_pool, i);
|
||||
sprite_draw(b->sprite, b->pos);
|
||||
}
|
||||
|
||||
EndMode2D();
|
||||
|
||||
// cleaning frame dependent memory
|
||||
arena_reset(&frame_arena);
|
||||
}
|
||||
|
||||
void game_screen_cleanup()
|
||||
{
|
||||
texture_manager_free(&tm);
|
||||
arena_reset(&screen_arena);
|
||||
texture_manager_destroy(&tm);
|
||||
entity_manager_destroy(&em);
|
||||
arena_destroy(&screen_arena);
|
||||
};
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
void intro_screen_update()
|
||||
{
|
||||
if (IsKeyPressed(KEY_ENTER)) {
|
||||
screen_switch_to(Menu);
|
||||
screen_request_switch(Menu);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,14 +14,14 @@ typedef enum Option {
|
||||
} Option;
|
||||
|
||||
static const Option menu_options[] = {Start, Settings, Quit};
|
||||
size_t selected = Start;
|
||||
i8 selected = Start;
|
||||
|
||||
void menu_screen_update()
|
||||
{
|
||||
if (IsKeyPressed(KEY_ENTER)) {
|
||||
switch (menu_options[selected]) {
|
||||
case Start:
|
||||
screen_switch_to(Game);
|
||||
screen_request_switch(Game);
|
||||
break;
|
||||
case Settings:
|
||||
break;
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "arena.h"
|
||||
#include "defs.h"
|
||||
|
||||
typedef struct Pool {
|
||||
u8* buf;
|
||||
size item_size;
|
||||
size cap;
|
||||
size offset;
|
||||
size len;
|
||||
} Pool;
|
||||
|
||||
Pool pool_init(size item_size, size cap, Arena* a);
|
||||
void pool_push(Pool* p, void* item);
|
||||
void* pool_get(Pool* p, size index);
|
||||
@@ -1,25 +1,25 @@
|
||||
#include "pool.h"
|
||||
#include "ring_buffer.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "arena.h"
|
||||
|
||||
Pool pool_init(size item_size, size cap, Arena* a)
|
||||
RingBuffer ring_buffer_init(size item_size, size cap, Arena* a)
|
||||
{
|
||||
return (Pool){.buf = arena_alloc(a, item_size * cap, sizeof(u8)),
|
||||
return (RingBuffer){.buf = arena_alloc(a, item_size * cap, sizeof(u8)),
|
||||
.item_size = item_size,
|
||||
.cap = cap};
|
||||
}
|
||||
|
||||
void pool_push(Pool* p, void* item)
|
||||
void ring_buffer_push(RingBuffer* p, void* item)
|
||||
{
|
||||
memcpy(p->buf + (p->offset * p->item_size), item, p->item_size);
|
||||
p->offset = (p->offset + 1) % p->cap;
|
||||
if (p->len < p->cap) p->len++;
|
||||
}
|
||||
|
||||
void* pool_get(Pool* p, size index)
|
||||
void* ring_buffer_get(RingBuffer* p, size index)
|
||||
{
|
||||
assert(index < p->len && "index too big");
|
||||
size actual = (p->offset - p->len + index + p->cap) % p->cap;
|
||||
16
src/utils/ring_buffer.h
Normal file
16
src/utils/ring_buffer.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "arena.h"
|
||||
#include "defs.h"
|
||||
|
||||
typedef struct RingBuffer {
|
||||
u8* buf;
|
||||
size item_size;
|
||||
size cap;
|
||||
size offset;
|
||||
size len;
|
||||
} RingBuffer;
|
||||
|
||||
RingBuffer ring_buffer_init(size item_size, size cap, Arena* a);
|
||||
void ring_buffer_push(RingBuffer* self, void* item);
|
||||
void* ring_buffer_get(RingBuffer* self, size index);
|
||||
@@ -53,7 +53,7 @@ Texture2D texture_manager_load_texture_from_image(TextureManager* tm,
|
||||
return tm->textures[texture_idx];
|
||||
}
|
||||
|
||||
void texture_manager_free(TextureManager* tm)
|
||||
void texture_manager_destroy(TextureManager* tm)
|
||||
{
|
||||
for (size i = 0; i < arrlen(tm->textures); i++) {
|
||||
UnloadTexture(tm->textures[i]);
|
||||
|
||||
@@ -19,4 +19,4 @@ Texture2D texture_manager_load_texture(TextureManager* tm, const char* path);
|
||||
Texture2D texture_manager_load_texture_from_image(TextureManager* tm,
|
||||
const char* name,
|
||||
Image image);
|
||||
void texture_manager_free(TextureManager* tm);
|
||||
void texture_manager_destroy(TextureManager* tm);
|
||||
|
||||
Reference in New Issue
Block a user