You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
5.2 KiB

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