#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#define PIC_HEIGHT 600
#define PIC_WIDTH 800
void FrameFun();
void RenderFun();
IMAGE src_img;
IMAGE dest_img(PIC_WIDTH, PIC_HEIGHT);
DWORD *img_ptr1;
DWORD *img_ptr2;
short *buf = new short[PIC_HEIGHT*PIC_WIDTH+PIC_WIDTH];
short *buf2 = new short[PIC_HEIGHT*PIC_WIDTH+PIC_WIDTH];
void main()
{
initgraph(PIC_WIDTH, PIC_HEIGHT);
SetWindowText(GetHWnd(), "Wave- ( 。 )");
loadimage(&src_img, "water.jpg");
setbkmode(TRANSPARENT);
settextcolor(BLACK);
setfont(25, 0, "Arial");
img_ptr1 = GetImageBuffer(&src_img);
img_ptr2 = GetImageBuffer(&dest_img);
memset(buf, 0, (PIC_HEIGHT*PIC_WIDTH+PIC_WIDTH) * sizeof(short));
memset(buf2, 0, (PIC_HEIGHT*PIC_WIDTH+PIC_WIDTH) * sizeof(short));
BeginBatchDraw();
while(true)
{
FrameFun();
RenderFun();
FlushBatchDraw();
Sleep(1);
}
EndBatchDraw();
}
void nextFrame()
{
for(int i = PIC_WIDTH; i < PIC_HEIGHT*(PIC_WIDTH-1); i++)
{
buf2[i] = ((buf[i-PIC_WIDTH] + buf[i+PIC_WIDTH] + buf[i-1] + buf[i+1]) >> 1) - buf2[i];
buf2[i] -= buf2[i] >> 5;
}
short *ptmp = buf;
buf = buf2;
buf2 = ptmp;
}
void RenderRipple()
{
int i = 0;
for (int y = 0; y < PIC_HEIGHT; y++)
{
for (int x = 0; x < PIC_WIDTH; x++)
{
short data = 1024 - buf[i];
int a = ((x - PIC_WIDTH / 2) * data / 1024) + PIC_WIDTH / 2;
int b = ((y - PIC_HEIGHT / 2) * data / 1024) + PIC_HEIGHT / 2;
if (a >= PIC_WIDTH) a = PIC_WIDTH - 1;
if (a < 0) a = 0;
if (b >= PIC_HEIGHT) b = PIC_HEIGHT - 1;
if (b < 0) b = 0;
img_ptr2[i] = img_ptr1[a + (b * PIC_WIDTH)];
i++;
}
}
}
void disturb(int x, int y, int stonesize, int stoneweight)
{
if ((x >= PIC_WIDTH - stonesize) ||
(x < stonesize) ||
(y >= PIC_HEIGHT - stonesize) ||
(y < stonesize))
return;
for (int posx=x-stonesize; posx<x+stonesize; posx++)
{
for (int posy=y-stonesize; posy<y+stonesize; posy++)
{
if ((posx-x)*(posx-x) + (posy-y)*(posy-y) < stonesize*stonesize)
{
buf[PIC_WIDTH*posy+posx] += stoneweight;
}
}
}
}
float getFps()
{
#define FPS_COUNT 8
static i = 0;
static oldTime = GetTickCount();
static float fps;
if (i > FPS_COUNT)
{
i = 0;
int newTime = GetTickCount();
int elapsedTime = newTime - oldTime;
fps = FPS_COUNT / (elapsedTime / 1000.0f);
oldTime = newTime;
}
i++;
return fps;
}
void RenderFun()
{
RenderRipple();
putimage(0, 0, &dest_img);
char s[5];
sprintf(s, "%.1f", getFps());
outtextxy(0, 0, s);
}
void FrameFun()
{
if(MouseHit())
{
MOUSEMSG msg = GetMouseMsg();
if(msg.uMsg == WM_MOUSEMOVE)
{
disturb(msg.x, msg.y, 3, 256);
}
else if(msg.uMsg == WM_LBUTTONDOWN)
{
disturb(msg.x, msg.y, 3, 2560);
}
FlushMouseMsgBuffer();
}
nextFrame();
}