[ion/simulator] Unify iOS/macOS image loading

This commit is contained in:
Romain Goyet
2020-09-11 16:56:30 -04:00
committed by Léa Saviot
parent a4213dcca8
commit ab1df4fbef
4 changed files with 16 additions and 71 deletions

View File

@@ -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 \

View File

@@ -1,62 +0,0 @@
#include "../shared/platform.h"
#include <SDL.h>
#include <UIKit/UIKit.h>
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;
}
}
}
}

View File

@@ -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 \

View File

@@ -1,17 +1,27 @@
#include "../shared/platform.h"
#include <SDL.h>
#include <TargetConditionals.h>
#if TARGET_OS_MAC
#include <AppKit/AppKit.h>
#else
#include <UIKit/UIKit.h>
#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;
}