
My game's map consists of Chunks (16x16x16 blocks)
I only keep the closest 9x9x9 chunks, to the player, in memory.
This means that the draw distance is not far at all.
(9-1/2)= 4 chunks in each direction * 16 blocks = Only 64 blocks in each direction, not even Near enough.
I have been watching the task manager as my program loads or creates chunks, and each chunk takes up 2MB of ram!
Meaning with just this really low draw distance, I am using 2*9*9*9 = 1.458 GB !
If I do increase it to 11*11*11 the game crashes. (memory allocation fault) (11)^3*2 = 2.662 GB
For a game of this type that is Way too much right?
I need the Draw distance to be able to be Alot more. Especially considering my compition is drawing atleast 9 in each direction compared to my 4.
First of all:
Is this too much memory per chunk?
Next:
How should I optimize this?
I am allocating quite alot of memory, it may be my functions and ways things work are not good and wasting memory.
Part of the Chunk.h (chunk class)
#define CHUNK_BLOCKS 5832
struct Block
{
float x,y,z;
float tx,ty;
int drawPos;
};
struct Block2
{
bool exists;
int tx,ty;
float amount;
};
struct fluid
{
int x,y,z;
};
Block2 space[16][16][16];
/* -Old Code
Block grid[CHUNK_BLOCKS];
*/
fluid gridActive[CHUNK_BLOCKS];
fluid fluidListNew[CHUNK_BLOCKS];
float drawVerts[24576][5];
float buffer[16*16*16][4][5];
float bufferLiquid[16*16*16][4][5];
float bufferTranslucent[16*16*16][4][5];
I know you are wondering, "What is all this for?"
I works as so, Each block is stored in the space array. Which has their amount (which applies if thier a fluid), texture x and y, and weither or not they exist.
When created or changed the chunk "buffers" the faces to an array (drawVerts, buffer, bufferLiquid, bufferTranslucent) based on what the block type is.
Water and glass would be Liquid, while dirt just buffer (this is used later to save time from sorting for OpenGL).
The gridActive and fluidListNew are arrays that hold the positions of blocks that are moving. If I set a fire it would have that 1 block and thats it. If the fire spreads to 4 blocks
the array will then have 4 blocks. The gridActive is copied to fluidListNew before checking what the blocks are doing in chunk::update(). So that if a block needs to move again
it can just add itself to the gridAcrive, to prevent being updated twice in 1 frame.
This is just a very basic rundown of biggest memory portions of chunks and functions that use them. The functions of course are far more complex and took alot of time to make. I hate to
delete code, but to make this smaller and more efficent I feel this may be a must.
Opinions would greatly be appreciated.
Thanks