diff --git a/src/Client/Bitmap.c b/src/Client/Bitmap.c index 54a8077e5..3d188e131 100644 --- a/src/Client/Bitmap.c +++ b/src/Client/Bitmap.c @@ -7,9 +7,7 @@ #include "Stream.h" void Bitmap_Create(struct Bitmap* bmp, Int32 width, Int32 height, UInt8* scan0) { - bmp->Width = width; bmp->Height = height; - bmp->Stride = width * BITMAP_SIZEOF_PIXEL; - bmp->Scan0 = scan0; + bmp->Width = width; bmp->Height = height; bmp->Scan0 = scan0; } void Bitmap_CopyBlock(Int32 srcX, Int32 srcY, Int32 dstX, Int32 dstY, struct Bitmap* src, struct Bitmap* dst, Int32 size) { @@ -25,19 +23,17 @@ void Bitmap_CopyBlock(Int32 srcX, Int32 srcY, Int32 dstX, Int32 dstY, struct Bit void Bitmap_Allocate(struct Bitmap* bmp, Int32 width, Int32 height) { bmp->Width = width; bmp->Height = height; - bmp->Stride = width * BITMAP_SIZEOF_PIXEL; bmp->Scan0 = Platform_MemAlloc(width * height, BITMAP_SIZEOF_PIXEL); - - if (bmp->Scan0 == NULL) { - ErrorHandler_Fail("Bitmap - failed to allocate memory"); - } + if (bmp->Scan0 == NULL) ErrorHandler_Fail("Bitmap - failed to allocate memory"); } void Bitmap_AllocateClearedPow2(struct Bitmap* bmp, Int32 width, Int32 height) { - width = Math_NextPowOf2(width); + width = Math_NextPowOf2(width); height = Math_NextPowOf2(height); - Bitmap_Allocate(bmp, width, height); - Platform_MemSet(bmp->Scan0, 0, Bitmap_DataSize(width, height)); + + bmp->Width = width; bmp->Height = height; + bmp->Scan0 = Platform_MemAllocCleared(width * height, BITMAP_SIZEOF_PIXEL); + if (bmp->Scan0 == NULL) ErrorHandler_Fail("Bitmap - failed to allocate memory"); } @@ -337,7 +333,6 @@ ReturnCode Bitmap_DecodePng(struct Bitmap* bmp, struct Stream* stream) { if (bmp->Width < 0 || bmp->Width > PNG_MAX_DIMS) return PNG_ERR_TOO_WIDE; if (bmp->Height < 0 || bmp->Height > PNG_MAX_DIMS) return PNG_ERR_TOO_TALL; - bmp->Stride = bmp->Width * BITMAP_SIZEOF_PIXEL; bmp->Scan0 = Platform_MemAlloc(bmp->Width * bmp->Height, BITMAP_SIZEOF_PIXEL); if (bmp->Scan0 == NULL) ErrorHandler_Fail("Failed to allocate memory for PNG bitmap"); diff --git a/src/Client/Bitmap.h b/src/Client/Bitmap.h index 6c21e6bf4..c2f941888 100644 --- a/src/Client/Bitmap.h +++ b/src/Client/Bitmap.h @@ -5,17 +5,12 @@ Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 */ struct Stream; -struct Bitmap { - UInt8* Scan0; /* Pointer to first scaneline. */ - UInt32 Stride; /* Number of bytes in each scanline. TODO: Obsolete this completely and just use Width << 2 ? */ - Int32 Width; /* Number of pixels horizontally. */ - Int32 Height; /* Number of pixels vertically. */ -}; +struct Bitmap { UInt8* Scan0; Int32 Width, Height; }; #define PNG_MAX_DIMS 0x8000L -#define BITMAP_SIZEOF_PIXEL 4 +#define BITMAP_SIZEOF_PIXEL 4 /* 32 bit ARGB */ #define Bitmap_DataSize(width, height) ((UInt32)(width) * (UInt32)(height) * (UInt32)BITMAP_SIZEOF_PIXEL) -#define Bitmap_GetRow(bmp, y) ((UInt32*)((bmp)->Scan0 + ((y) * (bmp)->Stride))) +#define Bitmap_GetRow(bmp, y) ((UInt32*)((bmp)->Scan0 + ((y) * ((bmp)->Width << 2)))) #define Bitmap_GetPixel(bmp, x, y) (Bitmap_GetRow(bmp, y)[x]) void Bitmap_Create(struct Bitmap* bmp, Int32 width, Int32 height, UInt8* scan0); diff --git a/src/Client/ChunkUpdater.c b/src/Client/ChunkUpdater.c index 32a358e18..6b3d9f038 100644 --- a/src/Client/ChunkUpdater.c +++ b/src/Client/ChunkUpdater.c @@ -94,14 +94,12 @@ static void ChunkUpdater_FreeAllocations(void) { } static void ChunkUpdater_PerformPartsAllocations(void) { - UInt32 partsCount = MapRenderer_ChunksCount * MapRenderer_1DUsedCount; - MapRenderer_PartsBuffer_Raw = Platform_MemAlloc(partsCount * 2, sizeof(struct ChunkPartInfo)); - if (MapRenderer_PartsBuffer_Raw == NULL) ErrorHandler_Fail("ChunkUpdater - failed to allocate chunk parts buffer"); + UInt32 count = MapRenderer_ChunksCount * MapRenderer_1DUsedCount; + MapRenderer_PartsBuffer_Raw = Platform_MemAllocCleared(count * 2, sizeof(struct ChunkPartInfo)); + if (MapRenderer_PartsBuffer_Raw == NULL) ErrorHandler_Fail("ChunkUpdater - failed to allocate chunk parts"); - UInt32 partsSize = partsCount * 2 * (UInt32)sizeof(struct ChunkPartInfo); - Platform_MemSet(MapRenderer_PartsBuffer_Raw, 0, partsSize); MapRenderer_PartsNormal = MapRenderer_PartsBuffer_Raw; - MapRenderer_PartsTranslucent = MapRenderer_PartsBuffer_Raw + partsCount; + MapRenderer_PartsTranslucent = MapRenderer_PartsBuffer_Raw + count; } static void ChunkUpdater_PerformAllocations(void) { diff --git a/src/Client/NixPlatform.c b/src/Client/NixPlatform.c index 293a2f28b..676cf5089 100644 --- a/src/Client/NixPlatform.c +++ b/src/Client/NixPlatform.c @@ -81,6 +81,10 @@ void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize) { return malloc(numElems * elemsSize); } +void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize) { + return calloc(numElems, elemsSize); +} + void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize) { return realloc(mem, numElems * elemsSize); } diff --git a/src/Client/PacketHandlers.c b/src/Client/PacketHandlers.c index 395cc8dd4..c9c04677f 100644 --- a/src/Client/PacketHandlers.c +++ b/src/Client/PacketHandlers.c @@ -404,7 +404,7 @@ static void Classic_LevelInit(struct Stream* stream) { /* Fast map puts volume in header, doesn't bother with gzip */ if (cpe_fastMap) { - mapVolume = Stream_ReadI32_BE(stream); + mapVolume = Stream_ReadU32_BE(stream); gzHeader.Done = true; mapSizeIndex = sizeof(UInt32); map = Platform_MemAlloc(mapVolume, sizeof(BlockID)); @@ -436,7 +436,7 @@ static void Classic_LevelDataChunk(struct Stream* stream) { if (mapSizeIndex == sizeof(UInt32)) { if (map == NULL) { - mapVolume = (mapSize[0] << 24) | (mapSize[1] << 16) | (mapSize[2] << 8) | mapSize[3]; + mapVolume = Stream_GetU32_BE(mapSize); map = Platform_MemAlloc(mapVolume, sizeof(BlockID)); if (map == NULL) ErrorHandler_Fail("Failed to allocate memory for map"); } diff --git a/src/Client/Platform.h b/src/Client/Platform.h index 48332ca06..8af4a8d1e 100644 --- a/src/Client/Platform.h +++ b/src/Client/Platform.h @@ -33,6 +33,7 @@ void Platform_Exit(ReturnCode code); STRING_PURE String Platform_GetCommandLineArgs(void); void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize); +void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize); void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize); void Platform_MemFree(void** mem); void Platform_MemSet(void* dst, UInt8 value, UInt32 numBytes); diff --git a/src/Client/WinPlatform.c b/src/Client/WinPlatform.c index f03f06305..af94351f6 100644 --- a/src/Client/WinPlatform.c +++ b/src/Client/WinPlatform.c @@ -106,6 +106,11 @@ void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize) { return HeapAlloc(heap, 0, numBytes); } +void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize) { + UInt32 numBytes = numElems * elemsSize; /* TODO: avoid overflow here */ + return HeapAlloc(heap, HEAP_ZERO_MEMORY, numBytes); +} + void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize) { UInt32 numBytes = numElems * elemsSize; /* TODO: avoid overflow here */ return HeapReAlloc(heap, 0, mem, numBytes); @@ -456,7 +461,7 @@ struct Size2D Platform_TextDraw(struct DrawTextArgs* args, Int32 x, Int32 y, Pac Int32 xx, yy; struct Bitmap* bmp = platform_bmp; for (yy = 0; yy < area.cy; yy++) { - UInt8* src = (UInt8*)platform_bits + (yy * bmp->Stride); + UInt8* src = (UInt8*)platform_bits + (yy * (bmp->Width << 2)); UInt8* dst = (UInt8*)Bitmap_GetRow(bmp, y + yy); dst += x * BITMAP_SIZEOF_PIXEL; for (xx = 0; xx < area.cx; xx++) {