Fixes a leak and begins restructure of controller code

This commit is contained in:
Linus Probert 2018-10-12 22:56:48 +02:00
parent 2e0a88e5e3
commit 423ff733af
5 changed files with 72 additions and 25 deletions

View File

@ -211,6 +211,7 @@ add_executable(breakhack
src/object
src/gui_util
src/tooltip
src/gamecontroller
${STEAM_SOURCES}
)

34
src/gamecontroller.c Normal file
View File

@ -0,0 +1,34 @@
#include "gamecontroller.h"
#include "util.h"
static SDL_GameController *controller = NULL;
static Uint8 controllerMode = 0;
void
gamecontroller_set(SDL_GameController *ctrler)
{
const char *ctrlName = SDL_GameControllerName(controller);
info("Game controller connected: %s", ctrlName);
controller = ctrler;
// Try to determine if this is a PS3/4 controller
if (ctrlName[0] == 'P' &&
ctrlName[1] == 'S' &&
(ctrlName[2] == '4' || ctrlName[2] == '3'))
controllerMode = 2;
else
controllerMode = 1;
}
Uint8
gamecontroller_mode()
{
return controllerMode;
}
void
gamecontroller_close()
{
if (controller)
SDL_GameControllerClose(controller);
}

17
src/gamecontroller.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include <SDL.h>
typedef struct GameController {
SDL_GameController *controller;
unsigned int mode;
} GameController;
void
gamecontroller_set(SDL_GameController *controller);
Uint8
gamecontroller_mode(void);
void
gamecontroller_close(void);

View File

@ -53,6 +53,7 @@
#include "hiscore.h"
#include "io_util.h"
#include "tooltip.h"
#include "gamecontroller.h"
#ifdef STEAM_BUILD
#include "steam/steamworks_api_wrapper.h"
@ -155,7 +156,6 @@ static Screen *scoreScreen = NULL;
static Sprite *new_skill_tooltip = NULL;
static Sprite *howto_tooltip = NULL;
static Sprite *new_artifact_tooltip = NULL;
static SDL_GameController *gController = NULL;
static unsigned int cLevel = 1;
static float deltaTime = 1.0;
static double renderScale = 1.0;
@ -170,7 +170,6 @@ static SDL_Rect statsGuiViewport;
static SDL_Rect minimapViewport;
static SDL_Rect menuViewport;
static Input input;
static Uint8 controllerMode = 0;
#ifdef DEBUG
static Sprite *fpsSprite = NULL;
@ -216,20 +215,9 @@ bool initSDL(void)
if (!SDL_IsGameController(i))
continue;
gController = SDL_GameControllerOpen(i);
if (gController) {
const char *ctrlName = SDL_GameControllerName(gController);
info("Game controller connected: %s", ctrlName);
// Try to determine if this is a PS3/4 controller
if (ctrlName[0] == 'P' &&
ctrlName[1] == 'S' &&
(ctrlName[2] == '4' || ctrlName[2] == '3'))
controllerMode = 2;
else
controllerMode = 1;
break;
SDL_GameController *ctrler = SDL_GameControllerOpen(i);
if (ctrler) {
gamecontroller_set(ctrler);
}
}
@ -312,7 +300,7 @@ initGame(void)
gCamera = camera_create(gRenderer);
gRoomMatrix = roommatrix_create();
gGui = gui_create(gCamera);
skillbar_set_controller_mode(controllerMode);
skillbar_set_controller_mode(gamecontroller_mode());
gSkillBar = skillbar_create(gCamera);
item_builder_init(gRenderer);
#ifdef DEBUG
@ -554,7 +542,7 @@ init(void)
hiscore_init();
initMainMenu();
tooltip_set_controller_mode(controllerMode);
tooltip_set_controller_mode(gamecontroller_mode());
howto_tooltip = tooltip_create(how_to_play_tooltip, gCamera);
new_skill_tooltip = tooltip_create(skills_tooltip, gCamera);
new_artifact_tooltip = tooltip_create(artifacts_tooltip, gCamera);
@ -1131,8 +1119,7 @@ void close(void)
steam_shutdown();
#endif // STEAM_BUILD
if (gController)
SDL_GameControllerClose(gController);
gamecontroller_close();
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gWindow = NULL;

View File

@ -84,6 +84,14 @@ map_create_tile(void)
return tile;
}
static void
map_tile_destroy(MapTile *tile)
{
if (tile->sprite)
sprite_destroy(tile->sprite);
free(tile);
}
void
map_add_tile(Map *map, Position *tile_pos, MapTile *tile)
{
@ -98,13 +106,13 @@ map_add_tile(Map *map, Position *tile_pos, MapTile *tile)
// If this is the level exit then clear the decoration if one exists
if (tile->levelExit && room->decorations[tile_pos->x][tile_pos->y]) {
MapTile **decoration = &room->decorations[tile_pos->x][tile_pos->y];
free(*decoration);
map_tile_destroy(*decoration);
*decoration = NULL;
}
// Clear possible tile
if (*oldTile != NULL) {
free(*oldTile);
map_tile_destroy(*oldTile);
*oldTile = NULL;
}
*oldTile = tile;
@ -120,7 +128,7 @@ void map_add_decoration(Map *map, Position *tile_pos, MapTile *tile)
tile_pos->y * TILE_DIMENSION + (map->currentRoom.y * GAME_VIEW_HEIGHT));
if (*oldTile != NULL) {
free(*oldTile);
map_tile_destroy(*oldTile);
*oldTile = NULL;
}
*oldTile = tile;
@ -380,10 +388,10 @@ void map_room_destroy(Room *room)
for (i=0; i < MAP_ROOM_WIDTH; ++i) {
for (j=0; j < MAP_ROOM_HEIGHT; ++j) {
if (room->tiles[i][j]) {
free(room->tiles[i][j]);
map_tile_destroy(room->tiles[i][j]);
}
if (room->decorations[i][j]) {
free(room->decorations[i][j]);
map_tile_destroy(room->decorations[i][j]);
}
if (room->traps[i][j]) {
trap_destroy(room->traps[i][j]);