summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorchriscamacho <[email protected]>2021-03-25 13:22:10 +0000
committerGitHub <[email protected]>2021-03-25 14:22:10 +0100
commit9569d6a802a2230c92503456d544a5470a59d5cf (patch)
tree2ab9ac314c03b689b9dd8dced8c14d3569119afb
parentdd5935048539486b0672d8cf112f3df806352dea (diff)
downloadraylib-9569d6a802a2230c92503456d544a5470a59d5cf.tar.gz
raylib-9569d6a802a2230c92503456d544a5470a59d5cf.zip
Add DrawTexturedPoly and example (#1677)
* adds DrawTexturedPoly with example * the actual example ... ahem * moved DrawTexturePoly to textures function and example NB function name changed to fit with other DrawTextureXXX functions (no "d" ) Co-authored-by: codifies <[email protected]>
-rw-r--r--examples/Makefile3
-rw-r--r--examples/textures/textures_poly.c95
-rw-r--r--src/raylib.h1
-rw-r--r--src/textures.c42
4 files changed, 140 insertions, 1 deletions
diff --git a/examples/Makefile b/examples/Makefile
index ac42ea98..c8fc3fb2 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -437,7 +437,8 @@ TEXTURES = \
textures/textures_sprite_explosion \
textures/textures_bunnymark \
textures/textures_blend_modes \
- textures/textures_draw_tiled
+ textures/textures_draw_tiled \
+ textures/textures_poly
TEXT = \
text/text_raylib_fonts \
diff --git a/examples/textures/textures_poly.c b/examples/textures/textures_poly.c
new file mode 100644
index 00000000..bfb8cdc5
--- /dev/null
+++ b/examples/textures/textures_poly.c
@@ -0,0 +1,95 @@
+/*******************************************************************************************
+*
+* raylib [shapes] example - Draw Textured Polygon
+*
+* This example has been created using raylib 99.98 (www.raylib.com)
+* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+*
+* Copyright (c) 2014 Ramon Santamaria (@raysan5)
+* Copyright (c) 2021 Chris Camacho (codifies - bedroomcoders.co.uk)
+*
+********************************************************************************************/
+
+#include "raylib.h"
+#include "raymath.h"
+
+int main(void)
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ const int screenWidth = 800;
+ const int screenHeight = 450;
+
+ int numPnts = 11; // 10 points and back to the start
+
+ Vector2 tPnts[] = {
+ (Vector2){.75, 0},
+ (Vector2){.25, 0},
+ (Vector2){0, .5},
+ (Vector2){0, .75},
+ (Vector2){.25, 1},
+ (Vector2){.375, .875},
+ (Vector2){.625, .875},
+ (Vector2){.75, 1},
+ (Vector2){1, .75},
+ (Vector2){1, .5},
+ (Vector2){.75, 0} // close the poly
+ };
+
+ Vector2 pnts[numPnts];
+
+ // create the poly coords from the UV's
+ // you don't have to do this you can specify
+ // them however you want
+ for (int i=0; i < numPnts; i++)
+ {
+ pnts[i].x = (tPnts[i].x - 0.5) * 256.0;
+ pnts[i].y = (tPnts[i].y - 0.5) * 256.0;
+ }
+
+ InitWindow(screenWidth, screenHeight, "raylib [textures] example - Textured Polygon");
+
+ Texture tex = LoadTexture("resources/cat.png");
+
+ SetTargetFPS(60); // Set our game to run at 60 frames-per-second
+ //--------------------------------------------------------------------------------------
+ float ang = 0;
+ // Main game loop
+ while (!WindowShouldClose()) // Detect window close button or ESC key
+ {
+ // Update
+ //----------------------------------------------------------------------------------
+ // Update your variables here
+ //----------------------------------------------------------------------------------
+ ang++;
+
+ Vector2 dPnts[numPnts];
+ for (int i = 0; i < numPnts; i++)
+ {
+ dPnts[i] = Vector2Rotate(pnts[i], ang);
+ }
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(RAYWHITE);
+
+ DrawText("Textured Polygon", 20, 20, 20, DARKGRAY);
+
+ DrawTexturePoly(tex, screenWidth/2, screenHeight/2,
+ dPnts, tPnts, numPnts, WHITE);
+
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+ }
+
+ UnloadTexture(tex);
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+}
diff --git a/src/raylib.h b/src/raylib.h
index d02ec4b3..fcf820a9 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -1260,6 +1260,7 @@ RLAPI void DrawTextureQuad(Texture2D texture, Vector2 tiling, Vector2 offset, Re
RLAPI void DrawTextureTiled(Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, float scale, Color tint); // Draw part of a texture (defined by a rectangle) with rotation and scale tiled into dest.
RLAPI void DrawTexturePro(Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint); // Draw a part of a texture defined by a rectangle with 'pro' parameters
RLAPI void DrawTextureNPatch(Texture2D texture, NPatchInfo nPatchInfo, Rectangle dest, Vector2 origin, float rotation, Color tint); // Draws a texture (or part of it) that stretches or shrinks nicely
+RLAPI void DrawTexturePoly(Texture t, float x, float y, Vector2 *points, Vector2 *tPnts, int numPoints, Color colour); // Draw a textured polygon
// Color/pixel related functions
RLAPI Color Fade(Color color, float alpha); // Returns color with alpha applied, alpha goes from 0.0f to 1.0f
diff --git a/src/textures.c b/src/textures.c
index 875f736c..61e6f30b 100644
--- a/src/textures.c
+++ b/src/textures.c
@@ -3510,6 +3510,48 @@ void DrawTextureNPatch(Texture2D texture, NPatchInfo nPatchInfo, Rectangle dest,
}
}
+// t texture to use
+// x,y position to draw the poly (centre)
+// points points of the poly (relative to 0,0)
+// tPnts uv coordinates
+// numPoints number of points in the poly
+// colour the tint of the poly
+//
+// NB centre (0,0) must have straight line path to all points
+// without crossing perimeter, points must be in anticlockwise
+// order
+void DrawTexturePoly(Texture t, float x, float y,
+ Vector2 *points, Vector2 *tPnts,
+ int numPoints, Color colour)
+{
+ rlEnableTexture(t.id);
+
+ // for some reason texturing doesn't work on trianglesso make a
+ // degenerate QUAD, DrawTriangleFan does this too why ?
+ rlCheckRenderBatchLimit((numPoints-1)*4);
+ rlBegin(RL_QUADS);
+ rlColor4ub(colour.r, colour.g, colour.b, colour.a);
+
+ for (int i = 0; i < numPoints-1; i++)
+ {
+ rlTexCoord2f(0.5, 0.5);
+ rlVertex2f(x, y);
+
+ rlTexCoord2f(tPnts[i].x, tPnts[i].y);
+ rlVertex2f(points[i].x + x, points[i].y + y);
+
+ rlTexCoord2f(tPnts[i + 1].x, tPnts[i + 1].y);
+ rlVertex2f(points[i + 1].x + x, points[i + 1].y + y);
+
+ rlTexCoord2f(tPnts[i + 1].x, tPnts[i + 1].y);
+ rlVertex2f(points[i + 1].x + x, points[i + 1].y + y);
+ }
+ rlEnd();
+ rlDisableTexture();
+
+}
+
+
// Returns color with alpha applied, alpha goes from 0.0f to 1.0f
Color Fade(Color color, float alpha)
{