Add backbuffer
This commit is contained in:
141
src/display/framebuffer/framebuffer.c
Normal file
141
src/display/framebuffer/framebuffer.c
Normal file
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "framebuffer.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
framebuffer_t FRAMEBUFFER_BACKBUFFER = {0};
|
||||
const framebuffer_t *FRAMEBUFFER_BOUND = &FRAMEBUFFER_BACKBUFFER;
|
||||
|
||||
void frameBufferInitBackbuffer() {
|
||||
memoryZero(&FRAMEBUFFER_BACKBUFFER, sizeof(framebuffer_t));
|
||||
|
||||
FRAMEBUFFER_BACKBUFFER.id = -1;
|
||||
FRAMEBUFFER_BOUND = &FRAMEBUFFER_BACKBUFFER;
|
||||
}
|
||||
|
||||
int32_t frameBufferGetWidth(const framebuffer_t *framebuffer) {
|
||||
#if DUSK_DISPLAY_SDL2
|
||||
if(framebuffer == &FRAMEBUFFER_BACKBUFFER) {
|
||||
int32_t windowWidth, windowHeight;
|
||||
SDL_GetWindowSize(DISPLAY.window, &windowWidth, &windowHeight);
|
||||
return windowWidth;
|
||||
}
|
||||
|
||||
assertUnreachable("Framebuffer width not implemented");
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
int32_t frameBufferGetHeight(const framebuffer_t *framebuffer) {
|
||||
#if DUSK_DISPLAY_SDL2
|
||||
if(framebuffer == &FRAMEBUFFER_BACKBUFFER) {
|
||||
int32_t windowWidth, windowHeight;
|
||||
SDL_GetWindowSize(DISPLAY.window, &windowWidth, &windowHeight);
|
||||
return windowHeight;
|
||||
}
|
||||
|
||||
assertUnreachable("Framebuffer height not implemented");
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void frameBufferBind(const framebuffer_t *framebuffer) {
|
||||
if(framebuffer == NULL) {
|
||||
#if DUSK_DISPLAY_SDL2
|
||||
frameBufferBind(&FRAMEBUFFER_BACKBUFFER);
|
||||
#endif
|
||||
|
||||
FRAMEBUFFER_BOUND = &FRAMEBUFFER_BACKBUFFER;
|
||||
return;
|
||||
}
|
||||
|
||||
// Bind the framebuffer for rendering
|
||||
#if DUSK_DISPLAY_SDL2
|
||||
if(framebuffer == &FRAMEBUFFER_BACKBUFFER) {
|
||||
|
||||
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
||||
int32_t windowWidth, windowHeight;
|
||||
SDL_GetWindowSize(DISPLAY.window, &windowWidth, &windowHeight);
|
||||
} else {
|
||||
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer->id);
|
||||
}
|
||||
glViewport(
|
||||
0, 0,
|
||||
frameBufferGetWidth(framebuffer), frameBufferGetHeight(framebuffer)
|
||||
);
|
||||
#endif
|
||||
|
||||
FRAMEBUFFER_BOUND = framebuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disposes of the framebuffer using EXT methods.
|
||||
*
|
||||
* @param framebuffer The framebuffer to dispose of.
|
||||
*/
|
||||
void frameBufferDispose(framebuffer_t *framebuffer) {
|
||||
assertNotNull(framebuffer, "Framebuffer cannot be NULL");
|
||||
|
||||
#if DUSK_DISPLAY_SDL2
|
||||
if(framebuffer == &FRAMEBUFFER_BACKBUFFER) {
|
||||
assertUnreachable("Cannot dispose of backbuffer");
|
||||
}
|
||||
|
||||
glDeleteFramebuffersEXT(1, &framebuffer->id);
|
||||
#endif
|
||||
}
|
||||
|
||||
// #if RENDER_USE_FRAMEBUFFER
|
||||
// void frameBufferInit(
|
||||
// framebuffer_t *framebuffer,
|
||||
// const uint32_t width,
|
||||
// const uint32_t height
|
||||
// ) {
|
||||
// assertNotNull(framebuffer, "Framebuffer cannot be NULL");
|
||||
// assertTrue(width > 0 && height > 0, "Width & height must be greater than 0");
|
||||
|
||||
// memoryZero(framebuffer, sizeof(framebuffer_t));
|
||||
// textureInit(&framebuffer->texture, width, height, GL_RGBA, NULL);
|
||||
|
||||
// // Generate the framebuffer object using EXT
|
||||
// glGenFramebuffersEXT(1, &framebuffer->id);
|
||||
// glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer->id);
|
||||
|
||||
// // Attach the texture to the framebuffer
|
||||
// glFramebufferTexture2DEXT(
|
||||
// GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
|
||||
// GL_TEXTURE_2D, framebuffer->texture.id, 0
|
||||
// );
|
||||
|
||||
// // Check if the framebuffer is complete
|
||||
// if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT) {
|
||||
// assertUnreachable("Framebuffer is not complete");
|
||||
// }
|
||||
|
||||
// // Unbind the framebuffer
|
||||
// glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
||||
// }
|
||||
|
||||
// void frameBufferBind(const framebuffer_t *framebuffer) {
|
||||
// if(framebuffer == NULL) {
|
||||
// glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// // Bind the framebuffer for rendering
|
||||
// glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer->id);
|
||||
// }
|
||||
|
||||
// void frameBufferDispose(framebuffer_t *framebuffer) {
|
||||
// assertNotNull(framebuffer, "Framebuffer cannot be NULL");
|
||||
|
||||
// glDeleteFramebuffersEXT(1, &framebuffer->id);
|
||||
// textureDispose(&framebuffer->texture);
|
||||
// }
|
||||
// #endif
|
Reference in New Issue
Block a user