Fixed menu, leaks and clickableness

This commit is contained in:
Linus_Probert 2018-02-09 09:36:24 +01:00
parent fdfc6fcf77
commit 064e3e0f87
4 changed files with 32 additions and 23 deletions

View File

@ -151,8 +151,8 @@ exitGame(void *unused)
static void
initMainMenu(void)
{
static SDL_Color C_WHITE = { 255, 255, 255, 0 };
static SDL_Color C_RED = { 255, 0, 0, 0 };
static SDL_Color C_DEFAULT = { 255, 255, 0, 0 };
static SDL_Color C_HOVER = { 255, 0, 0, 0 };
struct MENU_ITEM {
char label[20];
@ -167,16 +167,16 @@ initMainMenu(void)
for (unsigned int i = 0; i < 2; ++i) {
Sprite *s1 = sprite_create();
sprite_load_text_texture(s1, "assets/GUI/SDS_8x8.ttf", 0, 14);
sprite_load_text_texture(s1, "assets/GUI/SDS_8x8.ttf", 0, 20);
texture_load_from_text(s1->textures[0], menu_items[i].label,
C_WHITE, gRenderer);
C_DEFAULT, 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);
sprite_load_text_texture(s2, "assets/GUI/SDS_8x8.ttf", 0, 20);
texture_load_from_text(s2->textures[0], menu_items[i].label,
C_RED, gRenderer);
C_HOVER, gRenderer);
s2->pos = (Position) { 200, 100 + (i*50) };
s2->fixed = true;
@ -191,10 +191,12 @@ resetGame(void)
map_destroy(gMap);
info("Building new map");
gMap = map_lua_generator_run(++cLevel, gRenderer);
gMap = map_lua_generator_run(cLevel, gRenderer);
gPlayer->sprite->pos = (Position) {
TILE_DIMENSION, TILE_DIMENSION };
gCamera.pos = (Position) { 0, 0 };
map_set_current_room(gMap, &gPlayer->sprite->pos);
camera_follow_position(&gCamera, &gPlayer->sprite->pos);
}
static bool
@ -265,6 +267,7 @@ check_next_level(void)
return;
}
if (tile->levelExit) {
++cLevel;
resetGame();
}
}
@ -272,7 +275,6 @@ check_next_level(void)
static void
run_game(void)
{
SDL_RenderSetViewport(gRenderer, NULL);
map_clear_dead_monsters(gMap);
map_clear_collected_items(gMap);
roommatrix_populate_from_map(gRoomMatrix, gMap);
@ -290,6 +292,7 @@ run_game(void)
map_move_monsters(gMap, gRoomMatrix);
}
SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 0);
SDL_RenderClear(gRenderer);
SDL_RenderSetViewport(gRenderer, &gameViewport);
@ -323,6 +326,7 @@ run_game(void)
static void
run_menu(void)
{
SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 0);
SDL_RenderClear(gRenderer);
SDL_RenderSetViewport(gRenderer, NULL);
menu_render(mainMenu, &gCamera);
@ -387,17 +391,19 @@ void run(void)
static
void close(void)
{
if (gPlayer)
player_destroy(gPlayer);
if (gMap)
map_destroy(gMap);
if (mainMenu)
menu_destroy(mainMenu);
roommatrix_destroy(gRoomMatrix);
gui_destroy(gGui);
pointer_destroy(gPointer);
item_builder_close();
particle_engine_close();
if (mainMenu)
menu_destroy(mainMenu);
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gWindow = NULL;

View File

@ -302,6 +302,9 @@ Map* map_lua_generator_run(unsigned int level, SDL_Renderer *renderer)
lua_close(L);
// Reset the map
map->currentRoom = (Position) { 0, 0 };
info("Done");
return map;

View File

@ -60,12 +60,13 @@ menu_render(Menu *m, Camera *cam)
while (items) {
MenuItem *item = items->data;
items = items->next;
if (item->selected)
sprite_render(item->hsprite, cam);
else
sprite_render(item->sprite, cam);
items = items->next;
}
}
static void
@ -82,11 +83,9 @@ menu_item_destroy(MenuItem *item)
void
menu_destroy(Menu *m)
{
LinkedList *items = m->items;
while (items) {
menu_item_destroy(items->data);
items = items->next;
}
while (m->items)
menu_item_destroy(linkedlist_pop(&m->items));
free(m);
}

View File

@ -24,9 +24,10 @@ void
pointer_handle_event(Pointer *p, SDL_Event *event)
{
if (event->type == SDL_MOUSEMOTION) {
p->sprite->pos.x = event->motion.x;
p->sprite->pos.y = event->motion.y;
// debug("Pointer pos: %dx%d", p->sprite->pos.x, p->sprite->pos.y);
// Compensate for a small offset in the sprite
p->sprite->pos.x = event->motion.x - 6;
p->sprite->pos.y = event->motion.y - 6;
//debug("Pointer pos: %dx%d", p->sprite->pos.x, p->sprite->pos.y);
}
}