You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - following eyes
  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) 2013-2019 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #include <math.h> // Required for: atan2f()
  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 - following eyes");
  20. Vector2 scleraLeftPosition = { GetScreenWidth()/2 - 100, GetScreenHeight()/2 };
  21. Vector2 scleraRightPosition = { GetScreenWidth()/2 + 100, GetScreenHeight()/2 };
  22. float scleraRadius = 80;
  23. Vector2 irisLeftPosition = { GetScreenWidth()/2 - 100, GetScreenHeight()/2 };
  24. Vector2 irisRightPosition = { GetScreenWidth()/2 + 100, GetScreenHeight()/2};
  25. float irisRadius = 24;
  26. float angle = 0.0f;
  27. float dx = 0.0f, dy = 0.0f, dxx = 0.0f, dyy = 0.0f;
  28. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  29. //--------------------------------------------------------------------------------------
  30. // Main game loop
  31. while (!WindowShouldClose()) // Detect window close button or ESC key
  32. {
  33. // Update
  34. //----------------------------------------------------------------------------------
  35. irisLeftPosition = GetMousePosition();
  36. irisRightPosition = GetMousePosition();
  37. // Check not inside the left eye sclera
  38. if (!CheckCollisionPointCircle(irisLeftPosition, scleraLeftPosition, scleraRadius - 20))
  39. {
  40. dx = irisLeftPosition.x - scleraLeftPosition.x;
  41. dy = irisLeftPosition.y - scleraLeftPosition.y;
  42. angle = atan2f(dy, dx);
  43. dxx = (scleraRadius - irisRadius)*cosf(angle);
  44. dyy = (scleraRadius - irisRadius)*sinf(angle);
  45. irisLeftPosition.x = scleraLeftPosition.x + dxx;
  46. irisLeftPosition.y = scleraLeftPosition.y + dyy;
  47. }
  48. // Check not inside the right eye sclera
  49. if (!CheckCollisionPointCircle(irisRightPosition, scleraRightPosition, scleraRadius - 20))
  50. {
  51. dx = irisRightPosition.x - scleraRightPosition.x;
  52. dy = irisRightPosition.y - scleraRightPosition.y;
  53. angle = atan2f(dy, dx);
  54. dxx = (scleraRadius - irisRadius)*cosf(angle);
  55. dyy = (scleraRadius - irisRadius)*sinf(angle);
  56. irisRightPosition.x = scleraRightPosition.x + dxx;
  57. irisRightPosition.y = scleraRightPosition.y + dyy;
  58. }
  59. //----------------------------------------------------------------------------------
  60. // Draw
  61. //----------------------------------------------------------------------------------
  62. BeginDrawing();
  63. ClearBackground(RAYWHITE);
  64. DrawCircleV(scleraLeftPosition, scleraRadius, LIGHTGRAY);
  65. DrawCircleV(irisLeftPosition, irisRadius, BROWN);
  66. DrawCircleV(irisLeftPosition, 10, BLACK);
  67. DrawCircleV(scleraRightPosition, scleraRadius, LIGHTGRAY);
  68. DrawCircleV(irisRightPosition, irisRadius, DARKGREEN);
  69. DrawCircleV(irisRightPosition, 10, BLACK);
  70. DrawFPS(10, 10);
  71. EndDrawing();
  72. //----------------------------------------------------------------------------------
  73. }
  74. // De-Initialization
  75. //--------------------------------------------------------------------------------------
  76. CloseWindow(); // Close window and OpenGL context
  77. //--------------------------------------------------------------------------------------
  78. return 0;
  79. }