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.

105 lines
3.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Windows drop files
  4. *
  5. * NOTE: This example only works on platforms that support drag & drop (Windows, Linux, OSX, Html5?)
  6. *
  7. * Example originally created with raylib 1.3, last time updated with raylib 4.2
  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) 2015-2024 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include <stdlib.h> // Required for: calloc(), free()
  17. #define MAX_FILEPATH_RECORDED 4096
  18. #define MAX_FILEPATH_SIZE 2048
  19. //------------------------------------------------------------------------------------
  20. // Program main entry point
  21. //------------------------------------------------------------------------------------
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. InitWindow(screenWidth, screenHeight, "raylib [core] example - drop files");
  29. int filePathCounter = 0;
  30. char *filePaths[MAX_FILEPATH_RECORDED] = { 0 }; // We will register a maximum of filepaths
  31. // Allocate space for the required file paths
  32. for (int i = 0; i < MAX_FILEPATH_RECORDED; i++)
  33. {
  34. filePaths[i] = (char *)RL_CALLOC(MAX_FILEPATH_SIZE, 1);
  35. }
  36. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  37. //--------------------------------------------------------------------------------------
  38. // Main game loop
  39. while (!WindowShouldClose()) // Detect window close button or ESC key
  40. {
  41. // Update
  42. //----------------------------------------------------------------------------------
  43. if (IsFileDropped())
  44. {
  45. FilePathList droppedFiles = LoadDroppedFiles();
  46. for (int i = 0, offset = filePathCounter; i < (int)droppedFiles.count; i++)
  47. {
  48. if (filePathCounter < (MAX_FILEPATH_RECORDED - 1))
  49. {
  50. TextCopy(filePaths[offset + i], droppedFiles.paths[i]);
  51. filePathCounter++;
  52. }
  53. }
  54. UnloadDroppedFiles(droppedFiles); // Unload filepaths from memory
  55. }
  56. //----------------------------------------------------------------------------------
  57. // Draw
  58. //----------------------------------------------------------------------------------
  59. BeginDrawing();
  60. ClearBackground(RAYWHITE);
  61. if (filePathCounter == 0) DrawText("Drop your files to this window!", 100, 40, 20, DARKGRAY);
  62. else
  63. {
  64. DrawText("Dropped files:", 100, 40, 20, DARKGRAY);
  65. for (int i = 0; i < filePathCounter; i++)
  66. {
  67. if (i%2 == 0) DrawRectangle(0, 85 + 40*i, screenWidth, 40, Fade(LIGHTGRAY, 0.5f));
  68. else DrawRectangle(0, 85 + 40*i, screenWidth, 40, Fade(LIGHTGRAY, 0.3f));
  69. DrawText(filePaths[i], 120, 100 + 40*i, 10, GRAY);
  70. }
  71. DrawText("Drop new files...", 100, 110 + 40*filePathCounter, 20, DARKGRAY);
  72. }
  73. EndDrawing();
  74. //----------------------------------------------------------------------------------
  75. }
  76. // De-Initialization
  77. //--------------------------------------------------------------------------------------
  78. for (int i = 0; i < MAX_FILEPATH_RECORDED; i++)
  79. {
  80. RL_FREE(filePaths[i]); // Free allocated memory for all filepaths
  81. }
  82. CloseWindow(); // Close window and OpenGL context
  83. //--------------------------------------------------------------------------------------
  84. return 0;
  85. }