The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
input.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2022 - 2025
3 by Thomas Iorns <mesilliac@tomanui.nz>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#include "sdl/input.hpp"
17
18#include "sdl/point.hpp"
19#include "video.hpp"
20
21#include <SDL3/SDL_mouse.h>
22#include <SDL3/SDL_keyboard.h>
23#include <SDL3/SDL_render.h>
24
25namespace sdl
26{
27
28uint32_t get_mouse_state(float* x, float* y)
29{
30 uint32_t buttons = SDL_GetMouseState(x, y);
31
32 if (video::headless()) {
33 return buttons;
34 }
35
36 // The game canvas may be offset inside the window,
37 // as well as potentially having a different size.
38 // Translate to game-native coordinates using SDL
39 SDL_RenderCoordinatesFromWindow(video::get_renderer(), *x, *y, x, y);
40
41 return buttons;
42}
43
45{
46 return SDL_GetMouseState(nullptr, nullptr);
47}
48
50{
51 float x;
52 float y;
53 get_mouse_state(&x, &y);
54 return {static_cast<int>(x), static_cast<int>(y)};
55}
56
57unsigned get_mods()
58{
59 unsigned mods = SDL_GetModState();
60
61 // Filter for only the mods we use: shift, ctrl, alt, gui
62 mods &= SDL_KMOD_SHIFT | SDL_KMOD_CTRL | SDL_KMOD_ALT | SDL_KMOD_GUI;
63
64 // Set both left and right modifiers if either is active
65 if(mods & SDL_KMOD_SHIFT) {
66 mods |= SDL_KMOD_SHIFT;
67 }
68
69 if(mods & SDL_KMOD_CTRL) {
70 mods |= SDL_KMOD_CTRL;
71 }
72
73 if(mods & SDL_KMOD_ALT)
74 mods |= SDL_KMOD_ALT;
75
76 if(mods & SDL_KMOD_GUI) {
77 mods |= SDL_KMOD_GUI;
78 }
79
80 return mods;
81}
82
83} // namespace sdl
Contains functions for cleanly handling SDL input.
uint32_t get_mouse_button_mask()
Returns the current mouse button mask.
Definition input.cpp:44
uint32_t get_mouse_state(float *x, float *y)
A wrapper for SDL_GetMouseState that gives coordinates in draw space.
Definition input.cpp:28
unsigned get_mods()
Returns a bitmask of active modifier keys (ctrl, shift, alt, gui).
Definition input.cpp:57
point get_mouse_location()
Returns the current mouse location in draw space.
Definition input.cpp:49
bool headless()
The game is running headless.
Definition video.cpp:146
SDL_Renderer * get_renderer()
Definition video.cpp:703
Holds a 2D point.
Definition point.hpp:25