Compare commits
4 Commits
fd4f8ffbb1
...
78b4b739e6
| Author | SHA1 | Date | |
|---|---|---|---|
| 78b4b739e6 | |||
| 7e1748d3b4 | |||
| 65aafd0052 | |||
| ac4d458938 |
9
Makefile
9
Makefile
@ -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
|
LDFLAGS=-L./lib -lraylib -lm -ldl
|
||||||
INCLUDES=-I./include
|
INCLUDES=-I./include
|
||||||
CC=g++
|
CC=g++
|
||||||
|
|
||||||
.PHONY: build clean run all
|
.PHONY: build clean run all
|
||||||
|
|
||||||
all: main game.so
|
all: game.so main
|
||||||
|
|
||||||
main: main.cpp game.h Makefile
|
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
|
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
|
mv game.so.tmp game.so
|
||||||
|
|
||||||
run: all
|
run: all
|
||||||
|
|||||||
114
game.cpp
114
game.cpp
@ -13,15 +13,6 @@ using namespace std;
|
|||||||
#include "game_data.h"
|
#include "game_data.h"
|
||||||
|
|
||||||
GameState *InitState() {
|
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 assets = {};
|
||||||
assets.textures = (Texture*)malloc(sizeof(Texture2D) * TEXTURES_BUF_SIZE);
|
assets.textures = (Texture*)malloc(sizeof(Texture2D) * TEXTURES_BUF_SIZE);
|
||||||
assets.textures[TEX_GROUND] = LoadTexture("./assets/Terrain/Ground/Tilemap_Flat.png");
|
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");
|
assets.textures[TEX_TARGET_RETICLE] = LoadTexture("./assets/UI/Pointers/02.png");
|
||||||
|
|
||||||
auto game = new GameState;
|
auto game = new GameState;
|
||||||
|
game->should_reset = false;
|
||||||
game->assets = assets;
|
game->assets = assets;
|
||||||
// TODO: Probably want to use idiomatic C++ here?
|
// TODO: Probably want to use idiomatic C++ here?
|
||||||
game->knights = (Knight*)calloc(MAX_KNIGHTS, sizeof(Knight));
|
game->knights = (Knight*)calloc(MAX_KNIGHTS, sizeof(Knight));
|
||||||
@ -42,7 +34,7 @@ GameState *InitState() {
|
|||||||
game->knights[i].position = {rand_x,rand_y};
|
game->knights[i].position = {rand_x,rand_y};
|
||||||
|
|
||||||
PlayAnimation(ANIM_KNIGHT_IDLE, knight_anims, &game->anim_playbacks[i]);
|
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->anim_playbacks[i].current_frame = rand_frame;
|
||||||
}
|
}
|
||||||
game->entity_count = entities;
|
game->entity_count = entities;
|
||||||
@ -53,7 +45,7 @@ GameState *InitState() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
GameState *Init() {
|
GameState *Init() {
|
||||||
SetTraceLogLevel(4);
|
SetTraceLogLevel(LOG_WARNING);
|
||||||
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tiny Knights");
|
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tiny Knights");
|
||||||
|
|
||||||
int monitor = GetCurrentMonitor();
|
int monitor = GetCurrentMonitor();
|
||||||
@ -72,51 +64,17 @@ GameState *Init() {
|
|||||||
|
|
||||||
void Input(GameState *state) {
|
void Input(GameState *state) {
|
||||||
if (IsMouseButtonPressed(0)) {
|
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)) {
|
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->selection_mouse_start_pos = optional<Point>{};
|
||||||
|
state->drag_camera_start_pos = optional<Point>{};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsMouseButtonPressed(1)) {
|
if (IsMouseButtonPressed(1)) {
|
||||||
@ -134,32 +92,22 @@ void Input(GameState *state) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f32 cam_speed = 5.0;
|
if (IsKeyPressed(KEY_SPACE)) {
|
||||||
if (IsKeyDown(KEY_RIGHT)) {
|
for (int i = 0; i < state->entity_count; i++) {
|
||||||
state->camera.target.x += cam_speed;
|
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)) {
|
if (IsKeyPressed(KEY_F1)) {
|
||||||
global_debug_mode = !global_debug_mode;
|
global_debug_mode = !global_debug_mode;
|
||||||
}
|
}
|
||||||
if (IsKeyPressed(KEY_F2)) {
|
if (IsKeyPressed(KEY_F2)) {
|
||||||
auto new_state = InitState();
|
state->should_reset = true;
|
||||||
*state = *new_state;
|
printf("Resetting GameState\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,7 +117,6 @@ void Update(GameState *state) {
|
|||||||
Point start_pos = state->selection_mouse_start_pos.value();
|
Point start_pos = state->selection_mouse_start_pos.value();
|
||||||
Point current_pos = GetScreenToWorld2D(GetMousePosition(), state->camera);
|
Point current_pos = GetScreenToWorld2D(GetMousePosition(), state->camera);
|
||||||
|
|
||||||
if (Vector2DistanceSqr(current_pos, start_pos) >= 100.0f) {
|
|
||||||
f32 width = current_pos.x - start_pos.x;
|
f32 width = current_pos.x - start_pos.x;
|
||||||
f32 height = current_pos.y - start_pos.y;
|
f32 height = current_pos.y - start_pos.y;
|
||||||
f32 x = width >= 0.0f ? start_pos.x : current_pos.x;
|
f32 x = width >= 0.0f ? start_pos.x : current_pos.x;
|
||||||
@ -182,10 +129,12 @@ void Update(GameState *state) {
|
|||||||
knight_colrect_select.width,
|
knight_colrect_select.width,
|
||||||
knight_colrect_select.height,
|
knight_colrect_select.height,
|
||||||
};
|
};
|
||||||
state->knights[i].selected =
|
state->knights[i].selected = CheckCollisionRecs(rect, mouse_select_area);
|
||||||
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;
|
float dt = 0.33;
|
||||||
const float cam_move_speed = 1050.0f * dt;
|
const float cam_move_speed = 1050.0f * dt;
|
||||||
@ -234,6 +183,7 @@ void Update(GameState *state) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i == 0) continue;
|
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
|
// Sort the entities by y position using Insertion Sort
|
||||||
int j = i;
|
int j = i;
|
||||||
while (j > 0 && state->knights[j].position.y < state->knights[j - 1].position.y) {
|
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)};
|
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(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.width,
|
||||||
knight_colrect_select.height,
|
knight_colrect_select.height,
|
||||||
};
|
};
|
||||||
// Color color = game->selected_knight == NULL ? RED : GREEN;
|
|
||||||
DrawRectangleLinesEx(knight_col_area, 2.0f, GREEN);
|
DrawRectangleLinesEx(knight_col_area, 2.0f, GREEN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -334,7 +283,12 @@ extern "C" {
|
|||||||
return Init();
|
return Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GameState* game_init_state() {
|
||||||
|
return InitState();
|
||||||
|
}
|
||||||
|
|
||||||
void game_step(GameState *state) {
|
void game_step(GameState *state) {
|
||||||
|
try {
|
||||||
Input(state);
|
Input(state);
|
||||||
Update(state);
|
Update(state);
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
@ -351,8 +305,14 @@ extern "C" {
|
|||||||
}
|
}
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
catch (const exception& e) {
|
||||||
|
printf("Exception: %s\n", e.what());
|
||||||
|
TraceLog(LOG_ERROR, "Exception: %s", e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void game_cleanup(GameState *state) {
|
void game_cleanup(GameState *state) {
|
||||||
|
// free(state->assets.textures);
|
||||||
delete state;
|
delete state;
|
||||||
printf("Cleaning Up\n");
|
printf("Cleaning Up\n");
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
|
|||||||
13
game.h
13
game.h
@ -2,6 +2,7 @@
|
|||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include "lib.h"
|
#include "lib.h"
|
||||||
#include "sprites.h"
|
#include "sprites.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
@ -13,11 +14,9 @@ using namespace std;
|
|||||||
#define SCREEN_WIDTH 1300
|
#define SCREEN_WIDTH 1300
|
||||||
#define SCREEN_HEIGHT 1080
|
#define SCREEN_HEIGHT 1080
|
||||||
|
|
||||||
#define DEBUG_MODE_ENABLED
|
|
||||||
|
|
||||||
bool global_debug_mode = false;
|
bool global_debug_mode = false;
|
||||||
|
|
||||||
enum class KnightState : u8 {
|
enum class KnightState {
|
||||||
IDLE = 0,
|
IDLE = 0,
|
||||||
RUNNING = 1,
|
RUNNING = 1,
|
||||||
ATTACKING = 2,
|
ATTACKING = 2,
|
||||||
@ -48,6 +47,7 @@ struct Assets {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct GameState {
|
struct GameState {
|
||||||
|
bool should_reset;
|
||||||
Camera2D camera;
|
Camera2D camera;
|
||||||
int frame_count;
|
int frame_count;
|
||||||
Point camera_position;
|
Point camera_position;
|
||||||
@ -55,13 +55,16 @@ struct GameState {
|
|||||||
optional<Point> selected_point;
|
optional<Point> selected_point;
|
||||||
Knight *knights;
|
Knight *knights;
|
||||||
SpriteAnimationPlayback *anim_playbacks;
|
SpriteAnimationPlayback *anim_playbacks;
|
||||||
Knight *selected_knights;
|
|
||||||
|
|
||||||
optional<Point> selection_mouse_start_pos;
|
optional<Point> selection_mouse_start_pos;
|
||||||
|
|
||||||
int entity_count;
|
int entity_count;
|
||||||
Assets assets;
|
Assets assets;
|
||||||
|
|
||||||
|
optional<Point> drag_camera_start_pos;
|
||||||
|
|
||||||
|
Point camera_target_start_pos;
|
||||||
|
|
||||||
~GameState() {
|
~GameState() {
|
||||||
free(knights);
|
free(knights);
|
||||||
free(anim_playbacks);
|
free(anim_playbacks);
|
||||||
@ -69,6 +72,6 @@ struct GameState {
|
|||||||
UnloadTexture(assets.textures[i]);
|
UnloadTexture(assets.textures[i]);
|
||||||
}
|
}
|
||||||
free(assets.textures);
|
free(assets.textures);
|
||||||
printf("I freed the stuff?\n");
|
printf("Freeing GameState memory\n");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
64
game_data.h
64
game_data.h
@ -14,13 +14,6 @@
|
|||||||
#define ANIM_KNIGHT_ATTACK_BACK1 6
|
#define ANIM_KNIGHT_ATTACK_BACK1 6
|
||||||
#define ANIM_KNIGHT_ATTACK_BACK2 7
|
#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 u16 knight_sprite_size = 192;
|
||||||
const Vector2 knight_origin = {knight_sprite_size / 2, knight_sprite_size / 2 + 35};
|
const Vector2 knight_origin = {knight_sprite_size / 2, knight_sprite_size / 2 + 35};
|
||||||
const Size knight_col_size = {50, 75};
|
const Size knight_col_size = {50, 75};
|
||||||
@ -32,52 +25,13 @@ const Rectangle knight_colrect_select = {
|
|||||||
knight_col_size.height,
|
knight_col_size.height,
|
||||||
};
|
};
|
||||||
|
|
||||||
SpriteAnimation knight_idle = {
|
SpriteAnimation knight_anims[8] = {
|
||||||
.loop = true,
|
{.loop = true, .total_frames = 6}, // ANIM_KNIGHT_IDLE
|
||||||
.total_frames = 6,
|
{.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
34
lib.h
@ -17,7 +17,6 @@ typedef float f32;
|
|||||||
typedef double f64;
|
typedef double f64;
|
||||||
typedef uintptr_t uptr;
|
typedef uintptr_t uptr;
|
||||||
typedef char sbyte;
|
typedef char sbyte;
|
||||||
typedef ptrdiff_t size;
|
|
||||||
typedef size_t usize;
|
typedef size_t usize;
|
||||||
|
|
||||||
typedef Vector2 Point;
|
typedef Vector2 Point;
|
||||||
@ -48,7 +47,7 @@ extern "C" {
|
|||||||
extern bool global_debug_mode;
|
extern bool global_debug_mode;
|
||||||
inline void D_DrawTextureV(Texture2D texture, Vector2 position, Color tint) {
|
inline void D_DrawTextureV(Texture2D texture, Vector2 position, Color tint) {
|
||||||
DrawTextureV(texture, position, tint);
|
DrawTextureV(texture, position, tint);
|
||||||
#ifdef DEBUG_MODE_ENABLED
|
#ifdef DEBUG
|
||||||
if (global_debug_mode) {
|
if (global_debug_mode) {
|
||||||
Rectangle debug = {position.x,position.y,(f32)texture.width,(f32)texture.height};
|
Rectangle debug = {position.x,position.y,(f32)texture.width,(f32)texture.height};
|
||||||
DrawRectangleLinesEx(debug, 1.0f, RED);
|
DrawRectangleLinesEx(debug, 1.0f, RED);
|
||||||
@ -56,37 +55,10 @@ inline void D_DrawTextureV(Texture2D texture, Vector2 position, Color tint) {
|
|||||||
#endif
|
#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) {
|
inline void D_DrawTextureRec(Texture2D texture, Rectangle source, Vector2 position, Color tint) {
|
||||||
DrawTextureRec(texture, source, position, tint);
|
DrawTextureRec(texture, source, position, tint);
|
||||||
#ifdef DEBUG_MODE_ENABLED
|
#ifdef DEBUG
|
||||||
if (global_debug_mode) {
|
if (global_debug_mode) {
|
||||||
Rectangle debug = {position.x,position.y,source.width,source.height};
|
Rectangle debug = {position.x,position.y,source.width,source.height};
|
||||||
DrawRectangleLinesEx(debug, 1.0f, RED);
|
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) {
|
inline void D_DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint) {
|
||||||
DrawTexturePro(texture, source, dest, origin, rotation, tint);
|
DrawTexturePro(texture, source, dest, origin, rotation, tint);
|
||||||
#ifdef DEBUG_MODE_ENABLED
|
#ifdef DEBUG
|
||||||
if (global_debug_mode) {
|
if (global_debug_mode) {
|
||||||
Rectangle debug = {dest.x-origin.x,dest.y-origin.y,dest.width,dest.height};
|
Rectangle debug = {dest.x-origin.x,dest.y-origin.y,dest.width,dest.height};
|
||||||
DrawRectangleLinesEx(debug, 1.0f, RED);
|
DrawRectangleLinesEx(debug, 1.0f, RED);
|
||||||
|
|||||||
35
main.cpp
35
main.cpp
@ -3,14 +3,13 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
struct GameState;
|
#include "game.h"
|
||||||
|
|
||||||
struct GameApi {
|
struct GameApi {
|
||||||
struct GameState *(*init)();
|
GameState *(*init)();
|
||||||
|
GameState *(*init_state)();
|
||||||
void (*step) (GameState *state);
|
void (*step) (GameState *state);
|
||||||
void (*finalize) (GameState *state);
|
void (*cleanup) (GameState *state);
|
||||||
void (*reload) (GameState *state);
|
|
||||||
void (*unload) (GameState *state);
|
|
||||||
bool (*window_should_close)();
|
bool (*window_should_close)();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -38,20 +37,21 @@ bool load_game_api(Game* game) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (game->gamelib_handle) {
|
void *gamelib_handle = dlopen(lib_path, RTLD_NOW);
|
||||||
dlclose(game->gamelib_handle);
|
if (!gamelib_handle) {
|
||||||
}
|
|
||||||
|
|
||||||
game->gamelib_handle = dlopen(lib_path, RTLD_NOW);
|
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 = (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.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->api.window_should_close = (bool(*)())dlsym(game->gamelib_handle, "window_should_close");
|
||||||
|
|
||||||
game->last_write_time = write_time;
|
game->last_write_time = write_time;
|
||||||
@ -60,7 +60,7 @@ bool load_game_api(Game* game) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
Game game;
|
Game game = {};
|
||||||
|
|
||||||
load_game_api(&game);
|
load_game_api(&game);
|
||||||
|
|
||||||
@ -70,9 +70,14 @@ int main(void) {
|
|||||||
// float dt = GetFrameTime();
|
// float dt = GetFrameTime();
|
||||||
load_game_api(&game);
|
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.step(game.state);
|
||||||
}
|
}
|
||||||
|
|
||||||
game.api.finalize(game.state);
|
game.api.cleanup(game.state);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,7 @@ struct SpriteAnimationPlayback {
|
|||||||
u8 total_frames;
|
u8 total_frames;
|
||||||
u8 current_frame;
|
u8 current_frame;
|
||||||
u8 loop;
|
u8 loop;
|
||||||
|
u8 finished;
|
||||||
};
|
};
|
||||||
|
|
||||||
void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimationPlayback *playback);
|
void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimationPlayback *playback);
|
||||||
@ -27,6 +28,7 @@ inline void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimation
|
|||||||
playback->row = animation;
|
playback->row = animation;
|
||||||
playback->total_frames = anims[animation].total_frames;
|
playback->total_frames = anims[animation].total_frames;
|
||||||
playback->loop = anims[animation].loop;
|
playback->loop = anims[animation].loop;
|
||||||
|
playback->finished = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len) {
|
void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len) {
|
||||||
@ -41,11 +43,12 @@ void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len) {
|
|||||||
if (playback->loop) {
|
if (playback->loop) {
|
||||||
playback->current_frame = 0;
|
playback->current_frame = 0;
|
||||||
}
|
}
|
||||||
|
playback->finished = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool IsAnimationFinished(SpriteAnimationPlayback playback) {
|
inline bool IsAnimationFinished(SpriteAnimationPlayback playback) {
|
||||||
return playback.current_frame == playback.total_frames;
|
return playback.finished;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user