C++でシーンが変更できない
Posted: 2016年10月04日(火) 20:54
シーントップからシーンメニューにZキーで変更するようにしたつもりなのですが、変更できません
どうしてでしょうか?
ソース↓
https://github.com/inec0310/BulletHellGame
どうしてでしょうか?
ソース↓
https://github.com/inec0310/BulletHellGame
void Top::Update(){
if(CheckHitKey(KEY_INPUT_Z)==1){
SceneChange(Scene_Menu);
// ↑ これは this->SceneChange(Scene_Menu); を呼んでるけど
// 実装が空だから何も起きない
// 本当は、コンストラクタで受け取ってる changer を使って
// changer->SceneChange(Scene_Menu); にしたかった?
}
}
#include "DxLib.h" // DxLib
#include <iostream> // cerr
#include <array> // array
#include <vector> // vector
#include <climits> // UINT_MAX
#include <memory> // unique_ptr
#include <crtdbg.h> // _CrtSetDbgFlag
// DXライブラリ簡易ラップクラス
struct DxLibApp final
{
DxLibApp(const char* pWindowText = nullptr) try
: windowWidth_(640)
, windowHeight_(480)
, colorBitDepth_(16)
{
if (pWindowText != nullptr){
DxLib::SetWindowText(pWindowText);
}
SetGraphMode(windowWidth_, windowHeight_, colorBitDepth_);
ChangeWindowMode(TRUE);
if (DxLib_Init() == -1){
throw "DXライブラリの初期化に失敗しました。";
}
SetDrawScreen(DX_SCREEN_BACK);
}
catch (char* errStr) {
std::cerr << errStr << std::endl;
}
~DxLibApp(){
DxLib_End();
}
static bool ProcessLoop(){
return (ScreenFlip() == 0 && ProcessMessage() == 0 && ClearDrawScreen() == 0);
}
int windowWidth_; // ウィンドウ横幅
int windowHeight_; // ウィンドウ縦幅
int colorBitDepth_; // カラービット数
};
// 簡易入力クラス
struct InputKey final
{
InputKey()
: keyBuffer_()
, backKeyBuffer_()
{}
~InputKey(){}
inline int Update() {
std::copy(keyBuffer_.begin(), keyBuffer_.end(), backKeyBuffer_.begin());
return GetHitKeyStateAll(&keyBuffer_.front());
}
inline bool IsPress(const int keyCode) const {
return keyBuffer_[keyCode] ? true : false;
}
inline bool IsKeyDown(const int keyCode) const {
return (keyBuffer_[keyCode] && !backKeyBuffer_[keyCode]);
}
inline bool IsKeyUp(const int keyCode) const {
return (!keyBuffer_[keyCode] && backKeyBuffer_[keyCode]);
}
private:
typedef std::array<char, 256> KeyStateBuffer;
KeyStateBuffer keyBuffer_;
KeyStateBuffer backKeyBuffer_;
};
// 色定義
struct Color final
{
static const unsigned int White;
};
const unsigned int Color::White = GetColor(255, 255, 255);
// グローバル変数
static DxLibApp app("シーン変更テスト");
static InputKey inputKey;
// シーン列挙
enum eScene : unsigned int
{
Scene_Top , // トップ画面
Scene_Menu , // メニュー画面
Scene_Count , // シーン数
Scene_Other = UINT_MAX, // その他
};
// タスククラス:動作には必ずこのクラスを継承
// (GameObjectクラス的な物だろうか?)
struct ITask {
enum CommonResult : int
{
Result_OK = 0,
Result_Faile = -1
};
virtual int Init() = 0; //初期処理
virtual int End() = 0; //終了処理
virtual int Update() = 0; //更新処理
virtual int Draw() = 0; //描画処理
};
// Sceneインターフェース
struct IScene : public ITask
{
IScene(){}
virtual ~IScene(){}
virtual void ChangeScene(eScene scene) = 0;
};
// Sceneアブストラクト
// (規定の動作を実装するクラス)
struct AbsScene : public IScene
{
AbsScene(){}
virtual ~AbsScene(){}
virtual int Init() override {
OutputDebugString("AbsScene::Init \n");
return CommonResult::Result_OK;
}
virtual int End() override {
OutputDebugString("AbsScene::End \n");
return CommonResult::Result_OK;
}
};
// シーンコントローラ
struct SceneController final : public IScene
{
SceneController()
: sceneIndex_(0)
, sceneArrayList_()
{}
~SceneController(){}
// Topシーン
// (※ インナーが嫌だったら、適当にファイル分割して外に出す)
struct TopScene final : public IScene
{
TopScene()
: pSceneController_(nullptr)
{}
TopScene(SceneController* pSceneController)
: pSceneController_(pSceneController)
{}
~TopScene(){}
int Init() override {
OutputDebugString("TopScene::Init \n");
return CommonResult::Result_OK;
}
int End() override {
OutputDebugString("TopScene::End \n");
return CommonResult::Result_OK;
}
int Update() override {
if (inputKey.IsKeyDown(KEY_INPUT_Z)){
this->ChangeScene(eScene::Scene_Menu);
}
return CommonResult::Result_OK;
}
int Draw() override {
DrawString(0, 0, typeid(this).name(), Color::White);
return CommonResult::Result_OK;
}
void ChangeScene(eScene scene) override {
pSceneController_->ChangeScene(scene);
}
private:
SceneController* pSceneController_;
};
// Menuシーン
// (※ インナーが嫌だったら、適当にファイル分割して外に出す)
struct MenuScene final : public AbsScene
{
MenuScene()
: pSceneController_(nullptr)
{}
MenuScene(SceneController* pSceneController)
: pSceneController_(pSceneController)
{}
~MenuScene(){}
int Update() override {
if (inputKey.IsKeyDown(KEY_INPUT_Z)){
this->ChangeScene(eScene::Scene_Top);
}
return CommonResult::Result_OK;
}
int Draw() override {
DrawString(0, 0, typeid(this).name(), Color::White);
return CommonResult::Result_OK;
}
void ChangeScene(eScene scene) override {
pSceneController_->ChangeScene(scene);
}
private:
SceneController* pSceneController_;
};
int Init() override
{
sceneArrayList_.clear();
sceneArrayList_.emplace_back(new (std::nothrow) TopScene(this));
sceneArrayList_.emplace_back(new (std::nothrow) MenuScene(this));
// シーンの作成に失敗していたらfalseを戻す
for (auto it = sceneArrayList_.begin(); it != sceneArrayList_.end(); ++it) {
if (!(*it)){
return CommonResult::Result_Faile;
}
}
sceneIndex_ = eScene::Scene_Top;
return sceneArrayList_[sceneIndex_]->Init();
}
int End() override {
return sceneArrayList_[sceneIndex_]->End();
}
int Update() override {
return sceneArrayList_[sceneIndex_]->Update();
}
int Draw() override {
return sceneArrayList_[sceneIndex_]->Draw();
}
void ChangeScene(eScene scene) {
// 現在のシーンを終了し、変更後のシーンの初期化
sceneArrayList_[sceneIndex_]->End();
sceneIndex_ = scene;
sceneArrayList_[sceneIndex_]->Init();
}
private:
unsigned int sceneIndex_;
std::vector<std::unique_ptr<IScene>> sceneArrayList_;
};
// エントリーポイント
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
// メモリリーク検出
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
SceneController sceneController;
if (sceneController.Init() == ITask::CommonResult::Result_Faile){
return -1;
}
while (DxLibApp::ProcessLoop() && inputKey.Update() == 0)
{
sceneController.Update();
sceneController.Draw();
}
if (sceneController.End() == ITask::CommonResult::Result_Faile){
return -1;
}
return 0;
}