64,281
社区成员
发帖
与我相关
我的任务
分享
//momogame.h
namespace Wnd
{
const DWORD LEFT =-1, TOP = -2;
const DWORD RIGHT = -3, DOWN = -4;
const DWORD XCENTRE = -5, YCENTRE = -6;
const DWORD DEFULT = CW_USEDEFAULT;
DWORD CountWndPos( DWORD pos, const GameWindow *wnd, const GameWindow *parentwnd );
}
//load.cpp
DWORD Wnd::CountWndPos( DWORD pos, const GameWindow *wnd, const GameWindow *parentwnd = NULL )
{
if( wnd == NULL)
return pos;
DWORD with = wnd ->GetWith();
DWORD height = wnd ->GetHeight();
if( parent == NULL )
{
parentwith = GetSystemMetrics(SM_CXSCREEN);
parentheight = GetSystemMetrics(SM_CYSCREEN);
}
else
{
DWORD parentwith = parentwnd ->GetWith();
DWORD parentheight = parentwnd ->GetHeight();
}
switch( pos )
{
case XCENTRE:
return ( parentwith - with )/2;
case YCENTRE:
return ( parentheight - height )/2;
case DEFULT:
return DEFULT;
case LEFT:
case TOP:
return 0;
case RIGHT:
return parentwith - with;
case DOWN:
return parentheight - height;
}
return pos;
}
namespace Wnd
{
const DWORD LEFT =-1, TOP = -2;
const DWORD RIGHT = -3, DOWN = -4;
const DWORD XCENTRE = -5, YCENTRE = -6;
const DWORD DEFULT = CW_USEDEFAULT;
DWORD CountWndPos( DWORD pos, const GameWindow *wnd, const GameWindow *parentwnd );//编译器看到这里的时候,还真不知道GameWindow是什么
}
int main()
{
a=1;//编译器看到这里的时候,还真不知道a是什么
int a;
}
class GameWindow;//这里前向声明
namespace Wnd
{
const DWORD LEFT =-1, TOP = -2;
const DWORD RIGHT = -3, DOWN = -4;
const DWORD XCENTRE = -5, YCENTRE = -6;
const DWORD DEFULT = CW_USEDEFAULT;
DWORD CountWndPos( DWORD pos, const GameWindow *wnd, const GameWindow *parentwnd );
}
/*-----------------------------------头文件部分------------------------------------*/
#include <windows.h>
#include <ddraw.h>
#include <string.h>
#pragma comment(lib, "ddraw.lib")
#pragma comment(lib, "dxguid.lib")
/*-----------------------------------预处理部分-----------------------------------*/
#define DDRAW_INIT_STRUCT(ddstruct) { memset(&ddstruct, 0, sizeof(ddstruct)); ddstruct.dwSize = sizeof(ddstruct); }
//位图结构
typedef struct tagBITMAP_FILE_TAG
{
BITMAPFILEHEADER fileheader;
BITMAPINFOHEADER infoheader;
LPBYTE buffer;
}BITMAPSTRUCT, * LPBITMAPSTRUCT ;
//游戏窗口常量
namespace Wnd
{
const DWORD LEFT =-1, TOP = -2;
const DWORD RIGHT = -3, DOWN = -4;
const DWORD XCENTRE = -5, YCENTRE = -6;
const DWORD DEFULT = CW_USEDEFAULT;
DWORD CountWndPos( DWORD pos, const GameWindow *wnd, const GameWindow *parentwnd );
}
/*---------------------------------类型定义部分----------------------------------*/
//DDraw对象类
class DriectDraw
{
public:
DriectDraw();
~DriectDraw();
const LPDIRECTDRAW7 GetLPDriectDraw() const{ return lpdd; }
private:
LPDIRECTDRAW7 lpdd;
};
//DDrawSuface页面类
class DDrawSuface
{
public:
DWORD with;
DWORD height;
DWORD bit;
DDrawSuface();
virtual ~DDrawSuface() = 0;
virtual bool Create( const DriectDraw& );
const LPDIRECTDRAWSURFACE7 GetLPDDrawSuface() const{ return lpddsuface; }
void GetInfo();
protected:
LPDIRECTDRAW7 lpdd;
bool flag;
DDSURFACEDESC2 ddsd;
LPDIRECTDRAWSURFACE7 lpddsuface;
};
//MainSuface主页面类
class MainSuface :public DDrawSuface
{
public:
MainSuface();
~MainSuface(){}
};
//BackSuface从页面类
class BackSuface :public DDrawSuface
{
public:
BackSuface( DWORD, DWORD );
~BackSuface(){}
};
//DDrawClipper剪裁器类
class DDrawClipper
{
public:
DDrawClipper();
virtual ~DDrawClipper() = 0;
virtual bool Create( const DriectDraw& ddraw );
const LPDIRECTDRAWCLIPPER GetLPClipper() const{ return lpddclipper; }
protected:
LPDIRECTDRAW7 lpdd;
bool flag;
LPDIRECTDRAWCLIPPER lpddclipper;
};
//GameWindow窗口类
class GameWindow
{
public:
GameWindow::GameWindow( const WNDCLASSEX &wc, LPCSTR title = TEXT("MOMO游戏"),
DWORD Xpos = CW_USEDEFAULT, DWORD Ypos = CW_USEDEFAULT,
DWORD with = CW_USEDEFAULT, DWORD height = CW_USEDEFAULT,
DWORD style = WS_OVERLAPPEDWINDOW, DWORD EXstyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
virtual ~GameWindow() = 0{};
const HDC GetDC() const{ return ::GetDC( hwnd ); }
const HINSTANCE GetInstance() const{ return hinstance; }
const DWORD GetWith() const{ return with; }
const DWORD GetHeight() const { return height; }
const WNDCLASSEX GetWndClass() const { return wc; }
const HWND GetParent() const{ return hparent; }
virtual bool Create();
void Show( int );
bool Destroy(){ return (bool)DestroyWindow( hwnd ); }
protected:
LPCSTR title;
DWORD EXstyle;
DWORD style;
DWORD Xpos;
DWORD Ypos;
DWORD with;
DWORD height;
WNDCLASSEX wc;
HWND hparent;
HWND hwnd;
HINSTANCE hinstance;
};
//MainWindow主窗口类
class MainWindow :public GameWindow
{
public:
MainWindow( WNDCLASSEX, LPCSTR, DWORD, DWORD, DWORD, DWORD );
MainWindow( WNDCLASSEX, RECT, DWORD );
virtual ~MainWindow();
const DWORD GetScreenWith() const{ return screenwith; }
const DWORD GetScreenHeight() const{ return screenheight; }
const DWORD GetColorBit() const{ return bit; }
bool SetDisplayMode( DWORD, DWORD, DWORD );
virtual bool Create();
private:
static bool flag;
bool fullscreenflag;
DWORD screenwith;
DWORD screenheight;
DWORD bit;
};
#include "MOMOgame.h"
//函数实现部分
DWORD Wnd::CountWndPos( DWORD pos, const GameWindow *wnd, const GameWindow *parentwnd = NULL )
{
if( wnd == NULL)
return pos;
DWORD with = wnd ->GetWith();
DWORD height = wnd ->GetHeight();
if( parent == NULL )
{
parentwith = GetSystemMetrics(SM_CXSCREEN);
parentheight = GetSystemMetrics(SM_CYSCREEN);
}
else
{
DWORD parentwith = parentwnd ->GetWith();
DWORD parentheight = parentwnd ->GetHeight();
}
switch( pos )
{
case XCENTRE:
return ( parentwith - with )/2;
case YCENTRE:
return ( parentheight - height )/2;
case DEFULT:
return DEFULT;
case LEFT:
case TOP:
return 0;
case RIGHT:
return parentwith - with;
case DOWN:
return parentheight - height;
}
return pos;
}
//类实现部分
//DriectDraw类
DriectDraw::DriectDraw()
{
if ( FAILED(DirectDrawCreateEx( NULL, (void**)&lpdd, IID_IDirectDraw7, NULL ) ) )// 创建DirectCraw对象
{
MessageBox ( NULL, TEXT("创建DirectCraw对象错误!"),
TEXT("错误!"), MB_ICONERROR);
lpdd = NULL;
}
}
DriectDraw::~DriectDraw()
{
if( lpdd != NULL )
{
lpdd ->Release();
lpdd = NULL;
}
}
//DDrawSuface类
DDrawSuface::DDrawSuface()
{
DDRAW_INIT_STRUCT( ddsd );
lpddsuface = NULL;
flag = false;
}
DDrawSuface::~DDrawSuface()
{
if( !lpddsuface )
lpddsuface ->Release();
lpddsuface = NULL;
}
bool DDrawSuface::Create( const DriectDraw& ddraw )
{
lpdd = ddraw.GetLPDriectDraw();
if( flag )
{
MessageBox ( NULL, TEXT("已创建页面!"),
TEXT("错误!"), MB_ICONERROR);
return false;
}
if( FAILED(lpdd ->CreateSurface( &ddsd, &lpddsuface, NULL ) ) )
{
MessageBox ( NULL, TEXT("创建页面错误!"),
TEXT("错误!"), MB_ICONERROR);
lpddsuface = NULL;
return false;
}
flag = true;
return true;
}
//MainSufce类
MainSuface::MainSuface()
{
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; // 指定要创建的是主页面
}
//BackSuface类
BackSuface::BackSuface( DWORD w = 800, DWORD h = 600 )
{
with = w;
height = h;
lpdd = NULL;
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT ;
ddsd.dwWidth = with;
ddsd.dwHeight = height;
ddsd.ddsCaps.dwCaps = DDSCAPS_VIDEOMEMORY | DDSCAPS_OFFSCREENPLAIN ; // 指定要创建的是从页面
}
//DDrawClipper类
DDrawClipper::DDrawClipper()
{
lpddclipper = NULL;
}
DDrawClipper::~DDrawClipper()
{
if( !lpddclipper )
lpddclipper ->Release();
lpddclipper = NULL;
}
bool DDrawClipper::Create( const DriectDraw& ddraw )
{
lpdd = ddraw.GetLPDriectDraw();
if( flag )
{
MessageBox ( NULL, TEXT("已创建剪裁器!"),
TEXT("错误!"), MB_ICONERROR);
return false;
}
if( FAILED( lpdd ->CreateClipper( 0, &lpddclipper, NULL ) ) )
{
MessageBox ( NULL, TEXT("创建剪裁器错误!"),
TEXT("错误!"), MB_ICONERROR);
lpddclipper = NULL;
return false;
}
flag = true;
return true;
}
//GameWindow类
GameWindow::GameWindow( const WNDCLASSEX &wc, LPCSTR title = TEXT("MOMO游戏"),
DWORD Xpos = CW_USEDEFAULT, DWORD Ypos = CW_USEDEFAULT,
DWORD with = CW_USEDEFAULT, DWORD height = CW_USEDEFAULT,
DWORD style = WS_OVERLAPPEDWINDOW, DWORD EXstyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE)
{
this ->title = title;
this ->EXstyle = EXstyle;
this ->style = style;
this ->Xpos = Xpos;
this ->Ypos = Ypos;
if( with == 0 )
this ->with = 1;
else
this ->with = 1;
if( height == 0 )
this ->height = 1;
else
this ->height = winheight;
this ->wc = wc;
hparent = NULL;
hwnd = NULL;
hinstance = wc.hInstance;
if (!RegisterClass (&wc))
{
MessageBox ( hwnd, TEXT ("错误!不能注册窗口!"),
TEXT ("错误!"), MB_ICONERROR);
}
}
bool GameWindow::Create()
{
if(hwnd = CreateWindow( wc.lpszClassName , // 类别名称
title, // 标题名称
style, // 窗口风格
Xpos, // 初始化起点X坐标
Ypos, // 初始化起点Y坐标
with, // 窗口宽度
height, // 窗口高度
hparent, // 父视窗句柄
NULL, // 菜单栏句柄
hinstance, // 执行实体句柄
NULL) ) // 建立参数指针
return true;
return false;
}
void GameWindow::Show( int iShowWay )
{
ShowWindow( hwnd, iShowWay ); // 显示并更新窗口
UpdateWindow( hwnd ); // 更新窗口
}
//MainWindow游戏主窗口类
MainWindow::MainWindow()
{
title = "MOMO游戏";
}
MainWindow::MainWindow( const WNDCLASSEX &wc, LPCSTR title,
DWORD Xpos = Wnd::XCENTRE, DWORD Ypos = Wnd::YCENTRE, DWORD with = 800, DWORD height = 600 )
:GameWindow( wc, title, Xpos, Ypos, with, height )
{
this ->Xpos = Wnd::CountWndPos( Xpos, this );
this ->Ypos = Wnd::CountWndPos( Ypos, this );
fullscreenflag = false;
screenwith = GetSystemMetrics(SM_CXSCREEN);
screenheight = GetSystemMetrics(SM_CYSCREEN);
bit = GetDeviceCaps( ::GetDC( NULL ), BITSPIXEL );
}
bool MainWindow::flag = false;