Restructured everything so hot reloading works
This commit is contained in:
parent
40022bbdb4
commit
fd4f8ffbb1
1
.gitignore
vendored
1
.gitignore
vendored
@ -10,3 +10,4 @@
|
|||||||
/game.fasl
|
/game.fasl
|
||||||
/writeimage
|
/writeimage
|
||||||
*.fasl
|
*.fasl
|
||||||
|
*.so
|
||||||
|
|||||||
5
Makefile
5
Makefile
@ -11,10 +11,11 @@ main: main.cpp game.h Makefile
|
|||||||
$(CC) $(CFLAGS) $(INCLUDES) main.cpp $(LDFLAGS) -o main
|
$(CC) $(CFLAGS) $(INCLUDES) main.cpp $(LDFLAGS) -o main
|
||||||
|
|
||||||
game.so: game.cpp game.h lib.h sprites.h game_data.h Makefile
|
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
|
run: all
|
||||||
./main
|
LD_LIBRARY_PATH=lib ./main
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -vf *.so *.o main
|
rm -vf *.so *.o main
|
||||||
|
|||||||
267
game.cpp
267
game.cpp
@ -1,6 +1,8 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -10,8 +12,7 @@ using namespace std;
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "game_data.h"
|
#include "game_data.h"
|
||||||
|
|
||||||
// Assets Init() {
|
GameState *InitState() {
|
||||||
void Init() {
|
|
||||||
knight_anims[ANIM_KNIGHT_IDLE] = knight_idle;
|
knight_anims[ANIM_KNIGHT_IDLE] = knight_idle;
|
||||||
knight_anims[ANIM_KNIGHT_RUN] = knight_run;
|
knight_anims[ANIM_KNIGHT_RUN] = knight_run;
|
||||||
knight_anims[ANIM_KNIGHT_ATTACK_SIDE1] = knight_attack_side1;
|
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");
|
LoadTexture("./assets/Factions/Knights/Troops/Warrior/Blue/Warrior_Blue.png");
|
||||||
assets.textures[TEX_MOUSE_CURSOR] = LoadTexture("./assets/UI/Pointers/01.png");
|
assets.textures[TEX_MOUSE_CURSOR] = LoadTexture("./assets/UI/Pointers/01.png");
|
||||||
assets.textures[TEX_TARGET_RETICLE] = LoadTexture("./assets/UI/Pointers/02.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)) {
|
if (IsMouseButtonPressed(0)) {
|
||||||
game->selection_mouse_start_pos = optional<Point>{GetScreenToWorld2D(GetMousePosition(), *cam)};
|
state->selection_mouse_start_pos = optional<Point>{GetScreenToWorld2D(GetMousePosition(), state->camera)};
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (IsMouseButtonReleased(0)) {
|
if (IsMouseButtonReleased(0)) {
|
||||||
// TODO: This is what should happen when the distance is too small
|
// 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.some.point;
|
||||||
Point start_pos = game->selection_mouse_start_pos.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) {
|
if (Vector2DistanceSqr(current_pos, start_pos) >= 100.0f) {
|
||||||
f32 width = current_pos.x - start_pos.x;
|
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 x = width >= 0.0f ? start_pos.x : current_pos.x;
|
||||||
f32 y = height >= 0.0f ? start_pos.y : current_pos.y;
|
f32 y = height >= 0.0f ? start_pos.y : current_pos.y;
|
||||||
Rectangle rect = {x, y, fabs(width), fabs(height)};
|
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 = {
|
Rectangle mouse_select_area = {
|
||||||
game->knights[i].position.x + knight_colrect_select.x,
|
state->knights[i].position.x + knight_colrect_select.x,
|
||||||
game->knights[i].position.y + knight_colrect_select.y,
|
state->knights[i].position.y + knight_colrect_select.y,
|
||||||
knight_colrect_select.width,
|
knight_colrect_select.width,
|
||||||
knight_colrect_select.height,
|
knight_colrect_select.height,
|
||||||
};
|
};
|
||||||
game->knights[i].selected =
|
state->knights[i].selected =
|
||||||
CheckCollisionRecs(rect, mouse_select_area);
|
CheckCollisionRecs(rect, mouse_select_area);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// TODO: This kind of sucks that we're doing all the math here to calculate
|
// 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
|
// the position of the collider, we need a helper that calculates origin offset
|
||||||
int k_idx = -1;
|
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 = {
|
Rectangle mouse_select_area = {
|
||||||
game->knights[i].position.x + knight_colrect_select.x,
|
state->knights[i].position.x + knight_colrect_select.x,
|
||||||
game->knights[i].position.y + knight_colrect_select.y,
|
state->knights[i].position.y + knight_colrect_select.y,
|
||||||
knight_colrect_select.width,
|
knight_colrect_select.width,
|
||||||
knight_colrect_select.height,
|
knight_colrect_select.height,
|
||||||
};
|
};
|
||||||
if (CheckCollisionPointRec(current_pos, mouse_select_area)) {
|
if (CheckCollisionPointRec(current_pos, mouse_select_area)) {
|
||||||
k_idx = i;
|
k_idx = i;
|
||||||
}
|
}
|
||||||
game->knights[i].selected = false;
|
state->knights[i].selected = false;
|
||||||
}
|
}
|
||||||
if (k_idx != -1) {
|
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)) {
|
if (IsMouseButtonPressed(1)) {
|
||||||
Point target_pos = GetScreenToWorld2D(GetMousePosition(), *cam);
|
Point target_pos = GetScreenToWorld2D(GetMousePosition(), state->camera);
|
||||||
bool any = false;
|
bool any = false;
|
||||||
for (int i = 0; i < game->entity_count; i++) {
|
for (int i = 0; i < state->entity_count; i++) {
|
||||||
if (game->knights[i].selected) {
|
if (state->knights[i].selected) {
|
||||||
game->knights[i].move_target_point = target_pos;
|
state->knights[i].move_target_point = target_pos;
|
||||||
game->knights[i].ordered_to_move = true;
|
state->knights[i].ordered_to_move = true;
|
||||||
any = true;
|
any = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (any) {
|
if (any) {
|
||||||
game->selected_point = optional<Point>{target_pos};
|
state->selected_point = optional<Point>{target_pos};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const float cam_move_speed = 1050.0f * dt;
|
f32 cam_speed = 5.0;
|
||||||
Vector2 cam_vel = {};
|
if (IsKeyDown(KEY_RIGHT)) {
|
||||||
if (IsKeyDown(KEY_D)) {
|
state->camera.target.x += cam_speed;
|
||||||
cam_vel.x = -1;
|
|
||||||
}
|
}
|
||||||
if (IsKeyDown(KEY_A)) {
|
if (IsKeyDown(KEY_LEFT)) {
|
||||||
cam_vel.x = 1;
|
state->camera.target.x -= cam_speed;
|
||||||
}
|
}
|
||||||
if (IsKeyDown(KEY_W)) {
|
if (IsKeyDown(KEY_UP)) {
|
||||||
cam_vel.y = 1;
|
state->camera.target.y -= cam_speed;
|
||||||
}
|
}
|
||||||
if (IsKeyDown(KEY_S)) {
|
if (IsKeyDown(KEY_DOWN)) {
|
||||||
cam_vel.y = -1;
|
state->camera.target.y += cam_speed;
|
||||||
}
|
}
|
||||||
cam_vel = Vector2Normalize(cam_vel);
|
// if (IsKeyPressed(KEY_SPACE) && state->selected_knight != -1) {
|
||||||
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) {
|
|
||||||
// int k_idx = game->selected_knight;
|
// int k_idx = game->selected_knight;
|
||||||
// if (game->knights[k_idx].state != KNIGHT_ATTACKING) {
|
// if (game->knights[k_idx].state != KNIGHT_ATTACKING) {
|
||||||
// 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)) {
|
if (IsKeyPressed(KEY_F1)) {
|
||||||
global_debug_mode = !global_debug_mode;
|
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
|
// 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
|
// 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 = {};
|
Vector2 input_vel = {};
|
||||||
Knight *knight = &game->knights[i];
|
Knight *knight = &state->knights[i];
|
||||||
if (knight->ordered_to_move) {
|
if (knight->ordered_to_move) {
|
||||||
Vector2 target = knight->move_target_point;
|
Vector2 target = knight->move_target_point;
|
||||||
if (Vector2DistanceSqr(target, knight->position) < 2.5f) {
|
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
|
// Handle the attacking state, if not handle state transitions
|
||||||
if (knight->state == KnightState::ATTACKING) {
|
if (knight->state == KnightState::ATTACKING) {
|
||||||
if (IsAnimationFinished(game->anim_playbacks[i])) {
|
if (IsAnimationFinished(state->anim_playbacks[i])) {
|
||||||
knight->state = KnightState::IDLE;
|
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 {
|
} else {
|
||||||
if (input_vel.x != 0) {
|
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 (input_vel.x != 0 || input_vel.y != 0) {
|
||||||
if (knight->state == KnightState::IDLE) {
|
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;
|
knight->state = KnightState::RUNNING;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (knight->state == KnightState::RUNNING) {
|
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;
|
knight->state = KnightState::IDLE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -188,33 +236,21 @@ void Update(GameState *game, Camera2D *cam, float dt) {
|
|||||||
if (i == 0) continue;
|
if (i == 0) continue;
|
||||||
// Sort the entities by y position using Insertion Sort
|
// Sort the entities by y position using Insertion Sort
|
||||||
int j = i;
|
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
|
// Swap position
|
||||||
Knight temp_k = game->knights[j - 1];
|
Knight temp_k = state->knights[j - 1];
|
||||||
game->knights[j - 1] = game->knights[j];
|
state->knights[j - 1] = state->knights[j];
|
||||||
game->knights[j] = temp_k;
|
state->knights[j] = temp_k;
|
||||||
// Swap animations
|
// Swap animations
|
||||||
SpriteAnimationPlayback temp_a = game->anim_playbacks[j - 1];
|
SpriteAnimationPlayback temp_a = state->anim_playbacks[j - 1];
|
||||||
game->anim_playbacks[j - 1] = game->anim_playbacks[j];
|
state->anim_playbacks[j - 1] = state->anim_playbacks[j];
|
||||||
game->anim_playbacks[j] = temp_a;
|
state->anim_playbacks[j] = temp_a;
|
||||||
j--;
|
j--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Draw(GameState *state, Assets assets, Camera2D cam, float dt) {
|
void Draw2D(GameState *state) {
|
||||||
// BeginDrawing();
|
|
||||||
// {
|
|
||||||
// BeginMode2D(state.camera);
|
|
||||||
// {
|
|
||||||
// }
|
|
||||||
// EndMode2D();
|
|
||||||
// }
|
|
||||||
// EndDrawing();
|
|
||||||
(void)cam;
|
|
||||||
(void)dt;
|
|
||||||
ClearBackground({100, 149, 237, 255});
|
|
||||||
|
|
||||||
int size = 32;
|
int size = 32;
|
||||||
int topx = SCREEN_WIDTH / 2 - size * 32 / 2;
|
int topx = SCREEN_WIDTH / 2 - size * 32 / 2;
|
||||||
int topy = SCREEN_HEIGHT / 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)};
|
Vector2 pos = {(f32)(32 * col + topx), (f32)(32 * row + topy)};
|
||||||
Rectangle src_rect = {(f32)(32 * atlas_col), (f32)(32 * atlas_row), 32, 32};
|
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()) {
|
if (state->selected_point.has_value()) {
|
||||||
Vector2 marker_pos = state->selected_point.value();
|
Vector2 marker_pos = state->selected_point.value();
|
||||||
marker_pos.x -= assets.textures[TEX_TARGET_RETICLE].width / 2;
|
marker_pos.x -= state->assets.textures[TEX_TARGET_RETICLE].width / 2;
|
||||||
marker_pos.y -= assets.textures[TEX_TARGET_RETICLE].height / 2;
|
marker_pos.y -= state->assets.textures[TEX_TARGET_RETICLE].height / 2;
|
||||||
D_DrawTextureV(assets.textures[TEX_TARGET_RETICLE], marker_pos, WHITE);
|
D_DrawTextureV(state->assets.textures[TEX_TARGET_RETICLE], marker_pos, WHITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// for (int i = 0; i < game->anim_playbacks_count; i++) {
|
// 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);
|
src_rect.width = -abs((int)src_rect.width);
|
||||||
}
|
}
|
||||||
Rectangle dest_rect = {knight->position.x, knight->position.y, 192, 192};
|
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);
|
D_DrawTexturePro(tex, src_rect, dest_rect, knight_origin, 0.0f, WHITE);
|
||||||
if (knight->selected == true) {
|
if (knight->selected == true) {
|
||||||
Rectangle knight_col_area = {
|
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()) {
|
if (state->selection_mouse_start_pos.has_value()) {
|
||||||
Point start_pos = state->selection_mouse_start_pos.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) {
|
if (Vector2DistanceSqr(current_pos, start_pos) >= 100.0f) {
|
||||||
f32 width = current_pos.x - start_pos.x;
|
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});
|
Vector2 pointer_pos = Vector2Subtract(world, {24, 19});
|
||||||
D_DrawTextureV(assets.textures[TEX_MOUSE_CURSOR], pointer_pos, WHITE);
|
D_DrawTextureV(state->assets.textures[TEX_MOUSE_CURSOR], pointer_pos, WHITE);
|
||||||
|
|
||||||
DrawRectangle(SCREEN_WIDTH - 106, 4, 88, 30, WHITE);
|
|
||||||
DrawFPS(SCREEN_WIDTH - 100, 10);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
void game_init(GameState* state) {
|
GameState* game_init() {
|
||||||
(void)state;
|
return Init();
|
||||||
Init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_update(GameState* state) {
|
void game_step(GameState* state) {
|
||||||
(void)state;
|
Input(state);
|
||||||
// Your update logic
|
Update(state);
|
||||||
}
|
BeginDrawing();
|
||||||
|
{
|
||||||
void game_draw(GameState* state) {
|
ClearBackground({100, 149, 237, 255});
|
||||||
(void)state;
|
BeginMode2D(state->camera);
|
||||||
// Your render logic
|
{
|
||||||
|
Draw2D(state);
|
||||||
|
}
|
||||||
|
EndMode2D();
|
||||||
|
|
||||||
|
DrawRectangle(SCREEN_WIDTH - 106, 4, 88, 30, WHITE);
|
||||||
|
DrawFPS(SCREEN_WIDTH - 100, 10);
|
||||||
|
}
|
||||||
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_cleanup(GameState* state) {
|
void game_cleanup(GameState* state) {
|
||||||
(void)state;
|
delete state;
|
||||||
// Cleanup
|
printf("Cleaning Up\n");
|
||||||
|
CloseWindow();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool window_should_close() {
|
||||||
|
return WindowShouldClose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
26
game.h
26
game.h
@ -9,7 +9,7 @@ using namespace std;
|
|||||||
|
|
||||||
#define TEXTURES_BUF_SIZE 16
|
#define TEXTURES_BUF_SIZE 16
|
||||||
#define TARGET_FPS 60
|
#define TARGET_FPS 60
|
||||||
#define MAX_KNIGHTS 5000
|
#define MAX_KNIGHTS 50
|
||||||
#define SCREEN_WIDTH 1300
|
#define SCREEN_WIDTH 1300
|
||||||
#define SCREEN_HEIGHT 1080
|
#define SCREEN_HEIGHT 1080
|
||||||
|
|
||||||
@ -43,9 +43,9 @@ struct Knight {
|
|||||||
u8 ordered_to_move;
|
u8 ordered_to_move;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct Assets {
|
struct Assets {
|
||||||
Texture2D *textures;
|
Texture2D *textures;
|
||||||
} Assets;
|
};
|
||||||
|
|
||||||
struct GameState {
|
struct GameState {
|
||||||
Camera2D camera;
|
Camera2D camera;
|
||||||
@ -54,17 +54,21 @@ struct GameState {
|
|||||||
|
|
||||||
optional<Point> selected_point;
|
optional<Point> selected_point;
|
||||||
Knight *knights;
|
Knight *knights;
|
||||||
SpriteAnimationPlayback* anim_playbacks;
|
SpriteAnimationPlayback *anim_playbacks;
|
||||||
Knight *selected_knights;
|
Knight *selected_knights;
|
||||||
|
|
||||||
optional<Point> selection_mouse_start_pos;
|
optional<Point> selection_mouse_start_pos;
|
||||||
|
|
||||||
int entity_count;
|
int entity_count;
|
||||||
};
|
Assets assets;
|
||||||
|
|
||||||
extern "C" {
|
~GameState() {
|
||||||
void game_init(GameState* state);
|
free(knights);
|
||||||
void game_update(GameState* state);
|
free(anim_playbacks);
|
||||||
void game_draw(GameState* state);
|
for (int i = 0; i < TEXTURES_BUF_SIZE; i++) {
|
||||||
void game_cleanup(GameState* state);
|
UnloadTexture(assets.textures[i]);
|
||||||
}
|
}
|
||||||
|
free(assets.textures);
|
||||||
|
printf("I freed the stuff?\n");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
106
main.cpp
106
main.cpp
@ -3,18 +3,23 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
#include "include/raylib.h"
|
struct GameState;
|
||||||
#include "game.h"
|
|
||||||
#include "game_data.h"
|
|
||||||
|
|
||||||
struct GameAPI {
|
struct GameApi {
|
||||||
void* lib_handle;
|
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;
|
time_t last_write_time;
|
||||||
|
void *gamelib_handle;
|
||||||
void (*init)(GameState*);
|
ino_t gamelib_id;
|
||||||
void (*update)(GameState*);
|
GameApi api;
|
||||||
void (*draw)(GameState*);
|
GameState *state;
|
||||||
void (*cleanup)(GameState*);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
time_t get_file_write_time(const char* path) {
|
time_t get_file_write_time(const char* path) {
|
||||||
@ -25,88 +30,49 @@ time_t get_file_write_time(const char* path) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool load_game_api(GameAPI* api) {
|
bool load_game_api(Game* game) {
|
||||||
const char* lib_path = "./game.so";
|
const char* lib_path = "./game.so";
|
||||||
time_t write_time = get_file_write_time(lib_path);
|
time_t write_time = get_file_write_time(lib_path);
|
||||||
|
|
||||||
if (write_time <= api->last_write_time) {
|
if (write_time <= game->last_write_time) {
|
||||||
return false; // No changes
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (api->lib_handle) {
|
if (game->gamelib_handle) {
|
||||||
dlclose(api->lib_handle);
|
dlclose(game->gamelib_handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
api->lib_handle = dlopen(lib_path, RTLD_NOW);
|
game->gamelib_handle = dlopen(lib_path, RTLD_NOW);
|
||||||
if (!api->lib_handle) {
|
|
||||||
|
if (!game->gamelib_handle) {
|
||||||
printf("Failed to load game.so: %s\n", dlerror());
|
printf("Failed to load game.so: %s\n", dlerror());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
api->init = (void(*)(GameState*))dlsym(api->lib_handle, "game_init");
|
game->api.init = (GameState*(*)())dlsym(game->gamelib_handle, "game_init");
|
||||||
api->update = (void(*)(GameState*))dlsym(api->lib_handle, "game_update");
|
game->api.step = (void(*)(GameState*))dlsym(game->gamelib_handle, "game_step");
|
||||||
api->draw = (void(*)(GameState*))dlsym(api->lib_handle, "game_draw");
|
game->api.finalize = (void(*)(GameState*))dlsym(game->gamelib_handle, "game_cleanup");
|
||||||
api->cleanup = (void(*)(GameState*))dlsym(api->lib_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");
|
printf("Reloaded game.so\n");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
SetTraceLogLevel(4);
|
Game game;
|
||||||
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tiny Knights");
|
|
||||||
|
|
||||||
int monitor = GetCurrentMonitor();
|
load_game_api(&game);
|
||||||
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);
|
game.state = game.api.init();
|
||||||
|
while (!game.api.window_should_close()) {
|
||||||
HideCursor();
|
// game.frame_count++;
|
||||||
|
|
||||||
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++;
|
|
||||||
// float dt = GetFrameTime();
|
// float dt = GetFrameTime();
|
||||||
|
load_game_api(&game);
|
||||||
|
|
||||||
api.update(&state);
|
game.api.step(game.state);
|
||||||
api.draw(&state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(game.knights);
|
game.api.finalize(game.state);
|
||||||
free(game.anim_playbacks);
|
|
||||||
// for (int i = 0; i < TEXTURES_BUF_SIZE; i++) {
|
|
||||||
// UnloadTexture(assets.textures[i]);
|
|
||||||
// }
|
|
||||||
// free(assets.textures);
|
|
||||||
CloseWindow();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user