summaryrefslogtreecommitdiffhomepage
path: root/examples/textures
diff options
context:
space:
mode:
authorRay <[email protected]>2019-05-06 16:38:58 +0200
committerRay <[email protected]>2019-05-06 16:38:58 +0200
commit621965cb8cbb743820dd66bdde61fc3c79b156a8 (patch)
tree1cfbdf38c6ff775675999d33487327107d887a6a /examples/textures
parent8bafe03ee001b7af6ab638941683f42feb1aef65 (diff)
downloadraylib-621965cb8cbb743820dd66bdde61fc3c79b156a8.tar.gz
raylib-621965cb8cbb743820dd66bdde61fc3c79b156a8.zip
Move bunnymark example to another module
Diffstat (limited to 'examples/textures')
-rw-r--r--examples/textures/resources/wabbit_alpha.pngbin0 -> 561 bytes
-rw-r--r--examples/textures/textures_bunnymark.c116
2 files changed, 116 insertions, 0 deletions
diff --git a/examples/textures/resources/wabbit_alpha.png b/examples/textures/resources/wabbit_alpha.png
new file mode 100644
index 00000000..1a5eb0b4
--- /dev/null
+++ b/examples/textures/resources/wabbit_alpha.png
Binary files differ
diff --git a/examples/textures/textures_bunnymark.c b/examples/textures/textures_bunnymark.c
new file mode 100644
index 00000000..76078838
--- /dev/null
+++ b/examples/textures/textures_bunnymark.c
@@ -0,0 +1,116 @@
+/*******************************************************************************************
+*
+* raylib [textures] example - Bunnymark
+*
+* 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)
+*
+* Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
+*
+********************************************************************************************/
+
+#include "raylib.h"
+
+#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 {
+ Vector2 position;
+ Vector2 speed;
+ Color color;
+} Bunny;
+
+int main()
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ const int screenWidth = 800;
+ const int screenHeight = 450;
+
+ InitWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark");
+
+ Texture2D texBunny = LoadTexture("resources/wabbit_alpha.png");
+
+ Bunny *bunnies = (Bunny *)malloc(MAX_BUNNIES*sizeof(Bunny)); // Bunnies array
+
+ int bunniesCount = 0; // Bunnies counter
+
+ SetTargetFPS(60);
+ //--------------------------------------------------------------------------------------
+
+ // Main game loop
+ while (!WindowShouldClose()) // Detect window close button or ESC key
+ {
+ // Update
+ //----------------------------------------------------------------------------------
+ if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
+ {
+ // Create more bunnies
+ for (int i = 0; i < 100; i++)
+ {
+ bunnies[bunniesCount].position = GetMousePosition();
+ 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++;
+ }
+ }
+
+ // Update bunnies
+ for (int i = 0; i < bunniesCount; i++)
+ {
+ bunnies[i].position.x += bunnies[i].speed.x;
+ bunnies[i].position.y += bunnies[i].speed.y;
+
+ 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;
+ }
+ //----------------------------------------------------------------------------------
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(RAYWHITE);
+
+ for (int i = 0; i < bunniesCount; i++)
+ {
+ // 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, 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();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+ free(bunnies); // Unload bunnies data array
+
+ UnloadTexture(texBunny); // Unload bunny texture
+
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+}