想不到会由如此效果!

wangqiqi 2001-12-24 05:10:05
#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) ;
}
...全文
82 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangqiqi 2001-12-27
  • 打赏
  • 举报
回复
散分了,谁要?
KingofMagic 2001-12-26
  • 打赏
  • 举报
回复
我怎么编译不通过呢?
vc6.0
wangqiqi 2001-12-25
  • 打赏
  • 举报
回复
运行速度确实慢,主要耗在那四重循环上了,你有什么优化的方法吗?
你是指 GDI 资源没有释放吗?加上几个语句就行了。不过也不至于多运行几次系统就会崩溃吧?你内存多少啊?
老碧鹅 2001-12-24
  • 打赏
  • 举报
回复
而且资源没有释放
多运行几次系统就会崩溃
老碧鹅 2001-12-24
  • 打赏
  • 举报
回复
wangqiqi(Venom)说得对
可惜就是运行速度太慢了
NowCan 2001-12-24
  • 打赏
  • 举报
回复
厉害!
wangqiqi 2001-12-24
  • 打赏
  • 举报
回复
Sorry,把 WM_KEYDOWN 改成 WM_TIMER 吧。

8,303

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