设为首页 加入收藏

TOP

(Visual C++)游戏开发笔记二十二 游戏基础物理建模(四) 粒子系统模拟(一) (三)
2014-11-23 19:42:54 】 浏览:510
Tags:Visual 游戏 开发 笔记 二十二 基础 物理 建模 子系统 模拟
***************
// 1.窗口贴图
// 2.实现雪花纷飞的效果
void MyPaint(HDC hdc)
{

//创建粒子
if(count<50) //当粒子数小于50时,产生新的粒子,设定每个粒子的属性值
{
drop[count].x = rand()%rect.right; //将粒子的X坐标设为窗口中水平方向上的任意位置
drop[count].y = 0; //将每个粒子的Y坐标都设为"0",即从窗口上沿往下落
drop[count].exist = true; //设定粒子存在
count++; //每产生一个粒子后进行累加计数
}


//贴上背景图到mdc中
SelectObject(bufdc,bg);
BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);

//首先判断粒子是否存在,若存在,进行透明贴图操作
for(i=0;i<50;i++)
{

Sleep(1);
if(drop[i].exist)
{
SelectObject(bufdc,mask);
BitBlt(mdc,drop[i].x,drop[i].y,20,20,bufdc,0,0,SRCAND);

SelectObject(bufdc,snow);
BitBlt(mdc,drop[i].x,drop[i].y,20,20,bufdc,0,0,SRCPAINT);
if(rand()%2==0)
drop[i].x+=5;
else
drop[i].x-=5;
drop[i].y+=10;
if(drop[i].y > rect.bottom)
{
drop[i].x = rand()%rect.right;
drop[i].y = 0;
}
}

}


//将mdc中的全部内容贴到hdc中
BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);

}


//****消息处理函数***********************************
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_TIMER: //时间消息
MyPaint(hdc); //在消息循环中加入处理WM_TIMER消息,当接收到此消息时便调用MyPaint()函数进行窗口绘图
break;
case WM_KEYDOWN: //按键消息
if(wParam==VK_ESCAPE) //按下【Esc】键
PostQuitMessage(0);
break;
case WM_DESTROY: //窗口结束消息
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(bg);
DeleteObject(snow);
DeleteObject(mask);
KillTimer(hWnd,1); //窗口结束时,删除所建立的定时器
ReleaseDC(hWnd,hdc);
PostQuitMessage(0);
break;
default: //其他消息
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
#include "stdafx.h"
#include

//全局变量声明
HINSTANCE hInst;
HBITMAP bg,snow,mask; //用于贴图的三个HBITMAP变量
HDC hdc,mdc,bufdc;
HWND hWnd;
RECT rect;
int i,count; //定义count用于计数

struct snow
{
int x;
int y;
BOOL exist;
}drop[50];


//全局函数声明
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);

//****WinMain函数,程序入口点函数**************************************
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

MyRegisterClass(hInstance);

//初始化
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}


//消息循环
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

//****设计一个窗口类,类似填空题,使用窗口结构体*********************
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wc

首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇(Visual C++)游戏开发笔记二十.. 下一篇(Visual C++)游戏开发笔记十八 ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目