Framebuffer done.
This commit is contained in:
@@ -15,6 +15,7 @@ target_sources(${DUSK_TARGET_NAME}
|
|||||||
|
|
||||||
# Subdirs
|
# Subdirs
|
||||||
add_subdirectory(camera)
|
add_subdirectory(camera)
|
||||||
|
add_subdirectory(framebuffer)
|
||||||
add_subdirectory(mesh)
|
add_subdirectory(mesh)
|
||||||
add_subdirectory(texture)
|
add_subdirectory(texture)
|
||||||
add_subdirectory(spritebatch)
|
add_subdirectory(spritebatch)
|
@@ -11,6 +11,7 @@
|
|||||||
void cameraUIPush(void) {
|
void cameraUIPush(void) {
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
glViewport(0, 0, RENDER_WIDTH, RENDER_HEIGHT);
|
||||||
glOrtho(
|
glOrtho(
|
||||||
0.0f, (float_t)RENDER_WIDTH,
|
0.0f, (float_t)RENDER_WIDTH,
|
||||||
(float_t)RENDER_HEIGHT, 0.0f,
|
(float_t)RENDER_HEIGHT, 0.0f,
|
||||||
|
10
src/dusksdl2/display/framebuffer/CMakeLists.txt
Normal file
10
src/dusksdl2/display/framebuffer/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# Copyright (c) 2025 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Sources
|
||||||
|
target_sources(${DUSK_TARGET_NAME}
|
||||||
|
PRIVATE
|
||||||
|
framebuffer.c
|
||||||
|
)
|
57
src/dusksdl2/display/framebuffer/framebuffer.c
Normal file
57
src/dusksdl2/display/framebuffer/framebuffer.c
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
/**
|
||||||
|
* 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"
|
||||||
|
|
||||||
|
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, NULL);
|
||||||
|
|
||||||
|
// Generate the framebuffer object
|
||||||
|
glGenFramebuffers(1, &framebuffer->id);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer->id);
|
||||||
|
|
||||||
|
// Attach the texture to the framebuffer
|
||||||
|
glFramebufferTexture2D(
|
||||||
|
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
||||||
|
GL_TEXTURE_2D, framebuffer->texture.id, 0
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check if the framebuffer is complete
|
||||||
|
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||||
|
assertUnreachable("Framebuffer is not complete");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unbind the framebuffer
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void frameBufferBind(const framebuffer_t *framebuffer) {
|
||||||
|
if(framebuffer == NULL) {
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bind the framebuffer for rendering
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void frameBufferDispose(framebuffer_t *framebuffer) {
|
||||||
|
assertNotNull(framebuffer, "Framebuffer cannot be NULL");
|
||||||
|
|
||||||
|
glDeleteFramebuffers(1, &framebuffer->id);
|
||||||
|
textureDispose(&framebuffer->texture);
|
||||||
|
}
|
42
src/dusksdl2/display/framebuffer/framebuffer.h
Normal file
42
src/dusksdl2/display/framebuffer/framebuffer.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2025 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "display/texture/texture.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
GLuint id;
|
||||||
|
texture_t texture;
|
||||||
|
} framebuffer_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a framebuffer.
|
||||||
|
*
|
||||||
|
* @param framebuffer The framebuffer to initialize.
|
||||||
|
* @param width The width of the framebuffer.
|
||||||
|
* @param height The height of the framebuffer.
|
||||||
|
* @return An error code indicating success or failure.
|
||||||
|
*/
|
||||||
|
void frameBufferInit(
|
||||||
|
framebuffer_t *framebuffer,
|
||||||
|
const uint32_t width,
|
||||||
|
const uint32_t height
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Binds the framebuffer for rendering.
|
||||||
|
*
|
||||||
|
* @param framebuffer The framebuffer to bind, or NULL to unbind.
|
||||||
|
*/
|
||||||
|
void frameBufferBind(const framebuffer_t *framebuffer);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disposes of the framebuffer.
|
||||||
|
*
|
||||||
|
* @param framebuffer The framebuffer to dispose of.
|
||||||
|
*/
|
||||||
|
void frameBufferDispose(framebuffer_t *framebuffer);
|
@@ -74,8 +74,6 @@ errorret_t renderInit(void) {
|
|||||||
spriteBatchInit();
|
spriteBatchInit();
|
||||||
renderTextInit();
|
renderTextInit();
|
||||||
renderBackBufferInit();
|
renderBackBufferInit();
|
||||||
|
|
||||||
// Init other things
|
|
||||||
renderSceneInit();
|
renderSceneInit();
|
||||||
|
|
||||||
RENDER_RUNNING = true;
|
RENDER_RUNNING = true;
|
||||||
@@ -101,17 +99,14 @@ errorret_t renderDraw(void) {
|
|||||||
// Draw everything
|
// Draw everything
|
||||||
renderSceneDraw();
|
renderSceneDraw();
|
||||||
|
|
||||||
|
// UI
|
||||||
cameraUIPush();
|
cameraUIPush();
|
||||||
renderConsoleDraw();
|
renderConsoleDraw();
|
||||||
spriteBatchFlush();
|
spriteBatchFlush();
|
||||||
cameraUIPop();
|
cameraUIPop();
|
||||||
|
|
||||||
// Finish rendering
|
// Finish rendering, now render back buffer.
|
||||||
spriteBatchFlush();
|
|
||||||
renderBackBufferUnbind();
|
renderBackBufferUnbind();
|
||||||
|
|
||||||
// Draw the back buffer to the window
|
|
||||||
renderBackBufferDraw();
|
renderBackBufferDraw();
|
||||||
|
|
||||||
SDL_GL_SwapWindow(RENDER_WINDOW);
|
SDL_GL_SwapWindow(RENDER_WINDOW);
|
||||||
@@ -119,12 +114,11 @@ errorret_t renderDraw(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
errorret_t renderDispose(void) {
|
errorret_t renderDispose(void) {
|
||||||
|
renderSceneDispose();
|
||||||
|
renderBackBufferDispose();
|
||||||
|
renderTextDispose();
|
||||||
spriteBatchDispose();
|
spriteBatchDispose();
|
||||||
|
|
||||||
// renderTextDispose();
|
|
||||||
// renderSceneDispose();
|
|
||||||
// renderBackBufferDispose();
|
|
||||||
|
|
||||||
// Destroy OpenGL context
|
// Destroy OpenGL context
|
||||||
SDL_GL_DeleteContext(RENDER_GL_CONTEXT);
|
SDL_GL_DeleteContext(RENDER_GL_CONTEXT);
|
||||||
SDL_DestroyWindow(RENDER_WINDOW);
|
SDL_DestroyWindow(RENDER_WINDOW);
|
||||||
|
@@ -11,27 +11,16 @@
|
|||||||
#include "display/camera/camera.h"
|
#include "display/camera/camera.h"
|
||||||
|
|
||||||
#if RENDER_USE_FRAMEBUFFER
|
#if RENDER_USE_FRAMEBUFFER
|
||||||
SDL_Texture *RENDER_BACKBUFFER;
|
framebuffer_t RENDER_BACKBUFFER;
|
||||||
#else
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
errorret_t renderBackBufferInit(void) {
|
errorret_t renderBackBufferInit(void) {
|
||||||
#if RENDER_USE_FRAMEBUFFER
|
#if RENDER_USE_FRAMEBUFFER
|
||||||
// RENDER_BACKBUFFER = SDL_CreateTexture(
|
frameBufferInit(
|
||||||
// RENDER_RENDERER,
|
&RENDER_BACKBUFFER,
|
||||||
// SDL_PIXELFORMAT_RGBA8888,
|
RENDER_WIDTH,
|
||||||
// SDL_TEXTUREACCESS_TARGET,
|
RENDER_HEIGHT
|
||||||
// RENDER_WIDTH,
|
);
|
||||||
// RENDER_HEIGHT
|
|
||||||
// );
|
|
||||||
|
|
||||||
if(!RENDER_BACKBUFFER) {
|
|
||||||
errorThrow("SDL_CreateTexture failed: %s", SDL_GetError());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure we unbind the back buffer after creation
|
|
||||||
// SDL_SetRenderTarget(RENDER_RENDERER, NULL);
|
|
||||||
#else
|
#else
|
||||||
// No back buffer needed for window rendering
|
// No back buffer needed for window rendering
|
||||||
#endif
|
#endif
|
||||||
@@ -41,7 +30,7 @@ errorret_t renderBackBufferInit(void) {
|
|||||||
|
|
||||||
void renderBackBufferBind(void) {
|
void renderBackBufferBind(void) {
|
||||||
#if RENDER_USE_FRAMEBUFFER
|
#if RENDER_USE_FRAMEBUFFER
|
||||||
// SDL_SetRenderTarget(RENDER_RENDERER, RENDER_BACKBUFFER);
|
frameBufferBind(&RENDER_BACKBUFFER);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Fill background with cornflower blue.
|
// Fill background with cornflower blue.
|
||||||
@@ -51,7 +40,7 @@ void renderBackBufferBind(void) {
|
|||||||
|
|
||||||
void renderBackBufferUnbind(void) {
|
void renderBackBufferUnbind(void) {
|
||||||
#if RENDER_USE_FRAMEBUFFER
|
#if RENDER_USE_FRAMEBUFFER
|
||||||
// SDL_SetRenderTarget(RENDER_RENDERER, NULL);
|
frameBufferBind(NULL);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,11 +71,11 @@ void renderBackBufferDraw(void) {
|
|||||||
// Draw the back buffer texture
|
// Draw the back buffer texture
|
||||||
spriteBatchClear();
|
spriteBatchClear();
|
||||||
spriteBatchPush(
|
spriteBatchPush(
|
||||||
NULL,
|
&RENDER_BACKBUFFER.texture,
|
||||||
renderX, renderY,
|
renderX, renderY,
|
||||||
renderX+renderWidth, renderY+renderHeight,
|
renderX+renderWidth, renderY+renderHeight,
|
||||||
0xFF, 0x00, 0xFF, 0xFF,
|
0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
0, 0, 1, 1
|
0, 1, 1, 0
|
||||||
);
|
);
|
||||||
spriteBatchFlush();
|
spriteBatchFlush();
|
||||||
cameraScreenPop();
|
cameraScreenPop();
|
||||||
@@ -97,8 +86,7 @@ void renderBackBufferDraw(void) {
|
|||||||
|
|
||||||
errorret_t renderBackBufferDispose(void) {
|
errorret_t renderBackBufferDispose(void) {
|
||||||
#if RENDER_USE_FRAMEBUFFER
|
#if RENDER_USE_FRAMEBUFFER
|
||||||
SDL_DestroyTexture(RENDER_BACKBUFFER);
|
frameBufferDispose(&RENDER_BACKBUFFER);
|
||||||
RENDER_BACKBUFFER = NULL;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
|
@@ -7,6 +7,11 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "display/renderbase.h"
|
#include "display/renderbase.h"
|
||||||
|
#include "display/framebuffer/framebuffer.h"
|
||||||
|
|
||||||
|
#if RENDER_USE_FRAMEBUFFER
|
||||||
|
extern framebuffer_t RENDER_BACKBUFFER;
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the render back buffer. May be either a framebuffer or a texture
|
* Initializes the render back buffer. May be either a framebuffer or a texture
|
||||||
|
@@ -17,7 +17,6 @@ void textureInit(
|
|||||||
) {
|
) {
|
||||||
assertNotNull(texture, "Texture cannot be NULL");
|
assertNotNull(texture, "Texture cannot be NULL");
|
||||||
assertTrue(width > 0 && height > 0, "Width and height must be greater than 0");
|
assertTrue(width > 0 && height > 0, "Width and height must be greater than 0");
|
||||||
assertNotNull(data, "Data cannot be NULL");
|
|
||||||
|
|
||||||
#if PSP || 1
|
#if PSP || 1
|
||||||
assertTrue(
|
assertTrue(
|
||||||
|
Reference in New Issue
Block a user