better DRY in SDL

This commit is contained in:
NeatNit 2018-10-06 23:31:36 +03:00
parent 718aa51b0b
commit 5d3ca5ca11
2 changed files with 6 additions and 6 deletions

View file

@ -41,15 +41,15 @@ void EmuWindow_SDL2::OnMouseButton(u32 button, u8 state, s32 x, s32 y) {
} }
} }
std::pair<int, int> EmuWindow_SDL2::TouchToPixelPos(float touch_x, float touch_y) const { std::pair<unsigned, unsigned> EmuWindow_SDL2::TouchToPixelPos(float touch_x, float touch_y) const {
int w, h; int w, h;
SDL_GetWindowSize(render_window, &w, &h); SDL_GetWindowSize(render_window, &w, &h);
touch_x *= w; touch_x *= w;
touch_y *= h; touch_y *= h;
// add 0.5 to have .99992 round up instead of down. These will always be near-integers. return {static_cast<unsigned>(std::max(std::round(touch_x), 0.0f)),
return {static_cast<int>(touch_x + 0.5), static_cast<int>(touch_y + 0.5)}; static_cast<unsigned>(std::max(std::round(touch_y), 0.0f))};
} }
void EmuWindow_SDL2::OnFingerDown(float x, float y) { void EmuWindow_SDL2::OnFingerDown(float x, float y) {
@ -58,12 +58,12 @@ void EmuWindow_SDL2::OnFingerDown(float x, float y) {
// 3DS does // 3DS does
const auto [px, py] = TouchToPixelPos(x, y); const auto [px, py] = TouchToPixelPos(x, y);
TouchPressed(static_cast<unsigned>(std::max(px, 0)), static_cast<unsigned>(std::max(py, 0))); TouchPressed(px, py);
} }
void EmuWindow_SDL2::OnFingerMotion(float x, float y) { void EmuWindow_SDL2::OnFingerMotion(float x, float y) {
const auto [px, py] = TouchToPixelPos(x, y); const auto [px, py] = TouchToPixelPos(x, y);
TouchMoved(static_cast<unsigned>(std::max(px, 0)), static_cast<unsigned>(std::max(py, 0))); TouchMoved(px, py);
} }
void EmuWindow_SDL2::OnFingerUp() { void EmuWindow_SDL2::OnFingerUp() {

View file

@ -42,7 +42,7 @@ private:
void OnMouseButton(u32 button, u8 state, s32 x, s32 y); void OnMouseButton(u32 button, u8 state, s32 x, s32 y);
/// Translates pixel position (0..1) to pixel positions /// Translates pixel position (0..1) to pixel positions
std::pair<int, int> TouchToPixelPos(float touch_x, float touch_y) const; std::pair<unsigned, unsigned> TouchToPixelPos(float touch_x, float touch_y) const;
/// Called by PollEvents when a finger starts touching the touchscreen /// Called by PollEvents when a finger starts touching the touchscreen
void OnFingerDown(float x, float y); void OnFingerDown(float x, float y);