- 易迪拓培训,专注于微波、射频、天线设计工程师的培养
LabWindows/CVI虚拟仪器设计技术场景与游戏设计之: 下雪场景
(2)程序源代码
//头文件声明
#include "toolbox.h"
#include "asynctmr.h"
#include <utility.h>
#include <ansi_c.h>
#include "下雪场景.h"
//全局静态变量
static int TimerID;
static int panelHandle;
static int Snow[1000][3];
static int PWidth;
static int PHeight;
//声明异步定时器
int CVICALLBACK timer (int reserved, int timerId, int event, void *callbackData, int eventData1, int eventData2);
//主函数
int main (int argc, char *argv[])
{
int i;
if (InitCVIRTE (0, argv, 0) == 0)
return –1;
if ((panelHandle = LoadPanel (0, " 下雪场景.uir", PANEL)) < 0)
return –1;
SetCtrlAttribute (panelHandle, PANEL_CANVAS, ATTR_PEN_COLOR, VAL_WHITE);
SetCtrlAttribute (panelHandle, PANEL_CANVAS, ATTR_PEN_WIDTH, 1);
GetPanelAttribute (panelHandle, ATTR_HEIGHT, &PHeight);
GetPanelAttribute (panelHandle, ATTR_WIDTH, &PWidth);
//用当前时间产生随机数
SetRandomSeed (0);
//随机产生雪花
for (i = 0; i < 1000; i ++)
{
Snow[i][0] = (int) (Random(0, 1) * PWidth);
Snow[i][1] = (int) (Random(0, 1) * PHeight);
Snow[i][2] = 10 + (Random(0, 1) * 5);
}
//装载异步定时器
TimerID = NewAsyncTimer (0.2, –1, 1, timer, 0);
DisplayPanel (panelHandle);
RunUserInterface ();
//释放异步定时器资源
DiscardAsyncTimer (TimerID);
DiscardPanel (panelHandle);
return 0;
}
//面板回调函数
int CVICALLBACK PanelCB (int panel, int event, void *callbackData,
int eventData1, int eventData2)
{
switch (event)
{
case EVENT_CLOSE:
// 停止异步定时器
SuspendAsyncTimerCallbacks ();
// 延时0.2s
Delay (0.2);
QuitUserInterface (0);
break;
}
return 0;
}
//异步定时器——绘制下雪场景
int CVICALLBACK timer (int reserved, int timerId, int event, void *callbackData, int eventData1, int eventData2)
{
int i;
static int OldX;
static int OldY;
static int colory;
static int RGBValue;
switch (event)
{
case EVENT_TIMER_TICK:
// 禁止弹出运行时错误对话框
DisableBreakOnLibraryErrors ();
for (i = 0; i < 1000; i ++)
{
OldX = Snow[i][0];
OldY = Snow[i][1];
Snow[i][1] = Snow[i][1] + Snow[i][2];
if (Snow[i][1] > PHeight)
{
Snow[i][1] = 0;
Snow[i][2] = 5 + ((double)rand () / RAND_MAX * 50);
Snow[i][0] = (double)rand () / RAND_MAX * PWidth;
OldX = 0;
OldY = 0;
}
SetCtrlAttribute (panelHandle, PANEL_CANVAS, ATTR_PEN_COLOR, VAL_BLACK);
// 绘制雪花点
CanvasDrawPoint (panelHandle, PANEL_CANVAS, MakePoint (OldX, OldY));
// 设置雪花灰度渐变色阶
colory = 8 * (Snow[i][2] -10);
colory = 60 + colory;
RGBValue = MakeColor (colory, colory, colory);
SetCtrlAttribute (panelHandle, PANEL_CANVAS, ATTR_PEN_COLOR, RGBValue);
CanvasDrawPoint (panelHandle, PANEL_CANVAS, MakePoint (Snow[i][0], Snow[i][1]));
}
break;
}
return 0;
}
上一篇:LabWindows/CVI虚拟仪器设计技术场景与游戏设计之:
打蜜蜂
下一篇:LabWindows/CVI虚拟仪器设计技术场景与游戏设计之:时钟制作