From 8b5caeb39474777557e19929091c0005f9fbc9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Mon, 14 Sep 2020 14:24:34 +0200 Subject: [PATCH] [ion] IonSimulatorLoadImage returns a Texture (otherwise the pixels data which needs to be deleted when freeing the surface is hard to retrieve) --- ion/src/simulator/android/src/cpp/images.cpp | 12 +++++++++--- ion/src/simulator/ios/images.m | 12 +++++++++--- ion/src/simulator/linux/images.cpp | 8 +++++++- ion/src/simulator/macos/images.m | 12 +++++++++--- ion/src/simulator/shared/layout.cpp | 13 ++----------- ion/src/simulator/shared/platform.h | 2 +- ion/src/simulator/windows/images.cpp | 10 ++++++++-- 7 files changed, 45 insertions(+), 24 deletions(-) diff --git a/ion/src/simulator/android/src/cpp/images.cpp b/ion/src/simulator/android/src/cpp/images.cpp index c9a71ccfa..02fd9f5e7 100644 --- a/ion/src/simulator/android/src/cpp/images.cpp +++ b/ion/src/simulator/android/src/cpp/images.cpp @@ -3,7 +3,7 @@ #include #include -SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) { +SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier, bool withTransparency, uint8_t alpha) { JNIEnv * env = static_cast(SDL_AndroidGetJNIEnv()); jobject activity = static_cast(SDL_AndroidGetActivity()); @@ -35,7 +35,13 @@ SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi bytesPerPixel * bitmapInfo.width, SDL_PIXELFORMAT_ABGR8888); - AndroidBitmap_unlockPixels(env, j_bitmap); + SDL_SetColorKey(surface, withTransparency, SDL_MapRGB(surface->format, 0xFF, 0xFF, 0xFF)); + SDL_SetSurfaceAlphaMod(surface, alpha); - return surface; + SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface); + + AndroidBitmap_unlockPixels(env, j_bitmap); + SDL_FreeSurface(surface); + + return texture; } diff --git a/ion/src/simulator/ios/images.m b/ion/src/simulator/ios/images.m index 673fdc9d2..1830fc617 100644 --- a/ion/src/simulator/ios/images.m +++ b/ion/src/simulator/ios/images.m @@ -3,7 +3,7 @@ #include #include -SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) { +SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier, bool withTransparency, uint8_t alpha) { CGImageRef cgImage = [[UIImage imageNamed:[NSString stringWithUTF8String:identifier]] CGImage]; if (cgImage == NULL) { return NULL; @@ -39,7 +39,13 @@ SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi bytesPerRow, SDL_PIXELFORMAT_ABGR8888); - free(bitmapData); + SDL_SetColorKey(surface, withTransparency, SDL_MapRGB(surface->format, 0xFF, 0xFF, 0xFF)); + SDL_SetSurfaceAlphaMod(surface, alpha); - return surface; + SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface); + + free(bitmapData); + SDL_FreeSurface(surface); + + return texture; } diff --git a/ion/src/simulator/linux/images.cpp b/ion/src/simulator/linux/images.cpp index a4ce45b19..632a0bcbf 100644 --- a/ion/src/simulator/linux/images.cpp +++ b/ion/src/simulator/linux/images.cpp @@ -17,7 +17,7 @@ static struct { ASSETS_ADDRESS_RANGES_DEFINITION }; -SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) { +SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier, bool withTransparency, uint8_t alpha) { struct jpeg_decompress_struct info; struct jpeg_error_mgr err; info.err = jpeg_std_error(&err); @@ -70,7 +70,13 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi width * bytesPerPixel, surfacePixelFormat); + SDL_SetColorKey(surface, withTransparency, SDL_MapRGB(surface->format, 0xFF, 0xFF, 0xFF)); + SDL_SetSurfaceAlphaMod(surface, alpha); + + SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface); + delete[] bitmapData; + SDL_FreeSurface(surface); return texture; } diff --git a/ion/src/simulator/macos/images.m b/ion/src/simulator/macos/images.m index 030b8e4d0..57f839e3e 100644 --- a/ion/src/simulator/macos/images.m +++ b/ion/src/simulator/macos/images.m @@ -3,7 +3,7 @@ #include #include -SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) { +SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier, bool withTransparency, uint8_t alpha) { NSImage * nsImage = [NSImage imageNamed:[NSString stringWithUTF8String:identifier]]; CGImageRef cgImage = [nsImage CGImageForProposedRect:NULL context:NULL @@ -41,7 +41,13 @@ SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi bytesPerRow, SDL_PIXELFORMAT_ABGR8888); - free(bitmapData); + SDL_SetColorKey(surface, withTransparency, SDL_MapRGB(surface->format, 0xFF, 0xFF, 0xFF)); + SDL_SetSurfaceAlphaMod(surface, alpha); - return surface; + SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface); + + free(bitmapData); + SDL_FreeSurface(surface); + + return texture; } diff --git a/ion/src/simulator/shared/layout.cpp b/ion/src/simulator/shared/layout.cpp index b4cce47f6..1adb138d7 100644 --- a/ion/src/simulator/shared/layout.cpp +++ b/ion/src/simulator/shared/layout.cpp @@ -202,19 +202,10 @@ static constexpr uint8_t k_blendingRatio = 0x44; static SDL_Texture * sBackgroundTexture = nullptr; static SDL_Texture * sKeyLayoutTextures[KeyLayout::NumberOfShapes]; -SDL_Texture * textureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface, bool transparencyFlag, KDColor transparentColor) { - SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface); - SDL_SetColorKey(surface, transparencyFlag, SDL_MapRGB(surface->format, transparentColor.red(), transparentColor.green(), transparentColor.blue())); - SDL_FreeSurface(surface); - return texture; -} - void init(SDL_Renderer * renderer) { - sBackgroundTexture = textureFromSurface(renderer, IonSimulatorLoadImage(renderer, "background.jpg"), false, KDColorWhite); + sBackgroundTexture = IonSimulatorLoadImage(renderer, "background.jpg", false, 0xFF); for (size_t i = 0; i < KeyLayout::NumberOfShapes; i++) { - sKeyLayoutTextures[i] = textureFromSurface(renderer, IonSimulatorLoadImage(renderer, KeyLayout::imagePathForKey[i]), true, KDColorWhite); - SDL_SetTextureBlendMode(sKeyLayoutTextures[i], SDL_BLENDMODE_BLEND); - SDL_SetTextureAlphaMod(sKeyLayoutTextures[i], k_blendingRatio); + sKeyLayoutTextures[i] = IonSimulatorLoadImage(renderer, KeyLayout::imagePathForKey[i], true, k_blendingRatio); } } diff --git a/ion/src/simulator/shared/platform.h b/ion/src/simulator/shared/platform.h index 545594267..ce566a4a9 100644 --- a/ion/src/simulator/shared/platform.h +++ b/ion/src/simulator/shared/platform.h @@ -11,7 +11,7 @@ extern "C" { /* Those functions should be implemented per-platform. * They are defined as C function for easier interop. */ -SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier); +SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier, bool withTransparency, uint8_t alpha); char * IonSimulatorGetLanguageCode(); #if EPSILON_SDL_SCREEN_ONLY diff --git a/ion/src/simulator/windows/images.cpp b/ion/src/simulator/windows/images.cpp index 47a7b3b3f..aade41f1f 100644 --- a/ion/src/simulator/windows/images.cpp +++ b/ion/src/simulator/windows/images.cpp @@ -36,7 +36,7 @@ HRESULT CreateStreamOnResource(const char * name, LPSTREAM * stream) { return hr; } -SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) { +SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier, bool withTransparency, uint8_t alpha) { Gdiplus::GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr); @@ -71,10 +71,16 @@ SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi bytesPerPixel*width, SDL_PIXELFORMAT_ABGR8888); + SDL_SetColorKey(surface, withTransparency, SDL_MapRGB(surface->format, 0xFF, 0xFF, 0xFF)); + SDL_SetSurfaceAlphaMod(surface, alpha); + + SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface); + image->UnlockBits(bitmapData); delete bitmapData; delete image; Gdiplus::GdiplusShutdown(gdiplusToken); + SDL_FreeSurface(surface); - return surface; + return texture; }