Started writing menus

Everything is pretty broken right now.
This commit is contained in:
Linus_Probert 2018-02-08 17:01:38 +01:00
parent 0b2a29fe3b
commit 3fe549e6fe
5 changed files with 188 additions and 33 deletions

View File

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

View File

@ -19,6 +19,7 @@
#include "pointer.h" #include "pointer.h"
#include "gui_button.h" #include "gui_button.h"
#include "particle_engine.h" #include "particle_engine.h"
#include "menu.h"
static SDL_Window *gWindow = NULL; static SDL_Window *gWindow = NULL;
static SDL_Renderer *gRenderer = NULL; static SDL_Renderer *gRenderer = NULL;
@ -30,6 +31,7 @@ static Pointer *gPointer = NULL;
static unsigned int cLevel = 1; static unsigned int cLevel = 1;
static float deltaTime = 1.0; static float deltaTime = 1.0;
static double renderScale = 1.0; static double renderScale = 1.0;
static Menu *mainMenu = NULL;
static GameState gGameState; static GameState gGameState;
static Camera gCamera; static Camera gCamera;
static SDL_Rect gameViewport; static SDL_Rect gameViewport;
@ -115,38 +117,86 @@ initViewports(void)
RIGHT_GUI_WIDTH, RIGHT_GUI_HEIGHT }; RIGHT_GUI_WIDTH, RIGHT_GUI_HEIGHT };
} }
static static bool
bool initGame(void) initGame(void)
{ {
gMap = map_lua_generator_run(cLevel, gRenderer);
return true;
}
static
bool init(void)
{
bool result = true;
result = result && initSDL();
result = result && initGame();
if (result) {
initViewports(); initViewports();
gCamera.pos = (Position) { 0, 0 };
gCamera.renderer = gRenderer; gCamera.renderer = gRenderer;
gRoomMatrix = roommatrix_create(); gRoomMatrix = roommatrix_create();
gGui = gui_create(gRenderer); gGui = gui_create(gRenderer);
item_builder_init(gRenderer); item_builder_init(gRenderer);
gPointer = pointer_create(gRenderer); gPointer = pointer_create(gRenderer);
particle_engine_init(); particle_engine_init();
}
gGameState = PLAYING; return true;
}
static void
initMainMenu(void)
{
static SDL_Color C_WHITE = { 255, 255, 255, 0 };
static SDL_Color C_RED = { 255, 0, 0, 0 };
struct MENU_ITEM {
char label[20];
void (*callback)(void);
};
struct MENU_ITEM menu_items[] = {
{ "PLAY", NULL },
{ "QUIT", NULL },
};
mainMenu = menu_create();
for (unsigned int i = 0; i < 2; ++i) {
Sprite *s1 = sprite_create();
sprite_load_text_texture(s1, "assets/GUI/SDS_8x8.ttf", 0, 14);
texture_load_from_text(s1->textures[0], menu_items[i].label,
C_WHITE, gRenderer);
s1->pos = (Position) { 200, 100 + (i*50) };
s1->fixed = true;
Sprite *s2 = sprite_create();
sprite_load_text_texture(s2, "assets/GUI/SDS_8x8.ttf", 0, 14);
texture_load_from_text(s2->textures[0], menu_items[i].label,
C_RED, gRenderer);
s2->pos = (Position) { 200, 100 + (i*50) };
s2->fixed = true;
menu_item_add(mainMenu, s1, s2);
}
}
static void
resetGame(void)
{
if (gMap)
map_destroy(gMap);
info("Building new map");
gMap = map_lua_generator_run(++cLevel, gRenderer);
gPlayer->sprite->pos = (Position) {
TILE_DIMENSION, TILE_DIMENSION };
gCamera.pos = (Position) { 0, 0 };
}
static bool
init(void)
{
bool result = true;
result = result && initSDL();
result = result && initGame();
initMainMenu();
gCamera.pos = (Position) { 0, 0 };
gGameState = MENU;
return result; return result;
} }
static static void
void loadMedia(void) loadMedia(void)
{ {
gPlayer = player_create(WARRIOR, gRenderer); gPlayer = player_create(WARRIOR, gRenderer);
} }
@ -166,8 +216,10 @@ bool handle_events(void)
&event); &event);
camera_follow_position(&gCamera, &gPlayer->sprite->pos); camera_follow_position(&gCamera, &gPlayer->sprite->pos);
map_set_current_room(gMap, &gPlayer->sprite->pos); map_set_current_room(gMap, &gPlayer->sprite->pos);
}
roommatrix_handle_event(gRoomMatrix, &event); roommatrix_handle_event(gRoomMatrix, &event);
} else if (gGameState == MENU) {
menu_handle_event(mainMenu, &event);
}
pointer_handle_event(gPointer, &event); pointer_handle_event(gPointer, &event);
} }
@ -196,11 +248,7 @@ check_next_level(void)
return; return;
} }
if (tile->levelExit) { if (tile->levelExit) {
info("Building new map"); resetGame();
map_destroy(gMap);
gMap = map_lua_generator_run(++cLevel, gRenderer);
gPlayer->sprite->pos = (Position) {
TILE_DIMENSION, TILE_DIMENSION };
} }
} }
@ -255,6 +303,16 @@ run_game(void)
check_next_level(); check_next_level();
} }
static void
run_menu(void)
{
SDL_RenderClear(gRenderer);
SDL_RenderSetViewport(gRenderer, NULL);
menu_render(mainMenu, &gCamera);
pointer_render(gPointer, &gCamera);
SDL_RenderPresent(gRenderer);
}
static static
void run(void) void run(void)
{ {
@ -277,7 +335,7 @@ void run(void)
run_game(); run_game();
break; break;
case MENU: case MENU:
fatal("MENU not implemented"); run_menu();
break; break;
case IN_GAME_MENU: case IN_GAME_MENU:
fatal("IN_GAME_MENU not implemented"); fatal("IN_GAME_MENU not implemented");

73
src/menu.c Normal file
View File

@ -0,0 +1,73 @@
#include <stdbool.h>
#include "menu.h"
#include "util.h"
#include "sprite.h"
#include "defines.h"
typedef struct MenuItems_t {
Sprite *sprite;
Sprite *hsprite;
bool selected;
void (*onClick)(void);
} MenuItem;
Menu *
menu_create(void)
{
Menu *menu = ec_malloc(sizeof(Menu));
menu->items = linkedlist_create();
menu->selected = 0;
return menu;
}
void
menu_handle_event(Menu *m, SDL_Event *event)
{
if (event->type != SDL_KEYDOWN && event->type != SDL_MOUSEMOTION) {
return;
}
error("Menu events need implementation still");
UNUSED(m);
}
void
menu_item_add(Menu *m, Sprite *s1, Sprite *s2)
{
MenuItem *item = ec_malloc(sizeof(MenuItem));
item->sprite = s1;
item->hsprite = s2;
item->selected = false;
item->onClick = NULL;
linkedlist_append(&m->items, item);
}
void
menu_render(Menu *m, Camera *cam)
{
LinkedList *items = m->items;
while (items) {
MenuItem *item = items->data;
sprite_render(item->sprite, cam);
items = items->next;
}
}
static void
menu_item_destroy(MenuItem *item)
{
sprite_destroy(item->sprite);
free(item);
}
void
menu_destroy(Menu *m)
{
LinkedList *items = m->items;
while (items) {
menu_item_destroy(items->data);
items = items->next;
}
free(m);
}

30
src/menu.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef MENU_H_
#define MENU_H_
#include <SDL2/SDL.h>
#include "camera.h"
#include "linkedlist.h"
#include "texture.h"
#include "sprite.h"
typedef struct Menu_t {
LinkedList *items;
unsigned int selected;
} Menu;
Menu *
menu_create(void);
void
menu_handle_event(Menu*, SDL_Event*);
void
menu_item_add(Menu*, Sprite*, Sprite*);
void
menu_render(Menu*, Camera*);
void
menu_destroy(Menu*);
#endif // MENU_H_

View File

@ -107,16 +107,9 @@ texture_render(Texture *texture, Position *p, Camera *cam)
texture->dim.height texture->dim.height
}; };
SDL_Rect clip = (SDL_Rect) {
0,
0,
texture->dim.width,
texture->dim.height
};
SDL_RenderCopy(cam->renderer, SDL_RenderCopy(cam->renderer,
texture->texture, texture->texture,
&clip, NULL,
&draw_box); &draw_box);
} }