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, whileSDL_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 themotion
member of theSDL_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