Stuck in the introduction to C with game development part 8

5 replies to this topic

#1TTTNL

Member

• Members
• 48 posts

Posted 03 January 2013 - 09:47 AM

This tutorial

I understood everything up until part 8, the whole concept of pointers and pixels. This function in particular:

void Game::Tick( float a_DT )
{
for ( int i = 0; i < 307200; i++ ) address[i] = i;
}


what is meant by adress and where does the i in address[i] refer to? And could someone explain what Pixel* does?

#2fdmfdm

Member

• Members
• 25 posts

Posted 03 January 2013 - 11:59 AM

well, i have done that tutorial, and to be quite honest, it is not that important at this point to know where the i stands for, as arrays will come later,
but to give you a general idea, it stands for which pixel you are asking the adress for,
now your first question was what is meant by adress, it is simply the place where the information about that pixel is places, so is is really just the adress of the information.
then the i, as said before it stands for which pixel you want.
let take the screen of 640 by 480,
that means you have 307200 pixels.
you said that on the location of the pixel you wanted the informatiobn address

maybe that was not clear enough?
lets just look at Pixel*
Pixel* simply puts address as the location of writing, in this case in the buffer of your screen (so the screen you are drawing on, not the screen you are see at that moment (i trust you know by now how the drawing process works, it is explained somewhere in that tutorial not sure hwere)
you have the for loop, which you must know how it works,
and then in that loop stands
address[i] = i;

this is actually incredible simple if you know what a array is,
and array is a collection of many variables with the same name.
like:

this is a small array,
but you use an array for the pixels on your screen, or rather for the address of the pixels on your screen (because a pointer points to a address)
and it puts the value i on that location. thats why if you run this code you will see all pixels have different collors, to a certain extend. they will all be blueish if i remember right.
this is because the way colors are put in the memory, but again this is something you will learn later.

so in short:
the i refers to which pixellocation in the array is meant, and Pixel* points to the location of the Pixelarray, which you have never seen, but is somewhere in the screenfunction in the template

hope i did a good job explaining, as im still quite new myself

#3v71

Valued Member

• Members
• 353 posts

Posted 04 January 2013 - 11:57 AM

Maybe he has an hard time understanding how a linear array maps into a square frame buffer , actually the formula is pretty simple
its is : x+y*ResX , where ResX is the horizontal resolution of your window. A lot of tricks can be performed when the pixel writing happens in loops, like precomputation to avoid an expensive multiplication inside a tight for loop.
In the example abov you are simply filling all the buffer with a single color. Another fact is that you have 3 bytes for each color so actually you need a buffer which is 3 times wide and perform an aligned write in 3 consecutive bytes, but this is something you should inspect further after you have fully grasped the concept of mapping buffers with linear arrays , whichbasically points out to the earlier formula.
Check my code in the c/c++ section :
http://www.binpress.com/browse/c

#4TTTNL

Member

• Members
• 48 posts

Posted 04 January 2013 - 01:03 PM

Thanks for the help, but could somebody explain assignment 1 of that tutorial? It still isn't really clear to me how to get those coordinates in the funciton.
Also, what does the plot function mean?

Quote

DDraw a dotted line (skip every other dot) from (0, 0) to (400, 400). Do not use the Plot function, use a pointer variable instead. Figure out what the distance between dots in memory is to make this code super simple and fast.

#5fdmfdm

Member

• Members
• 25 posts

Posted 04 January 2013 - 01:26 PM

well, the plot function would be a function that plots something, as obvious as it may sound, think about it.
i take it you are dutch (considering your name) but you have had math at school,
you probably used a graphic calculator there, on which you had to plot a function.
a plotfunction seems nothing more then a function to calculate where and what should be placed on the screen.

but for assignment 1.
the first step you want to do with something like this is think
they want you to use a pointer variable after this tutorial, where did you use it earlier in this tutorial?

void Game::Tick( float a_DT )
{
Pixel* address = m_Screen->GetBuffer() + 100;
for ( int i = 0; i < 255; i++ ) address[i * 640] = i;
}


now you can say this is a plot function, but it is what they mean i think, because you are not pointing at a pixel, but at the loction of the pixel
now lets think, they want you to go from 0,0 to 400,00. that meant everytime you go down 1 pixel, you will need to go 1 to the right as well.
the standart screen is 640 in that tutorrial i think, so if you know how the array works for the pixels (it simply keeps going to the right, pixel 0 is the first one on the top left, pixel 1 is the second one, and pixel 639 is the last one on the top row.
pizel 640 at the other hand is the first pixel on the second row,

so the code should be something like:

void Game::Tick( float a_DT )
{
while (i<401)
{
i++;
}
}


now not a couple things, because you want to go to 400 i did <401 you can also make it a lower of equal to 400, just like you can do:


void Game::Tick( float a_DT )
{
while (i<401)
{

i++;
}
}


or many other forms of code for the same thing, one is a bit faster then the other, but at this point i dont thing thats really important, its more important that you understand the concept
now this is simply for a line from 0,0 to 400,400
think of a way to make it a dotted line, which is quite simple, if you can do that, you should understand the general concept good enough for this tutorial

if you still dont understand, you might want to check out #10 of that tutorial, which is about arrays

#6TTTNL

Member

• Members
• 48 posts

Posted 04 January 2013 - 10:00 PM

Thanks! It took me some effort but the pieces are falling together. And i figured out how to make it dotted! By accident really but hey it works and because of that i get it.

Thanks to everyone in this topic.

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users