SDL Question!!

The question is in image forms so please click link here to view qns
https://drive.google.com/file/d/0B0g6bHoWNSw8MTZNRXFSUGxlVEU/view?usp=sharing

The given ans are:

The first step is to delete the four drawSprite statements:
1
2
3
4
pCanvas->drawSprite( pSprite, 100, 100 );
pCanvas->drawSprite( pSprite, 400, 100 );
pCanvas->drawSprite( pSprite, 100, 400 );
pCanvas->drawSprite( pSprite, 400, 400 ); 


To produce figure 4.1, we replace the deleted statements with
1
2
3
4
5
6
7
8
for (int i=1; i<=4; i++) 
{
 for (int j=1; j<=4; j++) 
 {
  if (i <= j)
  pCanvas->drawSprite( pSprite, i*100, j*100 );
 }
}


To produce figure 4.2, we replace the deleted statements with
1
2
3
4
5
6
7
8
for (int i=1; i<=4; i++) 
{
 for (int j=1; j<=4; j++) 
 {
  if ((i + j) > 4)
  pCanvas->drawSprite( pSprite, i*100, j*100 );
 }
}


However I would have ans like this: Replace the deleted part with

For figure 4.1, --> keyed in the 10 set of coordinates.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void MyApp::updateGame(float fElapsedTime) 
{
 SDL_Color blue = {0,0,255};
 SDL_Color black = { 0, 0, 0};
 if(pCanvas->clearCanvas()) 
 {
  pCanvas->printText(blue, black, "Using Sprites", solid);
  pCanvas->drawSprite( pSprite, 100, 400 );
  pCanvas->drawSprite( pSprite, 100, 300 );
  pCanvas->drawSprite( pSprite, 200, 300 );
  pCanvas->drawSprite( pSprite, 100, 200 );
  pCanvas->drawSprite( pSprite, 200, 200 );
  pCanvas->drawSprite( pSprite, 300, 200 );
  pCanvas->drawSprite( pSprite, 100, 100 );
  pCanvas->drawSprite( pSprite, 200, 100 );
  pCanvas->drawSprite( pSprite, 300, 100 );
  pCanvas->drawSprite( pSprite, 400, 100 );
 }
}


and

For figure 4.2, --> would have simply added the flipcanvas function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void MyApp::updateGame(float fElapsedTime) 
{
 SDL_Color blue = {0,0,255};
 SDL_Color black = { 0, 0, 0};
 if(pCanvas->clearCanvas()) 
 {
  pCanvas->printText(blue, black, "Using Sprites", solid);
  pCanvas->drawSprite( pSprite, 100, 400 );
  pCanvas->drawSprite( pSprite, 100, 300 );
  pCanvas->drawSprite( pSprite, 200, 300 );
  pCanvas->drawSprite( pSprite, 100, 200 );
  pCanvas->drawSprite( pSprite, 200, 200 );
  pCanvas->drawSprite( pSprite, 300, 200 );
  pCanvas->drawSprite( pSprite, 100, 100 );
  pCanvas->drawSprite( pSprite, 200, 100 );
  pCanvas->drawSprite( pSprite, 300, 100 );
  pCanvas->drawSprite( pSprite, 400, 100 );

  pCanvas->flipCanvas();
 }
}


I am very new to this C++ and SDL. I am not sure if mine is correct or the long way to do. Can please explain how the model ans works and if mine is correct?

Thank you.
Both ways should work but I would prefer the loop solution because it's more compact and easier to work with. If you for some reason decided to have another row you just need to change the loop conditions. If you decide to change how much spacing there should be between the images you just need to change the number that you multiply the loop variables with. Doing the same changes with your solution would require you to do a lot more work and the chance of making a mistake would be higher.

An even better solution in my opinion would be to get rid of the if statements inside the loops by modifying the loop conditions slightly.
Can explain on the loop, I don't understand how the loop produce that output.
Both the loops are different, especially the if loops.
You can think of it as a 4x4 grid where only some of the cells have an image in them. The i variable represents the column and the j variable represents the row. The x coordinate is calculated from the i variable by multiplying it by 100 (use a different number if you want a different distance between the images). The y coordinate is calculated in the same way from the j variable.


Not all cells should contain images so that is what the if statement is used for. In 4.1 you want images if the column number (i) is less than or equal to the row number (j).
 
i <= j


4.2 is a bit more complicated but you can actually do exactly the same as before if you just make sure to count the column number from the right instead of the left. You can do this by subtracting the column number (i) from the grid width (4) and add one (because you want to start counting from 1 instead of 0) which gives you 4 - i + 1. That means you can use the following if condition.
 
4 - i + 1 <= j

We are working with integers so it would be the same if you removed +1 and used < (less than) instead of <= (less than or equal to).
 
4 - i < j

Using simple algebra we can move i and j to the same side.
 
4 < j + i

If we just flip this expression you see that we get the exact same condition that they used in the proposed solution.
 
i + j > 4
Last edited on
Thank You got it =)
Topic archived. No new replies allowed.