Search code examples
c++sdlkeypress

SDL Keypressing


I am trying to make a program that will be able to detect key-presses with SDL.

My current code is a modified version of somebody elses (trying to get it to work before making my own version).

#include "SDL/SDL.h"
#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
    //Start SDL
    if(0 != SDL_Init(SDL_INIT_EVERYTHING)) {
            std::cout << "Well I'm screwed\n";
            return EXIT_FAILURE;
    }
    SDL_Surface* display;
    display = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    SDL_Event event;
    bool running = true;
    std::cout << "Cake"; //Testing output (doesn't work)
    while(running) {
            std::cout << "Pie"; //Again, testing output and again doesn't work
            if(SDL_PollEvent(&event)) { //I have tried this is a while statement
                    switch(event.type) {
                            case SDL_KEYDOWN:
                                    std::cout << "Down\n"; // Have tried "<< std::endl" instead of "\n"
                                    break;
                            case SDL_KEYUP:
                                    std::cout << "Up\n";
                                    break;
                            case SDL_QUIT:
                                    running = false;
                                    break;
                            default:
                                    break;
                    }
            }
    }
    //Quit SDL
    SDL_Quit();

    return 0;
}

This code is supposed to detect any key-down/up and output it, but it doesn't output anything.

My ultimate goal is to make it detect the konami code and then do something.

I constantly update the code above making it identical to the one I am using (except with added comments of what people have suggested).

Also if it helps: g++ -o myprogram.exe mysource.cpp -lmingw32 -lSDLmain -lSDL is the command I am using to compile. (If you didn't figure it out from the command, I am running windows (7).) No errors occur when compiling

I am getting now output whatsoever, which leads me to believe that my probs has nothing to do with the key-checking; however there is a chance that is incorrect.


Solution

  • SDL By default redirects output to stdout.txt