Advertisement

SDL_KEYDOWN not working

Started by April 20, 2017 01:47 AM
4 comments, last by starfruit64 7 years, 8 months ago

I am trying to make a simple text game, but I have come across an odd problem. I have a class set up to take inputs from the user's keyboard using SDL_KEYDOWN. When ever the function check_event() is called it runs a loop that polls keyboard input and returns a string for the button. The odd thing is that pressing down on the keys have no effect. The code stops for my while loop, but it seems like my function has no effect at all.

Here is my main code:

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

And here is my input class .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";
}

Any help is greatly appreciated

pretty sure you need a window to get events

the console you are printing to is not a "real window" per say....and definitely not the SDL managed one that events come from

Advertisement

hi , I think you should try 2 things :

1 > did you declare event ? or you just declare it in SDL_PollEvent .

2 > you should debug in the SDL_KEYDOWN ( print out something when the SDL_KEYDOWN is called ) .

SDL isn't meant to be used only for input, I guess. SDL is meant to be used a standalone library for input, graphics, audio, etc.

As ncsu121978 says, you need a SDL window for getting SDL input. And even if you create one, SDL would be registering events when on that window not on the console. So, making an invisible window or something like that won't help.

If you just want to make a text based adventure game, and want input functionality for that, you would be better off with something like OIS or Gainput.

#include <iostream>
#include <SDL.h>
#include <gl.h>
#include <iostream>

//Screen dimension constants
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 768;

#define GE_0(function, message)                                                      \
    if ( (function)  < 0) {                                                         \
        std::cout << message << " SDL_Error: " << SDL_GetError() << std::endl;      \
        return -1;                                                                  \
    }

#define NOT_0(value, message)                                                      \
    if ( (value)  == NULL) {                                                         \
        std::cout << message << " SDL_Error: " << SDL_GetError() << std::endl;      \
        return -1;                                                                  \
    }


void draw(SDL_Window    *const window){
    
    // //Fill the surface white
    // /* Clear our buffer with a red background */
    // glClearColor ( color, color, color, 1.0 );

    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   
    glFlush ();
    // glFinish();

    SDL_GL_SwapWindow(window);
}

int init_and_destroy() {

    //Initialize SDL
    GE_0( SDL_Init(SDL_INIT_VIDEO) , "SDL Could not initialize!");

    // https://wiki.libsdl.org/SDL_GL_SetAttribute
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);


    // thanks http://headerphile.com/sdl2/opengl-part-1-sdl-opengl-awesome/
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
    // SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
    SDL_GL_SetSwapInterval(0);


    //Create window
    SDL_Window *const window = SDL_CreateWindow( 
        "SDL Tutorial", 
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 
        SCREEN_WIDTH, SCREEN_HEIGHT, 
        SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
    NOT_0( window, "Window could not be created");

    const SDL_GLContext context = SDL_GL_CreateContext(window);
    NOT_0(context, "OpenGL is not initialised");
    


    std::cout << "int: " << sizeof(int) << " long: " << sizeof(long) << std::endl;
    //Get the surface contained by the window
    // SDL_Surface *const screenSurface = SDL_GetWindowSurface( window );
    // NOT_0 (screenSurface, "The surface can not be retrieved from the window");

    while(1) {

        draw(window);
        // SDL_UpdateWindowSurface( window );

        SDL_Event event;
        while( SDL_PollEvent(&event) ) {
            switch(event.type){
                case SDL_QUIT:
                    std::cout << "SDL_QUIT" << std::endl;
                    goto break_while;
                case SDL_KEYDOWN:
                    std::cout << "KeyDown: " << ((SDL_KeyboardEvent*) &event)->keysym.sym << std::endl;
                    std::cout << "KeyDown: " << event.key.keysym.sym << std::endl;
                    if (event.key.keysym.sym == SDLK_ESCAPE){
                        goto break_while;
                    }
                    break;
            }
        }
    }
    break_while:


    // SDL_Delay(2000);

    SDL_GL_DeleteContext(context);

    //Destroy window
    SDL_DestroyWindow( window );

    //Quit SDL subsystems
    SDL_Quit();
    return 0;
}

int main() {
    return init_and_destroy();
        
    return 0;
}

Hi, @starfruit! This code works and does what you want :-)

SDL isn't meant to be used only for input, I guess. SDL is meant to be used a standalone library for input, graphics, audio, etc.
As ncsu121978 says, you need a SDL window for getting SDL input. And even if you create one, SDL would be registering events when on that window not on the console. So, making an invisible window or something like that won't help.
If you just want to make a text based adventure game, and want input functionality for that, you would be better off with something like OIS or Gainput.


pretty sure you need a window to get events
the console you are printing to is not a "real window" per say....and definitely not the SDL managed one that events come from


Thank you!

This topic is closed to new replies.

Advertisement