ex06a_color_select.c 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*******************************************************************************************
  2. *
  3. * raylib example 06a - Color selection by mouse (collision detection)
  4. *
  5. * This example has been created using raylib 1.0 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2013 Ramon Santamaria (Ray San - [email protected])
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. int main()
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. int screenWidth = 800;
  17. int screenHeight = 400;
  18. Color colors[21] = { DARKGRAY, MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, DARKBROWN,
  19. GRAY, RED, GOLD, LIME, BLUE, VIOLET, BROWN, LIGHTGRAY, PINK, YELLOW,
  20. GREEN, SKYBLUE, PURPLE, BEIGE };
  21. Rectangle recs[21]; // Rectangles array
  22. // Fills recs data (for every rectangle)
  23. for (int i = 0; i < 21; i++)
  24. {
  25. recs[i].x = 20 + 100*(i%7) + 10*(i%7);
  26. recs[i].y = 40 + 100*(i/7) + 10*(i/7);
  27. recs[i].width = 100;
  28. recs[i].height = 100;
  29. }
  30. bool selected[21] = { false }; // Selected rectangles indicator
  31. Vector2 mousePoint;
  32. InitWindowEx(screenWidth, screenHeight, "raylib example 06a - color selection", false, "resources/mouse.png");
  33. SetTargetFPS(60);
  34. //--------------------------------------------------------------------------------------
  35. // Main game loop
  36. while (!WindowShouldClose()) // Detect window close button or ESC key
  37. {
  38. // Update
  39. //----------------------------------------------------------------------------------
  40. mousePoint = GetMousePosition();
  41. for (int i = 0; i < 21; i++) // Iterate along all the rectangles
  42. {
  43. if (CheckCollisionPointRec(mousePoint, recs[i]))
  44. {
  45. colors[i].a = 120;
  46. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) selected[i] = !selected[i];
  47. }
  48. else colors[i].a = 255;
  49. }
  50. //----------------------------------------------------------------------------------
  51. // Draw
  52. //----------------------------------------------------------------------------------
  53. BeginDrawing();
  54. ClearBackground(RAYWHITE);
  55. for (int i = 0; i < 21; i++) // Draw all rectangles
  56. {
  57. DrawRectangleRec(recs[i], colors[i]);
  58. // Draw four rectangles around selected rectangle
  59. if (selected[i])
  60. {
  61. DrawRectangle(recs[i].x, recs[i].y, 100, 10, RAYWHITE); // Square top rectangle
  62. DrawRectangle(recs[i].x, recs[i].y, 10, 100, RAYWHITE); // Square left rectangle
  63. DrawRectangle(recs[i].x + 90, recs[i].y, 10, 100, RAYWHITE); // Square right rectangle
  64. DrawRectangle(recs[i].x, recs[i].y + 90, 100, 10, RAYWHITE); // Square bottom rectangle
  65. }
  66. }
  67. EndDrawing();
  68. //----------------------------------------------------------------------------------
  69. }
  70. // De-Initialization
  71. //--------------------------------------------------------------------------------------
  72. CloseWindow(); // Close window and OpenGL context
  73. //--------------------------------------------------------------------------------------
  74. return 0;
  75. }