From ccad59a17a80011c69a69d5398a92f01900f8ef1 Mon Sep 17 00:00:00 2001 From: codifies Date: Wed, 24 Mar 2021 21:23:58 +0000 Subject: [PATCH] adds DrawTexturedPoly with example --- examples/Makefile | 3 ++- src/raylib.h | 1 + src/shapes.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/examples/Makefile b/examples/Makefile index ac42ea986..cabbc99a6 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -416,7 +416,8 @@ SHAPES = \ shapes/shapes_easings_rectangle_array \ shapes/shapes_draw_ring \ shapes/shapes_draw_circle_sector \ - shapes/shapes_draw_rectangle_rounded + shapes/shapes_draw_rectangle_rounded \ + shapes/shapes_textured_polygon TEXTURES = \ textures/textures_logo_raylib \ diff --git a/src/raylib.h b/src/raylib.h index d02ec4b33..99fce14e9 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1147,6 +1147,7 @@ RLAPI void DrawTriangleFan(Vector2 *points, int pointsCount, Color color); RLAPI void DrawTriangleStrip(Vector2 *points, int pointsCount, Color color); // Draw a triangle strip defined by points RLAPI void DrawPoly(Vector2 center, int sides, float radius, float rotation, Color color); // Draw a regular polygon (Vector version) RLAPI void DrawPolyLines(Vector2 center, int sides, float radius, float rotation, Color color); // Draw a polygon outline of n sides +RLAPI void DrawTexturedPoly(Texture t, float x, float y, Vector2 *points, Vector2 *tPnts, int numPoints, Color colour); // Draw a textured polygon // Basic shapes collision detection functions RLAPI bool CheckCollisionRecs(Rectangle rec1, Rectangle rec2); // Check collision between two rectangles diff --git a/src/shapes.c b/src/shapes.c index b615f78af..e8974662f 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -1419,6 +1419,48 @@ void DrawPolyLines(Vector2 center, int sides, float radius, float rotation, Colo rlPopMatrix(); } + +// 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 DrawTexturedPoly(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(); + +} + //---------------------------------------------------------------------------------- // Module Functions Definition - Collision Detection functions //----------------------------------------------------------------------------------