Added frame buffer support.

This commit is contained in:
2021-05-05 07:37:25 -07:00
parent 469750b0a0
commit c3244274ce
10 changed files with 168 additions and 44 deletions

68
src/display/framebuffer.c Normal file
View File

@ -0,0 +1,68 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "framebuffer.h"
framebuffer_t * frameBufferCreate(int32_t w, int32_t h) {
framebuffer_t *fb = malloc(sizeof(framebuffer_t));
if(fb == NULL) return NULL;
// Create Color Attachment texture.
fb->texture = textureCreate(w, h, NULL);
if(fb->texture == NULL) {
free(fb);
return 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
);
// Render Buffer
glGenRenderbuffers(1, &fb->rboId);
glBindRenderbuffer(GL_RENDERBUFFER, fb->rboId);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, w, h);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
GL_RENDERBUFFER, fb->rboId
);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
uint32_t error;
error = 0;
}
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);
}
if(clear) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
}
void frameBufferDispose(framebuffer_t *frameBuffer) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glDeleteRenderbuffers(1, &frameBuffer->rboId);
glDeleteFramebuffers(1, &frameBuffer->fboId);
textureDispose(frameBuffer->texture);
free(frameBuffer);
}

34
src/display/framebuffer.h Normal file
View File

@ -0,0 +1,34 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "texture.h"
/**
* Creates a new frame buffer that can be rendered to.
*
* @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);
/**
* Use a given frame buffer as the current rendering context.
*
* @param frameBuffer Frame buffer to use, or NULL to not use any.
* @param clear Whether or not to clear the frame buffer prior to rendering.
*/
void frameBufferUse(framebuffer_t *frameBuffer, bool clear);
/**
* Dispose/cleanup a previously created frame buffer.
*
* @param frameBuffer Frame Buffer to clean.
*/
void frameBufferDispose(framebuffer_t *frameBuffer);

View File

@ -11,7 +11,28 @@
#define FONT_CHAR_START 33
/**
* Get the division for a given character.
*
* @param tileset Tileset to get the division from.
* @param character Character to get the division for.
* @return The division from the tileset for the character.
*/
tilesetdiv_t * fontGetCharacterDivision(tileset_t *tileset, char character);
/**
* Renders a set of font characters to the sprite. Coordinates are anchored to
* the top left (0,0) origin.
*
* @param batch Sprite Batch to render to.
* @param tileset Tileset for the font.
* @param string String to render.
* @param x Position in X space.
* @param y Position in Y space.
* @param z Position in Z space.
* @param charWidth Width of each character.
* @param charHeight Height of each character.
*/
void fontSpriteBatchBuffer(spritebatch_t *batch, tileset_t *tileset,
char *string, float x, float y, float z, float charWidth, float charHeight
);

View File

@ -32,5 +32,4 @@ void renderDispose() {
void renderSetResolution(int32_t width, int32_t height) {
RENDER_STATE.width = width;
RENDER_STATE.height = height;
glViewport(0, 0, width, height);
}

View File

@ -41,6 +41,8 @@ texture_t * textureCreate(int32_t width, int32_t height, pixel_t *pixels) {
GL_RGBA, GL_UNSIGNED_BYTE, pixels
);
}
glBindTexture(GL_TEXTURE_2D, 0);
return texture;
}