Equation help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void Layer::DrawLayer(int maxx, int maxy)
{
	SDL_Rect loc;
	loc.x = 0;
	loc.y = 0;
	for (int i = 0; i < (maxx*maxy); i++)
	{
		if(i == 0)
		{
			loc.x = 0;
		}
		else
		{
			loc.x += 32;
		}

		if((i + 1) % maxy == 0)
		{
			loc.x = 0;
			loc.y += 32;
		}

		myTile[theLayer[i]].DrawTile(loc);
	}
}


there is something wrong with my algorithmic/equation

what im trying to do is that im trying to create rows
you are given the maxX and maxY (which is the width and the height resspectively. but time by 32)

Since i dont want to touch multidimensional array, i coded it so that u have maxX and MaxY time with each other to create the total amount of tile u have. and when u want to draw all the tile (row by row) u have the maximum tile divide by maxy.

this code doesnt give any compiler error, but for some reason the last tile on the first row isnt draw at all. (the top right corner). Im pretty sure it is because of the way i wrote my equation.
Line 17, shouldn't the test be if( i % maxy == 0 ) ?
if i write that line

the first row is skipped
The problem is that you go to the next row before drawing the last tile of the current one.
What if you move line 23 before the test line 17 ?
Btw, since you're checking to see if you've reached the end of a row, the test should compare to the width (maxX), not the heigth.
What if you move line 23 before the test line 17 ?

then the first column except the first tile (the top left corner) doesnt draw

and the reason i have maxX instead of maxy is because i is the product of MaxX and MaxY

by dividing "i" by maxy i get MaxX as the result, which is the total of tile per row.

if i was divide it by Maxx, i would get maxy as the answer, which is how many row total
"%" is not a division but a modulus.


1
2
3
4
5
6
7
8
9
10
11
12
13
for (int i = 0; i < (maxx*maxy); i++)
{
	myTile[theLayer[i]].DrawTile(loc);
	if((i + 1) % maxx == 0)
	{
		loc.x = 0;
		loc.y += 32;
	}
        else
	{
		loc.x += 32;
	}
}
Last edited on
holy cow it work

thank you :O :D :3 <3
Topic archived. No new replies allowed.