diff options
| -rw-r--r-- | Source/Game.c | 820 | ||||
| -rw-r--r-- | Source/Game.h | 3 | ||||
| -rw-r--r-- | Source/GameGui.c | 11 | ||||
| -rw-r--r-- | Source/Gfx.c | 2 | ||||
| -rw-r--r-- | Source/Gfx.h | 2 | ||||
| -rw-r--r-- | Source/Makefile | 4 | ||||
| -rw-r--r-- | Source/Pad.c | 7 | ||||
| -rw-r--r-- | Source/System.c | 158 | ||||
| -rw-r--r-- | Source/System.h | 17 | ||||
| -rw-r--r-- | Source/Timer.c | 173 | ||||
| -rw-r--r-- | Source/Timer.h | 36 |
11 files changed, 894 insertions, 339 deletions
diff --git a/Source/Game.c b/Source/Game.c index fdf9f8a..148b581 100644 --- a/Source/Game.c +++ b/Source/Game.c @@ -3,6 +3,7 @@ * *************************************/ #include "Game.h" +#include "Timer.h" #include "LoadMenu.h" #include "System.h" #include "Camera.h" @@ -178,6 +179,7 @@ static void GameMinimumSpawnTimeout(void); static void GameRenderBuildingAircraft(TYPE_PLAYER* ptrPlayer); static void GameBuildingsInit(void); static void GameGetAircraftTilemap(uint8_t i); +static bool GameWaypointCheckExisting(TYPE_PLAYER* ptrPlayer, uint16_t temp_tile); /* ************************************* * Global Variables @@ -247,6 +249,19 @@ static bool TwoPlayersActive; // Determines whether game has finished or not. bool GameFinishedFlag; +/* *************************************************************************************** + * + * @name: void Game(bool two_players) + * + * @author: Xavier Del Campo + * + * @brief: + * Game main loop. Called by "Menu" module. + * + * @remarks: + * + * ***************************************************************************************/ + void Game(bool two_players) { TwoPlayersActive = two_players; @@ -294,6 +309,19 @@ void Game(bool two_players) SfxPlayTrack(INTRO_TRACK); } +/* *************************************************************************************** + * + * @name: bool GamePause(void) + * + * @author: Xavier Del Campo + * + * @brief: + * When PAD_START is pressed, it draws a rectangle on top of the screen and the game halts. + * + * @remarks: + * + * ***************************************************************************************/ + bool GamePause(void) { TYPE_PLAYER* ptrPlayer; @@ -332,6 +360,20 @@ bool GamePause(void) return false; } +/* *************************************************************************************** + * + * @name: void GameInit(void) + * + * @author: Xavier Del Campo + * + * @brief: + * Game basic parameters initialization. + * + * @remarks: + * Tilesets and buildings are only loaded on first game. Then, only PLT is loaded. + * + * ***************************************************************************************/ + void GameInit(void) { uint8_t i; @@ -436,7 +478,7 @@ void GameInit(void) GameMouseSpr.g = NORMAL_LUMINANCE; GameMouseSpr.b = NORMAL_LUMINANCE; - GameSpawnMinTime = SystemCreateTimer(GAME_MINIMUM_PARKING_SPAWN_TIME, false, &GameMinimumSpawnTimeout); + GameSpawnMinTime = TimerCreate(GAME_MINIMUM_PARKING_SPAWN_TIME, false, &GameMinimumSpawnTimeout); spawnMinTimeFlag = false; @@ -459,6 +501,21 @@ void GameInit(void) SfxPlayTrack(track); } +/* *************************************************************************************** + * + * @name: void GameBuildingsInit(void) + * + * @author: Xavier Del Campo + * + * + * @brief: + * Reportedly, it initializes coordinate/size data for each building instance. + * + * @remarks: + * + * + * ***************************************************************************************/ + void GameBuildingsInit(void) { enum @@ -580,6 +637,22 @@ void GameBuildingsInit(void) BUILDING_GATE,*/ } +/* *************************************************************************************** + * + * @name: void GameEmergencyMode(void) + * + * @author: Xavier Del Campo + * + * + * @brief: + * Draws a blue rectangle on top of the screen whenever one of the two active controllers + * (e.g.: pad1 on single player mode, pad1 || pad2 on two player mode) is disconnected. + * + * @remarks: + * See PSX_PollPad(), defined on psx.h, and Pad module for further information. + * + * ***************************************************************************************/ + void GameEmergencyMode(void) { uint8_t i; @@ -634,6 +707,24 @@ void GameEmergencyMode(void) } } +/* *************************************************************************************** + * + * @name: void GameGetAircraftTilemap(uint8_t i) + * + * @author: Xavier Del Campo + * + * @param: + * uint8_t i: + * Index for FlightData table. + * + * @brief: + * On each cycle, it creates a 2-dimensional array relating aircraft indexes against + * tile numbers. + * + * @remarks: + * + * ***************************************************************************************/ + void GameGetAircraftTilemap(uint8_t i) { uint16_t tileNr; @@ -665,12 +756,30 @@ void GameGetAircraftTilemap(uint8_t i) //Serial_printf("GameAircraftTileMap[%d][%d] = %d\n", tileNr, j, GameAircraftTilemap[tileNr][j]); } +/* *************************************************************************************** + * + * @name: void GameCalculations(void) + * + * @author: Xavier Del Campo + * + * @brief: + * First half of game execution. Executed when GPU is still drawing previous frame. + * Calculates all new states and values. + * + * @remarks: + * Since the GPU takes a long time to draw a frame, GameCalculations() should be used + * for all CPU-intensive tasks. + * + * ***************************************************************************************/ + void GameCalculations(void) { uint8_t i; GameClock(); + // FlightData handling + for(i = 0; i < FlightData.nAircraft; i++) { GameFinished(i); @@ -694,6 +803,27 @@ void GameCalculations(void) } } +/* *************************************************************************************** + * + * @name: void GamePlayerHandler(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) + * + * @author: Xavier Del Campo + * + * @param: + * TYPE_PLAYER* ptrPlayer: + * Pointer to a player structure + * + * TYPE_FLIGHT_DATA* ptrFlightData: + * In the end, pointer to FlightData data table, which contains + * information about all available flights. + * + * @brief: + * Calls all routines attached to a player. + * + * @remarks: + * + * ***************************************************************************************/ + void GamePlayerHandler(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) { ptrPlayer->SelectedTile = 0; // Reset selected tile if no states @@ -720,6 +850,19 @@ void GamePlayerHandler(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) GameSelectAircraftFromList(ptrPlayer, ptrFlightData); } +/* ******************************************************************* + * + * @name: void GameClock(void) + * + * @author: Xavier Del Campo + * + * @brief: + * Handles game clock later rendered on GameGui. + * + * @remarks: + * + * *******************************************************************/ + void GameClock(void) { if(System1SecondTick() == true) @@ -739,6 +882,23 @@ void GameClock(void) } } +/* ******************************************************************* + * + * @name: void GameClockFlights(uint8_t i) + * + * @author: Xavier Del Campo + * + * @param: + * uint8_t i: + * Index for FlightData table. + * + * @brief: + * Handles hours/minutes values for all active aircraft. + * + * @remarks: + * + * *******************************************************************/ + void GameClockFlights(uint8_t i) { if(System1SecondTick() == true) @@ -765,6 +925,24 @@ void GameClockFlights(uint8_t i) } } +/* ******************************************************************* + * + * @name: void GameGraphics(void) + * + * @author: Xavier Del Campo + * + * @brief: + * Second half of game execution. Once GameCalculations() has ended, + * states and new values have been calculated and all primitives are + * rendered depending on the obtained results. + * + * @remarks: + * It is advisable to keep CPU usage here, as once this function is + * entered, GPU is waiting for primitive data. Always try to move + * CPU-intensive operations to GameCalculations(). + * + * *******************************************************************/ + void GameGraphics(void) { int i; @@ -848,6 +1026,20 @@ void GameGraphics(void) GfxDrawScene(); } +/* ******************************************************************* + * + * @name: void GameGraphics(void) + * + * @author: Xavier Del Campo + * + * @brief: + * Determines rendering order depending on building/aircraft + * isometric position data. + * + * @remarks: + * + * *******************************************************************/ + void GameRenderBuildingAircraft(TYPE_PLAYER* ptrPlayer) { uint8_t tileNr; @@ -1007,6 +1199,22 @@ void GameRenderBuildingAircraft(TYPE_PLAYER* ptrPlayer) } } +/* ******************************************************************* + * + * @name: void GameLoadLevel(void) + * + * @author: Xavier Del Campo + * + * @brief: + * Loads and parses *.LVL data. + * + * + * @remarks: + * Filepath for *.LVL is given by GameLevelList[0]. Do NOT ever move + * it from there to avoid problems! + * + * *******************************************************************/ + void GameLoadLevel(void) { uint8_t i = 0; @@ -1072,11 +1280,6 @@ void GameLoadLevel(void) memcpy(GameLevelBuffer, &ptrBuffer[i], GameLevelSize * sizeof(uint16_t)); // 2 bytes per tile } -char* GetGameLevelTitle(void) -{ - return GameLevelTitle; -} - /* ****************************************************************************************** * * @name: void GameAircraftState(uint8_t i) @@ -1084,16 +1287,14 @@ char* GetGameLevelTitle(void) * @author: Xavier Del Campo * * @param: - * TYPE_PLAYER* ptrPlayer: - * Pointer to a player structure - * + * uint8_t i: + * Index for FlightData table. * * @brief: - * Draws all tiles depending on GameLevelBuffer configuration. + * It determines what state should be applied to aircraft when spawn timer expires. * * @remarks: - * Tiles are usually rendered with normal RGB values unless parking/runway is busy - * or ptrPlayer->InvalidPath == true. + * This is where TYPE_FLIGHT_DATA is transferred to TYPE_AIRCRAFT on departure. * * ******************************************************************************************/ @@ -1209,7 +1410,6 @@ void GameAircraftState(uint8_t i) void GameRenderLevel(TYPE_PLAYER* ptrPlayer) { uint16_t i; - uint16_t j; uint8_t columns = 0; uint8_t rows = 0; bool flip_id; @@ -1222,8 +1422,8 @@ void GameRenderLevel(TYPE_PLAYER* ptrPlayer) TYPE_ISOMETRIC_POS tileIsoPos; TYPE_CARTESIAN_POS tileCartPos; - uint16_t init_timer_value = 0; - uint16_t end_timer_value = 0; + //uint16_t init_timer_value = 0; + //uint16_t end_timer_value = 0; // Prepare runway to be painted in blue if player is on runway selection mode if(ptrPlayer->SelectRunway == true) @@ -1411,25 +1611,9 @@ void GameRenderLevel(TYPE_PLAYER* ptrPlayer) || (CurrentTile == TILE_RWY_HOLDING_POINT_2) ) ) { - bool bHoldingRwyBusy = false; - - init_timer_value = GetRCnt(2); - - - end_timer_value = GetRCnt(2); - - if(bHoldingRwyBusy == true) - { - ptrTileset->r = rwy_sine; - ptrTileset->g = NORMAL_LUMINANCE >> 2; - ptrTileset->b = NORMAL_LUMINANCE >> 2; - } - else - { - ptrTileset->r = NORMAL_LUMINANCE >> 2; - ptrTileset->g = rwy_sine; - ptrTileset->b = NORMAL_LUMINANCE >> 2; - } + ptrTileset->r = NORMAL_LUMINANCE >> 2; + ptrTileset->g = rwy_sine; + ptrTileset->b = NORMAL_LUMINANCE >> 2; } else if( (ptrPlayer->SelectTaxiwayParking == true) && @@ -1437,30 +1621,13 @@ void GameRenderLevel(TYPE_PLAYER* ptrPlayer) || (CurrentTile == TILE_PARKING_2) ) ) { - bool bParkingBusy = false; - - for(j = 0; j < FlightData.nAircraft; j++) - { - uint16_t aircraftTile = AircraftGetTileFromFlightDataIndex(j); + //init_timer_value = GetRCnt(2); - if(i == aircraftTile) - { - bParkingBusy = true; - } - } + //end_timer_value = GetRCnt(2); - if(bParkingBusy == true) - { - ptrTileset->r = rwy_sine; - ptrTileset->g = NORMAL_LUMINANCE >> 2; - ptrTileset->b = NORMAL_LUMINANCE >> 2; - } - else - { - ptrTileset->r = NORMAL_LUMINANCE >> 2; - ptrTileset->g = rwy_sine; - ptrTileset->b = NORMAL_LUMINANCE >> 2; - } + ptrTileset->r = NORMAL_LUMINANCE >> 2; + ptrTileset->g = rwy_sine; + ptrTileset->b = NORMAL_LUMINANCE >> 2; } } @@ -1494,11 +1661,11 @@ void GameRenderLevel(TYPE_PLAYER* ptrPlayer) } } - dprintf("GameRenderLevel execution time = %d\t" + /*dprintf("GameRenderLevel execution time = %d\t" "end_timer_value = 0x%04X\tinit_timer_value = 0x%04X\n", end_timer_value - init_timer_value, end_timer_value, - init_timer_value ); + init_timer_value );*/ } /* ******************************************************************* @@ -1567,7 +1734,7 @@ void GameActiveAircraft(uint8_t i) * TYPE_PLAYER* ptrPlayer: * Pointer to a player structure * - * TYPE_FLIGH_DATA* ptrFlightData: + * TYPE_FLIGHT_DATA* ptrFlightData: * In the end, pointer to FlightData data table, which contains * information about all available flights. * @@ -1610,7 +1777,7 @@ void GameStateShowAircraft(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightDa * TYPE_PLAYER* ptrPlayer: * Pointer to a player structure * - * TYPE_FLIGH_DATA* ptrFlightData: + * TYPE_FLIGHT_DATA* ptrFlightData: * In the end, pointer to FlightData data table, which contains * information about all available flights. * @@ -1678,7 +1845,7 @@ void GameStateLockTarget(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData * TYPE_PLAYER* ptrPlayer: * Pointer to a player structure * - * TYPE_FLIGH_DATA* ptrFlightData: + * TYPE_FLIGHT_DATA* ptrFlightData: * In the end, pointer to FlightData data table, which contains * information about all available flights. * @@ -1778,6 +1945,27 @@ void GameStateSelectTaxiwayRunway(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrF } } +/* ************************************************************************************************** + * + * @name: void GameStateSelectTaxiwayParking(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) + * + * @author: Xavier Del Campo + * + * @param: + * TYPE_PLAYER* ptrPlayer: + * Pointer to a player structure + * + * TYPE_FLIGHT_DATA* ptrFlightData: + * In the end, pointer to FlightData data table, which contains + * information about all available flights. + * + * @brief: + * Handler for ptrPlayer->SelectTaxiwayParking. + * + * @remarks: + * + * **************************************************************************************************/ + void GameStateSelectTaxiwayParking(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) { TYPE_ISOMETRIC_POS IsoPos = CameraGetIsoPos(ptrPlayer); @@ -1863,6 +2051,27 @@ void GameStateSelectTaxiwayParking(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptr } } +/* ************************************************************************************************** + * + * @name: void GameStateSelectRunway(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) + * + * @author: Xavier Del Campo + * + * @param: + * TYPE_PLAYER* ptrPlayer: + * Pointer to a player structure + * + * TYPE_FLIGHT_DATA* ptrFlightData: + * In the end, pointer to FlightData data table, which contains + * information about all available flights. + * + * @brief: + * Handler for ptrPlayer->SelectRunway. + * + * @remarks: + * + * **************************************************************************************************/ + void GameStateSelectRunway(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) { uint8_t i; @@ -1939,6 +2148,21 @@ void GameStateSelectRunway(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightDa } } +/* ************************************************************************************************** + * + * @name: void GameGetRunwayArray(void) + * + * @author: Xavier Del Campo + * + * + * @brief: + * On startup, an array of runway headers is created from GameLevelBuffer once *.LVL is parsed. + * + * @remarks: + * Do not confuse GameRwy with GameRwyArray, which are used for completely different purposes. + * + * **************************************************************************************************/ + void GameGetRunwayArray(void) { uint8_t i; @@ -1970,6 +2194,27 @@ void GameGetRunwayArray(void) Serial_printf("\n"); } +/* ************************************************************************************************** + * + * @name: void GameSelectAircraftFromList(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) + * + * @author: Xavier Del Campo + * + * @param: + * TYPE_PLAYER* ptrPlayer: + * Pointer to a player structure + * + * TYPE_FLIGHT_DATA* ptrFlightData: + * In the end, pointer to FlightData data table, which contains + * information about all available flights. + * + * @brief: + * Determines actions for aircraft on PAD_CROSS pressed. + * + * @remarks: + * + * **************************************************************************************************/ + void GameSelectAircraftFromList(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) { uint8_t AircraftIdx = ptrPlayer->FlightDataSelectedAircraft; @@ -2051,6 +2296,33 @@ void GameSelectAircraftFromList(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFli } } +/* ************************************************************************************************** + * + * @name: void GameGetSelectedRunwayArray(uint16_t rwyHeader, uint16_t* rwyArray, size_t sz) + * + * @author: Xavier Del Campo + * + * @param: + * uint16_t rwyHeader: + * Level tile number (located inside GameLevelBuffer) pointing to runway header. + * Only TILE_RWY_START_1 and TILE_RWY_START_2 (with or without TILE_MIRROR_FLAG) + * can be used for runway headers! + * + * uint16_t* rwyArray: + * Pointer to an array which will be filled with all the tiles belonging to a runway + * with header pointed to by rwyHeader. + * + * size_t sz: + * Maximum size of the array. + * + * @brief: + * Fills rwyArray with all the tile numbers (included in GameLevelBuffer) belonging to a + * runway with header pointed to by rwyHeader. + * + * @remarks: + * + * **************************************************************************************************/ + void GameGetSelectedRunwayArray(uint16_t rwyHeader, uint16_t* rwyArray, size_t sz) { typedef enum t_rwydir @@ -2145,6 +2417,28 @@ void GameGetSelectedRunwayArray(uint16_t rwyHeader, uint16_t* rwyArray, size_t s GameGetSelectedRunwayArray(0, rwyArray, sz); } +/* ************************************************************************************************** + * + * @name: void GameAssignRunwaytoAircraft(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) + * + * @author: Xavier Del Campo + * + * @param: + * TYPE_PLAYER* ptrPlayer: + * Pointer to a player structure + * + * TYPE_FLIGHT_DATA* ptrFlightData: + * In the end, pointer to FlightData data table, which contains + * information about all available flights. + * + * @brief: + * Assigns a runway to an incoming aircraft (FlightDirection == ARRIVAL) depending on + * player selection. + * + * @remarks: + * + * **************************************************************************************************/ + void GameAssignRunwaytoAircraft(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) { uint16_t assignedRwy = GameRwy[ptrPlayer->SelectedRunway]; @@ -2256,6 +2550,24 @@ void GameAssignRunwaytoAircraft(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFli } } +/* ******************************************************************* + * + * @name: short GameGetXFromTile_short(uint16_t tile) + * + * @author: Xavier Del Campo + * + * @param: + * uint16_t tile: + * Tile number from GameLevelBuffer. + * + * @return: + * Returns relative X position (no fixed-point arithmetic) given + * a tile number from GameLevelBuffer. + * + * @remarks: + * + * *******************************************************************/ + short GameGetXFromTile_short(uint16_t tile) { short retVal; @@ -2270,6 +2582,24 @@ short GameGetXFromTile_short(uint16_t tile) return retVal; } +/* ******************************************************************* + * + * @name: short GameGetYFromTile_short(uint16_t tile) + * + * @author: Xavier Del Campo + * + * @param: + * uint16_t tile: + * Tile number from GameLevelBuffer. + * + * @return: + * Returns relative Y position (no fixed-point arithmetic) given + * a tile number from GameLevelBuffer. + * + * @remarks: + * + * *******************************************************************/ + short GameGetYFromTile_short(uint16_t tile) { short retVal; @@ -2286,16 +2616,75 @@ short GameGetYFromTile_short(uint16_t tile) return retVal; } +/* ******************************************************************* + * + * @name: fix16_t GameGetXFromTile(uint16_t tile) + * + * @author: Xavier Del Campo + * + * @param: + * uint16_t tile: + * Tile number from GameLevelBuffer. + * + * @return: + * Returns relative X position in 16.16 (fix16_t) fixed-point format + * given a tile number from GameLevelBuffer. + * + * @remarks: + * + * *******************************************************************/ + fix16_t GameGetXFromTile(uint16_t tile) { return fix16_from_int(GameGetXFromTile_short(tile)); } +/* ******************************************************************* + * + * @name: fix16_t GameGetYFromTile(uint16_t tile) + * + * @author: Xavier Del Campo + * + * @param: + * uint16_t tile: + * Tile number from GameLevelBuffer. + * + * @return: + * Returns relative Y position in 16.16 (fix16_t) fixed-point format + * given a tile number from GameLevelBuffer. + * + * @remarks: + * + * *******************************************************************/ + fix16_t GameGetYFromTile(uint16_t tile) { return fix16_from_int(GameGetYFromTile_short(tile)); } +/* **************************************************************************** + * + * @name: FL_STATE GameTargetsReached(uint16_t firstTarget, uint8_t index) + * + * @author: Xavier Del Campo + * + * @param: + * uint16_t firstTarget: + * First waypoint of TYPE_AIRCRAFT instance. + * + * uint8_t index: + * Index of FlightData. + * + * @brief: + * Calculates new state for aircraft once all waypoints have been reached. + * + * @return: + * New state for aircraft once waypoints have been reached. + * + * @remarks: + * + * ****************************************************************************/ + FL_STATE GameTargetsReached(uint16_t firstTarget, uint8_t index) { FL_STATE retState = STATE_IDLE; @@ -2341,7 +2730,28 @@ FL_STATE GameTargetsReached(uint16_t firstTarget, uint8_t index) return retState; } -uint16_t GameGetTileFromIsoPosition(TYPE_ISOMETRIC_POS * IsoPos) +/* **************************************************************************** + * + * @name: uint16_t GameGetTileFromIsoPosition(TYPE_ISOMETRIC_POS* IsoPos) + * + * @author: Xavier Del Campo + * + * @param: + * TYPE_ISOMETRIC_POS* IsoPos: + * (x, y, z) position data. + * + * @brief: + * Calculates new state for aircraft once all waypoints have been reached. + * + * @return: + * Tile number to be used against GameLevelBuffer. + * + * @remarks: + * GameLevelColumns is used to determine tile number. + * + * ****************************************************************************/ + +uint16_t GameGetTileFromIsoPosition(TYPE_ISOMETRIC_POS* IsoPos) { uint16_t tile; @@ -2367,16 +2777,64 @@ uint16_t GameGetTileFromIsoPosition(TYPE_ISOMETRIC_POS * IsoPos) return tile; } +/* **************************************************************************** + * + * @name: uint8_t GameGetLevelColumns(void) + * + * @author: Xavier Del Campo + * + * @return: + * GameLevelColumns. Used for other modules without declaring GameLevelColumns + * as a global variable. + * + * ****************************************************************************/ + uint8_t GameGetLevelColumns(void) { return GameLevelColumns; } +/* **************************************************************************** + * + * @name: void GamePlayerAddWaypoint(TYPE_PLAYER* ptrPlayer) + * + * @author: Xavier Del Campo + * + * @param: + * TYPE_PLAYER* ptrPlayer: + * Pointer to a player structure + * + * @brief: + * Wrapper for GamePlayerAddWaypoint_Ex(). + * + * ****************************************************************************/ + void GamePlayerAddWaypoint(TYPE_PLAYER* ptrPlayer) { GamePlayerAddWaypoint_Ex(ptrPlayer, ptrPlayer->SelectedTile); } +/* **************************************************************************** + * + * @name: void GamePlayerAddWaypoint(TYPE_PLAYER* ptrPlayer) + * + * @author: Xavier Del Campo + * + * @param: + * TYPE_PLAYER* ptrPlayer: + * Pointer to a player structure. + * + * uint16_t tile: + * Tile number from GameLevelBuffer. + * + * @brief: + * It allows adding a tile number to ptrPlayer. + * + * @remark: + * To be used together with GamePathToTile(). + * + * ****************************************************************************/ + void GamePlayerAddWaypoint_Ex(TYPE_PLAYER* ptrPlayer, uint16_t tile) { // "_Ex" function allow selecting a certain tile, whereas the other one @@ -2395,13 +2853,80 @@ void GamePlayerAddWaypoint_Ex(TYPE_PLAYER* ptrPlayer, uint16_t tile) ptrPlayer->Waypoints[ptrPlayer->WaypointIdx++] = tile; } -bool GamePathToTile(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) +/* ************************************************************************************** + * + * @name: bool GameWaypointCheckExisting(TYPE_PLAYER* ptrPlayer, uint16_t temp_tile) + * + * @author: Xavier Del Campo + * + * @param: + * TYPE_PLAYER* ptrPlayer: + * Pointer to a player structure. + * + * uint16_t tile: + * Tile number from GameLevelBuffer. + * + * @brief: + * Checks if tile number temp_tile is already included on player's waypoint list. + * + * @return: + * True if waypoint is already included on waypoint list, false otherwise. + * + * **************************************************************************************/ + +bool GameWaypointCheckExisting(TYPE_PLAYER* ptrPlayer, uint16_t temp_tile) { - // Given an input TYPE_PLAYER structure and a selected tile, - // it updates current Waypoints array with all tiles between two points. - // If one of these tiles do not belong to desired tiles (i.e.: grass, - // water, buildings...), then false is returned. - + if(SystemContains_u16(temp_tile, ptrPlayer->Waypoints, PLAYER_MAX_WAYPOINTS) == false) + { + /*for(i = 0; i < FlightData.nAircraft; i++) + { + if( (ptrFlightData->State[i] != STATE_IDLE) + && + (AircraftMoving(i) == false) ) + { + if(temp_tile == AircraftGetTileFromFlightDataIndex(i)) + { + return false; // Check pending! + } + } + }*/ + + GamePlayerAddWaypoint_Ex(ptrPlayer, temp_tile); + + return false; + } + + // temp_tile is already included on ptrPlayer->Waypoints! + return true; +} + +/* **************************************************************************************** + * + * @name: bool GamePathToTile(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) + * + * @author: Xavier Del Campo + * + * @param: + * TYPE_PLAYER* ptrPlayer: + * Pointer to a player structure + * + * TYPE_FLIGHT_DATA* ptrFlightData: + * In the end, pointer to FlightData data table, which contains + * information about all available flights. + * + * @brief: + * Given an input TYPE_PLAYER structure and a selected tile, + * it updates current Waypoints array with all tiles between two points. + * If one of these tiles do not belong to desired tiles (i.e.: grass, + * water, buildings...), then false is returned. + * + * @return: + * Returns false on invalid path or invalid tile number selected. True otherwise. + * + * ****************************************************************************************/ + +bool GamePathToTile(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) +{ uint8_t AcceptedTiles[] = { TILE_ASPHALT_WITH_BORDERS, TILE_PARKING, TILE_RWY_MID, TILE_RWY_EXIT, TILE_TAXIWAY_CORNER_GRASS, @@ -2469,27 +2994,10 @@ bool GamePathToTile(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) temp_tile--; } - if(SystemContains_u16(temp_tile, ptrPlayer->Waypoints, PLAYER_MAX_WAYPOINTS) == false) - { - for(i = 0; i < FlightData.nAircraft; i++) - { - if( (ptrFlightData->State[i] != STATE_IDLE) - && - (AircraftMoving(i) == false) ) - { - if(temp_tile == AircraftGetTileFromFlightDataIndex(i)) - { - return false; // Check pending! - } - } - } - - GamePlayerAddWaypoint_Ex(ptrPlayer, temp_tile); - } - else - { + if(GameWaypointCheckExisting(ptrPlayer, temp_tile) == true) + { return false; // Tile is already included in the list of temporary tiles? - } + } } while( (y_diff--) > 0) @@ -2504,28 +3012,10 @@ bool GamePathToTile(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) temp_tile -= GameLevelColumns; } - if(SystemContains_u16(temp_tile, ptrPlayer->Waypoints, PLAYER_MAX_WAYPOINTS) == false) - { - for(i = 0; i < FlightData.nAircraft; i++) - { - if( (ptrFlightData->State[i] != STATE_IDLE) - && - (AircraftMoving(i) == false) ) - { - if(temp_tile == AircraftGetTileFromFlightDataIndex(i)) - { - return false; // Check pending! - } - } - } - - GamePlayerAddWaypoint_Ex(ptrPlayer, temp_tile); - } - else - { - // TEST - Check pending! + if(GameWaypointCheckExisting(ptrPlayer, temp_tile) == true) + { return false; // Tile is already included in the list of temporary tiles? - } + } } } else @@ -2541,29 +3031,11 @@ bool GamePathToTile(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) { temp_tile -= GameLevelColumns; } - - if(SystemContains_u16(temp_tile, ptrPlayer->Waypoints, PLAYER_MAX_WAYPOINTS) == false) - { - for(i = 0; i < FlightData.nAircraft; i++) - { - if( (ptrFlightData->State[i] != STATE_IDLE) - && - (AircraftMoving(i) == false) ) - { - if(temp_tile == AircraftGetTileFromFlightDataIndex(i)) - { - return false; // Check pending! - } - } - } - - GamePlayerAddWaypoint_Ex(ptrPlayer, temp_tile); - } - else - { - // TEST - Check pending! + + if(GameWaypointCheckExisting(ptrPlayer, temp_tile) == true) + { return false; // Tile is already included in the list of temporary tiles? - } + } } while( (x_diff--) > 0) @@ -2578,28 +3050,10 @@ bool GamePathToTile(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) temp_tile--; } - if(SystemContains_u16(temp_tile, ptrPlayer->Waypoints, PLAYER_MAX_WAYPOINTS) == false) - { - for(i = 0; i < FlightData.nAircraft; i++) - { - if( (ptrFlightData->State[i] != STATE_IDLE) - && - (AircraftMoving(i) == false) ) - { - if(temp_tile == AircraftGetTileFromFlightDataIndex(i)) - { - return false; // Check pending! - } - } - } - - GamePlayerAddWaypoint_Ex(ptrPlayer, temp_tile); - } - else - { - // TEST - Check pending! + if(GameWaypointCheckExisting(ptrPlayer, temp_tile) == true) + { return false; // Tile is already included in the list of temporary tiles? - } + } } } @@ -2644,6 +3098,24 @@ bool GamePathToTile(TYPE_PLAYER* ptrPlayer, TYPE_FLIGHT_DATA* ptrFlightData) return true; } +/* **************************************************************************************** + * + * @name: TYPE_ISOMETRIC_POS GameSelectAircraft(TYPE_PLAYER* ptrPlayer) + * + * @author: Xavier Del Campo + * + * @param: + * TYPE_PLAYER* ptrPlayer: + * Pointer to a player structure + * + * @brief: + * Moves player camera position to selected aircraft. + * + * @return: + * Isometric position of selected aircraft. + * + * ****************************************************************************************/ + TYPE_ISOMETRIC_POS GameSelectAircraft(TYPE_PLAYER* ptrPlayer) { uint8_t AircraftIdx = ptrPlayer->FlightDataSelectedAircraft; @@ -2654,6 +3126,21 @@ TYPE_ISOMETRIC_POS GameSelectAircraft(TYPE_PLAYER* ptrPlayer) return IsoPos; } +/* ******************************************************************************** + * + * @name: void GameSelectAircraftWaypoint(TYPE_PLAYER* ptrPlayer) + * + * @author: Xavier Del Campo + * + * @param: + * TYPE_PLAYER* ptrPlayer: + * Pointer to a player structure + * + * @brief: + * Moves player camera to selected aircraft and adds first waypoint. + * + * ********************************************************************************/ + void GameSelectAircraftWaypoint(TYPE_PLAYER* ptrPlayer) { TYPE_ISOMETRIC_POS IsoPos = GameSelectAircraft(ptrPlayer); @@ -2663,11 +3150,38 @@ void GameSelectAircraftWaypoint(TYPE_PLAYER* ptrPlayer) GamePlayerAddWaypoint(ptrPlayer); } +/* ******************************************************************************** + * + * @name: bool GameTwoPlayersActive(void) + * + * @author: Xavier Del Campo + * + * @return: + * Returns if a second player is active. To be used with other modules without + * declaring TwoPlayersActive as a global variable. + * + * ********************************************************************************/ + bool GameTwoPlayersActive(void) { return TwoPlayersActive; } +/* ***************************************************************** + * + * @name: void GameDrawMouse(TYPE_PLAYER* ptrPlayer) + * + * @author: Xavier Del Campo + * + * @param: + * TYPE_PLAYER* ptrPlayer: + * Pointer to a player structure + * + * @brief: + * Draws GameMouseSpr under determined player states. + * + * *****************************************************************/ + void GameDrawMouse(TYPE_PLAYER* ptrPlayer) { if( (ptrPlayer->SelectTaxiwayParking == true) @@ -3027,7 +3541,7 @@ void GameRemoveFlight(uint8_t idx, bool successful) FlightData.Finished[idx] = true; spawnMinTimeFlag = true; - SystemTimerRestart(GameSpawnMinTime); + TimerRestart(GameSpawnMinTime); return; } diff --git a/Source/Game.h b/Source/Game.h index 7c0f082..69e6bb4 100644 --- a/Source/Game.h +++ b/Source/Game.h @@ -32,7 +32,6 @@ extern TYPE_PLAYER PlayerData[]; * *************************************/ void Game(bool two_players); -char* GetGameLevelTitle(void); void GameSetTime(uint8_t hour, uint8_t minutes); bool GameTwoPlayersActive(void); uint8_t GameGetLevelColumns(void); @@ -41,7 +40,7 @@ fix16_t GameGetYFromTile(uint16_t tile); short GameGetXFromTile_short(uint16_t tile); short GameGetYFromTile_short(uint16_t tile); FL_STATE GameTargetsReached(uint16_t firstTarget, uint8_t index); -uint16_t GameGetTileFromIsoPosition(TYPE_ISOMETRIC_POS * IsoPos); +uint16_t GameGetTileFromIsoPosition(TYPE_ISOMETRIC_POS* IsoPos); FL_STATE GameGetFlightDataStateFromIdx(uint8_t FlightDataIdx); uint32_t GameGetScore(void); bool GameInsideLevelFromIsoPos(TYPE_ISOMETRIC_FIX16_POS* ptrIsoPos); diff --git a/Source/GameGui.c b/Source/GameGui.c index 82d8c71..e501827 100644 --- a/Source/GameGui.c +++ b/Source/GameGui.c @@ -7,6 +7,7 @@ #include "Gfx.h"
#include "Game.h"
#include "LoadMenu.h"
+#include "Timer.h"
/* *************************************
* Defines
@@ -288,7 +289,7 @@ void GameGuiInit(void) PageUpDownSpr.w = AIRCRAFT_DATA_FLIGHT_PAGEUPDN_SIZE;
- ShowAircraftPassengersTimer = SystemCreateTimer(20, true, GameGuiClearPassengersLeft);
+ ShowAircraftPassengersTimer = TimerCreate(20, true, GameGuiClearPassengersLeft);
slowScore = 0;
@@ -617,11 +618,11 @@ void GameGuiBubbleShow(void) if(GameGuiBubbleTimer == NULL)
{
Serial_printf("Started GameGuiBubbleTimer...\n");
- GameGuiBubbleTimer = SystemCreateTimer(50, false, &GameGuiBubbleStop);
+ GameGuiBubbleTimer = TimerCreate(50, false, &GameGuiBubbleStop);
}
else
{
- SystemTimerRestart(GameGuiBubbleTimer);
+ TimerRestart(GameGuiBubbleTimer);
}
GameGuiBubbleShowFlag = true;
@@ -641,11 +642,11 @@ void GameGuiBubble(TYPE_FLIGHT_DATA* ptrFlightData) if(GameGuiBubbleVibrationTimer == NULL)
{
Serial_printf("Started GameGuiBubbleVibrationTimer...\n");
- GameGuiBubbleVibrationTimer = SystemCreateTimer(20, false, &GameGuiBubbleStopVibration);
+ GameGuiBubbleVibrationTimer = TimerCreate(20, false, &GameGuiBubbleStopVibration);
}
else
{
- SystemTimerRestart(GameGuiBubbleVibrationTimer);
+ TimerRestart(GameGuiBubbleVibrationTimer);
}
}
diff --git a/Source/Gfx.c b/Source/Gfx.c index d974d1f..a664a12 100644 --- a/Source/Gfx.c +++ b/Source/Gfx.c @@ -593,7 +593,7 @@ bool GfxTPageOffsetFromVRAMPosition(GsSprite * spr, short x, short y) return false; } -TYPE_CARTESIAN_POS GfxIsometricToCartesian(TYPE_ISOMETRIC_POS * ptrIsoPos) +TYPE_CARTESIAN_POS GfxIsometricToCartesian(TYPE_ISOMETRIC_POS* ptrIsoPos) { TYPE_CARTESIAN_POS retCartPos; diff --git a/Source/Gfx.h b/Source/Gfx.h index c21e726..407e8f7 100644 --- a/Source/Gfx.h +++ b/Source/Gfx.h @@ -86,7 +86,7 @@ void GfxDrawButton(short x, short y, unsigned short btn); // sprite structure pointed to by "spr". void GfxSaveDisplayData(GsSprite *spr); -TYPE_CARTESIAN_POS GfxIsometricToCartesian(TYPE_ISOMETRIC_POS * ptrIsoPos); +TYPE_CARTESIAN_POS GfxIsometricToCartesian(TYPE_ISOMETRIC_POS* ptrIsoPos); // Function overload for fixed-point 16.16 data type. TYPE_CARTESIAN_POS GfxIsometricFix16ToCartesian(TYPE_ISOMETRIC_FIX16_POS * ptrIso16Pos); diff --git a/Source/Makefile b/Source/Makefile index dfe4290..5c47192 100644 --- a/Source/Makefile +++ b/Source/Makefile @@ -35,11 +35,11 @@ GNU_SIZE = mipsel-unknown-elf-size OBJECTS = $(addprefix $(OBJ_DIR)/,main.o System.o Menu.o Gfx.o Pad.o MainMenuBtnAni.o \ LoadMenu.o GameGui.o Sfx.o Camera.o EndAnimation.o \ PSXSDKIntro.o PltParser.o Game.o Font.o MemCard.o \ - Aircraft.o Serial.o) + Aircraft.o Serial.o Timer.o) DEPS = $(OBJECTS:.o=.d) -build: levels $(PROJECT).bin +build: levels $(PROJECT).bin rebuild: clean build diff --git a/Source/Pad.c b/Source/Pad.c index 8910298..e68804b 100644 --- a/Source/Pad.c +++ b/Source/Pad.c @@ -4,6 +4,7 @@ #include "Pad.h" #include "System.h" +#include "Timer.h" /* ************************************* * Defines @@ -456,8 +457,8 @@ void PadClearData(void) void PadInit(void) { - pad1_cheat_timer = SystemCreateTimer(PAD_CHEAT_TIMEOUT,true /* Repeat flag */,&PadOneCleanCheatArray); - pad2_cheat_timer = SystemCreateTimer(PAD_CHEAT_TIMEOUT,true /* Repeat flag */,&PadTwoCleanCheatArray); + pad1_cheat_timer = TimerCreate(PAD_CHEAT_TIMEOUT,true /* Repeat flag */,&PadOneCleanCheatArray); + pad2_cheat_timer = TimerCreate(PAD_CHEAT_TIMEOUT,true /* Repeat flag */,&PadTwoCleanCheatArray); memset(cheatsArray,0, sizeof(cheatsArray)); } @@ -524,7 +525,7 @@ void PadCheatHandler(uint8_t n_pad) { if(pressed_callback(available_keys[i]) == true) { - SystemTimerRestart(timer); + TimerRestart(timer); key = available_keys[i]; keys_released++; } diff --git a/Source/System.c b/Source/System.c index 6444b37..1830582 100644 --- a/Source/System.c +++ b/Source/System.c @@ -8,13 +8,13 @@ #include "Gfx.h" #include "MemCard.h" #include "EndAnimation.h" +#include "Timer.h" /* ************************************* * Defines * *************************************/ #define FILE_BUFFER_SIZE 0x20014 -#define SYSTEM_MAX_TIMERS 16 #define END_STACK_PATTERN (uint32_t) 0x18022015 #define BEGIN_STACK_ADDRESS (uint32_t*) 0x801FFF00 @@ -49,8 +49,6 @@ static bool five_hundred_ms_timer; static bool emergency_mode; //Critical section is entered (i.e.: when accessing fopen() or other BIOS functions static volatile bool system_busy; -//Timer array. -static TYPE_TIMER timer_array[SYSTEM_MAX_TIMERS]; // When true, it draws a rectangle on top of all primitives with // information for development purposes. static bool devmenu_flag; @@ -96,7 +94,7 @@ void SystemInit(void) //SPU init SsInit(); //Reset all user-handled timers - SystemResetTimers(); + TimerReset(); //Pads init PadInit(); //Set Drawing Environment @@ -665,156 +663,6 @@ bool SystemContains_u16(uint16_t value, uint16_t* buffer, size_t sz) return false; } -/* ******************************************************************************************** - * - * @name TYPE_TIMER* SystemCreateTimer(uint32_t t, bool rf, void (*timer_callback)(void) ) - * - * @author: Xavier Del Campo - * - * @brief: fills a TYPE_TIMER structure with input parameters - * - * @param: uint32_t t: - * Timeout value (1 unit = 10 ms) - * bool rf: - * Repeat flag - * void (*timer_callback)(void) - * Function to be called on timeout - * - * @return: pointer to TYPE_TIMER structure if filled correctly, NULL pointer otherwise. - * - * ********************************************************************************************/ - -TYPE_TIMER* SystemCreateTimer(uint32_t t, bool rf, void (*timer_callback)(void) ) -{ - bool success = false; - uint8_t i; - - if(t == 0) - { - Serial_printf("Cannot create timer with time == 0!\n"); - return NULL; - } - - for(i = 0; i < SYSTEM_MAX_TIMERS; i++) - { - if(timer_array[i].busy == false) - { - timer_array[i].Timeout_Callback = timer_callback; - timer_array[i].time = t; - timer_array[i].orig_time = t; - timer_array[i].repeat_flag = rf; - timer_array[i].busy = true; - success = true; - break; - } - } - - if(success == false) - { - Serial_printf("Could not find any free timer!\n"); - return NULL; - } - - return &timer_array[i]; -} - -/* ******************************************* - * - * @name void SystemResetTimers(void) - * - * @author: Xavier Del Campo - * - * @brief: reportedly, removes all timers. - * - * *******************************************/ - -void SystemResetTimers(void) -{ - uint8_t i; - - for(i = 0; i < SYSTEM_MAX_TIMERS; i++) - { - SystemTimerRemove(&timer_array[i]); - } -} - -/* ***************************************************** - * - * @name void SystemUserTimersHandler(void) - * - * @author: Xavier Del Campo - * - * @brief: reportedly, handles all available timers. - * - * @remarks: calls callback on timeout. - * - * *****************************************************/ - -void SystemUserTimersHandler(void) -{ - uint8_t i; - - for(i = 0; i < SYSTEM_MAX_TIMERS; i++) - { - if(timer_array[i].busy == true) - { - if(System100msTick() == true) - { - timer_array[i].time--; - - if(timer_array[i].time == 0) - { - timer_array[i].Timeout_Callback(); - - if(timer_array[i].repeat_flag == true) - { - timer_array[i].time = timer_array[i].orig_time; - } - } - } - } - } -} - -/* ********************************************************************* - * - * @name void SystemUserTimersHandler(void) - * - * @author: Xavier Del Campo - * - * @brief: sets time left for TYPE_TIMER instance to initial value. - * - * @remarks: specially used when TYPE_TIMER.rf is enabled. - * - * *********************************************************************/ - -void SystemTimerRestart(TYPE_TIMER* timer) -{ - timer->time = timer->orig_time; -} - -/* ********************************************************************* - * - * @name void SystemTimerRemove(TYPE_TIMER* timer) - * - * @author: Xavier Del Campo - * - * @brief: Resets timer parameters to default values so timer instance - * can be recycled. - * - * @remarks: - * - * *********************************************************************/ - -void SystemTimerRemove(TYPE_TIMER* timer) -{ - timer->time = 0; - timer->orig_time = 0; - timer->Timeout_Callback = NULL; - timer->busy = false; - timer->repeat_flag = false; -} - /* **************************************************************************************** * * @name bool SystemArrayCompare(unsigned short* arr1, unsigned short* arr2, size_t sz) @@ -1028,7 +876,7 @@ void SystemCyclicHandler(void) SystemRunTimers(); - SystemUserTimersHandler(); + TimerHandler(); SystemDisableScreenRefresh(); diff --git a/Source/System.h b/Source/System.h index b3d36de..cef7b4e 100644 --- a/Source/System.h +++ b/Source/System.h @@ -85,23 +85,6 @@ bool SystemContains_u8(uint8_t value, uint8_t* buffer, size_t sz); // Overload for uint16_t bool SystemContains_u16(uint16_t value, uint16_t* buffer, size_t sz); -// Creates a timer instance wiht a determined value and associates it to a callback -// Once time expires, callback is automatically called right after GfxDrawScene(). -// Time is expressed so that t = 100 ms e.g.: 2 seconds = 20. -TYPE_TIMER* SystemCreateTimer(uint32_t t, bool rf, void (*timer_callback)(void) ); - -// Reportedly, sets all timer data to zero. -void SystemResetTimers(void); - -// To be called every cycle (i.e.: inside GfxDrawScene() ). -void SystemUserTimersHandler(void); - -// Sets timer remaining time to initial value. -void SystemTimerRestart(TYPE_TIMER* timer); - -// Flushes a timer pointed to by timer. -void SystemTimerRemove(TYPE_TIMER* timer); - // Compares two arrays of unsigned short type. bool SystemArrayCompare(unsigned short* arr1, unsigned short* arr2, size_t sz); diff --git a/Source/Timer.c b/Source/Timer.c new file mode 100644 index 0000000..841979e --- /dev/null +++ b/Source/Timer.c @@ -0,0 +1,173 @@ +/* ************************************* + * Includes + * *************************************/ + +#include "Timer.h" +#include "GameStructures.h" + +/* ************************************* + * Defines + * *************************************/ + +#define MAX_TIMERS 16 + +/* ************************************* + * Local Variables + * *************************************/ + +//Timer array. +static TYPE_TIMER timer_array[MAX_TIMERS]; + +/* ************************************* + * Local Prototypes + * *************************************/ + +/* ******************************************************************************************** + * + * @name TYPE_TIMER* TimerCreate(uint32_t t, bool rf, void (*timer_callback)(void) ) + * + * @author: Xavier Del Campo + * + * @brief: fills a TYPE_TIMER structure with input parameters + * + * @param: uint32_t t: + * Timeout value (1 unit = 10 ms) + * bool rf: + * Repeat flag + * void (*timer_callback)(void) + * Function to be called on timeout + * + * @return: pointer to TYPE_TIMER structure if filled correctly, NULL pointer otherwise. + * + * ********************************************************************************************/ + +TYPE_TIMER* TimerCreate(uint32_t t, bool rf, void (*timer_callback)(void) ) +{ + bool success = false; + uint8_t i; + + if(t == 0) + { + Serial_printf("Cannot create timer with time == 0!\n"); + return NULL; + } + + for(i = 0; i < MAX_TIMERS; i++) + { + if(timer_array[i].busy == false) + { + timer_array[i].Timeout_Callback = timer_callback; + timer_array[i].time = t; + timer_array[i].orig_time = t; + timer_array[i].repeat_flag = rf; + timer_array[i].busy = true; + success = true; + break; + } + } + + if(success == false) + { + Serial_printf("Could not find any free timer!\n"); + return NULL; + } + + return &timer_array[i]; +} + +/* ******************************************* + * + * @name void TimerReset(void) + * + * @author: Xavier Del Campo + * + * @brief: reportedly, removes all timers. + * + * *******************************************/ + +void TimerReset(void) +{ + uint8_t i; + + for(i = 0; i < MAX_TIMERS; i++) + { + TimerRemove(&timer_array[i]); + } +} + +/* ***************************************************** + * + * @name void TimerHandler(void) + * + * @author: Xavier Del Campo + * + * @brief: reportedly, handles all available timers. + * + * @remarks: calls callback on timeout. + * + * *****************************************************/ + +void TimerHandler(void) +{ + uint8_t i; + + for(i = 0; i < MAX_TIMERS; i++) + { + if(timer_array[i].busy == true) + { + if(System100msTick() == true) + { + timer_array[i].time--; + + if(timer_array[i].time == 0) + { + timer_array[i].Timeout_Callback(); + + if(timer_array[i].repeat_flag == true) + { + timer_array[i].time = timer_array[i].orig_time; + } + } + } + } + } +} + +/* ********************************************************************* + * + * @name void TimerRestart(TYPE_TIMER* timer) + * + * @author: Xavier Del Campo + * + * @brief: sets time left for TYPE_TIMER instance to initial value. + * + * @remarks: specially used when TYPE_TIMER.rf is enabled. + * + * *********************************************************************/ + +void TimerRestart(TYPE_TIMER* timer) +{ + timer->time = timer->orig_time; +} + +/* ********************************************************************* + * + * @name void TimerRemove(TYPE_TIMER* timer) + * + * @author: Xavier Del Campo + * + * @brief: Resets timer parameters to default values so timer instance + * can be recycled. + * + * @remarks: + * + * *********************************************************************/ + +void TimerRemove(TYPE_TIMER* timer) +{ + timer->time = 0; + timer->orig_time = 0; + timer->Timeout_Callback = NULL; + timer->busy = false; + timer->repeat_flag = false; +} diff --git a/Source/Timer.h b/Source/Timer.h new file mode 100644 index 0000000..6f4a3a4 --- /dev/null +++ b/Source/Timer.h @@ -0,0 +1,36 @@ +#ifndef __TIMER_HEADER__ +#define __TIMER_HEADER__ + +/* ************************************** + * Includes * + * **************************************/ + +#include "Global_Inc.h" +#include "GameStructures.h" + +/* ************************************** + * Defines * + * **************************************/ + +/* ************************************** + * Global Prototypes * + * **************************************/ + +// Creates a timer instance wiht a determined value and associates it to a callback +// Once time expires, callback is automatically called right after GfxDrawScene(). +// Time is expressed so that t = 100 ms e.g.: 2 seconds = 20. +TYPE_TIMER* TimerCreate(uint32_t t, bool rf, void (*timer_callback)(void) ); + +// Reportedly, sets all timer data to zero. +void TimerReset(void); + +// To be called every cycle (i.e.: inside GfxDrawScene() ). +void TimerHandler(void); + +// Sets timer remaining time to initial value. +void TimerRestart(TYPE_TIMER* timer); + +// Flushes a timer pointed to by timer. +void TimerRemove(TYPE_TIMER* timer); + +#endif // __TIMER_HEADER__ |
