Skip to content

Commit

Permalink
clickable component + fixed gui render state
Browse files Browse the repository at this point in the history
  • Loading branch information
LucAlexander committed Jul 20, 2022
1 parent 224e095 commit e85d19e
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 4 deletions.
3 changes: 3 additions & 0 deletions entities.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,6 @@ uint32_t entity_furthest_n(entity_data* d, v2 pos, uint8_t exact, uint32_t n, ui
return entity_furthest_mask_n(d, pos, exact, n, mask);
}

void entity_add_flag(entity_data* d, uint32_t eid, uint32_t flag){
d->flags[eid] = bit_on(d->flags[eid], flag);
}
4 changes: 3 additions & 1 deletion entities.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ HASHMAP(mu32u32_t, uint32_t, uint32_t)
struct v2;

typedef enum FLAGS{
ENTITY_DEACTIVATED=1
ENTITY_DEACTIVATED=1,
ENTITY_GUI=2
}FLAGS;

typedef struct entity_data{
Expand All @@ -36,6 +37,7 @@ void* component_get(entity_data* d, uint32_t eid, uint32_t cid);
void component_add(entity_data* d, uint32_t eid, uint32_t cid, void* data);
void component_remove(entity_data* d, uint32_t eid, uint32_t cid);

void entity_add_flag(entity_data* d, uint32_t eid, uint32_t flag);
uint8_t entity_active(entity_data* d, uint32_t eid);
uint8_t entity_exists_mask(entity_data* d, uint8_t exact, uint64_t mask);
uint8_t entity_exists(entity_data* d, uint8_t exact, uint32_t n, ...);
Expand Down
Binary file modified template/spr/pon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion xi.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,15 @@ void std_systems(program_state* state){
system_add(state, system_init(behavior_s, 1, BEHAVIOR_C), XI_STATE_UPDATE);
system_add(state, system_init(repeater_s, 1, REPEATER_C), XI_STATE_UPDATE);
system_add(state, system_init(animate_s, 2, BLITABLE_C, ANIMATOR_C), XI_STATE_UPDATE);
system_add(state, system_init(clickable_s, 2, POSITION_C, CLICKABLE_C), XI_STATE_UPDATE);

system_add(state, system_init(blitable_s, 2, POSITION_C, BLITABLE_C), XI_STATE_RENDER);
system_t blit = system_init(blitable_s, 2, POSITION_C, BLITABLE_C);
system_add_filter(&blit, 1, ENTITY_GUI);
system_add(state, blit, XI_STATE_RENDER);

system_t guiblit = system_init(blitable_s, 2, POSITION_C, BLITABLE_C);
system_add_requirement(&guiblit, 1, ENTITY_GUI);
system_add(state, guiblit, XI_STATE_RENDER_GUI);
}

void xi_run_system_group(program_state* state, uint32_t group, uint16_t layer){
Expand Down Expand Up @@ -155,7 +162,10 @@ void do_frame_try(program_state* state){
renderClear(&state->graphics);
renderSetColor(&state->graphics, 0, 0, 0, 255);
run_render_systems(state, XI_STATE_RENDER);
view world = renderGetView(&state->graphics);
renderSetViewAbsolute(&state->graphics);
run_render_systems(state, XI_STATE_RENDER_GUI);
renderSetView(&state->graphics, world);
renderFlip(&state->graphics);
tick_reset(state);
}
Expand Down
9 changes: 9 additions & 0 deletions xi_components.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ void repeater_t_init(repeater_t* wrapper, void f(SYSTEM_ARG_REQUIREMENTS), uint3
wrapper->trigger_count = count;
wrapper->destroy_after = destruct;
}

void clickable_t_init(clickable_t* clicker, void f(SYSTEM_ARG_REQUIREMENTS), int32_t recharge_time, uint32_t w, uint32_t h){
clicker->f = f;
clicker->recharge_counter = 0;
clicker->recharge_time = recharge_time;
clicker->toggle = 0;
clicker->w = w;
clicker->h = h;
}
17 changes: 15 additions & 2 deletions xi_components.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
BLITABLE_C,/*Blitable*/\
BEHAVIOR_C,/*logic_t*/\
REPEATER_C,/*repeater_t*/\
ANIMATOR_C/*animator_t*/
ANIMATOR_C,/*animator_t*/\
CLICKABLE_C/*clickable_t*/

#define COMPONENT_SIZES\
sizeof(v2),\
sizeof(v2),\
sizeof(Blitable),\
sizeof(logic_t),\
sizeof(repeater_t),\
sizeof(animator_t)\
sizeof(animator_t),\
sizeof(clickable_t)\
USER_COMPONENT_SIZES

typedef enum COMPONENTS{
Expand All @@ -40,4 +42,15 @@ typedef struct repeater_t{

void repeater_t_init(repeater_t* wrapper, void f(SYSTEM_ARG_REQUIREMENTS), uint32_t interval_time, uint32_t count, uint8_t destroy);

typedef struct clickable_t{
void (*f)(SYSTEM_ARG_REQUIREMENTS);
int32_t recharge_counter;
int32_t recharge_time;
uint8_t toggle;
uint32_t w;
uint32_t h;
}clickable_t;

void clickable_t_init(clickable_t* clicker, void f(SYSTEM_ARG_REQUIREMENTS), int32_t recharge_time, uint32_t w, uint32_t h);

#endif
24 changes: 24 additions & 0 deletions xi_systems.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ SYSTEM(repeater_s){
}
}

SYSTEM(clickable_s){
ARG(v2* position, POSITION_C);
ARG(clickable_t* button, CLICKABLE_C);
if (button->recharge_counter > 0){
button->recharge_counter -= xi->ticks;
}
v2 mouse = mousePos(xi->user_input);
v4 bounds = {
position->x,
position->y,
button->w,
button->h
};
if (
pointInRectV2(mouse, bounds) &&
mousePressed(xi->user_input, 1) &&
button->recharge_counter <= 0
){
button->toggle = !button->toggle;
button->recharge_counter = button->recharge_time;
button->f(SYSTEM_ARGS);
}
}

SYSTEM(animate_s){
ARG(Blitable* sprite, BLITABLE_C);
ARG(animator_t* animation, ANIMATOR_C);
Expand Down
1 change: 1 addition & 0 deletions xi_systems.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ SYSTEM(blitable_s);

SYSTEM(behavior_s);
SYSTEM(repeater_s);
SYSTEM(clickable_s);

SYSTEM(animate_s);

Expand Down

0 comments on commit e85d19e

Please sign in to comment.