Handling Mouse Input

Calculate Mouse Position

How can I calculate the distance of the mouse cursor from the edges of the window?

Abstract art representing computer programming

Calculating the distance of the mouse cursor from the edges of the SDL window is straightforward. You need to know the current position of the mouse cursor and the dimensions of the window.

Step-by-Step Guide

  1. Get Mouse Position: Capture the current position of the mouse cursor using SDL events.
  2. Get Window Dimensions: Retrieve the dimensions of the SDL window.
  3. Calculate Distances: Use basic arithmetic to calculate the distances from each edge of the window.

Example Code

Here’s a complete example demonstrating how to calculate the distance of the mouse cursor from the edges of the window:

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

void CalculateMouseDistances(
  int mouseX, int mouseY,
  int windowWidth, int windowHeight
) {
  int distanceFromLeft = mouseX;
  int distanceFromTop = mouseY;
  int distanceFromRight = windowWidth - mouseX;
  int distanceFromBottom = windowHeight - mouseY;

  std::cout << "Distance from left: "
    << distanceFromLeft << '\n';      
  std::cout << "Distance from top: "
    << distanceFromTop << '\n';        
  std::cout << "Distance from right: "
    << distanceFromRight << '\n';    
  std::cout << "Distance from bottom: "
    << distanceFromBottom << '\n';  
}

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 Distance Calculation",
    100, 100, 640, 480, SDL_WINDOW_SHOWN);
  if (window == nullptr) {
    std::cerr << "SDL_CreateWindow Error: "
      << SDL_GetError() << '\n';
    SDL_Quit();
    return 1;
  }

  int windowWidth, windowHeight;
  SDL_GetWindowSize(
    window, &windowWidth, &windowHeight);

  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) {
        int mouseX = event.motion.x;
        int mouseY = event.motion.y;
        CalculateMouseDistances(
          mouseX, mouseY,
          windowWidth, windowHeight
        );
      }
    }
  }

  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}
Distance from left: 100
Distance from top: 150
Distance from right: 540
Distance from bottom: 330

Explanation

  • Initialization: SDL is initialized, and a window is created.
  • Window Size: SDL_GetWindowSize() retrieves the width and height of the window.
  • Event Loop: The event loop captures SDL_MOUSEMOTION events to get the current position of the mouse cursor.
  • Distance Calculation: The CalculateMouseDistances() function computes the distances from the mouse cursor to each edge of the window using simple arithmetic.

Calculations

  • Distance from Left: mouseX
  • Distance from Top: mouseY
  • Distance from Right: windowWidth - mouseX
  • Distance from Bottom: windowHeight - mouseY

Summary

By combining the current mouse position with the window dimensions, you can easily calculate the distance from the mouse cursor to any edge of the window.

This technique is useful for implementing features like edge scrolling or snapping UI elements to window edges.

This Question is from the Lesson:

Handling Mouse Input

Learn how to detect and handle mouse input events in SDL, including mouse motion, button clicks, and window entry/exit.

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

This Question is from the Lesson:

Handling Mouse Input

Learn how to detect and handle mouse input events in SDL, including mouse motion, button clicks, and window entry/exit.

3D art representing computer programming
Part of the course:

Building Minesweeper with C++ and SDL2

Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library

Free, unlimited access

This course includes:

  • 37 Lessons
  • 100+ Code Samples
  • 92% 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