From 35fe34ba0f4a8de97e0854a64c52eca88bab98cf Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 11 May 2017 16:24:40 +0200 Subject: [PATCH] Added some useful functions --- src/core.c | 74 ++++++++++++++++++++++++++++++++++++++++------------ src/raylib.h | 7 +++-- 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/src/core.c b/src/core.c index cca8a1ac..fa368747 100644 --- a/src/core.c +++ b/src/core.c @@ -108,6 +108,16 @@ #include // Required for: strrchr(), strcmp() //#include // Macros for reporting and retrieving error conditions through error codes +#ifdef _WIN32 + #include // Required for: _getch(), _chdir() + #define GETCWD _getcwd // NOTE: MSDN recommends not to use getcwd(), chdir() + #define CHDIR _chdir +#else + #include "unistd.h" // Required for: getch(), chdir() (POSIX) + #define GETCWD getcwd + #define CHDIR chdir +#endif + #if defined(__linux__) || defined(PLATFORM_WEB) #include // Required for: timespec, nanosleep(), select() - POSIX #elif defined(__APPLE__) @@ -259,6 +269,7 @@ static Matrix downscaleView; // Matrix to downscale view (in case static const char *windowTitle; // Window text title... static bool cursorOnScreen = false; // Tracks if cursor is inside client area static bool cursorHidden = false; // Track if cursor is hidden +static int screenshotCounter = 0; // Screenshots counter // Register mouse states static char previousMouseState[3] = { 0 }; // Registers previous mouse button state @@ -1131,25 +1142,15 @@ void SetConfigFlags(char flags) // NOTE TraceLog() function is located in [utils.h] -// Takes a screenshot and saves it in the same folder as executable -void TakeScreenshot(void) +// Takes a screenshot of current screen (saved a .png) +void TakeScreenshot(const char *fileName) { #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) - static int shotNum = 0; // Screenshot number, increments every screenshot take during program execution - char buffer[20]; // Buffer to store file name - unsigned char *imgData = rlglReadScreenPixels(renderWidth, renderHeight); - - sprintf(buffer, "screenshot%03i.png", shotNum); - - // Save image as PNG - SavePNG(buffer, imgData, renderWidth, renderHeight, 4); - + SavePNG(fileName, imgData, renderWidth, renderHeight, 4); // Save image as PNG free(imgData); - shotNum++; - - TraceLog(INFO, "[%s] Screenshot taken #03i", buffer, shotNum); + TraceLog(INFO, "Screenshot taken: %s", fileName); #endif } @@ -1167,6 +1168,37 @@ bool IsFileExtension(const char *fileName, const char *ext) return result; } +// Get directory for a given fileName (with path) +const char *GetDirectoryPath(const char *fileName) +{ + char *lastSlash = NULL; + static char filePath[256]; // MAX_DIRECTORY_PATH_SIZE = 256 + memset(filePath, 0, 256); + + lastSlash = strrchr(fileName, '\\'); + strncpy(filePath, fileName, strlen(fileName) - (strlen(lastSlash) - 1)); + filePath[strlen(fileName) - strlen(lastSlash)] = '\0'; + + return filePath; +} + +// Get current working directory +const char *GetWorkingDirectory(void) +{ + static char currentDir[256]; // MAX_DIRECTORY_PATH_SIZE = 256 + memset(currentDir, 0, 256); + + GETCWD(currentDir, 256 - 1); + + return currentDir; +} + +// Change working directory, returns true if success +bool ChangeDirectory(const char *dir) +{ + return (CHDIR(dir) == 0); +} + #if defined(PLATFORM_DESKTOP) // Check if a file has been dropped into window bool IsFileDropped(void) @@ -2363,7 +2395,11 @@ static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, i // NOTE: Before closing window, while loop must be left! } #if defined(PLATFORM_DESKTOP) - else if (key == GLFW_KEY_F12 && action == GLFW_PRESS) TakeScreenshot(); + else if (key == GLFW_KEY_F12 && action == GLFW_PRESS) + { + TakeScreenshot(FormatText("screenshot%03i.png", screenshotCounter)); + screenshotCounter++; + } #endif else { @@ -3003,8 +3039,12 @@ static void ProcessKeyboard(void) // Check exit key (same functionality as GLFW3 KeyCallback()) if (currentKeyState[exitKey] == 1) windowShouldClose = true; - // Check screen capture key - if (currentKeyState[301] == 1) TakeScreenshot(); // raylib key: KEY_F12 (GLFW_KEY_F12) + // Check screen capture key (raylib key: KEY_F12) + if (currentKeyState[301] == 1) + { + TakeScreenshot(FormatText("screenshot%03i.png", screenshotCounter)); + screenshotCounter++; + } } // Restore default keyboard input diff --git a/src/raylib.h b/src/raylib.h index e49081b5..a3276d8e 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -711,9 +711,12 @@ RLAPI Color Fade(Color color, float alpha); // Color fade- RLAPI void ShowLogo(void); // Activate raylib logo at startup (can be done with flags) RLAPI void SetConfigFlags(char flags); // Setup window configuration flags (view FLAGS) RLAPI void TraceLog(int logType, const char *text, ...); // Show trace log messages (INFO, WARNING, ERROR, DEBUG) -RLAPI void TakeScreenshot(void); // Takes a screenshot and saves it in the same folder as executable -RLAPI bool IsFileExtension(const char *fileName, const char *ext);// Check file extension +RLAPI void TakeScreenshot(const char *fileName); // Takes a screenshot of current screen (saved a .png) +RLAPI bool IsFileExtension(const char *fileName, const char *ext);// Check file extension +RLAPI const char *GetDirectoryPath(const char *fileName); // Get directory for a given fileName (with path) +RLAPI const char *GetWorkingDirectory(void); // Get current working directory +RLAPI bool ChangeDirectory(const char *dir); // Change working directory, returns true if success RLAPI bool IsFileDropped(void); // Check if a file has been dropped into window RLAPI char **GetDroppedFiles(int *count); // Get dropped files names RLAPI void ClearDroppedFiles(void); // Clear dropped files paths buffer