【VC开源代码栏目提醒】:本文主要为网学会员提供fire.cpp,希望对需要fire.cpp网友有所帮助,学习一下!
#define NAME "火焰仿真"
#define TITLE "火焰仿真"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <fstream.h>
#include <ddraw.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <io.h>
#include "gif89a.h"
#include "cfire.h"
#define TIMER_ID 1
#define TIMER_RATE 30
void showAFile();
DWORD WINAPI demo(LPVOID lpparm);
LPDIRECTDRAW lpDD; // DirectDraw object
LPDIRECTDRAWSURFACE lpDDSPrimary; // DirectDraw primary surface
LPDIRECTDRAWSURFACE lpDDSBack; // DirectDraw back surface
DWORD RBitMask,GBitMask,BBitMask;
BOOL bActive; // is application active?
BOOL bQuit = false;
HANDLE thandle;
HWND hwnd;
CFire fire(640,480);
WORD (*RGBto16bit)(unsigned char r,unsigned char g,unsigned char b);
BOOL initWindow( HINSTANCE hInstance, int nCmdShow );
BOOL initDDraw(void);
BOOL initFire(void);
long FAR PASCAL WindowProc( HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam );
void finiObjects( void );
time_t tt;
long frames,rate,k;
WORD RGBto16bit565(unsigned char r,unsigned char g,unsigned char b)
{ return (((WORD)r<<8)&0xf800)|(((WORD)g<<3)&0x07e0)|((WORD)b>>3);
}
WORD RGBto16bit555(unsigned char r,unsigned char g,unsigned char b)
{ return (((WORD)r<<7)&0x7c00)|(((WORD)g<<2)&0x03e0)|((WORD)b>>3);
}
int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{ MSG msg;
lpCmdLine = lpCmdLine;
hPrevInstance = hPrevInstance;
DWORD tID;
if( !initWindow( hInstance, nCmdShow ) )
{ return FALSE;
}
if(!initDDraw())
{ goto error;
}
if(!initFire())
{ goto error;
}
if((thandle = CreateThread(NULL,0,demo,NULL,0,&tID)) == NULL)
goto error;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
error:
char buf[40];
sprintf(buf, "初始化失败!");
MessageBox( hwnd, buf, "ERROR", MB_OK );
finiObjects();
DestroyWindow( hwnd );
return FALSE;
}
BOOL initWindow( HINSTANCE hInstance, int nCmdShow )
{ WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = NULL;
wc.lpszMenuName = NAME;
wc.lpszClassName = NAME;
RegisterClass( &wc );
hwnd = CreateWindowEx(
WS_EX_TOPMOST,
NAME,
TITLE,
WS_POPUP,
0, 0,
GetSystemMetrics( SM_CXSCREEN ),
GetSystemMetrics( SM_CYSCREEN ),
NULL,
NULL,
hInstance,
NULL );
if( !hwnd )
{
return FALSE;
}
ShowWindow( hwnd, nCmdShow );
UpdateWindow( hwnd );
return TRUE;
}
BOOL initFire(void)
{ if(!fire)
return FALSE;
if(!fire.loadSeedMap("seed.gif"))
return FALSE;
if(!fire.loadCoolMap("cool.gif"))
return FALSE;
if(!fire.loadLightMap("light.gif"))
return FALSE;
return TRUE;
}
BOOL initDDraw(void)
{ DDSURFACEDESC ddsd;
DDSCAPS ddscaps;
HRESULT ddrval;
char buf[256];
ddrval = DirectDrawCreate( NULL, &lpDD, NULL );
if( ddrval == DD_OK )
{ // Get exclusive mode
ddrval = lpDD->SetCooperativeLevel( hwnd,
DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
if(ddrval == DD_OK )
{ ddrval = lpDD->SetDisplayMode( 640,480, 16 );
if( ddrval == DD_OK )
{ // Create the primary surface with 1 back buffer
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS |
DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
DDSCAPS_FLIP |
DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
ddrval = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL );
if( ddrval == DD_OK )
{ ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
ddrval = lpDDSPrimary->GetAttachedSurface(&ddscaps,
&lpDDSBack);
if( ddrval == DD_OK )
{ DDPIXELFORM