WARNING: BREAKING: REDESIGNED: Filepath loading API
REDESIGNED: `LoadDirectoryFiles()` ADDED: `LoadDirectoryFilesEx()` REDESIGNED: `LoadDroppedFiles()` ADDED: `IsPathFile()` This BIG BREAKING change simplifies the functions and gives more control to the user: - A new `struct FilePathList` has been added to avoid exposing complex pointers. - User is responsible of memory loading/unloading - Filepaths loading support recursive directories and file extension filters
This commit is contained in:
parent
f7744404d6
commit
b8f67c6285
7 changed files with 247 additions and 100 deletions
|
@ -22,8 +22,7 @@ int main(void)
|
||||||
|
|
||||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - drop files");
|
InitWindow(screenWidth, screenHeight, "raylib [core] example - drop files");
|
||||||
|
|
||||||
int count = 0;
|
FilePathList droppedFiles = { 0 };
|
||||||
char **droppedFiles = { 0 };
|
|
||||||
|
|
||||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
@ -35,7 +34,11 @@ int main(void)
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
if (IsFileDropped())
|
if (IsFileDropped())
|
||||||
{
|
{
|
||||||
droppedFiles = LoadDroppedFiles(&count);
|
// Is some files have been previously loaded, unload them
|
||||||
|
if (droppedFiles.count > 0) UnloadDroppedFiles(droppedFiles);
|
||||||
|
|
||||||
|
// Load new dropped files
|
||||||
|
droppedFiles = LoadDroppedFiles();
|
||||||
}
|
}
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -55,7 +58,7 @@ int main(void)
|
||||||
if (i%2 == 0) DrawRectangle(0, 85 + 40*i, screenWidth, 40, Fade(LIGHTGRAY, 0.5f));
|
if (i%2 == 0) DrawRectangle(0, 85 + 40*i, screenWidth, 40, Fade(LIGHTGRAY, 0.5f));
|
||||||
else DrawRectangle(0, 85 + 40*i, screenWidth, 40, Fade(LIGHTGRAY, 0.3f));
|
else DrawRectangle(0, 85 + 40*i, screenWidth, 40, Fade(LIGHTGRAY, 0.3f));
|
||||||
|
|
||||||
DrawText(droppedFiles[i], 120, 100 + 40*i, 10, GRAY);
|
DrawText(droppedFiles.paths[i], 120, 100 + 40*i, 10, GRAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
DrawText("Drop new files...", 100, 110 + 40*count, 20, DARKGRAY);
|
DrawText("Drop new files...", 100, 110 + 40*count, 20, DARKGRAY);
|
||||||
|
@ -67,7 +70,7 @@ int main(void)
|
||||||
|
|
||||||
// De-Initialization
|
// De-Initialization
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
UnloadDroppedFiles(); // Clear internal buffers
|
UnloadDroppedFiles(droppedFiles); // Unload files memory
|
||||||
|
|
||||||
CloseWindow(); // Close window and OpenGL context
|
CloseWindow(); // Close window and OpenGL context
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
|
@ -67,35 +67,34 @@ int main(void)
|
||||||
// Load new models/textures on drag&drop
|
// Load new models/textures on drag&drop
|
||||||
if (IsFileDropped())
|
if (IsFileDropped())
|
||||||
{
|
{
|
||||||
int count = 0;
|
FilePathList droppedFiles = LoadDroppedFiles();
|
||||||
char **droppedFiles = LoadDroppedFiles(&count);
|
|
||||||
|
|
||||||
if (count == 1) // Only support one file dropped
|
if (droppedFiles.count == 1) // Only support one file dropped
|
||||||
{
|
{
|
||||||
if (IsFileExtension(droppedFiles[0], ".obj") ||
|
if (IsFileExtension(droppedFiles.paths[0], ".obj") ||
|
||||||
IsFileExtension(droppedFiles[0], ".gltf") ||
|
IsFileExtension(droppedFiles.paths[0], ".gltf") ||
|
||||||
IsFileExtension(droppedFiles[0], ".glb") ||
|
IsFileExtension(droppedFiles.paths[0], ".glb") ||
|
||||||
IsFileExtension(droppedFiles[0], ".vox") ||
|
IsFileExtension(droppedFiles.paths[0], ".vox") ||
|
||||||
IsFileExtension(droppedFiles[0], ".iqm")) // Model file formats supported
|
IsFileExtension(droppedFiles.paths[0], ".iqm")) // Model file formats supported
|
||||||
{
|
{
|
||||||
UnloadModel(model); // Unload previous model
|
UnloadModel(model); // Unload previous model
|
||||||
model = LoadModel(droppedFiles[0]); // Load new model
|
model = LoadModel(droppedFiles.paths[0]); // Load new model
|
||||||
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set current map diffuse texture
|
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set current map diffuse texture
|
||||||
|
|
||||||
bounds = GetMeshBoundingBox(model.meshes[0]);
|
bounds = GetMeshBoundingBox(model.meshes[0]);
|
||||||
|
|
||||||
// TODO: Move camera position from target enough distance to visualize model properly
|
// TODO: Move camera position from target enough distance to visualize model properly
|
||||||
}
|
}
|
||||||
else if (IsFileExtension(droppedFiles[0], ".png")) // Texture file formats supported
|
else if (IsFileExtension(droppedFiles.paths[0], ".png")) // Texture file formats supported
|
||||||
{
|
{
|
||||||
// Unload current model texture and load new one
|
// Unload current model texture and load new one
|
||||||
UnloadTexture(texture);
|
UnloadTexture(texture);
|
||||||
texture = LoadTexture(droppedFiles[0]);
|
texture = LoadTexture(droppedFiles.paths[0]);
|
||||||
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
|
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UnloadDroppedFiles(); // Clear internal buffers
|
UnloadDroppedFiles(droppedFiles); // Unload filepaths from memory
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select model on mouse click
|
// Select model on mouse click
|
||||||
|
|
|
@ -97,18 +97,17 @@ int main(void)
|
||||||
// Load new cubemap texture on drag&drop
|
// Load new cubemap texture on drag&drop
|
||||||
if (IsFileDropped())
|
if (IsFileDropped())
|
||||||
{
|
{
|
||||||
int count = 0;
|
FilePathList droppedFiles = LoadDroppedFiles();
|
||||||
char **droppedFiles = LoadDroppedFiles(&count);
|
|
||||||
|
|
||||||
if (count == 1) // Only support one file dropped
|
if (droppedFiles.count == 1) // Only support one file dropped
|
||||||
{
|
{
|
||||||
if (IsFileExtension(droppedFiles[0], ".png;.jpg;.hdr;.bmp;.tga"))
|
if (IsFileExtension(droppedFiles.paths[0], ".png;.jpg;.hdr;.bmp;.tga"))
|
||||||
{
|
{
|
||||||
// Unload current cubemap texture and load new one
|
// Unload current cubemap texture and load new one
|
||||||
UnloadTexture(skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture);
|
UnloadTexture(skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture);
|
||||||
if (useHDR)
|
if (useHDR)
|
||||||
{
|
{
|
||||||
Texture2D panorama = LoadTexture(droppedFiles[0]);
|
Texture2D panorama = LoadTexture(droppedFiles.paths[0]);
|
||||||
|
|
||||||
// Generate cubemap from panorama texture
|
// Generate cubemap from panorama texture
|
||||||
skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, panorama, 1024, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
|
skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, panorama, 1024, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
|
||||||
|
@ -116,7 +115,7 @@ int main(void)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Image img = LoadImage(droppedFiles[0]);
|
Image img = LoadImage(droppedFiles.paths[0]);
|
||||||
skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = LoadTextureCubemap(img, CUBEMAP_LAYOUT_AUTO_DETECT);
|
skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = LoadTextureCubemap(img, CUBEMAP_LAYOUT_AUTO_DETECT);
|
||||||
UnloadImage(img);
|
UnloadImage(img);
|
||||||
}
|
}
|
||||||
|
@ -125,7 +124,7 @@ int main(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UnloadDroppedFiles(); // Clear internal buffers
|
UnloadDroppedFiles(droppedFiles); // Unload filepaths from memory
|
||||||
}
|
}
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
@ -141,22 +141,22 @@ int main(void)
|
||||||
// Handle font files dropped
|
// Handle font files dropped
|
||||||
if (IsFileDropped())
|
if (IsFileDropped())
|
||||||
{
|
{
|
||||||
int count = 0;
|
FilePathList droppedFiles = LoadDroppedFiles();
|
||||||
char **droppedFiles = LoadDroppedFiles(&count);
|
|
||||||
|
|
||||||
// NOTE: We only support first ttf file dropped
|
// NOTE: We only support first ttf file dropped
|
||||||
if (IsFileExtension(droppedFiles[0], ".ttf"))
|
if (IsFileExtension(droppedFiles.paths[0], ".ttf"))
|
||||||
{
|
{
|
||||||
UnloadFont(font);
|
UnloadFont(font);
|
||||||
font = LoadFontEx(droppedFiles[0], fontSize, 0, 0);
|
font = LoadFontEx(droppedFiles.paths[0], fontSize, 0, 0);
|
||||||
}
|
}
|
||||||
else if (IsFileExtension(droppedFiles[0], ".fnt"))
|
else if (IsFileExtension(droppedFiles.paths[0], ".fnt"))
|
||||||
{
|
{
|
||||||
UnloadFont(font);
|
UnloadFont(font);
|
||||||
font = LoadFont(droppedFiles[0]);
|
font = LoadFont(droppedFiles.paths[0]);
|
||||||
fontSize = font.baseSize;
|
fontSize = font.baseSize;
|
||||||
}
|
}
|
||||||
UnloadDroppedFiles();
|
|
||||||
|
UnloadDroppedFiles(droppedFiles); // Unload filepaths from memory
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle Events
|
// Handle Events
|
||||||
|
|
|
@ -79,16 +79,16 @@ int main(void)
|
||||||
// Load a dropped TTF file dynamically (at current fontSize)
|
// Load a dropped TTF file dynamically (at current fontSize)
|
||||||
if (IsFileDropped())
|
if (IsFileDropped())
|
||||||
{
|
{
|
||||||
int count = 0;
|
FilePathList droppedFiles = LoadDroppedFiles();
|
||||||
char **droppedFiles = LoadDroppedFiles(&count);
|
|
||||||
|
|
||||||
// NOTE: We only support first ttf file dropped
|
// NOTE: We only support first ttf file dropped
|
||||||
if (IsFileExtension(droppedFiles[0], ".ttf"))
|
if (IsFileExtension(droppedFiles.paths[0], ".ttf"))
|
||||||
{
|
{
|
||||||
UnloadFont(font);
|
UnloadFont(font);
|
||||||
font = LoadFontEx(droppedFiles[0], (int)fontSize, 0, 0);
|
font = LoadFontEx(droppedFiles.paths[0], (int)fontSize, 0, 0);
|
||||||
UnloadDroppedFiles();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UnloadDroppedFiles(droppedFiles); // Unload filepaths from memory
|
||||||
}
|
}
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
20
src/raylib.h
20
src/raylib.h
|
@ -482,6 +482,12 @@ typedef struct VrStereoConfig {
|
||||||
float scaleIn[2]; // VR distortion scale in
|
float scaleIn[2]; // VR distortion scale in
|
||||||
} VrStereoConfig;
|
} VrStereoConfig;
|
||||||
|
|
||||||
|
// File path list
|
||||||
|
typedef struct FilePathList {
|
||||||
|
unsigned int count; // Filepaths entries count
|
||||||
|
char **paths; // Filepaths entries
|
||||||
|
} FilePathList;
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Enumerators Definition
|
// Enumerators Definition
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
@ -891,8 +897,8 @@ typedef enum {
|
||||||
typedef void (*TraceLogCallback)(int logLevel, const char *text, va_list args); // Logging: Redirect trace log messages
|
typedef void (*TraceLogCallback)(int logLevel, const char *text, va_list args); // Logging: Redirect trace log messages
|
||||||
typedef unsigned char *(*LoadFileDataCallback)(const char *fileName, unsigned int *bytesRead); // FileIO: Load binary data
|
typedef unsigned char *(*LoadFileDataCallback)(const char *fileName, unsigned int *bytesRead); // FileIO: Load binary data
|
||||||
typedef bool (*SaveFileDataCallback)(const char *fileName, void *data, unsigned int bytesToWrite); // FileIO: Save binary data
|
typedef bool (*SaveFileDataCallback)(const char *fileName, void *data, unsigned int bytesToWrite); // FileIO: Save binary data
|
||||||
typedef char *(*LoadFileTextCallback)(const char *fileName); // FileIO: Load text data
|
typedef char *(*LoadFileTextCallback)(const char *fileName); // FileIO: Load text data
|
||||||
typedef bool (*SaveFileTextCallback)(const char *fileName, char *text); // FileIO: Save text data
|
typedef bool (*SaveFileTextCallback)(const char *fileName, char *text); // FileIO: Save text data
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
// Global Variables Definition
|
// Global Variables Definition
|
||||||
|
@ -1059,11 +1065,13 @@ RLAPI const char *GetPrevDirectoryPath(const char *dirPath); // Get previou
|
||||||
RLAPI const char *GetWorkingDirectory(void); // Get current working directory (uses static string)
|
RLAPI const char *GetWorkingDirectory(void); // Get current working directory (uses static string)
|
||||||
RLAPI const char *GetApplicationDirectory(void); // Get the directory if the running application (uses static string)
|
RLAPI const char *GetApplicationDirectory(void); // Get the directory if the running application (uses static string)
|
||||||
RLAPI bool ChangeDirectory(const char *dir); // Change working directory, return true on success
|
RLAPI bool ChangeDirectory(const char *dir); // Change working directory, return true on success
|
||||||
RLAPI char **LoadDirectoryFiles(const char *dirPath, int *count); // Load directory filepaths
|
RLAPI bool IsPathFile(const char *path); // Check if a given path is a file or a directory
|
||||||
RLAPI void UnloadDirectoryFiles(void); // Unload directory filepaths
|
RLAPI FilePathList LoadDirectoryFiles(const char *dirPath); // Load directory filepaths
|
||||||
|
RLAPI FilePathList LoadDirectoryFilesEx(const char *basePath, const char *filter, bool scanSubdirs); // Load directory filepaths with extension filtering and recursive directory scan
|
||||||
|
RLAPI void UnloadDirectoryFiles(FilePathList files); // Unload filepaths
|
||||||
RLAPI bool IsFileDropped(void); // Check if a file has been dropped into window
|
RLAPI bool IsFileDropped(void); // Check if a file has been dropped into window
|
||||||
RLAPI char **LoadDroppedFiles(int *count); // Load dropped filepaths
|
RLAPI FilePathList LoadDroppedFiles(void); // Load dropped filepaths
|
||||||
RLAPI void UnloadDroppedFiles(void); // Unload dropped filepaths
|
RLAPI void UnloadDroppedFiles(FilePathList files); // Unload dropped filepaths
|
||||||
RLAPI long GetFileModTime(const char *fileName); // Get file modification time (last write time)
|
RLAPI long GetFileModTime(const char *fileName); // Get file modification time (last write time)
|
||||||
|
|
||||||
// Compression/Encoding functionality
|
// Compression/Encoding functionality
|
||||||
|
|
250
src/rcore.c
250
src/rcore.c
|
@ -183,7 +183,12 @@
|
||||||
#include <time.h> // Required for: time() [Used in InitTimer()]
|
#include <time.h> // Required for: time() [Used in InitTimer()]
|
||||||
#include <math.h> // Required for: tan() [Used in BeginMode3D()], atan2f() [Used in LoadVrStereoConfig()]
|
#include <math.h> // Required for: tan() [Used in BeginMode3D()], atan2f() [Used in LoadVrStereoConfig()]
|
||||||
|
|
||||||
#include <sys/stat.h> // Required for: stat() [Used in GetFileModTime()]
|
#define _CRT_INTERNAL_NONSTDC_NAMES 1
|
||||||
|
#include <sys/stat.h> // Required for: stat(), S_ISREG [Used in GetFileModTime(), IsFilePath()]
|
||||||
|
|
||||||
|
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
|
||||||
|
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(PLATFORM_DESKTOP) && defined(_WIN32) && (defined(_MSC_VER) || defined(__TINYC__))
|
#if defined(PLATFORM_DESKTOP) && defined(_WIN32) && (defined(_MSC_VER) || defined(__TINYC__))
|
||||||
#define DIRENT_MALLOC RL_MALLOC
|
#define DIRENT_MALLOC RL_MALLOC
|
||||||
|
@ -294,11 +299,7 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef MAX_FILEPATH_LENGTH
|
#ifndef MAX_FILEPATH_LENGTH
|
||||||
#if defined(__linux__)
|
#define MAX_FILEPATH_LENGTH 4096 // Maximum length for filepaths (Linux PATH_MAX default value)
|
||||||
#define MAX_FILEPATH_LENGTH 4096 // Maximum length for filepaths (Linux PATH_MAX default value)
|
|
||||||
#else
|
|
||||||
#define MAX_FILEPATH_LENGTH 512 // Maximum length supported for filepaths
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef MAX_KEYBOARD_KEYS
|
#ifndef MAX_KEYBOARD_KEYS
|
||||||
|
@ -404,8 +405,8 @@ typedef struct CoreData {
|
||||||
Point renderOffset; // Offset from render area (must be divided by 2)
|
Point renderOffset; // Offset from render area (must be divided by 2)
|
||||||
Matrix screenScale; // Matrix to scale screen (framebuffer rendering)
|
Matrix screenScale; // Matrix to scale screen (framebuffer rendering)
|
||||||
|
|
||||||
char **dropFilepaths; // Store dropped files paths as strings
|
const char **dropFilepaths; // Store dropped files paths pointers (provided by GLFW)
|
||||||
int dropFileCount; // Count dropped files strings
|
unsigned int dropFileCount; // Count dropped files strings
|
||||||
|
|
||||||
} Window;
|
} Window;
|
||||||
#if defined(PLATFORM_ANDROID)
|
#if defined(PLATFORM_ANDROID)
|
||||||
|
@ -501,13 +502,10 @@ typedef struct CoreData {
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Global Variables Definition
|
// Global Variables Definition
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
static CoreData CORE = { 0 }; // Global CORE state context
|
|
||||||
|
|
||||||
static char **dirFilesPath = NULL; // Store directory files paths as strings
|
|
||||||
static int dirFileCount = 0; // Count directory files strings
|
|
||||||
|
|
||||||
const char *raylibVersion = RAYLIB_VERSION; // raylib version symbol, it could be required for some bindings
|
const char *raylibVersion = RAYLIB_VERSION; // raylib version symbol, it could be required for some bindings
|
||||||
|
|
||||||
|
static CoreData CORE = { 0 }; // Global CORE state context
|
||||||
|
|
||||||
#if defined(SUPPORT_SCREEN_CAPTURE)
|
#if defined(SUPPORT_SCREEN_CAPTURE)
|
||||||
static int screenshotCounter = 0; // Screenshots counter
|
static int screenshotCounter = 0; // Screenshots counter
|
||||||
#endif
|
#endif
|
||||||
|
@ -622,6 +620,9 @@ static bool InitGraphicsDevice(int width, int height); // Initialize graphics d
|
||||||
static void SetupFramebuffer(int width, int height); // Setup main framebuffer
|
static void SetupFramebuffer(int width, int height); // Setup main framebuffer
|
||||||
static void SetupViewport(int width, int height); // Set viewport for a provided width and height
|
static void SetupViewport(int width, int height); // Set viewport for a provided width and height
|
||||||
|
|
||||||
|
static void ScanDirectoryFiles(const char *basePath, FilePathList *list, const char *filter); // Scan all files and directories in a base path
|
||||||
|
static void ScanDirectoryFilesRecursively(const char *basePath, FilePathList *list, const char *filter); // Scan all files and directories recursively from a base path
|
||||||
|
|
||||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
||||||
static void ErrorCallback(int error, const char *description); // GLFW3 Error Callback, runs on GLFW3 error
|
static void ErrorCallback(int error, const char *description); // GLFW3 Error Callback, runs on GLFW3 error
|
||||||
// Window callbacks events
|
// Window callbacks events
|
||||||
|
@ -1419,6 +1420,13 @@ void SetWindowState(unsigned int flags)
|
||||||
TRACELOG(LOG_WARNING, "WINDOW: High DPI can only by configured before window initialization");
|
TRACELOG(LOG_WARNING, "WINDOW: High DPI can only by configured before window initialization");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// State change: FLAG_WINDOW_MOUSE_PASSTHROUGH
|
||||||
|
if (((CORE.Window.flags & FLAG_WINDOW_MOUSE_PASSTHROUGH) != (flags & FLAG_WINDOW_MOUSE_PASSTHROUGH)) && ((flags & FLAG_WINDOW_MOUSE_PASSTHROUGH) > 0))
|
||||||
|
{
|
||||||
|
glfwSetWindowAttrib(CORE.Window.handle, GLFW_MOUSE_PASSTHROUGH, GLFW_TRUE);
|
||||||
|
CORE.Window.flags |= FLAG_WINDOW_MOUSE_PASSTHROUGH;
|
||||||
|
}
|
||||||
|
|
||||||
// State change: FLAG_MSAA_4X_HINT
|
// State change: FLAG_MSAA_4X_HINT
|
||||||
if (((CORE.Window.flags & FLAG_MSAA_4X_HINT) != (flags & FLAG_MSAA_4X_HINT)) && ((flags & FLAG_MSAA_4X_HINT) > 0))
|
if (((CORE.Window.flags & FLAG_MSAA_4X_HINT) != (flags & FLAG_MSAA_4X_HINT)) && ((flags & FLAG_MSAA_4X_HINT) > 0))
|
||||||
{
|
{
|
||||||
|
@ -1520,6 +1528,13 @@ void ClearWindowState(unsigned int flags)
|
||||||
TRACELOG(LOG_WARNING, "WINDOW: High DPI can only by configured before window initialization");
|
TRACELOG(LOG_WARNING, "WINDOW: High DPI can only by configured before window initialization");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// State change: FLAG_WINDOW_MOUSE_PASSTHROUGH
|
||||||
|
if (((CORE.Window.flags & FLAG_WINDOW_MOUSE_PASSTHROUGH) > 0) && ((flags & FLAG_WINDOW_MOUSE_PASSTHROUGH) > 0))
|
||||||
|
{
|
||||||
|
glfwSetWindowAttrib(CORE.Window.handle, GLFW_MOUSE_PASSTHROUGH, GLFW_FALSE);
|
||||||
|
CORE.Window.flags &= ~FLAG_WINDOW_MOUSE_PASSTHROUGH;
|
||||||
|
}
|
||||||
|
|
||||||
// State change: FLAG_MSAA_4X_HINT
|
// State change: FLAG_MSAA_4X_HINT
|
||||||
if (((CORE.Window.flags & FLAG_MSAA_4X_HINT) > 0) && ((flags & FLAG_MSAA_4X_HINT) > 0))
|
if (((CORE.Window.flags & FLAG_MSAA_4X_HINT) > 0) && ((flags & FLAG_MSAA_4X_HINT) > 0))
|
||||||
{
|
{
|
||||||
|
@ -3106,51 +3121,75 @@ const char *GetApplicationDirectory(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load directory filepaths
|
// Load directory filepaths
|
||||||
// NOTE: Files count is returned by parameters pointer
|
// NOTE: Base path is prepended to the scanned filepaths
|
||||||
char **LoadDirectoryFiles(const char *dirPath, int *fileCount)
|
// WARNING: Directory is scanned twice, first time to get files count
|
||||||
|
// No recursive scanning is done!
|
||||||
|
FilePathList LoadDirectoryFiles(const char *dirPath)
|
||||||
{
|
{
|
||||||
UnloadDirectoryFiles();
|
FilePathList files = { 0 };
|
||||||
|
unsigned int fileCounter = 0;
|
||||||
|
|
||||||
int counter = 0;
|
|
||||||
struct dirent *entity;
|
struct dirent *entity;
|
||||||
DIR *dir = opendir(dirPath);
|
DIR *dir = opendir(dirPath);
|
||||||
|
|
||||||
if (dir != NULL) // It's a directory
|
if (dir != NULL) // It's a directory
|
||||||
{
|
{
|
||||||
// Count files
|
// SCAN 1: Count files
|
||||||
while ((entity = readdir(dir)) != NULL) counter++;
|
while ((entity = readdir(dir)) != NULL)
|
||||||
|
{
|
||||||
dirFileCount = counter;
|
// NOTE: We skip '.' (current dir) and '..' (parent dir) filepaths
|
||||||
*fileCount = dirFileCount;
|
if ((strcmp(entity->d_name, ".") != 0) && (strcmp(entity->d_name, "..") != 0)) fileCounter++;
|
||||||
|
}
|
||||||
|
|
||||||
// Memory allocation for dirFileCount
|
// Memory allocation for dirFileCount
|
||||||
dirFilesPath = (char **)RL_MALLOC(dirFileCount*sizeof(char *));
|
files.paths = (char **)RL_MALLOC(fileCounter*sizeof(char *));
|
||||||
for (int i = 0; i < dirFileCount; i++) dirFilesPath[i] = (char *)RL_MALLOC(MAX_FILEPATH_LENGTH*sizeof(char));
|
for (int i = 0; i < fileCounter; i++) files.paths[i] = (char *)RL_MALLOC(MAX_FILEPATH_LENGTH*sizeof(char));
|
||||||
|
|
||||||
// Reset our position in the directory to the beginning
|
|
||||||
rewinddir(dir);
|
|
||||||
|
|
||||||
// Read file names
|
|
||||||
for (int i = 0; (entity = readdir(dir)) != NULL; i++) strcpy(dirFilesPath[i], entity->d_name);
|
|
||||||
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
|
||||||
|
// SCAN 2: Read filepaths
|
||||||
|
// NOTE: Directory paths are also registered
|
||||||
|
ScanDirectoryFiles(dirPath, &files, NULL);
|
||||||
|
|
||||||
|
// Security check: read files.count should match fileCounter
|
||||||
|
if (files.count != fileCounter) TRACELOG(LOG_WARNING, "FILEIO: Read files count do not match memory allocated");
|
||||||
}
|
}
|
||||||
else TRACELOG(LOG_WARNING, "FILEIO: Failed to open requested directory"); // Maybe it's a file...
|
else TRACELOG(LOG_WARNING, "FILEIO: Failed to open requested directory"); // Maybe it's a file...
|
||||||
|
|
||||||
return dirFilesPath;
|
|
||||||
|
|
||||||
|
return files;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load directory filepaths with extension filtering and recursive directory scan
|
||||||
|
FilePathList LoadDirectoryFilesEx(const char *basePath, const char *filter, bool scanSubdirs)
|
||||||
|
{
|
||||||
|
#define MAX_FILEPATH_COUNT 8192 // Maximum file paths scanned
|
||||||
|
|
||||||
|
FilePathList files = { 0 };
|
||||||
|
|
||||||
|
files.paths = (char **)RL_CALLOC(MAX_FILEPATH_COUNT, sizeof(char *));
|
||||||
|
for (int i = 0; i < MAX_FILEPATH_COUNT; i++) files.paths[i] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char));
|
||||||
|
|
||||||
|
// WARNING: basePath is always prepended to scanned paths
|
||||||
|
if (scanSubdirs) ScanDirectoryFilesRecursively(basePath, &files, filter);
|
||||||
|
else ScanDirectoryFiles(basePath, &files, filter);
|
||||||
|
|
||||||
|
// TODO: Filepath filtering
|
||||||
|
//if (IsFileExtension(files.paths[i], filter))
|
||||||
|
|
||||||
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unload directory filepaths
|
// Unload directory filepaths
|
||||||
void UnloadDirectoryFiles(void)
|
void UnloadDirectoryFiles(FilePathList files)
|
||||||
{
|
{
|
||||||
if (dirFileCount > 0)
|
if (files.count > 0)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < dirFileCount; i++) RL_FREE(dirFilesPath[i]);
|
for (int i = 0; i < files.count; i++) RL_FREE(files.paths[i]);
|
||||||
|
|
||||||
RL_FREE(dirFilesPath);
|
RL_FREE(files.paths);
|
||||||
}
|
}
|
||||||
|
|
||||||
dirFileCount = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change working directory, returns true on success
|
// Change working directory, returns true on success
|
||||||
|
@ -3163,6 +3202,15 @@ bool ChangeDirectory(const char *dir)
|
||||||
return (result == 0);
|
return (result == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if a given path point to a file
|
||||||
|
bool IsPathFile(const char *path)
|
||||||
|
{
|
||||||
|
struct stat pathStat = { 0 };
|
||||||
|
stat(path, &pathStat);
|
||||||
|
|
||||||
|
return S_ISREG(pathStat.st_mode);
|
||||||
|
}
|
||||||
|
|
||||||
// Check if a file has been dropped into window
|
// Check if a file has been dropped into window
|
||||||
bool IsFileDropped(void)
|
bool IsFileDropped(void)
|
||||||
{
|
{
|
||||||
|
@ -3171,22 +3219,37 @@ bool IsFileDropped(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load dropped filepaths
|
// Load dropped filepaths
|
||||||
char **LoadDroppedFiles(int *count)
|
FilePathList LoadDroppedFiles(void)
|
||||||
{
|
{
|
||||||
*count = CORE.Window.dropFileCount;
|
FilePathList files = { 0 };
|
||||||
return CORE.Window.dropFilepaths;
|
|
||||||
|
if (CORE.Window.dropFileCount > 0)
|
||||||
|
{
|
||||||
|
files.count = CORE.Window.dropFileCount;
|
||||||
|
files.paths = (char **)RL_CALLOC(files.count, sizeof(char *));
|
||||||
|
|
||||||
|
for (int i = 0; i < files.count; i++)
|
||||||
|
{
|
||||||
|
files.paths[i] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char));
|
||||||
|
strcpy(files.paths[i], CORE.Window.dropFilepaths[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// WARNING: We reset drop file count after loading the stored paths,
|
||||||
|
// despite internally GLFW probably keeps the pointers until next drop
|
||||||
|
CORE.Window.dropFileCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unload dropped filepaths
|
// Unload dropped filepaths
|
||||||
void UnloadDroppedFiles(void)
|
void UnloadDroppedFiles(FilePathList files)
|
||||||
{
|
{
|
||||||
if (CORE.Window.dropFileCount > 0)
|
if (files.count > 0)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < CORE.Window.dropFileCount; i++) RL_FREE(CORE.Window.dropFilepaths[i]);
|
for (int i = 0; i < files.count; i++) RL_FREE(files.paths[i]);
|
||||||
|
|
||||||
RL_FREE(CORE.Window.dropFilepaths);
|
RL_FREE(files.paths);
|
||||||
|
|
||||||
CORE.Window.dropFileCount = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5168,6 +5231,90 @@ void PollInputEvents(void)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Scan all files and directories in a base path
|
||||||
|
// WARNING: files.paths[] must be previously allocated and
|
||||||
|
// contain enough space to store all required paths
|
||||||
|
static void ScanDirectoryFiles(const char *basePath, FilePathList *files, const char *filter)
|
||||||
|
{
|
||||||
|
static char path[MAX_FILEPATH_LENGTH] = { 0 };
|
||||||
|
memset(path, 0, MAX_FILEPATH_LENGTH);
|
||||||
|
|
||||||
|
struct dirent *dp = NULL;
|
||||||
|
DIR *dir = opendir(basePath);
|
||||||
|
|
||||||
|
if (dir != NULL)
|
||||||
|
{
|
||||||
|
while ((dp = readdir(dir)) != NULL)
|
||||||
|
{
|
||||||
|
if ((strcmp(dp->d_name, ".") != 0) &&
|
||||||
|
(strcmp(dp->d_name, "..") != 0))
|
||||||
|
{
|
||||||
|
sprintf(path, "%s/%s", basePath, dp->d_name);
|
||||||
|
|
||||||
|
if (filter != NULL)
|
||||||
|
{
|
||||||
|
if (IsFileExtension(path, filter))
|
||||||
|
{
|
||||||
|
strcpy(files->paths[files->count], path);
|
||||||
|
files->count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
strcpy(files->paths[files->count], path);
|
||||||
|
files->count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(dir);
|
||||||
|
}
|
||||||
|
else TRACELOG(LOG_WARNING, "FILEIO: Directory cannot be opened (%s)", basePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scan all files and directories recursively from a base path
|
||||||
|
static void ScanDirectoryFilesRecursively(const char *basePath, FilePathList *files, const char *filter)
|
||||||
|
{
|
||||||
|
static char path[MAX_FILEPATH_LENGTH] = { 0 };
|
||||||
|
memset(path, 0, MAX_FILEPATH_LENGTH);
|
||||||
|
|
||||||
|
struct dirent *dp = NULL;
|
||||||
|
DIR *dir = opendir(basePath);
|
||||||
|
|
||||||
|
if (dir != NULL)
|
||||||
|
{
|
||||||
|
while ((dp = readdir(dir)) != NULL)
|
||||||
|
{
|
||||||
|
if ((strcmp(dp->d_name, ".") != 0) && (strcmp(dp->d_name, "..") != 0))
|
||||||
|
{
|
||||||
|
// Construct new path from our base path
|
||||||
|
sprintf(path, "%s/%s", basePath, dp->d_name);
|
||||||
|
|
||||||
|
if (IsPathFile(path))
|
||||||
|
{
|
||||||
|
if (filter != NULL)
|
||||||
|
{
|
||||||
|
if (IsFileExtension(path, filter))
|
||||||
|
{
|
||||||
|
strcpy(files->paths[files->count], path);
|
||||||
|
files->count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
strcpy(files->paths[files->count], path);
|
||||||
|
files->count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else ScanDirectoryFilesRecursively(path, files, filter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(dir);
|
||||||
|
}
|
||||||
|
else TRACELOG(LOG_WARNING, "FILEIO: Directory cannot be opened (%s)", basePath);
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
||||||
// GLFW3 Error Callback, runs on GLFW3 error
|
// GLFW3 Error Callback, runs on GLFW3 error
|
||||||
static void ErrorCallback(int error, const char *description)
|
static void ErrorCallback(int error, const char *description)
|
||||||
|
@ -5418,16 +5565,7 @@ static void CursorEnterCallback(GLFWwindow *window, int enter)
|
||||||
// Everytime new files are dropped, old ones are discarded
|
// Everytime new files are dropped, old ones are discarded
|
||||||
static void WindowDropCallback(GLFWwindow *window, int count, const char **paths)
|
static void WindowDropCallback(GLFWwindow *window, int count, const char **paths)
|
||||||
{
|
{
|
||||||
UnloadDroppedFiles();
|
CORE.Window.dropFilepaths = paths;
|
||||||
|
|
||||||
CORE.Window.dropFilepaths = (char **)RL_MALLOC(count*sizeof(char *));
|
|
||||||
|
|
||||||
for (int i = 0; i < count; i++)
|
|
||||||
{
|
|
||||||
CORE.Window.dropFilepaths[i] = (char *)RL_MALLOC(MAX_FILEPATH_LENGTH*sizeof(char));
|
|
||||||
strcpy(CORE.Window.dropFilepaths[i], paths[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
CORE.Window.dropFileCount = count;
|
CORE.Window.dropFileCount = count;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue