2
0

shapes_colors_palette.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include "raylib.h"
  14. #define MAX_COLORS_COUNT 21 // Number of colors available
  15. //------------------------------------------------------------------------------------
  16. // Program main entry point
  17. //------------------------------------------------------------------------------------
  18. int main(void)
  19. {
  20. // Initialization
  21. //--------------------------------------------------------------------------------------
  22. const int screenWidth = 800;
  23. const int screenHeight = 450;
  24. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - colors palette");
  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. const char *colorNames[MAX_COLORS_COUNT] = {
  30. "DARKGRAY", "MAROON", "ORANGE", "DARKGREEN", "DARKBLUE", "DARKPURPLE",
  31. "DARKBROWN", "GRAY", "RED", "GOLD", "LIME", "BLUE", "VIOLET", "BROWN",
  32. "LIGHTGRAY", "PINK", "YELLOW", "GREEN", "SKYBLUE", "PURPLE", "BEIGE" };
  33. Rectangle colorsRecs[MAX_COLORS_COUNT] = { 0 }; // Rectangles array
  34. // Fills colorsRecs data (for every rectangle)
  35. for (int i = 0; i < MAX_COLORS_COUNT; i++)
  36. {
  37. colorsRecs[i].x = 20.0f + 100.0f *(i%7) + 10.0f *(i%7);
  38. colorsRecs[i].y = 80.0f + 100.0f *(i/7) + 10.0f *(i/7);
  39. colorsRecs[i].width = 100.0f;
  40. colorsRecs[i].height = 100.0f;
  41. }
  42. int colorState[MAX_COLORS_COUNT] = { 0 }; // Color state: 0-DEFAULT, 1-MOUSE_HOVER
  43. Vector2 mousePoint = { 0.0f, 0.0f };
  44. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. mousePoint = GetMousePosition();
  52. for (int i = 0; i < MAX_COLORS_COUNT; i++)
  53. {
  54. if (CheckCollisionPointRec(mousePoint, colorsRecs[i])) colorState[i] = 1;
  55. else colorState[i] = 0;
  56. }
  57. //----------------------------------------------------------------------------------
  58. // Draw
  59. //----------------------------------------------------------------------------------
  60. BeginDrawing();
  61. ClearBackground(RAYWHITE);
  62. DrawText("raylib colors palette", 28, 42, 20, BLACK);
  63. DrawText("press SPACE to see all colors", GetScreenWidth() - 180, GetScreenHeight() - 40, 10, GRAY);
  64. for (int i = 0; i < MAX_COLORS_COUNT; i++) // Draw all rectangles
  65. {
  66. DrawRectangleRec(colorsRecs[i], Fade(colors[i], colorState[i]? 0.6f : 1.0f));
  67. if (IsKeyDown(KEY_SPACE) || colorState[i])
  68. {
  69. DrawRectangle((int)colorsRecs[i].x, (int)(colorsRecs[i].y + colorsRecs[i].height - 26), (int)colorsRecs[i].width, 20, BLACK);
  70. DrawRectangleLinesEx(colorsRecs[i], 6, Fade(BLACK, 0.3f));
  71. DrawText(colorNames[i], (int)(colorsRecs[i].x + colorsRecs[i].width - MeasureText(colorNames[i], 10) - 12),
  72. (int)(colorsRecs[i].y + colorsRecs[i].height - 20), 10, colors[i]);
  73. }
  74. }
  75. EndDrawing();
  76. //----------------------------------------------------------------------------------
  77. }
  78. // De-Initialization
  79. //--------------------------------------------------------------------------------------
  80. CloseWindow(); // Close window and OpenGL context
  81. //--------------------------------------------------------------------------------------
  82. return 0;
  83. }