hitcounter
dpointer
Wednesday, September 13, 2006
 
Underwater effect

Basic idea: shift the pixel in horizontal and vertical direction sinusoidally.

Here it is assumed that buffer is the off-screen buffer (will be copied to the video buffer), width and height hold the dimension of the buffer. tick is the time variable, in this example it is taken using SDL_GetTicks function. Divide by 4 causes the oscillation period to be 4 seconds. Bonus for readers who figure out why it is masked with 1023 instead of modulo 1000.

In real-world, these calculations are expensive and thus should be helped with some look-up tables, at least for the sine function. Everything will look simpler.

 unsigned char* ptr = buffer;
 int amp = width/64;
 const int tick = (SDL_GetTicks()/4) & 1023;
 for(int yi = 0; yi < height; yi++, ptr+= width)
 {
  int shift = amp/2 + amp/2*sin((4*yi/(double)height + tick/(double)1024)*2*M_PI);
  memmove(ptr+shift, ptr, width-shift);
 }
 for(int xi = 0; xi < width; xi++)
 {
  int shift = amp/2 + amp/2*sin((4*xi/(double)width + tick/(double)1024)*2*M_PI);
  ptr = buffer + xi + (height-1)*width;
  for(int c = 0; c < height-shift; c++, ptr -= width)
   *ptr = *(ptr-shift*width);
 }

underwater effect

Update: here is the screencast.



Powered by Blogger