2017-04-19 1 views
-1

간단한 텍스트 게임을 만들려고 노력하고 있지만 이상한 문제가 발생했습니다. 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; 
} 

그리고

#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"; 
    } 

이 어떤 도움을 주시면 더 좋구요 통화 당 내 입력 클래스는 다음과 같습니다

여기 내 주요 코드입니다.

+1

당신은 창을 만든 것 같지 않습니다. SDL은 (많은 이유로, 문제가 있거나 불가능할 수도있는) 전체 시스템 입력 잡기 기능을 가지고 있지 않습니다. 텍스트 게임 키 누르기의 경우 stdin (read, getch, ...)에서 읽어야합니다. – keltar

답변

0

여러 개의 빨간색 플래그가 있습니다.

우선 과 함께 std::cout이 작동하지 않습니다. 라이브러리는 콘솔/터미널이 아닌 창을 사용합니다. 텍스트를 렌더링하려면 적절한 자습서를 읽으십시오.

둘째, 이벤트 처리기를 초기화하지 않은 경우 이벤트를 확인할 수 없습니다. 루프 전에 SDL_Event event;을 추가해야합니다.

셋째하는 입력을 처리하기 위해 사용이 더 적합, 불필요 :

bool quit = false; 
SDL_Event event; 

while (!quit) 
{ 
    while (SDL_PollEvent(&event) != 0) 
    { 
     if (event.type == SDL_QUIT) 
     { 
      quit = true; 
     } 

     // Add if blocks, switch statements, and what have you 
    } 
}