1/*******************************************************************************************
2*
3* raylib [textures] example - Bunnymark
4*
5* This example has been created using raylib 1.6 (www.raylib.com)
6* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
7*
8* Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
9*
10********************************************************************************************/
11
12#include "raylib.h"
13
14#include <stdlib.h> // Required for: malloc(), free()
15
16#define MAX_BUNNIES 50000 // 50K bunnies limit
17
18// This is the maximum amount of elements (quads) per batch
19// NOTE: This value is defined in [rlgl] module and can be changed there
20#define MAX_BATCH_ELEMENTS 8192
21
22typedef struct Bunny {
23 Vector2 position;
24 Vector2 speed;
25 Color color;
26} Bunny;
27
28int main(void)
29{
30 // Initialization
31 //--------------------------------------------------------------------------------------
32 const int screenWidth = 800;
33 const int screenHeight = 450;
34
35 InitWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark");
36
37 // Load bunny texture
38 Texture2D texBunny = LoadTexture("resources/wabbit_alpha.png");
39
40 Bunny *bunnies = (Bunny *)malloc(MAX_BUNNIES*sizeof(Bunny)); // Bunnies array
41
42 int bunniesCount = 0; // Bunnies counter
43
44 SetTargetFPS(60); // Set our game to run at 60 frames-per-second
45 //--------------------------------------------------------------------------------------
46
47 // Main game loop
48 while (!WindowShouldClose()) // Detect window close button or ESC key
49 {
50 // Update
51 //----------------------------------------------------------------------------------
52 if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
53 {
54 // Create more bunnies
55 for (int i = 0; i < 100; i++)
56 {
57 if (bunniesCount < MAX_BUNNIES)
58 {
59 bunnies[bunniesCount].position = GetMousePosition();
60 bunnies[bunniesCount].speed.x = (float)GetRandomValue(-250, 250)/60.0f;
61 bunnies[bunniesCount].speed.y = (float)GetRandomValue(-250, 250)/60.0f;
62 bunnies[bunniesCount].color = (Color){ GetRandomValue(50, 240),
63 GetRandomValue(80, 240),
64 GetRandomValue(100, 240), 255 };
65 bunniesCount++;
66 }
67 }
68 }
69
70 // Update bunnies
71 for (int i = 0; i < bunniesCount; i++)
72 {
73 bunnies[i].position.x += bunnies[i].speed.x;
74 bunnies[i].position.y += bunnies[i].speed.y;
75
76 if (((bunnies[i].position.x + texBunny.width/2) > GetScreenWidth()) ||
77 ((bunnies[i].position.x + texBunny.width/2) < 0)) bunnies[i].speed.x *= -1;
78 if (((bunnies[i].position.y + texBunny.height/2) > GetScreenHeight()) ||
79 ((bunnies[i].position.y + texBunny.height/2 - 40) < 0)) bunnies[i].speed.y *= -1;
80 }
81 //----------------------------------------------------------------------------------
82
83 // Draw
84 //----------------------------------------------------------------------------------
85 BeginDrawing();
86
87 ClearBackground(RAYWHITE);
88
89 for (int i = 0; i < bunniesCount; i++)
90 {
91 // NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS),
92 // a draw call is launched and buffer starts being filled again;
93 // before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU...
94 // Process of sending data is costly and it could happen that GPU data has not been completely
95 // processed for drawing while new data is tried to be sent (updating current in-use buffers)
96 // it could generates a stall and consequently a frame drop, limiting the number of drawn bunnies
97 DrawTexture(texBunny, bunnies[i].position.x, bunnies[i].position.y, bunnies[i].color);
98 }
99
100 DrawRectangle(0, 0, screenWidth, 40, BLACK);
101 DrawText(FormatText("bunnies: %i", bunniesCount), 120, 10, 20, GREEN);
102 DrawText(FormatText("batched draw calls: %i", 1 + bunniesCount/MAX_BATCH_ELEMENTS), 320, 10, 20, MAROON);
103
104 DrawFPS(10, 10);
105
106 EndDrawing();
107 //----------------------------------------------------------------------------------
108 }
109
110 // De-Initialization
111 //--------------------------------------------------------------------------------------
112 free(bunnies); // Unload bunnies data array
113
114 UnloadTexture(texBunny); // Unload bunny texture
115
116 CloseWindow(); // Close window and OpenGL context
117 //--------------------------------------------------------------------------------------
118
119 return 0;
120}
121