1
0
Fork 0

Remove dynamically allocated memory

This commit is contained in:
Austin Morlan 2018-09-18 18:19:13 -07:00
parent c3c154e4ff
commit d26e4cb5dc
Signed by: austin
GPG Key ID: FD6B27654AF5E348
7 changed files with 21 additions and 35 deletions

View File

@ -3,6 +3,12 @@
#include <cstdint>
// CONSTANTS
#define WINDOW_WIDTH (1920)
#define WINDOW_HEIGHT (1080)
#define WINDOW_FPS (30)
// ENUMS
enum Engine_Input
{
@ -27,7 +33,7 @@ enum Engine_Input
struct Engine_Buffer
{
uint32_t *buffer;
float *zbuffer;
float zbuffer[WINDOW_HEIGHT][WINDOW_WIDTH];
int width;
int height;
};

View File

@ -6,14 +6,17 @@
#include <cstdint>
// CONSTANTS
#define VERTEX_LIMIT (20000)
#define FACE_LIMIT (30000)
#define MATERIAL_LIMIT (10)
#define TEXTURE_SIZE_LIMIT (1024)
// STRUCTURES
struct Texture
{
ColorU32 **texels;
ColorU32 texels[TEXTURE_SIZE_LIMIT][TEXTURE_SIZE_LIMIT];
unsigned int width;
unsigned int height;
};

View File

@ -41,8 +41,7 @@ struct LightDiffuse
struct LightList
{
LightAmbient ambient;
LightDiffuse *diffuse;
int diffuseCount;
LightDiffuse diffuse;
};

View File

@ -59,11 +59,9 @@ int Engine_Init(Engine_Buffer &buffer, char *objFilename, char *mtlFilename)
camera.SetFOV(90.0f, buffer.width, buffer.height);
lights.diffuse = (LightDiffuse*)malloc(sizeof(LightDiffuse));
lights.diffuseCount = 1;
lights.ambient.intensity = 1.0f;
lights.diffuse[0].intensity = 1.0f;
lights.diffuse[0].direction = Vector(1.0f, 1.0f, 1.0f);
lights.diffuse.intensity = 1.0f;
lights.diffuse.direction = Vector(1.0f, 1.0f, 1.0f);
return 0;
}
@ -119,15 +117,9 @@ void Engine_Render(Engine_Buffer &buffer, uint32_t input)
{
Vertex &vert = transVerts.data[face.vertIndex[i]];
ColorF32 totalColor = lights.ambient.ComputeColor(material.ambient);
for (int c = 0; c < lights.diffuseCount; ++c)
{
totalColor += lights.diffuse[c].ComputeColor(
material.diffuse, vert.normal);
}
vert.color = totalColor;
vert.color =
lights.ambient.ComputeColor(material.ambient)
+ lights.diffuse.ComputeColor(material.diffuse, vert.normal);
}
}

View File

@ -206,12 +206,11 @@ void RenderMesh(
// Draw the pixel if it's closer than what's in the z-buffer
int pixel = (y * buffer.width + x);
if (zInv > buffer.zbuffer[pixel])
if (zInv > buffer.zbuffer[y][x])
{
DrawPixel(buffer.buffer, buffer.width, color.u32, x, y);
buffer.zbuffer[pixel] = zInv;
buffer.zbuffer[y][x] = zInv;
}
}
}

View File

@ -208,15 +208,8 @@ static int LoadTexture(char *filename, Texture &texture)
fread((void*)&header, sizeof(BMP_Header), 1, fp);
fseek(fp, header.bitmapOffset, SEEK_SET);
texture.texels = (ColorU32**)malloc((size_t)header.height * sizeof(ColorU32*));
for (int row = 0; row < header.height; ++row)
{
texture.texels[row] = (ColorU32*)calloc(1, (size_t)header.width * sizeof(ColorU32));
}
// Padding is added to image to align to 4-byte boundaries
size_t paddingSize = header.width % 4;
uint8_t *padding = (uint8_t*)malloc(paddingSize * sizeof(*padding));
for (int y = 0; y < header.height; ++y)
{
@ -230,7 +223,8 @@ static int LoadTexture(char *filename, Texture &texture)
// Discard padding byte
if (paddingSize != 0)
{
fread(padding, paddingSize, 1, fp);
uint32_t padding;
fread(&padding, paddingSize, 1, fp);
}
}

View File

@ -5,12 +5,6 @@
#include <cstdlib>
// CONSTANTS
const unsigned int WINDOW_WIDTH = 1920;
const unsigned int WINDOW_HEIGHT = 1080;
const unsigned int WINDOW_FPS = 30;
// MAIN
int main(int argc, char *argv[])
{
@ -31,7 +25,6 @@ int main(int argc, char *argv[])
{
Engine_Buffer buffer = {};
buffer.buffer = (uint32_t*)platform.surface->pixels;
buffer.zbuffer = (float*)calloc((size_t)(platform.surface->w * platform.surface->h), sizeof(float));
buffer.width = platform.surface->w;
buffer.height = platform.surface->h;