SDL_MOUSEWHEEL
vs SDL_MouseWheelEvent
What is the difference between SDL_MOUSEWHEEL
and SDL_MouseWheelEvent
?
The SDL_MOUSEWHEEL
and SDL_MouseWheelEvent
are both related to handling mouse scroll wheel events in SDL, but they serve different purposes.
SDL_MOUSEWHEEL
is a constant that represents the type of event generated when the mouse wheel is scrolled. It is used to identify mouse wheel events within the SDL event loop.
When SDL detects that the mouse wheel has been moved, it generates an event of type SDL_MOUSEWHEEL
.
Here's an example of using SDL_MOUSEWHEEL
in an event loop:
#include <SDL.h>
#include <iostream>
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow(
"Mouse Wheel Event Example",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
800, 600,
SDL_WINDOW_SHOWN
);
SDL_Event event;
bool running = true;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_MOUSEWHEEL) {
std::cout << "Mouse wheel scrolled\n";
}
if (event.type == SDL_QUIT) {
running = false;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Mouse wheel scrolled
On the other hand, SDL_MouseWheelEvent
is a structure that contains detailed information about a mouse wheel event. This structure is a member of the SDL_Event
union, specifically used when the event type is SDL_MOUSEWHEEL
.
The SDL_MouseWheelEvent
provides additional data such as the amount of scroll and the direction. Here's how you can use SDL_MouseWheelEvent
:
#include <SDL.h>
#include <iostream>
void HandleMouseWheel(SDL_MouseWheelEvent& wheel) {
std::cout << "Scroll amount: " << wheel.y << '\n';
}
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow(
"Mouse Wheel Event Example",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
800, 600,
SDL_WINDOW_SHOWN
);
SDL_Event event;
bool running = true;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_MOUSEWHEEL) {
HandleMouseWheel(event.wheel);
}
if (event.type == SDL_QUIT) {
running = false;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Scroll amount: 1
In summary:
SDL_MOUSEWHEEL
is the event type constant used to identify mouse wheel events.SDL_MouseWheelEvent
is a structure that contains detailed information about the mouse wheel event, accessible via thewheel
member ofSDL_Event
.
Using both together allows you to detect and handle mouse wheel events efficiently in SDL.
Handling Mouse Scrolling
Learn how to detect and handle mouse scroll wheel events in SDL2, including vertical and horizontal scrolling, as well as scroll wheel button events.