aboutsummaryrefslogtreecommitdiff
path: root/src/core.c
diff options
context:
space:
mode:
authorraysan5 <raysan5@gmail.com>2013-11-28 19:59:56 +0100
committerraysan5 <raysan5@gmail.com>2013-11-28 19:59:56 +0100
commite9143b8a8d2eb439b01b94c00518db2b59ffb1e7 (patch)
tree8539603494a773f852721da1d6f7b19bca0d8f64 /src/core.c
parent818e79638b5ff14fdae9f6a162e596bf119f82c5 (diff)
downloadraylib-e9143b8a8d2eb439b01b94c00518db2b59ffb1e7.tar.gz
raylib-e9143b8a8d2eb439b01b94c00518db2b59ffb1e7.zip
Added some functions and Updated examples
View CHANGELOG for details
Diffstat (limited to 'src/core.c')
-rw-r--r--src/core.c117
1 files changed, 113 insertions, 4 deletions
diff --git a/src/core.c b/src/core.c
index a784f4bb..76df254f 100644
--- a/src/core.c
+++ b/src/core.c
@@ -61,6 +61,15 @@ static double targetTime = 0; // Desired time for one frame, if 0
static int windowWidth, windowHeight; // Required to switch between windowed/fullscren mode (F11)
static char *windowTitle; // Required to switch between windowed/fullscren mode (F11)
+static char previousKeyState[512] = { 0 }; // Required to check if key pressed/released once
+static char currentKeyState[512] = { 0 }; // Required to check if key pressed/released once
+
+static char previousMouseState[3] = { 0 }; // Required to check if mouse btn pressed/released once
+static char currentMouseState[3] = { 0 }; // Required to check if mouse btn pressed/released once
+
+static char previousGamepadState[32] = {0}; // Required to check if gamepad btn pressed/released once
+static char currentGamepadState[32] = {0}; // Required to check if gamepad btn pressed/released once
+
//----------------------------------------------------------------------------------
// Other Modules Functions Declaration (required by core)
//----------------------------------------------------------------------------------
@@ -289,30 +298,98 @@ int GetHexValue(Color color)
// Module Functions Definition - Input (Keyboard, Mouse, Gamepad) Functions
//----------------------------------------------------------------------------------
-// Detect if a key is being pressed (key held down)
+// Detect if a key has been pressed once
bool IsKeyPressed(int key)
+{
+ bool ret = false;
+
+ currentKeyState[key] = IsKeyDown(key);
+
+ if (currentKeyState[key] != previousKeyState[key])
+ {
+ if (currentKeyState[key]) ret = true;
+ previousKeyState[key] = currentKeyState[key];
+ }
+ else ret = false;
+
+ return ret;
+}
+
+// Detect if a key is being pressed (key held down)
+bool IsKeyDown(int key)
{
if (glfwGetKey(window, key) == GLFW_PRESS) return true;
else return false;
}
-// Detect if a key is NOT being pressed (key not held down)
+// Detect if a key has been released once
bool IsKeyReleased(int key)
+{
+ bool ret = false;
+
+ currentKeyState[key] = IsKeyUp(key);
+
+ if (currentKeyState[key] != previousKeyState[key])
+ {
+ if (currentKeyState[key]) ret = true;
+ previousKeyState[key] = currentKeyState[key];
+ }
+ else ret = false;
+
+ return ret;
+}
+
+// Detect if a key is NOT being pressed (key not held down)
+bool IsKeyUp(int key)
{
if (glfwGetKey(window, key) == GLFW_RELEASE) return true;
else return false;
}
-// Detect if a mouse button is being pressed
+// Detect if a mouse button has been pressed once
bool IsMouseButtonPressed(int button)
{
+ bool ret = false;
+
+ currentMouseState[button] = IsMouseButtonDown(button);
+
+ if (currentMouseState[button] != previousMouseState[button])
+ {
+ if (currentMouseState[button]) ret = true;
+ previousMouseState[button] = currentMouseState[button];
+ }
+ else ret = false;
+
+ return ret;
+}
+
+// Detect if a mouse button is being pressed
+bool IsMouseButtonDown(int button)
+{
if (glfwGetMouseButton(window, button) == GLFW_PRESS) return true;
else return false;
}
-// Detect if a mouse button is NOT being pressed
+// Detect if a mouse button has been released once
bool IsMouseButtonReleased(int button)
{
+ bool ret = false;
+
+ currentMouseState[button] = IsMouseButtonUp(button);
+
+ if (currentMouseState[button] != previousMouseState[button])
+ {
+ if (currentMouseState[button]) ret = true;
+ previousMouseState[button] = currentMouseState[button];
+ }
+ else ret = false;
+
+ return ret;
+}
+
+// Detect if a mouse button is NOT being pressed
+bool IsMouseButtonUp(int button)
+{
if (glfwGetMouseButton(window, button) == GLFW_RELEASE) return true;
else return false;
}
@@ -386,6 +463,22 @@ Vector2 GetGamepadMovement(int gamepad)
// Detect if a gamepad button is being pressed
bool IsGamepadButtonPressed(int gamepad, int button)
{
+ bool ret = false;
+
+ currentGamepadState[button] = IsGamepadButtonDown(gamepad, button);
+
+ if (currentGamepadState[button] != previousGamepadState[button])
+ {
+ if (currentGamepadState[button]) ret = true;
+ previousGamepadState[button] = currentGamepadState[button];
+ }
+ else ret = false;
+
+ return ret;
+}
+
+bool IsGamepadButtonDown(int gamepad, int button)
+{
const unsigned char* buttons;
int buttonsCount;
@@ -401,6 +494,22 @@ bool IsGamepadButtonPressed(int gamepad, int button)
// Detect if a gamepad button is NOT being pressed
bool IsGamepadButtonReleased(int gamepad, int button)
{
+ bool ret = false;
+
+ currentGamepadState[button] = IsGamepadButtonUp(gamepad, button);
+
+ if (currentGamepadState[button] != previousGamepadState[button])
+ {
+ if (currentGamepadState[button]) ret = true;
+ previousGamepadState[button] = currentGamepadState[button];
+ }
+ else ret = false;
+
+ return ret;
+}
+
+bool IsGamepadButtonUp(int gamepad, int button)
+{
const unsigned char* buttons;
int buttonsCount;