Working on menus. Still pretty broken game.

This commit is contained in:
Linus Probert 2018-02-09 06:53:06 +01:00
parent 3fe549e6fe
commit 31e006b69f
5 changed files with 61 additions and 18 deletions

View File

@ -72,6 +72,7 @@ add_executable(breakhack
src/gui_button src/gui_button
src/particle_engine src/particle_engine
src/menu src/menu
src/collisions
) )
target_link_libraries(breakhack target_link_libraries(breakhack

8
src/collisions.c Normal file
View File

@ -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;
}

12
src/collisions.h Normal file
View File

@ -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_

View File

@ -1,6 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "util.h" #include "util.h"
#include "gui_button.h" #include "gui_button.h"
#include "collisions.h"
GuiButton * GuiButton *
gui_button_create(SDL_Rect area, void (*event)(void*), void *usrdata) 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 void
gui_button_check_pointer(GuiButton *button, Pointer *pointer) gui_button_check_pointer(GuiButton *button, Pointer *pointer)
{ {
SDL_Rect *r = &button->area; button->hover = position_in_rect(&pointer->sprite->pos, &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;
pointer_toggle_clickable_pointer(pointer, button->hover); pointer_toggle_clickable_pointer(pointer, button->hover);
} }
void void
gui_button_handle_event(GuiButton *button, SDL_Event *event) gui_button_handle_event(GuiButton *button, SDL_Event *event)
{ {
if (event->type != SDL_MOUSEBUTTONDOWN) if (event->type == SDL_MOUSEBUTTONDOWN) {
return;
if (event->button.button != SDL_BUTTON_LEFT) if (event->button.button != SDL_BUTTON_LEFT)
return; return;
if (button->hover) if (button->hover)
button->event(button->usrdata); button->event(button->usrdata);
}
} }
void void

View File

@ -3,12 +3,13 @@
#include "util.h" #include "util.h"
#include "sprite.h" #include "sprite.h"
#include "defines.h" #include "defines.h"
#include "gui_button.h"
typedef struct MenuItems_t { typedef struct MenuItems_t {
Sprite *sprite; Sprite *sprite;
Sprite *hsprite; Sprite *hsprite;
GuiButton *button;
bool selected; bool selected;
void (*onClick)(void);
} MenuItem; } MenuItem;
Menu * Menu *
@ -23,11 +24,22 @@ menu_create(void)
void void
menu_handle_event(Menu *m, SDL_Event *event) menu_handle_event(Menu *m, SDL_Event *event)
{ {
UNUSED(m);
LinkedList *items;
if (event->type != SDL_KEYDOWN && event->type != SDL_MOUSEMOTION) { if (event->type != SDL_KEYDOWN && event->type != SDL_MOUSEMOTION) {
return; 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 void
@ -37,7 +49,14 @@ menu_item_add(Menu *m, Sprite *s1, Sprite *s2)
item->sprite = s1; item->sprite = s1;
item->hsprite = s2; item->hsprite = s2;
item->selected = false; 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); linkedlist_append(&m->items, item);
} }
@ -48,6 +67,9 @@ menu_render(Menu *m, Camera *cam)
while (items) { while (items) {
MenuItem *item = items->data; MenuItem *item = items->data;
if (item->selected)
sprite_render(item->hsprite, cam);
else
sprite_render(item->sprite, cam); sprite_render(item->sprite, cam);
items = items->next; items = items->next;
} }
@ -56,7 +78,11 @@ menu_render(Menu *m, Camera *cam)
static void static void
menu_item_destroy(MenuItem *item) menu_item_destroy(MenuItem *item)
{ {
if (item->sprite)
sprite_destroy(item->sprite); sprite_destroy(item->sprite);
if (item->hsprite)
sprite_destroy(item->hsprite);
gui_button_destroy(item->button);
free(item); free(item);
} }