bullets
This commit is contained in:
BIN
assets/bullet.aseprite
Normal file
BIN
assets/bullet.aseprite
Normal file
Binary file not shown.
BIN
assets/bullet.png
Normal file
BIN
assets/bullet.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 168 B |
@@ -1,31 +1,41 @@
|
||||
[
|
||||
{
|
||||
"directory": "/home/jens/Development/cex/colossus_march",
|
||||
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-O0", "-std=c23", "-Isrc", "-Isrc/screens", "-I", "/home/jens/.xmake/packages/r/raylib/5.5/efaf75c5cc7042868d9f9fc4196a080a/include", "-I", "/home/jens/.xmake/packages/b/box2d/v3.1.1/2aecaa50772341c7b9eb7c79ee9859aa/include", "-o", "build/.objs/colossus_march/linux/x86_64/debug/src/screens/intro.c.o", "src/screens/intro.c"],
|
||||
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-O0", "-std=c23", "-Isrc", "-Isrc/screens", "-Isrc/utils", "-I", "/home/jens/.xmake/packages/r/raylib/5.5/efaf75c5cc7042868d9f9fc4196a080a/include", "-I", "/home/jens/.xmake/packages/b/box2d/v3.1.1/2aecaa50772341c7b9eb7c79ee9859aa/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include/stb", "-o", "build/.objs/colossus_march/linux/x86_64/debug/src/screens/intro.c.o", "src/screens/intro.c"],
|
||||
"file": "src/screens/intro.c"
|
||||
},
|
||||
{
|
||||
"directory": "/home/jens/Development/cex/colossus_march",
|
||||
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-O0", "-std=c23", "-Isrc", "-Isrc/screens", "-I", "/home/jens/.xmake/packages/r/raylib/5.5/efaf75c5cc7042868d9f9fc4196a080a/include", "-I", "/home/jens/.xmake/packages/b/box2d/v3.1.1/2aecaa50772341c7b9eb7c79ee9859aa/include", "-o", "build/.objs/colossus_march/linux/x86_64/debug/src/screens/menu.c.o", "src/screens/menu.c"],
|
||||
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-O0", "-std=c23", "-Isrc", "-Isrc/screens", "-Isrc/utils", "-I", "/home/jens/.xmake/packages/r/raylib/5.5/efaf75c5cc7042868d9f9fc4196a080a/include", "-I", "/home/jens/.xmake/packages/b/box2d/v3.1.1/2aecaa50772341c7b9eb7c79ee9859aa/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include/stb", "-o", "build/.objs/colossus_march/linux/x86_64/debug/src/screens/menu.c.o", "src/screens/menu.c"],
|
||||
"file": "src/screens/menu.c"
|
||||
},
|
||||
{
|
||||
"directory": "/home/jens/Development/cex/colossus_march",
|
||||
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-O0", "-std=c23", "-Isrc", "-Isrc/screens", "-I", "/home/jens/.xmake/packages/r/raylib/5.5/efaf75c5cc7042868d9f9fc4196a080a/include", "-I", "/home/jens/.xmake/packages/b/box2d/v3.1.1/2aecaa50772341c7b9eb7c79ee9859aa/include", "-o", "build/.objs/colossus_march/linux/x86_64/debug/src/screens/game.c.o", "src/screens/game.c"],
|
||||
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-O0", "-std=c23", "-Isrc", "-Isrc/screens", "-Isrc/utils", "-I", "/home/jens/.xmake/packages/r/raylib/5.5/efaf75c5cc7042868d9f9fc4196a080a/include", "-I", "/home/jens/.xmake/packages/b/box2d/v3.1.1/2aecaa50772341c7b9eb7c79ee9859aa/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include/stb", "-o", "build/.objs/colossus_march/linux/x86_64/debug/src/screens/game.c.o", "src/screens/game.c"],
|
||||
"file": "src/screens/game.c"
|
||||
},
|
||||
{
|
||||
"directory": "/home/jens/Development/cex/colossus_march",
|
||||
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-O0", "-std=c23", "-Isrc", "-Isrc/screens", "-I", "/home/jens/.xmake/packages/r/raylib/5.5/efaf75c5cc7042868d9f9fc4196a080a/include", "-I", "/home/jens/.xmake/packages/b/box2d/v3.1.1/2aecaa50772341c7b9eb7c79ee9859aa/include", "-o", "build/.objs/colossus_march/linux/x86_64/debug/src/sprite.c.o", "src/sprite.c"],
|
||||
"file": "src/sprite.c"
|
||||
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-O0", "-std=c23", "-Isrc", "-Isrc/screens", "-Isrc/utils", "-I", "/home/jens/.xmake/packages/r/raylib/5.5/efaf75c5cc7042868d9f9fc4196a080a/include", "-I", "/home/jens/.xmake/packages/b/box2d/v3.1.1/2aecaa50772341c7b9eb7c79ee9859aa/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include/stb", "-o", "build/.objs/colossus_march/linux/x86_64/debug/src/screens.c.o", "src/screens.c"],
|
||||
"file": "src/screens.c"
|
||||
},
|
||||
{
|
||||
"directory": "/home/jens/Development/cex/colossus_march",
|
||||
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-O0", "-std=c23", "-Isrc", "-Isrc/screens", "-I", "/home/jens/.xmake/packages/r/raylib/5.5/efaf75c5cc7042868d9f9fc4196a080a/include", "-I", "/home/jens/.xmake/packages/b/box2d/v3.1.1/2aecaa50772341c7b9eb7c79ee9859aa/include", "-o", "build/.objs/colossus_march/linux/x86_64/debug/src/main.c.o", "src/main.c"],
|
||||
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-O0", "-std=c23", "-Isrc", "-Isrc/screens", "-Isrc/utils", "-I", "/home/jens/.xmake/packages/r/raylib/5.5/efaf75c5cc7042868d9f9fc4196a080a/include", "-I", "/home/jens/.xmake/packages/b/box2d/v3.1.1/2aecaa50772341c7b9eb7c79ee9859aa/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include/stb", "-o", "build/.objs/colossus_march/linux/x86_64/debug/src/utils/arena.c.o", "src/utils/arena.c"],
|
||||
"file": "src/utils/arena.c"
|
||||
},
|
||||
{
|
||||
"directory": "/home/jens/Development/cex/colossus_march",
|
||||
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-O0", "-std=c23", "-Isrc", "-Isrc/screens", "-Isrc/utils", "-I", "/home/jens/.xmake/packages/r/raylib/5.5/efaf75c5cc7042868d9f9fc4196a080a/include", "-I", "/home/jens/.xmake/packages/b/box2d/v3.1.1/2aecaa50772341c7b9eb7c79ee9859aa/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include/stb", "-o", "build/.objs/colossus_march/linux/x86_64/debug/src/utils/texture_manager.c.o", "src/utils/texture_manager.c"],
|
||||
"file": "src/utils/texture_manager.c"
|
||||
},
|
||||
{
|
||||
"directory": "/home/jens/Development/cex/colossus_march",
|
||||
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-O0", "-std=c23", "-Isrc", "-Isrc/screens", "-Isrc/utils", "-I", "/home/jens/.xmake/packages/r/raylib/5.5/efaf75c5cc7042868d9f9fc4196a080a/include", "-I", "/home/jens/.xmake/packages/b/box2d/v3.1.1/2aecaa50772341c7b9eb7c79ee9859aa/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include/stb", "-o", "build/.objs/colossus_march/linux/x86_64/debug/src/main.c.o", "src/main.c"],
|
||||
"file": "src/main.c"
|
||||
},
|
||||
{
|
||||
"directory": "/home/jens/Development/cex/colossus_march",
|
||||
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-O0", "-std=c23", "-Isrc", "-Isrc/screens", "-I", "/home/jens/.xmake/packages/r/raylib/5.5/efaf75c5cc7042868d9f9fc4196a080a/include", "-I", "/home/jens/.xmake/packages/b/box2d/v3.1.1/2aecaa50772341c7b9eb7c79ee9859aa/include", "-o", "build/.objs/colossus_march/linux/x86_64/debug/src/screens.c.o", "src/screens.c"],
|
||||
"file": "src/screens.c"
|
||||
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-O0", "-std=c23", "-Isrc", "-Isrc/screens", "-Isrc/utils", "-I", "/home/jens/.xmake/packages/r/raylib/5.5/efaf75c5cc7042868d9f9fc4196a080a/include", "-I", "/home/jens/.xmake/packages/b/box2d/v3.1.1/2aecaa50772341c7b9eb7c79ee9859aa/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include", "-I", "/home/jens/.xmake/packages/s/stb/2025.03.14/67005fc11486400896028eb775a57270/include/stb", "-o", "build/.objs/colossus_march/linux/x86_64/debug/src/sprite.c.o", "src/sprite.c"],
|
||||
"file": "src/sprite.c"
|
||||
}]
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 94 KiB After Width: | Height: | Size: 227 KiB |
16
src/game_defs.h
Normal file
16
src/game_defs.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "utils/defs.h"
|
||||
#define GAME_NAME "Colossus March"
|
||||
|
||||
#define BG CLITERAL(Color){243, 223, 162, 255}
|
||||
#define BALL CLITERAL(Color){126, 189, 194, 255}
|
||||
#define BLOCK CLITERAL(Color){187, 68, 48, 255}
|
||||
|
||||
typedef struct GameContext {
|
||||
size screen_width;
|
||||
size screen_height;
|
||||
bool exit;
|
||||
} GameContext;
|
||||
|
||||
extern GameContext g_ctx;
|
||||
16
src/main.c
16
src/main.c
@@ -1,19 +1,31 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "defs.h"
|
||||
#include "game_defs.h"
|
||||
#include "raylib.h"
|
||||
#include "screens.h"
|
||||
|
||||
GameContext g_ctx = {0};
|
||||
|
||||
void update_game_context()
|
||||
{
|
||||
if (IsWindowResized()) {
|
||||
g_ctx.screen_width = GetScreenWidth();
|
||||
g_ctx.screen_height = GetScreenHeight();
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
||||
InitWindow(800, 600, "flappu");
|
||||
SetTargetFPS(144);
|
||||
SetExitKey(KEY_F12);
|
||||
SetExitKey(KEY_F4);
|
||||
|
||||
update_game_context();
|
||||
screen_init();
|
||||
|
||||
while (!WindowShouldClose() && !g_ctx.exit) {
|
||||
update_game_context();
|
||||
screen_update();
|
||||
|
||||
BeginDrawing();
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "intro.h"
|
||||
#include "menu.h"
|
||||
|
||||
static Screen active_screen = Intro;
|
||||
static Screen active_screen = Game;
|
||||
|
||||
void screen_switch_to(Screen screen)
|
||||
{
|
||||
@@ -34,6 +34,21 @@ void screen_switch_to(Screen screen)
|
||||
}
|
||||
}
|
||||
|
||||
void screen_init()
|
||||
{
|
||||
switch (active_screen) {
|
||||
case Intro: {
|
||||
// intro_screen_init();
|
||||
} break;
|
||||
case Menu: {
|
||||
// menu_screen_init();
|
||||
} break;
|
||||
case Game: {
|
||||
game_screen_init();
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
void screen_update()
|
||||
{
|
||||
switch (active_screen) {
|
||||
|
||||
@@ -7,5 +7,6 @@ typedef enum Screen {
|
||||
} Screen;
|
||||
|
||||
void screen_switch_to(Screen screen);
|
||||
void screen_init();
|
||||
void screen_update();
|
||||
void screen_draw();
|
||||
|
||||
@@ -3,19 +3,71 @@
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "game_defs.h"
|
||||
#include "pool.h"
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
#include "screens.h"
|
||||
#include "sprite.h"
|
||||
#include "texture_manager.h"
|
||||
#include "utils/arena.h"
|
||||
|
||||
// TODO: remove
|
||||
typedef struct Bullet {
|
||||
Sprite* sprite;
|
||||
Vector2 pos;
|
||||
} Bullet;
|
||||
|
||||
static Player player;
|
||||
static Camera2D cam;
|
||||
static Texture2D noise_terrain_texture;
|
||||
static TextureManager tm;
|
||||
|
||||
static bool paused = false;
|
||||
static bool game_over = false;
|
||||
|
||||
void process_input()
|
||||
static Arena screen_arena;
|
||||
static Arena frame_arena;
|
||||
|
||||
static Pool bullet_pool;
|
||||
static Texture2D bullet_texture;
|
||||
|
||||
void update_camera()
|
||||
{
|
||||
if (IsWindowResized()) {
|
||||
cam.offset =
|
||||
(Vector2){g_ctx.screen_width / 2.0f, g_ctx.screen_height / 2.0f};
|
||||
}
|
||||
|
||||
cam.target = Vector2Lerp(cam.target, player.pos, 0.1);
|
||||
}
|
||||
|
||||
Sprite* new_bullet()
|
||||
{
|
||||
return sprite_from_texture(bullet_texture, NULL, NULL, &RED, &screen_arena);
|
||||
}
|
||||
|
||||
void process_input(float dt)
|
||||
{
|
||||
// Movement
|
||||
if (IsKeyDown(KEY_W)) {
|
||||
player.pos.y -= player.walk_speed * dt;
|
||||
}
|
||||
if (IsKeyDown(KEY_S)) {
|
||||
player.pos.y += player.walk_speed * dt;
|
||||
}
|
||||
if (IsKeyDown(KEY_A)) {
|
||||
player.pos.x -= player.walk_speed * dt;
|
||||
}
|
||||
if (IsKeyDown(KEY_D)) {
|
||||
player.pos.x += player.walk_speed * dt;
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KEY_SPACE)) {
|
||||
Bullet b = {.sprite = new_bullet(), .pos = player.pos};
|
||||
pool_push(&bullet_pool, &b);
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KEY_P)) {
|
||||
paused = true;
|
||||
}
|
||||
@@ -27,24 +79,38 @@ void process_input()
|
||||
|
||||
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);
|
||||
|
||||
tm = tm_init();
|
||||
bullet_texture = tm_load_texture(&tm, "assets/bullet.png");
|
||||
|
||||
paused = 0;
|
||||
game_over = 0;
|
||||
|
||||
Image noise_image = GenImagePerlinNoise(2000, 2000, 0, 0, 2);
|
||||
noise_terrain_texture = tm_load_texture_from_image(&tm, noise_image);
|
||||
|
||||
Sprite* player_sprite = sprite_new("assets/ball.png",
|
||||
&(Vector2){100, 400},
|
||||
NULL,
|
||||
&(Rectangle){0, 0, 32, 32},
|
||||
&BALL);
|
||||
&BALL,
|
||||
&screen_arena,
|
||||
&tm);
|
||||
|
||||
Animation* anim = malloc(sizeof(Animation));
|
||||
if (!anim) abort();
|
||||
Animation* anim =
|
||||
arena_alloc(&screen_arena, sizeof(Animation), sizeof(Animation));
|
||||
|
||||
*anim = (Animation){
|
||||
.current_frame = 0, .frame_count = 3, .current_time = 0, .fps = 4};
|
||||
player_sprite->animation = anim;
|
||||
player = (Player){player_sprite};
|
||||
player.walk_speed = 100;
|
||||
|
||||
cam = (Camera2D){{200, GetScreenHeight() / 2.0f}, player.sprite->pos, 0, 2};
|
||||
cam = (Camera2D){
|
||||
{GetScreenWidth() / 2.0f, GetScreenHeight() / 2.0f}, player.pos, 0, 2};
|
||||
}
|
||||
|
||||
void game_screen_update()
|
||||
@@ -60,30 +126,41 @@ void game_screen_update()
|
||||
|
||||
if (paused) return;
|
||||
|
||||
update_camera();
|
||||
|
||||
float dt = GetFrameTime();
|
||||
|
||||
process_input();
|
||||
|
||||
// Update Camera
|
||||
cam.target = Vector2Lerp(cam.target, player.sprite->pos, 0.1);
|
||||
process_input(dt);
|
||||
|
||||
// Update Animation
|
||||
// TODO: make it scalable
|
||||
animation_update(player.sprite->animation);
|
||||
animation_update(player.sprite->animation, dt);
|
||||
};
|
||||
|
||||
void game_screen_draw()
|
||||
{
|
||||
BeginMode2D(cam);
|
||||
|
||||
// Draw Stuff
|
||||
sprite_draw(player.sprite);
|
||||
// Draw Terrain
|
||||
DrawTexture(noise_terrain_texture, -400, -400, WHITE);
|
||||
|
||||
// Draw player
|
||||
sprite_draw(player.sprite, player.pos);
|
||||
|
||||
// Draw Bullets
|
||||
for (size i = 0; i < bullet_pool.len; i++) {
|
||||
Bullet* b = pool_get(&bullet_pool, i);
|
||||
sprite_draw(b->sprite, b->pos);
|
||||
}
|
||||
|
||||
EndMode2D();
|
||||
|
||||
// cleaning frame dependent memory
|
||||
arena_reset(&frame_arena);
|
||||
}
|
||||
|
||||
void game_screen_cleanup()
|
||||
{
|
||||
// free player
|
||||
sprite_free(player.sprite);
|
||||
tm_free(&tm);
|
||||
arena_reset(&screen_arena);
|
||||
};
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
typedef struct Player {
|
||||
Sprite* sprite;
|
||||
Vector2 pos;
|
||||
float walk_speed;
|
||||
} Player;
|
||||
|
||||
void game_screen_init();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "intro.h"
|
||||
|
||||
#include "defs.h"
|
||||
#include "game_defs.h"
|
||||
#include "raylib.h"
|
||||
#include "screens.h"
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "defs.h"
|
||||
#include "game_defs.h"
|
||||
#include "raylib.h"
|
||||
#include "screens.h"
|
||||
|
||||
|
||||
54
src/sprite.c
54
src/sprite.c
@@ -1,33 +1,21 @@
|
||||
#include "sprite.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
|
||||
void* xmalloc(size_t size)
|
||||
Sprite* sprite_from_texture(Texture2D tex,
|
||||
Vector2* anchor,
|
||||
Rectangle* source,
|
||||
Color* color,
|
||||
Arena* arena)
|
||||
{
|
||||
void* p = malloc(size);
|
||||
if (!p) abort();
|
||||
return p;
|
||||
}
|
||||
|
||||
Sprite* sprite_new(const char* path,
|
||||
Vector2* pos,
|
||||
Vector2* anchor,
|
||||
Rectangle* source,
|
||||
Color* color)
|
||||
{
|
||||
assert(path && "path must not be empty");
|
||||
|
||||
Sprite* sprite = xmalloc(sizeof(Sprite));
|
||||
Sprite* sprite = arena_alloc(arena, sizeof(Sprite), sizeof(Sprite));
|
||||
if (!sprite) abort();
|
||||
|
||||
sprite->tex = LoadTexture(path);
|
||||
|
||||
sprite->pos = pos != NULL ? *pos : (Vector2){0, 0};
|
||||
sprite->tex = tex;
|
||||
|
||||
sprite->source =
|
||||
source != NULL
|
||||
@@ -45,7 +33,20 @@ Sprite* sprite_new(const char* path,
|
||||
return sprite;
|
||||
}
|
||||
|
||||
void sprite_draw(Sprite* s)
|
||||
Sprite* sprite_new(const char* path,
|
||||
Vector2* anchor,
|
||||
Rectangle* source,
|
||||
Color* color,
|
||||
Arena* arena,
|
||||
TextureManager* tm)
|
||||
{
|
||||
assert(path && "path must not be empty");
|
||||
|
||||
Texture2D tex = tm_load_texture(tm, path);
|
||||
return sprite_from_texture(tex, anchor, source, color, arena);
|
||||
}
|
||||
|
||||
void sprite_draw(Sprite* s, Vector2 pos)
|
||||
{
|
||||
Rectangle source = s->source;
|
||||
|
||||
@@ -54,23 +55,14 @@ void sprite_draw(Sprite* s)
|
||||
source.x = source.width * (float)s->animation->current_frame;
|
||||
}
|
||||
|
||||
DrawTextureRec(
|
||||
s->tex, source, Vector2Subtract(s->pos, s->anchor), s->color);
|
||||
}
|
||||
|
||||
void sprite_free(Sprite* s)
|
||||
{
|
||||
if (s->animation) free(s->animation);
|
||||
UnloadTexture(s->tex);
|
||||
free(s);
|
||||
DrawTextureRec(s->tex, source, Vector2Subtract(pos, s->anchor), s->color);
|
||||
}
|
||||
|
||||
//
|
||||
// Animation System
|
||||
//
|
||||
void animation_update(Animation* a)
|
||||
void animation_update(Animation* a, float dt)
|
||||
{
|
||||
float dt = GetFrameTime();
|
||||
a->current_time += dt;
|
||||
|
||||
if (a->current_time >= 1.0f / a->fps) {
|
||||
|
||||
20
src/sprite.h
20
src/sprite.h
@@ -1,7 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "defs.h"
|
||||
#include "raylib.h"
|
||||
#include "texture_manager.h"
|
||||
#include "utils/arena.h"
|
||||
#include "utils/defs.h"
|
||||
|
||||
typedef struct Animation {
|
||||
u8 frame_count;
|
||||
@@ -12,21 +14,27 @@ typedef struct Animation {
|
||||
|
||||
typedef struct Sprite {
|
||||
Texture2D tex;
|
||||
Vector2 pos;
|
||||
Vector2 anchor;
|
||||
Rectangle source;
|
||||
Color color;
|
||||
Animation* animation;
|
||||
} Sprite;
|
||||
|
||||
Sprite* sprite_from_texture(Texture2D tex,
|
||||
Vector2* anchor,
|
||||
Rectangle* source,
|
||||
Color* color,
|
||||
Arena* arena);
|
||||
|
||||
Sprite* sprite_new(const char* path,
|
||||
Vector2* pos,
|
||||
Vector2* anchor,
|
||||
Rectangle* source,
|
||||
Color* color);
|
||||
Color* color,
|
||||
Arena* arena,
|
||||
TextureManager* tm);
|
||||
|
||||
void sprite_draw(Sprite* sprite);
|
||||
void sprite_draw(Sprite* sprite, Vector2 pos);
|
||||
|
||||
void sprite_free(Sprite* sprite);
|
||||
|
||||
void animation_update(Animation* a);
|
||||
void animation_update(Animation* a, float dt);
|
||||
|
||||
3
src/stb_impl.c
Normal file
3
src/stb_impl.c
Normal file
@@ -0,0 +1,3 @@
|
||||
#define STB_DS_IMPLEMENTATION
|
||||
|
||||
#include "stb_ds.h"
|
||||
36
src/utils/arena.c
Normal file
36
src/utils/arena.c
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "arena.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Arena arena_init(size size)
|
||||
{
|
||||
Arena a = {0};
|
||||
a.buffer = malloc(size);
|
||||
if (!a.buffer) {
|
||||
abort();
|
||||
}
|
||||
a.size = size;
|
||||
return a;
|
||||
}
|
||||
|
||||
void* arena_alloc(Arena* a, size bytes, size align)
|
||||
{
|
||||
size aligned_offset = (a->offset + align - 1) & ~(align - 1);
|
||||
assert(aligned_offset + bytes <= a->size && "arena too small");
|
||||
|
||||
void* ptr = a->buffer + aligned_offset;
|
||||
a->offset = aligned_offset + bytes;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void arena_reset(Arena* a)
|
||||
{
|
||||
a->offset = 0;
|
||||
}
|
||||
|
||||
void arena_destroy(Arena* a)
|
||||
{
|
||||
free(a->buffer);
|
||||
}
|
||||
14
src/utils/arena.h
Normal file
14
src/utils/arena.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "defs.h"
|
||||
|
||||
typedef struct Arena {
|
||||
u8* buffer;
|
||||
size size;
|
||||
size offset;
|
||||
} Arena;
|
||||
|
||||
Arena arena_init(size size);
|
||||
void* arena_alloc(Arena* a, size bytes, size align);
|
||||
void arena_reset(Arena* a);
|
||||
void arena_destroy(Arena* a);
|
||||
@@ -3,14 +3,6 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#define GAME_NAME "Colossus March"
|
||||
|
||||
#define BG CLITERAL(Color){243, 223, 162, 255}
|
||||
#define BALL CLITERAL(Color){126, 189, 194, 255}
|
||||
#define BLOCK CLITERAL(Color){187, 68, 48, 255}
|
||||
|
||||
#define length(a) (sizeof(a) / sizeof(*a))
|
||||
|
||||
#define append(a, i) \
|
||||
@@ -31,8 +23,5 @@ typedef uint16_t u16;
|
||||
typedef uint32_t u32;
|
||||
typedef uint64_t u64;
|
||||
|
||||
typedef struct GameContext {
|
||||
bool exit;
|
||||
} GameContext;
|
||||
|
||||
extern GameContext g_ctx;
|
||||
#define KB(count) 1024 * (count)
|
||||
#define MB(count) 1024 * 1024 * (count)
|
||||
27
src/utils/pool.c
Normal file
27
src/utils/pool.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "pool.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "arena.h"
|
||||
|
||||
Pool pool_init(size item_size, size cap, Arena* a)
|
||||
{
|
||||
return (Pool){.buf = arena_alloc(a, item_size * cap, sizeof(u8)),
|
||||
.item_size = item_size,
|
||||
.cap = cap};
|
||||
}
|
||||
|
||||
void pool_push(Pool* 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)
|
||||
{
|
||||
assert(index < p->len && "index too big");
|
||||
size actual = (p->offset - p->len + index + p->cap) % p->cap;
|
||||
return p->buf + actual * p->item_size;
|
||||
}
|
||||
16
src/utils/pool.h
Normal file
16
src/utils/pool.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#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);
|
||||
43
src/utils/texture_manager.c
Normal file
43
src/utils/texture_manager.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "texture_manager.h"
|
||||
|
||||
#include <stb_ds.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "defs.h"
|
||||
#include "raylib.h"
|
||||
|
||||
TextureManager tm_init()
|
||||
{
|
||||
TextureManager tm = {0};
|
||||
tm.textures = NULL;
|
||||
|
||||
// setting default cap of textures
|
||||
arrsetcap(tm.textures, 64);
|
||||
|
||||
return tm;
|
||||
}
|
||||
|
||||
Texture2D tm_load_texture(TextureManager* tm, const char* path)
|
||||
{
|
||||
Texture2D tex = LoadTexture(path);
|
||||
arrpush(tm->textures, tex);
|
||||
|
||||
return tex;
|
||||
}
|
||||
|
||||
Texture2D tm_load_texture_from_image(TextureManager* tm, Image image)
|
||||
{
|
||||
Texture2D tex = LoadTextureFromImage(image);
|
||||
arrpush(tm->textures, tex);
|
||||
|
||||
return tex;
|
||||
}
|
||||
|
||||
void tm_free(TextureManager* tm)
|
||||
{
|
||||
for (size i = 0; i < arrlen(tm->textures); i++) {
|
||||
UnloadTexture(tm->textures[i]);
|
||||
}
|
||||
|
||||
arrfree(tm->textures);
|
||||
}
|
||||
13
src/utils/texture_manager.h
Normal file
13
src/utils/texture_manager.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
typedef struct TextureManager {
|
||||
Texture2D* textures;
|
||||
|
||||
} TextureManager;
|
||||
|
||||
TextureManager tm_init();
|
||||
Texture2D tm_load_texture(TextureManager* tm, const char* path);
|
||||
Texture2D tm_load_texture_from_image(TextureManager* tm, Image image);
|
||||
void tm_free(TextureManager* tm);
|
||||
@@ -1,12 +1,12 @@
|
||||
add_rules("mode.debug", "mode.release")
|
||||
|
||||
add_requires("raylib", "box2d")
|
||||
add_requires("raylib", "box2d", "stb")
|
||||
|
||||
target("colossus_march")
|
||||
set_languages("c23")
|
||||
set_kind("binary")
|
||||
add_files("src/**.c")
|
||||
add_packages("raylib", "box2d")
|
||||
add_packages("raylib", "box2d", "stb")
|
||||
add_includedirs("src", os.dirs("src/**"))
|
||||
set_rundir("$(projectdir)")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user