mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[ion] IonSimulatorLoadImage returns a surface instead of a texture: this
way we can set a transparent color key before turning it into a texture.
This commit is contained in:
committed by
EmilieNumworks
parent
674703f4dc
commit
02b648e36d
@@ -3,7 +3,7 @@
|
||||
#include <jni.h>
|
||||
#include <android/bitmap.h>
|
||||
|
||||
SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) {
|
||||
SDL_Surface * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) {
|
||||
JNIEnv * env = static_cast<JNIEnv *>(SDL_AndroidGetJNIEnv());
|
||||
jobject activity = static_cast<jobject>(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;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <SDL.h>
|
||||
#include <UIKit/UIKit.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <SDL.h>
|
||||
#include <AppKit/AppKit.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user