Sign in to follow this  

Issue with turn counter

This topic is 668 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

 

xLpGlM0.png

 

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):

ToZHoL8.png

 

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.

Share this post


Link to post
Share on other sites

Don't know that SDL thingie, but you could try doing something like this (I use that in my code with ASyncKeyState tests to avoid repetition) (pseudocode):

bool Buttonstates[<big enough to hold all keys>] = { false};

if (SDL_ButtonPushed[pushedbutton])
{
   if (!Buttonstates[pushedbutton])
   {
      Buttonstates[pushedbutton] = true;
      return SUCCESS;
   }
}
else
{
   if (Buttonstates[pushedbutton])
   {
      Buttonstates[pushedbutton] = false;
   }
   return FAIL;
}

It only registers a buttondown once, user will have to release it and press it again for the routine to register a buttonpress smile.png

Edited by vinterberg

Share this post


Link to post
Share on other sites

Yeah, that was my first thought as well, it looks like it's registering multiple end turn events.  For UI buttons in general, I like to have events occur on Mouse Up, it tends to be how most of Windows does it as well, so it helps the user if you maintain the standard.  (For example, the Post button in this very forum is on Mouse Up)

Edited by ferrous

Share this post


Link to post
Share on other sites

This topic is 668 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this