想不到会由如此效果!
#include <windows.h>
const int Nx = 300 ;
const int Ny = 200 ;
const int Max = 300 ;
const int Scale = 3 ;
const int CoolRate = 20 ;
const int FuleQuality = 5 ;
int clr [2][Nx][Ny] ;
int base [Nx] ;
int sel = 0 ;
LRESULT CALLBACK WndProc ( HWND, UINT, WPARAM, LPARAM ) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = "Application" ;
MSG msg ;
WNDCLASS wndclass ;
HWND hWnd ;
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, "MainIcon") ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH) ;
wndclass.lpszMenuName = "mnuMain" ;
wndclass.lpszClassName = szAppName ;
RegisterClass (&wndclass);
hWnd = CreateWindow (szAppName, "Flames",
WS_OVERLAPPED,
0, 0,
900, 630,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hWnd, SW_SHOW) ;
UpdateWindow (hWnd) ;
SetTimer (hWnd, 1, 75, 0) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int i, j ;
int x, y ;
PAINTSTRUCT ps ;
HDC hdc ;
BITMAPINFO bmpinf ;
void * vptr ;
static DWORD * sptr ;
static HDC shdc ;
static HBITMAP shbmp ;
switch (message)
{
case WM_CREATE :
// Initialize bitmap infomation
bmpinf.bmiHeader.biSize = sizeof( bmpinf.bmiHeader ) ;
bmpinf.bmiHeader.biPlanes = 1 ;
bmpinf.bmiHeader.biBitCount = 32 ;
bmpinf.bmiHeader.biCompression = BI_RGB ;
bmpinf.bmiHeader.biSizeImage = 0 ;
bmpinf.bmiHeader.biClrUsed = 0 ;
bmpinf.bmiHeader.biClrImportant = 0 ;
bmpinf.bmiHeader.biWidth = Nx * Scale ;
bmpinf.bmiHeader.biHeight = Ny * Scale ;
shdc = CreateCompatibleDC (NULL) ;
shbmp = CreateDIBSection (NULL, &bmpinf, DIB_RGB_COLORS, &vptr, NULL, 0) ;
SelectObject (shdc, shbmp) ;
sptr = (DWORD *) vptr ;
srand (GetTickCount ()) ;
// Initialize the color array.
for( i = 0 ; i < Nx ; i ++ )
for( j = 0 ; j < Ny ; j ++ ){
clr[0][i][j] = clr[1][i][j] = 0 ;
}
// Initialize the base color array.
for( i = 0 ; i < Nx ; i ++ )
base[i] = rand () * Max / RAND_MAX ;
return 0 ;
case WM_KEYDOWN :
// Calculate
for( i = 0 ; i < Nx ; i ++ ){
clr[sel][i][0] = base[i] ;
}
for( i = 0 ; i < Nx ; i ++ )
for( j = 0 ; j < Ny ; j ++ ){
clr[!sel][i][j] = 0 ;
}
for( i = 0 ; i < Nx ; i ++ ){
for( j = 0 ; j < Ny ; j ++ ){
int temp = clr[sel][i][j] ;
int grow = temp / 80 ;
for( x = i - grow ; x <= i + grow ; x ++ ){
for( y = j + 2 * grow ; y <= j + 4 * grow ; y ++ ){
if( x >= 0 && x < Nx && y >= 0 && y < Ny ){
clr[!sel][x][y] += clr[sel][i][j]
/ (4 * grow * grow + 4 * grow + 1) + FuleQuality ;
if( clr[!sel][x][y] > Max )
clr[!sel][x][y] = Max ;
}
}
}
}
}
for( i = 0 ; i < Nx ; i ++ )
for( j = 0 ; j < Ny ; j ++ ){
if( clr[!sel][i][j] >= CoolRate ) clr[!sel][i][j] -= CoolRate ;
}
sel = !sel ;
// Display
for( i = 0 ; i < Nx ; i ++ ){
for( j = 0 ; j < Ny ; j ++ ){
int temp = clr[sel][i][j] ;
int cref ;
// Calculate the color to display on the screen.
if( temp < Max / 3 )
cref = (temp * 255 * 3 / Max) << 16 ;
else if( temp < Max * 2 / 3 )
cref = (255 << 16) + ((temp * 255 * 3 / Max - 255) << 8) ;
else
cref = (255 << 16) + (255 << 8) + (temp * 255 * 3 / Max - 2 * 255) ;
for( x = 0 ; x < Scale ; x ++ ){
for( y = 0 ; y < Scale ; y ++ ){
DWORD * p = sptr + (Scale * i + x) + (Scale * j + y) * Scale * Nx ;
* p = cref ;
}
}
}
}
InvalidateRgn (hwnd, 0, FALSE) ;
return 0 ;
case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;
BitBlt (hdc, 0, 0, Nx * Scale, Ny * Scale, shdc, 0, 0, SRCCOPY) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_LBUTTONDBLCLK :
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}