Fix iOS compilation issues

This commit is contained in:
UnknownShadow200 2022-06-19 17:13:13 +10:00
parent 548924c854
commit e2ed4884ba
6 changed files with 72 additions and 64 deletions

View File

@ -150,15 +150,15 @@ static void Font_SysTextDraw(struct DrawTextArgs* args, struct Bitmap* bmp, int
/*########################################################################################################################* /*########################################################################################################################*
*---------------------------------------------------Drawing functions-----------------------------------------------------* *---------------------------------------------------Drawing functions-----------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
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) {
if (*x >= bmp->width || *y >= bmp->height) return false; if (*x >= ctx->width || *y >= ctx->height) return false;
/* origin is negative, move inside */ /* origin is negative, move inside */
if (*x < 0) { *width += *x; *x = 0; } if (*x < 0) { *width += *x; *x = 0; }
if (*y < 0) { *height += *y; *y = 0; } if (*y < 0) { *height += *y; *y = 0; }
*width = min(*x + *width, bmp->width) - *x; *width = min(*x + *width, ctx->width) - *x;
*height = min(*y + *height, bmp->height) - *y; *height = min(*y + *height, ctx->height) - *y;
return *width > 0 && *height > 0; return *width > 0 && *height > 0;
} }
#define Drawer2D_ClampPixel(p) p = (p < 0 ? 0 : (p > 255 ? 255 : p)) #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"); 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) { void Context2D_Free(struct Context2D* ctx) {
Mem_Free(ctx->bmp.scan0); Mem_Free(ctx->bmp.scan0);
} }
@ -187,7 +194,7 @@ void Gradient_Noise(struct Context2D* ctx, BitmapCol color, int variation,
BitmapCol* dst; BitmapCol* dst;
int R, G, B, xx, yy, n; int R, G, B, xx, yy, n;
float noise; 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++) { for (yy = 0; yy < height; yy++) {
dst = Bitmap_GetRow(bmp, y + yy) + x; dst = Bitmap_GetRow(bmp, y + yy) + x;
@ -212,7 +219,7 @@ void Gradient_Vertical(struct Context2D* ctx, BitmapCol a, BitmapCol b,
BitmapCol* row, color; BitmapCol* row, color;
int xx, yy; int xx, yy;
float t; 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++) { for (yy = 0; yy < height; yy++) {
row = Bitmap_GetRow(bmp, y + yy) + x; 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; struct Bitmap* bmp = (struct Bitmap*)ctx;
BitmapCol* dst; BitmapCol* dst;
int R, G, B, xx, yy; 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 */ /* Pre compute the alpha blended source color */
/* TODO: Avoid shift when multiplying */ /* 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) { void Context2D_DrawPixels(struct Context2D* ctx, int x, int y, struct Bitmap* src) {
struct Bitmap* dst = (struct Bitmap*)ctx; struct Bitmap* dst = (struct Bitmap*)ctx;
int width = src->width, height = src->height; int width = src->width, height = src->height;
BitmapCol* dstRow; BitmapCol* dstRow;
BitmapCol* srcRow; BitmapCol* srcRow;
int xx, yy; 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++) { for (yy = 0; yy < height; yy++) {
srcRow = Bitmap_GetRow(src, yy); srcRow = Bitmap_GetRow(src, yy);
@ -304,7 +286,7 @@ void Context2D_Clear(struct Context2D* ctx, BitmapCol color,
struct Bitmap* bmp = (struct Bitmap*)ctx; struct Bitmap* bmp = (struct Bitmap*)ctx;
BitmapCol* row; BitmapCol* row;
int xx, yy; 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++) { for (yy = 0; yy < height; yy++) {
row = Bitmap_GetRow(bmp, y + yy) + x; row = Bitmap_GetRow(bmp, y + yy) + x;

View File

@ -31,14 +31,16 @@ void DrawTextArgs_MakeEmpty(struct DrawTextArgs* args, struct FontDesc* font, cc
/* Clamps the given rectangle to lie inside the bitmap */ /* Clamps the given rectangle to lie inside the bitmap */
/* Returns false if rectangle is completely outside bitmap's rectangle */ /* 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 */ /* Allocates a new context for 2D drawing */
/* Note: Allocates a power-of-2 sized backing bitmap equal to or greater than the given size */ /* 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); 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 */ /* Frees/Releases a previously allocatedcontext for 2D drawing */
CC_API void Context2D_Free(struct Context2D* ctx); 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); CC_API void Context2D_MakeTexture(struct Texture* tex, struct Context2D* ctx);
/* Draws text using the given font at the given coordinates */ /* 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 */ /* Note that this only blends RGB, A is not blended */
CC_API void Gradient_Blend(struct Context2D* ctx, BitmapCol color, int blend, CC_API void Gradient_Blend(struct Context2D* ctx, BitmapCol color, int blend,
int x, int y, int width, int height); 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 */ /* Returns how wide the given text would be when drawn */
CC_API int Drawer2D_TextWidth(struct DrawTextArgs* args); CC_API int Drawer2D_TextWidth(struct DrawTextArgs* args);

View File

@ -155,7 +155,7 @@ static CC_NOINLINE void MarkAllDirty(void) {
/* Marks the given area/region as needing to be redrawn. */ /* Marks the given area/region as needing to be redrawn. */
static CC_NOINLINE void MarkAreaDirty(int x, int y, int width, int height) { static CC_NOINLINE void MarkAreaDirty(int x, int y, int width, int height) {
int x1, y1, x2, y2; 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 */ /* union with existing dirty area */
if (dirty_rect.Width) { 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) { void LBackend_InitFramebuffer(void) {
framebuffer.bmp.width = max(WindowInfo.Width, 1); struct Bitmap bmp;
framebuffer.bmp.height = max(WindowInfo.Height, 1); bmp.width = max(WindowInfo.Width, 1);
bmp.height = max(WindowInfo.Height, 1);
Window_AllocFramebuffer(&framebuffer.bmp); Window_AllocFramebuffer(&bmp);
framebuffer.width = framebuffer.bmp.width; Context2D_Wrap(&framebuffer, &bmp);
framebuffer.height = framebuffer.bmp.height;
} }
void LBackend_FreeFramebuffer(void) { void LBackend_FreeFramebuffer(void) {

View File

@ -360,10 +360,26 @@ void Launcher_SaveTheme(void) {
/*########################################################################################################################* /*########################################################################################################################*
*---------------------------------------------------------Texture pack----------------------------------------------------* *---------------------------------------------------------Texture pack----------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
static cc_bool Launcher_SelectZipEntry(const cc_string* path) { /* Tints the given area, linearly interpolating from a to b */
return /* Note that this only tints RGB, A is not tinted */
String_CaselessEqualsConst(path, "default.png") || static void TintBitmap(struct Bitmap* bmp, cc_uint8 tintA, cc_uint8 tintB, int width, int height) {
String_CaselessEqualsConst(path, "terrain.png"); 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) { 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(&dirtBmp, bmp, 2 * tileSize, 0, tileSize, tileSize);
Bitmap_Scale(&stoneBmp, bmp, 1 * tileSize, 0, tileSize, tileSize); Bitmap_Scale(&stoneBmp, bmp, 1 * tileSize, 0, tileSize, tileSize);
Gradient_Tint(&dirtBmp, 128, 64, 0, 0, TILESIZE, TILESIZE); TintBitmap(&dirtBmp, 128, 64, TILESIZE, TILESIZE);
Gradient_Tint(&stoneBmp, 96, 96, 0, 0, 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) { 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* dstRow;
BitmapCol* srcRow; BitmapCol* srcRow;
int xx, yy; 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++) { for (yy = 0; yy < height; yy++) {
srcRow = Bitmap_GetRow(src, (y + yy) % TILESIZE); srcRow = Bitmap_GetRow(src, (y + yy) % TILESIZE);

View File

@ -6,6 +6,7 @@
*/ */
struct LScreen; struct LScreen;
struct FontDesc; struct FontDesc;
struct Context2D;
/* The screen/menu currently being shown */ /* The screen/menu currently being shown */
extern struct LScreen* Launcher_Active; extern struct LScreen* Launcher_Active;

View File

@ -789,20 +789,20 @@ void LBackend_Tick(void) { }
void LBackend_Free(void) { } void LBackend_Free(void) { }
void LBackend_UpdateLogoFont(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); CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)str);
CGRect bounds = CTLineGetImageBounds(line, win_ctx); 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); 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()) { if (Launcher_BitmappedText()) {
struct FontDesc font; struct FontDesc font;
Launcher_MakeLogoFont(&font); Launcher_MakeLogoFont(&font);
Launcher_DrawLogo(&font, title, bmp); Launcher_DrawLogo(&font, title, ctx);
// bitmapped fonts don't need to be freed // bitmapped fonts don't need to be freed
return; return;
} }
@ -815,7 +815,7 @@ void LBackend_DrawLogo(struct Bitmap* bmp, const char* title) {
NSForegroundColorAttributeName : UIColor.blackColor NSForegroundColorAttributeName : UIColor.blackColor
}; };
NSAttributedString* str_bg = [[NSAttributedString alloc] initWithString:text attributes:attrs_bg]; 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 = NSDictionary* attrs_fg =
@{ @{
@ -823,24 +823,26 @@ void LBackend_DrawLogo(struct Bitmap* bmp, const char* title) {
NSForegroundColorAttributeName : UIColor.whiteColor NSForegroundColorAttributeName : UIColor.whiteColor
}; };
NSAttributedString* str_fg = [[NSAttributedString alloc] initWithString:text attributes:attrs_fg]; 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_InitFramebuffer(void) { }
void LBackend_FreeFramebuffer(void) { } void LBackend_FreeFramebuffer(void) { }
void LBackend_Redraw(void) { void LBackend_Redraw(void) {
struct Context2D ctx;
struct Bitmap bmp; struct Bitmap bmp;
bmp.width = max(WindowInfo.Width, 1); bmp.width = max(WindowInfo.Width, 1);
bmp.height = max(WindowInfo.Height, 1); bmp.height = max(WindowInfo.Height, 1);
bmp.scan0 = (BitmapCol*)Mem_Alloc(bmp.width * bmp.height, 4, "window pixels"); 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, win_ctx = CGBitmapContextCreate(bmp.scan0, bmp.width, bmp.height, 8, bmp.width * 4,
CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrder32Host | kCGImageAlphaNoneSkipFirst); CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrder32Host | kCGImageAlphaNoneSkipFirst);
Launcher_Active->DrawBackground(Launcher_Active, &bmp); Launcher_Active->DrawBackground(Launcher_Active, &ctx);
view_handle.layer.contents = CFBridgingRelease(CGBitmapContextCreateImage(win_ctx)); view_handle.layer.contents = CFBridgingRelease(CGBitmapContextCreateImage(win_ctx));
Mem_Free(bmp.scan0); Mem_Free(bmp.scan0); // TODO Context2D_Free
CGContextRelease(win_ctx); CGContextRelease(win_ctx);
} }
@ -867,13 +869,16 @@ static void LBackend_ButtonUpdateBackground(struct LButton* w) {
int height = (int)rect.size.height; int height = (int)rect.size.height;
// memory freeing deferred until UIImage is freed (see FreeContents) // memory freeing deferred until UIImage is freed (see FreeContents)
struct Bitmap bmp1, bmp2; struct Bitmap bmp1, bmp2;
struct Context2D ctx1, ctx2;
Bitmap_Allocate(&bmp1, width, height); 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]; [btn setBackgroundImage:ToUIImage(&bmp1) forState:UIControlStateNormal];
Bitmap_Allocate(&bmp2, width, height); 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]; [btn setBackgroundImage:ToUIImage(&bmp2) forState:UIControlStateHighlighted];
} }