core_random_sequence.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - random sequence
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 5.0, last time updated with raylib 5.0
  8. *
  9. * Example contributed by Dalton Overmyer (@REDl3east) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2023-2025 Dalton Overmyer (@REDl3east)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "raymath.h"
  19. #include <stdlib.h> // Required for: malloc(), free()
  20. //----------------------------------------------------------------------------------
  21. // Types and Structures Definition
  22. //----------------------------------------------------------------------------------
  23. typedef struct ColorRect {
  24. Color color;
  25. Rectangle rect;
  26. } ColorRect;
  27. //------------------------------------------------------------------------------------
  28. // Module Functions Declaration
  29. //------------------------------------------------------------------------------------
  30. static Color GenerateRandomColor(void);
  31. static ColorRect *GenerateRandomColorRectSequence(float rectCount, float rectWidth, float screenWidth, float screenHeight);
  32. static void ShuffleColorRectSequence(ColorRect *rectangles, int rectCount);
  33. //------------------------------------------------------------------------------------
  34. // Program main entry point
  35. //------------------------------------------------------------------------------------
  36. int main(void)
  37. {
  38. // Initialization
  39. //--------------------------------------------------------------------------------------
  40. const int screenWidth = 800;
  41. const int screenHeight = 450;
  42. InitWindow(screenWidth, screenHeight, "raylib [core] example - random sequence");
  43. int rectCount = 20;
  44. float rectSize = (float)screenWidth/rectCount;
  45. ColorRect *rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f*screenHeight);
  46. SetTargetFPS(60);
  47. //--------------------------------------------------------------------------------------
  48. // Main game loop
  49. while (!WindowShouldClose()) // Detect window close button or ESC key
  50. {
  51. // Update
  52. //----------------------------------------------------------------------------------
  53. if (IsKeyPressed(KEY_SPACE)) ShuffleColorRectSequence(rectangles, rectCount);
  54. if (IsKeyPressed(KEY_UP))
  55. {
  56. rectCount++;
  57. rectSize = (float)screenWidth/rectCount;
  58. RL_FREE(rectangles);
  59. // Re-generate random sequence with new count
  60. rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f*screenHeight);
  61. }
  62. if (IsKeyPressed(KEY_DOWN))
  63. {
  64. if (rectCount >= 4)
  65. {
  66. rectCount--;
  67. rectSize = (float)screenWidth/rectCount;
  68. RL_FREE(rectangles);
  69. // Re-generate random sequence with new count
  70. rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f*screenHeight);
  71. }
  72. }
  73. //----------------------------------------------------------------------------------
  74. // Draw
  75. //----------------------------------------------------------------------------------
  76. BeginDrawing();
  77. ClearBackground(RAYWHITE);
  78. for (int i = 0; i < rectCount; i++)
  79. {
  80. DrawRectangleRec(rectangles[i].rect, rectangles[i].color);
  81. DrawText("Press SPACE to shuffle the sequence", 10, screenHeight - 96, 20, BLACK);
  82. DrawText("Press SPACE to shuffle the current sequence", 10, screenHeight - 96, 20, BLACK);
  83. DrawText("Press UP to add a rectangle and generate a new sequence", 10, screenHeight - 64, 20, BLACK);
  84. DrawText("Press DOWN to remove a rectangle and generate a new sequence", 10, screenHeight - 32, 20, BLACK);
  85. }
  86. DrawText(TextFormat("Count: %d rectangles", rectCount), 10, 10, 20, MAROON);
  87. DrawFPS(screenWidth - 80, 10);
  88. EndDrawing();
  89. //----------------------------------------------------------------------------------
  90. }
  91. // De-Initialization
  92. //--------------------------------------------------------------------------------------
  93. free(rectangles);
  94. CloseWindow(); // Close window and OpenGL context
  95. //--------------------------------------------------------------------------------------
  96. return 0;
  97. }
  98. //------------------------------------------------------------------------------------
  99. // Module Functions Definition
  100. //------------------------------------------------------------------------------------
  101. static Color GenerateRandomColor(void)
  102. {
  103. Color color = {
  104. GetRandomValue(0, 255),
  105. GetRandomValue(0, 255),
  106. GetRandomValue(0, 255),
  107. 255
  108. };
  109. return color;
  110. }
  111. static ColorRect *GenerateRandomColorRectSequence(float rectCount, float rectWidth, float screenWidth, float screenHeight)
  112. {
  113. ColorRect *rectangles = (ColorRect *)RL_CALLOC((int)rectCount, sizeof(ColorRect));
  114. int *seq = LoadRandomSequence((unsigned int)rectCount, 0, (unsigned int)rectCount - 1);
  115. float rectSeqWidth = rectCount*rectWidth;
  116. float startX = (screenWidth - rectSeqWidth)*0.5f;
  117. for (int i = 0; i < rectCount; i++)
  118. {
  119. int rectHeight = (int)Remap((float)seq[i], 0, rectCount - 1, 0, screenHeight);
  120. rectangles[i].color = GenerateRandomColor();
  121. rectangles[i].rect = CLITERAL(Rectangle){ startX + i*rectWidth, screenHeight - rectHeight, rectWidth, (float)rectHeight };
  122. }
  123. UnloadRandomSequence(seq);
  124. return rectangles;
  125. }
  126. static void ShuffleColorRectSequence(ColorRect *rectangles, int rectCount)
  127. {
  128. int *seq = LoadRandomSequence(rectCount, 0, rectCount - 1);
  129. for (int i1 = 0; i1 < rectCount; i1++)
  130. {
  131. ColorRect *r1 = &rectangles[i1];
  132. ColorRect *r2 = &rectangles[seq[i1]];
  133. // Swap only the color and height
  134. ColorRect tmp = *r1;
  135. r1->color = r2->color;
  136. r1->rect.height = r2->rect.height;
  137. r1->rect.y = r2->rect.y;
  138. r2->color = tmp.color;
  139. r2->rect.height = tmp.rect.height;
  140. r2->rect.y = tmp.rect.y;
  141. }
  142. UnloadRandomSequence(seq);
  143. }