Hello, I have been writing a turn based strategy game for around the last month. It is almost done but there is one critical issue, my turn counter bugs out when clicked.
I have been used SDL2's event library to handle clicks and it tends to click the buttons for quite a while
This isn't really an issue for when the units and tiles are selected and when pathfinding is performed, but when the next turn button is clicked it goes absolutely nuts in the status window(this is from turn 1 by the way):
I am not really sure what exactly causes this, but it seems to be either the main loop or the input functions.
while( oneSideIsDead != TRUE )
{
turnButtonISClicked = 0;
oldUnitSelected = 0;
if( turnIsChanged == 1 )
{
turnIsChanged = 0;
turnNumber = turnNumber + 1;
fprintf( stdout , "Turn : %d\n" , turnNumber );
turnButtonISClicked = 0;
}
for( looper = 1 ; looper <= map->sides[ Side ]->numberUnits ; looper++ )
{
map->sides[ whichSide ]->units[looper]->selected = FALSE;
}
fprintf(stdout, "works 2\n");
for( loopertwo = 0 ; loopertwo < map->tiles[0]->numberTiles ; loopertwo++ )
{
map->tiles[loopertwo]->isSelected = FALSE;
}
fprintf(stdout, "works 3\n");
if( turnNumber % 2 == 1 )
{
Side = 0;
enemySide = 1;
}
else
{
Side = 1;
enemySide = 0;
}
while( turnButtonIsClicked != END_TURN_BUTTON_CODE )
{
if( turnButtonIsClicked != 0 )
{
oldUnitSelected = turnButtonIsClicked;
}
turnButtonIsClicked = 0;
start = clock();
SDL_RenderClear( renderer );
if( time % 1 == 0 )
{
turnButtonIsClicked = handleMapBeenClicked( map->sides[ Side ] , map->sides[ enemySide ] , map->tiles , nextTurn , events );
if( ( oldUnitSelected == UNIT_IS_SELECTED && turnButtonIsClicked == TILE_IS_SELECTED) || ( oldUnitSelected == UNIT_IS_SELECTED && turnButtonIsClicked == UNIT_IS_SELECTED_OTHER ) )
{
for( looper = 1 ; looper <= map->sides[ Side ]->numberUnits ; looper++ )
{
if( map->sides[ whichSide ]->units[looper]->selected == TRUE )
{
break;
}
}
for( looperthree = 1 ; looperthree <= map->sides[ enemySide ]->numberUnits ; looperthree++ )
{
if( map->sides[ otherSide ]->units[looperthree]->selected == TRUE )
{
break;
}
}
for( loopertwo = 0 ; loopertwo < map->tiles[0]->numberTiles ; loopertwo++ )
{
if( map->tiles[loopertwo]->isSelected == TRUE )
{
break;
}
}
if( i != map->sides[ Side ]->numberUnits + 1 && !( loopertwo == map->tiles[0]->numberTiles ) && map->sides[ Side ]->units[looper]->selected == TRUE && map->tiles[loopertwo]->isSelected == TRUE && oldUnitSelected == UNIT_IS_SELECTED && turnButtonIsClicked == TILE_IS_SELECTED )
{
Mix_PlayChannel( -1 , marchSound , 0 );
moveUnit( map->sides[ Side ], map->tiles , map->tiles[loopertwo]->relativeX , map->tiles[loopertwo]->relativeY , looper );
}
if(looper != map->sides[ Side ]->numberUnits + 1 && map->sides[ Side ]->units[looper]->selected == TRUE && looperthree != map->sides[ enemySide ]->noUnits + 1 && map->sides[ enemySide ]->units[looperthree]->selected == TRUE && oldIsSelected == UNIT_IS_SELECTED && turnButtonIsClicked == UNIT_IS_SELECTED_OTHER )
{
Mix_PlayChannel( -1 , shootSound , 0 );
defendingSideCasualties = shootUnit( map->sides[ Side ] , looper , map->sides[ enemySide ] , looperthree , map->tiles );
attackingSideCasualties = shootUnit( map->sides[ enemySide ] , looper , map->sides[ Side ] , looper , map->tiles );
resolveShooting( map->sides[ Side ] , looper , map->sides[ Side ] , looperthree , defendingSideCasualties , attackingSideCasualties );
map->sides[ Side ]->units[looper]->selected = FALSE;
map->sides[ enemySide ]->units[looperthree]->selected = FALSE;
}
}
}
if( events->type == SDL_QUIT )
{
*success = FAIL;
return;
}
and here is the relevant input code for the turn button:
fprintf( stderr , "Checking if the button is pressed....\n" );
if(SDL_HasIntersection( mouseDimensions , &( button->dimensionsAndSize ) ) == SDL_TRUE )
{
return SUCCESS;
}
else
{
return FAIL;
}
return NULL_INPUT_VALUE;
If you need anything else to help you figure this out please don't hesitate to ask. This is at this point the only thing really holding me back and I am not sure why it does this.
Thanks a lot.