aboutsummaryrefslogtreecommitdiff
path: root/src
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
parent818e79638b5ff14fdae9f6a162e596bf119f82c5 (diff)
downloadraylib-e9143b8a8d2eb439b01b94c00518db2b59ffb1e7.tar.gz
raylib-e9143b8a8d2eb439b01b94c00518db2b59ffb1e7.zip
Added some functions and Updated examples
View CHANGELOG for details
Diffstat (limited to 'src')
-rw-r--r--src/core.c117
-rw-r--r--src/raylib.h29
-rw-r--r--src/text.c15
-rw-r--r--src/textures.c42
4 files changed, 178 insertions, 25 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;
diff --git a/src/raylib.h b/src/raylib.h
index c23834a0..d6319373 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -260,19 +260,25 @@ int GetHexValue(Color color); // Returns hexadecimal v
//------------------------------------------------------------------------------------
// Input Handling Functions (Module: core)
//------------------------------------------------------------------------------------
-bool IsKeyPressed(int key); // Detect if a key is being pressed
-bool IsKeyReleased(int key); // Detect if a key is NOT being pressed
-
-bool IsMouseButtonPressed(int button); // Detect if a mouse button is being pressed
-bool IsMouseButtonReleased(int button); // Detect if a mouse button is NOT being pressed
+bool IsKeyPressed(int key); // Detect if a key has been pressed once
+bool IsKeyDown(int key); // Detect if a key is being pressed
+bool IsKeyReleased(int key); // Detect if a key has been released once
+bool IsKeyUp(int key); // Detect if a key is NOT being pressed
+
+bool IsMouseButtonPressed(int button); // Detect if a mouse button has been pressed once
+bool IsMouseButtonDown(int button); // Detect if a mouse button is being pressed
+bool IsMouseButtonReleased(int button); // Detect if a mouse button has been released once
+bool IsMouseButtonUp(int button); // Detect if a mouse button is NOT being pressed
int GetMouseX(); // Returns mouse position X
int GetMouseY(); // Returns mouse position Y
Vector2 GetMousePosition(); // Returns mouse position XY
bool IsGamepadAvailable(int gamepad); // Detect if a gamepad is available
Vector2 GetGamepadMovement(int gamepad); // Return axis movement vector for a gamepad
-bool IsGamepadButtonPressed(int gamepad, int button); // Detect if a gamepad button is being pressed
-bool IsGamepadButtonReleased(int gamepad, int button); // Detect if a gamepad button is NOT being pressed
+bool IsGamepadButtonPressed(int gamepad, int button); // Detect if a gamepad button has been pressed once
+bool IsGamepadButtonDown(int gamepad, int button); // Detect if a gamepad button is being pressed
+bool IsGamepadButtonReleased(int gamepad, int button); // Detect if a gamepad button has been released once
+bool IsGamepadButtonUp(int gamepad, int button); // Detect if a gamepad button is NOT being pressed
//------------------------------------------------------------------------------------
// Basic Shapes Drawing Functions (Module: shapes)
@@ -305,19 +311,20 @@ Texture2D LoadTexture(const char *fileName);
void UnloadTexture(Texture2D texture); // Unload texture from GPU memory
void DrawTexture(Texture2D texture, int posX, int posY, Color tint); // Draw a Texture2D
void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float scale, Color tint); // Draw a Texture2D with extended parameters
-void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, float scale, Color tint); // Draw a part of a texture defined by a rectangle
+void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, Color tint); // Draw a part of a texture defined by a rectangle
+void DrawTexturePro(Texture2D texture, Rectangle sourceRec, Rectangle destRec, Vector2 origin, float rotation, Color tint); // Draw a part of a texture defined by a rectangle with 'pro' parameters
//------------------------------------------------------------------------------------
// Font Loading and Text Drawing Functions (Module: text)
//------------------------------------------------------------------------------------
SpriteFont LoadSpriteFont(const char *fileName); // Load a SpriteFont image into GPU memory
void UnloadSpriteFont(SpriteFont spriteFont); // Unload SpriteFont from GPU memory
-void DrawText(const char *text, int posX, int posY, int fontSize, int spacing, Color color); // Draw text (using default font)
+void DrawText(const char *text, int posX, int posY, int fontSize, Color color); // Draw text (using default font)
void DrawTextEx(SpriteFont spriteFont, const char* text, Vector2 position, int fontSize, int spacing, Color tint); // Draw text using SpriteFont
-int MeasureText(const char *text, int fontSize, int spacing); // Measure string width for default font
+int MeasureText(const char *text, int fontSize); // Measure string width for default font
Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, int fontSize, int spacing); // Measure string size for SpriteFont
int GetFontBaseSize(SpriteFont spriteFont); // Returns the base size for a SpriteFont (chars height)
-void DrawFps(int posX, int posY); // Shows current FPS on top-left corner
+void DrawFPS(int posX, int posY); // Shows current FPS on top-left corner
const char *FormatText(const char *text, ...); // Formatting of text with variables to 'embed'
//------------------------------------------------------------------------------------
diff --git a/src/text.c b/src/text.c
index ac0dacfd..74db16cd 100644
--- a/src/text.c
+++ b/src/text.c
@@ -283,11 +283,11 @@ void UnloadSpriteFont(SpriteFont spriteFont)
// Draw text (using default font)
// NOTE: fontSize work like in any drawing program but if fontSize is lower than font-base-size, then font-base-size is used
-void DrawText(const char* text, int posX, int posY, int fontSize, int spacing, Color color)
+void DrawText(const char* text, int posX, int posY, int fontSize, Color color)
{
Vector2 position = { (float)posX, (float)posY };
- DrawTextEx(defaultFont, text, position, fontSize, spacing, color);
+ DrawTextEx(defaultFont, text, position, fontSize, 1, color);
}
// Formatting of text with variables to 'embed'
@@ -349,16 +349,15 @@ void DrawTextEx(SpriteFont spriteFont, const char* text, Vector2 position, int f
}
// Measure string width for default font
-int MeasureText(const char *text, int fontSize, int spacing)
+int MeasureText(const char *text, int fontSize)
{
Vector2 vec;
- vec = MeasureTextEx(defaultFont, text, fontSize, spacing);
+ vec = MeasureTextEx(defaultFont, text, fontSize, 1);
return (int)vec.x;
}
-
// Measure string size for SpriteFont
Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, int fontSize, int spacing)
{
@@ -391,7 +390,7 @@ int GetFontBaseSize(SpriteFont spriteFont)
// Shows current FPS on top-left corner
// NOTE: Uses default font
-void DrawFps(int posX, int posY)
+void DrawFPS(int posX, int posY)
{
// NOTE: We are rendering fps every second for better viewing on high framerates
static float fps;
@@ -403,7 +402,7 @@ void DrawFps(int posX, int posY)
if (counter < refreshRate)
{
sprintf(buffer, "%2.0f FPS", fps);
- DrawText(buffer, posX, posY, 20, 1, LIME);
+ DrawText(buffer, posX, posY, 20, LIME);
counter++;
}
@@ -412,7 +411,7 @@ void DrawFps(int posX, int posY)
fps = GetFPS();
refreshRate = fps;
sprintf(buffer, "%2.0f FPS", fps);
- DrawText(buffer, posX, posY, 20, 1, LIME);
+ DrawText(buffer, posX, posY, 20, LIME);
counter = 0;
}
diff --git a/src/textures.c b/src/textures.c
index fc342a80..3ccb5358 100644
--- a/src/textures.c
+++ b/src/textures.c
@@ -197,7 +197,7 @@ void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float sc
}
// Draw a part of a texture (defined by a rectangle)
-void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, float scale, Color tint)
+void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, Color tint)
{
glEnable(GL_TEXTURE_2D); // Enable textures usage
@@ -205,7 +205,7 @@ void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, fl
glPushMatrix();
glTranslatef(position.x, position.y, 0);
- glScalef(scale, scale, 1.0f);
+ //glScalef(1.0f, 1.0f, 1.0f);
//glRotatef(rotation, 0, 0, 1);
glBegin(GL_QUADS);
@@ -233,6 +233,44 @@ void DrawTextureRec(Texture2D texture, Rectangle sourceRec, Vector2 position, fl
glDisable(GL_TEXTURE_2D); // Disable textures usage
}
+// Draw a part of a texture (defined by a rectangle) with 'pro' parameters
+// TODO: Test this function...
+void DrawTexturePro(Texture2D texture, Rectangle sourceRec, Rectangle destRec, Vector2 origin, float rotation, Color tint)
+{
+ glEnable(GL_TEXTURE_2D); // Enable textures usage
+
+ glBindTexture(GL_TEXTURE_2D, texture.glId);
+
+ glPushMatrix();
+ glTranslatef(-origin.x, -origin.y, 0);
+ glRotatef(rotation, 0, 0, 1);
+ glTranslatef(destRec.x + origin.x, destRec.y + origin.y, 0);
+
+ glBegin(GL_QUADS);
+ glColor4ub(tint.r, tint.g, tint.b, tint.a);
+ glNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer
+
+ // Bottom-left corner for texture and quad
+ glTexCoord2f((float)sourceRec.x / texture.width, (float)sourceRec.y / texture.height);
+ glVertex2f(0.0f, 0.0f);
+
+ // Bottom-right corner for texture and quad
+ glTexCoord2f((float)(sourceRec.x + sourceRec.width) / texture.width, (float)sourceRec.y / texture.height);
+ glVertex2f(destRec.width, 0.0f);
+
+ // Top-right corner for texture and quad
+ glTexCoord2f((float)(sourceRec.x + sourceRec.width) / texture.width, (float)(sourceRec.y + sourceRec.height) / texture.height);
+ glVertex2f(destRec.width, destRec.height);
+
+ // Top-left corner for texture and quad
+ glTexCoord2f((float)sourceRec.x / texture.width, (float)(sourceRec.y + sourceRec.height) / texture.height);
+ glVertex2f(0.0f, destRec.height);
+ glEnd();
+ glPopMatrix();
+
+ glDisable(GL_TEXTURE_2D); // Disable textures usage
+}
+
// Creates a bitmap (BMP) file from an array of pixel data
// NOTE: This function is only used by module [core], not explicitly available to raylib users
extern void WriteBitmap(const char *fileName, const Color *imgDataPixel, int width, int height)