From a4c67096a3afd74ec9ceff9260cff9ad0336b208 Mon Sep 17 00:00:00 2001 From: onelin Date: Thu, 13 Nov 2025 09:48:56 +0100 Subject: Use Direct State Access DSA (Direct State Access) is apparently a more modern way of using OpenGL, since version 4.5. --- src/rendering.c | 54 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 12 deletions(-) (limited to 'src/rendering.c') diff --git a/src/rendering.c b/src/rendering.c index 4db3308..a37de58 100644 --- a/src/rendering.c +++ b/src/rendering.c @@ -476,8 +476,7 @@ void render_present(Window* w) { gl->UseProgram(o->shader.program); // TODO: Use texture atlas - gl->ActiveTexture(GL_TEXTURE0); - gl->BindTexture(GL_TEXTURE_2D, o->texture); + gl->BindTextureUnit(0, o->texture); { mat4 model = GLM_MAT4_IDENTITY_INIT; @@ -671,12 +670,23 @@ Texture createTextureFromImageData(unsigned char* image_data, i32 width, i32 hei } const GladGLContext* gl = w->context; - - gl->GenTextures(1, &t.id); - gl->BindTexture(GL_TEXTURE_2D, t.id); u32 err = gl->GetError(); if (err) { - ERROR("Failed to bind texture from image data!"); + ERROR("There's already something wrong!"); + } + + gl->CreateTextures(GL_TEXTURE_2D, 1, &t.id); + err = gl->GetError(); + if (err) { + if (err == GL_INVALID_ENUM) { + ERROR("Failed to create texture! GL_INVALID_ENUM"); + } + else if (err == GL_INVALID_VALUE) { + ERROR("Failed to create texture! GL_INVALID_VALUE"); + } + else { + ERROR("Failed to create texture!"); + } } /* TODO: Support more formats than rgb and rgba, such as gray, gray/alpha, etc.*/ @@ -685,13 +695,33 @@ Texture createTextureFromImageData(unsigned char* image_data, i32 width, i32 hei /* TODO: Don't force internal format to RGB */ - gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, - GL_UNSIGNED_BYTE, image_data); - - gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + gl->TextureStorage2D(t.id, 1, GL_RGB8, width, height); + err = gl->GetError(); + if (err) { + char* errstr = NULL; + switch (err) { + case GL_INVALID_ENUM: + errstr = "GL_INVALID_ENUM"; break; + case GL_INVALID_VALUE: + errstr = "GL_INVALID_VALUE"; break; + case GL_INVALID_OPERATION: + errstr = "GL_INVALID_OPERATION"; break; + default: + errstr = "unknown"; break; + } + ERROR("Failed to allocate memory for texture! %dx%d size (%s)", width, height, errstr); + } + gl->TextureSubImage2D(t.id, 0, + // offset, size + 0, 0, width, height, + format, GL_UNSIGNED_BYTE, image_data); + err = gl->GetError(); + if (err) { + ERROR("Failed to copy image data!"); + } - gl->BindTexture(GL_TEXTURE_2D, 0); + gl->TextureParameteri(t.id, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + gl->TextureParameteri(t.id, GL_TEXTURE_MIN_FILTER, GL_NEAREST); return t; } -- cgit v1.3