# 2D Water effect woes...

This topic is 4068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello all, Has anyone ever coded a successful 2D water effect ala :
Ooooo... Pretty Water
Right now, after much ( to much ) effort, my looks like:
The left side is the background texture ( attempted to be ) modulated by the water calculations. The right is the actual modulating water. The problem w/the tutorials, is that none of them actual show what the modulating water is supposed to look like. Cause mine looks like it is lacking "smoothness". Here is the offending code:
/*
==========================================================================
LVL_UnderWater_CalcNewWater

Calcuates water.
==========================================================================
*/
void LVL_UnderWater_CalcNewWater( void )
{
static int xLimit = (currentGame.gfxConfig.xDimen/2 - 1);
static int yLimit = (currentGame.gfxConfig.yDimen - 1);
static int i=0;
int average=0;

for( int y=1; y<yLimit; y++ )
{
for( int x=1;x<xLimit;x++ )
{
average =
( (_getpixel16( oldWaterBuffer , x + 1 , y ) +
_getpixel16( oldWaterBuffer , x - 1 , y ) +
_getpixel16( oldWaterBuffer , x , y + 1  ) +
_getpixel16( oldWaterBuffer , x , y - 1 )  +

_getpixel16( oldWaterBuffer , x - 1 , y + 1 )  +
_getpixel16( oldWaterBuffer , x - 1 , y - 1 )  +
_getpixel16( oldWaterBuffer , x + 1 , y + 1 )  +
_getpixel16( oldWaterBuffer , x + 1 , y - 1 )  ) >> 2 ) -

_getpixel16( newWaterBuffer , x , y );

average -= _getpixel16( newWaterBuffer , x , y )>>5;

if( average < 0 )
{
average = 0;
}
if( average > 255 )
{
average = 255-average;
average %= average;
}

_putpixel16( newWaterBuffer , x , y , average  );
}

}

i++;
}
/*
===========================================================================
LVL_UnderWater_PaintWater

Simulated dynamic water in offscreen buffer.
===========================================================================
*/
void LVL_UnderWater_PaintWater( float dampening )
{
int deltaX=0,deltaY=0,indexX=0,indexY=0,color=0,texture=0;

for( int y = 87;y < 394;y++ )
{
for( int x = 12;x < 304;x++ )
{
deltaX = _getpixel16( newWaterBuffer , x , y ) - _getpixel( newWaterBuffer , x + 1 , y );
deltaY = _getpixel16( newWaterBuffer , x , y ) - _getpixel( newWaterBuffer , x , y + 1 );

//deltaX >>= 3;
//deltaY >>= 3;

indexX = deltaX + x;
indexY = deltaY + y;

color = indexX;
texture = _getpixel16( waterLevel , x , y );

if( color < 0 )
{
color = 1;
}
if( color > 255 )
{
color = 254;
}

_putpixel16( pageTwo , x , y , color + texture );

_putpixel16( pageThree , x , y , _getpixel16(newWaterBuffer , x ,y ) );

// save rendered water for next frame
_putpixel16( oldWaterBuffer , x , y , _getpixel16( newWaterBuffer , x , y ) );

}
}

}


Please excuse the code. After a weekends worth of work, it has degenerated to a hacked-compile-n-hopes-it-works-this-time coding terror. Thanks a lot, ahayweh

##### Share on other sites
n/m got it working.

##### Share on other sites

This topic is 4068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628670
• Total Posts
2984152

• 13
• 10
• 10
• 9
• 9