From 057f7dc98f20963fc6c1d0c30f6b5ba0359d0bd1 Mon Sep 17 00:00:00 2001 From: evertonse Date: Wed, 6 Nov 2024 17:02:16 -0300 Subject: [PATCH] [rcore] using win32_clipboard on platforms rlfw and rgfw --- examples/core/core_clipboard_image.c | 34 ------------------------- src/platforms/rcore_desktop_glfw.c | 32 ++++++++++++++++++++++++ src/platforms/rcore_desktop_rgfw.c | 37 ++++++++++++++++++++++++++++ src/rcore.c | 2 +- 4 files changed, 70 insertions(+), 35 deletions(-) delete mode 100644 examples/core/core_clipboard_image.c diff --git a/examples/core/core_clipboard_image.c b/examples/core/core_clipboard_image.c deleted file mode 100644 index 685d1a5fb..000000000 --- a/examples/core/core_clipboard_image.c +++ /dev/null @@ -1,34 +0,0 @@ -#include "raylib.h" -#include -#include - -static Image img = {0}; -int main(int argc, char *argv[]) { - - InitWindow(800, 450, "[core] raylib clipboard image"); - SetTraceLogLevel(LOG_TRACE); - SetTargetFPS(60); - - Texture tex = {0}; - while(!WindowShouldClose()) { - if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_V)) { - #ifdef _WIN32 - img = GetClipboardImage(); - tex = LoadTextureFromImage(img); - if(!IsTextureValid(tex)) { - exit(98); - } else { - ExportImage(img, "Debug.bmp"); - } - #endif - } - - BeginDrawing(); - ClearBackground(RAYWHITE); - if (IsTextureValid(tex)) { - DrawTexture(tex, 0, 10 + 21, WHITE); - } - DrawText("Print Screen and Crtl+V", 10, 10, 21, BLACK); - EndDrawing(); - } -} diff --git a/src/platforms/rcore_desktop_glfw.c b/src/platforms/rcore_desktop_glfw.c index 45811e766..ecbc92aaa 100644 --- a/src/platforms/rcore_desktop_glfw.c +++ b/src/platforms/rcore_desktop_glfw.c @@ -58,6 +58,7 @@ #if defined(_WIN32) typedef void *PVOID; typedef PVOID HANDLE; + #include "../external/win32_clipboard.h" typedef HANDLE HWND; #define GLFW_EXPOSE_NATIVE_WIN32 #define GLFW_NATIVE_INCLUDE_NONE // To avoid some symbols re-definition in windows.h @@ -966,6 +967,33 @@ const char *GetClipboardText(void) return glfwGetClipboardString(platform.handle); } +#if defined(SUPPORT_CLIPBOARD_IMAGE) +// Get clipboard image +Image GetClipboardImage(void) +{ + Image image = {0}; + unsigned long long int dataSize = 0; + void* fileData = NULL; + +#ifdef _WIN32 + int width, height; + fileData = (void*)Win32GetClipboardImageData(&width, &height, &dataSize); +#else + TRACELOG(LOG_WARNING, "Clipboard image: PLATFORM_GLFW doesn't implement `GetClipboardImage` for this OS"); +#endif + + if (fileData == NULL) + { + TRACELOG(LOG_WARNING, "Clipboard image: Couldn't get clipboard data."); + } + else + { + image = LoadImageFromMemory(".bmp", fileData, dataSize); + } + return image; +} +#endif // SUPPORT_CLIPBOARD_IMAGE + // Show mouse cursor void ShowCursor(void) { @@ -1898,4 +1926,8 @@ static void JoystickCallback(int jid, int event) } } +#ifdef _WIN32 +# define WIN32_CLIPBOARD_IMPLEMENTATION +# include "../external/win32_clipboard.h" +#endif // EOF diff --git a/src/platforms/rcore_desktop_rgfw.c b/src/platforms/rcore_desktop_rgfw.c index 68885ce3f..a8618e98e 100644 --- a/src/platforms/rcore_desktop_rgfw.c +++ b/src/platforms/rcore_desktop_rgfw.c @@ -664,6 +664,43 @@ const char *GetClipboardText(void) return RGFW_readClipboard(NULL); } + +#if defined(SUPPORT_CLIPBOARD_IMAGE) + +#ifdef _WIN32 +# define WIN32_CLIPBOARD_IMPLEMENTATION +# define WINUSER_ALREADY_INCLUDED +# define WINBASE_ALREADY_INCLUDED +# define WINGDI_ALREADY_INCLUDED +# include "../external/win32_clipboard.h" +#endif + +// Get clipboard image +Image GetClipboardImage(void) +{ + Image image = {0}; + unsigned long long int dataSize = 0; + void* fileData = NULL; + +#ifdef _WIN32 + int width, height; + fileData = (void*)Win32GetClipboardImageData(&width, &height, &dataSize); +#else + TRACELOG(LOG_WARNING, "Clipboard image: PLATFORM_GLFW doesn't implement `GetClipboardImage` for this OS"); +#endif + + if (fileData == NULL) + { + TRACELOG(LOG_WARNING, "Clipboard image: Couldn't get clipboard data."); + } + else + { + image = LoadImageFromMemory(".bmp", fileData, dataSize); + } + return image; +} +#endif // SUPPORT_CLIPBOARD_IMAGE + // Show mouse cursor void ShowCursor(void) { diff --git a/src/rcore.c b/src/rcore.c index 119e48382..2d1808c14 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -514,7 +514,7 @@ const char *TextFormat(const char *text, ...); // Formatting of tex #if defined(SUPPORT_CLIPBOARD_IMAGE) #if !defined(SUPPORT_FILEFORMAT_BMP) || !defined(STBI_REQUIRED) || !defined(SUPPORT_MODULE_RTEXTURES) - #error "To enabled SUPPORT_CLIPBOARD_IMAGE, it also needs SUPPORT_FILEFORMAT_BMP, SUPPORT_MODULE_RTEXTURES and STBI_REQUIRED to be defined" + #error "To enabled SUPPORT_CLIPBOARD_IMAGE, it also needs SUPPORT_FILEFORMAT_BMP, SUPPORT_MODULE_RTEXTURES and STBI_REQUIRED to be defined. It should have been defined earlier" #endif #endif // SUPPORT_CLIPBOARD_IMAGE