Reputation: 686
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.
Upvotes: 0
Views: 490
Reputation: 34
hey there i think you should go to your "project properties" then "linker settings" and "Subsystem" then choose "Console (/SUBSYSTEM:CONSOLE)" Otherwise you can't see what you typed in cout and in visual studio you cant use
#include"SDL/SDL.h"
you should type #include<SDL.h>
Upvotes: 0
Reputation: 52165
SDL needs a window to receive events.
Uncomment your SDL_SetVideoMode()
call:
#include <SDL/SDL.h>
#include <iostream>
using namespace std;
int main( int argc, char* argv[] )
{
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;
while(running)
{
if(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
std::cout << "Down" << endl;
break;
case SDL_KEYUP:
std::cout << "Up" << endl;
break;
case SDL_QUIT:
running = false;
break;
default:
break;
}
}
}
SDL_Quit();
return 0;
}
Upvotes: 3
Reputation: 4283
You should query for all the SDL events in the loop, not just the first one. Try this one to check all the events:
while( SDL_PollEvent( &event ) ){
...
}
also you can try to update the screen in the loop with:
SDL_Flip( display );
Upvotes: 0