Skip to content

Commit

Permalink
Merge pull request #14 from RobLoach/LoadDirectoryFilesFromPhysFS
Browse files Browse the repository at this point in the history
Add LoadDirectoryFilesFromPhysFS()
  • Loading branch information
RobLoach authored Oct 4, 2022
2 parents c0f88be + 3e73ad7 commit df53cae
Show file tree
Hide file tree
Showing 7 changed files with 431 additions and 117 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ char* LoadFileTextFromPhysFS(const char* fileName); // Load text fro
bool SetPhysFSWriteDirectory(const char* newDir); // Set the base directory where PhysFS should write files to (defaults to the current working directory)
bool SaveFileDataToPhysFS(const char* fileName, void* data, unsigned int bytesToWrite); // Save the given file data in PhysFS
bool SaveFileTextToPhysFS(const char* fileName, char* text); // Save the given file text in PhysFS
char** GetDirectoryFilesFromPhysFS(const char* dirPath, int* count); // Get filenames in a directory path (memory should be freed)
void ClearDirectoryFilesFromPhysFS(char** filesList); // Clear directory files paths buffers (free memory)
FilePathList LoadDirectoryFilesFromPhysFS(const char* dirPath); // Get filenames in a directory path (memory should be freed)
long GetFileModTimeFromPhysFS(const char* fileName); // Get file modification time (last write time) from PhysFS
Image LoadImageFromPhysFS(const char* fileName); // Load an image from PhysFS
Texture2D LoadTextureFromPhysFS(const char* fileName); // Load a texture from PhysFS
Expand Down
29 changes: 29 additions & 0 deletions cmake/FindPhysFS.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# physfs

# You can change these to support other formats
set(PHYSFS_ARCHIVE_ZIP ON CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_7Z OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_GRP OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_WAD OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_HOG OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_MVL OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_QPAK OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_SLB OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_ISO9660 OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_VDF OFF CACHE BOOL "" FORCE)

# library options
set(PHYSFS_BUILD_STATIC ON CACHE BOOL "" FORCE)
set(PHYSFS_BUILD_SHARED OFF CACHE BOOL "" FORCE)
set(PHYSFS_BUILD_TEST OFF CACHE BOOL "" FORCE)
set(PHYSFS_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(PHYSFS_DISABLE_INSTALL OFF CACHE BOOL "" FORCE)

include(FetchContent)
FetchContent_Declare(
physfs
GIT_REPOSITORY https://github.com/icculus/physfs.git
GIT_TAG release-3.2.0
)
FetchContent_MakeAvailable(physfs)
include_directories(${physfs_SOURCE_DIR}/src)
15 changes: 15 additions & 0 deletions cmake/FindRaylib.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# raylib
include(FetchContent)
FetchContent_Declare(
raylib
GIT_REPOSITORY https://github.com/raysan5/raylib.git
GIT_TAG 4.2.0
)
FetchContent_GetProperties(raylib)
if (NOT raylib_POPULATED) # Have we downloaded raylib yet?
set(FETCHCONTENT_QUIET NO)
FetchContent_Populate(raylib)
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(BUILD_GAMES OFF CACHE BOOL "" FORCE)
add_subdirectory(${raylib_SOURCE_DIR} ${raylib_BINARY_DIR})
endif()
52 changes: 4 additions & 48 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,53 +1,9 @@
# raylib
find_package(raylib QUIET)
if (NOT raylib_FOUND)
include(FetchContent)
FetchContent_Declare(
raylib
GIT_REPOSITORY https://github.com/raysan5/raylib.git
GIT_TAG 4.2.0
)
FetchContent_GetProperties(raylib)
if (NOT raylib_POPULATED) # Have we downloaded raylib yet?
set(FETCHCONTENT_QUIET NO)
FetchContent_Populate(raylib)
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(BUILD_GAMES OFF CACHE BOOL "" FORCE)
add_subdirectory(${raylib_SOURCE_DIR} ${raylib_BINARY_DIR})
endif()
endif()

# physfs
find_package(physfs QUIET)
if (NOT physfs_FOUND)
# You can change these to support other formats
set(PHYSFS_ARCHIVE_ZIP ON CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_7Z OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_GRP OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_WAD OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_HOG OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_MVL OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_QPAK OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_SLB OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_ISO9660 OFF CACHE BOOL "" FORCE)
set(PHYSFS_ARCHIVE_VDF OFF CACHE BOOL "" FORCE)
# Add the cmake module path
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)

