summaryrefslogtreecommitdiff
path: root/src/rendering.c
diff options
context:
space:
mode:
authoronelin <oscar@nelin.dk>2025-11-13 08:48:56 +0000
committeronelin <oscar@nelin.dk>2025-11-13 08:51:52 +0000
commita4c67096a3afd74ec9ceff9260cff9ad0336b208 (patch)
treec230dd2592433c7bbf6587fd81f4b52c649ec484 /src/rendering.c
parentf175d64cc00f9d08f8c02bcbf8287c3e21430611 (diff)
Use Direct State Access
DSA (Direct State Access) is apparently a more modern way of using OpenGL, since version 4.5.
Diffstat (limited to 'src/rendering.c')
-rw-r--r--src/rendering.c54
1 files changed, 42 insertions, 12 deletions
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;
}