今、テキストファイルから読み込んだデータをもとに問題を出題し入力された解答に対して正誤判定を行うというプログラムを作ろうと思っています。
しかし、それ以前に起動した瞬間に.exeが停止してしまい前に進めない状態です。ソースコードのどこに原因があるのでしょうか?
デバッグしたときはscreenのNowScreenNumの値が初期化されてないような値を示していたのでそれが原因かなと思ったのですがソースコードのどこに原因があるのかわわかりませんでした。
オブジェクト指向プログラミングをしようと練習中です。添付ファイルにUMLのクラス図を載せています。
構造や実装方法でも指摘やアドバイスがあればいってください。
環境 windows10,visualstudio2015community,C++,Dxライブラリ使用
以下コードです
common.h
#pragma once
#include "DxLib.h"
#define HEIGHT 400
#define WIDTH 640
#define BITS 16
#define TITLE "WORDLEARNING"
const int Color = GetColor(255, 255, 255);
#pragma once
template <typename _T>
class Singleton {
protected:
Singleton(){}
virtual ~Singleton(){}
Singleton(const Singleton& r){}
Singleton& operator=(const Singleton& r){}
public:
static _T* Instance() {
static _T inst;
return &inst;
}
};
#pragma once
#include "Singleton.h"
class Keyboard:public Singleton<Keyboard>{
Keyboard();
friend Singleton<Keyboard>;
public:
bool Update();
int GetPressingCount(int KeyCode);
int GetReleasingCount(int KeyCode);
private:
static const int KEY_NUM = 256;
int mKeyPressingCount[KEY_NUM];
int mKeyReleasingCount[KEY_NUM];
bool IsAvailableCode(int KeyCode);
};
#include "Keyboard.h"
#include <DxLib.h>
Keyboard::Keyboard(){
memset(mKeyPressingCount, 0, sizeof(mKeyPressingCount));
memset(mKeyReleasingCount, 0, sizeof(mKeyReleasingCount));
}
bool Keyboard::Update(){
char NowKeyState[KEY_NUM];
GetHitKeyStateAll(NowKeyState);
for (int i = 0;i < KEY_NUM;i++) {
if (NowKeyState[i] != 0) {
if (mKeyReleasingCount[i] > 0)mKeyReleasingCount[i] = 0;
mKeyPressingCount[i]++;
}
else {
if (mKeyPressingCount[i] > 0)mKeyPressingCount[i] = 0;
mKeyReleasingCount[i]++;
}
}
return true;
}
int Keyboard::GetPressingCount(int KeyCode){
if (!Keyboard::IsAvailableCode(KeyCode))return -1;
return mKeyPressingCount[KeyCode];
}
int Keyboard::GetReleasingCount(int KeyCode){
if (!Keyboard::IsAvailableCode(KeyCode))return -1;
return mKeyReleasingCount[KeyCode];
}
bool Keyboard::IsAvailableCode(int KeyCode){
if (!(0 <= KeyCode&&KeyCode < KEY_NUM))return false;
return true;
}
#pragma once
#include "Screen.h"
class Title {
private:
int transmittance;
int transmitswitch;
public:
Title();
~Title();
void Draw();
};
#include "Title.h"
#include "Common.h"
#include <DxLib.h>
Title::Title(){
transmitswitch = 0;
transmittance = 100;
}
void Title::Draw(){
switch (transmitswitch){
case 0:
transmittance += 2;
if (transmittance >= 255)transmitswitch++;
break;
case 1:
transmittance -= 2;
if (transmittance <= 80)transmitswitch--;
break;
}
DrawString(180, 180, "WORD LEARNING", Color);
SetDrawBlendMode(DX_BLENDMODE_ALPHA, transmittance);
DrawString(180, 300, "Press Enter to start", Color);
SetDrawBlendMode(DX_BLENDMODE_NOBLEND, 0);
}
Title::~Title(){
}
#pragma once
#include "Screen.h"
#include "Title.h"
class ScreenTransition{
protected:
int NowScreenNum;
bool active;
public:
ScreenTransition();
~ScreenTransition();
virtual void Transition() = 0;
bool GetActiveFlag();
};
#include "ScreenTransition.h"
ScreenTransition::ScreenTransition(){
}
bool ScreenTransition::GetActiveFlag() {
return active;
}
ScreenTransition::~ScreenTransition() {
}
#pragma once
#include "ScreenTransition.h"
class TitleScreen:public ScreenTransition{
private:
Title* title;
public:
TitleScreen();
~TitleScreen();
void Transition();
};
#include "TitleScreen.h"
#include "Keyboard.h"
#include "DxLib.h"
#include "Common.h"
TitleScreen::TitleScreen(){
NowScreenNum = 0;
active = true;
title = new Title;
}
void TitleScreen::Transition(){
switch (NowScreenNum){
case 0:
title->Draw();
break;
case 1:
active = false;
break;
default:
DrawFormatString(320, 150, Color, "%d", NowScreenNum);
break;
}
if (Keyboard::Instance()->GetPressingCount(KEY_INPUT_RETURN) == 1)NowScreenNum++;
}
TitleScreen::~TitleScreen(){
delete title;
}
#pragma once
#include "ScreenTransition.h"
class StateTransition{
private:
int NowStateNum;
int BackScreenGh;
ScreenTransition* screen[4];
public:
StateTransition();
~StateTransition();
void Draw();
void Transition();
};
#include "StateTransition.h"
#include "Common.h"//完成後必要ない
#include "Keyboard.h"
#include "DxLib.h"
#include "TitleScreen.h"
StateTransition::StateTransition(){
NowStateNum = 0;
BackScreenGh= LoadGraph("Graph/backscreen.png");//640*400
TitleScreen titleS;
screen[0] = &titleS;
}
void StateTransition::Draw(){
DrawGraph(0, 0, BackScreenGh, TRUE);
}
void StateTransition::Transition() {
while (!ScreenFlip() && !ProcessMessage() && !ClearDrawScreen()) {
Keyboard::Instance()->Update();
Draw();
switch (NowStateNum) {
case 0:
screen[0]->Transition();
if(screen[0]->GetActiveFlag())NowStateNum++;
break;
case 1:
DrawFormatString(320, 150, Color, "%d", NowStateNum);
break;
}
//if (Keyboard::Instance()->GetPressingCount(KEY_INPUT_RETURN) == 1)NowStateNum++;
if (Keyboard::Instance()->GetPressingCount(KEY_INPUT_ESCAPE) != 0)break;
}
}
StateTransition::~StateTransition(){
}
#include <DxLib.h>
#include "Common.h"
#include "StateTransition.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
SetGraphMode(WIDTH, HEIGHT, BITS);
SetMainWindowText(TITLE);
ChangeWindowMode(TRUE);
DxLib_Init();
SetDrawScreen(DX_SCREEN_BACK);
SetAlwaysRunFlag(TRUE);
ChangeFont("游明朝");
StateTransition* st;
st = new StateTransition;
st->Transition();
DxLib_End();
return 0;
}