diff --git a/CMakeLists.txt b/CMakeLists.txt index 8627f7a..4e6a430 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,12 @@ if (NOT WIN32) endif (NOT WIN32) if (NOT WIN32) - add_definitions("-std=gnu11 -pedantic -Wall -Wextra -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes") + add_definitions(-std=gnu11 + -pedantic -Wall -Wextra -Wshadow + -Wpointer-arith -Wcast-qual + -Wstrict-prototypes + -Wmissing-prototypes + ) endif (NOT WIN32) # PROGRAMS: @@ -62,6 +67,7 @@ add_executable(breakhack src/item src/item_builder src/pointer + src/gui_button ) target_link_libraries(breakhack diff --git a/src/gui_button.c b/src/gui_button.c new file mode 100644 index 0000000..424c9d0 --- /dev/null +++ b/src/gui_button.c @@ -0,0 +1,31 @@ +#include +#include "util.h" +#include "gui_button.h" + +GuiButton * +gui_button_create(SDL_Rect area) +{ + GuiButton *button = ec_malloc(sizeof(GuiButton)); + button->area = area; + button->hover = false; + return button; +} + +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; + + if (button->hover) + debug("Pointer is over button"); +} + +void +gui_button_destroy(GuiButton *button) +{ + free(button); +} diff --git a/src/gui_button.h b/src/gui_button.h new file mode 100644 index 0000000..7dd263f --- /dev/null +++ b/src/gui_button.h @@ -0,0 +1,24 @@ +#ifndef GUI_BUTTON_H_ +#define GUI_BUTTON_H_ + +#include +#include "pointer.h" +#include "sprite.h" +#include "linkedlist.h" +#include "camera.h" + +typedef struct GuiButton_t { + SDL_Rect area; + bool hover; +} GuiButton; + +GuiButton * +gui_button_create(SDL_Rect); + +void +gui_button_check_pointer(GuiButton*, Pointer*); + +void +gui_button_destroy(GuiButton*); + +#endif // GUI_BUTTON_H_ diff --git a/src/main.c b/src/main.c index 2b36e1a..c8e8ee9 100644 --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,7 @@ #include "util.h" #include "item_builder.h" #include "pointer.h" +#include "gui_button.h" static SDL_Window *gWindow = NULL; static SDL_Renderer *gRenderer = NULL; @@ -25,6 +26,7 @@ static Map *gMap = NULL; static RoomMatrix *gRoomMatrix = NULL; static Gui *gGui = NULL; static Pointer *gPointer = NULL; +static GuiButton *gButton = NULL; static unsigned int cLevel = 1; static double renderScale = 1.0; static GameState gGameState; @@ -134,6 +136,7 @@ bool init(void) gGui = gui_create(gRenderer); item_builder_init(gRenderer); gPointer = pointer_create(gRenderer); + gButton = gui_button_create((SDL_Rect) { 100, 100, 100, 100 }); } gGameState = PLAYING; @@ -165,6 +168,9 @@ bool handle_events(void) } pointer_handle_event(gPointer, &event); } + + gui_button_check_pointer(gButton, gPointer); + return quit; } @@ -292,6 +298,7 @@ void close(void) roommatrix_destroy(gRoomMatrix); gui_destroy(gGui); pointer_destroy(gPointer); + gui_button_destroy(gButton); item_builder_close(); SDL_DestroyRenderer(gRenderer); SDL_DestroyWindow(gWindow);