Browse Source

Move bunnymark example to another module

Ray 6 years ago
parent
commit
621965cb8c

BIN
examples/others/resources/wabbit_alpha.png


BIN
examples/textures/resources/wabbit_alpha.png


+ 37 - 22
examples/others/bunnymark.c → examples/textures/textures_bunnymark.c

@@ -1,18 +1,23 @@
 /*******************************************************************************************
 /*******************************************************************************************
 *
 *
-*   raylib example - Bunnymark
+*   raylib [textures] example - Bunnymark
 *
 *
 *   This example has been created using raylib 1.6 (www.raylib.com)
 *   This example has been created using raylib 1.6 (www.raylib.com)
 *   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
 *   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
 *
 *
-*   Copyright (c) 2014 Ramon Santamaria (@raysan5)
+*   Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
 *
 *
 ********************************************************************************************/
 ********************************************************************************************/
 
 
 #include "raylib.h"
 #include "raylib.h"
-#include <stdlib.h>     // Required for: malloc(), free()
 
 
-#define MAX_BUNNIES     100000       // 100K bunnies
+#include <stdlib.h>                 // Required for: malloc(), free()
+
+#define MAX_BUNNIES       100000    // 100K bunnies limit
+
+// This is the maximum amount of elements (quads) per batch
+// NOTE: This value is defined in [rlgl] module and can be changed there
+#define MAX_BATCH_ELEMENTS  8192
 
 
 typedef struct Bunny {
 typedef struct Bunny {
     Vector2 position;
     Vector2 position;
@@ -24,10 +29,10 @@ int main()
 {
 {
     // Initialization
     // Initialization
     //--------------------------------------------------------------------------------------
     //--------------------------------------------------------------------------------------
-    int screenWidth = 1280;
-    int screenHeight = 960;
+    const int screenWidth = 800;
+    const int screenHeight = 450;
 
 
-    InitWindow(screenWidth, screenHeight, "raylib example - Bunnymark");
+    InitWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark");
 
 
     Texture2D texBunny = LoadTexture("resources/wabbit_alpha.png");
     Texture2D texBunny = LoadTexture("resources/wabbit_alpha.png");
 
 
@@ -49,8 +54,11 @@ int main()
             for (int i = 0; i < 100; i++)
             for (int i = 0; i < 100; i++)
             {
             {
                 bunnies[bunniesCount].position = GetMousePosition();
                 bunnies[bunniesCount].position = GetMousePosition();
-                bunnies[bunniesCount].speed.x = (float)GetRandomValue(250, 500)/60.0f;
-                bunnies[bunniesCount].speed.y = (float)(GetRandomValue(250, 500) - 500)/60.0f;
+                bunnies[bunniesCount].speed.x = (float)GetRandomValue(-250, 250)/60.0f;
+                bunnies[bunniesCount].speed.y = (float)GetRandomValue(-250, 250)/60.0f;
+                bunnies[bunniesCount].color = (Color){ GetRandomValue(50, 240), 
+                                                       GetRandomValue(80, 240), 
+                                                       GetRandomValue(100, 240), 255 };
                 bunniesCount++;
                 bunniesCount++;
             }
             }
         }
         }
@@ -61,8 +69,10 @@ int main()
             bunnies[i].position.x += bunnies[i].speed.x;
             bunnies[i].position.x += bunnies[i].speed.x;
             bunnies[i].position.y += bunnies[i].speed.y;
             bunnies[i].position.y += bunnies[i].speed.y;
 
 
-            if ((bunnies[i].position.x > GetScreenWidth()) || (bunnies[i].position.x < 0)) bunnies[i].speed.x *= -1;
-            if ((bunnies[i].position.y > GetScreenHeight()) || (bunnies[i].position.y < 0)) bunnies[i].speed.y *= -1;
+            if (((bunnies[i].position.x + texBunny.width/2) > GetScreenWidth()) || 
+                ((bunnies[i].position.x + texBunny.width/2) < 0)) bunnies[i].speed.x *= -1;
+            if (((bunnies[i].position.y + texBunny.height/2) > GetScreenHeight()) || 
+                ((bunnies[i].position.y + texBunny.height/2 - 40) < 0)) bunnies[i].speed.y *= -1;
         }
         }
         //----------------------------------------------------------------------------------
         //----------------------------------------------------------------------------------
 
 
@@ -74,17 +84,20 @@ int main()
 
 
             for (int i = 0; i < bunniesCount; i++)
             for (int i = 0; i < bunniesCount; i++)
             {
             {
-                // NOTE: When internal QUADS batch limit is reached, a draw call is launched and
-                // batching buffer starts being filled again; before launching the draw call,
-                // updated vertex data from internal buffer is send to GPU... it seems it generates
-                // a stall and consequently a frame drop, limiting number of bunnies drawn at 60 fps
-                DrawTexture(texBunny, bunnies[i].position.x, bunnies[i].position.y, RAYWHITE);
+                // NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS), 
+                // a draw call is launched and buffer starts being filled again;
+                // before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU... 
+                // Process of sending data is costly and it could happen that GPU data has not been completely
+                // processed for drawing while new data is tried to be sent (updating current in-use buffers)
+                // it could generates a stall and consequently a frame drop, limiting the number of drawn bunnies
+                DrawTexture(texBunny, bunnies[i].position.x, bunnies[i].position.y, bunnies[i].color);
             }
             }
 
 
-            DrawRectangle(0, 0, screenWidth, 40, LIGHTGRAY);
-            DrawText("raylib bunnymark", 10, 10, 20, DARKGRAY);
-            DrawText(FormatText("bunnies: %i", bunniesCount), 400, 10, 20, RED);
-            DrawFPS(260, 10);
+            DrawRectangle(0, 0, screenWidth, 40, BLACK);
+            DrawText(FormatText("bunnies: %i", bunniesCount), 120, 10, 20, GREEN);
+            DrawText(FormatText("batched draw calls: %i", 1 + bunniesCount/MAX_BATCH_ELEMENTS), 320, 10, 20, MAROON);
+            
+            DrawFPS(10, 10);
 
 
         EndDrawing();
         EndDrawing();
         //----------------------------------------------------------------------------------
         //----------------------------------------------------------------------------------
@@ -92,9 +105,11 @@ int main()
 
 
     // De-Initialization
     // De-Initialization
     //--------------------------------------------------------------------------------------
     //--------------------------------------------------------------------------------------
-    free(bunnies);
+    free(bunnies);              // Unload bunnies data array
+    
+    UnloadTexture(texBunny);    // Unload bunny texture
 
 
-    CloseWindow();        // Close window and OpenGL context
+    CloseWindow();              // Close window and OpenGL context
     //--------------------------------------------------------------------------------------
     //--------------------------------------------------------------------------------------
 
 
     return 0;
     return 0;