shapes_colors_palette.c 4.0 KB

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