CODE:
//StdAfx.h
#pragma once
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include //スマートポインターで使うため必要ヘッダー
CODE:
//StdAfx.cpp
#include "StdAfx.h"
CODE:
//Application.h
#pragma once
#include "Game.h"
class Application
{
int ProcessLoop();
Game* game;
public:
Application(void);
~Application(void);
int Run();
};
CODE:
#include "Application.h"
Application::Application(void) {
if( ChangeWindowMode(TRUE)!= 0){
throw std::runtime_error("ウィンドウ切替に失敗");
}
if( SetOutApplicationLogValidFlag(FALSE)){
throw std::runtime_error("ログ出力無効に失敗");
}
if( DxLib_Init() != 0){
throw std::runtime_error("DXライブラリ初期化に失敗");
}
if( SetDrawScreen(DX_SCREEN_BACK) != 0){
throw std::runtime_error("裏画面設定に失敗");
}
}
//boost::shared_ptrがdeleteしたときに呼ばれる
Application::~Application(void){
if( DxLib_End() != 0){
throw std::runtime_error("DXライブラリ終了処に失敗");
}
}
//Runメソッドから毎フレーム呼ばれるDXライブラリ3大処理
int Application::ProcessLoop() {
if(ClearDrawScreen() != 0){
return -1;
}
if( ProcessMessage() != 0){
return -1;
}
if( ScreenFlip() != 0 ){
return -1;
}
return 0;
}
int Application::Run(){
//ゲームクラスのインスタンスを生成する
game = new Game();
//メインループの開始
while(1) {
//ProcessLoopが0を返す時だけゲームクラスのRunメソッドを毎フレーム実行
if( ProcessLoop() == 0){
game->Run();
} else {
// -1が返されたらブレイク
break;
}
}
//ゲームクラスのインスタンスを削除
delete game;
return 0;
}
CODE:
//Game.h
#pragma once
class Game {
public:
Game();
~Game();
void Run();
};
CODE:
//Game.cpp
#include "Game.h"
Game::Game() {
}
Game::~Game(){
}
void Game::Run() {
}
CODE:
//Main.cpp
#include "Application.h"
//スマートポインタ
typedef boost::shared_ptr ApplicationPtr;
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
ApplicationPtr app(new Application);
return app->Run();
}
[hr]
解説:
このプログラムはApplicationクラスのRunから呼ばれているProcessMessageが0を返す時だけDXライブラリ3大処理を行い-1が帰ってきたときはループを抜けます。
スマートポインタのboost::shared_ptrを用いるため delete がいらなくなります。