now get up to setup header before dying

This commit is contained in:
UnknownShadow200 2018-07-28 06:08:39 +10:00
parent 6cdc32f574
commit 098136160e
21 changed files with 113 additions and 112 deletions

1
.gitignore vendored
View File

@ -23,6 +23,7 @@ bld/
[Oo]bj/ [Oo]bj/
[Oo]utput/ [Oo]utput/
[Pp]rofilingSessions/ [Pp]rofilingSessions/
[sS]rc/Client/audio
[sS]rc/Client/texpacks [sS]rc/Client/texpacks
[sS]rc/Client/maps [sS]rc/Client/maps
[sS]rc/Client/texturecache [sS]rc/Client/texturecache

View File

@ -202,8 +202,7 @@ static void Animations_Draw(struct AnimationData* data, TextureLoc texLoc, Int32
UInt8* ptr = buffer; UInt8* ptr = buffer;
if (size > ANIMS_FAST_SIZE) { if (size > ANIMS_FAST_SIZE) {
/* cannot allocate memory on the stack for very big animation.png frames */ /* cannot allocate memory on the stack for very big animation.png frames */
ptr = Platform_MemAlloc(size * size, BITMAP_SIZEOF_PIXEL); ptr = Platform_MemAlloc(size * size, BITMAP_SIZEOF_PIXEL, "anim frame");
if (!ptr) ErrorHandler_Fail("Failed to allocate memory for anim frame");
} }
Int32 index_1D = Atlas1D_Index(texLoc); Int32 index_1D = Atlas1D_Index(texLoc);

View File

@ -85,6 +85,7 @@ void Ogg_MakeStream(struct Stream* stream, UInt8* buffer, struct Stream* source)
/* Ensures there are 'bitsCount' bits */ /* Ensures there are 'bitsCount' bits */
#define Vorbis_EnsureBits(state, bitsCount) while (state->NumBits < bitsCount) { Vorbis_GetByte(state); } #define Vorbis_EnsureBits(state, bitsCount) while (state->NumBits < bitsCount) { Vorbis_GetByte(state); }
/* Peeks then consumes given bits */ /* Peeks then consumes given bits */
/* TODO: COMPLETELY BROKEN */
#define Vorbis_ReadBits(state, bitsCount) Vorbis_PeekBits(state, bitsCount); Vorbis_ConsumeBits(state, bitsCount); #define Vorbis_ReadBits(state, bitsCount) Vorbis_PeekBits(state, bitsCount); Vorbis_ConsumeBits(state, bitsCount);
#define VORBIS_MAX_CHANS 8 #define VORBIS_MAX_CHANS 8
@ -130,7 +131,7 @@ UInt32 Codebook_Pow(UInt32 base, UInt32 exp) {
} }
UInt32 lookup1_values(UInt32 entries, UInt32 dimensions) { UInt32 lookup1_values(UInt32 entries, UInt32 dimensions) {
UInt32 i, j; UInt32 i;
/* the greatest integer value for which [value] to the power of [dimensions] is less than or equal to [entries] */ /* the greatest integer value for which [value] to the power of [dimensions] is less than or equal to [entries] */
/* TODO: verify this */ /* TODO: verify this */
for (i = 1; ; i++) { for (i = 1; ; i++) {
@ -388,6 +389,7 @@ static ReturnCode Vorbis_DecodeHeader(struct VorbisState* state, UInt8 type) {
static ReturnCode Vorbis_DecodeIdentifier(struct VorbisState* state) { static ReturnCode Vorbis_DecodeIdentifier(struct VorbisState* state) {
UInt8 header[23]; UInt8 header[23];
Stream_Read(state->Source, header, sizeof(header));
UInt32 version = Stream_GetU32_LE(&header[0]); UInt32 version = Stream_GetU32_LE(&header[0]);
if (version != 0) return VORBIS_ERR_VERSION; if (version != 0) return VORBIS_ERR_VERSION;
@ -424,6 +426,7 @@ static ReturnCode Vorbis_DecodeSetup(struct VorbisState* state) {
ReturnCode result; ReturnCode result;
count = Vorbis_ReadBits(state, 8); count++; count = Vorbis_ReadBits(state, 8); count++;
state->Codebooks = Platform_MemAlloc(count, sizeof(struct Codebook), "vorbis codebooks");
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
result = Codebook_DecodeSetup(state, &state->Codebooks[i]); result = Codebook_DecodeSetup(state, &state->Codebooks[i]);
if (result) return result; if (result) return result;
@ -436,6 +439,7 @@ static ReturnCode Vorbis_DecodeSetup(struct VorbisState* state) {
} }
count = Vorbis_ReadBits(state, 6); count++; count = Vorbis_ReadBits(state, 6); count++;
state->Floors = Platform_MemAlloc(count, sizeof(struct Floor), "vorbis floors");
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
UInt16 floor = Vorbis_ReadBits(state, 16); UInt16 floor = Vorbis_ReadBits(state, 16);
if (floor != 1) return VORBIS_ERR_FLOOR_TYPE; if (floor != 1) return VORBIS_ERR_FLOOR_TYPE;
@ -444,6 +448,7 @@ static ReturnCode Vorbis_DecodeSetup(struct VorbisState* state) {
} }
count = Vorbis_ReadBits(state, 6); count++; count = Vorbis_ReadBits(state, 6); count++;
state->Residues = Platform_MemAlloc(count, sizeof(struct Residue), "vorbis residues");
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
UInt16 residue = Vorbis_ReadBits(state, 16); UInt16 residue = Vorbis_ReadBits(state, 16);
if (residue > 2) return VORBIS_ERR_FLOOR_TYPE; if (residue > 2) return VORBIS_ERR_FLOOR_TYPE;
@ -452,6 +457,7 @@ static ReturnCode Vorbis_DecodeSetup(struct VorbisState* state) {
} }
count = Vorbis_ReadBits(state, 6); count++; count = Vorbis_ReadBits(state, 6); count++;
state->Mappings = Platform_MemAlloc(count, sizeof(struct Mapping), "vorbis mappings");
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
UInt16 mapping = Vorbis_ReadBits(state, 16); UInt16 mapping = Vorbis_ReadBits(state, 16);
if (mapping != 0) return VORBIS_ERR_FLOOR_TYPE; if (mapping != 0) return VORBIS_ERR_FLOOR_TYPE;
@ -460,6 +466,7 @@ static ReturnCode Vorbis_DecodeSetup(struct VorbisState* state) {
} }
count = Vorbis_ReadBits(state, 6); count++; count = Vorbis_ReadBits(state, 6); count++;
state->Modes = Platform_MemAlloc(count, sizeof(struct Mode), "vorbis modes");
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
result = Mode_DecodeSetup(state, &state->Modes[i]); result = Mode_DecodeSetup(state, &state->Modes[i]);
if (result) return result; if (result) return result;

View File

@ -43,4 +43,5 @@ struct VorbisState {
struct Mode* Modes; struct Mode* Modes;
}; };
void Vorbis_Init(struct VorbisState* state, struct Stream* source); void Vorbis_Init(struct VorbisState* state, struct Stream* source);
ReturnCode Vorbis_DecodeHeaders(struct VorbisState* state);
#endif #endif

View File

@ -23,8 +23,7 @@ void Bitmap_CopyBlock(Int32 srcX, Int32 srcY, Int32 dstX, Int32 dstY, struct Bit
void Bitmap_Allocate(struct Bitmap* bmp, Int32 width, Int32 height) { void Bitmap_Allocate(struct Bitmap* bmp, Int32 width, Int32 height) {
bmp->Width = width; bmp->Height = height; bmp->Width = width; bmp->Height = height;
bmp->Scan0 = Platform_MemAlloc(width * height, BITMAP_SIZEOF_PIXEL); bmp->Scan0 = Platform_MemAlloc(width * height, BITMAP_SIZEOF_PIXEL, "bitmap data");
if (!bmp->Scan0) ErrorHandler_Fail("Bitmap - failed to allocate memory");
} }
void Bitmap_AllocateClearedPow2(struct Bitmap* bmp, Int32 width, Int32 height) { void Bitmap_AllocateClearedPow2(struct Bitmap* bmp, Int32 width, Int32 height) {
@ -32,8 +31,7 @@ void Bitmap_AllocateClearedPow2(struct Bitmap* bmp, Int32 width, Int32 height) {
height = Math_NextPowOf2(height); height = Math_NextPowOf2(height);
bmp->Width = width; bmp->Height = height; bmp->Width = width; bmp->Height = height;
bmp->Scan0 = Platform_MemAllocCleared(width * height, BITMAP_SIZEOF_PIXEL); bmp->Scan0 = Platform_MemAllocCleared(width * height, BITMAP_SIZEOF_PIXEL, "bitmap data");
if (!bmp->Scan0) ErrorHandler_Fail("Bitmap - failed to allocate memory");
} }
@ -369,9 +367,7 @@ 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->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; if (bmp->Height < 0 || bmp->Height > PNG_MAX_DIMS) return PNG_ERR_TOO_TALL;
bmp->Scan0 = Platform_MemAlloc(bmp->Width * bmp->Height, BITMAP_SIZEOF_PIXEL); bmp->Scan0 = Platform_MemAlloc(bmp->Width * bmp->Height, BITMAP_SIZEOF_PIXEL, "PNG bitmap data");
if (!bmp->Scan0) ErrorHandler_Fail("Failed to allocate memory for PNG bitmap");
bitsPerSample = buffer[8]; col = buffer[9]; bitsPerSample = buffer[8]; col = buffer[9];
rowExpander = Png_GetExpander(col, bitsPerSample); rowExpander = Png_GetExpander(col, bitsPerSample);
if (rowExpander == NULL) return PNG_ERR_INVALID_COL_BPP; if (rowExpander == NULL) return PNG_ERR_INVALID_COL_BPP;

View File

@ -42,13 +42,10 @@ static void TickQueue_Resize(struct TickQueue* queue) {
if (queue->BufferSize >= (Int32_MaxValue / 4)) { if (queue->BufferSize >= (Int32_MaxValue / 4)) {
ErrorHandler_Fail("TickQueue - too large to resize."); ErrorHandler_Fail("TickQueue - too large to resize.");
} }
UInt32 capacity = queue->BufferSize * 2; UInt32 capacity = queue->BufferSize * 2;
if (capacity < 32) capacity = 32; if (capacity < 32) capacity = 32;
UInt32* newBuffer = Platform_MemAlloc(capacity, sizeof(UInt32), "physics tick queue");
UInt32* newBuffer = Platform_MemAlloc(capacity, sizeof(UInt32));
if (!newBuffer) {
ErrorHandler_Fail("TickQueue - failed to allocate memory");
}
UInt32 i, idx; UInt32 i, idx;
for (i = 0; i < queue->Size; i++) { for (i = 0; i < queue->Size; i++) {

View File

@ -368,10 +368,8 @@ static void Builder_DefaultPostStretchTiles(Int32 x1, Int32 y1, Int32 z1) {
if (vertsCount > Builder_VerticesElems) { if (vertsCount > Builder_VerticesElems) {
Platform_MemFree(&Builder_Vertices); Platform_MemFree(&Builder_Vertices);
/* ensure buffer can be accessed with 64 bytes alignment by putting 2 extra vertices at end. */ /* ensure buffer can be accessed with 64 bytes alignment by putting 2 extra vertices at end. */
Builder_Vertices = Platform_MemAlloc(vertsCount + 2, sizeof(VertexP3fT2fC4b)); Builder_Vertices = Platform_MemAlloc(vertsCount + 2, sizeof(VertexP3fT2fC4b), "chunk vertices");
Builder_VerticesElems = vertsCount; Builder_VerticesElems = vertsCount;
if (!Builder_Vertices) ErrorHandler_Fail("Builder1DPart_Prepare - failed to allocate memory");
} }
vertsCount = 0; vertsCount = 0;

View File

@ -95,26 +95,16 @@ static void ChunkUpdater_FreeAllocations(void) {
static void ChunkUpdater_PerformPartsAllocations(void) { static void ChunkUpdater_PerformPartsAllocations(void) {
UInt32 count = MapRenderer_ChunksCount * MapRenderer_1DUsedCount; UInt32 count = MapRenderer_ChunksCount * MapRenderer_1DUsedCount;
MapRenderer_PartsBuffer_Raw = Platform_MemAllocCleared(count * 2, sizeof(struct ChunkPartInfo)); MapRenderer_PartsBuffer_Raw = Platform_MemAllocCleared(count * 2, sizeof(struct ChunkPartInfo), "chunk parts");
if (!MapRenderer_PartsBuffer_Raw) ErrorHandler_Fail("ChunkUpdater - failed to allocate chunk parts"); MapRenderer_PartsNormal = MapRenderer_PartsBuffer_Raw;
MapRenderer_PartsNormal = MapRenderer_PartsBuffer_Raw;
MapRenderer_PartsTranslucent = MapRenderer_PartsBuffer_Raw + count; MapRenderer_PartsTranslucent = MapRenderer_PartsBuffer_Raw + count;
} }
static void ChunkUpdater_PerformAllocations(void) { static void ChunkUpdater_PerformAllocations(void) {
MapRenderer_Chunks = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(struct ChunkInfo)); MapRenderer_Chunks = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(struct ChunkInfo), "chunk info");
if (!MapRenderer_Chunks) ErrorHandler_Fail("ChunkUpdater - failed to allocate chunk info"); MapRenderer_SortedChunks = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(struct ChunkInfo*), "sorted chunk info");
MapRenderer_RenderChunks = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(struct ChunkInfo*), "render chunk info");
MapRenderer_SortedChunks = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(struct ChunkInfo*)); ChunkUpdater_Distances = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(Int32), "chunk distances");
if (!MapRenderer_Chunks) ErrorHandler_Fail("ChunkUpdater - failed to allocate sorted chunk info");
MapRenderer_RenderChunks = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(struct ChunkInfo*));
if (!MapRenderer_RenderChunks) ErrorHandler_Fail("ChunkUpdater - failed to allocate render chunk info");
ChunkUpdater_Distances = Platform_MemAlloc(MapRenderer_ChunksCount, sizeof(Int32));
if (!ChunkUpdater_Distances) ErrorHandler_Fail("ChunkUpdater - failed to allocate chunk distances");
ChunkUpdater_PerformPartsAllocations(); ChunkUpdater_PerformPartsAllocations();
} }

View File

@ -202,8 +202,7 @@ static void D3D9_DoMipmaps(IDirect3DTexture9* texture, Int32 x, Int32 y, struct
if (width > 1) width /= 2; if (width > 1) width /= 2;
if (height > 1) height /= 2; if (height > 1) height /= 2;
UInt8* cur = Platform_MemAlloc(width * height, BITMAP_SIZEOF_PIXEL); UInt8* cur = Platform_MemAlloc(width * height, BITMAP_SIZEOF_PIXEL, "mipmaps");
if (!cur) ErrorHandler_Fail("Allocating memory for mipmaps");
GfxCommon_GenMipmaps(width, height, cur, prev); GfxCommon_GenMipmaps(width, height, cur, prev);
struct Bitmap mipmap; struct Bitmap mipmap;

View File

@ -19,6 +19,7 @@
#include "Camera.h" #include "Camera.h"
#include "Particle.h" #include "Particle.h"
#define ENV_SMALL_VERTICES 4096
static Real32 EnvRenderer_BlendFactor(Real32 x) { static Real32 EnvRenderer_BlendFactor(Real32 x) {
/* return -0.05 + 0.22 * (Math_Log(x) * 0.25f); */ /* return -0.05 + 0.22 * (Math_Log(x) * 0.25f); */
Real64 blend = -0.13 + 0.28 * (Math_Log(x) * 0.25); Real64 blend = -0.13 + 0.28 * (Math_Log(x) * 0.25);
@ -176,17 +177,16 @@ static void EnvRenderer_UpdateClouds(void) {
Int32 z1 = -extent, z2 = World_Length + extent; Int32 z1 = -extent, z2 = World_Length + extent;
clouds_vertices = EnvRenderer_Vertices(x2 - x1, z2 - z1); clouds_vertices = EnvRenderer_Vertices(x2 - x1, z2 - z1);
VertexP3fT2fC4b v[4096]; VertexP3fT2fC4b v[ENV_SMALL_VERTICES];
VertexP3fT2fC4b* ptr = v; VertexP3fT2fC4b* ptr = v;
if (clouds_vertices > 4096) { if (clouds_vertices > ENV_SMALL_VERTICES) {
ptr = Platform_MemAlloc(clouds_vertices, sizeof(VertexP3fT2fC4b)); ptr = Platform_MemAlloc(clouds_vertices, sizeof(VertexP3fT2fC4b), "temp clouds vertices");
if (!ptr) ErrorHandler_Fail("EnvRenderer_Clouds - failed to allocate memory");
} }
EnvRenderer_DrawCloudsY(x1, z1, x2, z2, WorldEnv_CloudsHeight, ptr); EnvRenderer_DrawCloudsY(x1, z1, x2, z2, WorldEnv_CloudsHeight, ptr);
clouds_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FT2FC4B, clouds_vertices); clouds_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FT2FC4B, clouds_vertices);
if (clouds_vertices > 4096) Platform_MemFree(&ptr); if (clouds_vertices > ENV_SMALL_VERTICES) Platform_MemFree(&ptr);
} }
@ -249,18 +249,17 @@ static void EnvRenderer_UpdateSky(void) {
Int32 z1 = -extent, z2 = World_Length + extent; Int32 z1 = -extent, z2 = World_Length + extent;
sky_vertices = EnvRenderer_Vertices(x2 - x1, z2 - z1); sky_vertices = EnvRenderer_Vertices(x2 - x1, z2 - z1);
VertexP3fC4b v[4096]; VertexP3fC4b v[ENV_SMALL_VERTICES];
VertexP3fC4b* ptr = v; VertexP3fC4b* ptr = v;
if (sky_vertices > 4096) { if (sky_vertices > ENV_SMALL_VERTICES) {
ptr = Platform_MemAlloc(sky_vertices, sizeof(VertexP3fC4b)); ptr = Platform_MemAlloc(sky_vertices, sizeof(VertexP3fC4b), "temp sky vertices");
if (!ptr) ErrorHandler_Fail("EnvRenderer_Sky - failed to allocate memory");
} }
Int32 height = max((World_Height + 2) + 6, WorldEnv_CloudsHeight + 6); Int32 height = max((World_Height + 2) + 6, WorldEnv_CloudsHeight + 6);
EnvRenderer_DrawSkyY(x1, z1, x2, z2, height, ptr); EnvRenderer_DrawSkyY(x1, z1, x2, z2, height, ptr);
sky_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FC4B, sky_vertices); sky_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FC4B, sky_vertices);
if (sky_vertices > 4096) Platform_MemFree(&ptr); if (sky_vertices > ENV_SMALL_VERTICES) Platform_MemFree(&ptr);
} }
/*########################################################################################################################* /*########################################################################################################################*
@ -342,9 +341,7 @@ Real64 weather_accumulator;
Vector3I weather_lastPos; Vector3I weather_lastPos;
static void EnvRenderer_InitWeatherHeightmap(void) { static void EnvRenderer_InitWeatherHeightmap(void) {
Weather_Heightmap = Platform_MemAlloc(World_Width * World_Length, sizeof(Int16)); Weather_Heightmap = Platform_MemAlloc(World_Width * World_Length, sizeof(Int16), "weather heightmap");
if (!Weather_Heightmap) ErrorHandler_Fail("WeatherRenderer - Failed to allocate heightmap");
Int32 i; Int32 i;
for (i = 0; i < World_Width * World_Length; i++) { for (i = 0; i < World_Width * World_Length; i++) {
Weather_Heightmap[i] = Int16_MaxValue; Weather_Heightmap[i] = Int16_MaxValue;
@ -646,11 +643,10 @@ static void EnvRenderer_UpdateMapSides(void) {
sides_vertices += 2 * EnvRenderer_Vertices(World_Width, Math_AbsI(y)); /* ZQuads */ sides_vertices += 2 * EnvRenderer_Vertices(World_Width, Math_AbsI(y)); /* ZQuads */
sides_vertices += 2 * EnvRenderer_Vertices(World_Length, Math_AbsI(y)); /* XQuads */ sides_vertices += 2 * EnvRenderer_Vertices(World_Length, Math_AbsI(y)); /* XQuads */
VertexP3fT2fC4b v[4096]; VertexP3fT2fC4b v[ENV_SMALL_VERTICES];
VertexP3fT2fC4b* ptr = v; VertexP3fT2fC4b* ptr = v;
if (sides_vertices > 4096) { if (sides_vertices > ENV_SMALL_VERTICES) {
ptr = Platform_MemAlloc(sides_vertices, sizeof(VertexP3fT2fC4b)); ptr = Platform_MemAlloc(sides_vertices, sizeof(VertexP3fT2fC4b), "temp sides vertices");
if (!ptr) ErrorHandler_Fail("BordersRenderer_Sides - failed to allocate memory");
} }
VertexP3fT2fC4b* temp = ptr; VertexP3fT2fC4b* temp = ptr;
@ -675,7 +671,7 @@ static void EnvRenderer_UpdateMapSides(void) {
EnvRenderer_DrawBorderX(World_Width, 0, World_Length, y1, y2, col, &temp); EnvRenderer_DrawBorderX(World_Width, 0, World_Length, y1, y2, col, &temp);
sides_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FT2FC4B, sides_vertices); sides_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FT2FC4B, sides_vertices);
if (sides_vertices > 4096) Platform_MemFree(&ptr); if (sides_vertices > ENV_SMALL_VERTICES) Platform_MemFree(&ptr);
} }
static void EnvRenderer_UpdateMapEdges(void) { static void EnvRenderer_UpdateMapEdges(void) {
@ -693,11 +689,10 @@ static void EnvRenderer_UpdateMapEdges(void) {
edges_vertices += EnvRenderer_Vertices(r.Width, r.Height); /* YPlanes outside */ edges_vertices += EnvRenderer_Vertices(r.Width, r.Height); /* YPlanes outside */
} }
VertexP3fT2fC4b v[4096]; VertexP3fT2fC4b v[ENV_SMALL_VERTICES];
VertexP3fT2fC4b* ptr = v; VertexP3fT2fC4b* ptr = v;
if (edges_vertices > 4096) { if (edges_vertices > ENV_SMALL_VERTICES) {
ptr = Platform_MemAlloc(edges_vertices, sizeof(VertexP3fT2fC4b)); ptr = Platform_MemAlloc(edges_vertices, sizeof(VertexP3fT2fC4b), "temp edge vertices");
if (!ptr) ErrorHandler_Fail("BordersRenderer_Edges - failed to allocate memory");
} }
VertexP3fT2fC4b* temp = ptr; VertexP3fT2fC4b* temp = ptr;
@ -714,7 +709,7 @@ static void EnvRenderer_UpdateMapEdges(void) {
} }
edges_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FT2FC4B, edges_vertices); edges_vb = Gfx_CreateVb(ptr, VERTEX_FORMAT_P3FT2FC4B, edges_vertices);
if (edges_vertices > 4096) Platform_MemFree(&ptr); if (edges_vertices > ENV_SMALL_VERTICES) Platform_MemFree(&ptr);
} }

