starting textures
Some checks failed
Build Dusk / run-tests (push) Failing after 16s
Build Dusk / build-linux (push) Failing after 17s
Build Dusk / build-psp (push) Failing after 18s
Build Dusk / build-gamecube (push) Failing after 17s
Build Dusk / build-wii (push) Failing after 17s

This commit is contained in:
2026-03-23 19:42:24 -05:00
parent ebff7af9b5
commit 98947dea26
17 changed files with 93 additions and 106 deletions

View File

@@ -22,6 +22,8 @@
display_t DISPLAY = { 0 }; display_t DISPLAY = { 0 };
texture_t TEXTURE;
errorret_t displayInit(void) { errorret_t displayInit(void) {
memoryZero(&DISPLAY, sizeof(DISPLAY)); memoryZero(&DISPLAY, sizeof(DISPLAY));
@@ -36,6 +38,19 @@ errorret_t displayInit(void) {
errorChain(textInit()); errorChain(textInit());
errorChain(screenInit()); errorChain(screenInit());
color_t pixels[2 * 2] = {
COLOR_RED, COLOR_GREEN,
COLOR_BLUE, COLOR_WHITE
};
errorChain(textureInit(
&TEXTURE,
2, 2,
TEXTURE_FORMAT_RGBA,
(texturedata_t){
.rgbaColors = pixels
}
));
errorOk(); errorOk();
} }
@@ -76,10 +91,13 @@ errorret_t displayUpdate(void) {
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, proj)); errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, proj));
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, view)); errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, view));
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, model)); errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, model));
errorChain(shaderSetTexture(&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, &TEXTURE));
errorChain(spriteBatchPush( errorChain(spriteBatchPush(
NULL, 0.0f, 0.0f,
0.0f, 0.0f, 100, 100, COLOR_WHITE, 0, 0, 1, 1 1.0f, 1.0f,
COLOR_WHITE,
0.0f, 0.0f,
1.0f, 1.0f
)); ));
errorChain(spriteBatchFlush()); errorChain(spriteBatchFlush());

View File

@@ -32,6 +32,18 @@ errorret_t shaderSetMatrix(
errorOk(); errorOk();
} }
errorret_t shaderSetTexture(
shader_t *shader,
const char_t *name,
texture_t *texture
) {
assertNotNull(shader, "Shader cannot be null");
assertStrLenMin(name, 1, "Uniform name cannot be empty");
assertNotNull(texture, "Texture cannot be null");
errorChain(shaderSetTexturePlatform(shader, name, texture));
errorOk();
}
// errorret_t shaderSetColor( // errorret_t shaderSetColor(
// shader_t *shader, // shader_t *shader,
// const char_t *name, // const char_t *name,

View File

@@ -11,6 +11,7 @@
#define SHADER_UNLIT_PROJECTION "u_Proj" #define SHADER_UNLIT_PROJECTION "u_Proj"
#define SHADER_UNLIT_VIEW "u_View" #define SHADER_UNLIT_VIEW "u_View"
#define SHADER_UNLIT_MODEL "u_Model" #define SHADER_UNLIT_MODEL "u_Model"
#define SHADER_UNLIT_TEXTURE "u_Texture"
// #define SHADER_UNLIT_COLOR "u_Color" // #define SHADER_UNLIT_COLOR "u_Color"
extern shaderdefinition_t SHADER_UNLIT_DEFINITION; extern shaderdefinition_t SHADER_UNLIT_DEFINITION;

View File

