Every video game needs to get input from the player in order to respond to his or her actions. Without input, there would be no interaction with the user and it would not be a game. This tutorial will teach you a simple method for getting keyboard input from the player by using the SDL library.
We are going to use a technique called polling instead of the event-driven approach. Think of polling like taking a survey. Every frame of our game simulation, we survey which keys are being pressed at that particular time. This is the simplest way to get keyboard input into our application. SDL provides us with a very nice function for this. Here it is:
Uint8 *SDL_GetKeyState(int *numkeys);
This function returns a pointer to an array that holds the state of every key on the keyboard. If a key is being pressed, its value will be one and if not its value will be 0. SDL defines constants to access this array. For example, the space key is defined as SDLK_SPACE. A full list of these constants can be found here. Lets look at the code for this tutorial to see how this all works. I am going to assume at this point that you know how to set up an SDL project in your compiler/platform.
#include <iostream> #include <SDL.h> #define MOVE_SPEED 100.0f using std::cerr; using std::endl; int main(int argc, char* args[]) { // Initialize the SDL if (SDL_Init(SDL_INIT_VIDEO) != 0) { cerr << "SDL_Init() Failed: " << SDL_GetError() << endl; exit(1); } // Set the video mode SDL_Surface* display; display = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); if (display == NULL) { cerr << "SDL_SetVideoMode() Failed: " << SDL_GetError() << endl; exit(1); } // Set the title bar SDL_WM_SetCaption("SDL Tutorial", "SDL Tutorial"); // Gain access to keystate array Uint8 *keys = SDL_GetKeyState(NULL); // Timing variables Uint32 old_time, current_time; float ftime; // Need to initialize this here for event loop to work current_time = SDL_GetTicks(); SDL_Event event; // Box float x = 35.0f, y = 35.0f; SDL_Rect rect; rect.h = 30; rect.w = 30; // Main loop while(1) { // Update the timing information old_time = current_time; current_time = SDL_GetTicks(); ftime = (current_time - old_time) / 1000.0f; // Check for messages if (SDL_PollEvent(&event)) { // Check for the quit message if (event.type == SDL_QUIT) { // Quit the program break; } } // Handle input if (keys[SDLK_LEFT]) x -= MOVE_SPEED * ftime; if (keys[SDLK_RIGHT]) x += MOVE_SPEED * ftime; if (keys[SDLK_DOWN]) y += MOVE_SPEED * ftime; if (keys[SDLK_UP]) y -= MOVE_SPEED * ftime; // Clear the screen if (SDL_FillRect(display, NULL, SDL_MapRGB( display->format, 0,0,0)) != 0) { cerr << "SDL_FillRect() Failed: " << SDL_GetError() << endl; break; } // Draw box rect.x = static_cast<int>(x); rect.y = static_cast<int>(y); if (SDL_FillRect(display, &rect, SDL_MapRGB(display->format, 255,255,255)) != 0) { cerr << "SDL_FillRect() Failed: " << SDL_GetError() << endl; break; } //Update the display SDL_Flip(display); } // Tell the SDL to clean up and shut down SDL_Quit(); return 0; }
After you have copied the source code into your program be sure to save your progress. Go ahead and compile and run the program. You should see a white box on the screen. Press the arrow keys on your keyboard to move this box around. As you can see, it is incredibly easy to get keyboard input into your program by using the SDL. In the next tutorial, you will see how to get keyboard input using the event-driven approach.
See Also:
Back to SDL Tutorial Index
Back to Main Page