Game.h
#include "windows.h"
#include "DxLib.h"
typedef int (__cdecl *LOOPPROC)( int );
class Game
{
public:
int Start( LOOPPROC );
BOOL Init( void );
void WindowMode( void );
void SetTitle( LPTSTR lpTitle );
Game();
virtual ~Game();
BOOL err;
int keypad;
};
typedef int (__cdecl *PLAYPROC)( void );
class Play
{
public:
void Draw( void );
void Position( int x, int y );
int Check( PLAYPROC, Game &env );
void Image( LPTSTR lpPath );
Play();
virtual ~Play();
int ImgHandle;
BOOL left;
BOOL right;
int x;
int y;
};
メイン
#include "DxLib.h"
#include "Game.h"
#include "Play.h"
// ゲーム環境クラス
Game env;
// プレイヤークラス
Play me;
// ループ Callback 関数
int GameLoop( int keypad );
// プレイヤー処理 Callback 関数
int PlayCheck( void );
// ********************************************************
// 開始処理
// ********************************************************
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
// タイトル設定
env.SetTitle( "ゲームプログラミング" );
// ウインドウモード設定
env.WindowMode();
// 初期化
if ( !env.Init( ) ) {
return -1 ;
}
// プレイヤー画像のロード
me.Image( "play.bmp" );
// プレイヤー初期位置
me.Position( 300, 400 );
// 処理開始
int ret;
ret = env.Start( (LOOPPROC)GameLoop );
return 0;
}
// ********************************************************
// ゲーム用ループ
// 終わる時は return false;
// ********************************************************
int GameLoop( int keypad )
{
// プレイヤーの処理
me.Check( (PLAYPROC)PlayCheck, env );
return true;
}
// ********************************************************
// プレイヤー処理関数
// ********************************************************
int PlayCheck( void )
{
// 右へ移動
if ( me.right ) {
me.x += 3;
}
// 左へ移動
if ( me.left ) {
me.x -= 3;
}
me.Draw();
return 0;
}
Game クラス
#include "Game.h"
// ********************************************************
// コンストラクタ
// ********************************************************
Game::Game()
{
}
// ********************************************************
// デストラクタ
// ********************************************************
Game::~Game()
{
if ( !Game::err ) {
// DXライブラリ使用の終了処理
DxLib_End() ;
}
}
// ********************************************************
// 開始直前処理
// ********************************************************
void Game::SetTitle( LPTSTR lpTitle )
{
SetMainWindowText( lpTitle ) ;
}
void Game::WindowMode( void )
{
ChangeWindowMode( true ) ;
}
// ********************************************************
// 開始処理
// ********************************************************
BOOL Game::Init( void )
{
this->err = false;
if( DxLib_Init() == -1 ) {
this->err = true;
return false;
}
return true;
}
// ********************************************************
// 開始処理
// ********************************************************
int Game::Start( LOOPPROC loop )
{
// 描画先画面を裏画面にセット
SetDrawScreen( DX_SCREEN_BACK ) ;
while( 1 ) {
// Windows Message 処理
if ( ProcessMessage() != 0 ) {
return 0;
break;
}
// ESC で強制終了
if ( CheckHitKey( KEY_INPUT_ESCAPE ) == 1 ) {
return 1;
}
// 画面を初期化する
ClearDrawScreen() ;
this->keypad = GetJoypadInputState( DX_INPUT_KEY_PAD1 );
if ( loop( keypad ) == false ) {
return 2;
}
// 裏画面の内容を表画面に反映させる
ScreenFlip() ;
}
return 0;
}
Play クラス
#include "Game.h"
Play::Play()
{
}
Play::~Play()
{
}
void Play::Image( LPTSTR lpPath )
{
this->ImgHandle = LoadGraph( lpPath );
}
int Play::Check( PLAYPROC play, Game &env )
{
this->right = false;
if( env.keypad & PAD_INPUT_RIGHT ) {
this->right = true;
}
this->left = false;
if( env.keypad & PAD_INPUT_LEFT ) {
this->left = true;
}
return play();
}
void Play::Position( int x, int y )
{
this->x = x;
this->y = y;
}
void Play::Draw( void )
{
DrawGraph( this->x , this->y , this->ImgHandle , TRUE );
}