Remove dynamically allocated memory
This commit is contained in:
parent
c3c154e4ff
commit
d26e4cb5dc
|
@ -3,6 +3,12 @@
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
|
|
||||||
|
// CONSTANTS
|
||||||
|
#define WINDOW_WIDTH (1920)
|
||||||
|
#define WINDOW_HEIGHT (1080)
|
||||||
|
#define WINDOW_FPS (30)
|
||||||
|
|
||||||
|
|
||||||
// ENUMS
|
// ENUMS
|
||||||
enum Engine_Input
|
enum Engine_Input
|
||||||
{
|
{
|
||||||
|
@ -27,7 +33,7 @@ enum Engine_Input
|
||||||
struct Engine_Buffer
|
struct Engine_Buffer
|
||||||
{
|
{
|
||||||
uint32_t *buffer;
|
uint32_t *buffer;
|
||||||
float *zbuffer;
|
float zbuffer[WINDOW_HEIGHT][WINDOW_WIDTH];
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,14 +6,17 @@
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
|
|
||||||
|
// CONSTANTS
|
||||||
#define VERTEX_LIMIT (20000)
|
#define VERTEX_LIMIT (20000)
|
||||||
#define FACE_LIMIT (30000)
|
#define FACE_LIMIT (30000)
|
||||||
#define MATERIAL_LIMIT (10)
|
#define MATERIAL_LIMIT (10)
|
||||||
|
#define TEXTURE_SIZE_LIMIT (1024)
|
||||||
|
|
||||||
|
|
||||||
|
// STRUCTURES
|
||||||
struct Texture
|
struct Texture
|
||||||
{
|
{
|
||||||
ColorU32 **texels;
|
ColorU32 texels[TEXTURE_SIZE_LIMIT][TEXTURE_SIZE_LIMIT];
|
||||||
unsigned int width;
|
unsigned int width;
|
||||||
unsigned int height;
|
unsigned int height;
|
||||||
};
|
};
|
||||||
|
|
|
@ -41,8 +41,7 @@ struct LightDiffuse
|
||||||
struct LightList
|
struct LightList
|
||||||
{
|
{
|
||||||
LightAmbient ambient;
|
LightAmbient ambient;
|
||||||
LightDiffuse *diffuse;
|
LightDiffuse diffuse;
|
||||||
int diffuseCount;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -59,11 +59,9 @@ int Engine_Init(Engine_Buffer &buffer, char *objFilename, char *mtlFilename)
|
||||||
|
|
||||||
camera.SetFOV(90.0f, buffer.width, buffer.height);
|
camera.SetFOV(90.0f, buffer.width, buffer.height);
|
||||||
|
|
||||||
lights.diffuse = (LightDiffuse*)malloc(sizeof(LightDiffuse));
|
|
||||||
lights.diffuseCount = 1;
|
|
||||||
lights.ambient.intensity = 1.0f;
|
lights.ambient.intensity = 1.0f;
|
||||||
lights.diffuse[0].intensity = 1.0f;
|
lights.diffuse.intensity = 1.0f;
|
||||||
lights.diffuse[0].direction = Vector(1.0f, 1.0f, 1.0f);
|
lights.diffuse.direction = Vector(1.0f, 1.0f, 1.0f);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -119,15 +117,9 @@ void Engine_Render(Engine_Buffer &buffer, uint32_t input)
|
||||||
{
|
{
|
||||||
Vertex &vert = transVerts.data[face.vertIndex[i]];
|
Vertex &vert = transVerts.data[face.vertIndex[i]];
|
||||||
|
|
||||||
ColorF32 totalColor = lights.ambient.ComputeColor(material.ambient);
|
vert.color =
|
||||||
|
lights.ambient.ComputeColor(material.ambient)
|
||||||
for (int c = 0; c < lights.diffuseCount; ++c)
|
+ lights.diffuse.ComputeColor(material.diffuse, vert.normal);
|
||||||
{
|
|
||||||
totalColor += lights.diffuse[c].ComputeColor(
|
|
||||||
material.diffuse, vert.normal);
|
|
||||||
}
|
|
||||||
|
|
||||||
vert.color = totalColor;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -206,12 +206,11 @@ void RenderMesh(
|
||||||
|
|
||||||
|
|
||||||
// Draw the pixel if it's closer than what's in the z-buffer
|
// Draw the pixel if it's closer than what's in the z-buffer
|
||||||
int pixel = (y * buffer.width + x);
|
if (zInv > buffer.zbuffer[y][x])
|
||||||
if (zInv > buffer.zbuffer[pixel])
|
|
||||||
{
|
{
|
||||||
DrawPixel(buffer.buffer, buffer.width, color.u32, x, y);
|
DrawPixel(buffer.buffer, buffer.width, color.u32, x, y);
|
||||||
|
|
||||||
buffer.zbuffer[pixel] = zInv;
|
buffer.zbuffer[y][x] = zInv;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -208,15 +208,8 @@ static int LoadTexture(char *filename, Texture &texture)
|
||||||
fread((void*)&header, sizeof(BMP_Header), 1, fp);
|
fread((void*)&header, sizeof(BMP_Header), 1, fp);
|
||||||
fseek(fp, header.bitmapOffset, SEEK_SET);
|
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
|
// Padding is added to image to align to 4-byte boundaries
|
||||||
size_t paddingSize = header.width % 4;
|
size_t paddingSize = header.width % 4;
|
||||||
uint8_t *padding = (uint8_t*)malloc(paddingSize * sizeof(*padding));
|
|
||||||
|
|
||||||
for (int y = 0; y < header.height; ++y)
|
for (int y = 0; y < header.height; ++y)
|
||||||
{
|
{
|
||||||
|
@ -230,7 +223,8 @@ static int LoadTexture(char *filename, Texture &texture)
|
||||||
// Discard padding byte
|
// Discard padding byte
|
||||||
if (paddingSize != 0)
|
if (paddingSize != 0)
|
||||||
{
|
{
|
||||||
fread(padding, paddingSize, 1, fp);
|
uint32_t padding;
|
||||||
|
fread(&padding, paddingSize, 1, fp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,12 +5,6 @@
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
|
|
||||||
// CONSTANTS
|
|
||||||
const unsigned int WINDOW_WIDTH = 1920;
|
|
||||||
const unsigned int WINDOW_HEIGHT = 1080;
|
|
||||||
const unsigned int WINDOW_FPS = 30;
|
|
||||||
|
|
||||||
|
|
||||||
// MAIN
|
// MAIN
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
@ -31,7 +25,6 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
Engine_Buffer buffer = {};
|
Engine_Buffer buffer = {};
|
||||||
buffer.buffer = (uint32_t*)platform.surface->pixels;
|
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.width = platform.surface->w;
|
||||||
buffer.height = platform.surface->h;
|
buffer.height = platform.surface->h;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue