您的位置:首页 > 其它

DirectX学习2---建立一个简单的窗口

2012-09-22 21:10 387 查看
总共包括4个部分

1、WinMain函数

程序的入口函数,调用SystemClass类的相关函数

2、SystemClass类

窗口管理类,进行窗口的建立,消息的分发,以及各种基本的消息响应

3、InputClass类

输入管理类,记录键盘输入

4、GraphicsClass类

处理DirectX的图形代码的类,由于只是建立一个空的对话框,所以这个类几乎么有什么内容

WinMain部分

#include "systemclass.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow)
{
SystemClass* System;
bool result;          // 创建system对象。
System = new SystemClass;
if(!System)
{
return 0;
}         //初始化并运行system对象。
result = System->Initialize();
if(result)
{
System->Run();
}          // 关闭并释放system对象。
System->Shutdown();
delete System;
System = 0;
return 0;
}


SystemClass类部分

SystemClass.h

#ifndef SYSTEMCLASS_H
#define SYSTEMCLASS_H
#define WIN32_LEAN_AND_MEAN//去除某些不常用的API
#include <windows.h>

#include "inputclass.h"
#include "graphicsclass.h"

class SystemClass
{
public:
SystemClass();
SystemClass(const SystemClass&);
~SystemClass();

bool Initialize();
void Shutdown();
void Run();
LRESULT CALLBACK MessageHandler(HWND, UINT, WPARAM, LPARAM);

private:
bool Frame();
void InitializeWindows(int&, int&);
void ShutdownWindows();

private:
LPCWSTR m_applicationName;
HINSTANCE m_hinstance;
HWND m_hwnd;
InputClass* m_Input;
GraphicsClass* m_Graphics;
};
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

static SystemClass* ApplicationHandle = 0;

#endif

systemclass.cpp

#include "Systemclass.h"
SystemClass::SystemClass():
m_Input(NULL),
m_Graphics(NULL)
{
}
SystemClass::SystemClass(const SystemClass&)
{
}
SystemClass::~SystemClass()
{
}
bool SystemClass::Initialize()
{
int ScreenWindth,ScreenHeight;
ScreenHeight=ScreenWindth=0;
InitializeWindows(ScreenWindth,ScreenHeight);
m_Input=new InputClass();
m_Input->Initialize();
m_Graphics=new GraphicsClass();
int ret=m_Graphics->Initialize(ScreenWindth,ScreenHeight,m_hwnd);
if(ret==0)
{
return false;
}
return true;
}
void SystemClass::Shutdown()
{
if(m_Graphics!=NULL)
{
m_Graphics->Shutdown();
delete m_Graphics;
m_Graphics=NULL;
}
if(m_Input!=NULL)
{
delete m_Input;
m_Input=NULL;
}
ShutdownWindows();
}
void SystemClass::Run()
{
MSG msg;
bool done,result;
ZeroMemory(&msg,sizeof(MSG));
done=false;
while (!done)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message==WM_QUIT)
{
done=true;
}
else
{
result=Frame();
if(!result)
{
done=true;
}
}
}
}
bool SystemClass::Frame()
{
bool result;
if(m_Input->IsKeyDown(VK_ESCAPE))
{
return false;
}
result=m_Graphics->Frame();
if(!result)
{
return false;
}
return true;
}

LRESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam)
{
switch(umsg)
{
case WM_KEYDOWN:
m_Input->KeyDown((unsigned int)wParam);
break;
case WM_KEYUP:
m_Input->KeyUp((unsigned int)wParam);
break;
default:
return DefWindowProc(hwnd,umsg,wParam,lParam);
}
return 0;
}
void SystemClass::InitializeWindows(int& width, int& height)
{
WNDCLASSEX wc;
DEVMODE dmScreenSettings;
int posX,posY;
ApplicationHandle=this;
m_hinstance=GetModuleHandle(NULL);
m_applicationName=TEXT("Demo Window");
wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc   = WndProc;
wc.cbClsExtra    = 0;
wc.cbWndExtra    = 0;
wc.hInstance     = m_hinstance;
wc.hIcon         = LoadIcon(NULL, IDI_WINLOGO);
wc.hIconSm       = wc.hIcon;
wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName  = NULL;
wc.lpszClassName = m_applicationName;
wc.cbSize        = sizeof(WNDCLASSEX);
RegisterClassEx(&wc);
width=GetSystemMetrics(SM_CXSCREEN);
height=GetSystemMetrics(SM_CYSCREEN);
if(FULL_SCREEN)
{
memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
dmScreenSettings.dmSize       = sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth  = (unsigned long)width;
dmScreenSettings.dmPelsHeight = (unsigned long)height;
dmScreenSettings.dmBitsPerPel = 32;
dmScreenSettings.dmFields     = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);

posY=posX=0;
}
else
{
width=800;
height=600;
posX = (GetSystemMetrics(SM_CXSCREEN) - width)  / 2;
posY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
}
m_hwnd=CreateWindowEx(WS_EX_APPWINDOW,m_applicationName,m_applicationName,
WS_CLIPSIBLINGS|WS_CLIPCHILDREN|WS_POPUP,
posX,posY,width,height,NULL,NULL,m_hinstance,NULL);
ShowWindow(m_hwnd,SW_SHOW);
SetForegroundWindow(m_hwnd);
SetFocus(m_hwnd);
ShowCursor(false);
return;
}
void SystemClass::ShutdownWindows()
{
ShowCursor(true);
if(FULL_SCREEN)
{
ChangeDisplaySettings(NULL,0);
}
DestroyWindow(m_hwnd);
m_hwnd=NULL;
UnregisterClass(m_applicationName,m_hinstance);
m_hinstance=NULL;
ApplicationHandle=NULL;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam)
{
switch(umessage)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
return ApplicationHandle->MessageHandler(hwnd,umessage,wparam,lparam);
}
return 0;
}


