Stuck in the introduction to C with game development part 8

991c178cbb1237d5e8e147aae5782cb9
0
TTTNL 101 Jan 03, 2013 at 09:47

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 )
{
Pixel* address = m_Screen->GetBuffer();
for ( int i = 0; i < 307200; i++ ) address[i] = i;
}

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

5 Replies

Please log in or register to post a reply.

E607ff029c51f19bc6b6b7002dec7023
0
fdmfdm 101 Jan 03, 2013 at 11:59

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:
cookie[0]
cookie[1]
cookie[2]

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 :)

Ceee4d1295c32a0c1c08a9eae8c9459d
0
v71 105 Jan 04, 2013 at 11:57

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.

991c178cbb1237d5e8e147aae5782cb9
0
TTTNL 101 Jan 04, 2013 at 13:03

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?

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

E607ff029c51f19bc6b6b7002dec7023
0
fdmfdm 101 Jan 04, 2013 at 13:26

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 )
{
    Pixel* address = m_Screen->GetBuffer();
    while (i<401)
       {
       address[i * 641] = i;
            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:

int lineadress;

void Game::Tick( float a_DT )
{
    Pixel* address = m_Screen->GetBuffer();
    while (i<401)
       {

       address[lineadress] = i;
            i++;
           lineadress + 641;
       }
}

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 :D

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

991c178cbb1237d5e8e147aae5782cb9
0
TTTNL 101 Jan 04, 2013 at 22:00

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.