Implemented a button. Not quite usable yet though.

This commit is contained in:
Linus_Probert 2018-02-02 16:16:55 +01:00
parent 0564f6c3a2
commit c2266b892d
4 changed files with 69 additions and 1 deletions

View File

@ -35,7 +35,12 @@ if (NOT WIN32)
endif (NOT WIN32) endif (NOT WIN32)
if (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) endif (NOT WIN32)
# PROGRAMS: # PROGRAMS:
@ -62,6 +67,7 @@ add_executable(breakhack
src/item src/item
src/item_builder src/item_builder
src/pointer src/pointer
src/gui_button
) )
target_link_libraries(breakhack target_link_libraries(breakhack

31
src/gui_button.c Normal file
View File

@ -0,0 +1,31 @@
#include <stdlib.h>
#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);
}

24
src/gui_button.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef GUI_BUTTON_H_
#define GUI_BUTTON_H_
#include <SDL2/SDL.h>
#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_

View File

@ -17,6 +17,7 @@
#include "util.h" #include "util.h"
#include "item_builder.h" #include "item_builder.h"
#include "pointer.h" #include "pointer.h"
#include "gui_button.h"
static SDL_Window *gWindow = NULL; static SDL_Window *gWindow = NULL;
static SDL_Renderer *gRenderer = NULL; static SDL_Renderer *gRenderer = NULL;
@ -25,6 +26,7 @@ static Map *gMap = NULL;
static RoomMatrix *gRoomMatrix = NULL; static RoomMatrix *gRoomMatrix = NULL;
static Gui *gGui = NULL; static Gui *gGui = NULL;
static Pointer *gPointer = NULL; static Pointer *gPointer = NULL;
static GuiButton *gButton = NULL;
static unsigned int cLevel = 1; static unsigned int cLevel = 1;
static double renderScale = 1.0; static double renderScale = 1.0;
static GameState gGameState; static GameState gGameState;
@ -134,6 +136,7 @@ bool init(void)
gGui = gui_create(gRenderer); gGui = gui_create(gRenderer);
item_builder_init(gRenderer); item_builder_init(gRenderer);
gPointer = pointer_create(gRenderer); gPointer = pointer_create(gRenderer);
gButton = gui_button_create((SDL_Rect) { 100, 100, 100, 100 });
} }
gGameState = PLAYING; gGameState = PLAYING;
@ -165,6 +168,9 @@ bool handle_events(void)
} }
pointer_handle_event(gPointer, &event); pointer_handle_event(gPointer, &event);
} }
gui_button_check_pointer(gButton, gPointer);
return quit; return quit;
} }
@ -292,6 +298,7 @@ void close(void)
roommatrix_destroy(gRoomMatrix); roommatrix_destroy(gRoomMatrix);
gui_destroy(gGui); gui_destroy(gGui);
pointer_destroy(gPointer); pointer_destroy(gPointer);
gui_button_destroy(gButton);
item_builder_close(); item_builder_close();
SDL_DestroyRenderer(gRenderer); SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow); SDL_DestroyWindow(gWindow);