Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

127 rindas
5.3 KiB

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