Refactoring structs Part 1

This commit is contained in:
2021-05-20 22:20:52 -07:00
parent c19f7c1083
commit 5ae1f1c0d4
56 changed files with 484 additions and 422 deletions

View File

@ -7,26 +7,19 @@
#include "framebuffer.h"
framebuffer_t * frameBufferCreate(int32_t w, int32_t h) {
framebuffer_t *fb = malloc(sizeof(framebuffer_t));
if(fb == NULL) return NULL;
void frameBufferInit(framebuffer_t *fb, int32_t w, int32_t h) {
// At least one pixel
if(w <= 0) w = 1;
if(h <= 0) h = 1;
// Create Color Attachment texture.
fb->texture = textureCreate(w, h, NULL);
if(fb->texture == NULL) {
free(fb);
return NULL;
}
textureInit(&fb->texture, w, h, NULL);
// Create Frame Buffer
glGenFramebuffers(1, &fb->fboId);
glBindFramebuffer(GL_FRAMEBUFFER, fb->fboId);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, fb->texture->id, 0
GL_TEXTURE_2D, fb->texture.id, 0
);
// Render Buffer
@ -44,18 +37,20 @@ framebuffer_t * frameBufferCreate(int32_t w, int32_t h) {
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return fb;
}
void frameBufferUse(framebuffer_t *frameBuffer, bool clear) {
if(frameBuffer == NULL) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, RENDER_STATE.width, RENDER_STATE.height);
} else {
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer->fboId);
glViewport(0, 0, frameBuffer->texture->width, frameBuffer->texture->height);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer->fboId);
glViewport(0, 0, frameBuffer->texture.width, frameBuffer->texture.height);
if(clear) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
}
void frameBufferUnbind(render_t *render, bool clear) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, render->width, render->height);
if(clear) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -67,6 +62,6 @@ void frameBufferDispose(framebuffer_t *frameBuffer) {
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glDeleteRenderbuffers(1, &frameBuffer->rboId);
glDeleteFramebuffers(1, &frameBuffer->fboId);
textureDispose(frameBuffer->texture);
textureDispose(&frameBuffer->texture);
free(frameBuffer);
}

View File

@ -10,22 +10,30 @@
#include "texture.h"
/**
* Creates a new frame buffer that can be rendered to.
*
* Initializes frame buffer that can be rendered to.
* @param frameBuffer Frame buffer to initialize.
* @param width Width of the frame buffer (in pixels).
* @param height Height of the frame buffer (in pixels).
* @return A renderable frame buffer.
*/
framebuffer_t * frameBufferCreate(int32_t width, int32_t height);
void frameBufferInit(framebuffer_t *buffer, int32_t width, int32_t height);
/**
* Use a given frame buffer as the current rendering context.
*
* @param frameBuffer Frame buffer to use, or NULL to not use any.
* @param frameBuffer Frame buffer to use.
* @param clear Whether or not to clear the frame buffer prior to rendering.
*/
void frameBufferUse(framebuffer_t *frameBuffer, bool clear);
/**
* Unbind the currently bound frame buffer.
*
* @param render Render manager
* @param clear Whether or not to clear the back buffer.
*/
void frameBufferUnbind(render_t *render, bool clear);
/**
* Dispose/cleanup a previously created frame buffer.
*

View File

@ -7,9 +7,7 @@
#include "primitive.h"
primitive_t * primitiveCreate(int32_t verticeCount, int32_t indiceCount) {
primitive_t *primitive = malloc(sizeof(primitive_t));
void primitiveInit(primitive_t *primitive, int32_t verticeCount, int32_t indiceCount) {
primitive->verticeCount = verticeCount;
primitive->indiceCount = indiceCount;
@ -44,8 +42,6 @@ primitive_t * primitiveCreate(int32_t verticeCount, int32_t indiceCount) {
GL_FALSE, 0, (void *)offset
);
glEnableVertexAttribArray(1);
return primitive;
}
void primitiveBufferVertices(primitive_t *primitive,
@ -131,5 +127,4 @@ void primitiveDispose(primitive_t *primitive) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &primitive->vertexBuffer);
glDeleteBuffers(1, &primitive->indexBuffer);
free(primitive);
}

View File

@ -10,11 +10,11 @@
/**
* Creates a new primitive.
* @param primitive Primitive to initialize.
* @param verticeCount How many vertices can the primitive hold.
* @param indiceCount How many indices can the primitive hold.
* @return The newly created primitive ready to be buffered to.
*/
primitive_t * primitiveCreate(int32_t verticeCount, int32_t indiceCount);
void primitiveInit(primitive_t *primitive, int32_t verticeCount, int32_t indiceCount);
/**
* Buffer Vertices to a primitive for use in rendering.
@ -47,7 +47,7 @@ void primitiveBufferIndices(primitive_t *primitive,
void primitiveDraw(primitive_t *primitive, int32_t start, int32_t count);
/**
* Cleanup a previously created primitive.
* Cleanup a previously initialized primitive.
* @param primitive Primitive to cleanup.
*/
void primitiveDispose(primitive_t *primitive);

