core_drop_files.c 4.0 KB

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