Sign in to follow this  

C++ Problem with loop

This topic is 1681 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

Hi guys, maybe its because I am tired after staying up all night watching the E3 conference but I cant wrap my head round this..

 

It is a very simple map builder, written in SDL. Its blit-ing all the squares to screen ok, but its not putting in the very first square.

anyway the code :

void Build_Map()
{
	int hor = 0,ver = 0;
	
	while (hor < MAP_SIZE)
	{
		 while (ver < MAP_SIZE)
		{
				
			Map[hor][ver].x = (hor * 30);
			Map[hor][ver].y = (ver * 30);
			
			Map[hor][ver].Set_CLips();
			ver++;
		}
			
				ver = 0;
			
		hor++;
	} 

}


int Drawtoscreen()
{
		
      apply_surface(0,0,background, screen);
		int hor=0,ver=0;
		
		while (hor<=MAP_SIZE)
		{
			while (ver<=MAP_SIZE)
			{
				apply_surface(Map[hor][ver].x ,Map[hor][ver].y , Clipboard[1] , screen , &Map[hor][ver].CLip[2]);
				ver++;
			}
			
			ver = 0;
		hor++;
		}
		//hor = 0;
		//ver = 0;
		
	    apply_surface(0,(SCREEN_HEIGHT - 28), message, screen);
	    
		
		// Draw Player
		apply_surface( 100 , player.Coordsy , Clipboard[0], screen, &player.CLip[ player.ImageChoice ] );
		

	return 0;
}

?

?

?

 

Share this post


Link to post
Share on other sites

Why don't you use for-loops instead of building for loops out of while loops? Maybe you find your error faster if you don't have to deal with unnecessariliy bloated code...

 

And are you sure, your loops in Drawtoscreen are supposed to loop while <= MAP_SIZE; not <MAP_SIZE?

Edited by rnlf

Share this post


Link to post
Share on other sites

Hi rnlf, thanks for the speedy reply there. I have just been adding to the programming as I have been going without tidying up my code as I go, as for the while loops - I think that's because I come from a vb6 background and its what I feel comfy with :D. but point taken. here is the tidy and less bloated code, still getting the same problem though - no map tile drawn to top left of screen, cords would be x=0, y=0.

 

void Build_Map()
{
	int hor,ver;
	
	for (hor =0; hor <MAP_SIZE; hor++)
	{
		 for (ver = 0; ver < MAP_SIZE; ver++)
		{
			Map[hor][ver].x = (hor * 30);
			Map[hor][ver].y = (ver * 30);
			Map[hor][ver].Set_CLips();
		}
	} 

}

int Drawtoscreen()
{
		
      apply_surface(0,0,background, screen);
		int hor =0,ver = 0;

		for (hor =0; hor <MAP_SIZE; hor++)
		{
			for (ver = 0; ver <MAP_SIZE; ver++)
			{
				apply_surface(Map[hor][ver].x ,Map[hor][ver].y , Clipboard[1] , screen , &Map[hor][ver].CLip[2]);
			}
			
		}

		
	    apply_surface(0,(SCREEN_HEIGHT - 28), message, screen);

		apply_surface(player.Coordsx , player.Coordsy , Clipboard[0], screen, &player.CLip[ player.ImageChoice ] );
		
	

	return 0;
}

Share this post


Link to post
Share on other sites

don't worry, I have sorted it.. guess I woke up :D

replaced

Map[hor][ver].x = (hor * 30);
Map[hor][ver].y = (ver * 30);

with:

Map[hor][ver].x = ((hor - 1) * 30);
Map[hor][ver].y = ((ver - 1) * 30);

 

 

Thank you for taking the time to look and I wouldn't have sorted it if I didn't change to a for loop. so cheers again

Share this post


Link to post
Share on other sites
Sign in to follow this