# library options
set(PHYSFS_BUILD_STATIC ON CACHE BOOL "" FORCE)
set(PHYSFS_BUILD_SHARED OFF CACHE BOOL "" FORCE)
set(PHYSFS_BUILD_TEST OFF CACHE BOOL "" FORCE)
set(PHYSFS_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(PHYSFS_DISABLE_INSTALL OFF CACHE BOOL "" FORCE)

include(FetchContent)
FetchContent_Declare(
physfs
GIT_REPOSITORY https://github.com/icculus/physfs.git
GIT_TAG release-3.2.0
)
FetchContent_MakeAvailable(physfs)
include_directories(${physfs_SOURCE_DIR}/src)
endif()
find_package(Raylib)
find_package(PhysFS)

# Resources
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/audio/resources/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/resources/)
Expand Down
45 changes: 19 additions & 26 deletions raylib-physfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Copyright 2021 Rob Loach (@RobLoach)
*
* DEPENDENCIES:
* raylib 4 https://www.raylib.com/
* raylib 4.2+ https://www.raylib.com/
* physfs https://github.com/icculus/physfs
*
* LICENSE: zlib/libpng
Expand Down Expand Up @@ -33,8 +33,6 @@
#ifndef INCLUDE_RAYLIB_PHYSFS_H_
#define INCLUDE_RAYLIB_PHYSFS_H_

#include "raylib.h" // NOLINT

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -61,8 +59,7 @@ RAYLIB_PHYSFS_DEF char* LoadFileTextFromPhysFS(const char* fileName);
RAYLIB_PHYSFS_DEF bool SetPhysFSWriteDirectory(const char* newDir); // Set the base directory where PhysFS should write files to (defaults to the current working directory)
RAYLIB_PHYSFS_DEF bool SaveFileDataToPhysFS(const char* fileName, void* data, unsigned int bytesToWrite); // Save the given file data in PhysFS
RAYLIB_PHYSFS_DEF bool SaveFileTextToPhysFS(const char* fileName, char* text); // Save the given file text in PhysFS
RAYLIB_PHYSFS_DEF char** GetDirectoryFilesFromPhysFS(const char* dirPath, int* count); // Get filenames in a directory path (memory should be freed)
RAYLIB_PHYSFS_DEF void ClearDirectoryFilesFromPhysFS(char** filesList); // Clear directory files paths buffers (free memory)
RAYLIB_PHYSFS_DEF FilePathList LoadDirectoryFilesFromPhysFS(const char* dirPath); // Get filenames in a directory path (memory should be freed)
RAYLIB_PHYSFS_DEF long GetFileModTimeFromPhysFS(const char* fileName); // Get file modification time (last write time) from PhysFS
RAYLIB_PHYSFS_DEF Image LoadImageFromPhysFS(const char* fileName); // Load an image from PhysFS
RAYLIB_PHYSFS_DEF Texture2D LoadTextureFromPhysFS(const char* fileName); // Load a texture from PhysFS
Expand Down Expand Up @@ -554,40 +551,36 @@ bool SaveFileTextToPhysFS(const char* fileName, char* text) {
/**
* Gets a list of files in the given directory in PhysFS.
*
* Make sure to clear the loaded list by using ClearDirectoryFilesFromPhysFS().
* Make sure to clear the loaded list by using UnloadDirectoryFiles().
*
* @see ClearDirectoryFilesFromPhysFS()
* @see UnloadDirectoryFiles()
*/
char** GetDirectoryFilesFromPhysFS(const char* dirPath, int *count) {
FilePathList LoadDirectoryFilesFromPhysFS(const char* dirPath) {
// Make sure the directory exists.
if (!DirectoryExistsInPhysFS(dirPath)) {
TraceLog(LOG_WARNING, "PHYSFS: Can't get files from non-existant directory (%s)", dirPath);
return 0;
FilePathList out;
out.capacity = 0;
out.count = 0;
out.paths = 0;
return out;
}

// Prepare the output.
FilePathList output;

// Load the list of files from PhysFS.
char** list = PHYSFS_enumerateFiles(dirPath);
output.paths = PHYSFS_enumerateFiles(dirPath);

// Find out how many files there were.
int number = 0;
for (char** i = list; *i != 0; i++) {
number++;
output.count = 0;
for (char** i = output.paths; *i != 0; i++) {
output.count++;
}
output.capacity = output.count;

// Output the count and the list.
*count = number;
return list;
}

/**
* Clears the loaded list of directory files from GetDirectoryFilesFromPhysFS().
*
* @param filesList The list of files to clear that was provided from GetDirectoryFilesFromPhysFS().
*
* @see GetDirectoryFilesFromPhysFS()
*/
void ClearDirectoryFilesFromPhysFS(char** filesList) {
PHYSFS_freeList(filesList);
return output;
}

/**
Expand Down
Loading

0 comments on commit df53cae

Please sign in to comment.