From 5d185aa48016ddb9deca64ad93d6275f393fec30 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Sun, 2 May 2021 05:01:43 +0200 Subject: [PATCH] New method Input_GetKeyDir, to get direction with only the keys. --- src/Input.c | 37 +++++++++++++++++++++++++++++++++---- src/Input.h | 8 +++++++- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/Input.c b/src/Input.c index 499d9a4..2ce4179 100644 --- a/src/Input.c +++ b/src/Input.c @@ -113,9 +113,7 @@ void Input_SetPointerDown(int pointerDown) { ///////////////////////////// // Input_GetPointerDown // -int Input_GetPointerDown(){ - return _pointerDown; -} +int Input_GetPointerDown() { return _pointerDown; } ///////////////////////////// // Input_GetPointerPosition @@ -152,7 +150,7 @@ int Input_AnyKey() { ///////////////////////////// // Input_GetDir // -// Reports the direction of the dpad. +// Reports the direction of the dpad and mouse. int Input_GetDir(vec2 dir) { float vlen; Uint8 buttons; @@ -193,3 +191,34 @@ int Input_GetDir(vec2 dir) { } } } + +///////////////////////////// +// Input_GetKeyDir +// +// Reports the direction of the dpad. +int Input_GetKeyDir(vec2 dir) { + float vlen; + + // Use the keyboard + vec2_set(dir, 0.0f, 0.0f); + if (Input_GetKey(InputKey_Up)) { + dir[1] -= 1.0f; + } + if (Input_GetKey(InputKey_Down)) { + dir[1] += 1.0f; + } + if (Input_GetKey(InputKey_Left)) { + dir[0] -= 1.0f; + } + if (Input_GetKey(InputKey_Right)) { + dir[0] += 1.0f; + } + vlen = vec2_dot(dir, dir); + if (vlen > 0.0f) { + vlen = sqrtf(vlen); + vec2_scale(dir, dir, 1.0f / vlen); + return (1); + } else { + return (0); + } +} diff --git a/src/Input.h b/src/Input.h index 31d5f55..f6414fa 100644 --- a/src/Input.h +++ b/src/Input.h @@ -99,7 +99,13 @@ int Input_AnyKey(); ///////////////////////////// // Input_GetDir // -// Reports the direction of the dpad. +// Reports the direction of the dpad and mouse. int Input_GetDir(vec2 dir); +///////////////////////////// +// Input_GetKeyDir +// +// Reports the direction of the dpad. +int Input_GetKeyDir(vec2 dir); + #endif