summaryrefslogtreecommitdiffhomepage
path: root/src/shapes.c
diff options
context:
space:
mode:
authorvictorfisac <[email protected]>2018-03-10 19:10:37 +0100
committervictorfisac <[email protected]>2018-03-10 19:10:37 +0100
commit8f1d6f38506ff6449866913c6d88b0f25ca2d8f4 (patch)
tree659719ef12dbdedd9a51c85af0e43ac327c84b40 /src/shapes.c
parentdd50348b4dffe59be03538bdbaf2a3d084426e1f (diff)
parentdf50eada531b54d6771eff81cbe140f9453d54d9 (diff)
downloadraylib-8f1d6f38506ff6449866913c6d88b0f25ca2d8f4.tar.gz
raylib-8f1d6f38506ff6449866913c6d88b0f25ca2d8f4.zip
Merge branch 'master' of github.com:raysan5/raylib into fork/master
Diffstat (limited to 'src/shapes.c')
-rw-r--r--src/shapes.c208
1 files changed, 160 insertions, 48 deletions
diff --git a/src/shapes.c b/src/shapes.c
index 0e544718..693463ff 100644
--- a/src/shapes.c
+++ b/src/shapes.c
@@ -10,10 +10,14 @@
* #define SUPPORT_TRIANGLES_ONLY
* Draw shapes using only TRIANGLES, vertex are accumulated in TRIANGLES arrays
*
+* #define USE_DEFAULT_FONT_TEXTURE
+* Draw rectangle shapes using font texture white character instead of default white texture
+* Allows drawing rectangles and text with a single draw call, very useful for GUI systems!
+*
*
* LICENSE: zlib/libpng
*
-* Copyright (c) 2014-2017 Ramon Santamaria (@raysan5)
+* Copyright (c) 2014-2018 Ramon Santamaria (@raysan5)
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
@@ -32,6 +36,8 @@
*
**********************************************************************************************/
+#define USE_DEFAULT_FONT_TEXTURE
+
#include "raylib.h"
#include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2
@@ -245,50 +251,6 @@ void DrawRectangle(int posX, int posY, int width, int height, Color color)
DrawRectangleV(position, size, color);
}
-// Draw a color-filled rectangle
-void DrawRectangleRec(Rectangle rec, Color color)
-{
- DrawRectangle(rec.x, rec.y, rec.width, rec.height, color);
-}
-
-void DrawRectanglePro(Rectangle rec, Vector2 origin, float rotation, Color color)
-{
- rlEnableTexture(GetTextureDefault().id);
-
- rlPushMatrix();
- rlTranslatef((float)rec.x, (float)rec.y, 0);
- rlRotatef(rotation, 0, 0, 1);
- rlTranslatef(-origin.x, -origin.y, 0);
-
- rlBegin(RL_QUADS);
- rlColor4ub(color.r, color.g, color.b, color.a);
- rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer
-
- rlVertex2f(0.0f, 0.0f);
- rlVertex2f(0.0f, (float)rec.height);
- rlVertex2f((float)rec.width, (float)rec.height);
- rlVertex2f((float)rec.width, 0.0f);
- rlEnd();
- rlPopMatrix();
-
- rlDisableTexture();
-}
-
-// Draw a gradient-filled rectangle
-// NOTE: Gradient goes from bottom (color1) to top (color2)
-void DrawRectangleGradient(int posX, int posY, int width, int height, Color color1, Color color2)
-{
- rlBegin(RL_TRIANGLES);
- rlColor4ub(color1.r, color1.g, color1.b, color1.a); rlVertex2i(posX, posY);
- rlColor4ub(color2.r, color2.g, color2.b, color2.a); rlVertex2i(posX, posY + height);
- rlColor4ub(color2.r, color2.g, color2.b, color2.a); rlVertex2i(posX + width, posY + height);
-
- rlColor4ub(color1.r, color1.g, color1.b, color1.a); rlVertex2i(posX, posY);
- rlColor4ub(color2.r, color2.g, color2.b, color2.a); rlVertex2i(posX + width, posY + height);
- rlColor4ub(color1.r, color1.g, color1.b, color1.a); rlVertex2i(posX + width, posY);
- rlEnd();
-}
-
// Draw a color-filled rectangle (Vector version)
// NOTE: On OpenGL 3.3 and ES2 we use QUADS to avoid drawing order issues (view rlglDraw)
void DrawRectangleV(Vector2 position, Vector2 size, Color color)
@@ -309,6 +271,34 @@ void DrawRectangleV(Vector2 position, Vector2 size, Color color)
}
else if ((rlGetVersion() == OPENGL_21) || (rlGetVersion() == OPENGL_33) || (rlGetVersion() == OPENGL_ES_20))
{
+#if defined(USE_DEFAULT_FONT_TEXTURE)
+ // Draw rectangle using font texture white character
+ rlEnableTexture(GetDefaultFont().texture.id);
+
+ rlBegin(RL_QUADS);
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlNormal3f(0.0f, 0.0f, 1.0f);
+
+ // NOTE: Default raylib font character 95 is a white square
+ rlTexCoord2f((float)GetDefaultFont().chars[95].rec.x/GetDefaultFont().texture.width,
+ (float)GetDefaultFont().chars[95].rec.y/GetDefaultFont().texture.height);
+ rlVertex2f(position.x, position.y);
+
+ rlTexCoord2f((float)GetDefaultFont().chars[95].rec.x/GetDefaultFont().texture.width,
+ (float)(GetDefaultFont().chars[95].rec.y + GetDefaultFont().chars[95].rec.height)/GetDefaultFont().texture.height);
+ rlVertex2f(position.x, position.y + size.y);
+
+ rlTexCoord2f((float)(GetDefaultFont().chars[95].rec.x + GetDefaultFont().chars[95].rec.width)/GetDefaultFont().texture.width,
+ (float)(GetDefaultFont().chars[95].rec.y + GetDefaultFont().chars[95].rec.height)/GetDefaultFont().texture.height);
+ rlVertex2f(position.x + size.x, position.y + size.y);
+
+ rlTexCoord2f((float)(GetDefaultFont().chars[95].rec.x + GetDefaultFont().chars[95].rec.width)/GetDefaultFont().texture.width,
+ (float)GetDefaultFont().chars[95].rec.y/GetDefaultFont().texture.height);
+ rlVertex2f(position.x + size.x, position.y);
+ rlEnd();
+
+ rlDisableTexture();
+#else
rlEnableTexture(GetTextureDefault().id); // Default white texture
rlBegin(RL_QUADS);
@@ -329,9 +319,114 @@ void DrawRectangleV(Vector2 position, Vector2 size, Color color)
rlEnd();
rlDisableTexture();
+#endif
}
}
+// Draw a color-filled rectangle
+void DrawRectangleRec(Rectangle rec, Color color)
+{
+ DrawRectangle(rec.x, rec.y, rec.width, rec.height, color);
+}
+
+void DrawRectanglePro(Rectangle rec, Vector2 origin, float rotation, Color color)
+{
+ rlEnableTexture(GetTextureDefault().id);
+
+ rlPushMatrix();
+ rlTranslatef((float)rec.x, (float)rec.y, 0);
+ rlRotatef(rotation, 0, 0, 1);
+ rlTranslatef(-origin.x, -origin.y, 0);
+
+ rlBegin(RL_QUADS);
+ rlColor4ub(color.r, color.g, color.b, color.a);
+ rlNormal3f(0.0f, 0.0f, 1.0f); // Normal vector pointing towards viewer
+
+ rlVertex2f(0.0f, 0.0f);
+ rlVertex2f(0.0f, (float)rec.height);
+ rlVertex2f((float)rec.width, (float)rec.height);
+ rlVertex2f((float)rec.width, 0.0f);
+ rlEnd();
+ rlPopMatrix();
+
+ rlDisableTexture();
+}
+
+// Draw a vertical-gradient-filled rectangle
+// NOTE: Gradient goes from bottom (color1) to top (color2)
+void DrawRectangleGradientV(int posX, int posY, int width, int height, Color color1, Color color2)
+{
+ DrawRectangleGradientEx((Rectangle){ posX, posY, width, height }, color1, color2, color2, color1);
+}
+
+// Draw a horizontal-gradient-filled rectangle
+// NOTE: Gradient goes from bottom (color1) to top (color2)
+void DrawRectangleGradientH(int posX, int posY, int width, int height, Color color1, Color color2)
+{
+ DrawRectangleGradientEx((Rectangle){ posX, posY, width, height }, color1, color1, color2, color2);
+}
+
+// Draw a gradient-filled rectangle
+// NOTE: Colors refer to corners, starting at top-lef corner and counter-clockwise
+void DrawRectangleGradientEx(Rectangle rec, Color col1, Color col2, Color col3, Color col4)
+{
+#if defined(USE_DEFAULT_FONT_TEXTURE)
+ // Draw rectangle using font texture white character
+ rlEnableTexture(GetDefaultFont().texture.id);
+
+ rlBegin(RL_QUADS);
+ rlNormal3f(0.0f, 0.0f, 1.0f);
+
+ // NOTE: Default raylib font character 95 is a white square
+ rlColor4ub(col1.r, col1.g, col1.b, col1.a);
+ rlTexCoord2f((float)GetDefaultFont().chars[95].rec.x/GetDefaultFont().texture.width,
+ (float)GetDefaultFont().chars[95].rec.y/GetDefaultFont().texture.height);
+ rlVertex2f(rec.x, rec.y);
+
+ rlColor4ub(col2.r, col2.g, col2.b, col2.a);
+ rlTexCoord2f((float)GetDefaultFont().chars[95].rec.x/GetDefaultFont().texture.width,
+ (float)(GetDefaultFont().chars[95].rec.y + GetDefaultFont().chars[95].rec.height)/GetDefaultFont().texture.height);
+ rlVertex2f(rec.x, rec.y + rec.height);
+
+ rlColor4ub(col3.r, col3.g, col3.b, col3.a);
+ rlTexCoord2f((float)(GetDefaultFont().chars[95].rec.x + GetDefaultFont().chars[95].rec.width)/GetDefaultFont().texture.width,
+ (float)(GetDefaultFont().chars[95].rec.y + GetDefaultFont().chars[95].rec.height)/GetDefaultFont().texture.height);
+ rlVertex2f(rec.x + rec.width, rec.y + rec.height);
+
+ rlColor4ub(col4.r, col4.g, col4.b, col4.a);
+ rlTexCoord2f((float)(GetDefaultFont().chars[95].rec.x + GetDefaultFont().chars[95].rec.width)/GetDefaultFont().texture.width,
+ (float)GetDefaultFont().chars[95].rec.y/GetDefaultFont().texture.height);
+ rlVertex2f(rec.x + rec.width, rec.y);
+ rlEnd();
+
+ rlDisableTexture();
+#else
+ rlEnableTexture(GetTextureDefault().id); // Default white texture
+
+ rlBegin(RL_QUADS);
+ rlNormal3f(0.0f, 0.0f, 1.0f);
+
+ rlColor4ub(col1.r, col1.g, col1.b, col1.a);
+ rlTexCoord2f(0.0f, 0.0f);
+ rlVertex2f(rec.x, rec.y);
+
+ rlColor4ub(col2.r, col2.g, col2.b, col2.a);
+ rlTexCoord2f(0.0f, 1.0f);
+ rlVertex2f(rec.x, rec.y + rec.height);
+
+ rlColor4ub(col3.r, col3.g, col3.b, col3.a);
+ rlTexCoord2f(1.0f, 1.0f);
+ rlVertex2f(rec.x + rec.width, rec.y + rec.height);
+
+ rlColor4ub(col4.r, col4.g, col4.b, col4.a);
+ rlTexCoord2f(1.0f, 0.0f);
+ rlVertex2f(rec.x + rec.width, rec.y);
+ rlEnd();
+
+ rlDisableTexture();
+#endif
+}
+
// Draw rectangle outline
// NOTE: On OpenGL 3.3 and ES2 we use QUADS to avoid drawing order issues (view rlglDraw)
void DrawRectangleLines(int posX, int posY, int width, int height, Color color)
@@ -362,6 +457,21 @@ void DrawRectangleLines(int posX, int posY, int width, int height, Color color)
}
}
+// Draw rectangle outline with extended parameters
+void DrawRectangleLinesEx(Rectangle rec, int lineThick, Color color)
+{
+ if (lineThick > rec.width || lineThick > rec.height)
+ {
+ if(rec.width > rec.height) lineThick = rec.height/2;
+ else if (rec.width < rec.height) lineThick = rec.width/2;
+ }
+
+ DrawRectangle(rec.x, rec.y, rec.width, lineThick, color);
+ DrawRectangle(rec.x - lineThick + rec.width, rec.y + lineThick, lineThick, rec.height - lineThick*2, color);
+ DrawRectangle(rec.x, rec.y + rec.height - lineThick, rec.width, lineThick, color);
+ DrawRectangle(rec.x, rec.y + lineThick, lineThick, rec.height - lineThick*2, color);
+}
+
// Draw a triangle
void DrawTriangle(Vector2 v1, Vector2 v2, Vector2 v3, Color color)
{
@@ -627,6 +737,8 @@ Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2)
// NOTE: Required for DrawLineBezier()
static float EaseCubicInOut(float t, float b, float c, float d)
{
- if ((t/=d/2) < 1) return (c/2*t*t*t + b);
- return (c/2*((t-=2)*t*t + 2) + b);
-} \ No newline at end of file
+ if ((t /= 0.5*d) < 1)
+ return 0.5*c*t*t*t + b;
+ t -= 2;
+ return 0.5*c*(t*t*t + 2) + b;
+}