Mouse Capture and Global Mouse State

Relative vs Global Mouse Coordinates

What's the difference between relative and global mouse coordinates? When should I use each?

Abstract art representing computer programming

SDL provides two main ways to get mouse coordinates through SDL_GetMouseState() and SDL_GetGlobalMouseState(). The key difference lies in their frame of reference and typical use cases.

Relative Coordinates

Relative coordinates are reported by SDL_GetMouseState() and are measured relative to the top-left corner of your window. These coordinates start at (0,0) at your window's top-left corner and increase as you move right and down. They're most useful when you need to:

  • Handle mouse interactions within your window's UI elements
  • Implement window-specific features like buttons or drag handles
  • Calculate if the mouse is within specific regions of your window

Here's an example showing how to use relative coordinates:

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

int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Window* window =
    SDL_CreateWindow("Relative Coordinates",
                     SDL_WINDOWPOS_CENTERED,
                     SDL_WINDOWPOS_CENTERED,
                     800, 600,
                     SDL_WINDOW_SHOWN);

  bool running = true;
  while (running) {
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        running = false;
      }
    }

    int x, y;
    SDL_GetMouseState(&x, &y); 

    // Check if mouse is in top-left quadrant
    if (x < 400 && y < 300) {
      std::cout <<
        "Mouse in top-left quadrant at "
        << x << "," << y << "\n";
    }
  }

  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}

Global Coordinates

Global coordinates are reported by SDL_GetGlobalMouseState() and are measured relative to the top-left corner of the primary display. These coordinates can be negative in multi-monitor setups. They're most useful when you need to:

  • Track mouse movement across multiple windows
  • Implement drag-and-drop between windows
  • Handle multi-monitor scenarios
  • Position windows relative to the mouse cursor

Here's an example demonstrating global coordinates:

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

int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_VIDEO);

  // Create two windows side by side
  SDL_Window* window1 =
    SDL_CreateWindow("Window 1", 100, 100, 400,
                     300, SDL_WINDOW_SHOWN);
  SDL_Window* window2 =
    SDL_CreateWindow("Window 2", 550, 100, 400,
                     300, SDL_WINDOW_SHOWN);

  bool running = true;
  while (running) {
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        running = false;
      }
    }

    int globalX, globalY;
    SDL_GetGlobalMouseState(&globalX, &globalY);

    std::cout << "Mouse at global position: "
      << globalX << "," << globalY << "\n";
  }

  SDL_DestroyWindow(window1);
  SDL_DestroyWindow(window2);
  SDL_Quit();
  return 0;
}

When choosing between relative and global coordinates, consider your specific needs:

  • Use relative coordinates when working within a single window
  • Use global coordinates when dealing with multi-window or multi-monitor scenarios
  • Consider using both if you need to implement features like drag-and-drop between windows
This Question is from the Lesson:

Mouse Capture and Global Mouse State

Learn how to track mouse movements and button states across your entire application, even when the mouse leaves your window.

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

This Question is from the Lesson:

Mouse Capture and Global Mouse State

Learn how to track mouse movements and button states across your entire application, even when the mouse leaves your window.

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:

  • 75 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 © 2025 - All Rights Reserved