• Welcome to Dark Bit Factory & Gravity.
 

pointers

Started by Storm Trooper, July 21, 2010

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Storm Trooper

how do pointers help to give a turbo charge to rendering?
and how do you use them?
say for example, a usual FOR X and Y LOOP.

thanks again,
tR4nt0r

hellfire

let's assume you want to fill an image which is simply an array of width*height integers.

instead of adressing each pixel individually, like this:

for (y=0; y < height; y++)
{
  for (x=0; x < width; x++)
  {
    adress= y * width + x;
    image[adress] = color;
  }
}


...you just increase your pointer when stepping from one pixel to the next:

dest= image;
for (y=0; y < height; y++)
{
  for (x=0; x < width; x++)
  {
    *dest= color;
    dest++;
  }
}


any half-decent compiler will do this optimization automatically for you, though.
Challenge Trophies Won: