Deprecated use of sprintf() in favor of custom routine Systemitoa().

When B button is released and showActionsMenu == true, showActionsMenu = false.
This commit is contained in:
XaviDCR92 2017-11-05 18:00:57 +01:00
parent 8fba2176bc
commit 2d04eba931
12 changed files with 2037 additions and 2080 deletions

View File

@ -53,9 +53,9 @@ void CameraApplyCoordinatesToSprite(TYPE_CAMERA* ptrCamera,
void CameraUpdateSpeed(TYPE_CAMERA* ptrCamera)
{
if (PadDirectionKeyPressed() == true)
if (PadDirectionKeyPressed() != false)
{
if (PadButtonPressed(PAD_LEFT) == true)
if (PadButtonPressed(PAD_LEFT) != false)
{
if (ptrCamera->X_Speed < 0)
{
@ -67,7 +67,7 @@ void CameraUpdateSpeed(TYPE_CAMERA* ptrCamera)
}
}
if (PadButtonPressed(PAD_UP) == true)
if (PadButtonPressed(PAD_UP) != false)
{
if (ptrCamera->Y_Speed < 0)
{
@ -79,7 +79,7 @@ void CameraUpdateSpeed(TYPE_CAMERA* ptrCamera)
}
}
if (PadButtonPressed(PAD_DOWN) == true)
if (PadButtonPressed(PAD_DOWN) != false)
{
if (ptrCamera->Y_Speed > 0)
{
@ -91,7 +91,7 @@ void CameraUpdateSpeed(TYPE_CAMERA* ptrCamera)
}
}
if (PadButtonPressed(PAD_RIGHT) == true)
if (PadButtonPressed(PAD_RIGHT) != false)
{
if (ptrCamera->X_Speed > 0)
{
@ -135,7 +135,7 @@ void CameraUpdateSpeed(TYPE_CAMERA* ptrCamera)
void CameraHandler(TYPE_CAMERA* ptrCamera)
{
if (ptrCamera->locked == true)
if (ptrCamera->locked != false)
{
ptrCamera->X_Speed = 0;
ptrCamera->Y_Speed = 0;
@ -167,12 +167,6 @@ void CameraHandler(TYPE_CAMERA* ptrCamera)
ptrCamera->X_Speed = 0;
}
/*char str[8];
snprintf(str, 8, "%u", ptrCamera->Y_Offset);
GfxPrintText(str, 40, 40);*/
if ((ptrCamera->Y_Offset + ptrCamera->Y_Speed) < 0)
{
ptrCamera->Y_Offset += ptrCamera->Y_Speed;

Binary file not shown.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -60,7 +60,7 @@ void GameInit(void)
bool GamePause(void)
{
if (PadButtonReleased(PAD_C) == true)
if (PadButtonReleased(PAD_C) != false)
{
uint8_t choice = gb.menu(PauseMenuOptions, 2);
@ -82,7 +82,7 @@ void GameCalculations(void)
GamePlayers[i].Handler();
}
if (PadAnyKeyPressed() == true)
if (PadAnyKeyPressed() != false)
{
SystemSetRandSeed();
}
@ -106,7 +106,7 @@ void GameLoop(void)
{
while (1)
{
if (GamePause() == true)
if (GamePause() != false)
{
return;
}

21
Gfx.cpp
View File

@ -31,7 +31,7 @@ void GfxDrawSprite(TYPE_SPRITE* ptrSprite)
return;
}
if (GfxIsSpriteInsideScreenArea(ptrSprite) == true)
if (GfxIsSpriteInsideScreenArea(ptrSprite) != false)
{
int8_t orig_color = gb.display.getColor();
@ -64,17 +64,6 @@ void GfxClearScreen(void)
bool GfxIsInsideScreenArea(int8_t x, int8_t y, uint8_t w, uint8_t h)
{
/*char strBuffer[16];
snprintf(strBuffer, 16, "%d", (int)(x + w));
GfxPrintText(strBuffer,48,8);
snprintf(strBuffer, 16, "x = %d", (int)(x));
GfxPrintText(strBuffer,48,16);
snprintf(strBuffer, 16, "w = %d", (int)(w));
GfxPrintText(strBuffer,48,24);*/
if ( ( (x + w) >= 0)
&&
(x < X_SCREEN_RESOLUTION)
@ -137,7 +126,7 @@ void GfxPrintText(const char* str, uint8_t x, uint8_t y)
void GfxDrawCircle(uint16_t x, uint16_t y, uint8_t radius, int8_t color)
{
if (GfxIsInsideScreenArea(x, y, radius, radius) == true)
if (GfxIsInsideScreenArea(x, y, radius, radius) != false)
{
int8_t orig_color = gb.display.getColor();
gb.display.setColor(color);
@ -148,7 +137,7 @@ void GfxDrawCircle(uint16_t x, uint16_t y, uint8_t radius, int8_t color)
void GfxDrawFillCircle(uint16_t x, uint16_t y, uint8_t radius, int8_t color)
{
if (GfxIsInsideScreenArea(x, y, radius, radius) == true)
if (GfxIsInsideScreenArea(x, y, radius, radius) != false)
{
int8_t orig_color = gb.display.getColor();
gb.display.setColor(color);
@ -159,7 +148,7 @@ void GfxDrawFillCircle(uint16_t x, uint16_t y, uint8_t radius, int8_t color)
void GfxDrawRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, int8_t color)
{
if (GfxIsInsideScreenArea(x, y, w, h) == true)
if (GfxIsInsideScreenArea(x, y, w, h) != false)
{
int8_t orig_color = gb.display.getColor();
@ -177,7 +166,7 @@ void GfxDrawLine(uint8_t x0, uint8_t x1, uint8_t y0, uint8_t y1, uint8_t color)
void GfxFillRectangle(uint8_t x, uint8_t y, uint8_t w, uint8_t h, int8_t color)
{
if (GfxIsInsideScreenArea(x, y, w, h) == true)
if (GfxIsInsideScreenArea(x, y, w, h) != false)
{
int8_t orig_color = gb.display.getColor();

View File

@ -23,9 +23,12 @@
* Defines
* *************************************/
#define DEBUG_VAR(var, suff, x, y) char buffer##suff[16]; \
snprintf(buffer##suff, sizeof(buffer##suff), "%d", var); \
GfxPrintText(buffer##suff, x - (strlen(buffer##suff)<<3), y)
#define DEBUG_VAR(var, suff, x, y) if (1) \
{ \
char buffer##suff[16]; \
Systemitoa(str, sizeof(buffer##suff[16]), var); \
GfxPrintText(buffer##suff, x - (strlen(buffer##suff)<<3), y); \
}
/* *************************************
* Structs and enums

View File

@ -40,9 +40,9 @@ bool PadDirectionKeyPressed(void)
bool PadAnyKeyPressed(void)
{
return ( (PadDirectionKeyPressed() == true)
return ( (PadDirectionKeyPressed() != false)
||
(PadButtonPressed(PAD_A) == true)
(PadButtonPressed(PAD_A) != false)
||
(PadButtonPressed(PAD_B) == true) );
(PadButtonPressed(PAD_B) != false) );
}

View File

@ -85,9 +85,13 @@ void Player::showHealth(uint8_t hp)
char str[8];
str[0] = 'H';
str[1] = 'P';
str[2] = '=';
GfxFillRectangle(0, Y_SCREEN_RESOLUTION - 5, X_SCREEN_RESOLUTION, 8, GFX_WHITE);
snprintf(str, sizeof(str), "HP=%u", hp);
Systemitoa(&str[3], sizeof(str) - 3, hp);
GfxPrintTextFont(str, font3x3, HP_TEXT_X, HP_TEXT_Y);
}
@ -123,7 +127,7 @@ void Player::DrawHandler(void)
UnitDraw(u, &Camera, selected);
if ( (u->selected == true) && (bAnyoneSelected == false) )
if ( (u->selected != false) && (bAnyoneSelected == false) )
{
bAnyoneSelected = true;
@ -131,9 +135,9 @@ void Player::DrawHandler(void)
}
}
if (human == true)
if (human != false)
{
if (bAnyoneSelected == true)
if (bAnyoneSelected != false)
{
GfxDrawRectangle(PROGRESS_BAR_X, PROGRESS_BAR_Y, PROGRESS_BAR_W, PROGRESS_BAR_H, GFX_BLACK);
@ -151,24 +155,47 @@ void Player::DrawHandler(void)
void Player::ShowResources(void)
{
char str[16];
char str[8];
uint8_t i;
str[1] = '=';
gb.display.setColor(GFX_GRAY);
gb.display.fillRect(0, 0, X_SCREEN_RESOLUTION, 8);
snprintf(str, sizeof(str), "W=%d", Resources.Wood);
str[0] = 'W';
GfxPrintTextFont(str, font3x3, 2, 1);
Systemitoa(&str[2], 6, Resources.Wood);
snprintf(str, sizeof(str), "G=%d", Resources.Gold);
GfxPrintTextFont(str, font3x3, 8, 1);
str[0] = 'G';
Systemitoa(&str[2], 6, Resources.Gold);
GfxPrintTextFont(str, font3x3, 2, 5);
snprintf(str, sizeof(str), "F=%d", Resources.Food);
str[0] = 'F';
Systemitoa(&str[2], 6, Resources.Food);
GfxPrintTextFont(str, font3x3, 42, 1);
snprintf(str, sizeof(str), "%d/%d", unit_i, PLAYER_MAX_UNITS_BUILDINGS);
Systemitoa(str, 3, unit_i);
for (i = 0; i < 3; i++)
{
if (str[i] == '\0')
{
break;
}
}
str[i++] = '/';
Systemitoa(&str[i], sizeof(str) - i, PLAYER_MAX_UNITS_BUILDINGS);
//~ snprintf(str, sizeof(str), "%d/%d", unit_i, PLAYER_MAX_UNITS_BUILDINGS);
GfxPrintTextFont(str, font3x3, 42, 5);
}
@ -196,7 +223,7 @@ bool Player::checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb)
bldgCB.w = UnitGetWidthFromID(ptrUnit->id);
bldgCB.h = UnitGetHeightFromID(ptrUnit->id);
if (SystemCollisionCheck(cb, &bldgCB) == true)
if (SystemCollisionCheck(cb, &bldgCB) != false)
{
success = false;
}
@ -260,15 +287,6 @@ void Player::createUnit(TYPE_UNIT_ID id, TYPE_COLLISION_BLOCK cb)
Resources.Wood -= res.Wood;
Resources.Gold -= res.Gold;
Resources.Food -= res.Food;
if (ptrNewUnit->building == true)
{
GfxPrintText_Flash(F("Building created"));
}
else
{
GfxPrintText_Flash(F("Unit created"));
}
}
else
{
@ -300,7 +318,7 @@ void Player::UnitBuildingSelection(void)
{
TYPE_UNIT* u = &units[i];
if ( (u->alive == false) || (u->selected == true) )
if ( (u->alive == false) || (u->selected != false) )
{
continue;
}
@ -325,13 +343,13 @@ void Player::UnitBuildingSelection(void)
void Player::ActionsMenu(void)
{
if (showActionsMenu == true)
if (showActionsMenu != false)
{
for (uint8_t i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
TYPE_UNIT* ptrUnit = &units[i];
if (ptrUnit->selected == true)
if (ptrUnit->selected != false)
{
uint8_t availableActions = UnitGetAvailableActions(ptrUnit);
@ -369,27 +387,27 @@ void Player::Handler(void)
void Player::ButtonHandler(void)
{
if (PadButtonPressed(PAD_A) == true)
if (PadButtonPressed(PAD_A) != false)
{
ButtonAPressed();
}
else if (PadButtonReleased(PAD_A) == true)
else if (PadButtonReleased(PAD_A) != false)
{
ButtonAReleased();
}
else if (PadButtonPressed(PAD_B) == true)
else if (PadButtonPressed(PAD_B) != false)
{
ButtonBPressed();
}
else if (PadButtonReleased(PAD_B) == true)
else if (PadButtonReleased(PAD_B) != false)
{
ButtonBReleased();
}
else if (PadButtonReleased(PAD_LEFT) == true)
else if (PadButtonReleased(PAD_LEFT) != false)
{
ButtonLeftReleased();
}
else if (PadButtonReleased(PAD_RIGHT) == true)
else if (PadButtonReleased(PAD_RIGHT) != false)
{
ButtonRightReleased();
}
@ -398,7 +416,7 @@ void Player::ButtonHandler(void)
void Player::ButtonAPressed(void)
{
// Only increase progress bar when any unit has been previously selected
if (anyUnitSelected == true)
if (anyUnitSelected != false)
{
if (showActionsMenu == false)
{
@ -428,7 +446,7 @@ void Player::ButtonAReleased(void)
showActionsMenu_counter = 0;
}
else if (showActionsMenu == true)
else if (showActionsMenu != false)
{
uint8_t i = 0;
@ -436,7 +454,7 @@ void Player::ButtonAReleased(void)
{
TYPE_UNIT* ptrUnit = &units[i];
if (ptrUnit->selected == true)
if (ptrUnit->selected != false)
{
showActionsMenu_counterLevel1 = 0;
@ -474,7 +492,7 @@ void Player::ActionCreateUnit(TYPE_UNIT* ptrUnit, TYPE_UNIT_ID unit)
{
uint8_t w = UnitGetWidthFromID(ptrUnit->id);
uint8_t h = UnitGetHeightFromID(ptrUnit->id);
uint8_t new_pos_x = ptrUnit->x + SystemRand(w, w + (w >> 1));
uint8_t new_pos_x = ptrUnit->x + SystemRand(0, w + (w >> 1));
uint8_t new_pos_y = ptrUnit->y + SystemRand(h, h + (h >> 1));
TYPE_COLLISION_BLOCK cb = {.x = new_pos_x, .y = new_pos_y};
@ -485,6 +503,26 @@ void Player::ActionCreateBuilding(TYPE_UNIT* ptrUnit, TYPE_UNIT_ID bldg)
{
TYPE_COLLISION_BLOCK cb = GetCursorPos();
for (uint8_t i = 0; i < unit_i; i++)
{
TYPE_UNIT* ptrOtherUnit = &units[i];
TYPE_COLLISION_BLOCK ocb = {.x = ptrOtherUnit->x,
.y = ptrOtherUnit->y,
.w = UnitGetWidthFromID(ptrOtherUnit->id),
.h = UnitGetHeightFromID(ptrOtherUnit->id)};
if (ptrOtherUnit->alive == false)
{
continue;
}
if (SystemCollisionCheck(&cb, &ocb) != false)
{
GfxPrintText_Flash(F("Cannot build here"));
return;
}
}
createUnit(bldg, cb);
}
@ -495,7 +533,7 @@ void Player::ButtonBPressed(void)
CANCEL_SELECTION_FRAMES = 5
};
if (anyUnitSelected == true)
if (anyUnitSelected != false)
{
if (unselectUnits_counter < CANCEL_SELECTION_FRAMES)
{
@ -503,22 +541,22 @@ void Player::ButtonBPressed(void)
}
else
{
if (anyUnitSelected == true)
if (anyUnitSelected != false)
{
uint8_t i;
// Cancel selection of all units
/* Cancel selection of all units */
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
TYPE_UNIT* ptrUnit = &units[i];
if (ptrUnit->selected == true)
if (ptrUnit->selected != false)
{
ptrUnit->selected = false;
}
}
// Reset accumulated counter and flags
/* Reset accumulated counter and flags */
unselectUnits_counter = 0;
anyUnitSelected = false;
showActionsMenu = false;
@ -530,7 +568,7 @@ void Player::ButtonBPressed(void)
void Player::ButtonBReleased(void)
{
if (anyUnitSelected == true)
if (anyUnitSelected != false)
{
TYPE_COLLISION_BLOCK cursor = GetCursorPos();
uint8_t i;
@ -539,21 +577,29 @@ void Player::ButtonBReleased(void)
{
TYPE_UNIT* ptrUnit = &units[i];
if (ptrUnit->selected == true)
if (ptrUnit->selected != false)
{
UnitMoveTo(ptrUnit, cursor.x, cursor.y);
if (showActionsMenu == false)
{
UnitMoveTo(ptrUnit, cursor.x, cursor.y);
}
else
{
showActionsMenu = false;
showActionsMenu_counter = 0;
}
}
}
}
// Reset accumulated counter
/* Reset accumulated counter */
unselectUnits_counter = 0;
}
void Player::ButtonLeftReleased(void)
{
if (showActionsMenu == true)
if (showActionsMenu != false)
{
uint8_t i;
@ -561,10 +607,10 @@ void Player::ButtonLeftReleased(void)
{
TYPE_UNIT* ptrUnit = &units[i];
if (ptrUnit->selected == true)
if (ptrUnit->selected != false)
{
// We need to iterate over all available actions
// for current unit.
/* We need to iterate over all available actions
* for current unit. */
uint8_t availableActions = UnitGetAvailableActions(ptrUnit);
for (uint8_t j = showActionsMenu_index - 1; j != showActionsMenu_index ; j--)
@ -594,7 +640,7 @@ void Player::ButtonRightReleased(void)
void Player::IncreaseShowActionsMenuIndex(void)
{
if (showActionsMenu == true)
if (showActionsMenu != false)
{
uint8_t i;
@ -602,10 +648,10 @@ void Player::IncreaseShowActionsMenuIndex(void)
{
TYPE_UNIT* ptrUnit = &units[i];
if (ptrUnit->selected == true)
if (ptrUnit->selected != false)
{
// We need to iterate over all available actions
// for current unit.
/* We need to iterate over all available actions
* for current unit. */
uint8_t availableActions = UnitGetAvailableActions(ptrUnit);
for (uint8_t j = showActionsMenu_index + 1; j != showActionsMenu_index ; j++)

View File

@ -104,7 +104,7 @@ void SystemRunTimers(void)
void SystemCheckTimer(bool * timer, uint64_t * last_timer, uint8_t step)
{
if (*timer == true)
if (*timer != false)
{
*timer = false;
*last_timer = global_timer;
@ -215,9 +215,9 @@ void SystemUserTimersHandler(void)
for (i = 0; i < SYSTEM_MAX_TIMERS; i++)
{
if (timer_array[i].busy == true)
if (timer_array[i].busy != false)
{
if (System1SecondTick() == true)
if (System1SecondTick() != false)
{
timer_array[i].time--;
@ -225,7 +225,7 @@ void SystemUserTimersHandler(void)
{
timer_array[i].Timeout_Callback();
if (timer_array[i].repeat_flag == true)
if (timer_array[i].repeat_flag != false)
{
timer_array[i].time = timer_array[i].orig_time;
}
@ -276,3 +276,53 @@ bool SystemCollisionCheck(TYPE_COLLISION_BLOCK* c1, TYPE_COLLISION_BLOCK* c2)
return (bool)check_bb_collision( c1->x, c1->y, c1->w, c1->h,
c2->x, c2->y, c2->w, c2->h );
}
bool Systemitoa(char* str, size_t sz, int16_t value)
{
if (sz != 0)
{
bool first_digit_found = false;
uint16_t i;
uint8_t j = 0;
/* Example: 65535 */
/* Another example: -32767 */
if (value & 0x8000)
{
/* Sign bit */
str[j++] = '-';
}
for (i = 10000; i >= 1; i /= 10)
{
uint8_t digit = (uint8_t)(value / i);
value -= (uint16_t)(digit * i);
if (digit != 0)
{
if (first_digit_found == false)
{
first_digit_found = true;
}
}
else if (first_digit_found == false)
{
continue;
}
str[j++] = digit + '0';
if (j >= (sz - 1))
{
return false;
}
}
str[j] = '\0';
return true;
}
return false;
}

View File

@ -85,6 +85,8 @@ void SystemTimerRemove(TYPE_TIMER * timer);
bool SystemArrayCompare(unsigned short * arr1, unsigned short * arr2, size_t sz);
// Checks collision of two objects
bool SystemCollisionCheck(TYPE_COLLISION_BLOCK* c1, TYPE_COLLISION_BLOCK* c2);
// Transforms integer to string. Use this instead of sprintf() as much as possible.
bool Systemitoa(char* str, size_t sz, int16_t value);
/* **************************************
* Global Variables *

26
Unit.c
View File

@ -162,7 +162,7 @@ void UnitDraw(TYPE_UNIT* ptrUnit, TYPE_CAMERA* ptrCamera, bool bHighlighted)
ptrSpr->rotation = GFX_NOROT;
ptrSpr->flip = GFX_NOFLIP;
if (ptrUnit->walking == true)
if (ptrUnit->walking != false)
{
if (++ptrUnit->walk_counter > WALK_FRAMES)
{
@ -176,14 +176,14 @@ void UnitDraw(TYPE_UNIT* ptrUnit, TYPE_CAMERA* ptrCamera, bool bHighlighted)
case DIRECTION_UP:
ptrSpr->flip |= GFX_FLIPV;
if (ptrUnit->mirror == true)
if (ptrUnit->mirror != false)
{
ptrSpr->flip |= GFX_FLIPH;
}
break;
case DIRECTION_DOWN:
if (ptrUnit->mirror == true)
if (ptrUnit->mirror != false)
{
ptrSpr->flip |= GFX_FLIPH;
}
@ -193,7 +193,7 @@ void UnitDraw(TYPE_UNIT* ptrUnit, TYPE_CAMERA* ptrCamera, bool bHighlighted)
ptrSpr->rotation = GFX_ROTCCW;
ptrSpr->flip |= GFX_FLIPH;
if (ptrUnit->mirror == true)
if (ptrUnit->mirror != false)
{
ptrSpr->flip |= GFX_FLIPV;
}
@ -202,7 +202,7 @@ void UnitDraw(TYPE_UNIT* ptrUnit, TYPE_CAMERA* ptrCamera, bool bHighlighted)
case DIRECTION_RIGHT:
ptrSpr->rotation = GFX_ROTCCW;
if (ptrUnit->mirror == true)
if (ptrUnit->mirror != false)
{
ptrSpr->flip |= GFX_FLIPV;
}
@ -232,14 +232,14 @@ void UnitDraw(TYPE_UNIT* ptrUnit, TYPE_CAMERA* ptrCamera, bool bHighlighted)
GfxDrawSprite(ptrSpr);
if ( (bHighlighted == true) || (ptrUnit->selected == true) )
if ( (bHighlighted != false) || (ptrUnit->selected != false) )
{
TYPE_COLLISION_BLOCK cb = CameraApplyCoordinatesToCoordinates(ptrCamera, ptrUnit->x, ptrUnit->y);
int8_t colour = ptrUnit->selected? GFX_BLACK : GFX_GRAY;
uint8_t w = UnitGetWidthFromID(id);
uint8_t h = UnitGetHeightFromID(id);
if (ptrUnit->building == true)
if (ptrUnit->building != false)
{
GfxDrawRectangle(cb.x - (w >> 3), cb.y - (h >> 3), w + (w >> 2), h + (h >> 2), colour);
}
@ -298,7 +298,7 @@ void UnitHandler(TYPE_UNIT* unitArray, size_t sz)
bool bMoving = true;
if (ptrUnit->walking == true)
if (ptrUnit->walking != false)
{
int8_t x_d = 0;
int8_t y_d = 0;
@ -328,7 +328,7 @@ void UnitHandler(TYPE_UNIT* unitArray, size_t sz)
bMoving = false;
}
if (ptrUnit->walking == true)
if (ptrUnit->walking != false)
{
// If player is still walking, check collisions
// against all other active units.
@ -361,17 +361,17 @@ void UnitHandler(TYPE_UNIT* unitArray, size_t sz)
ou.w = UnitGetWidthFromID(ptrOtherUnit->id);
ou.h = UnitGetHeightFromID(ptrOtherUnit->id);
if (SystemCollisionCheck(&cu, &ou) == true)
if (SystemCollisionCheck(&cu, &ou) != false)
{
bMoving = false;
break;
//bMoving = false;
return;
}
}
}
ptrUnit->walking = bMoving;
if (ptrUnit->walking == true)
if (ptrUnit->walking != false)
{
// If no collision is detected, keep moving to the new position
ptrUnit->x += x_d;