2
0
raysan5 4 жил өмнө
parent
commit
bab1b9c1c5

+ 0 - 0
examples/textures/resources/pat.png → examples/textures/resources/patterns.png


+ 61 - 44
examples/textures/textures_draw_tiled.c

@@ -11,9 +11,9 @@
 #include "raylib.h"
 
 #define SIZEOF(A) (sizeof(A)/sizeof(A[0]))
-#define OPT_WIDTH 220  // max width for the options container
-#define MARGIN_SIZE 8  // size for the margins
-#define COLOR_SIZE 16  // size of the color select buttons
+#define OPT_WIDTH       220       // Max width for the options container
+#define MARGIN_SIZE       8       // Size for the margins
+#define COLOR_SIZE       16       // Size of the color select buttons
 
 int main(int argc, char **argv)
 {
@@ -26,31 +26,41 @@ int main(int argc, char **argv)
     InitWindow(screenWidth, screenHeight, "raylib [textures] example - Draw part of a texture tiled");
 
     // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
-    Texture ptex = LoadTexture("resources/pat.png");
-    SetTextureFilter(ptex, FILTER_TRILINEAR); // Makes the texture smoother when upscaled
+    Texture texPattern = LoadTexture("resources/patterns.png");
+    SetTextureFilter(texPattern, FILTER_TRILINEAR); // Makes the texture smoother when upscaled
     
     // Coordinates for all patterns inside the texture
-    const Rectangle patRec[] = { (Rectangle){3,3,66,66}, (Rectangle){75,3,100,100}, 
-            (Rectangle){3,75,66,66}, (Rectangle){7,156,50,50}, (Rectangle){85,106,90,45}, (Rectangle){75,154,100,60} };
+    const Rectangle recPattern[] = { 
+        (Rectangle){ 3, 3, 66, 66 }, 
+        (Rectangle){ 75, 3, 100, 100 }, 
+        (Rectangle){ 3, 75, 66, 66 },
+        (Rectangle){ 7, 156, 50, 50 },
+        (Rectangle){ 85, 106, 90, 45 },
+        (Rectangle){ 75, 154, 100, 60}
+    };
     
     // Setup colors
-    const Color colors[] = { BLACK, MAROON, ORANGE, BLUE, PURPLE, BEIGE, LIME, RED, DARKGRAY, SKYBLUE};
-    enum {MAX_COLORS = SIZEOF(colors)};
+    const Color colors[] = { BLACK, MAROON, ORANGE, BLUE, PURPLE, BEIGE, LIME, RED, DARKGRAY, SKYBLUE };
+    enum { MAX_COLORS = SIZEOF(colors) };
     Rectangle colorRec[MAX_COLORS] = { 0 };
 
     // Calculate rectangle for each color
-    for(int i=0, x=0, y=0; i<MAX_COLORS; i++) {
-        colorRec[i].x =  2+MARGIN_SIZE + x;
-        colorRec[i].y =  22+256+MARGIN_SIZE + y;
+    for (int i = 0, x = 0, y = 0; i < MAX_COLORS; i++)
+    {
+        colorRec[i].x = 2 + MARGIN_SIZE + x;
+        colorRec[i].y = 22 + 256 + MARGIN_SIZE + y;
         colorRec[i].width = COLOR_SIZE*2;
         colorRec[i].height = COLOR_SIZE;
-        if(i == MAX_COLORS/2 - 1) {
-            x = 0; y += COLOR_SIZE + MARGIN_SIZE;
-        } else x += COLOR_SIZE * 2 + MARGIN_SIZE; 
         
+        if (i == (MAX_COLORS/2 - 1))
+        {
+            x = 0; 
+            y += COLOR_SIZE + MARGIN_SIZE;
+        } 
+        else x += (COLOR_SIZE*2 + MARGIN_SIZE);
     }
 
-    int activePat = 0, activeCol = 0;
+    int activePattern = 0, activeCol = 0;
     float scale = 1.0f, rotation = 0.0f;
     
     SetTargetFPS(60);
@@ -65,20 +75,25 @@ int main(int argc, char **argv)
         screenHeight = GetScreenHeight();
         
         // Handle mouse
-        if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
+        if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
+        {
             const Vector2 mouse = GetMousePosition();
             
             // Check which pattern was clicked and set it as the active pattern
-            for(int i=0; i<SIZEOF(patRec); ++i) {
-                if(CheckCollisionPointRec(mouse, (Rectangle){2+MARGIN_SIZE+patRec[i].x, 40+MARGIN_SIZE+patRec[i].y,patRec[i].width, patRec[i].height})) { 
-                    activePat = i; 
+            for (int i = 0; i < SIZEOF(recPattern); i++)
+            {
+                if (CheckCollisionPointRec(mouse, (Rectangle){ 2 + MARGIN_SIZE + recPattern[i].x, 40 + MARGIN_SIZE + recPattern[i].y, recPattern[i].width, recPattern[i].height }))
+                { 
+                    activePattern = i; 
                     break; 
                 }
             }
             
             // Check to see which color was clicked and set it as the active color
-            for(int i=0; i<MAX_COLORS; ++i) {
-                if(CheckCollisionPointRec(mouse, colorRec[i])) {
+            for (int i = 0; i < MAX_COLORS; ++i)
+            {
+                if (CheckCollisionPointRec(mouse, colorRec[i]))
+                {
                     activeCol = i;
                     break;
                 }
@@ -88,17 +103,17 @@ int main(int argc, char **argv)
         // Handle keys
         
         // Change scale
-        if(IsKeyPressed(KEY_UP)) scale += 0.25f;
-        if(IsKeyPressed(KEY_DOWN)) scale -= 0.25f;
-        if(scale > 10.0f) scale = 10.0f;
-        else if( scale <= 0.0f) scale = 0.25f;
+        if (IsKeyPressed(KEY_UP)) scale += 0.25f;
+        if (IsKeyPressed(KEY_DOWN)) scale -= 0.25f;
+        if (scale > 10.0f) scale = 10.0f;
+        else if ( scale <= 0.0f) scale = 0.25f;
         
         // Change rotation
-        if(IsKeyPressed(KEY_LEFT)) rotation -= 25.0f;
-        if(IsKeyPressed(KEY_RIGHT)) rotation += 25.0f;
+        if (IsKeyPressed(KEY_LEFT)) rotation -= 25.0f;
+        if (IsKeyPressed(KEY_RIGHT)) rotation += 25.0f;
         
         // Reset
-        if(IsKeyPressed(KEY_SPACE)) { rotation = 0.0f; scale = 1.0f; }
+        if (IsKeyPressed(KEY_SPACE)) { rotation = 0.0f; scale = 1.0f; }
         //----------------------------------------------------------------------------------
         
         // Draw
@@ -107,40 +122,42 @@ int main(int argc, char **argv)
             ClearBackground(RAYWHITE);
             
             // Draw the tiled area
-            DrawTextureTiled(ptex, patRec[activePat], (Rectangle){OPT_WIDTH+MARGIN_SIZE, MARGIN_SIZE, screenWidth - OPT_WIDTH - 2*MARGIN_SIZE, screenHeight - 2*MARGIN_SIZE},
+            DrawTextureTiled(texPattern, recPattern[activePattern], (Rectangle){OPT_WIDTH+MARGIN_SIZE, MARGIN_SIZE, screenWidth - OPT_WIDTH - 2*MARGIN_SIZE, screenHeight - 2*MARGIN_SIZE},
                 (Vector2){0.0f, 0.0f}, rotation, scale, colors[activeCol]);
             
             // Draw options
-            DrawRectangle(MARGIN_SIZE, MARGIN_SIZE, OPT_WIDTH - MARGIN_SIZE, screenHeight-2*MARGIN_SIZE, ColorAlpha(LIGHTGRAY, 0.5f));
+            DrawRectangle(MARGIN_SIZE, MARGIN_SIZE, OPT_WIDTH - MARGIN_SIZE, screenHeight - 2*MARGIN_SIZE, ColorAlpha(LIGHTGRAY, 0.5f));
             
-            DrawText("Select Pattern", 2+MARGIN_SIZE, 30+MARGIN_SIZE, 10, BLACK);
-            DrawTexture(ptex, 2+MARGIN_SIZE, 40+MARGIN_SIZE, BLACK);
-            DrawRectangle(2+MARGIN_SIZE + patRec[activePat].x, 40+MARGIN_SIZE+patRec[activePat].y,patRec[activePat].width, patRec[activePat].height, ColorAlpha(DARKBLUE, 0.3f));
+            DrawText("Select Pattern", 2 + MARGIN_SIZE, 30 + MARGIN_SIZE, 10, BLACK);
+            DrawTexture(texPattern, 2 + MARGIN_SIZE, 40 + MARGIN_SIZE, BLACK);
+            DrawRectangle(2 + MARGIN_SIZE + recPattern[activePattern].x, 40 + MARGIN_SIZE + recPattern[activePattern].y, recPattern[activePattern].width, recPattern[activePattern].height, ColorAlpha(DARKBLUE, 0.3f));
             
             DrawText("Select Color", 2+MARGIN_SIZE, 10+256+MARGIN_SIZE, 10, BLACK);
-            for(int i=0; i<MAX_COLORS; ++i) {
+            for (int i = 0; i < MAX_COLORS; i++)
+            {
                 DrawRectangleRec(colorRec[i], colors[i]);
-                if(activeCol == i) DrawRectangleLinesEx(colorRec[i], 3.0f, ColorAlpha(WHITE, 0.5f));
+                if (activeCol == i) DrawRectangleLinesEx(colorRec[i], 3.0f, ColorAlpha(WHITE, 0.5f));
             }
             
-            DrawText("Scale (UP/DOWN to change)", 2+MARGIN_SIZE, 80+256+MARGIN_SIZE, 10, BLACK);
-            DrawText(TextFormat("%.2fx", scale), 2+MARGIN_SIZE, 92+256+MARGIN_SIZE, 20, BLACK);
+            DrawText("Scale (UP/DOWN to change)", 2 + MARGIN_SIZE, 80 + 256 + MARGIN_SIZE, 10, BLACK);
+            DrawText(TextFormat("%.2fx", scale), 2 + MARGIN_SIZE, 92 + 256 + MARGIN_SIZE, 20, BLACK);
             
-            DrawText("Rotation (LEFT/RIGHT to change)", 2+MARGIN_SIZE, 122+256+MARGIN_SIZE, 10, BLACK);
-            DrawText(TextFormat("%.0f degrees", rotation), 2+MARGIN_SIZE, 134+256+MARGIN_SIZE, 20, BLACK);
+            DrawText("Rotation (LEFT/RIGHT to change)", 2 + MARGIN_SIZE, 122 + 256 + MARGIN_SIZE, 10, BLACK);
+            DrawText(TextFormat("%.0f degrees", rotation), 2 + MARGIN_SIZE, 134 + 256 + MARGIN_SIZE, 20, BLACK);
             
-            DrawText("Press [SPACE] to reset",  2+MARGIN_SIZE, 164+256+MARGIN_SIZE, 10, DARKBLUE);
+            DrawText("Press [SPACE] to reset", 2 + MARGIN_SIZE, 164 + 256 + MARGIN_SIZE, 10, DARKBLUE);
             
             // Draw FPS
-            DrawText(TextFormat("%i FPS", GetFPS()), 2+MARGIN_SIZE, 2+MARGIN_SIZE, 20, BLACK);
+            DrawText(TextFormat("%i FPS", GetFPS()), 2 + MARGIN_SIZE, 2 + MARGIN_SIZE, 20, BLACK);
         EndDrawing();
         //----------------------------------------------------------------------------------
     }
     
     // De-Initialization
     //--------------------------------------------------------------------------------------
-    UnloadTexture(ptex);
-    CloseWindow();                // Close window and OpenGL context
+    UnloadTexture(texPattern);        // Unload texture
+    
+    CloseWindow();              // Close window and OpenGL context
     //--------------------------------------------------------------------------------------
     
 	return 0;