diff --git a/CMakeLists.txt b/CMakeLists.txt index 56a51d8..53dac98 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -211,6 +211,7 @@ add_executable(breakhack src/object src/gui_util src/tooltip + src/gamecontroller ${STEAM_SOURCES} ) diff --git a/src/gamecontroller.c b/src/gamecontroller.c new file mode 100644 index 0000000..7a2bb19 --- /dev/null +++ b/src/gamecontroller.c @@ -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); +} diff --git a/src/gamecontroller.h b/src/gamecontroller.h new file mode 100644 index 0000000..6622f9a --- /dev/null +++ b/src/gamecontroller.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +typedef struct GameController { + SDL_GameController *controller; + unsigned int mode; +} GameController; + +void +gamecontroller_set(SDL_GameController *controller); + +Uint8 +gamecontroller_mode(void); + +void +gamecontroller_close(void); diff --git a/src/main.c b/src/main.c index b3203e8..b196f48 100644 --- a/src/main.c +++ b/src/main.c @@ -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; diff --git a/src/map.c b/src/map.c index cb8f606..e6d811f 100644 --- a/src/map.c +++ b/src/map.c @@ -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]);