2
0
Эх сурвалжийг харах

REVIEWED: DrawTexturePoly()

raysan5 4 жил өмнө
parent
commit
bc6b16beb2

+ 39 - 38
examples/textures/textures_poly.c

@@ -2,17 +2,21 @@
 *
 *   raylib [shapes] example - Draw Textured Polygon
 *
-*   This example has been created using raylib 99.98 (www.raylib.com)
+*   This example has been created using raylib 3.7 (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)
+*   Example contributed by Chris Camacho (@codifies - bedroomcoders.co.uk) and 
+*   reviewed by Ramon Santamaria (@raysan5)
+*
+*   Copyright (c) 2021 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
 *
 ********************************************************************************************/
 
 #include "raylib.h"
 #include "raymath.h"
 
+#define MAX_POINTS  11  // 10 points and back to the start
+
 int main(void)
 {
     // Initialization
@@ -20,54 +24,51 @@ int main(void)
     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 texcoords[MAX_POINTS] = {
+        (Vector2){ 0.75f, 0.0f },
+        (Vector2){ 0.25f, 0.0f },
+        (Vector2){ 0.0f, 0.5f },
+        (Vector2){ 0.0f, 0.75f },
+        (Vector2){ 0.25f, 1.0f},
+        (Vector2){ 0.375f, 0.875f},
+        (Vector2){ 0.625f, 0.875f},
+        (Vector2){ 0.75f, 1.0f},
+        (Vector2){ 1.0f, 0.75f},
+        (Vector2){ 1.0f, 0.5f},
+        (Vector2){ 0.75f, 0.0f}  // Close the poly
     };
 
-    Vector2 pnts[numPnts];
+    Vector2 points[MAX_POINTS] = { 0 };
 
-    // create the poly coords from the UV's
+    // 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++)
+    for (int i = 0; i < MAX_POINTS; i++)
     {
-        pnts[i].x = (tPnts[i].x - 0.5) * 256.0;
-        pnts[i].y = (tPnts[i].y - 0.5) * 256.0;
+        points[i].x = (texcoords[i].x - 0.5f)*256.0f;
+        points[i].y = (texcoords[i].y - 0.5f)*256.0f;
     }
 
-    InitWindow(screenWidth, screenHeight, "raylib [textures] example - Textured Polygon");
+    InitWindow(screenWidth, screenHeight, "raylib [textures] example - textured polygon");
 
-    Texture tex = LoadTexture("resources/cat.png");
+    Texture texture = LoadTexture("resources/cat.png");
+    
+    float ang = 0;
 
     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);
-        }
+        Vector2 positions[MAX_POINTS] = { 0 };
+        
+        for (int i = 0; i < MAX_POINTS; i++) positions[i] = Vector2Rotate(points[i], ang);
+        //----------------------------------------------------------------------------------
 
         // Draw
         //----------------------------------------------------------------------------------
@@ -75,20 +76,20 @@ int main(void)
 
             ClearBackground(RAYWHITE);
 
-            DrawText("Textured Polygon", 20, 20, 20, DARKGRAY);
+            DrawText("textured polygon", 20, 20, 20, DARKGRAY);
             
-            DrawTexturePoly(tex, screenWidth/2, screenHeight/2,
-                                dPnts, tPnts, numPnts, WHITE);
+            DrawTexturePoly(texture, (Vector2){ GetScreenWidth()/2, GetScreenHeight()/2 }, 
+                            positions, texcoords, MAX_POINTS, WHITE);
 
         EndDrawing();
         //----------------------------------------------------------------------------------
     }
-    
-    UnloadTexture(tex);
 
     // De-Initialization
     //--------------------------------------------------------------------------------------
-    CloseWindow();        // Close window and OpenGL context
+    UnloadTexture(texture); // Unload texture
+    
+    CloseWindow();          // Close window and OpenGL context
     //--------------------------------------------------------------------------------------
 
     return 0;

+ 5 - 5
src/raylib.h

@@ -1266,12 +1266,12 @@ RLAPI void SetTextureWrap(Texture2D texture, int wrap);
 RLAPI void DrawTexture(Texture2D texture, int posX, int posY, Color tint);                               // Draw a Texture2D
 RLAPI void DrawTextureV(Texture2D texture, Vector2 position, Color tint);                                // Draw a Texture2D with position defined as Vector2
 RLAPI void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float scale, Color tint);  // Draw a Texture2D with extended parameters
-RLAPI void DrawTextureRec(Texture2D texture, Rectangle source, Vector2 position, Color tint);         // Draw a part of a texture defined by a rectangle
+RLAPI void DrawTextureRec(Texture2D texture, Rectangle source, Vector2 position, Color tint);            // Draw a part of a texture defined by a rectangle
 RLAPI void DrawTextureQuad(Texture2D texture, Vector2 tiling, Vector2 offset, Rectangle quad, Color tint);  // Draw texture quad with tiling and offset parameters
-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
+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(Texture2D texture, Vector2 center, Vector2 *points, Vector2 *texcoords, int pointsCount, Color tint);      // 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

+ 22 - 31
src/textures.c

@@ -3510,48 +3510,39 @@ 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)
+// Draw textured polygon, defined by vertex and texturecoordinates
+// NOTE: Polygon center must have straight line path to all points 
+// without crossing perimeter, points must be in anticlockwise order
+void DrawTexturePoly(Texture2D texture, Vector2 center, Vector2 *points, Vector2 *texcoords, int pointsCount, Color tint)
 {
-    rlEnableTexture(t.id);
+    rlCheckRenderBatchLimit((pointsCount - 1)*4);
+    
+    rlSetTexture(texture.id);
 
-    // for some reason texturing doesn't work on trianglesso make a 
-    // degenerate QUAD, DrawTriangleFan does this too why ?
-    rlCheckRenderBatchLimit((numPoints-1)*4);
+    // Texturing is only supported on QUADs
     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);
+        rlColor4ub(tint.r, tint.g, tint.b, tint.a);
 
-        rlTexCoord2f(tPnts[i].x, tPnts[i].y);
-        rlVertex2f(points[i].x + x, points[i].y + y);
+        for (int i = 0; i < pointsCount - 1; i++)
+        {
+            rlTexCoord2f(0.5f, 0.5f);
+            rlVertex2f(center.x, center.y);
 
-        rlTexCoord2f(tPnts[i + 1].x, tPnts[i + 1].y);
-        rlVertex2f(points[i + 1].x + x, points[i + 1].y + y);
+            rlTexCoord2f(texcoords[i].x, texcoords[i].y);
+            rlVertex2f(points[i].x + center.x, points[i].y + center.y);
 
-        rlTexCoord2f(tPnts[i + 1].x, tPnts[i + 1].y);
-        rlVertex2f(points[i + 1].x + x, points[i + 1].y + y);
-    }
+            rlTexCoord2f(texcoords[i + 1].x, texcoords[i + 1].y);
+            rlVertex2f(points[i + 1].x + center.x, points[i + 1].y + center.y);
+
+            rlTexCoord2f(texcoords[i + 1].x, texcoords[i + 1].y);
+            rlVertex2f(points[i + 1].x + center.x, points[i + 1].y + center.y);
+        }
     rlEnd();
-    rlDisableTexture();
 
+    rlSetTexture(0);
 }
 
-
 // Returns color with alpha applied, alpha goes from 0.0f to 1.0f
 Color Fade(Color color, float alpha)
 {