portable_window.c 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*******************************************************************************************
  2. *
  3. * raygui - portable window
  4. *
  5. * DEPENDENCIES:
  6. * raylib 4.0 - Windowing/input management and drawing.
  7. * raygui 3.0 - Immediate-mode GUI controls.
  8. *
  9. * COMPILATION (Windows - MinGW):
  10. * gcc -o $(NAME_PART).exe $(FILE_NAME) -I../../src -lraylib -lopengl32 -lgdi32 -std=c99
  11. *
  12. * LICENSE: zlib/libpng
  13. *
  14. * Copyright (c) 2016-2024 Ramon Santamaria (@raysan5)
  15. *
  16. **********************************************************************************************/
  17. #include "raylib.h"
  18. #define RAYGUI_IMPLEMENTATION
  19. #include "../../src/raygui.h"
  20. //------------------------------------------------------------------------------------
  21. // Program main entry point
  22. //------------------------------------------------------------------------------------
  23. int main()
  24. {
  25. // Initialization
  26. //---------------------------------------------------------------------------------------
  27. const int screenWidth = 800;
  28. const int screenHeight = 600;
  29. SetConfigFlags(FLAG_WINDOW_UNDECORATED);
  30. InitWindow(screenWidth, screenHeight, "raygui - portable window");
  31. // General variables
  32. Vector2 mousePosition = { 0 };
  33. Vector2 windowPosition = { 500, 200 };
  34. Vector2 panOffset = mousePosition;
  35. bool dragWindow = false;
  36. SetWindowPosition(windowPosition.x, windowPosition.y);
  37. bool exitWindow = false;
  38. SetTargetFPS(60);
  39. //--------------------------------------------------------------------------------------
  40. // Main game loop
  41. while (!exitWindow && !WindowShouldClose()) // Detect window close button or ESC key
  42. {
  43. // Update
  44. //----------------------------------------------------------------------------------
  45. mousePosition = GetMousePosition();
  46. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && !dragWindow)
  47. {
  48. if (CheckCollisionPointRec(mousePosition, (Rectangle){ 0, 0, screenWidth, 20 }))
  49. {
  50. windowPosition = GetWindowPosition();
  51. dragWindow = true;
  52. panOffset = mousePosition;
  53. }
  54. }
  55. if (dragWindow)
  56. {
  57. windowPosition.x += (mousePosition.x - panOffset.x);
  58. windowPosition.y += (mousePosition.y - panOffset.y);
  59. SetWindowPosition((int)windowPosition.x, (int)windowPosition.y);
  60. if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) dragWindow = false;
  61. }
  62. //----------------------------------------------------------------------------------
  63. // Draw
  64. //----------------------------------------------------------------------------------
  65. BeginDrawing();
  66. ClearBackground(RAYWHITE);
  67. exitWindow = GuiWindowBox((Rectangle){ 0, 0, screenWidth, screenHeight }, "#198# PORTABLE WINDOW");
  68. DrawText(TextFormat("Mouse Position: [ %.0f, %.0f ]", mousePosition.x, mousePosition.y), 10, 40, 10, DARKGRAY);
  69. DrawText(TextFormat("Window Position: [ %.0f, %.0f ]", windowPosition.x, windowPosition.y), 10, 60, 10, DARKGRAY);
  70. EndDrawing();
  71. //----------------------------------------------------------------------------------
  72. }
  73. // De-Initialization
  74. //--------------------------------------------------------------------------------------
  75. CloseWindow(); // Close window and OpenGL context
  76. //--------------------------------------------------------------------------------------
  77. return 0;
  78. }