SDL Mouse Motion Events

What is the difference between SDL_MOUSEMOTION and SDL_MouseMotionEvent?

SDL_MOUSEMOTION and SDL_MouseMotionEvent are both related to mouse movement in SDL, but they serve different purposes in the event handling system.

SDL_MOUSEMOTION

SDL_MOUSEMOTION is an event type constant used to identify mouse motion events. When the mouse is moved, SDL generates an event with this type.

You check for this event type in your event loop to determine if the user has moved the mouse.

Here's an example of how to use SDL_MOUSEMOTION in an event loop:

SDL_Event event;
while (SDL_PollEvent(&event)) {
  if (event.type == SDL_MOUSEMOTION) {
    std::cout << "Mouse moved to - x: "
      << event.motion.x << ", y: "
      << event.motion.y << '\n';  
  }
}

SDL_MouseMotionEvent

SDL_MouseMotionEvent is a structure that contains detailed information about a mouse motion event.

When SDL_PollEvent() updates the SDL_Event structure with a mouse motion event, the motion member of the SDL_Event structure is an SDL_MouseMotionEvent.

Here's an example structure:

typedef struct SDL_MouseMotionEvent {
  Uint32 type;      // Event type, SDL_MOUSEMOTION
  Uint32 timestamp; // Timestamp of the event
  Uint32 windowID;  // The window with mouse focus
  Uint32 which;     // The mouse instance ID
  Uint32 state;     // The current button state
  Sint32 x;         // X coordinate of the mouse
  Sint32 y;         // Y coordinate of the mouse
  Sint32 xrel;      // Relative motion in the X direction
  Sint32 yrel;      // Relative motion in the Y direction
} SDL_MouseMotionEvent;

Differences and Usage

  • Event Type: SDL_MOUSEMOTION is an event type constant, while SDL_MouseMotionEvent is a structure containing detailed information about the mouse motion event.
  • Detection: Use SDL_MOUSEMOTION to detect a mouse motion event in the event loop. Once detected, you can access the motion member of the SDL_Event structure to get details.
  • Details: SDL_MouseMotionEvent provides detailed information such as the cursor's new position (x, y), relative movement (xrel, yrel), and the state of mouse buttons during the movement.

Here's a full example demonstrating how to use both:

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

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(
    "Mouse Motion",
    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;
  bool running = true;
  while (running) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        running = false;
      } else if (event.type == SDL_MOUSEMOTION) {
        SDL_MouseMotionEvent& motion = event.motion;

        std::cout << "Mouse moved to - x: "
          << motion.x << ", y: " << motion.y << '\n';  

        std::cout << "Relative movement - xrel: "
          << motion.xrel << ", yrel: "
          << motion.yrel << '\n';  
      }
    }
  }

  SDL_DestroyWindow(window);
  SDL_Quit();
  
  return 0;
}
Mouse moved to - x: 150, y: 200
Relative movement - xrel: 5, yrel: 5

In summary, use SDL_MOUSEMOTION to detect mouse movement events and SDL_MouseMotionEvent to access detailed information about the motion.

Mouse Input Basics

Discover how to process mouse input, including position tracking and button presses

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

SDL Coordinate System: Why Y-Down?
Why does SDL use a y-down coordinate system instead of y-up?
SDL: Get Mouse Screen Coordinates
Can I get mouse coordinates relative to the screen instead of the window?
SDL_PollEvent() vs SDL_WaitEvent()
What's the difference between SDL_PollEvent() and SDL_WaitEvent()?
SDL Event Timestamp Field
What does the timestamp field in the event structures mean?
SDL Event which Field
What does the which field in the event structures refer to (e.g., multiple mice)?
SDL: Custom Mouse Cursor Appearance
Can I change the mouse cursor's appearance (e.g., to a crosshair)?
SDL Double Click Timing Customization
How does SDL determine the time window for registering a double click? Can I customize this timing?
Retrieve Mouse Position
How do I retrieve the current position of the mouse cursor in SDL?
Detect Double Clicks in SDL
How can I detect double clicks using SDL?
SDL_MouseButtonEvent Structure
What is the SDL_MouseButtonEvent structure used for?
Calculate Mouse Position
How can I calculate the distance of the mouse cursor from the edges of the window?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant