私は今、DXライブラリで簡単な2Dシューティングゲームを制作しています。
以前はC言語でゲームを制作していたのですが、今回は勉強も兼ねてC++を使って制作しています。
http://dixq.net/g/sp_06.html
こちらのサイトを参考にプログラムを組んでいたのですが、少し分からないことが出てきました。
// BaseScene.h
#pragma once
#include "Task.h"
#include "Pad.h"
#include "ISceneChanger.h"
// シーンの基底クラス
class BaseScene : public Task
{
protected:
Pad p1_pad, p2_pad; // パッドのインスタンスを生成
ISceneChanger *mSceneChanger; // クラス所有元にシーン切り替えを伝えるインターフェース
public:
BaseScene(ISceneChanger *changer);
virtual ~BaseScene() {}
virtual void Initialize() override {} // 初期化処理をオーバーライド
virtual void Finalize() override; // 終了処理をオーバーライド
virtual void Update() override {} // 更新処理をオーバーライド
virtual void Draw() override; // 描画処理をオーバーライド
};
// Game.cpp
#include "..\Header\Game.h"
#include "DxLib.h"
Game::Game(ISceneChanger *changer) : BaseScene(changer)
{}
// 初期化
void Game::Initialize()
{
p1_pad.ChangePad(DX_INPUT_PAD1, FALSE); // パッド1のDirectInput情報を取得
p2_pad.ChangePad(DX_INPUT_PAD2, FALSE); // パッド2のDirectInput情報を取得
}
// 更新
void Game::Update()
{
p1_pad.Update(); // 更新
p2_pad.Update();
if (CheckHitKey(KEY_INPUT_RETURN) == 1)
mSceneChanger->ChangeScene(eScene_Menu); // シーンの切り替え
}
// 描画
void Game::Draw()
{
p1_pad.Draw(); // 描画
DrawString(480, 240, _T("eScene_Game"), GetColor(0xFF, 0xFF, 0x00));
}
// Pad.h
#pragma once
#include "Task.h"
#include "DxLib.h"
class Pad : public Task
{
protected:
DINPUT_JOYSTATE dinput; // DirectInputの入力データを格納する構造体
XINPUT_STATE xinput; // XInputの入力データを格納する構造体
int num; // 情報を取得するパッドの番号
bool xflag; // XInputを使用するか判定
public:
void ChangePad(int pad_no, bool xinput_flag); // 取得するパッドの番号を変更する
virtual void Initialize() override; // 初期化処理をオーバーライド
//virtual void Finalize() override; // 終了処理をオーバーライド
virtual void Update() override; // 更新処理をオーバーライド
virtual void Draw() override; // 描画処理をオーバーライド
};
// Pad.cpp
#include "..\Header\Pad.h"
void Pad::ChangePad(int pad_no, bool xinput_flag) // 入力を受け取るパッドの番号を変更
{
num = pad_no; // パッドの番号
xflag = xinput_flag; // XInputを使用するか判定
}
void Pad::Initialize()
{}
void Pad::Update()
{
xflag ?
GetJoypadXInputState(num, &xinput) :
GetJoypadDirectInputState(num, &dinput);
}
void Pad::Draw()
{
// テスト用
if (xflag) // XInputの情報を表示
{
DrawFormatString(0, 0, GetColor(0xFF, 0xFF, 0xFF), _T("Pad %-2d XInput Mode"), num);
DrawFormatString(0, 16 * 2, GetColor(0xFF, 0xFF, 0xFF), _T("D-Pad Up :%d"), xinput.Buttons[XINPUT_BUTTON_DPAD_UP]);
DrawFormatString(0, 16 * 3, GetColor(0xFF, 0xFF, 0xFF), _T("D-Pad Down :%d"), xinput.Buttons[XINPUT_BUTTON_DPAD_DOWN]);
DrawFormatString(0, 16 * 4, GetColor(0xFF, 0xFF, 0xFF), _T("D-Pad Left :%d"), xinput.Buttons[XINPUT_BUTTON_DPAD_LEFT]);
DrawFormatString(0, 16 * 5, GetColor(0xFF, 0xFF, 0xFF), _T("D-Pad Right:%d"), xinput.Buttons[XINPUT_BUTTON_DPAD_RIGHT]);
DrawFormatString(0, 16 * 7, GetColor(0xFF, 0xFF, 0xFF), _T(" Start :%d"), xinput.Buttons[XINPUT_BUTTON_START]);
DrawFormatString(0, 16 * 8, GetColor(0xFF, 0xFF, 0xFF), _T(" Back :%d"), xinput.Buttons[XINPUT_BUTTON_BACK]);
DrawFormatString(0, 16 * 10, GetColor(0xFF, 0xFF, 0xFF), _T("L Thumb:%d"), xinput.Buttons[XINPUT_BUTTON_LEFT_THUMB]);
DrawFormatString(0, 16 * 11, GetColor(0xFF, 0xFF, 0xFF), _T("R Thumb:%d"), xinput.Buttons[XINPUT_BUTTON_RIGHT_THUMB]);
DrawFormatString(150, 16 * 2, GetColor(0xFF, 0xFF, 0xFF), _T("LB:%d"), xinput.Buttons[XINPUT_BUTTON_LEFT_SHOULDER]);
DrawFormatString(150, 16 * 3, GetColor(0xFF, 0xFF, 0xFF), _T("RB:%d"), xinput.Buttons[XINPUT_BUTTON_RIGHT_SHOULDER]);
DrawFormatString(150, 16 * 5, GetColor(0xFF, 0xFF, 0xFF), _T("A :%d"), xinput.Buttons[XINPUT_BUTTON_A]);
DrawFormatString(150, 16 * 6, GetColor(0xFF, 0xFF, 0xFF), _T("B :%d"), xinput.Buttons[XINPUT_BUTTON_B]);
DrawFormatString(150, 16 * 7, GetColor(0xFF, 0xFF, 0xFF), _T("X :%d"), xinput.Buttons[XINPUT_BUTTON_X]);
DrawFormatString(150, 16 * 8, GetColor(0xFF, 0xFF, 0xFF), _T("Y :%d"), xinput.Buttons[XINPUT_BUTTON_Y]);
DrawFormatString(150, 16 * 10, GetColor(0xFF, 0xFF, 0xFF), _T("LT:%3d"), xinput.LeftTrigger);
DrawFormatString(150, 16 * 11, GetColor(0xFF, 0xFF, 0xFF), _T("RT:%3d"), xinput.RightTrigger);
DrawFormatString(300, 16 * 2, GetColor(0xFF, 0xFF, 0xFF), _T("L-Stick X:%6d"), xinput.ThumbLX);
DrawFormatString(300, 16 * 3, GetColor(0xFF, 0xFF, 0xFF), _T("L-Stick Y:%6d"), xinput.ThumbLY);
DrawFormatString(300, 16 * 5, GetColor(0xFF, 0xFF, 0xFF), _T("R-Stick X:%6d"), xinput.ThumbRX);
DrawFormatString(300, 16 * 6, GetColor(0xFF, 0xFF, 0xFF), _T("R-Stick Y:%6d"), xinput.ThumbRY);
}
else // DirectInputの情報を表示
{
DrawFormatString(0, 0, GetColor(0xFF, 0xFF, 0xFF), _T("Pad %-2d DirectInput Mode"), num);
for (int i = 0; i < 16; i++)
{
DrawFormatString(0, 16 * (i + 2), GetColor(0xFF, 0xFF, 0xFF), _T("Button %2d:%3d"), i + 1, dinput.Buttons[i]);
DrawFormatString(150, 16 * (i + 2), GetColor(0xFF, 0xFF, 0xFF), _T("Button %2d:%3d"), i + 17, dinput.Buttons[i + 16]);
}
DrawFormatString(300, 16 * 2, GetColor(0xFF, 0xFF, 0xFF), _T("X :%5d"), dinput.X);
DrawFormatString(300, 16 * 3, GetColor(0xFF, 0xFF, 0xFF), _T("Y :%5d"), dinput.Y);
DrawFormatString(300, 16 * 4, GetColor(0xFF, 0xFF, 0xFF), _T("Z :%5d"), dinput.Z);
DrawFormatString(300, 16 * 5, GetColor(0xFF, 0xFF, 0xFF), _T("Rx:%5d"), dinput.Rx);
DrawFormatString(300, 16 * 6, GetColor(0xFF, 0xFF, 0xFF), _T("Ry:%5d"), dinput.Ry);
DrawFormatString(300, 16 * 7, GetColor(0xFF, 0xFF, 0xFF), _T("Rz:%5d"), dinput.Rz);
for (int i = 0; i < 4; i++)
{
DrawFormatString(300, 16 * (i + 9), GetColor(0xFF, 0xFF, 0xFF), _T("POV %d:%5d"), i + 1, dinput.POV[i]);
}
for (int i = 0; i < 2; i++)
{
DrawFormatString(300, 16 * (i + 14), GetColor(0xFF, 0xFF, 0xFF), _T("Slider %d:%3d"), i + 1, dinput.Slider[i]);
}
}
}
シーンを切り替えても同じパッドの入力を受け取るようにしたいのですが、どのように組めばいいか分からず困っています。
まだまだ経験不足の私ですが、どうかご教示願います。