Mouse State

Events vs State for Mouse Input

What's the difference between using events and using state checking for mouse position?

Abstract art representing computer programming

The choice between using events and state checking for mouse input depends on your specific needs. Let's explore the key differences.

Event-Based Approach

An event-based approach might look something like this:

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

void HandleMouseEvent(SDL_Event& E) {
  if (E.type == SDL_MOUSEMOTION) {
    std::cout << "\nMouse moved to: ("
      << E.motion.x << ", " << E.motion.y;
  } else if (E.type == SDL_MOUSEBUTTONDOWN) {
    std::cout << "\nButton pressed at: ("
      << E.button.x << ", " << E.button.y;
  }
}

int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_VIDEO);
  Window GameWindow;
  SDL_Event E;

  while (true) {
    while (SDL_PollEvent(&E)) {
      HandleMouseEvent(E);
    }
    GameWindow.Update();
  }

  SDL_Quit();
  return 0;
}
Mouse moved to: 110, 102
Mouse moved to: 111, 102
Button pressed at: 111, 102
Mouse moved to: 106, 102

State-Based Approach

Implementing the same functionality using a state-based approach could look like this

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

void CheckMouseState() {
  int x, y;
  Uint32 Buttons = SDL_GetMouseState(&x, &y);  

  std::cout << "Current mouse state - Position:"
    " (" << x << ", " << y << ") ";

  if (Buttons & SDL_BUTTON_LMASK) {
    std::cout << "Left button pressed";
  }
  std::cout << "\n";
}

int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_VIDEO);
  Window GameWindow;
  SDL_Event E;

  while (true) {
    while (SDL_PollEvent(&E)) {
      // Handle other events...
    }

    CheckMouseState();  
    GameWindow.Update();
  }

  SDL_Quit();
  return 0;
}
Current mouse state - Position (0, 43)
Current mouse state - Position (1, 43)
Current mouse state - Position (1, 44) Left Button Pressed
Current mouse state - Position (1, 44) Left Button Pressed

Key Differences

Timing

  • Events tell you WHEN something happened
  • State tells you WHAT is happening right now

Data Capture

  • Events capture every change (movement, clicks)
  • State only gives you the current situation when you check

Use Cases

Events are better for:

  • UI interactions (clicks, hover effects)
  • Precise movement tracking
  • Responding to specific actions

State is better for:

  • Continuous tracking (drag operations)
  • Regular polling (cursor following)
  • Checking conditions during other events

Performance

  • Events generate data for every change
  • State checking only uses resources when you explicitly check

Choose events when you need to know about every change, and state checking when you just need to know the current situation at specific times.

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

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:

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