Handling Mouse Input

Store Mouse Motion Events

How do I store and transfer mouse motion events in SDL?

Abstract art representing computer programming

Storing and transferring mouse motion events in SDL can be useful for handling complex input scenarios.

You can achieve this by using the SDL_MouseMotionEvent structure to capture the details of the event and then store it in a suitable data structure for later use.

Capturing Mouse Motion Events

First, you need to capture the mouse motion events in your event loop. The SDL_MOUSEMOTION event provides the details of the mouse motion in the motion member of the SDL_Event structure.

Example Code

Here’s an example demonstrating how to capture, store, and later use mouse motion events:

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

struct MouseMotion {
  int x;
  int y;
  int xrel;
  int yrel;
};

void HandleMouseMotion(
  const SDL_MouseMotionEvent& motionEvent,
  std::vector<MouseMotion>& motions
) {
  MouseMotion motion{
    motionEvent.x, motionEvent.y,
    motionEvent.xrel, motionEvent.yrel
  };
  motions.push_back(motion);
}

void ProcessStoredMotions(
  const std::vector<MouseMotion>& motions) {
  for (const auto& motion : motions) {
    std::cout << "Stored motion "
      << "- x: " << motion.x
      << ", y: " << motion.y
      << ", xrel: " << motion.xrel
      << ", yrel: " << motion.yrel << '\n';
  }
}

int main(int argc, char* argv[]) {
  if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    std::cerr << "SDL_Init Error: "
      << SDL_GetError() << '\n';
    return 1;
  }

  SDL_Window* window = SDL_CreateWindow(
    "Store Mouse Motion Events",
    100, 100, 640, 480,
    SDL_WINDOW_SHOWN
  );

  if (window == nullptr) {
    std::cerr << "SDL_CreateWindow Error: "
      << SDL_GetError() << '\n';
    SDL_Quit();
    return 1;
  }

  SDL_Event event;
  std::vector<MouseMotion> mouseMotions;
  bool running = true;
  while (running) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        running = false;
      } else if (event.type == SDL_MOUSEMOTION) {
        HandleMouseMotion(
          event.motion, mouseMotions);
      }
    }
  }

  ProcessStoredMotions(mouseMotions);

  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}
Stored motion - x: 150, y: 200, xrel: 5, yrel: 5
Stored motion - x: 155, y: 205, xrel: 5, yrel: 5

Explanation

  • Initialization: SDL is initialized, and a window is created.
  • Event Loop: The event loop captures SDL_MOUSEMOTION events and calls the HandleMouseMotion() function to store the event details.
  • Storage: The MouseMotion structure is used to store the relevant details of the motion event (x, y, xrel, yrel). These structures are stored in a std::vector.
  • Processing Stored Events: After exiting the event loop, the stored mouse motion events are processed in the ProcessStoredMotions() function.

Advantages

  • Organized Storage: Using a std::vector allows for easy storage and retrieval of mouse motion events.
  • Flexibility: You can process stored events at a later time, making it easier to handle complex input scenarios.
  • Detailed Information: Storing the x, y, xrel, and yrel values provides comprehensive details about each motion event.

Summary

Storing and transferring mouse motion events in SDL involves capturing the events in the event loop, storing the details in a suitable data structure like std::vector, and processing them as needed.

This approach is useful for scenarios where you need to handle mouse input more flexibly and in an organized manner.

This Question is from the Lesson:

Handling Mouse Input

Learn how to detect and handle mouse input events in SDL, including mouse motion, button clicks, and window entry/exit.

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

This Question is from the Lesson:

Handling Mouse Input

Learn how to detect and handle mouse input events in SDL, including mouse motion, button clicks, and window entry/exit.

3D art representing computer programming
Part of the course:

Building Minesweeper with C++ and SDL2

Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library

Free, unlimited access

This course includes:

  • 37 Lessons
  • 100+ Code Samples
  • 92% 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