//-----------------------------------------------------------------------------
// Name: dx9_2d_demo_game.cpp
// Author: Kevin Harris (kevin@codesampler.com)
// Last Modified: 10/04/04
// Description: 2D Sample game written in DirectX 9.0
// Controls: Use the arrow keys to move and the space bar to shoot!
//-----------------------------------------------------------------------------
#define STRICT
#define WIN32_LEAN_AND_MEAN
#define INITGUID
#define DIRECTINPUT_VERSION 0x0800
#include <d3d9.h>
#include <d3dx9.h>
#include <dinput.h>
#include <windows.h>
#include <mmsystem.h>
#include "resource.h"
#include "sprite.h"
#include <list>
using namespace std;
//-----------------------------------------------------------------------------
// SYMBOLIC CONSTANTS
//-----------------------------------------------------------------------------
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 16;
const int TOTAL_STARS = 100;
const int TOTAL_PARTICLES = 250;
const int SHIP_SPEED = 5;
//-----------------------------------------------------------------------------
// MACROS
//-----------------------------------------------------------------------------
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
#define KEYDOWN(name,key) (name[key] & 0x80)
//-----------------------------------------------------------------------------
// GLOBALS
//-----------------------------------------------------------------------------
HWND g_hWnd = NULL;
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPDIRECTINPUT8 g_lpdi = NULL;
LPDIRECTINPUTDEVICE8 g_pKeyboard = NULL;
RECT g_rcWindow;
RECT g_rcViewport;
RECT g_rcScreen;
BOOL g_bWindowed = true;
BOOL g_bActive = false;
DWORD g_dwLastFireTick = 0;
int g_nShipsLeft = 3;
int g_nScore = 0;
int g_nPowerLevel = 20;
int g_nCurrentLevel = 0;
double g_dElpasedTime;
double g_dCurTime;
double g_dLastTime;
double g_dAnimationTimer = 0.0;
struct STAR
{
int x; // Star posit x
int y; // Star posit y
int nVelocity; // Star velocity
COLORREF color; // Star color
};
STAR g_stars[TOTAL_STARS]; // Star field array
struct PARTICLE
{
int x; // Particle posit x
int y; // Particle posit y
int nVelocity; // Particle velocity
bool bVisible; // Is particle visible or active
COLORREF color; // Particle color
};
PARTICLE g_exhaust[TOTAL_PARTICLES]; // Particle array for engine exhaust
// Create a linked list with STL to hold and manage CSprite objects
typedef list<CSprite> SPRITELIST;
SPRITELIST g_SpriteList;
SPRITELIST::iterator g_sprite_i;
SPRITELIST::iterator g_sprite_j;
SPRITELIST::iterator g_sprite_k;
//-----------------------------------------------------------------------------
// PROTOTYPES
//-----------------------------------------------------------------------------
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
HRESULT WinInit(HINSTANCE hInst, int nCmdShow, HWND* phWnd, HACCEL* phAccel);
HRESULT GameStart(HWND hWnd);
HRESULT GameMain(HWND hWnd);
void GameOver(void);
HRESULT InitDirect3D(HWND hWnd, BOOL bWindowed);
void FreeDirect3D(void);
HRESULT InitDirectInput(HWND hWnd);
void FreeDirectInput(void);
void InitializeSprites(void);
void InitializeStars(void);
void InitializeExhaust(void);
void MoveShip(void);
void MoveSprites(void);
void MoveStars(void);
void MoveExhaust(void);
void CheckCollisions(void);
bool SpriteCollisionTest(CSprite& sprite1, CSprite& sprite2);
bool CollisionTest(int x1, int y1, int w1, int h1,
int x2, int y2, int w2, int h2);
void ComputeScore(void);
HRESULT DisplayFrame(void);
HRESULT DrawStars(void);
HRESULT DrawExhaust(void);
HRESULT DrawPowerBar(void);
int RandomInt(int nLow, int nHigh);
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point to the program. Initializes everything and calls.
//-----------------------------------------------------------------------------
int APIENTRY WinMain( HINSTANCE hInst,
HINSTANCE hPrevInst,
LPSTR pCmdLine,
int nCmdShow )
{
MSG msg;
HWND hWnd;
HACCEL hAccel;
memset(&msg,0,sizeof(msg));
WinInit( hInst, nCmdShow, &hWnd, &hAccel );
GameStart( hWnd );
while( TRUE )
{
// Look for messages, if none are found then
// update the state and display it
if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
{
if( 0 == GetMessage(&msg, NULL, 0, 0 ) )
{
// WM_QUIT was posted, so exit
return (int)msg.wParam;
}
// Translate and dispatch the message
if( 0 == TranslateAccelerator( hWnd, hAccel, &msg ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
else
{
if( g_bActive )
{
// Move the sprites, blt them to the back buffer, then
// flip or blt the back buffer to the primary buffer
g_dCurTime = timeGetTime();
g_dElpasedTime = ((g_dCurTime - g_dLastTime) * 0.001);
g_dLastTime = g_dCurTime;
if( FAILED( GameMain( hWnd ) ) )
{
//SAFE_DELETE( g_pDisplay );
MessageBox( hWnd, TEXT("GameMain() failed. ")
TEXT("The game will now exit. "), TEXT("PrototypeX"),
MB_ICONERROR | MB_OK );
return FALSE;
}
}
else
{
// Go to sleep if we have nothing else to do
WaitMessage();
// Ignore time spent inactive
//g_dwLastTick = timeGetTime();
}
}
}
}
//-----------------------------------------------------------------------------
// Name: WinInit()
// Desc: Init our game's window
//-----------------------------------------------------------------------------
HRESULT WinInit( HINSTANCE hInst, int nCmdShow, HWND *phWnd, HACCEL *phAccel )
{
WNDCLASSEX wc;
HWND hWnd;
HACCEL hAccel;
// Register the window class
wc.cbSize = sizeof(wc);
wc.lpszClassName = TEXT("MY_WINDOWS_CLASS");
wc.lpfnWndProc = MainWndProc;
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.hInstance = hInst;
wc.hIcon = LoadIcon( hInst, MAKEINTRESOURCE(IDI_MAIN_ICON) );
wc.hIconSm = LoadIcon( hInst, MAKEINTRESOURCE(IDI_MAIN_ICON) );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
if( RegisterClassEx( &wc ) == 0 )
return E_FAIL;
// Load keyboard accelerators
hAccel = LoadAccelerators( hInst, MAKEINTRESOURCE(IDR_MAIN_ACCEL) );
// Calculate the proper size for the window given a client of 640x480
DWORD dwFrameWidth = GetSystemMetrics( SM_CXSIZEFRAME );
DWORD dwFrameHeight = GetSystemMetrics( SM_CYSIZEFRAME );
DWORD dwMenuHeight = GetSystemMetrics( SM_CYMENU );
DWORD dwCaptionHeight = GetSystemMetrics( SM_CYCAPTION );
DWORD dwWindowWidth = SCREEN_WIDTH + dwFrameWidth * 2;
DWORD dwWindowHeight = SCRE
dx9_2d_demo_game directx9实现2D游戏例程 Windows Develop
5星 · 超过95%的资源 需积分: 13 183 浏览量
2009-08-27
18:06:26
上传
评论
收藏 379KB ZIP 举报
dxszbz
- 粉丝: 6
- 资源: 10
最新资源
- [图片动画]iFoto v1.0_ifoto-1.0.1.rar
- ssm+vue的物资物流系统的设计与实现(有报告) Javaee项目,ssm vue前后端分离项目
- 521yy 网站Whois查询 php版 1.0_whois_工具查询网站开发模板(使用说明+PHP源代码+html).zip
- vb药品库房管理系统设计(源代码+可执行程序+论文+开题报告+外文翻译+答辩ppt).rar
- 按键 12864显示_单片机C语言实例(纯C语言源代码).zip
- (FZY314)触屏版HTML5左侧展开导航菜单手机wap网站特效源码.zip
- 红外解码数码管显示_单片机C语言实例(纯C语言源代码).zip
- 案例3 遗传算法优化BP神经网络-非线性函数拟合.zip
- 阳光酒店管理系统(javaapplet+SQL)130425.rar
- 在线词典源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页