Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


C++ Problem with loop


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 bluwind   Members   -  Reputation: 181

Like
0Likes
Like

Posted 11 June 2013 - 03:49 AM

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;
}







 



Sponsor:

#2 rnlf   Members   -  Reputation: 1185

Like
1Likes
Like

Posted 11 June 2013 - 04:04 AM

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, 11 June 2013 - 04:06 AM.

my blog (German)


#3 bluwind   Members   -  Reputation: 181

Like
0Likes
Like

Posted 11 June 2013 - 04:17 AM

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;
}


#4 bluwind   Members   -  Reputation: 181

Like
1Likes
Like

Posted 11 June 2013 - 04:19 AM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS