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