Handling Mouse Scrolling

SDL_MOUSEWHEEL vs SDL_MouseWheelEvent

What is the difference between SDL_MOUSEWHEEL and SDL_MouseWheelEvent?

Abstract art representing computer programming

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 the wheel member of SDL_Event.

Using both together allows you to detect and handle mouse wheel events efficiently in SDL.

This Question is from the Lesson:

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.

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

This Question is from the Lesson:

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.

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:

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