Why this code doesn't work ?!

Please could you say me where is the problem ?
Thanks a lot

void bump(SDL_Surface *image, SDL_Surface *screen)
{
SDL_Event event;
int nx,ny,x,y,offset,done;
float lx,ly,count,nX,nY,nZ;
unsigned char *envmap;
SDL_Surface *bumpmap = SDL_AllocSurface(SDL_HWSURFACE,320,200,32,0xFF0000,0xFF00,0xFF,0);
int *imageptr = (int *)image->pixels;
int *bumpmapptr = (int *)bumpmap->pixels;
SDL_LockSurface(image);
SDL_LockSurface(bumpmap);

for (y=0;y<256;y++)
{
for (x=0;x<256;x++)
{
nX=(x-128)/128.0;
nY=(y-128)/128.0;
nZ=1-sqrt(nXnX+nYnY);
if (nZ<0) nZ=0;
envmap[x+(y<<8)] = ambient + diffuse * nZ;
}
}

for (done=0; !done; )
{
/* Look for a key or quit event */
while ( SDL_PollEvent(&event) )
{
if ( (event.type == SDL_KEYDOWN) || (event.type == SDL_QUIT) )
done = 1;
}
count++;
lx = cos(count/vitesse)*amplitude+288;
ly = sin(count/vitesse)*amplitude+228;

for (y=0;y<200;y++)
{
for (x=0;x<320;x++)
{
// attribue une valeur selon les normales de ses voisins
ny = nx = imageptr[offset+1] - imageptr[offset-1];
// Ajuste les pseudo-normales selon la source lumineuse
nx-=(x-lx);
ny-=(y-ly);

 // On s'assure d'etre dans les limites
 if (nx>255 || nx<0) nx=255;
 if (ny>255 || ny<0) ny=255;

 bumpmapptr[offset++] = envmap[nx+(ny<<8)];

}
}
SDL_BlitSurface(image, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
SDL_UnlockSurface(bumpmap);
SDL_UnlockSurface(image);
}

Um…how about you tell us what the problem is, and then someone may be able
to help you find out where it is?On Tue, Jan 23, 2001 at 10:06:10AM -0800, Arnaud DESMONS wrote:

Please could you say me where is the problem ?
Thanks a lot


Martin

Bother! said Pooh, as he put the message in the wrong conference.

Please could you say me where is the problem ?

tell us what it is supposed to do, and what you observe instead

problems I can see right away:

  • you forget to check the return values of SDL_AllocSurface and
    SDL_LockSurface for errors
  • you lock the surfaces after reading the surface->pixels pointers
  • you assume the lines of the surfaces are contiguous in memory instead
    of using surface->pitch
  • you assume int is 32 bits
  • you call SDL_BlitSurface() while holding a lock to one of the surfaces
  • you never allocate envmap

Please could you say me where is the problem ?

tell us what it is supposed to do, and what you observe instead

problems I can see right away:

And what kind of fun you’re likely to see: :slight_smile:

  • you forget to check the return values of SDL_AllocSurface and
    SDL_LockSurface for errors

Nothing special if you’re lucky - segfault otherwise.

  • you lock the surfaces after reading the surface->pixels pointers

Invalid pointers -> segfault as soon as you try to do something with
tho pixels.

  • you assume the lines of the surfaces are contiguous in memory
    instead of using surface->pitch

Nothing special on some targets; horizontal “rolling” on other
targets.

  • you assume int is 32 bits

Will blow up on anything 16 bit, and may blow up on anything 64 bit.

  • you call SDL_BlitSurface() while holding a lock to one of the
    surfaces

The blit may not be performed at all, AFAIK.

  • you never allocate envmap

Segfault.

//David

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Tuesday 23 January 2001 20:43, Mattias Engdeg?rd wrote:

Nothing special if you’re lucky - segfault otherwise.

me, too

Invalid pointers -> segfault as soon as you try to do something with
tho pixels.

me, too

Nothing special on some targets; horizontal “rolling” on other
targets.

me, too

Will blow up on anything 16 bit, and may blow up on anything 64 bit.

me, too

The blit may not be performed at all, AFAIK.

me, too

Segfault.

me, too

Okay, martin, you owe me a beer.

m.–
“Advocating a system based on consumption is the ultimate violence
against poor nations.”
-Masamune Shirow