diff --git a/entities.c b/entities.c index a1bb567..800acba 100644 --- a/entities.c +++ b/entities.c @@ -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); +} diff --git a/entities.h b/entities.h index 67eb478..5a3c718 100644 --- a/entities.h +++ b/entities.h @@ -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{ @@ -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, ...); diff --git a/template/spr/pon.png b/template/spr/pon.png index 7b2bde8..877b05e 100644 Binary files a/template/spr/pon.png and b/template/spr/pon.png differ diff --git a/xi.c b/xi.c index 38996ed..973ad66 100644 --- a/xi.c +++ b/xi.c @@ -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){ @@ -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); } diff --git a/xi_components.c b/xi_components.c index 428b376..d9a9430 100644 --- a/xi_components.c +++ b/xi_components.c @@ -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; +} diff --git a/xi_components.h b/xi_components.h index 896fca3..7f0294e 100644 --- a/xi_components.h +++ b/xi_components.h @@ -10,7 +10,8 @@ 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),\ @@ -18,7 +19,8 @@ sizeof(Blitable),\ sizeof(logic_t),\ sizeof(repeater_t),\ - sizeof(animator_t)\ + sizeof(animator_t),\ + sizeof(clickable_t)\ USER_COMPONENT_SIZES typedef enum COMPONENTS{ @@ -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 diff --git a/xi_systems.c b/xi_systems.c index 5b0e207..48eca88 100644 --- a/xi_systems.c +++ b/xi_systems.c @@ -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); diff --git a/xi_systems.h b/xi_systems.h index 0818d25..aab8e81 100644 --- a/xi_systems.h +++ b/xi_systems.h @@ -11,6 +11,7 @@ SYSTEM(blitable_s); SYSTEM(behavior_s); SYSTEM(repeater_s); +SYSTEM(clickable_s); SYSTEM(animate_s);