|
| 1 | +/******************************************************************************************* |
| 2 | +* |
| 3 | +* raylib [shapes] example - Colors palette |
| 4 | +* |
| 5 | +* Example originally created with raylib 1.0, last time updated with raylib 2.5 |
| 6 | +* |
| 7 | +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, |
| 8 | +* BSD-like license that allows static linking with closed source software |
| 9 | +* |
| 10 | +* Copyright (c) 2014-2024 Ramon Santamaria (@raysan5) |
| 11 | +* |
| 12 | +********************************************************************************************/ |
| 13 | + |
| 14 | +#include "raylib.h" |
| 15 | + |
| 16 | +#define MAX_COLORS_COUNT 21 // Number of colors available |
| 17 | + |
| 18 | +//------------------------------------------------------------------------------------ |
| 19 | +// Program main entry point |
| 20 | +//------------------------------------------------------------------------------------ |
| 21 | +int main(void) |
| 22 | +{ |
| 23 | + // Initialization |
| 24 | + //-------------------------------------------------------------------------------------- |
| 25 | + const int screenWidth = 800; |
| 26 | + const int screenHeight = 450; |
| 27 | + |
| 28 | + InitWindow(screenWidth, screenHeight, "raylib [shapes] example - colors palette"); |
| 29 | + |
| 30 | + Color colors[MAX_COLORS_COUNT] = { |
| 31 | + DARKGRAY, MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, DARKBROWN, |
| 32 | + GRAY, RED, GOLD, LIME, BLUE, VIOLET, BROWN, LIGHTGRAY, PINK, YELLOW, |
| 33 | + GREEN, SKYBLUE, PURPLE, BEIGE }; |
| 34 | + |
| 35 | + const char *colorNames[MAX_COLORS_COUNT] = { |
| 36 | + "DARKGRAY", "MAROON", "ORANGE", "DARKGREEN", "DARKBLUE", "DARKPURPLE", |
| 37 | + "DARKBROWN", "GRAY", "RED", "GOLD", "LIME", "BLUE", "VIOLET", "BROWN", |
| 38 | + "LIGHTGRAY", "PINK", "YELLOW", "GREEN", "SKYBLUE", "PURPLE", "BEIGE" }; |
| 39 | + |
| 40 | + Rectangle colorsRecs[MAX_COLORS_COUNT] = { 0 }; // Rectangles array |
| 41 | + |
| 42 | + // Fills colorsRecs data (for every rectangle) |
| 43 | + for (int i = 0; i < MAX_COLORS_COUNT; i++) |
| 44 | + { |
| 45 | + colorsRecs[i].x = 20.0f + 100.0f *(i%7) + 10.0f *(i%7); |
| 46 | + colorsRecs[i].y = 80.0f + 100.0f *(i/7) + 10.0f *(i/7); |
| 47 | + colorsRecs[i].width = 100.0f; |
| 48 | + colorsRecs[i].height = 100.0f; |
| 49 | + } |
| 50 | + |
| 51 | + int colorState[MAX_COLORS_COUNT] = { 0 }; // Color state: 0-DEFAULT, 1-MOUSE_HOVER |
| 52 | + |
| 53 | + Vector2 mousePoint = { 0.0f, 0.0f }; |
| 54 | + |
| 55 | + SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
| 56 | + //-------------------------------------------------------------------------------------- |
| 57 | + |
| 58 | + // Main game loop |
| 59 | + while (!WindowShouldClose()) // Detect window close button or ESC key |
| 60 | + { |
| 61 | + // Update |
| 62 | + //---------------------------------------------------------------------------------- |
| 63 | + mousePoint = GetMousePosition(); |
| 64 | + |
| 65 | + for (int i = 0; i < MAX_COLORS_COUNT; i++) |
| 66 | + { |
| 67 | + if (CheckCollisionPointRec(mousePoint, colorsRecs[i])) colorState[i] = 1; |
| 68 | + else colorState[i] = 0; |
| 69 | + } |
| 70 | + //---------------------------------------------------------------------------------- |
| 71 | + |
| 72 | + // Draw |
| 73 | + //---------------------------------------------------------------------------------- |
| 74 | + BeginDrawing(); |
| 75 | + |
| 76 | + ClearBackground(RAYWHITE); |
| 77 | + |
| 78 | + DrawText("raylib colors palette", 28, 42, 20, BLACK); |
| 79 | + DrawText("press SPACE to see all colors", GetScreenWidth() - 180, GetScreenHeight() - 40, 10, GRAY); |
| 80 | + |
| 81 | + for (int i = 0; i < MAX_COLORS_COUNT; i++) // Draw all rectangles |
| 82 | + { |
| 83 | + DrawRectangleRec(colorsRecs[i], Fade(colors[i], colorState[i]? 0.6f : 1.0f)); |
| 84 | + |
| 85 | + if (IsKeyDown(KEY_SPACE) || colorState[i]) |
| 86 | + { |
| 87 | + DrawRectangle((int)colorsRecs[i].x, (int)(colorsRecs[i].y + colorsRecs[i].height - 26), (int)colorsRecs[i].width, 20, BLACK); |
| 88 | + DrawRectangleLinesEx(colorsRecs[i], 6, Fade(BLACK, 0.3f)); |
| 89 | + DrawText(colorNames[i], (int)(colorsRecs[i].x + colorsRecs[i].width - MeasureText(colorNames[i], 10) - 12), |
| 90 | + (int)(colorsRecs[i].y + colorsRecs[i].height - 20), 10, colors[i]); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + EndDrawing(); |
| 95 | + //---------------------------------------------------------------------------------- |
| 96 | + } |
| 97 | + |
| 98 | + // De-Initialization |
| 99 | + //-------------------------------------------------------------------------------------- |
| 100 | + CloseWindow(); // Close window and OpenGL context |
| 101 | + //-------------------------------------------------------------------------------------- |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
0 commit comments