shapes_colors_palette.c 4.4 KB

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