Skip to content

Commit

Permalink
fix torch
Browse files Browse the repository at this point in the history
  • Loading branch information
computermouth committed Nov 2, 2023
1 parent 978f196 commit 433cdf4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ IFLAGS += -I./external/lodepng -I./external/libdsa -I./external/mpack -I./extern
IFLAGS += -I./tools

# todo -Wextra
CFLAGS = -Wall -Wextra $(IFLAGS) $(shell sdl2-config --cflags) $(shell pkg-config --cflags SDL2_mixer SDL2_ttf) -std=c11 -pedantic
CFLAGS = -Wall -Wextra -D_FORTIFY_SOURCE=2 $(IFLAGS) $(shell sdl2-config --cflags) $(shell pkg-config --cflags SDL2_mixer SDL2_ttf) -std=c11 -pedantic
LFLAGS = -lm $(shell sdl2-config --libs) $(shell pkg-config --libs SDL2_mixer SDL2_ttf)

UNAME := $(shell uname)
Expand Down
13 changes: 3 additions & 10 deletions entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
#include "entity_particle.h"
#include "audio.h"

uint32_t no_idea_placeholder[] = {0};

animation_t default_anim[] = {
{
.frames = (uint32_t[]){ 0 },
.frames_ng = (animation_frame_t[]){ 0 },
.num_frames = 1,
.time = 1,
}
Expand Down Expand Up @@ -284,13 +282,8 @@ void entity_draw_model(entity_t * e) {
uint32_t frame_cur = 0;
uint32_t frame_next = 0;

if (e->_anim->frames_ng) {
frame_cur = e->_anim->frames_ng[(uint32_t)f % e->_anim->num_frames].id;
frame_next = e->_anim->frames_ng[(1 + (uint32_t)f) % e->_anim->num_frames].id;
} else {
frame_cur = e->_anim->frames[(uint32_t)f % e->_anim->num_frames];
frame_next = e->_anim->frames[(1 + (uint32_t)f) % e->_anim->num_frames];
}
frame_cur = e->_anim->frames_ng[(uint32_t)f % e->_anim->num_frames].id;
frame_next = e->_anim->frames_ng[(1 + (uint32_t)f) % e->_anim->num_frames].id;

// Swap frames if we're looping to the first frame again
if (frame_next < frame_cur) {
Expand Down
2 changes: 0 additions & 2 deletions entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ typedef struct {
float time;
uint32_t num_frames;
animation_frame_t * frames_ng;
uint32_t * frames;
// todo, remove ^
} animation_t;

typedef struct {
Expand Down
10 changes: 5 additions & 5 deletions entity_enemy.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ animation_t default_animations[] = {
{ // 0: Idle
.time = 1,
.num_frames = 1,
.frames = (uint32_t[]){0},
.frames_ng = (animation_frame_t[]){ 0 },
},
{ // 1: Walk
.time = 0.40f,
.num_frames = 4,
.frames = (uint32_t[]){1,2,3,4},
.frames_ng = (animation_frame_t[]){ 0 },
},
{ // 2: Run
.time = 0.20f,
.num_frames = 4,
.frames = (uint32_t[]){1,2,3,4},
.frames_ng = (animation_frame_t[]){ 0 },
},
{ // 3: Attack prepare
.time = 0.25f,
.num_frames = 4,
.frames = (uint32_t[]){0,5,5,5},
.frames_ng = (animation_frame_t[]){ 0 },
},
{ // 4: Attack
.time = 0.25f,
.num_frames = 4,
.frames = (uint32_t[]){5,0,0,0},
.frames_ng = (animation_frame_t[]){ 0 },
},
};

Expand Down
46 changes: 39 additions & 7 deletions entity_torch.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,33 @@
#include "map.h"
#include "render.h"

uint32_t torch_anim_frames[] = {0,1,2,1,2,0,0,1,2,1,2,1,2,0};
animation_t torch_animation = {
.time = 0.25,
.frames = torch_anim_frames,
.num_frames = sizeof(torch_anim_frames)/sizeof(torch_anim_frames[0]),
animation_t torch_animation[] = {
{ // 0: Idle
.time = .25,
.num_frames = 14,
.frames_ng = (animation_frame_t[]) {
{.name = "default"},
{.name = "torch1"},
{.name = "torch2"},
{.name = "torch1"},
{.name = "torch2"},
{.name = "default"},
{.name = "default"},
{.name = "torch1"},
{.name = "torch2"},
{.name = "torch1"},
{.name = "torch2"},
{.name = "torch1"},
{.name = "torch2"},
{.name = "default"},
},
}
};

// hack for caching parsed frame names per-map
static ref_entt_t * last_ref_entt = NULL;


void entity_torch_init(entity_t * e);
void entity_torch_update(entity_t * e);

Expand All @@ -28,8 +48,6 @@ void entity_torch_constructor(entity_t * e, vec3_t pos) {

void entity_torch_init(entity_t * e) {

e->_anim = &torch_animation;

e->p.x -= 16;
e->p.z -= 16;
e->_light_pos = e->p;
Expand All @@ -55,6 +73,20 @@ void entity_torch_init(entity_t * e) {

e->_light = 0;

entity_parse_animation_frames(
e->_params->entity_generic_params.ref_entt,
torch_animation,
sizeof(torch_animation)/sizeof(torch_animation[0]),
&last_ref_entt
);

e->_animation_collection = (animation_collection_t) {
.animations = torch_animation,
.num_animations = sizeof(torch_animation)/sizeof(torch_animation[0]),
};

e->_anim = &(torch_animation[0]);

entity_set_model(e);
}

Expand Down

0 comments on commit 433cdf4

Please sign in to comment.