REVIEWED: core_drop_files #2943

This commit is contained in:
Ray 2023-03-03 20:13:35 +01:00
parent 394b31faff
commit 6897b43599

View file

@ -15,6 +15,11 @@
#include "raylib.h" #include "raylib.h"
#include <stdlib.h> // Required for: calloc(), free()
#define MAX_FILEPATH_RECORDED 4096
#define MAX_FILEPATH_SIZE 2048
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Program main entry point // Program main entry point
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
@ -27,7 +32,14 @@ int main(void)
InitWindow(screenWidth, screenHeight, "raylib [core] example - drop files"); InitWindow(screenWidth, screenHeight, "raylib [core] example - drop files");
FilePathList droppedFiles = { 0 }; int filePathCounter = 0;
char *filePaths[MAX_FILEPATH_RECORDED] = { 0 }; // We will register a maximum of filepaths
// Allocate space for the required file paths
for (int i = 0; i < MAX_FILEPATH_RECORDED; i++)
{
filePaths[i] = (char *)RL_CALLOC(MAX_FILEPATH_SIZE, 1);
}
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -39,11 +51,18 @@ int main(void)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
if (IsFileDropped()) if (IsFileDropped())
{ {
// Is some files have been previously loaded, unload them FilePathList droppedFiles = LoadDroppedFiles();
if (droppedFiles.count > 0) UnloadDroppedFiles(droppedFiles);
for (int i = 0, offset = filePathCounter; i < droppedFiles.count; i++)
// Load new dropped files {
droppedFiles = LoadDroppedFiles(); if (filePathCounter < (MAX_FILEPATH_RECORDED - 1))
{
TextCopy(filePaths[offset + i], droppedFiles.paths[i]);
filePathCounter++;
}
}
UnloadDroppedFiles(droppedFiles); // Unload filepaths from memory
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -53,20 +72,20 @@ int main(void)
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
if (droppedFiles.count == 0) DrawText("Drop your files to this window!", 100, 40, 20, DARKGRAY); if (filePathCounter == 0) DrawText("Drop your files to this window!", 100, 40, 20, DARKGRAY);
else else
{ {
DrawText("Dropped files:", 100, 40, 20, DARKGRAY); DrawText("Dropped files:", 100, 40, 20, DARKGRAY);
for (unsigned int i = 0; i < droppedFiles.count; i++) for (unsigned int i = 0; i < filePathCounter; i++)
{ {
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.paths[i], 120, 100 + 40*i, 10, GRAY); DrawText(filePaths[i], 120, 100 + 40*i, 10, GRAY);
} }
DrawText("Drop new files...", 100, 110 + 40*droppedFiles.count, 20, DARKGRAY); DrawText("Drop new files...", 100, 110 + 40*filePathCounter, 20, DARKGRAY);
} }
EndDrawing(); EndDrawing();
@ -75,7 +94,10 @@ int main(void)
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UnloadDroppedFiles(droppedFiles); // Unload files memory for (int i = 0; i < MAX_FILEPATH_RECORDED; i++)
{
RL_FREE(filePaths[i]); // Free allocated memory for all filepaths
}
CloseWindow(); // Close window and OpenGL context CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------