View File

@ -13,10 +13,7 @@
static void Map_ReadBlocks(struct Stream* stream) { static void Map_ReadBlocks(struct Stream* stream) {
World_BlocksSize = World_Width * World_Length * World_Height; World_BlocksSize = World_Width * World_Length * World_Height;
World_Blocks = Platform_MemAlloc(World_BlocksSize, sizeof(BlockID)); World_Blocks = Platform_MemAlloc(World_BlocksSize, sizeof(BlockID), "map blocks for load");
if (!World_Blocks) {
ErrorHandler_Fail("Failed to allocate memory for reading blocks array from file");
}
Stream_Read(stream, World_Blocks, World_BlocksSize); Stream_Read(stream, World_Blocks, World_BlocksSize);
} }
@ -294,8 +291,7 @@ static void Nbt_ReadTag(UInt8 typeId, bool readTagName, struct Stream* stream, s
if (count < NBT_SMALL_SIZE) { if (count < NBT_SMALL_SIZE) {
Stream_Read(stream, tag.DataSmall, count); Stream_Read(stream, tag.DataSmall, count);
} else { } else {
tag.DataBig = Platform_MemAlloc(count, sizeof(UInt8)); tag.DataBig = Platform_MemAlloc(count, sizeof(UInt8), "NBT tag data");
if (!tag.DataBig) ErrorHandler_Fail("Nbt_ReadTag - allocating memory");
Stream_Read(stream, tag.DataBig, count); Stream_Read(stream, tag.DataBig, count);
} }
break; break;
@ -379,8 +375,7 @@ static bool Cw_Callback_1(struct NbtTag* tag) {
if (IsTag(tag, "BlockArray")) { if (IsTag(tag, "BlockArray")) {
World_BlocksSize = tag->DataSize; World_BlocksSize = tag->DataSize;
if (tag->DataSize < NBT_SMALL_SIZE) { if (tag->DataSize < NBT_SMALL_SIZE) {
World_Blocks = Platform_MemAlloc(World_BlocksSize, sizeof(UInt8)); World_Blocks = Platform_MemAlloc(World_BlocksSize, sizeof(UInt8), ".cw map blocks");
if (!World_Blocks) ErrorHandler_Fail("Failed to allocate memory for map");
Platform_MemCpy(World_Blocks, tag->DataSmall, tag->DataSize); Platform_MemCpy(World_Blocks, tag->DataSmall, tag->DataSize);
} else { } else {
World_Blocks = tag->DataBig; World_Blocks = tag->DataBig;
@ -706,8 +701,7 @@ static void Dat_ReadFieldData(struct Stream* stream, struct JFieldDesc* field) {
if (arrayClassDesc.ClassName[1] != JFIELD_INT8) ErrorHandler_Fail("Only byte array fields supported"); if (arrayClassDesc.ClassName[1] != JFIELD_INT8) ErrorHandler_Fail("Only byte array fields supported");
UInt32 size = Stream_ReadU32_BE(stream); UInt32 size = Stream_ReadU32_BE(stream);
field->Value_Ptr = Platform_MemAlloc(size, sizeof(UInt8)); field->Value_Ptr = Platform_MemAlloc(size, sizeof(UInt8), ".dat map blocks");
if (!field->Value_Ptr) ErrorHandler_Fail("Failed to allocate memory for map");
Stream_Read(stream, field->Value_Ptr, size); Stream_Read(stream, field->Value_Ptr, size);
field->Value_Size = size; field->Value_Size = size;

View File

@ -332,8 +332,7 @@ static void Lighting_OnNewMap(void) {
} }
static void Lighting_OnNewMapLoaded(void) { static void Lighting_OnNewMapLoaded(void) {
Lighting_heightmap = Platform_MemAlloc(World_Width * World_Length, sizeof(Int16)); Lighting_heightmap = Platform_MemAlloc(World_Width * World_Length, sizeof(Int16), "lighting heightmap");
if (!Lighting_heightmap) ErrorHandler_Fail("WorldLighting - failed to allocate heightmap");
Lighting_Refresh(); Lighting_Refresh();
} }

View File

@ -15,10 +15,7 @@ static void Gen_Init(void) {
Gen_CurrentProgress = 0.0f; Gen_CurrentProgress = 0.0f;
Gen_CurrentState = ""; Gen_CurrentState = "";
Gen_Blocks = Platform_MemAlloc(Gen_Width * Gen_Height * Gen_Length, sizeof(BlockID)); Gen_Blocks = Platform_MemAlloc(Gen_Width * Gen_Height * Gen_Length, sizeof(BlockID), "map blocks for gen");
if (!Gen_Blocks) {
ErrorHandler_Fail("MapGen - failed to allocate Blocks array");
}
Gen_Done = false; Gen_Done = false;
} }
@ -470,10 +467,7 @@ static void NotchyGen_PlantTrees(void) {
void NotchyGen_Generate(void) { void NotchyGen_Generate(void) {
Gen_Init(); Gen_Init();
Heightmap = Platform_MemAlloc(Gen_Width * Gen_Length, sizeof(Int16)); Heightmap = Platform_MemAlloc(Gen_Width * Gen_Length, sizeof(Int16), "gen heightmap");
if (!Heightmap) {
ErrorHandler_Fail("NotchyGen - Failed to allocate Heightmap array");
}
Random_Init(&rnd, Gen_Seed); Random_Init(&rnd, Gen_Seed);
oneY = Gen_Width * Gen_Length; oneY = Gen_Width * Gen_Length;

View File

@ -76,16 +76,30 @@ STRING_PURE String Platform_GetCommandLineArgs(void) {
return String_MakeNull(); return String_MakeNull();
} }
void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize) { /* TODO: Not duplicate with windows*/
return malloc(numElems * elemsSize); static void Platform_AllocFailed(const UChar* place) {
UChar logBuffer[String_BufferSize(STRING_SIZE + 20)];
String log = String_InitAndClearArray(logBuffer);
String_Format1(&log, "Failed allocating memory for: %c", place);
ErrorHandler_Fail(&log.buffer);
} }
void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize) { void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize, const UChar* place) {
return calloc(numElems, elemsSize); void* ptr = malloc(numElems * elemsSize); /* TODO: avoid overflow here */
if (!ptr) Platform_AllocFailed(place);
return ptr;
} }
void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize) { void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize, const UChar* place) {
return realloc(mem, numElems * elemsSize); void* ptr = calloc(numElems, elemsSize); /* TODO: avoid overflow here */
if (!ptr) Platform_AllocFailed(place);
return ptr;
}
void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize, const UChar* place) {
void* ptr = realloc(mem, numElems * elemsSize); /* TODO: avoid overflow here */
if (!ptr) Platform_AllocFailed(place);
return ptr;
} }
void Platform_MemFree(void** mem) { void Platform_MemFree(void** mem) {

View File

@ -122,8 +122,7 @@ static void GL_DoMipmaps(GfxResourceID texId, Int32 x, Int32 y, struct Bitmap* b
if (width > 1) width /= 2; if (width > 1) width /= 2;
if (height > 1) height /= 2; if (height > 1) height /= 2;
UInt8* cur = Platform_MemAlloc(width * height, BITMAP_SIZEOF_PIXEL); UInt8* cur = Platform_MemAlloc(width * height, BITMAP_SIZEOF_PIXEL, "mipmaps");
if (!cur) ErrorHandler_Fail("Allocating memory for mipmaps");
GfxCommon_GenMipmaps(width, height, cur, prev); GfxCommon_GenMipmaps(width, height, cur, prev);
if (partial) { if (partial) {

View File

@ -407,8 +407,7 @@ static void Classic_LevelInit(struct Stream* stream) {
mapVolume = Stream_ReadU32_BE(stream); mapVolume = Stream_ReadU32_BE(stream);
gzHeader.Done = true; gzHeader.Done = true;
mapSizeIndex = sizeof(UInt32); mapSizeIndex = sizeof(UInt32);
map = Platform_MemAlloc(mapVolume, sizeof(BlockID)); map = Platform_MemAlloc(mapVolume, sizeof(BlockID), "map blocks");
if (!map) ErrorHandler_Fail("Failed to allocate memory for map");
} }
} }
@ -437,8 +436,7 @@ static void Classic_LevelDataChunk(struct Stream* stream) {
if (mapSizeIndex == sizeof(UInt32)) { if (mapSizeIndex == sizeof(UInt32)) {
if (!map) { if (!map) {
mapVolume = Stream_GetU32_BE(mapSize); mapVolume = Stream_GetU32_BE(mapSize);
map = Platform_MemAlloc(mapVolume, sizeof(BlockID)); map = Platform_MemAlloc(mapVolume, sizeof(BlockID), "map blocks");
if (!map) ErrorHandler_Fail("Failed to allocate memory for map");
} }
UInt8* src = map + mapIndex; UInt8* src = map + mapIndex;

View File

@ -169,11 +169,7 @@ Int32 Searcher_FindReachableBlocks(struct Entity* entity, struct AABB* entityBB,
Platform_MemFree(&Searcher_States); Platform_MemFree(&Searcher_States);
} }
Searcher_StatesCount = elements; Searcher_StatesCount = elements;
Searcher_States = Platform_MemAlloc(elements, sizeof(struct SearcherState), "collision search states");
Searcher_States = Platform_MemAlloc(elements, sizeof(struct SearcherState));
if (!Searcher_States) {
ErrorHandler_Fail("Failed to allocate memory for Searcher_FindReachableBlocks");
}
} }
/* Order loops so that we minimise cache misses */ /* Order loops so that we minimise cache misses */

View File

@ -33,9 +33,9 @@ void Platform_Free(void);
void Platform_Exit(ReturnCode code); void Platform_Exit(ReturnCode code);
STRING_PURE String Platform_GetCommandLineArgs(void); STRING_PURE String Platform_GetCommandLineArgs(void);
void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize); void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize, const UChar* place);
void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize); void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize, const UChar* place);
void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize); void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize, const UChar* place);
void Platform_MemFree(void** mem); void Platform_MemFree(void** mem);
void Platform_MemSet(void* dst, UInt8 value, UInt32 numBytes); void Platform_MemSet(void* dst, UInt8 value, UInt32 numBytes);
void Platform_MemCpy(void* dst, void* src, UInt32 numBytes); void Platform_MemCpy(void* dst, void* src, UInt32 numBytes);