@@ -25,7 +25,6 @@ errorret_t spriteBatchInit() {
} }
errorret_t spriteBatchPush( errorret_t spriteBatchPush(
texture_t *texture,
const float_t minX, const float_t minX,
const float_t minY, const float_t minY,
const float_t maxX, const float_t maxX,
@@ -37,7 +36,6 @@ errorret_t spriteBatchPush(
const float_t v1 const float_t v1
) { ) {
errorChain(spriteBatchPush3D( errorChain(spriteBatchPush3D(
texture,
(vec3){ minX, minY, 0 }, (vec3){ minX, minY, 0 },
(vec3){ maxX, maxY, 0 }, (vec3){ maxX, maxY, 0 },
color, color,
@@ -48,7 +46,6 @@ errorret_t spriteBatchPush(
} }
errorret_t spriteBatchPush3D( errorret_t spriteBatchPush3D(
texture_t *texture,
const vec3 min, const vec3 min,
const vec3 max, const vec3 max,
const color_t color, const color_t color,
@@ -56,12 +53,8 @@ errorret_t spriteBatchPush3D(
const vec2 uv1 const vec2 uv1
) { ) {
// Need to flush? // Need to flush?
if( if(SPRITEBATCH.spriteCount >= SPRITEBATCH_SPRITES_MAX) {
SPRITEBATCH.currentTexture != texture ||
SPRITEBATCH.spriteCount >= SPRITEBATCH_SPRITES_MAX
) {
errorChain(spriteBatchFlush()); errorChain(spriteBatchFlush());
SPRITEBATCH.currentTexture = texture;
} }
size_t vertexOffset = SPRITEBATCH.spriteCount * QUAD_VERTEX_COUNT; size_t vertexOffset = SPRITEBATCH.spriteCount * QUAD_VERTEX_COUNT;
@@ -75,7 +68,6 @@ errorret_t spriteBatchPush3D(
void spriteBatchClear() { void spriteBatchClear() {
SPRITEBATCH.spriteCount = 0; SPRITEBATCH.spriteCount = 0;
SPRITEBATCH.currentTexture = NULL;
} }
errorret_t spriteBatchFlush() { errorret_t spriteBatchFlush() {
@@ -85,7 +77,6 @@ errorret_t spriteBatchFlush() {
size_t vertexCount = QUAD_VERTEX_COUNT * SPRITEBATCH.spriteCount; size_t vertexCount = QUAD_VERTEX_COUNT * SPRITEBATCH.spriteCount;
errorChain(meshFlush(&SPRITEBATCH.mesh, 0, vertexCount)); errorChain(meshFlush(&SPRITEBATCH.mesh, 0, vertexCount));
errorChain(textureBind(SPRITEBATCH.currentTexture));
errorChain(meshDraw(&SPRITEBATCH.mesh, 0, vertexCount)); errorChain(meshDraw(&SPRITEBATCH.mesh, 0, vertexCount));
spriteBatchClear(); spriteBatchClear();
errorOk(); errorOk();

View File

@@ -7,16 +7,13 @@
#pragma once #pragma once
#include "display/mesh/quad.h" #include "display/mesh/quad.h"
#include "display/texture/texture.h"
#define SPRITEBATCH_SPRITES_MAX 1 #define SPRITEBATCH_SPRITES_MAX 16
#define SPRITEBATCH_VERTEX_COUNT (SPRITEBATCH_SPRITES_MAX * QUAD_VERTEX_COUNT) #define SPRITEBATCH_VERTEX_COUNT (SPRITEBATCH_SPRITES_MAX * QUAD_VERTEX_COUNT)
typedef struct { typedef struct {
mesh_t mesh; mesh_t mesh;
int32_t spriteCount; int32_t spriteCount;
texture_t *currentTexture;
} spritebatch_t; } spritebatch_t;
// Have to define these seperately because of alignment in certain platforms. // Have to define these seperately because of alignment in certain platforms.
@@ -39,7 +36,6 @@ errorret_t spriteBatchInit();
* Currently changing texture pointer will cause the buffer to flush but this is * Currently changing texture pointer will cause the buffer to flush but this is
* also likely to change in the future. * also likely to change in the future.
* *
* @param texture The texture to use for the sprite.
* @param minX The minimum x coordinate of the sprite. * @param minX The minimum x coordinate of the sprite.
* @param minY The minimum y coordinate of the sprite. * @param minY The minimum y coordinate of the sprite.
* @param maxX The maximum x coordinate of the sprite. * @param maxX The maximum x coordinate of the sprite.
@@ -52,7 +48,6 @@ errorret_t spriteBatchInit();
* @return An error code indicating success or failure. * @return An error code indicating success or failure.
*/ */
errorret_t spriteBatchPush( errorret_t spriteBatchPush(
texture_t *texture,
const float_t minX, const float_t minX,
const float_t minY, const float_t minY,
const float_t maxX, const float_t maxX,
@@ -68,7 +63,6 @@ errorret_t spriteBatchPush(
* Pushes a 3D sprite to the batch. This is like spriteBatchPush but takes * Pushes a 3D sprite to the batch. This is like spriteBatchPush but takes
* 3D coordinates instead of 2D. * 3D coordinates instead of 2D.
* *
* @param texture The texture to use for the sprite.
* @param min The minimum (x,y,z) coordinate of the sprite. * @param min The minimum (x,y,z) coordinate of the sprite.
* @param max The maximum (x,y,z) coordinate of the sprite. * @param max The maximum (x,y,z) coordinate of the sprite.
* @param color The color to tint the sprite with. * @param color The color to tint the sprite with.
@@ -77,7 +71,6 @@ errorret_t spriteBatchPush(
* @return An error code indicating success or failure. * @return An error code indicating success or failure.
*/ */
errorret_t spriteBatchPush3D( errorret_t spriteBatchPush3D(
texture_t *texture,
const vec3 min, const vec3 min,
const vec3 max, const vec3 max,
const color_t color, const color_t color,

View File

@@ -46,7 +46,7 @@ errorret_t textDrawChar(
tilesetTileGetUV(tileset, tileIndex, uv); tilesetTileGetUV(tileset, tileIndex, uv);
errorChain(spriteBatchPush( errorChain(spriteBatchPush(
texture, // texture,
x, y, x, y,
x + tileset->tileWidth, x + tileset->tileWidth,
y + tileset->tileHeight, y + tileset->tileHeight,

View File

@@ -11,8 +11,6 @@
#include "util/math.h" #include "util/math.h"
#include "display/display.h" #include "display/display.h"
texture_t *TEXTURE_BOUND = NULL;
errorret_t textureInit( errorret_t textureInit(
texture_t *texture, texture_t *texture,
const int32_t width, const int32_t width,
@@ -34,18 +32,9 @@ errorret_t textureInit(
errorOk(); errorOk();
} }
errorret_t textureBind(texture_t *texture) {
errorChain(textureBindPlatform(texture));
errorOk();
}
errorret_t textureDispose(texture_t *texture) { errorret_t textureDispose(texture_t *texture) {
assertNotNull(texture, "Texture cannot be NULL"); assertNotNull(texture, "Texture cannot be NULL");
if(TEXTURE_BOUND == texture) {
textureBind(NULL);
}
errorChain(textureDisposePlatform(texture)); errorChain(textureDisposePlatform(texture));
errorOk(); errorOk();
} }

View File

@@ -13,9 +13,6 @@
#ifndef textureInitPlatform #ifndef textureInitPlatform
#error "textureInitPlatform should not be defined." #error "textureInitPlatform should not be defined."
#endif #endif
#ifndef textureBindPlatform
#error "textureBindPlatform should not be defined."
#endif
#ifndef textureDisposePlatform #ifndef textureDisposePlatform
#error "textureDisposePlatform should not be defined." #error "textureDisposePlatform should not be defined."
#endif #endif
@@ -28,8 +25,6 @@ typedef union texturedata_u {
color_t *rgbaColors; color_t *rgbaColors;
} texturedata_t; } texturedata_t;
extern texture_t *TEXTURE_BOUND;
/** /**
* Initializes a texture. * Initializes a texture.
* *
@@ -47,13 +42,6 @@ errorret_t textureInit(
const texturedata_t data const texturedata_t data
); );
/**
* Binds a texture for rendering. Providing NULL will unbind any texture.
*
* @param texture The texture to bind.
*/
errorret_t textureBind(texture_t *texture);
/** /**
* Disposes a texture. * Disposes a texture.
* *

View File

@@ -176,7 +176,7 @@ void mapUpdate() {
void mapRender() { void mapRender() {
if(!mapIsLoaded()) return; if(!mapIsLoaded()) return;
textureBind(NULL); // textureBind(NULL);
for(chunkindex_t i = 0; i < MAP_CHUNK_COUNT; i++) { for(chunkindex_t i = 0; i < MAP_CHUNK_COUNT; i++) {
mapChunkRender(&MAP.chunks[i]); mapChunkRender(&MAP.chunks[i]);
} }

View File

@@ -32,32 +32,20 @@ int moduleSpriteBatchClear(lua_State *L) {
int moduleSpriteBatchPush(lua_State *L) { int moduleSpriteBatchPush(lua_State *L) {
assertNotNull(L, "Lua state is null"); assertNotNull(L, "Lua state is null");
// Texture pointer or Nil for no texture
if(!lua_isuserdata(L, 1) && !lua_isnil(L, 1)) {
return luaL_error(L, "First argument must be a texture or nil");
}
// If texture is not nil, check it's a texture userdata
texture_t *tex = NULL;
if(lua_isuserdata(L, 1)) {
tex = (texture_t *)luaL_checkudata(L, 1, "texture_mt");
assertNotNull(tex, "Texture pointer cannot be NULL");
}
// MinX, MinY, MaxX, MaxY // MinX, MinY, MaxX, MaxY
if( if(
!lua_isnumber(L, 2) || !lua_isnumber(L, 3) || !lua_isnumber(L, 4) || !lua_isnumber(L, 1) || !lua_isnumber(L, 2) || !lua_isnumber(L, 3) ||
!lua_isnumber(L, 5) !lua_isnumber(L, 4)
) { ) {
return luaL_error(L, "Sprite coordinates must be numbers"); return luaL_error(L, "Sprite coordinates must be numbers");
} }
// Color (struct) or nil for white // Color (struct) or nil for white
color_t *color = NULL; color_t *color = NULL;
if(lua_gettop(L) < 6 || lua_isnil(L, 6)) { if(lua_gettop(L) < 5 || lua_isnil(L, 5)) {
// Allow NULL // Allow NULL
} else if(lua_isuserdata(L, 6)) { } else if(lua_isuserdata(L, 5)) {
color = (color_t*)luaL_checkudata(L, 6, "color_mt"); color = (color_t*)luaL_checkudata(L, 5, "color_mt");
} else { } else {
return luaL_error(L, "Sprite color must be a color struct or nil"); return luaL_error(L, "Sprite color must be a color struct or nil");
} }
@@ -68,31 +56,30 @@ int moduleSpriteBatchPush(lua_State *L) {
float_t u1 = 1.0f; float_t u1 = 1.0f;
float_t v1 = 1.0f; float_t v1 = 1.0f;
if(lua_gettop(L) >= 8) { if(lua_gettop(L) >= 7) {
if(!lua_isnumber(L, 7) || !lua_isnumber(L, 8)) { if(!lua_isnumber(L, 6) || !lua_isnumber(L, 7)) {
return luaL_error(L, "Sprite UV min coordinates must be numbers"); return luaL_error(L, "Sprite UV min coordinates must be numbers");
} }
u0 = (float_t)lua_tonumber(L, 7); u0 = (float_t)lua_tonumber(L, 6);
v0 = (float_t)lua_tonumber(L, 8); v0 = (float_t)lua_tonumber(L, 7);
} }
if(lua_gettop(L) >= 10) { if(lua_gettop(L) >= 9) {
if(!lua_isnumber(L, 9) || !lua_isnumber(L, 10)) { if(!lua_isnumber(L, 8) || !lua_isnumber(L, 9)) {
return luaL_error(L, "Sprite UV max coordinates must be numbers"); return luaL_error(L, "Sprite UV max coordinates must be numbers");
} }
u1 = (float_t)lua_tonumber(L, 9); u1 = (float_t)lua_tonumber(L, 8);
v1 = (float_t)lua_tonumber(L, 10); v1 = (float_t)lua_tonumber(L, 9);
} }
float_t minX = (float_t)lua_tonumber(L, 2); float_t minX = (float_t)lua_tonumber(L, 1);
float_t minY = (float_t)lua_tonumber(L, 3); float_t minY = (float_t)lua_tonumber(L, 2);
float_t maxX = (float_t)lua_tonumber(L, 4); float_t maxX = (float_t)lua_tonumber(L, 3);
float_t maxY = (float_t)lua_tonumber(L, 5); float_t maxY = (float_t)lua_tonumber(L, 4);
errorret_t ret = spriteBatchPush( errorret_t ret = spriteBatchPush(
tex,
minX, minX,
minY, minY,
maxX, maxX,

View File

@@ -203,6 +203,32 @@ errorret_t shaderSetMatrixGL(
errorOk(); errorOk();
} }
errorret_t shaderSetTextureGL(
shadergl_t *shader,
const char_t *name,
texture_t *texture
) {
assertNotNull(shader, "Shader cannot be null");
assertNotNull(texture, "Texture cannot be null");
assertStrLenMin(name, 1, "Uniform name cannot be empty");
#ifdef DUSK_OPENGL_LEGACY
assertUnreachable("Cannot set textures on legacy opengl.");
#else
GLint location;
errorChain(shaderParamGetLocationGL(shader, name, &location));
glActiveTexture(GL_TEXTURE0);
errorChain(errorGLCheck());
glBindTexture(GL_TEXTURE_2D, texture->id);
errorChain(errorGLCheck());
glUniform1i(location, 0);
errorChain(errorGLCheck());
#endif
errorOk();
}
// errorret_t shaderSetColorGL( // errorret_t shaderSetColorGL(
// shadergl_t *shader, // shadergl_t *shader,
// const char_t *name, // const char_t *name,

View File

@@ -7,7 +7,7 @@
#pragma once #pragma once
#include "error/errorgl.h" #include "error/errorgl.h"
#include "display/color.h" #include "display/texture/texture.h"
typedef struct { typedef struct {
#ifdef DUSK_OPENGL_LEGACY #ifdef DUSK_OPENGL_LEGACY
@@ -100,11 +100,11 @@ errorret_t shaderSetMatrixGL(
* @param color The color data to set. * @param color The color data to set.
* @return An errorret_t indicating success or failure. * @return An errorret_t indicating success or failure.
*/ */
// errorret_t shaderSetTextureGL( errorret_t shaderSetTextureGL(
// shadergl_t *shader, shadergl_t *shader,
// const char_t *name, const char_t *name,
// texture_t *texture texture_t *texture
// ); );
/** /**
* Sets a color uniform parameter in the shader. * Sets a color uniform parameter in the shader.

View File

@@ -14,6 +14,6 @@ typedef shaderdefinitiongl_t shaderdefinitionplatform_t;
#define shaderInitPlatform shaderInitGL #define shaderInitPlatform shaderInitGL
#define shaderBindPlatform shaderBindGL #define shaderBindPlatform shaderBindGL
#define shaderSetMatrixPlatform shaderSetMatrixGL #define shaderSetMatrixPlatform shaderSetMatrixGL
// #define shaderSetTexturePlatform shaderSetTextureGL #define shaderSetTexturePlatform shaderSetTextureGL
// #define shaderSetColorPlatform shaderSetColorGL // #define shaderSetColorPlatform shaderSetColorGL
#define shaderDisposePlatform shaderDisposeGL #define shaderDisposePlatform shaderDisposeGL

View File

@@ -20,17 +20,21 @@
"uniform mat4 u_View;\n" "uniform mat4 u_View;\n"
"uniform mat4 u_Model;\n" "uniform mat4 u_Model;\n"
"out vec4 v_Color;\n" "out vec4 v_Color;\n"
"out vec2 v_TexCoord;\n"
"void main() {\n" "void main() {\n"
" gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n" " gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n"
" v_Color = aColor;\n" " v_Color = aColor;\n"
" v_TexCoord = aTexCoord;\n"
"}\n", "}\n",
.frag = .frag =
"#version 330 core\n" "#version 330 core\n"
"uniform sampler2D u_Texture;\n"
"in vec4 v_Color;\n" "in vec4 v_Color;\n"
"in vec2 v_TexCoord;\n"
"out vec4 FragColor;\n" "out vec4 FragColor;\n"
"void main() {\n" "void main() {\n"
" FragColor = v_Color;\n" " FragColor = texture(u_Texture, v_TexCoord) * v_Color;\n"
"}\n" "}\n"
}; };
#endif #endif

View File

@@ -63,19 +63,6 @@ errorret_t textureInitGL(
errorOk(); 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) { errorret_t textureDisposeGL(texturegl_t *texture) {
assertNotNull(texture, "Texture cannot be NULL"); assertNotNull(texture, "Texture cannot be NULL");
assertTrue(texture->id != 0, "Texture ID must be valid"); assertTrue(texture->id != 0, "Texture ID must be valid");

View File

@@ -40,14 +40,6 @@ errorret_t textureInitGL(
const texturedata_t data 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. * Disposes a texture.
* *

View File

@@ -12,5 +12,4 @@ typedef textureformatgl_t textureformatplatform_t;
typedef texturegl_t textureplatform_t; typedef texturegl_t textureplatform_t;
#define textureInitPlatform textureInitGL #define textureInitPlatform textureInitGL
#define textureBindPlatform textureBindGL
#define textureDisposePlatform textureDisposeGL #define textureDisposePlatform textureDisposeGL