/******************************************************************************************* * * raylib [core] example - Generates a random sequence * * Example originally created with raylib 5.0, last time updated with raylib 5.0 * * Example contributed by Dalton Overmyer (@REDl3east) and reviewed by Ramon Santamaria (@raysan5) * * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, * BSD-like license that allows static linking with closed source software * * Copyright (c) 2023 Dalton Overmyer (@REDl3east) * ********************************************************************************************/ #include "raylib.h" #include "raymath.h" #include // Required for: malloc() and free() typedef struct ColorRect{ Color c; Rectangle r; } ColorRect; static Color GenerateRandomColor(); static ColorRect* GenerateRandomColorRectSequence(float rectCount, float rectWidth, float screenWidth, float screenHeight); static void ShuffleColorRectSequence(ColorRect* rectangles, int rectCount); static void DrawTextCenterKeyHelp(const char* key, const char* text, int posX, int posY, int fontSize, Color color); //------------------------------------------------------------------------------------ // Program main entry point //------------------------------------------------------------------------------------ int main(void) { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [core] example - Generates a random sequence"); int rectCount = 20; float rectSize = (float)screenWidth/rectCount; ColorRect* rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f * screenHeight); SetTargetFPS(60); //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- if(IsKeyPressed(KEY_SPACE)) { ShuffleColorRectSequence(rectangles, rectCount); } if(IsKeyPressed(KEY_UP)) { rectCount++; rectSize = (float)screenWidth/rectCount; free(rectangles); rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f * screenHeight); } if(IsKeyPressed(KEY_DOWN)) { if(rectCount >= 4){ rectCount--; rectSize = (float)screenWidth/rectCount; free(rectangles); rectangles = GenerateRandomColorRectSequence((float)rectCount, rectSize, (float)screenWidth, 0.75f * screenHeight); } } // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); int fontSize = 20; for(int x=0;xc = r2->c; r1->r.height = r2->r.height; r1->r.y = r2->r.y; r2->c = tmp.c; r2->r.height = tmp.r.height; r2->r.y = tmp.r.y; } UnloadRandomSequence(seq); } static void DrawTextCenterKeyHelp(const char* key, const char* text, int posX, int posY, int fontSize, Color color) { int spaceSize = MeasureText(" ", fontSize); int pressSize = MeasureText("Press", fontSize); int keySize = MeasureText(key, fontSize); int textSize = MeasureText(text, fontSize); int totalSize = pressSize + 2 * spaceSize + keySize + 2 * spaceSize + textSize; int textSizeCurrent = 0; DrawText("Press", posX, posY, fontSize, color); textSizeCurrent += pressSize + 2 * spaceSize; DrawText(key, posX + textSizeCurrent, posY, fontSize, RED); DrawRectangle(posX + textSizeCurrent, posY + fontSize, keySize, 3, RED); textSizeCurrent += keySize + 2 * spaceSize; DrawText(text, posX + textSizeCurrent, posY, fontSize, color); }