|
| 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 | + |
| 18 | +#include "raylib.h" |
| 19 | +#include <string.h> // Required for: strcpy() |
| 20 | + |
| 21 | +#define MAX_FILEPATH_SIZE 2048 |
| 22 | + |
| 23 | +#define RAYGUI_IMPLEMENTATION |
| 24 | +#include "../shapes/raygui.h" // Required for GUI controls |
| 25 | + |
| 26 | +//------------------------------------------------------------------------------------ |
| 27 | +// Program main entry point |
| 28 | +//------------------------------------------------------------------------------------ |
| 29 | +int main(void) |
| 30 | +{ |
| 31 | + // Initialization |
| 32 | + //-------------------------------------------------------------------------------------- |
| 33 | + const int screenWidth = 800; |
| 34 | + const int screenHeight = 450; |
| 35 | + |
| 36 | + InitWindow(screenWidth, screenHeight, "raylib [core] example - directory files"); |
| 37 | + |
| 38 | + char directory[MAX_FILEPATH_SIZE] = { 0 }; |
| 39 | + strcpy(directory, GetWorkingDirectory()); |
| 40 | + FilePathList files = LoadDirectoryFiles(directory); |
| 41 | + |
| 42 | + SetTargetFPS(60); |
| 43 | + //-------------------------------------------------------------------------------------- |
| 44 | + |
| 45 | + // Main game loop |
| 46 | + while (!WindowShouldClose()) // Detect window close button or ESC key |
| 47 | + { |
| 48 | + // Update |
| 49 | + //---------------------------------------------------------------------------------- |
| 50 | + |
| 51 | + // Draw |
| 52 | + //---------------------------------------------------------------------------------- |
| 53 | + BeginDrawing(); |
| 54 | + ClearBackground(RAYWHITE); |
| 55 | + |
| 56 | + DrawText(directory, 100, 40, 20, DARKGRAY); |
| 57 | + |
| 58 | + if (GuiButton((Rectangle){40.0f, 40.0f, 20, 20}, "<")) |
| 59 | + { |
| 60 | + strcpy(directory, GetPrevDirectoryPath(directory)); |
| 61 | + UnloadDirectoryFiles(files); |
| 62 | + files = LoadDirectoryFiles(directory); |
| 63 | + } |
| 64 | + |
| 65 | + for (int i = 0; i < (int)files.count; i++) |
| 66 | + { |
| 67 | + Color color = Fade(LIGHTGRAY, 0.3f); |
| 68 | + |
| 69 | + if (!IsPathFile(files.paths[i])) |
| 70 | + { |
| 71 | + if (GuiButton((Rectangle){0.0f, 85.0f + 40.0f*(float)i, screenWidth, 40}, "")) |
| 72 | + { |
| 73 | + strcpy(directory, files.paths[i]); |
| 74 | + UnloadDirectoryFiles(files); |
| 75 | + files = LoadDirectoryFiles(directory); |
| 76 | + } |
| 77 | + } |
| 78 | + DrawRectangle(0, 85 + 40*i, screenWidth, 40, color); |
| 79 | + |
| 80 | + DrawText(GetFileName(files.paths[i]), 120, 100 + 40*i, 10, GRAY); |
| 81 | + } |
| 82 | + EndDrawing(); |
| 83 | + //---------------------------------------------------------------------------------- |
| 84 | + } |
| 85 | + |
| 86 | + // De-Initialization |
| 87 | + //-------------------------------------------------------------------------------------- |
| 88 | + UnloadDirectoryFiles(files); |
| 89 | + |
| 90 | + CloseWindow(); // Close window and OpenGL context |
| 91 | + //-------------------------------------------------------------------------------------- |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
0 commit comments