From 90f3f870c29bfb0ddc1aaecb7011c3e977f56373 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 22 May 2017 20:47:28 +0200 Subject: [PATCH] Added bunnymark example --- examples/others/bunnymark.c | 102 +++++++++++++++++++++ examples/others/resources/wabbit_alpha.png | Bin 0 -> 449 bytes 2 files changed, 102 insertions(+) create mode 100644 examples/others/bunnymark.c create mode 100644 examples/others/resources/wabbit_alpha.png diff --git a/examples/others/bunnymark.c b/examples/others/bunnymark.c new file mode 100644 index 00000000..1ada54db --- /dev/null +++ b/examples/others/bunnymark.c @@ -0,0 +1,102 @@ +/******************************************************************************************* +* +* raylib 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 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#include // Required for: malloc(), free() + +#define MAX_BUNNIES 100000 // 100K bunnies + +typedef struct Bunny { + Vector2 position; + Vector2 speed; + Color color; +} Bunny; + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 1280; + int screenHeight = 960; + + InitWindow(screenWidth, screenHeight, "raylib 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, 500)/60.0f; + bunnies[bunniesCount].speed.y = (float)(GetRandomValue(250, 500) - 500)/60.0f; + 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 > 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; + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + 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); + } + + 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); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + free(bunnies); + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} \ No newline at end of file diff --git a/examples/others/resources/wabbit_alpha.png b/examples/others/resources/wabbit_alpha.png new file mode 100644 index 0000000000000000000000000000000000000000..79c31675083b7ffc272a6370bc189360bde484d0 GIT binary patch literal 449 zcmV;y0Y3hTP)SOJFMz`0@qn1GK$9W|wOohG3Lai}FkWLtiT<2<{JbM8>W7|R$! zUuNqg#gmKmsTnILy&K;zJjd^_40xy)%EA1>FWWk~LFpr>Et|!Hv(V>Hs9!2xEQJ>~!qz76HLsxFXdG zs5~GjUDeD6dXvG$_#HtoUok|M-X@BSTy{VK4P?TJZcz}O&FVzD0fZu8Yajr@?2aYL r_Q@_>J=ju&>AqoA-=wzwF98MsNX4