core_random_sequence.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Generates a 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() and free()
  20. typedef struct ColorRect {
  21. Color c;
  22. Rectangle r;
  23. } ColorRect;
  24. //------------------------------------------------------------------------------------
  25. // Module functions declaration
  26. //------------------------------------------------------------------------------------
  27. static Color GenerateRandomColor();
  28. static ColorRect *GenerateRandomColorRectSequence(float rectCount, float rectWidth, float screenWidth, float screenHeight);
  29. static void ShuffleColorRectSequence(ColorRect *rectangles, int rectCount);
  30. static void DrawTextCenterKeyHelp(const char *key, const char *text, int posX, int posY, int fontSize, Color color);
  31. //------------------------------------------------------------------------------------
  32. // Program main entry point
  33. //------------------------------------------------------------------------------------
  34. int main(void)
  35. {
  36. // Initialization
  37. //--------------------------------------------------------------------------------------
  38. const int screenWidth = 800;
  39. const int screenHeight = 450;
  40. InitWindow(screenWidth, screenHeight, "raylib [core] example - Generates a random sequence");
  41. int rectCount = 20;
  42. float rectSize = (float)screenWidth/rectCount;
  43. ColorRect *rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f*screenHeight);
  44. SetTargetFPS(60);
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. if (IsKeyPressed(KEY_SPACE)) ShuffleColorRectSequence(rectangles, rectCount);
  52. if (IsKeyPressed(KEY_UP))
  53. {
  54. rectCount++;
  55. rectSize = (float)screenWidth/rectCount;
  56. free(rectangles);
  57. rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f*screenHeight);
  58. }
  59. if (IsKeyPressed(KEY_DOWN))
  60. {
  61. if (rectCount >= 4)
  62. {
  63. rectCount--;
  64. rectSize = (float)screenWidth/rectCount;
  65. free(rectangles);
  66. rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f*screenHeight);
  67. }
  68. }
  69. //----------------------------------------------------------------------------------
  70. // Draw
  71. //----------------------------------------------------------------------------------
  72. BeginDrawing();
  73. ClearBackground(RAYWHITE);
  74. int fontSize = 20;
  75. for (int i = 0; i < rectCount; i++)
  76. {
  77. DrawRectangleRec(rectangles[i].r, rectangles[i].c);
  78. DrawTextCenterKeyHelp("SPACE", "to shuffle the sequence.", 10, screenHeight - 96, fontSize, BLACK);
  79. DrawTextCenterKeyHelp("UP", "to add a rectangle and generate a new sequence.", 10, screenHeight - 64, fontSize, BLACK);
  80. DrawTextCenterKeyHelp("DOWN", "to remove a rectangle and generate a new sequence.", 10, screenHeight - 32, fontSize, BLACK);
  81. }
  82. const char *rectCountText = TextFormat("%d rectangles", rectCount);
  83. int rectCountTextSize = MeasureText(rectCountText, fontSize);
  84. DrawText(rectCountText, screenWidth - rectCountTextSize - 10, 10, fontSize, BLACK);
  85. DrawFPS(10, 10);
  86. EndDrawing();
  87. //----------------------------------------------------------------------------------
  88. }
  89. // De-Initialization
  90. //--------------------------------------------------------------------------------------
  91. free(rectangles);
  92. CloseWindow(); // Close window and OpenGL context
  93. //--------------------------------------------------------------------------------------
  94. return 0;
  95. }
  96. //------------------------------------------------------------------------------------
  97. // Module functions definition
  98. //------------------------------------------------------------------------------------
  99. static Color GenerateRandomColor()
  100. {
  101. Color color = {
  102. GetRandomValue(0, 255),
  103. GetRandomValue(0, 255),
  104. GetRandomValue(0, 255),
  105. 255
  106. };
  107. return color;
  108. }
  109. static ColorRect *GenerateRandomColorRectSequence(float rectCount, float rectWidth, float screenWidth, float screenHeight)
  110. {
  111. ColorRect *rectangles = (ColorRect *)malloc((int)rectCount*sizeof(ColorRect));
  112. int *seq = LoadRandomSequence((unsigned int)rectCount, 0, (unsigned int)rectCount - 1);
  113. float rectSeqWidth = rectCount*rectWidth;
  114. float startX = (screenWidth - rectSeqWidth)*0.5f;
  115. for (int i = 0; i < rectCount; i++)
  116. {
  117. int rectHeight = (int)Remap((float)seq[i], 0, rectCount - 1, 0, screenHeight);
  118. rectangles[i].c = GenerateRandomColor();
  119. rectangles[i].r = CLITERAL(Rectangle){ startX + i*rectWidth, screenHeight - rectHeight, rectWidth, (float)rectHeight };
  120. }
  121. UnloadRandomSequence(seq);
  122. return rectangles;
  123. }
  124. static void ShuffleColorRectSequence(ColorRect *rectangles, int rectCount)
  125. {
  126. int *seq = LoadRandomSequence(rectCount, 0, rectCount - 1);
  127. for (int i1 = 0; i1 < rectCount; i1++)
  128. {
  129. ColorRect *r1 = &rectangles[i1];
  130. ColorRect *r2 = &rectangles[seq[i1]];
  131. // Swap only the color and height
  132. ColorRect tmp = *r1;
  133. r1->c = r2->c;
  134. r1->r.height = r2->r.height;
  135. r1->r.y = r2->r.y;
  136. r2->c = tmp.c;
  137. r2->r.height = tmp.r.height;
  138. r2->r.y = tmp.r.y;
  139. }
  140. UnloadRandomSequence(seq);
  141. }
  142. static void DrawTextCenterKeyHelp(const char *key, const char *text, int posX, int posY, int fontSize, Color color)
  143. {
  144. int spaceSize = MeasureText(" ", fontSize);
  145. int pressSize = MeasureText("Press", fontSize);
  146. int keySize = MeasureText(key, fontSize);
  147. int textSize = MeasureText(text, fontSize);
  148. int textSizeCurrent = 0;
  149. DrawText("Press", posX, posY, fontSize, color);
  150. textSizeCurrent += pressSize + 2*spaceSize;
  151. DrawText(key, posX + textSizeCurrent, posY, fontSize, RED);
  152. DrawRectangle(posX + textSizeCurrent, posY + fontSize, keySize, 3, RED);
  153. textSizeCurrent += keySize + 2*spaceSize;
  154. DrawText(text, posX + textSizeCurrent, posY, fontSize, color);
  155. }