raylib/examples/textures/textures_bunnymark.c

117 lines
4.8 KiB
C
Raw Normal View History

2017-05-22 21:47:28 +03:00
/*******************************************************************************************
*
* raylib [textures] example - Bunnymark
2017-05-22 21:47:28 +03:00
*
* 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)
2017-05-22 21:47:28 +03:00
*
********************************************************************************************/
#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
2017-05-22 21:47:28 +03:00
typedef struct Bunny {
Vector2 position;
Vector2 speed;
Color color;
} Bunny;
int main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
2017-05-22 21:47:28 +03:00
InitWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark");
2017-05-22 21:47:28 +03:00
Texture2D texBunny = LoadTexture("resources/wabbit_alpha.png");
2017-05-22 21:47:28 +03:00
Bunny *bunnies = (Bunny *)malloc(MAX_BUNNIES*sizeof(Bunny)); // Bunnies array
2017-05-22 21:47:28 +03:00
int bunniesCount = 0; // Bunnies counter
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
2017-05-22 21:47:28 +03:00
// 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 };
2017-05-22 21:47:28 +03:00
bunniesCount++;
}
}
2017-05-22 21:47:28 +03:00
// 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;
2017-05-22 21:47:28 +03:00
}
//----------------------------------------------------------------------------------
2017-05-22 21:47:28 +03:00
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
for (int i = 0; i < bunniesCount; i++)
2017-05-22 21:47:28 +03:00
{
// 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);
2017-05-22 21:47:28 +03:00
}
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);
2017-05-22 21:47:28 +03:00
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
free(bunnies); // Unload bunnies data array
UnloadTexture(texBunny); // Unload bunny texture
CloseWindow(); // Close window and OpenGL context
2017-05-22 21:47:28 +03:00
//--------------------------------------------------------------------------------------
return 0;
}