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.

120 lines
4.9 KiB

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