From 1383530c4fd68c10fe488453e3b866d7486215b8 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 28 Jun 2024 16:52:16 +1000 Subject: [PATCH] Fix 16bpp bitmaps --- src/Bitmap.h | 24 ++++++++++++------------ src/ClassiCube.vcxproj | 2 +- src/ClassiCube.vcxproj.filters | 6 +++--- src/Game.c | 11 ++++++----- src/Graphics_D3D9.c | 4 ++-- src/Graphics_N64.c | 3 ++- src/Graphics_PS2.c | 11 +++++++---- src/Graphics_PS3.c | 8 +++++--- src/Graphics_PSP.c | 11 +++++++---- src/Graphics_PSVita.c | 8 +++++--- src/Graphics_SoftGPU.c | 7 +++++-- src/Graphics_WiiU.c | 6 ++++-- src/Graphics_Xbox360.c | 3 ++- src/Menus.c | 2 +- src/SystemFonts.c | 2 ++ src/_GLShared.h | 3 ++- src/_GraphicsBase.h | 2 +- src/main.c | 4 +--- 18 files changed, 68 insertions(+), 49 deletions(-) diff --git a/src/Bitmap.h b/src/Bitmap.h index 1a236bff9..3a04912bc 100644 --- a/src/Bitmap.h +++ b/src/Bitmap.h @@ -6,15 +6,7 @@ */ struct Stream; -/* Represents a packed RGBA colour, suitable for 3D graphics API textures and 2D window framebuffers. */ -#ifndef BITMAP_16BPP - typedef cc_uint32 BitmapCol; - #define BITMAPCOLOR_SIZE 4 -#else - typedef cc_uint16 BitmapCol; - #define BITMAPCOLOR_SIZE 2 -#endif - +#define BITMAP_16BPP #if defined CC_BUILD_WEB || defined CC_BUILD_ANDROID || defined CC_BUILD_PSP || defined CC_BUILD_PSVITA || defined CC_BUILD_PS2 #define BITMAPCOLOR_R_SHIFT 0 #define BITMAPCOLOR_G_SHIFT 8 @@ -38,6 +30,10 @@ struct Stream; #endif #ifndef BITMAP_16BPP + /* Represents a packed RGBA colour, suitable for 3D graphics API textures and 2D window framebuffers. */ + typedef cc_uint32 BitmapCol; + #define BITMAPCOLOR_SIZE 4 + /* Masks a packed color to the selected component */ #define BITMAPCOLOR_R_MASK (0xFFU << BITMAPCOLOR_R_SHIFT) #define BITMAPCOLOR_G_MASK (0xFFU << BITMAPCOLOR_G_SHIFT) @@ -56,6 +52,10 @@ struct Stream; #define BitmapColor_B_Bits(value) ((cc_uint8)(value) << BITMAPCOLOR_B_SHIFT) #define BitmapColor_A_Bits(value) ((cc_uint8)(value) << BITMAPCOLOR_A_SHIFT) #else + /* Represents a packed RGBA colour, suitable for 3D graphics API textures and 2D window framebuffers. */ + typedef cc_uint16 BitmapCol; + #define BITMAPCOLOR_SIZE 2 + /* Masks a packed color to the selected component */ #define BITMAPCOLOR_R_MASK (0x3FU << BITMAPCOLOR_R_SHIFT) #define BITMAPCOLOR_G_MASK (0x3FU << BITMAPCOLOR_G_SHIFT) @@ -63,9 +63,9 @@ struct Stream; #define BITMAPCOLOR_A_MASK (0x01U << BITMAPCOLOR_A_SHIFT) /* Extracts just the R/G/B/A component from a bitmap color */ - #define BitmapCol_R(color) (((cc_uint8)(color >> BITMAPCOLOR_R_SHIFT) & 0x3F) << 3) - #define BitmapCol_G(color) (((cc_uint8)(color >> BITMAPCOLOR_G_SHIFT) & 0x3F) << 3) - #define BitmapCol_B(color) (((cc_uint8)(color >> BITMAPCOLOR_B_SHIFT) & 0x3F) << 3) + #define BitmapCol_R(color) (((cc_uint8)(color >> BITMAPCOLOR_R_SHIFT) & 0x1F) << 3) + #define BitmapCol_G(color) (((cc_uint8)(color >> BITMAPCOLOR_G_SHIFT) & 0x1F) << 3) + #define BitmapCol_B(color) (((cc_uint8)(color >> BITMAPCOLOR_B_SHIFT) & 0x1F) << 3) #define BitmapCol_A(color) (((cc_uint8)(color >> BITMAPCOLOR_A_SHIFT) & 0x01) << 7) /* Converts input value into a packed color component */ diff --git a/src/ClassiCube.vcxproj b/src/ClassiCube.vcxproj index bd22d69a6..093015a0e 100644 --- a/src/ClassiCube.vcxproj +++ b/src/ClassiCube.vcxproj @@ -568,7 +568,7 @@ - + diff --git a/src/ClassiCube.vcxproj.filters b/src/ClassiCube.vcxproj.filters index d86d9b512..7898ff6f3 100644 --- a/src/ClassiCube.vcxproj.filters +++ b/src/ClassiCube.vcxproj.filters @@ -641,9 +641,6 @@ Source Files\Window - - Source Files\Window - Source Files\Window @@ -788,6 +785,9 @@ Source Files\Launcher + + Source Files\Window + diff --git a/src/Game.c b/src/Game.c index 7ff1cdb7c..cc715c5ac 100644 --- a/src/Game.c +++ b/src/Game.c @@ -703,11 +703,12 @@ static CC_INLINE void Game_RenderFrame(void) { float t; cc_uint64 render = Stopwatch_Measure(); - double delta = Stopwatch_ElapsedMicroseconds(frameStart, render) / (1000.0 * 1000.0); + double deltaD = Stopwatch_ElapsedMicroseconds(frameStart, render) / (1000.0 * 1000.0); + float delta = (float)deltaD; Window_ProcessEvents(delta); - if (delta > 5.0) delta = 5.0; /* avoid large delta with suspended process */ - if (delta <= 0.0) return; + if (delta > 5.0f) delta = 5.0f; /* avoid large delta with suspended process */ + if (delta <= 0.0f) return; frameStart = render; /* TODO: Should other tasks get called back too? */ @@ -727,7 +728,7 @@ static CC_INLINE void Game_RenderFrame(void) { Gfx_BeginFrame(); Gfx_BindIb(Gfx_defaultIb); - Game.Time += delta; + Game.Time += deltaD; Game_Vertices = 0; if (Input.Sources & INPUT_SOURCE_GAMEPAD) Gamepad_Tick(delta); @@ -739,7 +740,7 @@ static CC_INLINE void Game_RenderFrame(void) { InputHandler_SetFOV(Camera.ZoomFov); } - PerformScheduledTasks(delta); + PerformScheduledTasks(deltaD); entTask = tasks[entTaskI]; t = (float)(entTask.accumulator / entTask.interval); LocalPlayer_SetInterpPosition(Entities.CurPlayer, t); diff --git a/src/Graphics_D3D9.c b/src/Graphics_D3D9.c index ce2d8ed05..f22336645 100644 --- a/src/Graphics_D3D9.c +++ b/src/Graphics_D3D9.c @@ -252,7 +252,7 @@ static void D3D9_SetTextureData(IDirect3DTexture9* texture, struct Bitmap* bmp, cc_result res = IDirect3DTexture9_LockRect(texture, lvl, &rect, NULL, 0); if (res) Logger_Abort2(res, "D3D9_LockTextureData"); - CopyTextureData(rect.pBits, rect.Pitch, bmp, rowWidth << 2); + CopyTextureData(rect.pBits, rect.Pitch, bmp, rowWidth * BITMAPCOLOR_SIZE); res = IDirect3DTexture9_UnlockRect(texture, lvl); if (res) Logger_Abort2(res, "D3D9_UnlockTextureData"); @@ -268,7 +268,7 @@ static void D3D9_SetTexturePartData(IDirect3DTexture9* texture, int x, int y, co res = IDirect3DTexture9_LockRect(texture, lvl, &rect, &part, 0); if (res) Logger_Abort2(res, "D3D9_LockTexturePartData"); - CopyTextureData(rect.pBits, rect.Pitch, bmp, rowWidth << 2); + CopyTextureData(rect.pBits, rect.Pitch, bmp, rowWidth * BITMAPCOLOR_SIZE); res = IDirect3DTexture9_UnlockRect(texture, lvl); if (res) Logger_Abort2(res, "D3D9_UnlockTexturePartData"); diff --git a/src/Graphics_N64.c b/src/Graphics_N64.c index f9adc2ed0..3a09e6271 100644 --- a/src/Graphics_N64.c +++ b/src/Graphics_N64.c @@ -162,7 +162,8 @@ static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 } } else { // 32 bpp can just be copied straight across - CopyTextureData(fb->buffer, fb->stride, bmp, rowWidth << 2); + CopyTextureData(fb->buffer, fb->stride, + bmp, rowWidth * BITMAPCOLOR_SIZE); } diff --git a/src/Graphics_PS2.c b/src/Graphics_PS2.c index e3a5f7b94..b06997c7d 100644 --- a/src/Graphics_PS2.c +++ b/src/Graphics_PS2.c @@ -163,7 +163,7 @@ typedef struct CCTexture_ { cc_uint32 width, height; cc_uint32 log2_width, log2_height; cc_uint32 pad[(64 - 16)/4]; - cc_uint32 pixels[]; // aligned to 64 bytes (only need 16?) + BitmapCol pixels[]; // aligned to 64 bytes (only need 16?) } CCTexture; static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, cc_bool mipmaps) { @@ -175,7 +175,8 @@ static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 tex->log2_width = draw_log2(bmp->width); tex->log2_height = draw_log2(bmp->height); - CopyTextureData(tex->pixels, bmp->width * 4, bmp, rowWidth << 2); + CopyTextureData(tex->pixels, bmp->width * BITMAPCOLOR_SIZE, + bmp, rowWidth * BITMAPCOLOR_SIZE); return tex; } @@ -227,8 +228,10 @@ void Gfx_DeleteTexture(GfxResourceID* texId) { void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, int rowWidth, cc_bool mipmaps) { CCTexture* tex = (CCTexture*)texId; - cc_uint32* dst = (tex->pixels + x) + y * tex->width; - CopyTextureData(dst, tex->width * 4, part, rowWidth << 2); + BitmapCol* dst = (tex->pixels + x) + y * tex->width; + + CopyTextureData(dst, tex->width * BITMAPCOLOR_SIZE, + part, rowWidth * BITMAPCOLOR_SIZE); } void Gfx_EnableMipmaps(void) { } diff --git a/src/Graphics_PS3.c b/src/Graphics_PS3.c index 27ff64322..22b65e53a 100644 --- a/src/Graphics_PS3.c +++ b/src/Graphics_PS3.c @@ -562,7 +562,8 @@ static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 tex->width = bmp->width; tex->height = bmp->height; - CopyTextureData(tex->pixels, bmp->width * 4, bmp, rowWidth << 2); + CopyTextureData(tex->pixels, bmp->width * BITMAPCOLOR_SIZE, + bmp, rowWidth * BITMAPCOLOR_SIZE); return tex; } @@ -613,8 +614,9 @@ void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, i CCTexture* tex = (CCTexture*)texId; // NOTE: Only valid for LINEAR textures - cc_uint32* dst = (tex->pixels + x) + y * tex->width; - CopyTextureData(dst, tex->width * 4, part, rowWidth << 2); + BitmapCol* dst = (tex->pixels + x) + y * tex->width; + CopyTextureData(dst, tex->width * BITMAPCOLOR_SIZE, + part, rowWidth * BITMAPCOLOR_SIZE); rsxInvalidateTextureCache(context, GCM_INVALIDATE_TEXTURE); /* TODO */ diff --git a/src/Graphics_PSP.c b/src/Graphics_PSP.c index 67b2c6dc2..13240a81d 100644 --- a/src/Graphics_PSP.c +++ b/src/Graphics_PSP.c @@ -111,7 +111,7 @@ void Gfx_FreeState(void) { typedef struct CCTexture_ { cc_uint32 width, height; cc_uint32 pad1, pad2; // data must be aligned to 16 bytes - cc_uint32 pixels[]; + BitmapCol pixels[]; } CCTexture; static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, cc_bool mipmaps) { @@ -120,14 +120,17 @@ static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 tex->width = bmp->width; tex->height = bmp->height; - CopyTextureData(tex->pixels, bmp->width * 4, bmp, rowWidth << 2); + CopyTextureData(tex->pixels, bmp->width * BITMAPCOLOR_SIZE, + bmp, rowWidth * BITMAPCOLOR_SIZE); return tex; } void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, int rowWidth, cc_bool mipmaps) { CCTexture* tex = (CCTexture*)texId; - cc_uint32* dst = (tex->pixels + x) + y * tex->width; - CopyTextureData(dst, tex->width * 4, part, rowWidth << 2); + BitmapCol* dst = (tex->pixels + x) + y * tex->width; + + CopyTextureData(dst, tex->width * BITMAPCOLOR_SIZE, + part, rowWidth * BITMAPCOLOR_SIZE); // TODO: Do line by line and only invalidate the actually changed parts of lines? sceKernelDcacheWritebackInvalidateRange(dst, (tex->width * part->height) * 4); } diff --git a/src/Graphics_PSVita.c b/src/Graphics_PSVita.c index 88606d03b..c05f0cf5b 100644 --- a/src/Graphics_PSVita.c +++ b/src/Graphics_PSVita.c @@ -682,7 +682,8 @@ static void GPUTextures_DeleteUnreferenced(void) { static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, cc_bool mipmaps) { int size = bmp->width * bmp->height * 4; struct GPUTexture* tex = GPUTexture_Alloc(size); - CopyTextureData(tex->data, bmp->width * 4, bmp, rowWidth << 2); + CopyTextureData(tex->data, bmp->width * BITMAPCOLOR_SIZE, + bmp, rowWidth * BITMAPCOLOR_SIZE); sceGxmTextureInitLinear(&tex->texture, tex->data, SCE_GXM_TEXTURE_FORMAT_A8B8G8R8, bmp->width, bmp->height, 0); @@ -697,9 +698,10 @@ void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, i int texWidth = sceGxmTextureGetWidth(&tex->texture); // NOTE: Only valid for LINEAR textures - cc_uint32* dst = (tex->data + x) + y * texWidth; + BitmapCol* dst = (tex->data + x) + y * texWidth; - CopyTextureData(dst, texWidth * 4, part, rowWidth << 2); + CopyTextureData(dst, texWidth * BITMAPCOLOR_SIZE, + part, rowWidth * BITMAPCOLOR_SIZE); // TODO: Do line by line and only invalidate the actually changed parts of lines? //sceKernelDcacheWritebackInvalidateRange(dst, (tex->width * part->height) * 4); } diff --git a/src/Graphics_SoftGPU.c b/src/Graphics_SoftGPU.c index 9da6d28f5..565f47693 100644 --- a/src/Graphics_SoftGPU.c +++ b/src/Graphics_SoftGPU.c @@ -92,14 +92,17 @@ static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 tex->width = bmp->width; tex->height = bmp->height; - CopyTextureData(tex->pixels, bmp->width * 4, bmp, rowWidth << 2); + CopyTextureData(tex->pixels, bmp->width * BITMAPCOLOR_SIZE, + bmp, rowWidth * BITMAPCOLOR_SIZE); return tex; } void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, int rowWidth, cc_bool mipmaps) { CCTexture* tex = (CCTexture*)texId; BitmapCol* dst = (tex->pixels + x) + y * tex->width; - CopyTextureData(dst, tex->width * 4, part, rowWidth << 2); + + CopyTextureData(dst, tex->width * BITMAPCOLOR_SIZE, + part, rowWidth * BITMAPCOLOR_SIZE); } void Gfx_EnableMipmaps(void) { } diff --git a/src/Graphics_WiiU.c b/src/Graphics_WiiU.c index ce8148fd5..8a73442b7 100644 --- a/src/Graphics_WiiU.c +++ b/src/Graphics_WiiU.c @@ -104,7 +104,8 @@ static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 tex->surface.image = MEMAllocFromDefaultHeapEx(tex->surface.imageSize, tex->surface.alignment); if (!tex->surface.image) { Mem_Free(tex); return NULL; } - CopyTextureData(tex->surface.image, tex->surface.pitch << 2, bmp, rowWidth << 2); + CopyTextureData(tex->surface.image, tex->surface.pitch << 2, + bmp, rowWidth * BITMAPCOLOR_SIZE); GX2Invalidate(GX2_INVALIDATE_MODE_CPU_TEXTURE, tex->surface.image, tex->surface.imageSize); return tex; } @@ -113,7 +114,8 @@ void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, i GX2Texture* tex = (GX2Texture*)texId; uint32_t* dst = (uint32_t*)tex->surface.image + (y * tex->surface.pitch) + x; - CopyTextureData(dst, tex->surface.pitch << 2, part, rowWidth << 2); + CopyTextureData(dst, tex->surface.pitch << 2, + part, rowWidth * BITMAPCOLOR_SIZE); GX2Invalidate(GX2_INVALIDATE_MODE_CPU_TEXTURE, tex->surface.image, tex->surface.imageSize); } diff --git a/src/Graphics_Xbox360.c b/src/Graphics_Xbox360.c index 490282494..8f5f3ef15 100644 --- a/src/Graphics_Xbox360.c +++ b/src/Graphics_Xbox360.c @@ -96,7 +96,8 @@ static void Gfx_RestoreState(void) { static void SetTextureData(struct XenosSurface* xtex, int x, int y, const struct Bitmap* bmp, int rowWidth, int lvl) { void* dst = Xe_Surface_LockRect(xe, xtex, x, y, bmp->width, bmp->height, XE_LOCK_WRITE); - CopyTextureData(dst, bmp->width * 4, bmp, rowWidth << 2); + CopyTextureData(dst, bmp->width * BITMAPCOLOR_SIZE, + bmp, rowWidth * BITMAPCOLOR_SIZE); Xe_Surface_Unlock(xe, xtex); } diff --git a/src/Menus.c b/src/Menus.c index fed51dbee..42ef076b4 100644 --- a/src/Menus.c +++ b/src/Menus.c @@ -1987,7 +1987,7 @@ static void KeyBindsScreen_OnBindingClick(void* screen, void* widget) { struct ButtonWidget* btn = (struct ButtonWidget*)widget; int old = s->curI; - s->curI = btn->meta.val; + s->curI = (int)btn->meta.val; s->closable = false; KeyBindsScreen_Update(s, s->curI); diff --git a/src/SystemFonts.c b/src/SystemFonts.c index e4f3878fd..54d89d0b0 100644 --- a/src/SystemFonts.c +++ b/src/SystemFonts.c @@ -817,7 +817,9 @@ static void DrawGrayscaleGlyph(FT_Bitmap* img, struct Bitmap* bmp, int x, int y, for (xx = 0; xx < img->width; xx++, src++, dst++) { if ((unsigned)(x + xx) >= (unsigned)bmp->width) continue; + I = *src; invI = UInt8_MaxValue - I; + if (!I) continue; /* TODO: Support transparent text */ /* dst->A = ((col.A * intensity) >> 8) + ((dst->A * invIntensity) >> 8);*/ diff --git a/src/_GLShared.h b/src/_GLShared.h index b568289a5..912661e03 100644 --- a/src/_GLShared.h +++ b/src/_GLShared.h @@ -107,7 +107,8 @@ static CC_NOINLINE void UpdateTextureSlow(int x, int y, struct Bitmap* part, int ptr = Mem_Alloc(count, 4, "Gfx_UpdateTexture temp"); } - CopyTextureData(ptr, part->width << 2, part, rowWidth << 2); + CopyTextureData(ptr, part->width * BITMAPCOLOR_SIZE, + part, rowWidth * BITMAPCOLOR_SIZE); if (full) { _glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, part->width, part->height, 0, PIXEL_FORMAT, TRANSFER_FORMAT, ptr); diff --git a/src/_GraphicsBase.h b/src/_GraphicsBase.h index b1ac96422..8f46b3c14 100644 --- a/src/_GraphicsBase.h +++ b/src/_GraphicsBase.h @@ -328,7 +328,7 @@ static CC_INLINE void CopyTextureData(void* dst, int dstStride, const struct Bit } else { /* Have to copy scanline by scanline */ for (y = 0; y < src->height; y++) { - Mem_Copy(dst_, src_, src->width << 2); + Mem_Copy(dst_, src_, src->width * BITMAPCOLOR_SIZE); src_ += srcStride; dst_ += dstStride; } diff --git a/src/main.c b/src/main.c index 761e82ed4..591147f0c 100644 --- a/src/main.c +++ b/src/main.c @@ -73,8 +73,6 @@ static void SetupProgram(int argc, char** argv) { static int RunProgram(int argc, char** argv) { cc_string args[GAME_MAX_CMDARGS]; - cc_uint16 port; - int argsCount = Platform_GetCommandLineArgs(argc, argv, args); #ifdef _MSC_VER /* NOTE: Make sure to comment this out before pushing a commit */ @@ -120,7 +118,7 @@ static int RunProgram(int argc, char** argv) { String_Copy(&Game_Mppass, &args[1]); String_Copy(&Server.Address,&args[2]); - if (!Convert_ParseInt(&args[3], &Server.Port) || port < 0 || port > 65535) { + if (!Convert_ParseInt(&args[3], &Server.Port) || Server.Port < 0 || Server.Port > 65535) { WarnInvalidArg("Invalid port", &args[3]); return 1; }