diff --git a/ion/src/simulator/android/src/cpp/images.cpp b/ion/src/simulator/android/src/cpp/images.cpp index 45a1f5c5f..c9a71ccfa 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_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) { +SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) { JNIEnv * env = static_cast(SDL_AndroidGetJNIEnv()); jobject activity = static_cast(SDL_AndroidGetActivity()); @@ -25,22 +25,17 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi AndroidBitmap_lockPixels(env, j_bitmap, &bitmapPixels); // TODO: Handle the case where lockPixels fails - SDL_Texture * texture = SDL_CreateTexture( - renderer, - SDL_PIXELFORMAT_ABGR8888, - SDL_TEXTUREACCESS_STATIC, + size_t bytesPerPixel = 4; + size_t bitsPerPixel = bytesPerPixel*8; + SDL_Surface * surface = SDL_CreateRGBSurfaceWithFormatFrom( + bitmapPixels, bitmapInfo.width, - bitmapInfo.height - ); - - SDL_UpdateTexture( - texture, - nullptr, - bitmapPixels, - 4 * bitmapInfo.width - ); + bitmapInfo.height, + bitsPerPixel, + bytesPerPixel * bitmapInfo.width, + SDL_PIXELFORMAT_ABGR8888); AndroidBitmap_unlockPixels(env, j_bitmap); - return texture; + return surface; } diff --git a/ion/src/simulator/ios/images.m b/ion/src/simulator/ios/images.m index c255aec61..673fdc9d2 100644 --- a/ion/src/simulator/ios/images.m +++ b/ion/src/simulator/ios/images.m @@ -3,7 +3,7 @@ #include #include -SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) { +SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) { CGImageRef cgImage = [[UIImage imageNamed:[NSString stringWithUTF8String:identifier]] CGImage]; if (cgImage == NULL) { return NULL; @@ -13,6 +13,7 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi size_t bytesPerPixel = 4; + size_t bitsPerPixel = bytesPerPixel*8; size_t bytesPerRow = bytesPerPixel * width; size_t bitsPerComponent = 8; @@ -30,22 +31,15 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi CGContextRelease(context); CGColorSpaceRelease(colorSpace); - SDL_Texture * texture = SDL_CreateTexture( - renderer, - SDL_PIXELFORMAT_ABGR8888, - SDL_TEXTUREACCESS_STATIC, - width, - height - ); - - SDL_UpdateTexture( - texture, - NULL, + SDL_Surface * surface = SDL_CreateRGBSurfaceWithFormatFrom( bitmapData, - 4 * width - ); + width, + height, + bitsPerPixel, + bytesPerRow, + SDL_PIXELFORMAT_ABGR8888); free(bitmapData); - return texture; + return surface; } diff --git a/ion/src/simulator/linux/images.cpp b/ion/src/simulator/linux/images.cpp index 48ac055c5..a4ce45b19 100644 --- a/ion/src/simulator/linux/images.cpp +++ b/ion/src/simulator/linux/images.cpp @@ -46,6 +46,7 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi int width = info.output_width; int height = info.output_height; int bytesPerPixel = info.output_components; + int bitsPerPixel = bytesPerPixel*8; unsigned char * bitmapData = new unsigned char[height * width * bytesPerPixel]; @@ -58,23 +59,16 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi jpeg_finish_decompress(&info); jpeg_destroy_decompress(&info); - Uint32 texturePixelFormat = SDL_PIXELFORMAT_RGB24; - assert(bytesPerPixel == SDL_BYTESPERPIXEL(texturePixelFormat)); + Uint32 surfacePixelFormat = SDL_PIXELFORMAT_RGB24; + assert(bytesPerPixel == SDL_BYTESPERPIXEL(surfacePixelFormat)); - SDL_Texture * texture = SDL_CreateTexture( - renderer, - texturePixelFormat, - SDL_TEXTUREACCESS_STATIC, - width, - height - ); - - SDL_UpdateTexture( - texture, - NULL, - bitmapData, - width * bytesPerPixel - ); + SDL_Surface * surface = SDL_CreateRGBSurfaceWithFormatFrom( + bitmapData, + width, + height, + bitsPerPixel, + width * bytesPerPixel, + surfacePixelFormat); delete[] bitmapData; diff --git a/ion/src/simulator/macos/images.m b/ion/src/simulator/macos/images.m index 8e19b3b49..030b8e4d0 100644 --- a/ion/src/simulator/macos/images.m +++ b/ion/src/simulator/macos/images.m @@ -3,7 +3,7 @@ #include #include -SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) { +SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) { NSImage * nsImage = [NSImage imageNamed:[NSString stringWithUTF8String:identifier]]; CGImageRef cgImage = [nsImage CGImageForProposedRect:NULL context:NULL @@ -14,8 +14,8 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi size_t width = CGImageGetWidth(cgImage); size_t height = CGImageGetHeight(cgImage); - size_t bytesPerPixel = 4; + size_t bitsPerPixel = bytesPerPixel*8; size_t bytesPerRow = bytesPerPixel * width; size_t bitsPerComponent = 8; @@ -33,22 +33,15 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi CGContextRelease(context); CGColorSpaceRelease(colorSpace); - SDL_Texture * texture = SDL_CreateTexture( - renderer, - SDL_PIXELFORMAT_ABGR8888, - SDL_TEXTUREACCESS_STATIC, - width, - height - ); - - SDL_UpdateTexture( - texture, - NULL, - bitmapData, - 4 * width - ); + SDL_Surface * surface = SDL_CreateRGBSurfaceWithFormatFrom( + bitmapData, + width, + height, + bitsPerPixel, + bytesPerRow, + SDL_PIXELFORMAT_ABGR8888); free(bitmapData); - return texture; + return surface; } diff --git a/ion/src/simulator/shared/layout.cpp b/ion/src/simulator/shared/layout.cpp index fbc265c93..b4cce47f6 100644 --- a/ion/src/simulator/shared/layout.cpp +++ b/ion/src/simulator/shared/layout.cpp @@ -202,10 +202,17 @@ 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 = IonSimulatorLoadImage(renderer, "background.jpg"); + sBackgroundTexture = textureFromSurface(renderer, IonSimulatorLoadImage(renderer, "background.jpg"), false, KDColorWhite); for (size_t i = 0; i < KeyLayout::NumberOfShapes; i++) { - sKeyLayoutTextures[i] = IonSimulatorLoadImage(renderer, KeyLayout::imagePathForKey[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); } diff --git a/ion/src/simulator/shared/platform.h b/ion/src/simulator/shared/platform.h index 494c83c23..545594267 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_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier); +SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier); char * IonSimulatorGetLanguageCode(); #if EPSILON_SDL_SCREEN_ONLY diff --git a/ion/src/simulator/windows/images.cpp b/ion/src/simulator/windows/images.cpp index 73feaf813..47a7b3b3f 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_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) { +SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) { Gdiplus::GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr); @@ -61,25 +61,20 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi Gdiplus::BitmapData * bitmapData = new Gdiplus::BitmapData; image->LockBits(&rc, Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, bitmapData); - SDL_Texture * texture = SDL_CreateTexture( - renderer, - SDL_PIXELFORMAT_ARGB8888, - SDL_TEXTUREACCESS_STATIC, - width, - height - ); - - SDL_UpdateTexture( - texture, - NULL, + size_t bytesPerPixel = 4; + size_t bitsPerPixel = bytesPerPixel*8; + SDL_Surface * surface = SDL_CreateRGBSurfaceWithFormatFrom( bitmapData->Scan0, - 4 * width - ); + width, + height, + bitsPerPixel, + bytesPerPixel*width, + SDL_PIXELFORMAT_ABGR8888); image->UnlockBits(bitmapData); delete bitmapData; delete image; Gdiplus::GdiplusShutdown(gdiplusToken); - return texture; + return surface; }