Skip to content

Commit

Permalink
Initial WIP on making BitmapCol raw uint32.
Browse files Browse the repository at this point in the history
Also causes hundreds of compile errors, oops.
  • Loading branch information
UnknownShadow200 committed Oct 6, 2019
1 parent e0620c5 commit df31c95
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 113 deletions.
21 changes: 9 additions & 12 deletions src/Bitmap.c
Expand Up @@ -54,7 +54,6 @@ void Bitmap_Scale(Bitmap* dst, Bitmap* src, int srcX, int srcY, int srcWidth, in
*#########################################################################################################################*/
#define PNG_SIG_SIZE 8
#define PNG_IHDR_SIZE 13
#define PNG_RGB_MASK 0xFFFFFFUL
#define PNG_PALETTE 256
#define PNG_FourCC(a, b, c, d) (((cc_uint32)a << 24) | ((cc_uint32)b << 16) | ((cc_uint32)c << 8) | (cc_uint32)d)

Expand Down Expand Up @@ -127,7 +126,7 @@ static void Png_Reconstruct(cc_uint8 type, cc_uint8 bytesPerPixel, cc_uint8* lin
}
}

#define Bitmap_Set(dst, r,g,b,a) dst.B = b; dst.G = g; dst.R = r; dst.A = a;
#define Bitmap_Set(dst, r,g,b,a) dst = BitmapCol_Make(r, g, b, a);

#define PNG_Do_Grayscale(dstI, src, scale) rgb = (src) * scale; Bitmap_Set(dst[dstI], rgb, rgb, rgb, 255);
#define PNG_Do_Grayscale_8(dstI, srcI) rgb = src[srcI]; Bitmap_Set(dst[dstI], rgb, rgb, rgb, 255);
Expand Down Expand Up @@ -307,14 +306,15 @@ static Png_RowExpander Png_GetExpander(cc_uint8 col, cc_uint8 bitsPerSample) {
return NULL;
}

/* Sets alpha to 0 for any pixels in the bitmap whose RGB is same as col */
static void Png_ComputeTransparency(Bitmap* bmp, BitmapCol col) {
cc_uint32 trnsRGB = col.B | (col.G << 8) | (col.R << 16); /* TODO: Remove this!! */
BitmapCol trnsRGB = col & BITMAPCOL_RGB_MASK;
int x, y, width = bmp->Width, height = bmp->Height;

for (y = 0; y < height; y++) {
cc_uint32* row = Bitmap_RawRow(bmp, y);
BitmapCol* row = Bitmap_GetRow(bmp, y);
for (x = 0; x < width; x++) {
cc_uint32 rgb = row[x] & PNG_RGB_MASK;
BitmapCol rgb = row[x] & BITMAPCOL_RGB_MASK;
row[x] = (rgb == trnsRGB) ? trnsRGB : row[x];
}
}
Expand All @@ -337,7 +337,6 @@ ReturnCode Png_Decode(Bitmap* bmp, struct Stream* stream) {
cc_uint32 scanlineSize, scanlineBytes;

/* palette data */
BitmapCol black = BITMAPCOL_CONST(0, 0, 0, 255);
BitmapCol transparentCol;
BitmapCol palette[PNG_PALETTE];
cc_uint32 i;
Expand All @@ -360,8 +359,8 @@ ReturnCode Png_Decode(Bitmap* bmp, struct Stream* stream) {
if (res) return res;
if (!Png_Detect(tmp, PNG_SIG_SIZE)) return PNG_ERR_INVALID_SIG;

transparentCol = black;
for (i = 0; i < PNG_PALETTE; i++) { palette[i] = black; }
transparentCol = BITMAPCOL_BLACK;
for (i = 0; i < PNG_PALETTE; i++) { palette[i] = BITMAPCOL_BLACK; }

Inflate_MakeStream(&compStream, &inflate, stream);
ZLibHeader_Init(&zlibHeader);
Expand Down Expand Up @@ -424,8 +423,7 @@ ReturnCode Png_Decode(Bitmap* bmp, struct Stream* stream) {
if (res) return res;

/* RGB is 16 bits big endian, ignore least significant 8 bits */
transparentCol.R = tmp[0]; transparentCol.G = tmp[0];
transparentCol.B = tmp[0]; transparentCol.A = 0;
transparentCol = BitmapCol_Make(tmp[0], tmp[0], tmp[0], 0);
} else if (col == PNG_COL_INDEXED) {
if (dataSize > PNG_PALETTE) return PNG_ERR_TRANS_COUNT;
res = Stream_Read(stream, tmp, dataSize);
Expand All @@ -441,8 +439,7 @@ ReturnCode Png_Decode(Bitmap* bmp, struct Stream* stream) {
if (res) return res;

/* R,G,B is 16 bits big endian, ignore least significant 8 bits */
transparentCol.R = tmp[0]; transparentCol.G = tmp[2];
transparentCol.B = tmp[4]; transparentCol.A = 0;
transparentCol = BitmapCol_Make(tmp[0], tmp[2], tmp[4], 0);
} else {
return PNG_ERR_TRANS_INVALID;
}
Expand Down
48 changes: 29 additions & 19 deletions src/Bitmap.h
Expand Up @@ -6,37 +6,47 @@
*/
struct Stream;

/* Represents an ARGB colour, suitable for native graphics API texture pixels. */
typedef union BitmapCol_ {
/* Represents a packed 32 bit RGBA colour, suitable for native graphics API texture pixels. */
typedef cc_uint32 BitmapCol;
#if defined CC_BUILD_WEB || defined CC_BUILD_ANDROID
struct { cc_uint8 R, G, B, A; };
#define BITMAPCOL_R_SHIFT 0
#define BITMAPCOL_G_SHIFT 8
#define BITMAPCOL_B_SHIFT 16
#define BITMAPCOL_A_SHIFT 24
#else
struct { cc_uint8 B, G, R, A; };
#define BITMAPCOL_B_SHIFT 0
#define BITMAPCOL_G_SHIFT 8
#define BITMAPCOL_R_SHIFT 16
#define BITMAPCOL_A_SHIFT 24
#endif
cc_uint32 _raw;
} BitmapCol;

/* Whether components of two colours are all equal. */
#define BitmapCol_Equals(a,b) ((a)._raw == (b)._raw)
#define PackedCol_ARGB(r, g, b, a) (((cc_uint32)(r) << 16) | ((cc_uint32)(g) << 8) | ((cc_uint32)(b)) | ((cc_uint32)(a) << 24))
#define BITMAPCOL_R_MASK (0xFFU << BITMAPCOL_R_SHIFT)
#define BITMAPCOL_G_MASK (0xFFU << BITMAPCOL_G_SHIFT)
#define BITMAPCOL_B_MASK (0xFFU << BITMAPCOL_B_SHIFT)
#define BITMAPCOL_A_MASK (0xFFU << BITMAPCOL_A_SHIFT)

#define BitmapCol_R(col) ((cc_uint8)(col >> BITMAPCOL_R_SHIFT))
#define BitmapCol_G(col) ((cc_uint8)(col >> BITMAPCOL_G_SHIFT))
#define BitmapCol_B(col) ((cc_uint8)(col >> BITMAPCOL_B_SHIFT))
#define BitmapCol_A(col) ((cc_uint8)(col >> BITMAPCOL_A_SHIFT))

#define BitmapCol_R_Bits(col) ((cc_uint8)(col) << BITMAPCOL_R_SHIFT)
#define BitmapCol_G_Bits(col) ((cc_uint8)(col) << BITMAPCOL_G_SHIFT)
#define BitmapCol_B_Bits(col) ((cc_uint8)(col) << BITMAPCOL_B_SHIFT)
#define BitmapCol_A_Bits(col) ((cc_uint8)(col) << BITMAPCOL_A_SHIFT)

#define BitmapCol_Make(r, g, b, a) (BitmapCol_R_Bits(r) | BitmapCol_G_Bits(g) | BitmapCol_B_Bits(b) | BitmapCol_A_Bits(a))
#define BITMAPCOL_RGB_MASK (BITMAPCOL_R_MASK | BITMAPCOL_G_MASK | BITMAPCOL_B_MASK)

#define BITMAPCOL_BLACK BitmapCol_Make( 0, 0, 0, 255)
#define BITMAPCOL_WHITE BitmapCol_Make(255, 255, 255, 255)

/* A 2D array of BitmapCol pixels */
typedef struct Bitmap_ { cc_uint8* Scan0; int Width, Height; } Bitmap;

#define PNG_MAX_DIMS 0x8000
#if defined CC_BUILD_WEB || defined CC_BUILD_ANDROID
#define BITMAPCOL_CONST(r, g, b, a) { r, g, b, a }
#else
#define BITMAPCOL_CONST(r, g, b, a) { b, g, r, a }
#endif

/* Returns number of bytes a bitmap consumes. */
#define Bitmap_DataSize(width, height) ((cc_uint32)(width) * (cc_uint32)(height) * 4)
/* Gets the yth row of a bitmap as raw cc_uint32* pointer. */
/* NOTE: You SHOULD not rely on the order of the 4 bytes in the pointer. */
/* Different platforms may have different endian, or different component order. */
#define Bitmap_RawRow(bmp, y) ((cc_uint32*)(bmp)->Scan0 + (y) * (bmp)->Width)
/* Gets the yth row of the given bitmap. */
#define Bitmap_GetRow(bmp, y) ((BitmapCol*)(bmp)->Scan0 + (y) * (bmp)->Width)
/* Gets the pixel at (x,y) in the given bitmap. */
Expand Down
5 changes: 3 additions & 2 deletions src/Core.h
Expand Up @@ -30,8 +30,9 @@ typedef unsigned int cc_uintptr;
#define CC_HAS_TYPES
#define CC_HAS_MISC
#elif __GNUC__
/* really old GCC/clang might not have these */
/* really old GCC/clang might not have these defined */
#ifdef __INT8_TYPE__
/* avoid including <stdint.h> because it breaks defining UNICODE in Platform.c with MinGW */
typedef __INT8_TYPE__ cc_int8;
typedef __INT16_TYPE__ cc_int16;
typedef __INT32_TYPE__ cc_int32;
Expand Down Expand Up @@ -74,7 +75,7 @@ typedef unsigned __INTPTR_TYPE__ cc_uintptr;
#define CC_BIG_ENDIAN
#endif

/* Unrecognised compiler, so just go with sensisble defaults */
/* Unrecognised compiler, so just go with sensible defaults */
#ifndef CC_HAS_TYPES
#include <stdint.h>
typedef int8_t cc_int8;
Expand Down
19 changes: 8 additions & 11 deletions src/Drawer2D.c
Expand Up @@ -278,7 +278,7 @@ void Drawer2D_BmpIndexed(Bitmap* bmp, int x, int y, int size,
for (xx = 0; xx < size; xx++) {
col = palette[*indices++];

if (col._raw == 0) continue; /* transparent pixel */
if (col == 0) continue; /* transparent pixel */
if ((x + xx) < 0 || (x + xx) >= bmp->Width) continue;
row[xx] = col;
}
Expand Down Expand Up @@ -433,7 +433,6 @@ void Drawer2D_Underline(Bitmap* bmp, int x, int y, int width, int height, Bitmap
}

static void Drawer2D_DrawCore(Bitmap* bmp, struct DrawTextArgs* args, int x, int y, bool shadow) {
BitmapCol black = BITMAPCOL_CONST(0, 0, 0, 255);
BitmapCol col;
String text = args->text;
int i, point = args->font->size, count = 0;
Expand All @@ -454,15 +453,15 @@ static void Drawer2D_DrawCore(Bitmap* bmp, struct DrawTextArgs* args, int x, int

col = Drawer2D_Cols['f'];
if (shadow) {
col = Drawer2D_BlackTextShadows ? black : Drawer2D_ShadowCol(col);
col = Drawer2D_BlackTextShadows ? BITMAPCOL_BLACK : Drawer2D_ShadowCol(col);
}

for (i = 0; i < text.length; i++) {
char c = text.buffer[i];
if (c == '&' && Drawer2D_ValidColCodeAt(&text, i + 1)) {
col = Drawer2D_GetCol(text.buffer[i + 1]);
if (shadow) {
col = Drawer2D_BlackTextShadows ? black : Drawer2D_ShadowCol(col);
col = Drawer2D_BlackTextShadows ? BITMAPCOL_BLACK : Drawer2D_ShadowCol(col);
}
i++; continue; /* skip over the colour code */
}
Expand Down Expand Up @@ -523,7 +522,7 @@ static void Drawer2D_DrawCore(Bitmap* bmp, struct DrawTextArgs* args, int x, int
dstWidth = 0;
col = cols[i];

for (; i < count && BitmapCol_Equals(col, cols[i]); i++) {
for (; i < count && col == cols[i]; i++) {
dstWidth += dstWidths[i] + xPadding;
}
Drawer2D_Underline(bmp, x, underlineY, dstWidth, underlineHeight, col);
Expand Down Expand Up @@ -567,7 +566,7 @@ static int Drawer2D_MeasureBitmapWidth(const struct DrawTextArgs* args) {
}

void Drawer2D_DrawText(Bitmap* bmp, struct DrawTextArgs* args, int x, int y) {
BitmapCol col, backCol, black = BITMAPCOL_CONST(0, 0, 0, 255);
BitmapCol col, backCol;
String value = args->text;
char colCode, nextCol = 'f';
int i, partWidth;
Expand All @@ -582,7 +581,7 @@ void Drawer2D_DrawText(Bitmap* bmp, struct DrawTextArgs* args, int x, int y) {

col = Drawer2D_GetCol(colCode);
if (args->useShadow) {
backCol = Drawer2D_BlackTextShadows ? black : Drawer2D_ShadowCol(col);
backCol = Drawer2D_BlackTextShadows ? BITMAPCOL_BLACK : Drawer2D_ShadowCol(col);
Font_SysTextDraw(args, bmp, x, y, backCol, true);
}

Expand Down Expand Up @@ -679,11 +678,9 @@ static void Drawer2D_HexEncodedCol(int i, int hex, cc_uint8 lo, cc_uint8 hi) {
}

static void Drawer2D_Reset(void) {
BitmapCol col = BITMAPCOL_CONST(0, 0, 0, 0);
int i;

int i;
for (i = 0; i < DRAWER2D_MAX_COLS; i++) {
Drawer2D_Cols[i] = col;
Drawer2D_Cols[i] = 0;
}

for (i = 0; i <= 9; i++) {
Expand Down
10 changes: 4 additions & 6 deletions src/Entity.c
Expand Up @@ -229,7 +229,7 @@ bool Entity_TouchesAnyWater(struct Entity* e) {

static void Entity_MakeNameTexture(struct Entity* e) {
String colorlessName; char colorlessBuffer[STRING_SIZE];
BitmapCol shadowCol = BITMAPCOL_CONST(80, 80, 80, 255);
BitmapCol shadowCol = BitmapCol_Make(80, 80, 80, 255);
BitmapCol origWhiteCol;

struct DrawTextArgs args;
Expand Down Expand Up @@ -401,13 +401,11 @@ static void Entity_ClearHat(Bitmap* bmp, cc_uint8 skinType) {
}

/* only perform filtering when the entire hat is opaque */
cc_uint32 white = PackedCol_ARGB(255, 255, 255, 255);
cc_uint32 black = PackedCol_ARGB(0, 0, 0, 255);
for (y = 0; y < sizeY; y++) {
cc_uint32* row = Bitmap_RawRow(bmp, y) + sizeX;
BitmapCol* row = Bitmap_GetRow(bmp, y) + sizeX;
for (x = 0; x < sizeX; x++) {
cc_uint32 pixel = row[x];
if (pixel == white || pixel == black) row[x] = 0;
BitmapCol c = row[x];
if (c == BITMAPCOL_WHITE || c == BITMAPCOL_BLACK) row[x] = 0;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/EntityComponents.c
Expand Up @@ -586,8 +586,8 @@ static bool ShadowComponent_GetBlocks(struct Entity* e, int x, int y, int z, str
#define sh_half (sh_size / 2)
static void ShadowComponent_MakeTex(void) {
cc_uint8 pixels[Bitmap_DataSize(sh_size, sh_size)];
BitmapCol inPix = BITMAPCOL_CONST(0, 0, 0, 200);
BitmapCol outPix = BITMAPCOL_CONST(0, 0, 0, 0);
BitmapCol inPix = BitmapCol_Make(0, 0, 0, 200);
BitmapCol outPix = BitmapCol_Make(0, 0, 0, 0);
Bitmap bmp;
cc_uint32 x, y;

Expand Down
12 changes: 6 additions & 6 deletions src/LScreens.c
Expand Up @@ -330,15 +330,15 @@ CC_NOINLINE static void ColoursScreen_Update(struct ColoursScreen* s, int i, Bit
String tmp; char tmpBuffer[3];

String_InitArray(tmp, tmpBuffer);
String_AppendInt(&tmp, col.R);
String_AppendInt(&tmp, BitmapCol_R(col));
LInput_SetText(&s->iptColours[i + 0], &tmp);

tmp.length = 0;
String_AppendInt(&tmp, col.G);
String_AppendInt(&tmp, BitmapCol_G(col));
LInput_SetText(&s->iptColours[i + 1], &tmp);

tmp.length = 0;
String_AppendInt(&tmp, col.B);
String_AppendInt(&tmp, BitmapCol_B(col));
LInput_SetText(&s->iptColours[i + 2], &tmp);
}

Expand Down Expand Up @@ -952,7 +952,7 @@ static void ResourcesScreen_Next(void* w, int x, int y) {

static void ResourcesScreen_Init(struct LScreen* s_) {
String str; char buffer[STRING_SIZE];
BitmapCol progressCol = BITMAPCOL_CONST(0, 220, 0, 255);
BitmapCol progressCol = BitmapCol_Make(0, 220, 0, 255);
struct ResourcesScreen* s = (struct ResourcesScreen*)s_;
float size;

Expand Down Expand Up @@ -1001,13 +1001,13 @@ static void ResourcesScreen_Layout(struct LScreen* s_) {
}

CC_NOINLINE static void ResourcesScreen_ResetArea(int x, int y, int width, int height) {
BitmapCol boxCol = BITMAPCOL_CONST(120, 85, 151, 255);
BitmapCol boxCol = BitmapCol_Make(120, 85, 151, 255);
Gradient_Noise(&Launcher_Framebuffer, boxCol, 4, x, y, width, height);
Launcher_MarkDirty(x, y, width, height);
}

static void ResourcesScreen_Draw(struct LScreen* s) {
BitmapCol backCol = BITMAPCOL_CONST(12, 12, 12, 255);
BitmapCol backCol = BitmapCol_Make(12, 12, 12, 255);
int x, y, width, height;

Drawer2D_Clear(&Launcher_Framebuffer, backCol,
Expand Down

0 comments on commit df31c95

Please sign in to comment.