InputClass类部分

Inputclass.h

#ifndef _INPUTCLASS_H_
#define _INPUTCLASS_H_
class InputClass
{
public:
InputClass();
InputClass(const InputClass&);
~InputClass();

void Initialize();
void KeyDown(unsigned int);
void KeyUp(unsigned int);
bool IsKeyDown(unsigned int);

private:
bool m_keys[256];
};

#endif

inputclass.cpp

#include "inputclass.h"
#include <windows.h>
InputClass::InputClass()
{

}

InputClass::InputClass(const InputClass& other)
{

}

InputClass::~InputClass()
{

}

void InputClass::Initialize()
{
int i;
for(i=0; i<256; i++)
{
m_keys[i] = false;
}
return;
}

void InputClass::KeyDown(unsigned int input)
{
m_keys[input] = true;
return;
}

void InputClass::KeyUp(unsigned int input)
{
m_keys[input] = false;
return;
}

bool InputClass::IsKeyDown(unsigned int key)
{
return m_keys[key];
}

GraphicsClass类部分

graphicsclass.h

#ifndef _GRAPHICSCLASS_H_
#define _GRAPHICSCLASS_H_
#include<windows.h>

const bool FULL_SCREEN = true;
const bool VSYNC_ENABLED = true;
const float SCREEN_DEPTH = 1000.0f;
const float SCREEN_NEAR = 0.1f;

class GraphicsClass
{
public:
GraphicsClass();
GraphicsClass(const GraphicsClass&);
~GraphicsClass();

bool Initialize(int, int, HWND);
void Shutdown();
bool Frame();

private:
bool Render();
private:

};

#endif

Graphicsclass.cpp

#include "graphicsclass.h"

GraphicsClass::GraphicsClass()
{

}

GraphicsClass::GraphicsClass(const GraphicsClass& other)
{

}

GraphicsClass::~GraphicsClass()
{

}

bool GraphicsClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
{
return true;
}

void GraphicsClass::Shutdown()
{
return;
}

bool GraphicsClass::Frame()
{
return true;
}

bool GraphicsClass::Render()
{
return true;
}


这四个部分可以实现一个简单的窗口。

#include "graphicsclass.h"

GraphicsClass::GraphicsClass()
{

}

GraphicsClass::GraphicsClass(const GraphicsClass& other)
{

}

GraphicsClass::~GraphicsClass()
{

}

bool GraphicsClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
{
return true;
}

void GraphicsClass::Shutdown()
{
return;
}

bool GraphicsClass::Frame()
{
return true;
}

bool GraphicsClass::Render()
{
return true;
}


GraphicsClassGraphicsClassGraphicsClass

#include "inputclass.h"
#include <windows.h>
InputClass::InputClass()
{

}

InputClass::InputClass(const InputClass& other)
{

}

InputClass::~InputClass()
{

}

void InputClass::Initialize()
{
int i;
for(i=0; i<256; i++)
{
m_keys[i] = false;
}
return;
}

void InputClass::KeyDown(unsigned int input)
{
m_keys[input] = true;
return;
}

void InputClass::KeyUp(unsigned int input)
{
m_keys[input] = false;
return;
}

bool InputClass::IsKeyDown(unsigned int key)
{
return m_keys[key];
}


#ifndef _INPUTCLASS_H_
#define _INPUTCLASS_H_
class InputClass
{
public:
InputClass();
InputClass(const InputClass&);
~InputClass();

void Initialize();
void KeyDown(unsigned int);
void KeyUp(unsigned int);
bool IsKeyDown(unsigned int);

private:
bool m_keys[256];
};

#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: