Moved a bunch of code around

This commit is contained in:
2026-03-07 09:35:56 -06:00
parent 93074d653e
commit dd048d9b0d
40 changed files with 810 additions and 842 deletions

14
src/duskgl/CMakeLists.txt Normal file
View File

@@ -0,0 +1,14 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Includes
target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
# Subdirs
add_subdirectory(display)
add_subdirectory(error)

View File

@@ -0,0 +1,14 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
displaygl.c
)
# Subdirs
add_subdirectory(framebuffer)
add_subdirectory(texture)

View File

@@ -0,0 +1,31 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "displaygl.h"
errorret_t displayOpenGLInit(void) {
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);// PSP defaults this on?
glShadeModel(GL_SMOOTH); // Fixes color on PSP?
errorChain(errorGLCheck());
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepth(1.0f);
errorChain(errorGLCheck());
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
errorChain(errorGLCheck());
glEnableClientState(GL_COLOR_ARRAY);// To confirm: every frame on PSP?
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
errorChain(errorGLCheck());
}

View File

@@ -0,0 +1,14 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/errorgl.h"
/**
* Initializes the OpenGL specific contexts for rendering.
*/
errorret_t displayOpenGLInit(void);

View File

@@ -0,0 +1,9 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
)

View File

@@ -0,0 +1,133 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "display/display.h"
#include "display/framebuffer/framebuffer.h"
#include "assert/assert.h"
errorret_t frameBufferGLInitBackBuffer(void) {
errorOk();
}
uint32_t frameBufferGetWidth(const framebuffer_t *framebuffer) {
if(framebuffer == NULL) {
return 0;
}
if(framebuffer == &FRAMEBUFFER_BACKBUFFER) {
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
int32_t windowWidth, windowHeight;
SDL_GetWindowSize(DISPLAY.window, &windowWidth, &windowHeight);
return windowWidth;
#else
return DUSK_DISPLAY_WIDTH;
#endif
}
return framebuffer->texture.width;
}
uint32_t frameBufferGLGetHeight(const framebuffer_t *framebuffer) {
if(framebuffer == NULL) {
return 0;
}
if(framebuffer == &FRAMEBUFFER_BACKBUFFER) {
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
int32_t windowWidth, windowHeight;
SDL_GetWindowSize(DISPLAY.window, &windowWidth, &windowHeight);
return windowHeight;
#else
return DUSK_DISPLAY_HEIGHT;
#endif
}
return framebuffer->texture.height;
}
errorret_t frameBufferGLBind(framebuffer_t *framebuffer) {
assertNotNull(framebuffer, "Framebuffer cannot be NULL");
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
if(framebuffer == &FRAMEBUFFER_BACKBUFFER) {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
} else {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer->id);
}
glViewport(
0, 0,
frameBufferGetWidth(framebuffer), frameBufferGetHeight(framebuffer)
);
#else
glViewport(
0, 0,
DUSK_DISPLAY_WIDTH, DUSK_DISPLAY_HEIGHT
);
#endif
errorChain(errorGLCheck());
errorOk();
}
void frameBufferClear(const uint8_t flags, const color_t color) {
GLbitfield glFlags = 0;
if(flags & FRAMEBUFFER_CLEAR_COLOR) {
glFlags |= GL_COLOR_BUFFER_BIT;
glClearColor(
color.r / 255.0f,
color.g / 255.0f,
color.b / 255.0f,
color.a / 255.0f
);
}
if(flags & FRAMEBUFFER_CLEAR_DEPTH) {
glFlags |= GL_DEPTH_BUFFER_BIT;
}
glClear(glFlags);
}
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
errorret_t frameBufferGLInit(
framebuffer_t *fb,
const uint32_t width,
const uint32_t height
) {
assertNotNull(fb, "Framebuffer cannot be NULL");
assertTrue(width > 0 && height > 0, "W/H must be greater than 0");
memoryZero(fb, sizeof(framebuffer_t));
textureInit(&fb->texture, width, height, TEXTURE_FORMAT_RGBA,(texturedata_t){
.rgbaColors = NULL
});
errorChain(errorGLCheck());
glGenFramebuffersEXT(1, &fb->id);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb->id);
errorChain(errorGLCheck());
glFramebufferTexture2DEXT(
GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D, fb->texture.id, 0
);
errorChain(errorGLCheck());
if(
glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) !=
GL_FRAMEBUFFER_COMPLETE_EXT
) {
assertUnreachable("Framebuffer is not complete");
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
errorChain(errorGLCheck());
errorOk();
}
#endif

View File

@@ -0,0 +1,36 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/texture/texture.h"
#include "error/errorgl.h"
typedef struct {
GLuint id;
texture_t texture;
} framebuffergl_t;
/**
* Initializes the backbuffer framebuffer. (OpenGL implementation).
*/
errorret_t frameBufferGLInitBackBuffer(void);
#ifdef DUSK_DISPLAY_SIZE_DYNAMIC
/**
* Initializes an OpenGL style framebuffer.
*
* @param fb The framebuffer to initialize.
* @param width The width of the framebuffer.
* @param height The height of the framebuffer.
* @return Either error or not.
*/
errorret_t frameBufferGLInit(
framebuffergl_t *fb,
const uint32_t width,
const uint32_t height
);
#endif

