2018-02-16 18:11:26 +01:00
|
|
|
/*
|
|
|
|
* BreakHack - A dungeone crawler RPG
|
|
|
|
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-02-09 09:39:31 +01:00
|
|
|
#include <stdlib.h>
|
2018-02-08 17:01:38 +01:00
|
|
|
#include <stdbool.h>
|
2019-02-20 19:48:24 +01:00
|
|
|
#include <string.h>
|
2018-02-08 17:01:38 +01:00
|
|
|
#include "menu.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "sprite.h"
|
|
|
|
#include "defines.h"
|
2018-02-09 06:53:06 +01:00
|
|
|
#include "gui_button.h"
|
2018-02-12 10:55:36 +01:00
|
|
|
#include "keyboard.h"
|
2018-02-14 16:04:40 +01:00
|
|
|
#include "mixer.h"
|
2018-05-20 17:25:53 +02:00
|
|
|
#include "collisions.h"
|
2018-09-12 14:45:09 +02:00
|
|
|
#include "texturecache.h"
|
2018-02-08 17:01:38 +01:00
|
|
|
|
2018-09-11 15:32:33 +02:00
|
|
|
static SDL_Color C_MENU_DEFAULT = { 255, 255, 0, 255 };
|
|
|
|
static SDL_Color C_MENU_OUTLINE_DEFAULT = { 0, 0, 0, 255 };
|
|
|
|
static SDL_Color C_MENU_HOVER = { 255, 0, 0, 255 };
|
|
|
|
|
2018-09-12 14:45:09 +02:00
|
|
|
typedef struct MenuItem {
|
2018-02-08 17:01:38 +01:00
|
|
|
Sprite *sprite;
|
|
|
|
Sprite *hsprite;
|
2018-02-09 06:53:06 +01:00
|
|
|
GuiButton *button;
|
2018-02-08 17:01:38 +01:00
|
|
|
} MenuItem;
|
|
|
|
|
2018-10-26 18:37:50 +02:00
|
|
|
static void redraw_description(Menu *m, SDL_Renderer *renderer);
|
2018-10-22 09:04:16 +02:00
|
|
|
|
2018-02-08 17:01:38 +01:00
|
|
|
Menu *
|
|
|
|
menu_create(void)
|
|
|
|
{
|
|
|
|
Menu *menu = ec_malloc(sizeof(Menu));
|
|
|
|
menu->items = linkedlist_create();
|
2018-10-22 09:04:16 +02:00
|
|
|
menu->descriptions = linkedlist_create();
|
2018-02-08 17:01:38 +01:00
|
|
|
menu->selected = 0;
|
2018-10-22 09:04:16 +02:00
|
|
|
menu->menuDescription = sprite_create();
|
|
|
|
sprite_load_text_texture(menu->menuDescription,
|
|
|
|
"GUI/SDS_8x8.ttf",
|
|
|
|
0,
|
2018-10-22 13:56:59 +02:00
|
|
|
10,
|
2018-10-22 09:04:16 +02:00
|
|
|
1);
|
|
|
|
menu->menuDescription->fixed = true;
|
|
|
|
menu->menuDescription->pos = POS(20, SCREEN_HEIGHT - 20);
|
|
|
|
menu->menuDescription->hidden = true;
|
|
|
|
|
2018-02-08 17:01:38 +01:00
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
2018-09-11 15:32:33 +02:00
|
|
|
void
|
|
|
|
menu_create_text_menu(Menu **menu, TEXT_MENU_ITEM *menu_items, unsigned int size, SDL_Renderer *renderer)
|
|
|
|
{
|
|
|
|
if (*menu != NULL) {
|
|
|
|
menu_destroy(*menu);
|
|
|
|
*menu = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*menu = menu_create();
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < size; ++i) {
|
|
|
|
unsigned int hcenter;
|
|
|
|
|
|
|
|
Sprite *s1 = sprite_create();
|
|
|
|
sprite_load_text_texture(s1, "GUI/SDS_8x8.ttf", 0, 25, 2);
|
|
|
|
texture_load_from_text(s1->textures[0], menu_items[i].label,
|
|
|
|
C_MENU_DEFAULT, C_MENU_OUTLINE_DEFAULT, renderer);
|
|
|
|
|
|
|
|
hcenter = (SCREEN_WIDTH/2) - (s1->textures[0]->dim.width/2);
|
|
|
|
s1->pos = (Position) { (int) hcenter, (int) 200 + (i*50) };
|
|
|
|
s1->dim = s1->textures[0]->dim;
|
|
|
|
s1->fixed = true;
|
|
|
|
|
|
|
|
Sprite *s2 = sprite_create();
|
|
|
|
sprite_load_text_texture(s2, "GUI/SDS_8x8.ttf", 0, 25, 2);
|
|
|
|
texture_load_from_text(s2->textures[0], menu_items[i].label,
|
|
|
|
C_MENU_HOVER, C_MENU_OUTLINE_DEFAULT, renderer);
|
|
|
|
|
|
|
|
s2->pos = (Position) { (int) hcenter, (int) 200 + (i*50) };
|
|
|
|
s2->dim = s2->textures[0]->dim;
|
|
|
|
s2->fixed = true;
|
|
|
|
|
|
|
|
menu_item_add(*menu, s1, s2, menu_items[i].callback);
|
2018-10-22 09:04:16 +02:00
|
|
|
linkedlist_append(&(*menu)->descriptions, (void*) menu_items[i].description);
|
2018-09-11 15:32:33 +02:00
|
|
|
}
|
2018-10-26 18:37:50 +02:00
|
|
|
|
|
|
|
(*menu)->selected = 0;
|
|
|
|
redraw_description(*menu, renderer);
|
2018-09-11 15:32:33 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 14:45:09 +02:00
|
|
|
Menu *
|
2018-10-22 09:04:16 +02:00
|
|
|
menu_create_character_selector(void (*onCharacterSelect)(const char *), Camera *cam)
|
2018-09-12 14:45:09 +02:00
|
|
|
{
|
|
|
|
const char *spriteSheets[] = {
|
2018-09-12 20:56:50 +02:00
|
|
|
"Commissions/Warrior.png",
|
|
|
|
"Commissions/Rogue.png"
|
|
|
|
};
|
2018-09-12 14:45:09 +02:00
|
|
|
|
2018-09-15 11:01:35 +02:00
|
|
|
static char *callbackData[] = {
|
2018-09-12 20:56:50 +02:00
|
|
|
"warrior",
|
|
|
|
"rogue"
|
|
|
|
};
|
2018-09-12 14:45:09 +02:00
|
|
|
|
2018-10-22 09:04:16 +02:00
|
|
|
static char *descriptions[] = {
|
|
|
|
"Play as the warrior",
|
|
|
|
"Play as the rogue",
|
|
|
|
};
|
|
|
|
|
2018-09-12 20:56:50 +02:00
|
|
|
Menu *menu = menu_create();
|
2018-09-13 08:05:17 +02:00
|
|
|
int xoffset = 224;
|
2018-09-12 20:56:50 +02:00
|
|
|
for (size_t i = 0; i < 2; ++i) {
|
|
|
|
Sprite *s1 = sprite_create();
|
|
|
|
sprite_set_texture(s1, texturecache_add(spriteSheets[i]), 0);
|
2018-09-13 08:05:17 +02:00
|
|
|
s1->clip = CLIP16(0, 48);
|
2018-09-12 20:56:50 +02:00
|
|
|
s1->dim = DIM(64, 64);
|
2018-09-13 08:05:17 +02:00
|
|
|
s1->pos = POS(xoffset - 32, 256);
|
2018-10-15 22:19:23 +02:00
|
|
|
s1->fixed = true;
|
2018-09-12 14:45:09 +02:00
|
|
|
|
2018-09-12 20:56:50 +02:00
|
|
|
Sprite *s2 = sprite_create();
|
|
|
|
sprite_set_texture(s2, texturecache_add(spriteSheets[i]), 0);
|
2018-09-13 08:05:17 +02:00
|
|
|
s2->clip = CLIP16(0, 0);
|
2018-09-12 20:56:50 +02:00
|
|
|
s2->dim = DIM(64, 64);
|
2018-09-13 08:05:17 +02:00
|
|
|
s2->pos = POS(xoffset - 32, 256);
|
2018-10-15 22:19:23 +02:00
|
|
|
s2->fixed = true;
|
2018-09-12 20:56:50 +02:00
|
|
|
|
|
|
|
menu_item_add(menu, s1, s2, (void (*)(void *)) onCharacterSelect);
|
|
|
|
MenuItem *item = linkedlist_get(&menu->items, (Uint32) i);
|
|
|
|
item->button->usrdata = callbackData[i];
|
2018-09-13 08:05:17 +02:00
|
|
|
xoffset += 224;
|
2018-10-22 09:04:16 +02:00
|
|
|
|
|
|
|
linkedlist_append(&menu->descriptions, descriptions[i]);
|
2018-09-12 20:56:50 +02:00
|
|
|
}
|
2018-09-12 14:45:09 +02:00
|
|
|
|
2018-10-22 09:04:16 +02:00
|
|
|
menu->selected = 0;
|
2018-10-26 18:37:50 +02:00
|
|
|
redraw_description(menu, cam->renderer);
|
2018-10-22 09:04:16 +02:00
|
|
|
|
2018-09-12 14:45:09 +02:00
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
2018-05-20 17:25:53 +02:00
|
|
|
static bool
|
2018-05-20 00:02:39 +02:00
|
|
|
handle_keyboard_input(Menu *m, Input *input)
|
2018-02-08 17:01:38 +01:00
|
|
|
{
|
2018-02-14 23:14:30 +01:00
|
|
|
int lastSelect = -1;
|
2018-02-09 06:53:06 +01:00
|
|
|
|
2018-09-13 08:05:17 +02:00
|
|
|
if (input_key_is_pressed(input, KEY_UP | KEY_LEFT)) {
|
2018-02-14 23:14:30 +01:00
|
|
|
lastSelect = m->selected;
|
2018-02-12 10:55:36 +01:00
|
|
|
m->selected--;
|
2018-09-13 08:05:17 +02:00
|
|
|
} else if (input_key_is_pressed(input, KEY_DOWN | KEY_RIGHT)) {
|
2018-02-14 23:14:30 +01:00
|
|
|
lastSelect = m->selected;
|
2018-02-12 10:55:36 +01:00
|
|
|
m->selected++;
|
2018-05-20 00:02:39 +02:00
|
|
|
} else if (input_key_is_pressed(input, KEY_ENTER)) {
|
2018-02-09 10:18:22 +01:00
|
|
|
MenuItem *item = linkedlist_get(&m->items, m->selected);
|
|
|
|
if (item->button->event)
|
|
|
|
item->button->event(item->button->usrdata);
|
2018-05-20 17:25:53 +02:00
|
|
|
return true;
|
2018-02-14 23:14:30 +01:00
|
|
|
} else {
|
2018-05-20 17:25:53 +02:00
|
|
|
return false;
|
2018-02-09 10:18:22 +01:00
|
|
|
}
|
2018-02-14 23:14:30 +01:00
|
|
|
m->selected = m->selected % linkedlist_size(m->items);
|
2018-02-09 10:18:22 +01:00
|
|
|
|
2018-02-14 23:14:30 +01:00
|
|
|
if (lastSelect != -1)
|
2018-02-14 16:04:40 +01:00
|
|
|
mixer_play_effect(CLICK);
|
|
|
|
|
2018-02-14 23:14:30 +01:00
|
|
|
((MenuItem*) linkedlist_get(&m->items, lastSelect))->button->hover = false;
|
|
|
|
((MenuItem*) linkedlist_get(&m->items, m->selected))->button->hover = true;
|
2018-05-20 17:25:53 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_mouse_motion(Menu *m, Input *input)
|
|
|
|
{
|
|
|
|
if (!input_mouse_moved(input))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Position p = { input->mouseX, input->mouseY };
|
|
|
|
|
2018-10-02 12:44:59 +02:00
|
|
|
int lastSelect = m->selected;
|
2018-05-20 17:25:53 +02:00
|
|
|
int index = 0;
|
2018-10-02 12:44:59 +02:00
|
|
|
LinkedList *items = m->items;
|
2018-05-20 17:25:53 +02:00
|
|
|
while (items) {
|
|
|
|
MenuItem *item = items->data;
|
|
|
|
items = items->next;
|
|
|
|
|
|
|
|
if (position_in_rect(&p, &item->button->area)) {
|
|
|
|
m->selected = index;
|
2018-10-02 12:44:59 +02:00
|
|
|
if (index != lastSelect)
|
|
|
|
mixer_play_effect(CLICK);
|
2018-05-20 17:25:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
2018-02-14 23:14:30 +01:00
|
|
|
}
|
|
|
|
|
2018-10-22 09:04:16 +02:00
|
|
|
static void
|
2018-10-26 18:37:50 +02:00
|
|
|
redraw_description(Menu *m, SDL_Renderer *renderer)
|
2018-10-22 09:04:16 +02:00
|
|
|
{
|
|
|
|
char *description = linkedlist_get(&m->descriptions, m->selected);
|
|
|
|
if (!description || strlen(description) <= 1) {
|
|
|
|
m->menuDescription->hidden = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m->menuDescription->hidden = false;
|
|
|
|
texture_load_from_text(m->menuDescription->textures[0],
|
|
|
|
description,
|
|
|
|
C_WHITE,
|
|
|
|
C_BLACK,
|
2018-10-26 18:37:50 +02:00
|
|
|
renderer);
|
2018-10-22 09:04:16 +02:00
|
|
|
m->menuDescription->dim = DIM(
|
|
|
|
m->menuDescription->textures[0]->dim.width,
|
|
|
|
m->menuDescription->textures[0]->dim.height);
|
|
|
|
}
|
|
|
|
|
2018-02-14 23:14:30 +01:00
|
|
|
void
|
2018-10-22 09:04:16 +02:00
|
|
|
menu_update(Menu *m, Input *input, Camera *cam)
|
2018-02-14 23:14:30 +01:00
|
|
|
{
|
2019-02-26 11:59:07 +01:00
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
|
2018-10-22 09:04:16 +02:00
|
|
|
static int lastSelected = -1;
|
|
|
|
|
2018-05-20 17:25:53 +02:00
|
|
|
if (handle_keyboard_input(m, input)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handle_mouse_motion(m, input);
|
|
|
|
|
|
|
|
LinkedList *items = m->items;
|
|
|
|
while (items) {
|
|
|
|
MenuItem *item = items->data;
|
|
|
|
items = items->next;
|
|
|
|
|
|
|
|
Position p = { input->mouseX, input->mouseY };
|
|
|
|
if (position_in_rect(&p, &item->button->area)
|
|
|
|
&& input_mousebutton_is_pressed(input, MBUTTON_LEFT))
|
|
|
|
{
|
2018-10-02 22:42:29 +02:00
|
|
|
item->button->event(item->button->usrdata);
|
2018-05-20 17:25:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-10-22 09:04:16 +02:00
|
|
|
|
|
|
|
if (lastSelected != m->selected) {
|
|
|
|
lastSelected = m->selected;
|
2018-10-26 18:37:50 +02:00
|
|
|
redraw_description(m, cam->renderer);
|
2018-10-22 09:04:16 +02:00
|
|
|
}
|
2018-02-14 23:14:30 +01:00
|
|
|
}
|
|
|
|
|
2018-02-08 17:01:38 +01:00
|
|
|
void
|
2018-02-09 07:26:44 +01:00
|
|
|
menu_item_add(Menu *m, Sprite *s1, Sprite *s2, void (*event)(void*))
|
2018-02-08 17:01:38 +01:00
|
|
|
{
|
|
|
|
MenuItem *item = ec_malloc(sizeof(MenuItem));
|
|
|
|
item->sprite = s1;
|
|
|
|
item->hsprite = s2;
|
2018-02-09 06:53:06 +01:00
|
|
|
|
|
|
|
SDL_Rect area = {
|
|
|
|
item->sprite->pos.x,
|
|
|
|
item->sprite->pos.y,
|
|
|
|
item->sprite->textures[0]->dim.width,
|
|
|
|
item->sprite->textures[0]->dim.height
|
|
|
|
};
|
2018-02-09 07:26:44 +01:00
|
|
|
item->button = gui_button_create(area, event, NULL);
|
2018-02-14 23:14:30 +01:00
|
|
|
if (linkedlist_size(m->items) == 0)
|
|
|
|
item->button->hover = true;
|
2018-02-08 17:01:38 +01:00
|
|
|
linkedlist_append(&m->items, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
menu_render(Menu *m, Camera *cam)
|
|
|
|
{
|
2019-02-26 11:59:07 +01:00
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
|
2018-02-08 17:01:38 +01:00
|
|
|
LinkedList *items = m->items;
|
|
|
|
|
2018-05-20 17:25:53 +02:00
|
|
|
int index = 0;
|
2018-02-08 17:01:38 +01:00
|
|
|
while (items) {
|
|
|
|
MenuItem *item = items->data;
|
2018-02-09 09:36:24 +01:00
|
|
|
items = items->next;
|
2018-05-20 17:25:53 +02:00
|
|
|
if (m->selected == index)
|
2018-02-09 06:53:06 +01:00
|
|
|
sprite_render(item->hsprite, cam);
|
|
|
|
else
|
|
|
|
sprite_render(item->sprite, cam);
|
2018-05-20 17:25:53 +02:00
|
|
|
index++;
|
2018-02-08 17:01:38 +01:00
|
|
|
}
|
2018-02-09 09:36:24 +01:00
|
|
|
|
2018-10-22 09:04:16 +02:00
|
|
|
sprite_render(m->menuDescription, cam);
|
|
|
|
|
2018-02-08 17:01:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
menu_item_destroy(MenuItem *item)
|
|
|
|
{
|
2018-02-09 06:53:06 +01:00
|
|
|
if (item->sprite)
|
|
|
|
sprite_destroy(item->sprite);
|
|
|
|
if (item->hsprite)
|
|
|
|
sprite_destroy(item->hsprite);
|
|
|
|
gui_button_destroy(item->button);
|
2018-02-08 17:01:38 +01:00
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
menu_destroy(Menu *m)
|
|
|
|
{
|
2018-02-09 09:36:24 +01:00
|
|
|
while (m->items)
|
|
|
|
menu_item_destroy(linkedlist_pop(&m->items));
|
|
|
|
|
2018-10-22 09:04:16 +02:00
|
|
|
while (m->descriptions)
|
|
|
|
linkedlist_pop(&m->descriptions);
|
|
|
|
|
|
|
|
sprite_destroy(m->menuDescription);
|
2018-02-08 17:01:38 +01:00
|
|
|
free(m);
|
|
|
|
}
|
|
|
|
|