diff --git a/CMakeLists.txt b/CMakeLists.txt index d09ece2..9ca1cb6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,6 +72,7 @@ add_executable(breakhack src/gui_button src/particle_engine src/menu + src/collisions ) target_link_libraries(breakhack diff --git a/src/collisions.c b/src/collisions.c new file mode 100644 index 0000000..72777dc --- /dev/null +++ b/src/collisions.c @@ -0,0 +1,8 @@ +#include "collisions.h" + +bool +position_in_rect(Position *p, SDL_Rect *r) +{ + return r->x <= p->x && r->x + r->w >= p->x && + r->y <= p->y && r->y + r->h >= p->y; +} diff --git a/src/collisions.h b/src/collisions.h new file mode 100644 index 0000000..02ff9cf --- /dev/null +++ b/src/collisions.h @@ -0,0 +1,12 @@ +#ifndef COLLISIONS_H_ +#define COLLISIONS_H_ + +#include +#include + +#include "position.h" + +bool +position_in_rect(Position*, SDL_Rect*); + +#endif // COLLISIONS_H_ diff --git a/src/gui_button.c b/src/gui_button.c index 676d7c5..bd691c2 100644 --- a/src/gui_button.c +++ b/src/gui_button.c @@ -1,6 +1,7 @@ #include #include "util.h" #include "gui_button.h" +#include "collisions.h" GuiButton * gui_button_create(SDL_Rect area, void (*event)(void*), void *usrdata) @@ -16,26 +17,21 @@ gui_button_create(SDL_Rect area, void (*event)(void*), void *usrdata) void gui_button_check_pointer(GuiButton *button, Pointer *pointer) { - SDL_Rect *r = &button->area; - Position *p = &pointer->sprite->pos; - - button->hover = r->x <= p->x && r->x + r->w >= p->x && - r->y <= p->y && r->y + r->h >= p->y; - + button->hover = position_in_rect(&pointer->sprite->pos, &button->area); pointer_toggle_clickable_pointer(pointer, button->hover); } void gui_button_handle_event(GuiButton *button, SDL_Event *event) { - if (event->type != SDL_MOUSEBUTTONDOWN) - return; + if (event->type == SDL_MOUSEBUTTONDOWN) { - if (event->button.button != SDL_BUTTON_LEFT) - return; + if (event->button.button != SDL_BUTTON_LEFT) + return; - if (button->hover) - button->event(button->usrdata); + if (button->hover) + button->event(button->usrdata); + } } void diff --git a/src/menu.c b/src/menu.c index c20beee..150a0a6 100644 --- a/src/menu.c +++ b/src/menu.c @@ -3,12 +3,13 @@ #include "util.h" #include "sprite.h" #include "defines.h" +#include "gui_button.h" typedef struct MenuItems_t { Sprite *sprite; Sprite *hsprite; + GuiButton *button; bool selected; - void (*onClick)(void); } MenuItem; Menu * @@ -23,11 +24,22 @@ menu_create(void) void menu_handle_event(Menu *m, SDL_Event *event) { + UNUSED(m); + LinkedList *items; + if (event->type != SDL_KEYDOWN && event->type != SDL_MOUSEMOTION) { return; } - error("Menu events need implementation still"); - UNUSED(m); + + items = m->items; + if (event->type == SDL_MOUSEMOTION) { + while (items) { + MenuItem *item = items->data; + items = items->next; + gui_button_handle_event(item->button, event); + item->selected = item->button->hover; + } + } } void @@ -37,7 +49,14 @@ menu_item_add(Menu *m, Sprite *s1, Sprite *s2) item->sprite = s1; item->hsprite = s2; item->selected = false; - item->onClick = NULL; + + SDL_Rect area = { + item->sprite->pos.x, + item->sprite->pos.y, + item->sprite->textures[0]->dim.width, + item->sprite->textures[0]->dim.height + }; + item->button = gui_button_create(area, NULL, NULL); linkedlist_append(&m->items, item); } @@ -48,7 +67,10 @@ menu_render(Menu *m, Camera *cam) while (items) { MenuItem *item = items->data; - sprite_render(item->sprite, cam); + if (item->selected) + sprite_render(item->hsprite, cam); + else + sprite_render(item->sprite, cam); items = items->next; } } @@ -56,7 +78,11 @@ menu_render(Menu *m, Camera *cam) static void menu_item_destroy(MenuItem *item) { - sprite_destroy(item->sprite); + if (item->sprite) + sprite_destroy(item->sprite); + if (item->hsprite) + sprite_destroy(item->hsprite); + gui_button_destroy(item->button); free(item); }