Handling Mouse Scrolling

Implement Zoom with Mouse Scroll

How can I implement zoom functionality using the mouse scroll wheel in SDL?

Abstract art representing computer programming

Implementing zoom functionality using the mouse scroll wheel in SDL involves detecting the scroll events and adjusting the zoom level of your application accordingly.

Here’s an example application that keeps track of a zoom level:

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

int main(int argc, char* argv[]) {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Window* window = SDL_CreateWindow(
    "Zoom Functionality Example",
    SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED,
    800, 600,
    SDL_WINDOW_SHOWN
  );

  SDL_Event event;
  bool running = true;
  float zoomLevel = 1.0f; 

  while (running) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_MOUSEWHEEL) {
        if (event.wheel.y > 0) {
          zoomLevel += 0.1f; 
        } else if (event.wheel.y < 0) {
          zoomLevel -= 0.1f; 
        }
        std::cout << "Zoom level: "
          << zoomLevel << '\n'; 
      }
      if (event.type == SDL_QUIT) {
        running = false;
      }
    }
  }

  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}
Zoom level: 1.1
Zoom level: 1.0

In the event loop, check for SDL_MOUSEWHEEL events and adjust the zoomLevel variable based on the y value of event.wheel. Positive values increase the zoom level, and negative values decrease it.

Once you have adjusted the zoomLevel, you need to apply it to your rendering logic. This can be done by scaling your rendering based on the zoomLevel.

Here’s an example of how to integrate zooming into your rendering:

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

void Render(
  SDL_Renderer* renderer, float zoomLevel) {
  SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  SDL_RenderClear(renderer);

  SDL_SetRenderDrawColor(
    renderer, 255, 255, 255, 255);
  int width = static_cast<int>(50 * zoomLevel); 
  int height = static_cast<int>(50 * zoomLevel); 
  SDL_Rect rect = {375, 275, width, height};
  SDL_RenderFillRect(renderer, &rect);

  SDL_RenderPresent(renderer);
}

int main(int argc, char* argv[]) {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Window* window = SDL_CreateWindow(
    "Zoom Functionality Example",
    SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED,
    800, 600,
    SDL_WINDOW_SHOWN
  );
  SDL_Renderer* renderer = SDL_CreateRenderer(
    window, -1, 0);

  SDL_Event event;
  bool running = true;
  float zoomLevel = 1.0f;

  while (running) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_MOUSEWHEEL) {
        if (event.wheel.y > 0) {
          zoomLevel += 0.1f;
        } else if (event.wheel.y < 0) {
          zoomLevel -= 0.1f;
        }
        std::cout << "Zoom level: "
          << zoomLevel << '\n';
      }
      if (event.type == SDL_QUIT) {
        running = false;
      }
    }
    Render(renderer, zoomLevel);
  }

  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}
Zoom level: 1.1
Zoom level: 1.0

By following these steps, you can implement zoom functionality in your SDL application using the mouse scroll wheel.

Adjust the zoomLevel based on scroll input and apply it to your rendering logic to achieve the desired zoom effect.

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:

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