Working on menus. Still pretty broken game.
This commit is contained in:
parent
3fe549e6fe
commit
31e006b69f
|
@ -72,6 +72,7 @@ add_executable(breakhack
|
|||
src/gui_button
|
||||
src/particle_engine
|
||||
src/menu
|
||||
src/collisions
|
||||
)
|
||||
|
||||
target_link_libraries(breakhack
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef COLLISIONS_H_
|
||||
#define COLLISIONS_H_
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "position.h"
|
||||
|
||||
bool
|
||||
position_in_rect(Position*, SDL_Rect*);
|
||||
|
||||
#endif // COLLISIONS_H_
|
|
@ -1,6 +1,7 @@
|
|||
#include <stdlib.h>
|
||||
#include "util.h"
|
||||
#include "gui_button.h"
|
||||
#include "collisions.h"
|
||||
|
||||
GuiButton *
|
||||
gui_button_create(SDL_Rect area, void (*event)(void*), void *usrdata)
|
||||
|
@ -16,20 +17,14 @@ 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;
|
||||
|
@ -37,6 +32,7 @@ gui_button_handle_event(GuiButton *button, SDL_Event *event)
|
|||
if (button->hover)
|
||||
button->event(button->usrdata);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
gui_button_destroy(GuiButton *button)
|
||||
|
|
34
src/menu.c
34
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,6 +67,9 @@ menu_render(Menu *m, Camera *cam)
|
|||
|
||||
while (items) {
|
||||
MenuItem *item = items->data;
|
||||
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)
|
||||
{
|
||||
if (item->sprite)
|
||||
sprite_destroy(item->sprite);
|
||||
if (item->hsprite)
|
||||
sprite_destroy(item->hsprite);
|
||||
gui_button_destroy(item->button);
|
||||
free(item);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue