• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# 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.

3 replies to this topic

### #1bluwind  Members

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

﻿

﻿

﻿

### #2rnlf  Members

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.

### #3bluwind  Members

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

### #4bluwind  Members

Posted 11 June 2013 - 04:19 AM

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

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.