Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: Storm Trooper on July 21, 2010

Title: pointers
Post by: Storm Trooper on July 21, 2010
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
Title: Re: pointers
Post by: hellfire on July 21, 2010
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.