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/particle_engine
src/menu
src/collisions
)
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 "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

View File

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