シンプルなテキストゲームを作成しようとしていますが、奇妙な問題が発生しました。 SDL_KEYDOWNを使用してユーザーのキーボードから入力を受け取るようにクラスを設定しました。関数check_event()
が呼び出されると、キーボード入力をポーリングしてボタンの文字列を返すループを実行します。奇妙なことは、キーを押しても効果がないということです。コードは私のwhileループで停止しますが、私の関数はまったく効果がないようです。ここでSDLキーボード入力がトリガーされない
は私のメインのコードです:
#include <iostream>
#include <fstream>
#include <SDL.h>
#include "SDL_ttf.h"
#include "input.h"
using namespace std;
Input input;
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return 1;
}
cout << "Welcome to Hero V1.0!" << endl; //intro stuff
cout << "Written By: Jojo" << endl;
cout << endl;
cout << "1) New Game" << endl;
cout << "2) Continue Game" << endl;
while (true) {
string event = input.check_event();
if(event == "1"){
cout << "Test" << flush;
}
}
SDL_Quit();
return 0;
}
そしてここでは、私の入力クラスの.cpp
#include <iostream>
#include <SDL.h>
#include "Input.h"
using namespace std;
string Input::check_event() {
while (SDL_PollEvent(&event)) {
if(event.type == SDL_KEYDOWN){
switch(event.key.keysym.sym){
case SDLK_1:
return "1";
}
}
}
return "null";
}
あるすべてのヘルプは大歓迎です。
あなたは、ウィンドウを作成しているようには見えません。 SDLには、フルシステムの入力グラブ機能がありません(多くの理由から、問題があるかもしれないし、実装が不可能かもしれません)。テキストゲームのキー入力では、stdin(read、getch、...)から読み込む必要があります。 – keltar