主题:粒子系统
由粒子系统(5000个粒子)实现的黄金火焰十字架。
平台DOS + Watcom C
[img]http://www.wodutom.com/coolwindows/10z.PNG[/img]
这个是50000个粒子形成的粒子星云
[img]http://www.wodutom.com/coolwindows/star.PNG[/img]
#include <i86.h>
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include "x\xsvga.hpp"
#include "x\xtimer.hpp"
#include "x\xkey.hpp"
#include "x\xpsystem.hpp"
#include "x\xmath.hpp"
cMath *pmath;
int angle=0;
/*坐标更新*/
cParticleF2D& drop(cParticleF2D& x,cParameterQueue& paras)
{
float t;
paras.Peek(&t);
x.vx+=x.ax*t;
x.vy+=x.ay*t;
x.x+=x.vx*t;
x.y+=x.vy*t;
return x;
}
/*坐标转换并显示*/
cParticleF2D& draw(cParticleF2D& x,cParameterQueue& paras)
{
cBitmap *bmp;
paras.Peek(&bmp);
char *p;
long dx,dy;
dx=(x.x-400)*pmath->fcos(angle)-(300-x.y)*pmath->fsin(angle)+400;
dy=(x.x-400)*pmath->fsin(angle)+(300-x.y)*pmath->fcos(angle)+300;
if(dx>0&&dx<799&&dy>0&&dy<599)
{
p=(char *)bmp->GetBuffer(dx,600-dy);
bmp->Set(p,&x.C);
}
if(dx<0||dx>=800) x.vx=-x.vx;
if(dy<0||dy>=600) x.vy=-x.vy;
return x;
}
void main()
{
long i;
float t=0.01;
cTimer timer;
cSVGA* svga=cSVGA::Instance();
float RGB[256][3];
cBitmap bmp;
pmath=cMath::Instance();
cParticleF2D particle;
cParticleGroup<cParticleF2D> pg(50000,2); /*第一个例子是5000*/
memset(&particle,0,sizeof(particle));
particle.y=300;
particle.x=400;
particle.C=255;
for(i=0;i<50000;i++)
{
particle.ax=rand()%10191/1000.0-5; /*水平随机加速度*/
particle.ay=rand()%8191/1000.0-4; /*垂直随机加速度*/
pg.Add(particle);
}
pg.AddTransFunc(drop); /*绑定函数*/
pg.AddTransPara(&t); /*绑定参数*/
svga->SetMode(800,600,8,cSVGA::_c_linear);
bmp.Create(svga->GetDirectBuffer(),800,600,8);
cPALETTE pal;
for(i=0;i<256;i++) RGB[i][0]=sqrt(i)/16.0;
for(i=0;i<256;i++) RGB[i][1]=pow(i,0.7)/48.5;
for(i=0;i<256;i++) RGB[i][2]=(float)(i*i)/65536.0;
for(i=0;i<256;i++) pal[i].R=RGB[i][0]*256;
for(i=0;i<256;i++) pal[i].G=RGB[i][1]*256;
for(i=0;i<256;i++) pal[i].B=RGB[i][2]*256;
svga->SetPal(&pal);
pg.AddTransFunc(draw);
pg.AddTransPointPara(&bmp);
char *p,*q;
while(cKey::Asc()!=27)
{
/*自动对pg里面的每一个粒子调用绑定的函数*/
pg.Refresh();
/*全屏 Blur*/
for(p=(char *)bmp.GetBuffer(1,1),q=(char *)bmp.GetBuffer(bmp.Width()-2,bmp.Height()-2);p<q;p++)
*p=*(p-1)+*(p+1)+*(p-bmp.Width())+*(p+bmp.Width())>>2;
/*全屏 FadeOut*/
for(p=(char *)bmp.GetBuffer(0,0),q=(char *)bmp.GetBuffer(bmp.Width()-1,bmp.Height()-1);p<q;p++) if(*p>0) (*p)--;
angle++; /*第一个例子是angle+=0;*/
}
delete pmath;
delete svga;
}
平台DOS + Watcom C
[img]http://www.wodutom.com/coolwindows/10z.PNG[/img]
这个是50000个粒子形成的粒子星云
[img]http://www.wodutom.com/coolwindows/star.PNG[/img]
#include <i86.h>
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include "x\xsvga.hpp"
#include "x\xtimer.hpp"
#include "x\xkey.hpp"
#include "x\xpsystem.hpp"
#include "x\xmath.hpp"
cMath *pmath;
int angle=0;
/*坐标更新*/
cParticleF2D& drop(cParticleF2D& x,cParameterQueue& paras)
{
float t;
paras.Peek(&t);
x.vx+=x.ax*t;
x.vy+=x.ay*t;
x.x+=x.vx*t;
x.y+=x.vy*t;
return x;
}
/*坐标转换并显示*/
cParticleF2D& draw(cParticleF2D& x,cParameterQueue& paras)
{
cBitmap *bmp;
paras.Peek(&bmp);
char *p;
long dx,dy;
dx=(x.x-400)*pmath->fcos(angle)-(300-x.y)*pmath->fsin(angle)+400;
dy=(x.x-400)*pmath->fsin(angle)+(300-x.y)*pmath->fcos(angle)+300;
if(dx>0&&dx<799&&dy>0&&dy<599)
{
p=(char *)bmp->GetBuffer(dx,600-dy);
bmp->Set(p,&x.C);
}
if(dx<0||dx>=800) x.vx=-x.vx;
if(dy<0||dy>=600) x.vy=-x.vy;
return x;
}
void main()
{
long i;
float t=0.01;
cTimer timer;
cSVGA* svga=cSVGA::Instance();
float RGB[256][3];
cBitmap bmp;
pmath=cMath::Instance();
cParticleF2D particle;
cParticleGroup<cParticleF2D> pg(50000,2); /*第一个例子是5000*/
memset(&particle,0,sizeof(particle));
particle.y=300;
particle.x=400;
particle.C=255;
for(i=0;i<50000;i++)
{
particle.ax=rand()%10191/1000.0-5; /*水平随机加速度*/
particle.ay=rand()%8191/1000.0-4; /*垂直随机加速度*/
pg.Add(particle);
}
pg.AddTransFunc(drop); /*绑定函数*/
pg.AddTransPara(&t); /*绑定参数*/
svga->SetMode(800,600,8,cSVGA::_c_linear);
bmp.Create(svga->GetDirectBuffer(),800,600,8);
cPALETTE pal;
for(i=0;i<256;i++) RGB[i][0]=sqrt(i)/16.0;
for(i=0;i<256;i++) RGB[i][1]=pow(i,0.7)/48.5;
for(i=0;i<256;i++) RGB[i][2]=(float)(i*i)/65536.0;
for(i=0;i<256;i++) pal[i].R=RGB[i][0]*256;
for(i=0;i<256;i++) pal[i].G=RGB[i][1]*256;
for(i=0;i<256;i++) pal[i].B=RGB[i][2]*256;
svga->SetPal(&pal);
pg.AddTransFunc(draw);
pg.AddTransPointPara(&bmp);
char *p,*q;
while(cKey::Asc()!=27)
{
/*自动对pg里面的每一个粒子调用绑定的函数*/
pg.Refresh();
/*全屏 Blur*/
for(p=(char *)bmp.GetBuffer(1,1),q=(char *)bmp.GetBuffer(bmp.Width()-2,bmp.Height()-2);p<q;p++)
*p=*(p-1)+*(p+1)+*(p-bmp.Width())+*(p+bmp.Width())>>2;
/*全屏 FadeOut*/
for(p=(char *)bmp.GetBuffer(0,0),q=(char *)bmp.GetBuffer(bmp.Width()-1,bmp.Height()-1);p<q;p++) if(*p>0) (*p)--;
angle++; /*第一个例子是angle+=0;*/
}
delete pmath;
delete svga;
}