C language realizes water ripple effect
- 2020-06-03 07:31:45
- OfStack
This paper shares the concrete code of C language to realize the effect of water ripple for your reference. The specific content is as follows
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#define PIC_HEIGHT 600
#define PIC_WIDTH 800
void FrameFun(); // Frame logic function that handles each 1 The logic of the frame
void RenderFun(); // Frame rendering function, output each 1 Frame to display device
IMAGE src_img; // In situ figure
IMAGE dest_img(PIC_WIDTH, PIC_HEIGHT); // The bitmap displayed after processing
DWORD *img_ptr1; // Original slice memory pointer
DWORD *img_ptr2; // The bitmap memory pointer displayed after processing
// The following two buf For each 1 The amplitude of a point, the former is the current amplitude, the latter is the bottom amplitude 1 The amplitude of a moment.
short *buf = new short[PIC_HEIGHT*PIC_WIDTH+PIC_WIDTH];
short *buf2 = new short[PIC_HEIGHT*PIC_WIDTH+PIC_WIDTH];
void main()
{
// Initialize the device and load the image
initgraph(PIC_WIDTH, PIC_HEIGHT);
SetWindowText(GetHWnd(), "Wave- Water ripple effect (click to create 1 A ripple of water. Continuously generate water ripples by moving the mouse) ");
loadimage(&src_img, "water.jpg"); // Load picture, size: 800*600
setbkmode(TRANSPARENT);
settextcolor(BLACK);
setfont(25, 0, "Arial");
// Get a memory pointer
img_ptr1 = GetImageBuffer(&src_img);
img_ptr2 = GetImageBuffer(&dest_img);
// Initializes the amplitude array
memset(buf, 0, (PIC_HEIGHT*PIC_WIDTH+PIC_WIDTH) * sizeof(short));
memset(buf2, 0, (PIC_HEIGHT*PIC_WIDTH+PIC_WIDTH) * sizeof(short));
// Let's Go!
BeginBatchDraw(); // Double buffering is required for flash screen
while(true)
{
FrameFun();
RenderFun();
FlushBatchDraw();
Sleep(1);
}
EndBatchDraw();
}
// To calculate the 1 The amplitude of waves at all points in time
void nextFrame()
{
for(int i = PIC_WIDTH; i < PIC_HEIGHT*(PIC_WIDTH-1); i++)
{
// Formula: X0'= (X1+X2+X3+X4) / 2 - X0
buf2[i] = ((buf[i-PIC_WIDTH] + buf[i+PIC_WIDTH] + buf[i-1] + buf[i+1]) >> 1) - buf2[i];
// Wave energy attenuation
buf2[i] -= buf2[i] >> 5;
}
short *ptmp = buf;
buf = buf2;
buf2 = ptmp;
}
// Handle the current moment after the amplitude effect of the bitmap, save in dest_img In the
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];
// The offset
int a = ((x - PIC_WIDTH / 2) * data / 1024) + PIC_WIDTH / 2;
int b = ((y - PIC_HEIGHT / 2) * data / 1024) + PIC_HEIGHT / 2;
// Boundary processing
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;
// Deal with deviation
img_ptr2[i] = img_ptr1[a + (b * PIC_WIDTH)];
i++;
}
}
}
// Mouse simulation throw stone
// Parameter description:
// (x, y): The mouse coordinates
// stonesize: The size of the rock
// stoneweight: The force of a rock
// Ps: If an error occurs, 1 General is the array caused by the boundary, please adjust the size of the "stone" and the strength of the "stone"
void disturb(int x, int y, int stonesize, int stoneweight)
{
// Crossing the boundary is not handled
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;
}
}
}
}
// To calculate fps
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;
}
// Apply colours to a drawing
void RenderFun()
{
RenderRipple();
putimage(0, 0, &dest_img);
char s[5];
sprintf(s, "%.1f", getFps());
outtextxy(0, 0, s);
}
// logic
void FrameFun()
{
// The mouse
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();
}
// Under the calculation 1 The frame of the amplitude
nextFrame();
}