Building SDL2 from Source (GCC and Make)

SDL2 Event Handling

How can I handle multiple types of events in SDL2, such as keyboard and mouse events?

Vector illustration representing computer programming

SDL2 provides an event system that allows you to handle various types of events, including keyboard and mouse events. Here's an example of how to handle multiple event types in an SDL2Ā application:

#include <SDL.h>

#include <iostream>

int main(int argc, char* argv[]) {
  SDL_Init(SDL_INIT_VIDEO);

  SDL_Window* window{SDL_CreateWindow(
    "Event Handling",
    SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED,
    800, 600, SDL_WINDOW_SHOWN
  )};

  SDL_Event event;
  bool quit = false;
  while (!quit) {
    while (SDL_PollEvent(&event)) {
      switch (event.type) {
        case SDL_QUIT:
          quit = true;
          break;
        case SDL_KEYDOWN:
          if (event.key.keysym.sym
            == SDLK_ESCAPE) {
            quit = true;
          } else {
            std::cout << "Key pressed: "
              << SDL_GetKeyName(
                event.key.keysym.sym) << '\n';
          }
          break;
        case SDL_MOUSEBUTTONDOWN:
          std::cout << "Mouse button pressed: "
            << event.button.button << "\n";
          break;
        case SDL_MOUSEMOTION:
          std::cout << "Mouse position: ("
            << event.motion.x << ", "
            << event.motion.y << ")\n";
          break;
      }
    }
  }

  SDL_DestroyWindow(window);
  SDL_Quit();

  return 0;
}

In this example, we use a switch statement inside the event loop to handle different event types. The event.type field determines the type of event thatĀ occurred.

  • For theĀ SDL_QUITĀ event, we set theĀ quitĀ flag toĀ trueĀ to exit the application.
  • For theĀ SDL_KEYDOWNĀ event, we check if the pressed key is the Escape key ( SDLK_ESCAPE) to quit the application. Otherwise, we print the name of the pressed key usingĀ SDL_GetKeyName.
  • For theĀ SDL_MOUSEBUTTONDOWNĀ event, we print the mouse button that was pressed usingĀ event.button.button.
  • For theĀ SDL_MOUSEMOTIONĀ event, we print the current mouse position usingĀ event.motion.xĀ andĀ event.motion.y.

You can extend this event handling code to include additional event types and perform specific actions based on your application'sĀ requirements.

Remember to include the necessary headers and initialize SDL2 properly before handling events. Also, make sure to clean up resources, such as windows and renderers, before quitting theĀ application.

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

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:

  • 27 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