From e2ed4884ba2cbf458d6df8fc01f46313c38a819a Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 19 Jun 2022 17:13:13 +1000 Subject: [PATCH] Fix iOS compilation issues --- src/Drawer2D.c | 50 +++++++++++++++-------------------------------- src/Drawer2D.h | 10 ++++------ src/LBackend.c | 12 ++++++------ src/Launcher.c | 36 +++++++++++++++++++++++++++------- src/Launcher.h | 1 + src/interop_ios.m | 27 ++++++++++++++----------- 6 files changed, 72 insertions(+), 64 deletions(-) diff --git a/src/Drawer2D.c b/src/Drawer2D.c index 4aba9ac0b..58681e494 100644 --- a/src/Drawer2D.c +++ b/src/Drawer2D.c @@ -150,15 +150,15 @@ static void Font_SysTextDraw(struct DrawTextArgs* args, struct Bitmap* bmp, int /*########################################################################################################################* *---------------------------------------------------Drawing functions-----------------------------------------------------* *#########################################################################################################################*/ -cc_bool Drawer2D_Clamp(struct Bitmap* bmp, int* x, int* y, int* width, int* height) { - if (*x >= bmp->width || *y >= bmp->height) return false; +cc_bool Drawer2D_Clamp(struct Context2D* ctx, int* x, int* y, int* width, int* height) { + if (*x >= ctx->width || *y >= ctx->height) return false; /* origin is negative, move inside */ if (*x < 0) { *width += *x; *x = 0; } if (*y < 0) { *height += *y; *y = 0; } - *width = min(*x + *width, bmp->width) - *x; - *height = min(*y + *height, bmp->height) - *y; + *width = min(*x + *width, ctx->width) - *x; + *height = min(*y + *height, ctx->height) - *y; return *width > 0 && *height > 0; } #define Drawer2D_ClampPixel(p) p = (p < 0 ? 0 : (p > 255 ? 255 : p)) @@ -177,6 +177,13 @@ void Context2D_Alloc(struct Context2D* ctx, int width, int height) { ctx->bmp.scan0 = (BitmapCol*)Mem_AllocCleared(width * height, 4, "bitmap data"); } +void Context2D_Wrap(struct Context2D* ctx, struct Bitmap* bmp) { + ctx->bmp = *bmp; + ctx->width = bmp->width; + ctx->height = bmp->height; + ctx->meta = NULL; +} + void Context2D_Free(struct Context2D* ctx) { Mem_Free(ctx->bmp.scan0); } @@ -187,7 +194,7 @@ void Gradient_Noise(struct Context2D* ctx, BitmapCol color, int variation, BitmapCol* dst; int R, G, B, xx, yy, n; float noise; - if (!Drawer2D_Clamp(bmp, &x, &y, &width, &height)) return; + if (!Drawer2D_Clamp(ctx, &x, &y, &width, &height)) return; for (yy = 0; yy < height; yy++) { dst = Bitmap_GetRow(bmp, y + yy) + x; @@ -212,7 +219,7 @@ void Gradient_Vertical(struct Context2D* ctx, BitmapCol a, BitmapCol b, BitmapCol* row, color; int xx, yy; float t; - if (!Drawer2D_Clamp(bmp, &x, &y, &width, &height)) return; + if (!Drawer2D_Clamp(ctx, &x, &y, &width, &height)) return; for (yy = 0; yy < height; yy++) { row = Bitmap_GetRow(bmp, y + yy) + x; @@ -233,7 +240,7 @@ void Gradient_Blend(struct Context2D* ctx, BitmapCol color, int blend, struct Bitmap* bmp = (struct Bitmap*)ctx; BitmapCol* dst; int R, G, B, xx, yy; - if (!Drawer2D_Clamp(bmp, &x, &y, &width, &height)) return; + if (!Drawer2D_Clamp(ctx, &x, &y, &width, &height)) return; /* Pre compute the alpha blended source color */ /* TODO: Avoid shift when multiplying */ @@ -258,38 +265,13 @@ void Gradient_Blend(struct Context2D* ctx, BitmapCol color, int blend, } } -void Gradient_Tint(struct Context2D* ctx, cc_uint8 tintA, cc_uint8 tintB, - int x, int y, int width, int height) { - struct Bitmap* bmp = (struct Bitmap*)ctx; - BitmapCol* row, color; - cc_uint8 tint; - int xx, yy; - if (!Drawer2D_Clamp(bmp, &x, &y, &width, &height)) return; - - for (yy = 0; yy < height; yy++) { - row = Bitmap_GetRow(bmp, y + yy) + x; - tint = (cc_uint8)Math_Lerp(tintA, tintB, (float)yy / height); - - for (xx = 0; xx < width; xx++) { - /* TODO: Not shift when multiplying */ - color = BitmapCol_Make( - BitmapCol_R(row[xx]) * tint / 255, - BitmapCol_G(row[xx]) * tint / 255, - BitmapCol_B(row[xx]) * tint / 255, - 0); - - row[xx] = color | (row[xx] & BITMAPCOL_A_MASK); - } - } -} - void Context2D_DrawPixels(struct Context2D* ctx, int x, int y, struct Bitmap* src) { struct Bitmap* dst = (struct Bitmap*)ctx; int width = src->width, height = src->height; BitmapCol* dstRow; BitmapCol* srcRow; int xx, yy; - if (!Drawer2D_Clamp(dst, &x, &y, &width, &height)) return; + if (!Drawer2D_Clamp(ctx, &x, &y, &width, &height)) return; for (yy = 0; yy < height; yy++) { srcRow = Bitmap_GetRow(src, yy); @@ -304,7 +286,7 @@ void Context2D_Clear(struct Context2D* ctx, BitmapCol color, struct Bitmap* bmp = (struct Bitmap*)ctx; BitmapCol* row; int xx, yy; - if (!Drawer2D_Clamp(bmp, &x, &y, &width, &height)) return; + if (!Drawer2D_Clamp(ctx, &x, &y, &width, &height)) return; for (yy = 0; yy < height; yy++) { row = Bitmap_GetRow(bmp, y + yy) + x; diff --git a/src/Drawer2D.h b/src/Drawer2D.h index 93472029f..3f89d801e 100644 --- a/src/Drawer2D.h +++ b/src/Drawer2D.h @@ -31,14 +31,16 @@ void DrawTextArgs_MakeEmpty(struct DrawTextArgs* args, struct FontDesc* font, cc /* Clamps the given rectangle to lie inside the bitmap */ /* Returns false if rectangle is completely outside bitmap's rectangle */ -cc_bool Drawer2D_Clamp(struct Bitmap* bmp, int* x, int* y, int* width, int* height); +cc_bool Drawer2D_Clamp(struct Context2D* ctx, int* x, int* y, int* width, int* height); /* Allocates a new context for 2D drawing */ /* Note: Allocates a power-of-2 sized backing bitmap equal to or greater than the given size */ CC_API void Context2D_Alloc(struct Context2D* ctx, int width, int height); +/* Allocates a new context for 2D drawing, using an existing bimap as backing bitmap */ +CC_API void Context2D_Wrap(struct Context2D* ctx, struct Bitmap* bmp); /* Frees/Releases a previously allocatedcontext for 2D drawing */ CC_API void Context2D_Free(struct Context2D* ctx); -/* Creates a texture consisting of the pixels from the bitmap underlying the given 2D context */ +/* Creates a texture consisting of the pixels from the backing bitmap of the given 2D context */ CC_API void Context2D_MakeTexture(struct Texture* tex, struct Context2D* ctx); /* Draws text using the given font at the given coordinates */ @@ -61,10 +63,6 @@ CC_API void Gradient_Vertical(struct Context2D* ctx, BitmapCol a, BitmapCol b, /* Note that this only blends RGB, A is not blended */ CC_API void Gradient_Blend(struct Context2D* ctx, BitmapCol color, int blend, int x, int y, int width, int height); -/* Tints the given area, linearly interpolating from a to b */ -/* Note that this only tints RGB, A is not tinted */ -CC_API void Gradient_Tint(struct Context2D* ctx, cc_uint8 tintA, cc_uint8 tintB, - int x, int y, int width, int height); /* Returns how wide the given text would be when drawn */ CC_API int Drawer2D_TextWidth(struct DrawTextArgs* args); diff --git a/src/LBackend.c b/src/LBackend.c index af653f57a..e5461ecf9 100644 --- a/src/LBackend.c +++ b/src/LBackend.c @@ -155,7 +155,7 @@ static CC_NOINLINE void MarkAllDirty(void) { /* Marks the given area/region as needing to be redrawn. */ static CC_NOINLINE void MarkAreaDirty(int x, int y, int width, int height) { int x1, y1, x2, y2; - if (!Drawer2D_Clamp(&framebuffer.bmp, &x, &y, &width, &height)) return; + if (!Drawer2D_Clamp(&framebuffer, &x, &y, &width, &height)) return; /* union with existing dirty area */ if (dirty_rect.Width) { @@ -174,12 +174,12 @@ static CC_NOINLINE void MarkAreaDirty(int x, int y, int width, int height) { } void LBackend_InitFramebuffer(void) { - framebuffer.bmp.width = max(WindowInfo.Width, 1); - framebuffer.bmp.height = max(WindowInfo.Height, 1); + struct Bitmap bmp; + bmp.width = max(WindowInfo.Width, 1); + bmp.height = max(WindowInfo.Height, 1); - Window_AllocFramebuffer(&framebuffer.bmp); - framebuffer.width = framebuffer.bmp.width; - framebuffer.height = framebuffer.bmp.height; + Window_AllocFramebuffer(&bmp); + Context2D_Wrap(&framebuffer, &bmp); } void LBackend_FreeFramebuffer(void) { diff --git a/src/Launcher.c b/src/Launcher.c index e45bc8929..76b6965a9 100644 --- a/src/Launcher.c +++ b/src/Launcher.c @@ -360,10 +360,26 @@ void Launcher_SaveTheme(void) { /*########################################################################################################################* *---------------------------------------------------------Texture pack----------------------------------------------------* *#########################################################################################################################*/ -static cc_bool Launcher_SelectZipEntry(const cc_string* path) { - return - String_CaselessEqualsConst(path, "default.png") || - String_CaselessEqualsConst(path, "terrain.png"); +/* Tints the given area, linearly interpolating from a to b */ +/* Note that this only tints RGB, A is not tinted */ +static void TintBitmap(struct Bitmap* bmp, cc_uint8 tintA, cc_uint8 tintB, int width, int height) { + BitmapCol* row; + cc_uint8 tint; + int xx, yy; + + for (yy = 0; yy < height; yy++) { + row = Bitmap_GetRow(bmp, yy); + tint = (cc_uint8)Math_Lerp(tintA, tintB, (float)yy / height); + + for (xx = 0; xx < width; xx++) { + /* TODO: Not shift when multiplying */ + row[xx] = BitmapCol_Make( + BitmapCol_R(row[xx]) * tint / 255, + BitmapCol_G(row[xx]) * tint / 255, + BitmapCol_B(row[xx]) * tint / 255, + 255); + } + } } static void ExtractTerrainTiles(struct Bitmap* bmp) { @@ -375,8 +391,14 @@ static void ExtractTerrainTiles(struct Bitmap* bmp) { Bitmap_Scale(&dirtBmp, bmp, 2 * tileSize, 0, tileSize, tileSize); Bitmap_Scale(&stoneBmp, bmp, 1 * tileSize, 0, tileSize, tileSize); - Gradient_Tint(&dirtBmp, 128, 64, 0, 0, TILESIZE, TILESIZE); - Gradient_Tint(&stoneBmp, 96, 96, 0, 0, TILESIZE, TILESIZE); + TintBitmap(&dirtBmp, 128, 64, TILESIZE, TILESIZE); + TintBitmap(&stoneBmp, 96, 96, TILESIZE, TILESIZE); +} + +static cc_bool Launcher_SelectZipEntry(const cc_string* path) { + return + String_CaselessEqualsConst(path, "default.png") || + String_CaselessEqualsConst(path, "terrain.png"); } static cc_result Launcher_ProcessZipEntry(const cc_string* path, struct Stream* data, struct ZipState* s) { @@ -455,7 +477,7 @@ CC_NOINLINE static void ClearTile(int x, int y, int width, int height, BitmapCol* dstRow; BitmapCol* srcRow; int xx, yy; - if (!Drawer2D_Clamp(dst, &x, &y, &width, &height)) return; + if (!Drawer2D_Clamp(ctx, &x, &y, &width, &height)) return; for (yy = 0; yy < height; yy++) { srcRow = Bitmap_GetRow(src, (y + yy) % TILESIZE); diff --git a/src/Launcher.h b/src/Launcher.h index 92846e36d..4424d5e8c 100644 --- a/src/Launcher.h +++ b/src/Launcher.h @@ -6,6 +6,7 @@ */ struct LScreen; struct FontDesc; +struct Context2D; /* The screen/menu currently being shown */ extern struct LScreen* Launcher_Active; diff --git a/src/interop_ios.m b/src/interop_ios.m index f92c57a64..3478a309d 100644 --- a/src/interop_ios.m +++ b/src/interop_ios.m @@ -789,20 +789,20 @@ void LBackend_Tick(void) { } void LBackend_Free(void) { } void LBackend_UpdateLogoFont(void) { } -static void DrawText(NSAttributedString* str, struct Bitmap* bmp, int x, int y) { +static void DrawText(NSAttributedString* str, struct Context2D* ctx, int x, int y) { CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)str); CGRect bounds = CTLineGetImageBounds(line, win_ctx); - int centreX = (int)(bmp->width / 2.0f - bounds.size.width / 2.0f); + int centreX = (int)(ctx->width / 2.0f - bounds.size.width / 2.0f); - CGContextSetTextPosition(win_ctx, centreX + x, bmp->height - y); + CGContextSetTextPosition(win_ctx, centreX + x, ctx->height - y); CTLineDraw(line, win_ctx); } -void LBackend_DrawLogo(struct Bitmap* bmp, const char* title) { +void LBackend_DrawLogo(struct Context2D* ctx, const char* title) { if (Launcher_BitmappedText()) { struct FontDesc font; Launcher_MakeLogoFont(&font); - Launcher_DrawLogo(&font, title, bmp); + Launcher_DrawLogo(&font, title, ctx); // bitmapped fonts don't need to be freed return; } @@ -815,7 +815,7 @@ void LBackend_DrawLogo(struct Bitmap* bmp, const char* title) { NSForegroundColorAttributeName : UIColor.blackColor }; NSAttributedString* str_bg = [[NSAttributedString alloc] initWithString:text attributes:attrs_bg]; - DrawText(str_bg, bmp, 4, 42); + DrawText(str_bg, ctx, 4, 42); NSDictionary* attrs_fg = @{ @@ -823,24 +823,26 @@ void LBackend_DrawLogo(struct Bitmap* bmp, const char* title) { NSForegroundColorAttributeName : UIColor.whiteColor }; NSAttributedString* str_fg = [[NSAttributedString alloc] initWithString:text attributes:attrs_fg]; - DrawText(str_fg, bmp, 0, 38); + DrawText(str_fg, ctx, 0, 38); } void LBackend_InitFramebuffer(void) { } void LBackend_FreeFramebuffer(void) { } void LBackend_Redraw(void) { + struct Context2D ctx; struct Bitmap bmp; bmp.width = max(WindowInfo.Width, 1); bmp.height = max(WindowInfo.Height, 1); bmp.scan0 = (BitmapCol*)Mem_Alloc(bmp.width * bmp.height, 4, "window pixels"); + Context2D_Wrap(&ctx, &bmp); win_ctx = CGBitmapContextCreate(bmp.scan0, bmp.width, bmp.height, 8, bmp.width * 4, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrder32Host | kCGImageAlphaNoneSkipFirst); - Launcher_Active->DrawBackground(Launcher_Active, &bmp); + Launcher_Active->DrawBackground(Launcher_Active, &ctx); view_handle.layer.contents = CFBridgingRelease(CGBitmapContextCreateImage(win_ctx)); - Mem_Free(bmp.scan0); + Mem_Free(bmp.scan0); // TODO Context2D_Free CGContextRelease(win_ctx); } @@ -867,13 +869,16 @@ static void LBackend_ButtonUpdateBackground(struct LButton* w) { int height = (int)rect.size.height; // memory freeing deferred until UIImage is freed (see FreeContents) struct Bitmap bmp1, bmp2; + struct Context2D ctx1, ctx2; Bitmap_Allocate(&bmp1, width, height); - LButton_DrawBackground(&bmp1, 0, 0, width, height, false); + Context2D_Wrap(&ctx1, &bmp1); + LButton_DrawBackground(&ctx1, 0, 0, width, height, false); [btn setBackgroundImage:ToUIImage(&bmp1) forState:UIControlStateNormal]; Bitmap_Allocate(&bmp2, width, height); - LButton_DrawBackground(&bmp2, 0, 0, width, height, true); + Context2D_Wrap(&ctx2, &bmp2); + LButton_DrawBackground(&ctx2, 0, 0, width, height, true); [btn setBackgroundImage:ToUIImage(&bmp2) forState:UIControlStateHighlighted]; }