昨天晚上,三本书,翻过来翻过去,写下了下面的程序(部分):
int CMyFlashDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
CClientDC dc(this);
CRect rect;
GetClientRect(&rect);
m_nWidth=rect.Width();
m_nHeight=rect.Height();
m_mbm1.CreateCompatibleBitmap(&dc,m_nWidth/3,m_nHeight/3);
m_mbm2.CreateCompatibleBitmap(&dc,m_nWidth/3,m_nHeight/3);
m_mdc1.CreateCompatibleDC(&dc);
m_mdc2.CreateCompatibleDC(&dc);
m_mdc1.SelectObject(m_mbm1);
m_mdc2.SelectObject(m_mbm2);
SetTimer(IDT_MAIN_TIMER,30,NULL);
return 0;
}
void CMyFlashDlg::myfire()
{
CClientDC dc(this);
int sw=m_nWidth/3,sh=m_nHeight/3;
for(int y=1;y<sh;++y)
for(int x=1;x<sw;++x)
{
COLORREF p=0;
p=(m_mdc1.GetPixel(x,y)+m_mdc1.GetPixel(x-1,y)+m_mdc1.GetPixel(x+1,y)
+m_mdc1.GetPixel(x,y-1)+m_mdc1.GetPixel(x,y+1))/5;
p-=3;
if(p<0)
p=0;
m_mdc2.SetPixel(x,y-1,p);
}
m_mdc1.BitBlt(0,0,m_nWidth,m_nHeight,&m_mdc2,0,0,SRCCOPY);
}
void CMyFlashDlg::myrand()
{
int sw=m_nWidth/3,sh=m_nHeight/3;
int t=0;
for(int x=1;x<sw;++x)
{
COLORREF rcolor;
if(!t)
{
//rcolor=RGB(255,0,0);
rcolor=RGB(128+(rand() & 0x7F),0,0);
t=4;
}
m_mdc1.SetPixel(x,sh-1,rcolor);
--t;
}
}
void CMyFlashDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
CClientDC dc(this);
//dc.BitBlt(0,0,m_nWidth,m_nHeight,&m_mdc1,0,0,SRCCOPY);
dc.StretchBlt(0,0,m_nWidth,m_nHeight,&m_mdc1,0,0,m_nWidth/3,m_nHeight/3,SRCCOPY);
}
}
void CMyFlashDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==IDT_MAIN_TIMER)
{
myrand();
myfire();
Invalidate(FALSE);
}
CDialog::OnTimer(nIDEvent);
}
发现效果不好,这是图:
一开始,生成热源的时候,全是设成RGB(255,0,0),结果出来不像是火,顶多就像液化气灶的火(- -!),后来随机化了一下,myrand()用于随机化热源,从底部看还有那么点意思,火焰上部分不太像,匀速往上飘,真实的火焰应该是在上面有气流的对撞,应该有更大的随机化,而且从底往上气流上升速度也不是匀速,应该是开始快然后慢。
其实最大的问题不在这里,而是显示得太慢了,我用了两个内存DC缓冲,很慢,只能开一个很小的窗口,不知怎么解决:(
评论