View File

@ -7,8 +7,6 @@
#include "render.h"
render_t RENDER_STATE;
void renderInit() {
// Enable GL things.
glEnable(GL_BLEND);
@ -29,7 +27,7 @@ void renderFrameStart() {
void renderDispose() {
}
void renderSetResolution(int32_t width, int32_t height) {
RENDER_STATE.width = width;
RENDER_STATE.height = height;
void renderSetResolution(render_t *render, int32_t width, int32_t height) {
render->width = width;
render->height = height;
}

View File

@ -26,7 +26,8 @@ void renderDispose();
/**
* Sets the internal display resolution.
*
* @param render Render context to resize.
* @param width Width of the display (in pixels).
* @param height Height of the display (in pixels).
*/
void renderSetResolution(int32_t width, int32_t height);
void renderSetResolution(render_t *render, int32_t width, int32_t height);

View File

@ -7,22 +7,13 @@
#include "spritebatch.h"
spritebatch_t * spriteBatchCreate(int32_t maxSprites) {
spritebatch_t *batch = malloc(sizeof(spritebatch_t));
if(batch == NULL) return NULL;
void spriteBatchInit(spritebatch_t *batch, int32_t maxSprites) {
batch->maxSprites = maxSprites;
batch->currentSprite = 0;
batch->primitive = primitiveCreate(
primitiveInit(&batch->primitive,
maxSprites*QUAD_VERTICE_COUNT, maxSprites*QUAD_INDICE_COUNT
);
if(batch == NULL) {
free(batch);
return NULL;
}
return batch;
}
void spriteBatchQuad(spritebatch_t *spriteBatch, int32_t index,
@ -35,7 +26,7 @@ void spriteBatchQuad(spritebatch_t *spriteBatch, int32_t index,
spriteBatch->currentSprite = mathMax(index, spriteBatch->currentSprite);
}
quadBuffer(spriteBatch->primitive, z,
quadBuffer(&spriteBatch->primitive, z,
x, y, u0, v0,
x+width, y+height, u1, v1,
index*QUAD_VERTICE_COUNT, index*QUAD_INDICE_COUNT
@ -48,12 +39,11 @@ void spriteBatchFlush(spritebatch_t *spriteBatch) {
void spriteBatchDraw(spritebatch_t *spriteBatch, int32_t index, int32_t count) {
if(count == -1) count = spriteBatch->currentSprite;
primitiveDraw(spriteBatch->primitive,
primitiveDraw(&spriteBatch->primitive,
index*QUAD_INDICE_COUNT, count*QUAD_INDICE_COUNT
);
}
void spriteBatchDispose(spritebatch_t *spriteBatch) {
primitiveDispose(spriteBatch->primitive);
free(spriteBatch);
primitiveDispose(&spriteBatch->primitive);
}

View File

@ -11,10 +11,10 @@
/**
* Creates a new Sprite Batch made of standard quads.
*
* @param batch Sprite batch to init.
* @param maxSprites The maxiumum number of sprites the batch can hold.
* @returns A new quad Sprite Batch.
*/
spritebatch_t * spriteBatchCreate(int32_t maxSprites);
void spriteBatchInit(spritebatch_t *batch, int32_t maxSprites);
/**
* Renders a sprite onto a given Sprite Batch.

View File

@ -7,10 +7,9 @@
#include "texture.h"
texture_t * textureCreate(int32_t width, int32_t height, pixel_t *pixels) {
texture_t *texture = malloc(sizeof(texture_t));
if(texture == NULL) return NULL;
void textureInit(texture_t *texture, int32_t width, int32_t height,
pixel_t *pixels
) {
texture->width = width;
texture->height = height;
@ -27,13 +26,12 @@ texture_t * textureCreate(int32_t width, int32_t height, pixel_t *pixels) {
// Start by buffering all transparent black pixels.
if(pixels == NULL) {
// TODO: I can optimize this, I think the GPU can default this somehow
pixels = calloc(width * height, sizeof(pixel_t));
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, pixels
);
free(pixels);
} else {
glTexImage2D(
@ -59,5 +57,4 @@ void textureBufferPixels(texture_t *texture,
void textureDispose(texture_t *texture) {
glDeleteTextures(1, &texture->id);
free(texture);
}

View File

@ -7,14 +7,16 @@
#include <dawn/dawn.h>
/**
* Creates a new texture that can be written in to.
* Initializes a texture that can be written in to.
*
* @param texture Texture to initialize.
* @param width Width of the texture (in pixels).
* @param height Height of the texture (in pixels).
* @param pixels Default pixel array, set to NULL to set all pixel data to 0.
* @return The newly created texture instance.
*/
texture_t * textureCreate(int32_t width, int32_t height, pixel_t *pixels);
void textureInit(texture_t *texture, int32_t width, int32_t height,
pixel_t *pixels
);
/**
* Buffer pixel data onto the GPU. Pixel buffering is rather costly so avoid
@ -33,6 +35,7 @@ void textureBufferPixels(texture_t *texture,
/**
* Clean a previously created texture.
*
* @param texture Texture to clean up.
*/
void textureDispose(texture_t *texture);

View File

@ -7,19 +7,15 @@
#include "tileset.h"
tileset_t * tilesetCreate(
tileset_t * tilesetInit(tileset_t *tileset,
int32_t columns, int32_t rows,
int32_t width, int32_t height,
int32_t gapX, int32_t gapY,
int32_t borderX, int32_t borderY
) {
tileset_t *tileset;
float tdivX, tdivY;
int32_t x, y, i;
tileset = malloc(sizeof(tileset_t));
if(tileset == NULL) return NULL;
tileset->count = rows * columns;
tileset->divisions = malloc(sizeof(tilesetdiv_t) * tileset->count);
if(tileset->divisions == NULL) {
@ -51,21 +47,13 @@ tileset_t * tilesetCreate(
borderY + (tileset->divY * y) + (gapY * y)
) / height;
tileset->divisions[i].y1 = tileset->divisions[i].y0 + tdivY;
// Vertically flipped if necessary
// tileset->divisions[i].y1 = (
// borderY + (tileset->divY * y) + (gapY * y)
// ) / height;
// tileset->divisions[i].y0 = tileset->divisions[i].y1 + tdivY;
}
}
return tileset;
}
tilesetdiv_t tilesetGetDivision(tileset_t *tileset,
int32_t column, int32_t row
) {
tilesetdiv_t tilesetGetDivision(tileset_t *tileset,int32_t column,int32_t row) {
return tileset->divisions[
(column % tileset->columns) + (row * tileset->columns)
];
@ -73,5 +61,4 @@ tilesetdiv_t tilesetGetDivision(tileset_t *tileset,
void tilesetDispose(tileset_t *tileset) {
free(tileset->divisions);
free(tileset);
}

View File

@ -9,6 +9,7 @@
/**
* Create a tileset. Tilesets will be pre-divided to save performance later.
*
* @param tileset Tileset to init into.
* @param columns Count of columns.
* @param rows Count of rows.
* @param width Width of the tileset.
@ -17,9 +18,8 @@
* @param gapY Space between each row.
* @param borderX Space around the edge of the tileset.
* @param borderY Space around the edge of the tileset.
* @returns The tileset.
*/
tileset_t * tilesetCreate(
void tilesetInit(tileset_t *tileset,
int32_t columns, int32_t rows,
int32_t width, int32_t height,
int32_t gapX, int32_t gapY,
@ -34,9 +34,7 @@ tileset_t * tilesetCreate(
* @param row Y axis of the tileset.
* @returns The Tileset division.
*/
tilesetdiv_t tilesetGetDivision(tileset_t *tileset,
int32_t column, int32_t row
);
tilesetdiv_t tilesetGetDivision(tileset_t *tileset,int32_t column, int32_t row);
/**
* Cleans a previously created tileset