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.

101 lines
3.8 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib 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 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #include <stdlib.h> // Required for: malloc(), free()
  13. #define MAX_BUNNIES 100000 // 100K bunnies
  14. typedef struct Bunny {
  15. Vector2 position;
  16. Vector2 speed;
  17. Color color;
  18. } Bunny;
  19. int main()
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. int screenWidth = 1280;
  24. int screenHeight = 960;
  25. InitWindow(screenWidth, screenHeight, "raylib example - Bunnymark");
  26. Texture2D texBunny = LoadTexture("resources/wabbit_alpha.png");
  27. Bunny *bunnies = (Bunny *)malloc(MAX_BUNNIES*sizeof(Bunny)); // Bunnies array
  28. int bunniesCount = 0; // Bunnies counter
  29. SetTargetFPS(60);
  30. //--------------------------------------------------------------------------------------
  31. // Main game loop
  32. while (!WindowShouldClose()) // Detect window close button or ESC key
  33. {
  34. // Update
  35. //----------------------------------------------------------------------------------
  36. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
  37. {
  38. // Create more bunnies
  39. for (int i = 0; i < 100; i++)
  40. {
  41. bunnies[bunniesCount].position = GetMousePosition();
  42. bunnies[bunniesCount].speed.x = (float)GetRandomValue(250, 500)/60.0f;
  43. bunnies[bunniesCount].speed.y = (float)(GetRandomValue(250, 500) - 500)/60.0f;
  44. bunniesCount++;
  45. }
  46. }
  47. // Update bunnies
  48. for (int i = 0; i < bunniesCount; i++)
  49. {
  50. bunnies[i].position.x += bunnies[i].speed.x;
  51. bunnies[i].position.y += bunnies[i].speed.y;
  52. if ((bunnies[i].position.x > GetScreenWidth()) || (bunnies[i].position.x < 0)) bunnies[i].speed.x *= -1;
  53. if ((bunnies[i].position.y > GetScreenHeight()) || (bunnies[i].position.y < 0)) bunnies[i].speed.y *= -1;
  54. }
  55. //----------------------------------------------------------------------------------
  56. // Draw
  57. //----------------------------------------------------------------------------------
  58. BeginDrawing();
  59. ClearBackground(RAYWHITE);
  60. for (int i = 0; i < bunniesCount; i++)
  61. {
  62. // NOTE: When internal QUADS batch limit is reached, a draw call is launched and
  63. // batching buffer starts being filled again; before launching the draw call,
  64. // updated vertex data from internal buffer is send to GPU... it seems it generates
  65. // a stall and consequently a frame drop, limiting number of bunnies drawn at 60 fps
  66. DrawTexture(texBunny, bunnies[i].position.x, bunnies[i].position.y, RAYWHITE);
  67. }
  68. DrawRectangle(0, 0, screenWidth, 40, LIGHTGRAY);
  69. DrawText("raylib bunnymark", 10, 10, 20, DARKGRAY);
  70. DrawText(FormatText("bunnies: %i", bunniesCount), 400, 10, 20, RED);
  71. DrawFPS(260, 10);
  72. EndDrawing();
  73. //----------------------------------------------------------------------------------
  74. }
  75. // De-Initialization
  76. //--------------------------------------------------------------------------------------
  77. free(bunnies);
  78. CloseWindow(); // Close window and OpenGL context
  79. //--------------------------------------------------------------------------------------
  80. return 0;
  81. }