View File

@@ -0,0 +1,14 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/framebuffer/framebuffergl.h"
typedef framebuffergl_t framebufferplatform_t;
#define frameBufferPlatformInitBackBuffer frameBufferGLInitBackBuffer
#define frameBufferPlatformInit frameBufferGLInit
#define frameBufferPlatformBind frameBufferGLBind

View File

@@ -0,0 +1,10 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
texturegl.c
)

View File

@@ -0,0 +1,86 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "display/texture/texture.h"
#include "assert/assert.h"
#include "error/errorgl.h"
errorret_t textureInitGL(
texturegl_t *texture,
const int32_t width,
const int32_t height,
const textureformatgl_t format,
const texturedata_t data
) {
glGenTextures(1, &texture->id);
glBindTexture(GL_TEXTURE_2D, texture->id);
errorChain(errorGLCheck());
switch(format) {
case TEXTURE_FORMAT_RGBA:
glTexImage2D(
GL_TEXTURE_2D, 0, format, width, height, 0,
format, GL_UNSIGNED_BYTE, (void*)data.rgbaColors
);
break;
case TEXTURE_FORMAT_PALETTE:
assertNotNull(data.paletteData, "Palette texture data cannot be NULL");
glTexImage2D(
GL_TEXTURE_2D,
0, GL_COLOR_INDEX8_EXT,
width, height,
0, GL_COLOR_INDEX8_EXT,
GL_UNSIGNED_BYTE, (void*)data.paletteData
);
errorChain(errorGLCheck());
glColorTableEXT(
GL_TEXTURE_2D, GL_RGBA, data.palette.palette->colorCount, GL_RGBA,
GL_UNSIGNED_BYTE, (const void*)data.palette.palette->colors
);
break;
default:
assertUnreachable("Unknown texture format");
break;
}
errorChain(errorGLCheck());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
errorChain(errorGLCheck());
glBindTexture(GL_TEXTURE_2D, 0);
errorChain(errorGLCheck());
errorOk();
}
errorret_t textureBindGL(texturegl_t *texture) {
if(texture == NULL) {
glBindTexture(GL_TEXTURE_2D, 0);
errorChain(errorGLCheck());
errorOk();
}
assertTrue(texture->id != 0, "Texture ID must be valid");
glBindTexture(GL_TEXTURE_2D, texture->id);
errorChain(errorGLCheck());
errorOk();
}
errorret_t textureDisposeGL(texturegl_t *texture) {
assertNotNull(texture, "Texture cannot be NULL");
assertTrue(texture->id != 0, "Texture ID must be valid");
glDeleteTextures(1, &texture->id);
errorChain(errorGLCheck());
errorOk();
}

View File

@@ -0,0 +1,57 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
typedef union texturedata_u texturedata_t;
typedef enum {
TEXTURE_FORMAT_RGBA = GL_RGBA,
TEXTURE_FORMAT_PALETTE = GL_COLOR_INDEX8_EXT,
} textureformatgl_t;
typedef struct {
GLuint id;
textureformatgl_t format;
int32_t width;
int32_t height;
} texturegl_t;
/**
* Initializes a texture.
*
* @param texture The texture to initialize.
* @param width The width of the texture.
* @param height The height of the texture.
* @param format The format of the texture (e.g., GL_RGBA, GL_ALPHA).
* @param data The data for the texture, the format changes per format.
* @return An error if the texture failed to initialize, otherwise success.
*/
errorret_t textureInitGL(
texturegl_t *texture,
const int32_t width,
const int32_t height,
const textureformatgl_t format,
const texturedata_t data
);
/**
* Binds a texture for rendering. Providing NULL will unbind any texture.
*
* @param texture The texture to bind.
* @return An error if the texture failed to bind, otherwise success.
*/
errorret_t textureBindGL(texturegl_t *texture);
/**
* Disposes a texture.
*
* @param texture The texture to dispose.
* @return An error if the texture failed to dispose, otherwise success.
*/
errorret_t textureDisposeGL(texturegl_t *texture);

View File

@@ -0,0 +1,16 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/texture/texturegl.h"
typedef textureformatgl_t textureformatplatform_t;
typedef texturegl_t textureplatform_t;
#define textureInitPlatform textureInitGL
#define textureBindPlatform textureBindGL
#define textureDisposePlatform textureDisposeGL

11
src/duskgl/duskgl.h Normal file
View File

@@ -0,0 +1,11 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>

View File

@@ -0,0 +1,10 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
errorgl.c
)

View File

@@ -0,0 +1,17 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "errorgl.h"
errorret_t errorGLCheck(void) {
GLenum err = glGetError();
if(err != GL_NO_ERROR) {
errorThrow("GL Error: %d", err);
}
errorOk();
}

View File

@@ -0,0 +1,16 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "error/error.h"
/**
* Gets the GL error (if any) and throws it as a dusk style error.
*
* @return Error code, if GL error present.
*/
errorret_t errorGLCheck(void);