core_random_sequence.c 7.0 KB

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