Compare commits

...

4 Commits

7 changed files with 119 additions and 221 deletions

View File

@ -1,17 +1,18 @@
CFLAGS=-g --std=c++23 -fsanitize=address -fno-omit-frame-pointer -Wall -Wextra -pedantic -O0
CFLAGS=--std=c++23 -Wall -Wextra -pedantic -O0 -DDEBUG
DEBUGFLAGS=-g3 -gdwarf-4 -fsanitize=address -fno-omit-frame-pointer -DDEBUG
LDFLAGS=-L./lib -lraylib -lm -ldl
INCLUDES=-I./include
CC=g++
.PHONY: build clean run all
all: main game.so
all: game.so main
main: main.cpp game.h Makefile
$(CC) $(CFLAGS) $(INCLUDES) main.cpp $(LDFLAGS) -o main
$(CC) $(CFLAGS) $(INCLUDES) $(DEBUGFLAGS) 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.tmp
$(CC) $(CFLAGS) $(INCLUDES) $(DEBUGFLAGS) -shared -fPIC game.cpp -L./lib -lraylib -lm -o game.so.tmp
mv game.so.tmp game.so
run: all

180
game.cpp
View File

@ -13,15 +13,6 @@ using namespace std;
#include "game_data.h"
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;
knight_anims[ANIM_KNIGHT_ATTACK_SIDE2] = knight_attack_side2;
knight_anims[ANIM_KNIGHT_ATTACK_TOP1] = knight_attack_front1;
knight_anims[ANIM_KNIGHT_ATTACK_TOP2] = knight_attack_front2;
knight_anims[ANIM_KNIGHT_ATTACK_BACK1] = knight_attack_back1;
knight_anims[ANIM_KNIGHT_ATTACK_BACK2] = knight_attack_back2;
Assets assets = {};
assets.textures = (Texture*)malloc(sizeof(Texture2D) * TEXTURES_BUF_SIZE);
assets.textures[TEX_GROUND] = LoadTexture("./assets/Terrain/Ground/Tilemap_Flat.png");
@ -31,6 +22,7 @@ GameState *InitState() {
assets.textures[TEX_TARGET_RETICLE] = LoadTexture("./assets/UI/Pointers/02.png");
auto game = new GameState;
game->should_reset = false;
game->assets = assets;
// TODO: Probably want to use idiomatic C++ here?
game->knights = (Knight*)calloc(MAX_KNIGHTS, sizeof(Knight));
@ -42,7 +34,7 @@ GameState *InitState() {
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);
int rand_frame = GetRandomValue(0, knight_anims[ANIM_KNIGHT_IDLE].total_frames - 1);
game->anim_playbacks[i].current_frame = rand_frame;
}
game->entity_count = entities;
@ -53,7 +45,7 @@ GameState *InitState() {
}
GameState *Init() {
SetTraceLogLevel(4);
SetTraceLogLevel(LOG_WARNING);
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tiny Knights");
int monitor = GetCurrentMonitor();
@ -72,51 +64,17 @@ GameState *Init() {
void Input(GameState *state) {
if (IsMouseButtonPressed(0)) {
state->selection_mouse_start_pos = optional<Point>{GetScreenToWorld2D(GetMousePosition(), state->camera)};
if (IsKeyDown(KEY_LEFT_CONTROL)) {
state->drag_camera_start_pos = optional<Point>{GetMousePosition()};
state->camera_target_start_pos = state->camera.target;
} else {
auto point = GetScreenToWorld2D(GetMousePosition(), state->camera);
state->selection_mouse_start_pos = optional<Point>{point};
}
}
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 = 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);
}
} 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 < 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,
};
if (CheckCollisionPointRec(current_pos, mouse_select_area)) {
k_idx = i;
}
state->knights[i].selected = false;
}
if (k_idx != -1) {
state->knights[k_idx].selected = true;
}
}
state->selection_mouse_start_pos = optional<Point>{};
state->drag_camera_start_pos = optional<Point>{};
}
if (IsMouseButtonPressed(1)) {
@ -134,32 +92,22 @@ void Input(GameState *state) {
}
}
f32 cam_speed = 5.0;
if (IsKeyDown(KEY_RIGHT)) {
state->camera.target.x += cam_speed;
if (IsKeyPressed(KEY_SPACE)) {
for (int i = 0; i < state->entity_count; i++) {
auto& knight = state->knights[i];
if (knight.selected && knight.state != KnightState::ATTACKING) {
knight.ordered_to_move = false;
knight.state = KnightState::ATTACKING;
PlayAnimation(ANIM_KNIGHT_ATTACK_SIDE1, knight_anims, &state->anim_playbacks[i]);
}
}
}
if (IsKeyDown(KEY_LEFT)) {
state->camera.target.x -= cam_speed;
}
if (IsKeyDown(KEY_UP)) {
state->camera.target.y -= cam_speed;
}
if (IsKeyDown(KEY_DOWN)) {
state->camera.target.y += cam_speed;
}
// 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;
// PlayAnimation(ANIM_KNIGHT_ATTACK_SIDE1, knight_anims, &game->anim_playbacks[k_idx]);
// }
// }
if (IsKeyPressed(KEY_F1)) {
global_debug_mode = !global_debug_mode;
}
if (IsKeyPressed(KEY_F2)) {
auto new_state = InitState();
*state = *new_state;
state->should_reset = true;
printf("Resetting GameState\n");
}
}
@ -169,24 +117,25 @@ void Update(GameState *state) {
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);
}
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);
}
}
if (state->drag_camera_start_pos.has_value()) {
Point start_pos = state->drag_camera_start_pos.value();
state->camera.target = state->camera_target_start_pos + (start_pos - GetMousePosition());
}
float dt = 0.33;
const float cam_move_speed = 1050.0f * dt;
// TODO: This was moved out of Input
@ -234,6 +183,7 @@ void Update(GameState *state) {
}
}
if (i == 0) continue;
// TODO: This is likely getting called every frame, probably best to have a dirty flag to detect movement
// Sort the entities by y position using Insertion Sort
int j = i;
while (j > 0 && state->knights[j].position.y < state->knights[j - 1].position.y) {
@ -270,7 +220,7 @@ void Draw2D(GameState *state) {
}
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(state->assets.textures[TEX_GROUND], src_rect, pos, WHITE);
DrawTextureRec(state->assets.textures[TEX_GROUND], src_rect, pos, WHITE);
}
}
@ -305,7 +255,6 @@ void Draw2D(GameState *state) {
knight_colrect_select.width,
knight_colrect_select.height,
};
// Color color = game->selected_knight == NULL ? RED : GREEN;
DrawRectangleLinesEx(knight_col_area, 2.0f, GREEN);
}
}
@ -333,26 +282,37 @@ extern "C" {
GameState* game_init() {
return Init();
}
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();
GameState* game_init_state() {
return InitState();
}
void game_cleanup(GameState* state) {
void game_step(GameState *state) {
try {
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();
}
catch (const exception& e) {
printf("Exception: %s\n", e.what());
TraceLog(LOG_ERROR, "Exception: %s", e.what());
}
}
void game_cleanup(GameState *state) {
// free(state->assets.textures);
delete state;
printf("Cleaning Up\n");
CloseWindow();

13
game.h
View File

@ -2,6 +2,7 @@
#include "raylib.h"
#include "lib.h"
#include "sprites.h"
#include <vector>
#include <optional>
@ -13,11 +14,9 @@ using namespace std;
#define SCREEN_WIDTH 1300
#define SCREEN_HEIGHT 1080
#define DEBUG_MODE_ENABLED
bool global_debug_mode = false;
enum class KnightState : u8 {
enum class KnightState {
IDLE = 0,
RUNNING = 1,
ATTACKING = 2,
@ -48,6 +47,7 @@ struct Assets {
};
struct GameState {
bool should_reset;
Camera2D camera;
int frame_count;
Point camera_position;
@ -55,13 +55,16 @@ struct GameState {
optional<Point> selected_point;
Knight *knights;
SpriteAnimationPlayback *anim_playbacks;
Knight *selected_knights;
optional<Point> selection_mouse_start_pos;
int entity_count;
Assets assets;
optional<Point> drag_camera_start_pos;
Point camera_target_start_pos;
~GameState() {
free(knights);
free(anim_playbacks);
@ -69,6 +72,6 @@ struct GameState {
UnloadTexture(assets.textures[i]);
}
free(assets.textures);
printf("I freed the stuff?\n");
printf("Freeing GameState memory\n");
}
};

View File

@ -14,13 +14,6 @@
#define ANIM_KNIGHT_ATTACK_BACK1 6
#define ANIM_KNIGHT_ATTACK_BACK2 7
// SpriteSheet knight_spritesheet = {
// .name = "blue_knight",
// .texture = {0},
// // .anim_count = 8,
// .size = { 1152, 1536 },
// };
const u16 knight_sprite_size = 192;
const Vector2 knight_origin = {knight_sprite_size / 2, knight_sprite_size / 2 + 35};
const Size knight_col_size = {50, 75};
@ -32,52 +25,13 @@ const Rectangle knight_colrect_select = {
knight_col_size.height,
};
SpriteAnimation knight_idle = {
.loop = true,
.total_frames = 6,
SpriteAnimation knight_anims[8] = {
{.loop = true, .total_frames = 6}, // ANIM_KNIGHT_IDLE
{.loop = true, .total_frames = 6}, // ANIM_KNIGHT_RUN
{.loop = false, .total_frames = 6}, // ANIM_KNIGHT_ATTACK_SIDE1
{.loop = false, .total_frames = 6}, // ANIM_KNIGHT_ATTACK_SIDE2
{.loop = false, .total_frames = 6}, // ANIM_KNIGHT_ATTACK_TOP1
{.loop = false, .total_frames = 6}, // ANIM_KNIGHT_ATTACK_TOP2
{.loop = false, .total_frames = 6}, // ANIM_KNIGHT_ATTACK_BACK1
{.loop = false, .total_frames = 6}, // ANIM_KNIGHT_ATTACK_BACK2
};
SpriteAnimation knight_run = {
.loop = true,
.total_frames = 6,
};
SpriteAnimation knight_attack_side1 = {
.loop = false,
.total_frames = 6,
};
SpriteAnimation knight_attack_side2 = {
.loop = false,
.total_frames = 6,
};
SpriteAnimation knight_attack_front1 = {
.loop = false,
.total_frames = 6,
};
SpriteAnimation knight_attack_front2 = {
.loop = false,
.total_frames = 6,
};
SpriteAnimation knight_attack_back1 = {
.loop = false,
.total_frames = 6,
};
SpriteAnimation knight_attack_back2 = {
.loop = false,
.total_frames = 6,
};
SpriteAnimation knight_anims[8];
// knight_anims[ANIM_KNIGHT_IDLE] = knight_idle;
// knight_anims[ANIM_KNIGHT_RUN] = knight_run;
// knight_anims[ANIM_KNIGHT_ATTACK_SIDE1] = knight_attack_side1;
// knight_anims[ANIM_KNIGHT_ATTACK_SIDE2] = knight_attack_side2;
// knight_anims[ANIM_KNIGHT_ATTACK_TOP1] = knight_attack_front1;
// knight_anims[ANIM_KNIGHT_ATTACK_TOP2] = knight_attack_front2;
// knight_anims[ANIM_KNIGHT_ATTACK_BACK1] = knight_attack_back1;
// knight_anims[ANIM_KNIGHT_ATTACK_BACK2] = knight_attack_back2;

34
lib.h
View File

@ -17,7 +17,6 @@ typedef float f32;
typedef double f64;
typedef uintptr_t uptr;
typedef char sbyte;
typedef ptrdiff_t size;
typedef size_t usize;
typedef Vector2 Point;
@ -48,7 +47,7 @@ extern "C" {
extern bool global_debug_mode;
inline void D_DrawTextureV(Texture2D texture, Vector2 position, Color tint) {
DrawTextureV(texture, position, tint);
#ifdef DEBUG_MODE_ENABLED
#ifdef DEBUG
if (global_debug_mode) {
Rectangle debug = {position.x,position.y,(f32)texture.width,(f32)texture.height};
DrawRectangleLinesEx(debug, 1.0f, RED);
@ -56,37 +55,10 @@ inline void D_DrawTextureV(Texture2D texture, Vector2 position, Color tint) {
#endif
}
// Vector2 operations
#define v2_add Vector2Add
#define v2_sub Vector2Subtract
#define v2_mul Vector2Scale
#define v2_div Vector2Divide
#define v2_dot Vector2DotProduct
#define v2_len Vector2Length
#define v2_norm Vector2Normalize
#define v2_dist Vector2Distance
#define v2_lerp Vector2Lerp
// Vector3 operations
#define v3_add Vector3Add
#define v3_sub Vector3Subtract
#define v3_mul Vector3Scale
#define v3_div Vector3Divide
#define v3_dot Vector3DotProduct
#define v3_cross Vector3CrossProduct
#define v3_len Vector3Length
#define v3_norm Vector3Normalize
#define v3_dist Vector3Distance
#define v3_lerp Vector3Lerp
// Constructor-style macros
#define v2(x, y) ((Vector2){x, y})
#define v3(x, y, z) ((Vector3){x, y, z})
inline void D_DrawTextureRec(Texture2D texture, Rectangle source, Vector2 position, Color tint) {
DrawTextureRec(texture, source, position, tint);
#ifdef DEBUG_MODE_ENABLED
#ifdef DEBUG
if (global_debug_mode) {
Rectangle debug = {position.x,position.y,source.width,source.height};
DrawRectangleLinesEx(debug, 1.0f, RED);
@ -96,7 +68,7 @@ inline void D_DrawTextureRec(Texture2D texture, Rectangle source, Vector2 positi
inline void D_DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint) {
DrawTexturePro(texture, source, dest, origin, rotation, tint);
#ifdef DEBUG_MODE_ENABLED
#ifdef DEBUG
if (global_debug_mode) {
Rectangle debug = {dest.x-origin.x,dest.y-origin.y,dest.width,dest.height};
DrawRectangleLinesEx(debug, 1.0f, RED);

View File

@ -3,14 +3,13 @@
#include <sys/stat.h>
#include <cstdio>
struct GameState;
#include "game.h"
struct GameApi {
struct GameState *(*init)();
GameState *(*init)();
GameState *(*init_state)();
void (*step) (GameState *state);
void (*finalize) (GameState *state);
void (*reload) (GameState *state);
void (*unload) (GameState *state);
void (*cleanup) (GameState *state);
bool (*window_should_close)();
};
@ -38,20 +37,21 @@ bool load_game_api(Game* game) {
return false;
}
if (game->gamelib_handle) {
dlclose(game->gamelib_handle);
}
game->gamelib_handle = dlopen(lib_path, RTLD_NOW);
if (!game->gamelib_handle) {
void *gamelib_handle = dlopen(lib_path, RTLD_NOW);
if (!gamelib_handle) {
printf("Failed to load game.so: %s\n", dlerror());
return false;
}
if (game->gamelib_handle) {
dlclose(game->gamelib_handle);
}
game->gamelib_handle = gamelib_handle;
game->api.init = (GameState*(*)())dlsym(game->gamelib_handle, "game_init");
game->api.init_state = (GameState*(*)())dlsym(game->gamelib_handle, "game_init_state");
game->api.step = (void(*)(GameState*))dlsym(game->gamelib_handle, "game_step");
game->api.finalize = (void(*)(GameState*))dlsym(game->gamelib_handle, "game_cleanup");
game->api.cleanup = (void(*)(GameState*))dlsym(game->gamelib_handle, "game_cleanup");
game->api.window_should_close = (bool(*)())dlsym(game->gamelib_handle, "window_should_close");
game->last_write_time = write_time;
@ -60,7 +60,7 @@ bool load_game_api(Game* game) {
}
int main(void) {
Game game;
Game game = {};
load_game_api(&game);
@ -70,9 +70,14 @@ int main(void) {
// float dt = GetFrameTime();
load_game_api(&game);
if (game.state->should_reset) {
delete game.state;
game.state = game.api.init_state();
}
game.api.step(game.state);
}
game.api.finalize(game.state);
game.api.cleanup(game.state);
return 0;
}

View File

@ -15,6 +15,7 @@ struct SpriteAnimationPlayback {
u8 total_frames;
u8 current_frame;
u8 loop;
u8 finished;
};
void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimationPlayback *playback);
@ -27,6 +28,7 @@ inline void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimation
playback->row = animation;
playback->total_frames = anims[animation].total_frames;
playback->loop = anims[animation].loop;
playback->finished = false;
}
void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len) {
@ -41,11 +43,12 @@ void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len) {
if (playback->loop) {
playback->current_frame = 0;
}
playback->finished = true;
}
}
}
}
inline bool IsAnimationFinished(SpriteAnimationPlayback playback) {
return playback.current_frame == playback.total_frames;
return playback.finished;
}