View File

@ -11,11 +11,25 @@
#include "Game.h" #include "Game.h"
#include "Funcs.h" #include "Funcs.h"
#include "AsyncDownloader.h" #include "AsyncDownloader.h"
#include "Audio.h"
int main(void) { int main(void) {
ErrorHandler_Init("client.log"); ErrorHandler_Init("client.log");
Platform_Init(); Platform_Init();
void* file;
String oggPath = String_FromConst("audio/calm1.ogg");
Platform_FileOpen(&file, &oggPath);
struct Stream oggBase;
Stream_FromFile(&oggBase, file, &oggPath);
struct Stream ogg;
UInt8 oggBuffer[OGG_BUFFER_SIZE];
Ogg_MakeStream(&ogg, oggBuffer, &oggBase);
struct VorbisState state;
Vorbis_Init(&state, &ogg);
Vorbis_DecodeHeaders(&state);
/*Platform_HttpInit(); /*Platform_HttpInit();
AsyncRequest req = { 0 }; AsyncRequest req = { 0 };
String url = String_FromEmptyArray(req.URL); String url = String_FromEmptyArray(req.URL);

View File

@ -721,12 +721,10 @@ void StringsBuffer_Resize(void** buffer, UInt32* elems, UInt32 elemSize, UInt32
UInt32 curElems = *elems; UInt32 curElems = *elems;
if (curElems <= defElems) { if (curElems <= defElems) {
dst = Platform_MemAlloc(curElems + expandElems, elemSize); dst = Platform_MemAlloc(curElems + expandElems, elemSize, "StringsBuffer");
if (!dst) ErrorHandler_Fail("Failed allocating memory for StringsBuffer");
Platform_MemCpy(dst, cur, curElems * elemSize); Platform_MemCpy(dst, cur, curElems * elemSize);
} else { } else {
dst = Platform_MemRealloc(cur, curElems + expandElems, elemSize); dst = Platform_MemRealloc(cur, curElems + expandElems, elemSize, "resizing StringsBuffer");
if (!dst) ErrorHandler_Fail("Failed allocating memory for resizing StringsBuffer");
} }
*buffer = dst; *buffer = dst;

View File

@ -102,19 +102,32 @@ STRING_PURE String Platform_GetCommandLineArgs(void) {
return args; return args;
} }
void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize) { static void Platform_AllocFailed(const UChar* place) {
UInt32 numBytes = numElems * elemsSize; /* TODO: avoid overflow here */ UChar logBuffer[String_BufferSize(STRING_SIZE + 20)];
return HeapAlloc(heap, 0, numBytes); String log = String_InitAndClearArray(logBuffer);
String_Format1(&log, "Failed allocating memory for: %c", place);
ErrorHandler_Fail(log.buffer);
} }
void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize) { void* Platform_MemAlloc(UInt32 numElems, UInt32 elemsSize, const UChar* place) {
UInt32 numBytes = numElems * elemsSize; /* TODO: avoid overflow here */ UInt32 numBytes = numElems * elemsSize; /* TODO: avoid overflow here */
return HeapAlloc(heap, HEAP_ZERO_MEMORY, numBytes); void* ptr = HeapAlloc(heap, 0, numBytes);
if (!ptr) Platform_AllocFailed(place);
return ptr;
} }
void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize) { void* Platform_MemAllocCleared(UInt32 numElems, UInt32 elemsSize, const UChar* place) {
UInt32 numBytes = numElems * elemsSize; /* TODO: avoid overflow here */ UInt32 numBytes = numElems * elemsSize; /* TODO: avoid overflow here */
return HeapReAlloc(heap, 0, mem, numBytes); void* ptr = HeapAlloc(heap, HEAP_ZERO_MEMORY, numBytes);
if (!ptr) Platform_AllocFailed(place);
return ptr;
}
void* Platform_MemRealloc(void* mem, UInt32 numElems, UInt32 elemsSize, const UChar* place) {
UInt32 numBytes = numElems * elemsSize; /* TODO: avoid overflow here */
void* ptr = HeapReAlloc(heap, 0, mem, numBytes);
if (!ptr) Platform_AllocFailed(place);
return ptr;
} }
void Platform_MemFree(void** mem) { void Platform_MemFree(void** mem) {
@ -552,8 +565,7 @@ ReturnCode Platform_HttpGetRequestHeaders(struct AsyncRequest* request, void* ha
ReturnCode Platform_HttpGetRequestData(struct AsyncRequest* request, void* handle, void** data, UInt32 size, volatile Int32* progress) { ReturnCode Platform_HttpGetRequestData(struct AsyncRequest* request, void* handle, void** data, UInt32 size, volatile Int32* progress) {
if (size == 0) return ERROR_NOT_SUPPORTED; if (size == 0) return ERROR_NOT_SUPPORTED;
*data = Platform_MemAlloc(size, 1); *data = Platform_MemAlloc(size, sizeof(UInt8), "http get data");
if (*data == NULL) ErrorHandler_Fail("Failed to allocate memory for http get data");
*progress = 0; *progress = 0;
UInt8* buffer = *data; UInt8* buffer = *data;