core_directory_files.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - directory files
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 5.5, last time updated with raylib 5.6
  8. *
  9. * Example contributed by Hugo ARNAL (@hugoarnal) and reviewed by Ramon Santamaria (@raysan5)
  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) 2025 Hugo ARNAL (@hugoarnal)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #define RAYGUI_IMPLEMENTATION
  19. #include "raygui.h" // Required for GUI controls
  20. #define MAX_FILEPATH_SIZE 1024
  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 - directory files");
  31. char directory[MAX_FILEPATH_SIZE] = { 0 };
  32. strcpy(directory, GetWorkingDirectory());
  33. FilePathList files = LoadDirectoryFiles(directory);
  34. int btnBackPressed = false;
  35. SetTargetFPS(60);
  36. //--------------------------------------------------------------------------------------
  37. // Main game loop
  38. while (!WindowShouldClose()) // Detect window close button or ESC key
  39. {
  40. // Update
  41. //----------------------------------------------------------------------------------
  42. if (btnBackPressed)
  43. {
  44. TextCopy(directory, GetPrevDirectoryPath(directory));
  45. UnloadDirectoryFiles(files);
  46. files = LoadDirectoryFiles(directory);
  47. }
  48. //----------------------------------------------------------------------------------
  49. // Draw
  50. //----------------------------------------------------------------------------------
  51. BeginDrawing();
  52. ClearBackground(RAYWHITE);
  53. DrawText(directory, 100, 40, 20, DARKGRAY);
  54. btnBackPressed = GuiButton((Rectangle){ 40.0f, 38.0f, 48, 24 }, "<");
  55. for (int i = 0; i < (int)files.count; i++)
  56. {
  57. Color color = Fade(LIGHTGRAY, 0.3f);
  58. if (!IsPathFile(files.paths[i]) && DirectoryExists(files.paths[i]))
  59. {
  60. if (GuiButton((Rectangle){0.0f, 85.0f + 40.0f*(float)i, screenWidth, 40}, ""))
  61. {
  62. TextCopy(directory, files.paths[i]);
  63. UnloadDirectoryFiles(files);
  64. files = LoadDirectoryFiles(directory);
  65. continue;
  66. }
  67. }
  68. DrawRectangle(0, 85 + 40*i, screenWidth, 40, color);
  69. DrawText(GetFileName(files.paths[i]), 120, 100 + 40*i, 10, GRAY);
  70. }
  71. EndDrawing();
  72. //----------------------------------------------------------------------------------
  73. }
  74. // De-Initialization
  75. //--------------------------------------------------------------------------------------
  76. UnloadDirectoryFiles(files);
  77. CloseWindow(); // Close window and OpenGL context
  78. //--------------------------------------------------------------------------------------
  79. return 0;
  80. }