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.

175 lines
6.2 KiB

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