欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C語言實(shí)現(xiàn)水波紋效果

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

本文實(shí)例為大家分享了C語言實(shí)現(xiàn)水波紋效果的具體代碼,供大家參考,具體內(nèi)容如下

#include <graphics.h>   
#include <conio.h>
#include <stdio.h>

#define PIC_HEIGHT 600
#define PIC_WIDTH 800

void FrameFun();     // 幀邏輯函數(shù),處理每一幀的邏輯
void RenderFun();     // 幀渲染函數(shù),輸出每一幀到顯示設(shè)備

IMAGE src_img;     // 原位圖 
IMAGE dest_img(PIC_WIDTH, PIC_HEIGHT);  // 處理后顯示的位圖
DWORD *img_ptr1;     // 原圖片片內(nèi)存指針
DWORD *img_ptr2;     // 處理后顯示的位圖內(nèi)存指針


// 以下兩個(gè) buf 為每一個(gè)點(diǎn)的波幅,前者為當(dāng)前波幅,后者為下一個(gè)時(shí)刻的波幅。
short *buf = new short[PIC_HEIGHT*PIC_WIDTH+PIC_WIDTH];
short *buf2 = new short[PIC_HEIGHT*PIC_WIDTH+PIC_WIDTH];


void main()
{
 // 初始化設(shè)備,加載圖片
  initgraph(PIC_WIDTH, PIC_HEIGHT); 
 SetWindowText(GetHWnd(), "Wave-水波紋效果(點(diǎn)擊產(chǎn)生一個(gè)水波紋。移動(dòng)鼠標(biāo)連續(xù)產(chǎn)生水波紋)");
  loadimage(&src_img, "water.jpg"); // 加載圖片,大?。?00*600
 setbkmode(TRANSPARENT);
 settextcolor(BLACK);
 setfont(25, 0, "Arial");

 // 獲得內(nèi)存指針
 img_ptr1 = GetImageBuffer(&src_img);
 img_ptr2 = GetImageBuffer(&dest_img);

 // 初始化波幅數(shù)組
 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(); // 雙緩沖,閃屏?xí)r需要
 while(true) 
 {
 FrameFun();
 RenderFun();
 FlushBatchDraw();
 Sleep(1);
 }
 EndBatchDraw();
}

// 計(jì)算出下一個(gè)時(shí)刻所有點(diǎn)的波幅
void nextFrame()
{
 for(int i = PIC_WIDTH; i < PIC_HEIGHT*(PIC_WIDTH-1); i++)
 {
 // 公式: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];

 // 波能衰減
 buf2[i] -= buf2[i] >> 5;
 }

 short *ptmp = buf;
 buf = buf2;
 buf2 = ptmp;
}

// 處理當(dāng)前時(shí)刻波幅影響之后的位圖,保存在 dest_img 中
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++;
  }
 }
}

// 鼠標(biāo)模擬投石頭
// 參數(shù)說明:
// (x, y): 鼠標(biāo)坐標(biāo)
// stonesize: “石頭”的大小
// stoneweight: 投“石頭”的力度
// Ps: 如果產(chǎn)生錯(cuò)誤,一般就是數(shù)組越界所致,請(qǐng)酌情調(diào)整“石頭”的大小和“石頭”的力度
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;
  }
 }
 }
}

// 計(jì)算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;
}

// 渲染
void RenderFun()
{
 RenderRipple();
 putimage(0, 0, &dest_img);

 char s[5];
 sprintf(s, "%.1f", getFps());
 outtextxy(0, 0, s);
}

// 邏輯
void FrameFun() 
{
 // 鼠標(biāo)
 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();
 }

 // 計(jì)算下一幀的波幅
 nextFrame();
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:C 指針和OC 對(duì)象之間的轉(zhuǎn)換方法

欄    目:C語言

下一篇:c/c++中struct定義、聲明、對(duì)齊方式解析

本文標(biāo)題:C語言實(shí)現(xiàn)水波紋效果

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/871.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有