Understanding Keyboard State

Polling for Continuous Interactions

What are some examples of continuous interactions best handled by polling?

Abstract art representing computer programming

Polling is particularly useful for handling continuous interactions where the state of input devices needs to be checked regularly. Here are some common examples of such interactions:

Movement in Games

In many games, character movement is controlled by holding down keys. Polling is ideal for this because you need to continuously check the state of movement keys (e.g., W, A, S, D) to determine the character's movement direction.

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

class Window {
public:
  Window() {
    SDL_Init(SDL_INIT_VIDEO);
    SDLWindow = SDL_CreateWindow(
      "Continuous Movement",
      SDL_WINDOWPOS_CENTERED,
      SDL_WINDOWPOS_CENTERED,
      640, 480, SDL_WINDOW_SHOWN
    );
  }

  ~Window() {
    SDL_DestroyWindow(SDLWindow);
    SDL_Quit();
  }

  void HandleMovement() {
    const Uint8* state = SDL_GetKeyboardState(NULL);
    if (state[SDL_SCANCODE_W]) {
      std::cout << "Moving Up\n";
    }
    if (state[SDL_SCANCODE_A]) {
      std::cout << "Moving Left\n";
    }
    if (state[SDL_SCANCODE_S]) {
      std::cout << "Moving Down\n";
    }
    if (state[SDL_SCANCODE_D]) {
      std::cout << "Moving Right\n";
    }
  }

private:
  SDL_Window* SDLWindow{nullptr};
};

int main(int argc, char* argv[]) {
  Window GameWindow;
  bool running = true;
  SDL_Event event;

  while (running) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        running = false;
      }
    }
    GameWindow.HandleMovement();
    // Rendering code here
  }

  return 0;
}
Moving Up
Moving Left

Camera Control

In 3D applications, camera control is often handled by polling the mouse and keyboard inputs to adjust the camera position and orientation continuously.

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

class Window {
public:
  Window() {
    SDL_Init(SDL_INIT_VIDEO);
    SDLWindow = SDL_CreateWindow(
      "Camera Control",
      SDL_WINDOWPOS_CENTERED,
      SDL_WINDOWPOS_CENTERED,
      640, 480, SDL_WINDOW_SHOWN
    );
  }

  ~Window() {
    SDL_DestroyWindow(SDLWindow);
    SDL_Quit();
  }

  void HandleCamera() {
    int x, y;
    SDL_GetMouseState(&x, &y);
    std::cout << "Mouse Position: ("
      << x << ", " << y << ")\n";
  }

private:
  SDL_Window* SDLWindow{nullptr};
};

int main(int argc, char* argv[]) {
  Window GameWindow;
  bool running = true;
  SDL_Event event;

  while (running) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        running = false;
      }
    }
    GameWindow.HandleCamera();
    // Rendering code here
  }

  return 0;
}
Mouse Position: (320, 240)

Real-time Strategy (RTS) Games

In RTS games, unit selection and map scrolling are often handled continuously. Polling helps to keep track of the mouse and keyboard state to update selections and scroll the map smoothly.

Conclusion

Polling is well-suited for continuous interactions where the input state needs to be checked regularly.

It provides a straightforward way to handle real-time updates, ensuring that your application can respond immediately to user inputs. Examples include character movement, camera control, and interactions in RTS games.

This Question is from the Lesson:

Understanding Keyboard State

Learn how to detect and handle keyboard input in SDL2 using both event-driven and polling methods. This lesson covers obtaining and interpreting the keyboard state array.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Understanding Keyboard State

Learn how to detect and handle keyboard input in SDL2 using both event-driven and polling methods. This lesson covers obtaining and interpreting the keyboard state array.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 36 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved