|
我总是觉得我的程序结构做得不好,所以想给大家看看,吸取多一点意见
以下是我的程序代码: [em5]
//_Mode.cpp
//Create By rogramma in Netmez! @ 2004/7/24 08:17:46 AM;
#include "_MODE.H"
_MODE::_MODE(DORISSYS *DorisSys)
{
this->Doris = DorisSys;
this->Activate = TRUE;
this->NextMode = -1;
this->Frame = 0;
}
void _MODE::Initialize(void)
{
}
_MODE* _MODE::Main_Loop(void)
{
this->Frame ++;
;
if (this->NextMode != -1)
{
return this->Mode[this->NextMode];
}
else
{
return this;
}
}
void _MODE::Terminate(void)
{
this->Activate = FALSE;
}
//_Mode.h
//Create By :Programma in Netmez! @ 2004/7/24 08:17:21 AM;
#pragma once
#include "DORISSYS.H"
class _MODE
{
public:
bool Activate;
_MODE(DORISSYS *DorisSys);
~_MODE(void){};
virtual void Initialize(void);
virtual _MODE* Main_Loop(void);
virtual void Terminate(void);
protected:
DORISSYS *Doris;
_MODE** Mode;
int Frame;
int NextMode;
};
//_BattleMode.cpp
//Create By :Programma in Netmez! @ 2004/7/24 08:15:54 AM;
#include "BATTLEMODE.H"
//_BattleMode.h
//Create By :Programma in Netmez! @ 2004/7/24 08:16:24 AM;
#include "_MODE.H"
class BATTLEMODE:public _MODE
{
public:
BATTLEMODE(DORISSYS* DorisSys):_MODE(DorisSys){};
};
//CoverMode.cpp
//Create By :Programma in Netmez! @ 2004/7/22 09:16:14 AM;
#include "COVERMODE.H"
_MODE* COVERMODE::Main_Loop(void)
{
this->Frame++;
this->Doris->GraphicSys->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
this->Doris->GraphicSys->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
this->Doris->GraphicSys->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
this->Doris->GraphicSys->Set2DCamera();
this->Back->Transform(0,0);
this->Back->Render();
if (this->Frame % 20 < 15)
{
this->Title->Transform(144,500);
this->Title->Render();
}
return _MODE::Main_Loop();
}
void COVERMODE::Initialize(void)
{
this->Back = this->Doris->GraphicSys->CreateFace();
this->Back->Initialize(800,600);
this->Back->LoadTexture(" icture\\Cover\\Back.bmp");
this->Title = this->Doris->GraphicSys->CreateFace();
this->Title->Initialize(512,64);
this->Title->LoadTexture("Picture\\Cover\\Text.bmp",D3DCOLOR_XRGB(0,0,0));
_MODE::Initialize();
}
void COVERMODE::CheckInput(void)
{
char* KeyState = this->Doris->InputSys->GetKeyState();
if (KEYDOWN(KeyState, DIK_RETURN))
{
}
}
void COVERMODE::Terminate(void)
{
this->Activate = FALSE;
this->Back->Terminate();
_MODE::Terminate();
}
//CoverMode.h
//Create By :Programma in Netmez! @ 2004/7/22 09:16:16 AM;
#pragma once
#include "DORISSYS.H"
#include "_MODE.H"
class COVERMODE: public _MODE
{
public:
bool Activate;
COVERMODE(DORISSYS* DorisSys):_MODE(DorisSys){};
~COVERMODE(void){};
void Initialize(void);
_MODE* Main_Loop(void);
void Terminate(void);
private:
DORISFACE *Back;
DORISFACE *Title;
void CheckInput(void);
};
//GunPlay.cpp
//Create By :Programma in Netmez! @ 2004/7/22 08:38:05 AM;
#include "GUNPLAY.H"
void GUNPLAY::Initialize(HWND hWnd, HINSTANCE hInst)
{
this->Doris = new DORISSYS();
this->Doris->Initialize(hWnd,hInst);
this->Mode = new MODESYS();
this->Mode->Initialize(this->Doris);
}
void GUNPLAY::Main_Loop(void)
{
Sleep(5);
this->Doris->GraphicSys->RenderStart();
this->Mode->Main_Loop();
this->Doris->GraphicSys->RenderEnd();
}
void GUNPLAY::Terminate(void)
{
this->Mode->Terminate();
this->Doris->Terminate();
}
//GunPlay.h
//Create By :Programma in Netmez! @ 2004/7/22 08:38:22 AM;
#include "WINDOWS.H"
#include "DORISSYS.H"
#include "MODESYS.H"
class GUNPLAY
{
public:
GUNPLAY(void){};
~GUNPLAY(void){};
void Initialize(HWND hWnd, HINSTANCE hInst);
void Main_Loop(void);
void Terminate(void);
private:
DORISSYS *Doris;
MODESYS *Mode;
};
//Main.cpp
//Create By :Programma in Netmez! @ 2004/7/22 08:27:36 AM;
#include "WINDOWS.H"
#include "GUNPLAY.H"
LRESULT CALLBACK Game_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
}
case WM_PAINT:
{
ValidateRect(hWnd, NULL);
return 0;
}
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
char *ClassName = "Netmez! Raw";
WNDCLASSEX *WndClass = new WNDCLASSEX;
ZeroMemory(WndClass,sizeof(WNDCLASSEX));
WndClass->hbrBackground = CreateSolidBrush(0);
WndClass->cbSize = sizeof(WNDCLASSEX);
WndClass->hInstance = hInstance;
WndClass->lpfnWndProc = Game_Proc;
WndClass->lpszClassName = ClassName;
WndClass->hCursor = LoadCursorFromFile("Pic\\Blank.cur");
if (!RegisterClassEx(WndClass)) return 0;
int ClientW, ClientH;
ClientW = ClientH = GetSystemMetrics(SM_CXFRAME)*2;
ClientW += 800;
ClientH += 600 + GetSystemMetrics(SM_CYCAPTION) - 1;
HWND hWnd = CreateWindow(ClassName, "Gunplay",
WS_OVERLAPPED|WS_CAPTION|WS_MINIMIZEBOX|WS_SYSMENU,
CW_USEDEFAULT,CW_USEDEFAULT,ClientW,ClientH, GetDesktopWindow(),
(HMENU)NULL,hInstance,(LPSTR)NULL);
ShowWindow(hWnd,TRUE);
UpdateWindow(hWnd);
GUNPLAY *GunPlay = new GUNPLAY();
GunPlay->Initialize(hWnd, hInstance);
MSG Message;
for (;;)
{
if(PeekMessage(&Message,NULL,0,0,PM_REMOVE))
{
if(Message.message==WM_QUIT)
{
break;
}
TranslateMessage(&Message);
DispatchMessage(&Message);
}else
{
GunPlay->Main_Loop();
}
}
GunPlay->Terminate();
return 0;
}
//ModeSys.cpp
//Create By :Programma in Netmez! @ 2004/7/24 09:23:51 AM;
#include "MODESYS.H"
void MODESYS::Initialize(DORISSYS *Doris)
{
this->ModeNum = 1;
this->Mode = (_MODE**)new int[1];
this->Mode[0] = new COVERMODE(Doris);
this->Mode[1] = new BATTLEMODE(Doris);
this->ActiveMode = this->Mode[0];
this->ActiveMode->Initialize();
// this->Mode[1] = new BATTLEMODE(Doris);
}
void MODESYS::Main_Loop(void)
{
this->ActiveMode->Main_Loop();
}
void MODESYS::Terminate(void)
{
int i = 0;
for (i = 0; i<this->ModeNum;i++)
{
if (this->Mode->Activate == TRUE)
{
this->Mode->Terminate();
}
}
}
//ModeSys.h
//Create By :Programma in Netmez! @ 2004/7/24 09:24:06 AM;
#pragma once
#include "_MODE.H"
#include "COVERMODE.H"
#include "BATTLEMODE.H"
class MODESYS
{
public:
MODESYS(){};
~MODESYS(void);
void Initialize(DORISSYS *Doris);
void Main_Loop(void);
void Terminate(void);
private:
_MODE* ActiveMode;
_MODE** Mode;
int ModeNum;
};
//DorisFace.cpp
//Create By :Programma in Netmez! @ 2004/7/22 09:33:05 AM;
#include "DORISFACE.H"
DORISFACE: ORISFACE(LPDIRECT3DDEVICE8 pDevice,int ScreenW,int ScreenH)
{
this->Device = pDevice;
this->VertexBuffer = NULL;
this->Texture = NULL;
this->ScrWidth = ScreenW;
this->ScrHeight = ScreenH;
}
void DORISFACE::Initialize(int nWidth,int nHeight)
{
this->Width = nWidth;
this->Height = nHeight;
this->Device->CreateVertexBuffer(4*sizeof(DXFACE_VERTEX),0,DXFACE_VERTEX_FLAGS,
D3DPOOL_DEFAULT,&this->VertexBuffer);
DXFACE_VERTEX *pVertices = NULL;
this->VertexBuffer->Lock(0, sizeof(DXFACE_VERTEX[4]),(BYTE**)&pVertices,0);
pVertices[0].colour = pVertices[1].colour =
pVertices[2].colour = pVertices[3].colour =
0xffffffff;
pVertices[0].x = pVertices[3].x = -this->Width / 2.0f;
pVertices[1].x = pVertices[2].x = this->Width / 2.0f;
pVertices[0].y = pVertices[1].y = this->Height / 2.0f;
pVertices[2].y = pVertices[3].y = -this->Height / 2.0f;
pVertices[0].z = pVertices[1].z = pVertices[2].z = pVertices[3].z = 1.0f;
pVertices[1].u = pVertices[2].u = 1.0f;
pVertices[0].u = pVertices[3].u = 0.0f;
pVertices[0].v = pVertices[1].v = 0.0f;
pVertices[2].v = pVertices[3].v = 1.0f;
this->VertexBuffer->Unlock();
}
void DORISFACE: oadTexture(const char *FilePath, int ColorKey)
{
if (ColorKey == -1)
{
D3DXCreateTextureFromFile(this->Device,FilePath,&this->Texture);
}
else
{
D3DXCreateTextureFromFileEx(this->Device,FilePath,0,0,0,0,D3DFMT_UNKNOWN,
D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,ColorKey,NULL,NULL,&this->Texture);
}
}
void DORISFACE::Transform(int X, int Y, float Angle)
{
D3DXMATRIX MatrixMove;
D3DXMatrixTranslation(&MatrixMove, (float)(X - (this->ScrWidth / 2 - this->Width / 2)),
float(-Y + (this->ScrHeight / 2 - this->Height / 2)), 0.0f);
D3DXMATRIX MatrixRotate;
D3DXMatrixRotationZ(&MatrixRotate, Angle);
D3DXMATRIX MatrixWorld;
D3DXMatrixMultiply(&MatrixWorld,&MatrixMove,&MatrixRotate);
this->Device->SetTransform(D3DTS_WORLD, &MatrixWorld);
}
void DORISFACE::Render(void)
{
this->Device->SetStreamSource(0,this->VertexBuffer,sizeof(DXFACE_VERTEX));
this->Device->SetVertexShader(DXFACE_VERTEX_FLAGS);
;
if(this->Texture)
{
this->Device->SetTexture(0,this->Texture);
this->Device->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
}else
{
this->Device->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_DISABLE);
}
;
this->Device->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
}
void DORISFACE::Terminate(void)
{
if(this->VertexBuffer) this->VertexBuffer->Release();
if(this->Texture) this->Texture->Release();
}
//DorisFace.h
//Create By :Programma in Netmez! @ 2004/7/22 09:33:02 AM;
#include "D3D8.H"
#include "D3DX8.H"
#define DXFACE_VERTEX_FLAGS (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
class DORISFACE
{
public:
DORISFACE(LPDIRECT3DDEVICE8 pDevice,int ScreenW,int ScreenH);
~DORISFACE(){};
void Initialize(int nWidth,int nHeight);
void LoadTexture(const char *FilePath, int ColorKey = -1);
void Transform(int X, int Y, float Angle = 0.0f);
void Render(void);
void Terminate(void);
private:
struct DXFACE_VERTEX
{
FLOAT x, y, z;
DWORD colour;
FLOAT u, v;
};
int Width, Height;
int ScrWidth, ScrHeight;
LPDIRECT3DDEVICE8 Device;
LPDIRECT3DVERTEXBUFFER8 VertexBuffer;
LPDIRECT3DTEXTURE8 Texture;
};
//DorisSys.cpp
//Create By :Programma in Netmez! @ 2004/7/17 08:53:42 AM;
#include "DORISSYS.H"
void DORISSYS::Initialize(HWND hWnd,HINSTANCE hInstance)
{
this->GraphicSys = new GRAPHICSYS();
this->GraphicSys->Initialize(hWnd,true,800,600);
this->InputSys = new INPUTSYS();
this->InputSys->Initialize(hInstance,hWnd);
}
void DORISSYS::Terminate(void)
{
this->GraphicSys->Terminate();
delete this->GraphicSys;
}
//DorisSys.h
//Create By :Programma in Netmez! @ 2004/7/17 08:53:52 AM;
#pragma once
#include "D3D8.H"
#include "D3DX8.H"
#include "GRAPHICSYS.H"
#include "INPUTSYS.H"
class DORISSYS
{
public:
GRAPHICSYS *GraphicSys;
INPUTSYS *InputSys;
DORISSYS(void){};
~DORISSYS(void){};
void Initialize(HWND hWnd,HINSTANCE hInstance);
void Terminate();
};
//GraphicSys.cpp
//Create By :Programma in Netmez! @ 2004/7/17 08:54:10 AM;
#include "GRAPHICSYS.H"
int GRAPHICSYS::Initialize(HWND hWnd, bool Windowed, UINT Width, UINT Height)
{
this->ScreenW = Width;
this->ScreenH = Height;
int Result;
if ((this->Direct3D = Direct3DCreate8(D3D_SDK_VERSION)) == NULL) return FALSE;
D3DPRESENT_PARAMETERS Present_Param;
ZeroMemory(&Present_Param, sizeof(Present_Param));
D3DDISPLAYMODE DisplayMode;
Result = this->Direct3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&DisplayMode);
if (FAILED(Result)) return Result;
if (Present_Param.Windowed = Windowed)
{
Present_Param.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
Present_Param.hDeviceWindow = hWnd;
Present_Param.BackBufferCount = 1;
Present_Param.BackBufferFormat = DisplayMode.Format;
Present_Param.AutoDepthStencilFormat = D3DFMT_D16;
Present_Param.EnableAutoDepthStencil = TRUE;
}else
{
Present_Param.SwapEffect = D3DSWAPEFFECT_DISCARD;
Present_Param.hDeviceWindow = hWnd;
Present_Param.BackBufferWidth = Width;
Present_Param.BackBufferHeight = Height;
Present_Param.BackBufferCount = 2;
Present_Param.BackBufferFormat = this->GetDisplayFormat(Width,Height,16);
Present_Param.AutoDepthStencilFormat = D3DFMT_D16;
Present_Param.EnableAutoDepthStencil = TRUE;
Present_Param.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
Present_Param.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_ONE;
}
Result = this->Direct3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &Present_Param, &this->Device3D);
if (FAILED(Result)) return Result;
return D3D_OK;
}
void GRAPHICSYS::Terminate(void)
{
if (this->Device3D != NULL) this->Device3D->Release();
if (this->Direct3D != NULL) this->Direct3D->Release();
}
void GRAPHICSYS::Set2DCamera()
{
D3DXMATRIX MatrixOrtho;
D3DXMATRIX MatrixIdentity;
D3DXMatrixOrthoLH(&MatrixOrtho, (float)this->ScreenW, (float)this->ScreenH, 0.0f, 1.0f);
D3DXMatrixIdentity(&MatrixIdentity);
this->Device3D->SetTransform(D3DTS_PROJECTION, &MatrixOrtho);
this->Device3D->SetTransform(D3DTS_WORLD, &MatrixIdentity);
this->Device3D->SetTransform(D3DTS_VIEW, &MatrixIdentity);
this->Device3D->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
this->Device3D->SetRenderState(D3DRS_LIGHTING, FALSE);
}
void GRAPHICSYS::SetRenderState(D3DRENDERSTATETYPE State, DWORD Value)
{
this->Device3D->SetRenderState(State,Value);
}
D3DFORMAT GRAPHICSYS::GetDisplayFormat(UINT nWidth, UINT nHeight, UINT nDepth)
{
UINT i;
D3DDISPLAYMODE Mode;
for(i = 0; i < this->Direct3D->GetAdapterModeCount(0); i++)
{
this->Direct3D->EnumAdapterModes(0, i, &Mode);
if (Mode.Width == nWidth && Mode.Height == nHeight)
{
if ((Mode.Format == D3DFMT_R5G6B5 ||
Mode.Format == D3DFMT_X1R5G5B5 ||
Mode.Format == D3DFMT_X4R4G4B4) && nDepth == 16)
{
return Mode.Format;
}
if((Mode.Format == D3DFMT_R8G8B8 ||
Mode.Format == D3DFMT_X8R8G8B8) && nDepth == 32)
{
return Mode.Format;
}
}
}
return (D3DFORMAT) NULL;
}
void GRAPHICSYS::RenderStart(void)
{
this->Device3D->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0,0,255),1.0f,0);
this->Device3D->BeginScene();
}
void GRAPHICSYS::RenderEnd(void)
{
this->Device3D->EndScene();
this->Device3D->Present(NULL,NULL,NULL,NULL);
}
DORISFACE* GRAPHICSYS::CreateFace(void)
{
return new DORISFACE(this->Device3D,this->ScreenW,this->ScreenH);
}
//GraphicSys.h
//Create By :Programma in Netmez! @ 2004/7/17 08:54:17 AM;
#pragma once
#include "D3D8.H"
#include "D3DX8.H"
#include "DORISFACE.H"
class GRAPHICSYS
{
public:
GRAPHICSYS(){};
~GRAPHICSYS(){};
;
int Initialize(HWND hWnd, bool Windowed, UINT Width, UINT Height);
void RenderStart(void);
void Set2DCamera(void);
void SetRenderState(D3DRENDERSTATETYPE State, DWORD Value);
void RenderEnd(void);
void Terminate(void);
DORISFACE* CreateFace(void);
private:
LPDIRECT3D8 Direct3D;
LPDIRECT3DDEVICE8 Device3D;
int ScreenW, ScreenH;
D3DFORMAT GetDisplayFormat(UINT, UINT, UINT);
};
//InputSys.cpp
//Create By :Programma in Netmez! @ 2004/7/17 08:54:22 AM;
#include "INPUTSYS.H"
int INPUTSYS::Initialize(HINSTANCE hInstance,HWND hWnd)
{
int Result;
this->DirectInput = NULL;
if(FAILED(Result = DirectInput8Create(hInstance,DIRECTINPUT_VERSION,IID_IDirectInput8,
(void**)&this->DirectInput,NULL))) return Result;
if(FAILED(Result = this->DirectInput->CreateDevice(GUID_SysKeyboard,
&this->Keyboard,NULL))) return Result;
if(FAILED(Result = this->Keyboard->SetDataFormat(&c_dfDIKeyboard))) return Result;
if(FAILED(Result = this->Keyboard->SetCooperativeLevel(hWnd,
DISCL_FOREGROUND|DISCL_NONEXCLUSIVE))) return Result;
this->Keyboard->Acquire();
if(FAILED(Result = this->DirectInput->CreateDevice(GUID_SysMouse,&this->Mouse,NULL)))
return Result;
if(FAILED(Result = this->DirectInput->CreateDevice(GUID_SysMouse,
&this->Mouse,NULL))) return Result;
if(FAILED(Result = this->Mouse->SetDataFormat(&c_dfDIMouse))) return Result;
if(FAILED(Result = this->Mouse->SetCooperativeLevel(hWnd,
DISCL_FOREGROUND|DISCL_NONEXCLUSIVE))) return Result;
this->Mouse->Acquire();
return DI_OK;
}
void INPUTSYS::Terminate(void)
{
if(this->Keyboard)
{
this->Keyboard->Unacquire();
this->Keyboard->Release();
}
if(this->Mouse)
{
this->Mouse->Unacquire();
this->Mouse->Release();
}
}
char* INPUTSYS::GetKeyState(void)
{
if(FAILED(this->Keyboard->GetDeviceState(sizeof(this->KeyState),(void*)&this->KeyState)))
return NULL;
return this->KeyState;
}
DIMOUSESTATE* INPUTSYS::GetMouseState(void)
{
if(FAILED(this->Mouse->GetDeviceState(sizeof(this->MouseState),(void*)&this->MouseState)))
return (DIMOUSESTATE*)NULL;
return &this->MouseState;
}
//InputSys.h
//Create By :Programma in Netmez! @ 2004/7/17 08:54:28 AM;
#pragma once
#include "DINPUT.H"
#define KEYDOWN(name,value) (name[value] & 0x80)
#define MOUSEDOWN(value) (value &0x80)
class INPUTSYS
{
public:
INPUTSYS(){};
~INPUTSYS(){};
int Initialize(HINSTANCE hInstance,HWND hWNd);
void Terminate(void);
char* GetKeyState(void);
DIMOUSESTATE* GetMouseState(void);
private:
LPDIRECTINPUT8 DirectInput;
LPDIRECTINPUTDEVICE8 Keyboard;
LPDIRECTINPUTDEVICE8 Mouse;
DIMOUSESTATE MouseState;
char KeyState[256];
}; |
|