From ab1df4fbefa180de70f6e84392a0ecd2d0ddf5e9 Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Fri, 11 Sep 2020 16:56:30 -0400 Subject: [PATCH] [ion/simulator] Unify iOS/macOS image loading --- ion/src/simulator/ios/Makefile | 5 +- ion/src/simulator/ios/platform_images.mm | 62 ------------------- ion/src/simulator/macos/Makefile | 2 +- .../apple}/platform_images.mm | 18 ++++-- 4 files changed, 16 insertions(+), 71 deletions(-) delete mode 100644 ion/src/simulator/ios/platform_images.mm rename ion/src/simulator/{macos => shared/apple}/platform_images.mm (74%) diff --git a/ion/src/simulator/ios/Makefile b/ion/src/simulator/ios/Makefile index 6a51f63d3..efb5cd9a8 100644 --- a/ion/src/simulator/ios/Makefile +++ b/ion/src/simulator/ios/Makefile @@ -1,8 +1,5 @@ -ion_src += $(addprefix ion/src/simulator/ios/, \ - platform_images.mm \ -) - ion_src += $(addprefix ion/src/simulator/shared/, \ + apple/platform_images.mm \ apple/platform_language.mm \ dummy/haptics_enabled.cpp \ dummy/journal.cpp \ diff --git a/ion/src/simulator/ios/platform_images.mm b/ion/src/simulator/ios/platform_images.mm deleted file mode 100644 index 3d3bb70b2..000000000 --- a/ion/src/simulator/ios/platform_images.mm +++ /dev/null @@ -1,62 +0,0 @@ -#include "../shared/platform.h" -#include -#include - -namespace Ion { -namespace Simulator { -namespace Platform { - -SDL_Texture * loadImage(SDL_Renderer * renderer, const char * identifier) { - CGImageRef cgImage = [[UIImage imageNamed:[NSString stringWithUTF8String:identifier]] CGImage]; - if (cgImage == NULL) { - return NULL; - } - size_t width = CGImageGetWidth(cgImage); - size_t height = CGImageGetHeight(cgImage); - - - size_t bytesPerPixel = 4; - size_t bytesPerRow = bytesPerPixel * width; - size_t bitsPerComponent = 8; - - size_t size = height * width * bytesPerPixel; - void * bitmapData = malloc(size); - memset(bitmapData, 0, size); - - CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); - CGContextRef context = CGBitmapContextCreate( - bitmapData, width, height, - bitsPerComponent, bytesPerRow, colorSpace, - kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big - ); - - CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage); - - CGContextRelease(context); - CGColorSpaceRelease(colorSpace); - - SDL_Texture * texture = SDL_CreateTexture( - renderer, - SDL_PIXELFORMAT_ABGR8888, - SDL_TEXTUREACCESS_STATIC, - width, - height - ); - - SDL_UpdateTexture( - texture, - NULL, - bitmapData, - bytesPerPixel * width - ); - - SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); - - free(bitmapData); - - return texture; -} - -} -} -} diff --git a/ion/src/simulator/macos/Makefile b/ion/src/simulator/macos/Makefile index 52a76b36a..8fc361b7e 100644 --- a/ion/src/simulator/macos/Makefile +++ b/ion/src/simulator/macos/Makefile @@ -1,9 +1,9 @@ ion_src += $(addprefix ion/src/simulator/macos/, \ platform_files.mm \ - platform_images.mm \ ) ion_src += $(addprefix ion/src/simulator/shared/, \ + apple/platform_images.mm \ apple/platform_language.mm \ dummy/haptics_enabled.cpp \ dummy/keyboard_callback.cpp \ diff --git a/ion/src/simulator/macos/platform_images.mm b/ion/src/simulator/shared/apple/platform_images.mm similarity index 74% rename from ion/src/simulator/macos/platform_images.mm rename to ion/src/simulator/shared/apple/platform_images.mm index 1cf9f61a9..1b68a11f3 100644 --- a/ion/src/simulator/macos/platform_images.mm +++ b/ion/src/simulator/shared/apple/platform_images.mm @@ -1,17 +1,27 @@ #include "../shared/platform.h" - #include +#include +#if TARGET_OS_MAC #include +#else +#include +#endif namespace Ion { namespace Simulator { namespace Platform { SDL_Texture * loadImage(SDL_Renderer * renderer, const char * identifier) { + CGImageRef cgImage = NULL; +#if TARGET_OS_MAC + //http://lists.libsdl.org/pipermail/commits-libsdl.org/2016-December/001235.html + [[[NSApp windows] firstObject] setColorSpace:[NSColorSpace sRGBColorSpace]]; + NSImage * nsImage = [NSImage imageNamed:[NSString stringWithUTF8String:identifier]]; - CGImageRef cgImage = [nsImage CGImageForProposedRect:NULL - context:NULL - hints:0]; + cgImage = [nsImage CGImageForProposedRect:NULL context:NULL hints:0]; +#else + cgImage = [[UIImage imageNamed:[NSString stringWithUTF8String:identifier]] CGImage]; +#endif if (cgImage == NULL) { return NULL; }