Restructured everything so hot reloading works

This commit is contained in:
Joseph Ferano 2025-10-04 18:18:42 +07:00
parent 40022bbdb4
commit fd4f8ffbb1
6 changed files with 211 additions and 194 deletions

1
.gitignore vendored
View File

@ -10,3 +10,4 @@
/game.fasl
/writeimage
*.fasl
*.so

View File

@ -11,10 +11,11 @@ main: main.cpp game.h Makefile
$(CC) $(CFLAGS) $(INCLUDES) main.cpp $(LDFLAGS) -o main
game.so: game.cpp game.h lib.h sprites.h game_data.h Makefile
$(CC) $(CFLAGS) $(INCLUDES) -shared -fPIC game.cpp -L./lib -lraylib -lm -o game.so
$(CC) $(CFLAGS) $(INCLUDES) -shared -fPIC game.cpp -L./lib -lraylib -lm -o game.so.tmp
mv game.so.tmp game.so
run: all
./main
LD_LIBRARY_PATH=lib ./main
clean:
rm -vf *.so *.o main

267
game.cpp
View File

@ -1,6 +1,8 @@
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <optional>
#include <iostream>
using namespace std;
@ -10,8 +12,7 @@ using namespace std;
#include "game.h"
#include "game_data.h"
// Assets Init() {
void Init() {
GameState *InitState() {
knight_anims[ANIM_KNIGHT_IDLE] = knight_idle;
knight_anims[ANIM_KNIGHT_RUN] = knight_run;
knight_anims[ANIM_KNIGHT_ATTACK_SIDE1] = knight_attack_side1;
@ -28,41 +29,56 @@ void Init() {
LoadTexture("./assets/Factions/Knights/Troops/Warrior/Blue/Warrior_Blue.png");
assets.textures[TEX_MOUSE_CURSOR] = LoadTexture("./assets/UI/Pointers/01.png");
assets.textures[TEX_TARGET_RETICLE] = LoadTexture("./assets/UI/Pointers/02.png");
// return assets;
auto game = new GameState;
game->assets = assets;
// TODO: Probably want to use idiomatic C++ here?
game->knights = (Knight*)calloc(MAX_KNIGHTS, sizeof(Knight));
game->anim_playbacks = (SpriteAnimationPlayback*)calloc(MAX_KNIGHTS, sizeof(SpriteAnimationPlayback));
const int entities = MAX_KNIGHTS;
for (int i = 0; i < entities; i++) {
f32 rand_x = GetRandomValue(165, 1130);
f32 rand_y = 100 + ((float)950 / (float)entities) * i;
game->knights[i].position = {rand_x,rand_y};
PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &game->anim_playbacks[i]);
int rand_frame = GetRandomValue(0, knight_anims[ANIM_KNIGHT_IDLE].total_frames);
game->anim_playbacks[i].current_frame = rand_frame;
}
game->entity_count = entities;
game->camera = Camera2D{.offset = {0, 0}, .target = {0, 0}, .rotation = 0.0f, .zoom = 1.0f};
return game;
}
void Update(GameState *game, Camera2D *cam, float dt) {
GameState *Init() {
SetTraceLogLevel(4);
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tiny Knights");
int monitor = GetCurrentMonitor();
int monitor_width = GetMonitorWidth(monitor);
int monitor_height = GetMonitorHeight(monitor);
int win_pos_x = monitor_width / 2 - SCREEN_WIDTH / 2;
int win_pos_y = monitor_height / 2 - SCREEN_HEIGHT / 2;
SetWindowPosition(win_pos_x, win_pos_y);
SetTargetFPS(TARGET_FPS);
HideCursor();
return InitState();
}
void Input(GameState *state) {
if (IsMouseButtonPressed(0)) {
game->selection_mouse_start_pos = optional<Point>{GetScreenToWorld2D(GetMousePosition(), *cam)};
}
if (game->selection_mouse_start_pos.has_value()) {
Point start_pos = game->selection_mouse_start_pos.value();
Point current_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
if (Vector2DistanceSqr(current_pos, start_pos) >= 100.0f) {
f32 width = current_pos.x - start_pos.x;
f32 height = current_pos.y - start_pos.y;
f32 x = width >= 0.0f ? start_pos.x : current_pos.x;
f32 y = height >= 0.0f ? start_pos.y : current_pos.y;
Rectangle rect = {x, y, fabs(width), fabs(height)};
for (int i = 0; i < game->entity_count; i++) {
Rectangle mouse_select_area = {
game->knights[i].position.x + knight_colrect_select.x,
game->knights[i].position.y + knight_colrect_select.y,
knight_colrect_select.width,
knight_colrect_select.height,
};
game->knights[i].selected =
CheckCollisionRecs(rect, mouse_select_area);
}
}
state->selection_mouse_start_pos = optional<Point>{GetScreenToWorld2D(GetMousePosition(), state->camera)};
}
if (IsMouseButtonReleased(0)) {
// TODO: This is what should happen when the distance is too small
// Point start_pos = game->selection_mouse_start_pos.some.point;
Point start_pos = game->selection_mouse_start_pos.value();
Point current_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
Point start_pos = state->selection_mouse_start_pos.value();
Point current_pos = GetScreenToWorld2D(GetMousePosition(), state->camera);
if (Vector2DistanceSqr(current_pos, start_pos) >= 100.0f) {
f32 width = current_pos.x - start_pos.x;
@ -70,72 +86,68 @@ void Update(GameState *game, Camera2D *cam, float dt) {
f32 x = width >= 0.0f ? start_pos.x : current_pos.x;
f32 y = height >= 0.0f ? start_pos.y : current_pos.y;
Rectangle rect = {x, y, fabs(width), fabs(height)};
for (int i = 0; i < game->entity_count; i++) {
for (int i = 0; i < state->entity_count; i++) {
Rectangle mouse_select_area = {
game->knights[i].position.x + knight_colrect_select.x,
game->knights[i].position.y + knight_colrect_select.y,
state->knights[i].position.x + knight_colrect_select.x,
state->knights[i].position.y + knight_colrect_select.y,
knight_colrect_select.width,
knight_colrect_select.height,
};
game->knights[i].selected =
state->knights[i].selected =
CheckCollisionRecs(rect, mouse_select_area);
}
} else {
// TODO: This kind of sucks that we're doing all the math here to calculate
// the position of the collider, we need a helper that calculates origin offset
int k_idx = -1;
for (int i = 0; i < game->entity_count; i++) {
for (int i = 0; i < state->entity_count; i++) {
Rectangle mouse_select_area = {
game->knights[i].position.x + knight_colrect_select.x,
game->knights[i].position.y + knight_colrect_select.y,
state->knights[i].position.x + knight_colrect_select.x,
state->knights[i].position.y + knight_colrect_select.y,
knight_colrect_select.width,
knight_colrect_select.height,
};
if (CheckCollisionPointRec(current_pos, mouse_select_area)) {
k_idx = i;
}
game->knights[i].selected = false;
state->knights[i].selected = false;
}
if (k_idx != -1) {
game->knights[k_idx].selected = true;
state->knights[k_idx].selected = true;
}
}
game->selection_mouse_start_pos = optional<Point>{};
state->selection_mouse_start_pos = optional<Point>{};
}
if (IsMouseButtonPressed(1)) {
Point target_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
Point target_pos = GetScreenToWorld2D(GetMousePosition(), state->camera);
bool any = false;
for (int i = 0; i < game->entity_count; i++) {
if (game->knights[i].selected) {
game->knights[i].move_target_point = target_pos;
game->knights[i].ordered_to_move = true;
for (int i = 0; i < state->entity_count; i++) {
if (state->knights[i].selected) {
state->knights[i].move_target_point = target_pos;
state->knights[i].ordered_to_move = true;
any = true;
}
}
if (any) {
game->selected_point = optional<Point>{target_pos};
state->selected_point = optional<Point>{target_pos};
}
}
const float cam_move_speed = 1050.0f * dt;
Vector2 cam_vel = {};
if (IsKeyDown(KEY_D)) {
cam_vel.x = -1;
f32 cam_speed = 5.0;
if (IsKeyDown(KEY_RIGHT)) {
state->camera.target.x += cam_speed;
}
if (IsKeyDown(KEY_A)) {
cam_vel.x = 1;
if (IsKeyDown(KEY_LEFT)) {
state->camera.target.x -= cam_speed;
}
if (IsKeyDown(KEY_W)) {
cam_vel.y = 1;
if (IsKeyDown(KEY_UP)) {
state->camera.target.y -= cam_speed;
}
if (IsKeyDown(KEY_S)) {
cam_vel.y = -1;
if (IsKeyDown(KEY_DOWN)) {
state->camera.target.y += cam_speed;
}
cam_vel = Vector2Normalize(cam_vel);
game->camera_position = Vector2Scale(cam_vel, cam_move_speed);
cam->offset = Vector2Add(cam->offset, game->camera_position);
// if (IsKeyPressed(KEY_SPACE) && game->selected_knight != -1) {
// if (IsKeyPressed(KEY_SPACE) && state->selected_knight != -1) {
// int k_idx = game->selected_knight;
// if (game->knights[k_idx].state != KNIGHT_ATTACKING) {
// game->knights[k_idx].state = KNIGHT_ATTACKING;
@ -145,14 +157,50 @@ void Update(GameState *game, Camera2D *cam, float dt) {
if (IsKeyPressed(KEY_F1)) {
global_debug_mode = !global_debug_mode;
}
if (IsKeyPressed(KEY_F2)) {
auto new_state = InitState();
*state = *new_state;
}
}
void Update(GameState *state) {
// cout << "Cool" << endl;
if (state->selection_mouse_start_pos.has_value()) {
Point start_pos = state->selection_mouse_start_pos.value();
Point current_pos = GetScreenToWorld2D(GetMousePosition(), state->camera);
if (Vector2DistanceSqr(current_pos, start_pos) >= 100.0f) {
f32 width = current_pos.x - start_pos.x;
f32 height = current_pos.y - start_pos.y;
f32 x = width >= 0.0f ? start_pos.x : current_pos.x;
f32 y = height >= 0.0f ? start_pos.y : current_pos.y;
Rectangle rect = {x, y, fabs(width), fabs(height)};
for (int i = 0; i < state->entity_count; i++) {
Rectangle mouse_select_area = {
state->knights[i].position.x + knight_colrect_select.x,
state->knights[i].position.y + knight_colrect_select.y,
knight_colrect_select.width,
knight_colrect_select.height,
};
state->knights[i].selected =
CheckCollisionRecs(rect, mouse_select_area);
}
}
}
float dt = 0.33;
const float cam_move_speed = 1050.0f * dt;
// TODO: This was moved out of Input
Vector2 cam_vel = {};
cam_vel = Vector2Normalize(cam_vel);
state->camera_position = Vector2Scale(cam_vel, cam_move_speed);
state->camera.offset = Vector2Add(state->camera.offset, state->camera_position);
// Process animation data
TickSpriteAnimations(game->anim_playbacks, game->entity_count);
TickSpriteAnimations(state->anim_playbacks, state->entity_count);
// Handle knight movement if they are moving somewhere
for (int i = 0; i < game->entity_count; i++) {
for (int i = 0; i < state->entity_count; i++) {
Vector2 input_vel = {};
Knight *knight = &game->knights[i];
Knight *knight = &state->knights[i];
if (knight->ordered_to_move) {
Vector2 target = knight->move_target_point;
if (Vector2DistanceSqr(target, knight->position) < 2.5f) {
@ -165,9 +213,9 @@ void Update(GameState *game, Camera2D *cam, float dt) {
// Handle the attacking state, if not handle state transitions
if (knight->state == KnightState::ATTACKING) {
if (IsAnimationFinished(game->anim_playbacks[i])) {
if (IsAnimationFinished(state->anim_playbacks[i])) {
knight->state = KnightState::IDLE;
PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &game->anim_playbacks[i]);
PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &state->anim_playbacks[i]);
}
} else {
if (input_vel.x != 0) {
@ -175,12 +223,12 @@ void Update(GameState *game, Camera2D *cam, float dt) {
}
if (input_vel.x != 0 || input_vel.y != 0) {
if (knight->state == KnightState::IDLE) {
PlayAnimation(ANIM_KNIGHT_RUN, knight_anims, &game->anim_playbacks[i]);
PlayAnimation(ANIM_KNIGHT_RUN, knight_anims, &state->anim_playbacks[i]);
knight->state = KnightState::RUNNING;
}
} else {
if (knight->state == KnightState::RUNNING) {
PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &game->anim_playbacks[i]);
PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &state->anim_playbacks[i]);
knight->state = KnightState::IDLE;
}
}
@ -188,33 +236,21 @@ void Update(GameState *game, Camera2D *cam, float dt) {
if (i == 0) continue;
// Sort the entities by y position using Insertion Sort
int j = i;
while (j > 0 && game->knights[j].position.y < game->knights[j - 1].position.y) {
while (j > 0 && state->knights[j].position.y < state->knights[j - 1].position.y) {
// Swap position
Knight temp_k = game->knights[j - 1];
game->knights[j - 1] = game->knights[j];
game->knights[j] = temp_k;
Knight temp_k = state->knights[j - 1];
state->knights[j - 1] = state->knights[j];
state->knights[j] = temp_k;
// Swap animations
SpriteAnimationPlayback temp_a = game->anim_playbacks[j - 1];
game->anim_playbacks[j - 1] = game->anim_playbacks[j];
game->anim_playbacks[j] = temp_a;
SpriteAnimationPlayback temp_a = state->anim_playbacks[j - 1];
state->anim_playbacks[j - 1] = state->anim_playbacks[j];
state->anim_playbacks[j] = temp_a;
j--;
}
}
}
void Draw(GameState *state, Assets assets, Camera2D cam, float dt) {
// BeginDrawing();
// {
// BeginMode2D(state.camera);
// {
// }
// EndMode2D();
// }
// EndDrawing();
(void)cam;
(void)dt;
ClearBackground({100, 149, 237, 255});
void Draw2D(GameState *state) {
int size = 32;
int topx = SCREEN_WIDTH / 2 - size * 32 / 2;
int topy = SCREEN_HEIGHT / 2 - size * 32 / 2;
@ -234,15 +270,15 @@ void Draw(GameState *state, Assets assets, Camera2D cam, float dt) {
}
Vector2 pos = {(f32)(32 * col + topx), (f32)(32 * row + topy)};
Rectangle src_rect = {(f32)(32 * atlas_col), (f32)(32 * atlas_row), 32, 32};
D_DrawTextureRec(assets.textures[TEX_GROUND], src_rect, pos, WHITE);
D_DrawTextureRec(state->assets.textures[TEX_GROUND], src_rect, pos, WHITE);
}
}
if (state->selected_point.has_value()) {
Vector2 marker_pos = state->selected_point.value();
marker_pos.x -= assets.textures[TEX_TARGET_RETICLE].width / 2;
marker_pos.y -= assets.textures[TEX_TARGET_RETICLE].height / 2;
D_DrawTextureV(assets.textures[TEX_TARGET_RETICLE], marker_pos, WHITE);
marker_pos.x -= state->assets.textures[TEX_TARGET_RETICLE].width / 2;
marker_pos.y -= state->assets.textures[TEX_TARGET_RETICLE].height / 2;
D_DrawTextureV(state->assets.textures[TEX_TARGET_RETICLE], marker_pos, WHITE);
}
// for (int i = 0; i < game->anim_playbacks_count; i++) {
@ -260,7 +296,7 @@ void Draw(GameState *state, Assets assets, Camera2D cam, float dt) {
src_rect.width = -abs((int)src_rect.width);
}
Rectangle dest_rect = {knight->position.x, knight->position.y, 192, 192};
Texture2D tex = assets.textures[TEX_KNIGHT];
Texture2D tex = state->assets.textures[TEX_KNIGHT];
D_DrawTexturePro(tex, src_rect, dest_rect, knight_origin, 0.0f, WHITE);
if (knight->selected == true) {
Rectangle knight_col_area = {
@ -276,7 +312,7 @@ void Draw(GameState *state, Assets assets, Camera2D cam, float dt) {
if (state->selection_mouse_start_pos.has_value()) {
Point start_pos = state->selection_mouse_start_pos.value();
Point current_pos = GetScreenToWorld2D(GetMousePosition(), cam);
Point current_pos = GetScreenToWorld2D(GetMousePosition(), state->camera);
if (Vector2DistanceSqr(current_pos, start_pos) >= 100.0f) {
f32 width = current_pos.x - start_pos.x;
@ -288,32 +324,41 @@ void Draw(GameState *state, Assets assets, Camera2D cam, float dt) {
}
}
Vector2 world = GetScreenToWorld2D(GetMousePosition(), cam);
Vector2 world = GetScreenToWorld2D(GetMousePosition(), state->camera);
Vector2 pointer_pos = Vector2Subtract(world, {24, 19});
D_DrawTextureV(assets.textures[TEX_MOUSE_CURSOR], pointer_pos, WHITE);
DrawRectangle(SCREEN_WIDTH - 106, 4, 88, 30, WHITE);
DrawFPS(SCREEN_WIDTH - 100, 10);
D_DrawTextureV(state->assets.textures[TEX_MOUSE_CURSOR], pointer_pos, WHITE);
}
extern "C" {
void game_init(GameState* state) {
(void)state;
Init();
GameState* game_init() {
return Init();
}
void game_update(GameState* state) {
(void)state;
// Your update logic
}
void game_draw(GameState* state) {
(void)state;
// Your render logic
void game_step(GameState* state) {
Input(state);
Update(state);
BeginDrawing();
{
ClearBackground({100, 149, 237, 255});
BeginMode2D(state->camera);
{
Draw2D(state);
}
EndMode2D();
DrawRectangle(SCREEN_WIDTH - 106, 4, 88, 30, WHITE);
DrawFPS(SCREEN_WIDTH - 100, 10);
}
EndDrawing();
}
void game_cleanup(GameState* state) {
(void)state;
// Cleanup
delete state;
printf("Cleaning Up\n");
CloseWindow();
}
bool window_should_close() {
return WindowShouldClose();
}
}

26
game.h
View File

@ -9,7 +9,7 @@ using namespace std;
#define TEXTURES_BUF_SIZE 16
#define TARGET_FPS 60
#define MAX_KNIGHTS 5000
#define MAX_KNIGHTS 50
#define SCREEN_WIDTH 1300
#define SCREEN_HEIGHT 1080
@ -43,9 +43,9 @@ struct Knight {
u8 ordered_to_move;
};
typedef struct Assets {
struct Assets {
Texture2D *textures;
} Assets;
};
struct GameState {
Camera2D camera;
@ -54,17 +54,21 @@ struct GameState {
optional<Point> selected_point;
Knight *knights;
SpriteAnimationPlayback* anim_playbacks;
SpriteAnimationPlayback *anim_playbacks;
Knight *selected_knights;
optional<Point> selection_mouse_start_pos;
int entity_count;
};
Assets assets;
extern "C" {
void game_init(GameState* state);
void game_update(GameState* state);
void game_draw(GameState* state);
void game_cleanup(GameState* state);
}
~GameState() {
free(knights);
free(anim_playbacks);
for (int i = 0; i < TEXTURES_BUF_SIZE; i++) {
UnloadTexture(assets.textures[i]);
}
free(assets.textures);
printf("I freed the stuff?\n");
}
};

BIN
game.so

Binary file not shown.

106
main.cpp
View File

@ -3,18 +3,23 @@
#include <sys/stat.h>
#include <cstdio>
#include "include/raylib.h"
#include "game.h"
#include "game_data.h"
struct GameState;
struct GameAPI {
void* lib_handle;
struct GameApi {
struct GameState *(*init)();
void (*step) (GameState *state);
void (*finalize) (GameState *state);
void (*reload) (GameState *state);
void (*unload) (GameState *state);
bool (*window_should_close)();
};
struct Game {
time_t last_write_time;
void (*init)(GameState*);
void (*update)(GameState*);
void (*draw)(GameState*);
void (*cleanup)(GameState*);
void *gamelib_handle;
ino_t gamelib_id;
GameApi api;
GameState *state;
};
time_t get_file_write_time(const char* path) {
@ -25,88 +30,49 @@ time_t get_file_write_time(const char* path) {
return 0;
}
bool load_game_api(GameAPI* api) {
bool load_game_api(Game* game) {
const char* lib_path = "./game.so";
time_t write_time = get_file_write_time(lib_path);
if (write_time <= api->last_write_time) {
return false; // No changes
if (write_time <= game->last_write_time) {
return false;
}
if (api->lib_handle) {
dlclose(api->lib_handle);
if (game->gamelib_handle) {
dlclose(game->gamelib_handle);
}
api->lib_handle = dlopen(lib_path, RTLD_NOW);
if (!api->lib_handle) {
game->gamelib_handle = dlopen(lib_path, RTLD_NOW);
if (!game->gamelib_handle) {
printf("Failed to load game.so: %s\n", dlerror());
return false;
}
api->init = (void(*)(GameState*))dlsym(api->lib_handle, "game_init");
api->update = (void(*)(GameState*))dlsym(api->lib_handle, "game_update");
api->draw = (void(*)(GameState*))dlsym(api->lib_handle, "game_draw");
api->cleanup = (void(*)(GameState*))dlsym(api->lib_handle, "game_cleanup");
game->api.init = (GameState*(*)())dlsym(game->gamelib_handle, "game_init");
game->api.step = (void(*)(GameState*))dlsym(game->gamelib_handle, "game_step");
game->api.finalize = (void(*)(GameState*))dlsym(game->gamelib_handle, "game_cleanup");
game->api.window_should_close = (bool(*)())dlsym(game->gamelib_handle, "window_should_close");
api->last_write_time = write_time;
game->last_write_time = write_time;
printf("Reloaded game.so\n");
return true;
}
int main(void) {
SetTraceLogLevel(4);
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tiny Knights");
Game game;
int monitor = GetCurrentMonitor();
int monitor_width = GetMonitorWidth(monitor);
int monitor_height = GetMonitorHeight(monitor);
int win_pos_x = monitor_width / 2 - SCREEN_WIDTH / 2;
int win_pos_y = monitor_height / 2 - SCREEN_HEIGHT / 2;
SetWindowPosition(win_pos_x, win_pos_y);
load_game_api(&game);
SetTargetFPS(TARGET_FPS);
HideCursor();
GameAPI api = {};
GameState state = {};
state.camera.zoom = 1.0f;
api.init(&state);
// Assets assets = api.init(&state);
GameState game = {};
game.knights = (Knight*)calloc(MAX_KNIGHTS, sizeof(Knight));
game.anim_playbacks = (SpriteAnimationPlayback*)calloc(MAX_KNIGHTS, sizeof(SpriteAnimationPlayback));
const int entities = MAX_KNIGHTS;
for (int i = 0; i < entities; i++) {
f32 rand_x = GetRandomValue(165, 1130);
f32 rand_y = 100 + ((float)950 / (float)entities) * i;
game.knights[i].position = {rand_x,rand_y};
PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &game.anim_playbacks[i]);
int rand_frame = GetRandomValue(0, knight_anims[ANIM_KNIGHT_IDLE].total_frames);
game.anim_playbacks[i].current_frame = rand_frame;
}
game.entity_count = entities;
api.update(&game);
while (!WindowShouldClose()) {
game.frame_count++;
game.state = game.api.init();
while (!game.api.window_should_close()) {
// game.frame_count++;
// float dt = GetFrameTime();
load_game_api(&game);
api.update(&state);
api.draw(&state);
game.api.step(game.state);
}
free(game.knights);
free(game.anim_playbacks);
// for (int i = 0; i < TEXTURES_BUF_SIZE; i++) {
// UnloadTexture(assets.textures[i]);
// }
// free(assets.textures);
CloseWindow();
game.api.finalize(game.state);
return 0;
}