From 9d1eb251b5d26759e17dd7bee316cf27fdcb28bf Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 17 Jun 2023 20:23:34 -0700 Subject: Move and import error library from clib. --- gfx/CMakeLists.txt | 2 +- gfx/include/gfx/error.h | 22 -------------- gfx/src/error.c | 5 ---- gfx/src/render/framebuffer.c | 67 ++++++++++++++++++++++--------------------- gfx/src/render/renderbuffer.c | 14 ++++----- gfx/src/render/texture.c | 5 ++-- gfx/src/scene/light.c | 10 +++---- gfx/src/util/scene.c | 4 +-- gfx/src/util/texture.c | 20 ++++++------- gltfview/CMakeLists.txt | 1 + gltfview/src/game.c | 4 +-- 11 files changed, 64 insertions(+), 90 deletions(-) delete mode 100644 gfx/include/gfx/error.h delete mode 100644 gfx/src/error.c diff --git a/gfx/CMakeLists.txt b/gfx/CMakeLists.txt index f5c9b5a..f5ef44c 100644 --- a/gfx/CMakeLists.txt +++ b/gfx/CMakeLists.txt @@ -52,7 +52,6 @@ add_library(gfx SHARED src/scene/object.c src/scene/scene.c src/scene/scene_memory.c - src/error.c src/gfx.c src/util/geometry.c src/util/ibl.c @@ -75,6 +74,7 @@ target_link_libraries(gfx PUBLIC target_link_libraries(gfx PRIVATE cgltf cgltf-tangents + error glad listpool log diff --git a/gfx/include/gfx/error.h b/gfx/include/gfx/error.h deleted file mode 100644 index 062dba1..0000000 --- a/gfx/include/gfx/error.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#include -#include - -/// Get the last error. -const char* gfx_get_error(void); - -extern xlstring gfx_error; - -/// Set the last error. -#define gfx_set_error(...) \ - gfx_error.length = snprintf(gfx_error.str, xlstring_size, __VA_ARGS__) - -/// Prepend an error to the last error. -#define gfx_prepend_error(...) \ - { \ - xlstring head; \ - head.length = snprintf(head.str, xlstring_size, __VA_ARGS__); \ - xlstring_append(&head, xlstring_make(": ")); \ - gfx_error = xlstring_concat(head, gfx_error); \ - } diff --git a/gfx/src/error.c b/gfx/src/error.c deleted file mode 100644 index 4370097..0000000 --- a/gfx/src/error.c +++ /dev/null @@ -1,5 +0,0 @@ -#include - -xlstring gfx_error; - -const char* gfx_get_error(void) { return xlstring_cstr(&gfx_error); } diff --git a/gfx/src/render/framebuffer.c b/gfx/src/render/framebuffer.c index 323ef56..84ade4a 100644 --- a/gfx/src/render/framebuffer.c +++ b/gfx/src/render/framebuffer.c @@ -3,12 +3,12 @@ #include "renderbuffer.h" #include "texture.h" -#include +#include #include -static void framebuffer_attach_colour(FrameBuffer* framebuffer, - const FrameBufferAttachment* attachment) { +static void framebuffer_attach_colour( + FrameBuffer* framebuffer, const FrameBufferAttachment* attachment) { assert(framebuffer); assert(attachment); @@ -16,27 +16,28 @@ static void framebuffer_attach_colour(FrameBuffer* framebuffer, case FrameBufferNoAttachment: break; case FrameBufferTexture: - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, - attachment->texture.texture->id, - attachment->texture.mip_level); + glFramebufferTexture2D( + GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, + attachment->texture.texture->id, attachment->texture.mip_level); break; case FrameBufferCubemapTexture: - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, - to_GL_cubemap_face(attachment->cubemap.face), - attachment->cubemap.texture->id, - attachment->cubemap.mip_level); + glFramebufferTexture2D( + GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, + to_GL_cubemap_face(attachment->cubemap.face), + attachment->cubemap.texture->id, attachment->cubemap.mip_level); break; case FrameBufferRenderBuffer: - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, - GL_RENDERBUFFER, attachment->renderbuffer->id); + glFramebufferRenderbuffer( + GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, + attachment->renderbuffer->id); break; } ASSERT_GL; } -static void framebuffer_attach_depth(FrameBuffer* framebuffer, - const FrameBufferAttachment* attachment) { +static void framebuffer_attach_depth( + FrameBuffer* framebuffer, const FrameBufferAttachment* attachment) { assert(framebuffer); assert(attachment); @@ -44,33 +45,33 @@ static void framebuffer_attach_depth(FrameBuffer* framebuffer, case FrameBufferNoAttachment: break; case FrameBufferTexture: - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, - GL_DEPTH_COMPONENT, attachment->texture.texture->id, - attachment->texture.mip_level); + glFramebufferTexture2D( + GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_DEPTH_COMPONENT, + attachment->texture.texture->id, attachment->texture.mip_level); break; // TODO: Could distinguish between colour and depth attachment types to make // this a compile-time error. case FrameBufferCubemapTexture: - gfx_set_error( - "Cannot use a cubemap texture as a depth framebuffer attachment"); + set_error("Cannot use a cubemap texture as a depth framebuffer attachment"); break; case FrameBufferRenderBuffer: - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, - GL_RENDERBUFFER, attachment->renderbuffer->id); + glFramebufferRenderbuffer( + GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, + attachment->renderbuffer->id); break; } ASSERT_GL; } -bool gfx_init_framebuffer(FrameBuffer* framebuffer, - const FrameBufferDesc* desc) { +bool gfx_init_framebuffer( + FrameBuffer* framebuffer, const FrameBufferDesc* desc) { assert(framebuffer); assert(desc); glGenFramebuffers(1, &framebuffer->id); if (!framebuffer->id) { - gfx_set_error("glGenFramebuffers() failed"); + set_error("glGenFramebuffers() failed"); return false; } @@ -84,7 +85,7 @@ bool gfx_init_framebuffer(FrameBuffer* framebuffer, framebuffer_attach_colour(framebuffer, &desc->colour); framebuffer_attach_depth(framebuffer, &desc->depth); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { - gfx_set_error("glCheckFramebufferStatus() failed"); + set_error("glCheckFramebufferStatus() failed"); gfx_del_framebuffer(framebuffer); return false; } @@ -95,8 +96,8 @@ bool gfx_init_framebuffer(FrameBuffer* framebuffer, return true; } -bool gfx_framebuffer_attach_colour(FrameBuffer* framebuffer, - const FrameBufferAttachment* attachment) { +bool gfx_framebuffer_attach_colour( + FrameBuffer* framebuffer, const FrameBufferAttachment* attachment) { assert(framebuffer); assert(attachment); @@ -104,14 +105,14 @@ bool gfx_framebuffer_attach_colour(FrameBuffer* framebuffer, glBindFramebuffer(GL_FRAMEBUFFER, framebuffer->id); framebuffer_attach_colour(framebuffer, attachment); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { - gfx_set_error("glCheckFramebufferStatus() failed"); + set_error("glCheckFramebufferStatus() failed"); return false; } return true; } -bool gfx_framebuffer_attach_depth(FrameBuffer* framebuffer, - const FrameBufferAttachment* attachment) { +bool gfx_framebuffer_attach_depth( + FrameBuffer* framebuffer, const FrameBufferAttachment* attachment) { assert(framebuffer); assert(attachment); @@ -119,7 +120,7 @@ bool gfx_framebuffer_attach_depth(FrameBuffer* framebuffer, glBindFramebuffer(GL_FRAMEBUFFER, framebuffer->id); framebuffer_attach_depth(framebuffer, attachment); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { - gfx_set_error("glCheckFramebufferStatus() failed"); + set_error("glCheckFramebufferStatus() failed"); return false; } return true; @@ -143,8 +144,8 @@ void gfx_deactivate_framebuffer(const FrameBuffer* framebuffer) { glBindFramebuffer(GL_FRAMEBUFFER, 0); } -void gfx_framebuffer_set_viewport(FrameBuffer* framebuffer, int x, int y, - int width, int height) { +void gfx_framebuffer_set_viewport( + FrameBuffer* framebuffer, int x, int y, int width, int height) { assert(framebuffer); glViewport(x, y, width, height); } diff --git a/gfx/src/render/renderbuffer.c b/gfx/src/render/renderbuffer.c index 3b19483..a2eae52 100644 --- a/gfx/src/render/renderbuffer.c +++ b/gfx/src/render/renderbuffer.c @@ -2,23 +2,23 @@ #include "texture.h" -#include +#include -bool gfx_init_renderbuffer(RenderBuffer* renderbuffer, - const RenderBufferDesc* desc) { +bool gfx_init_renderbuffer( + RenderBuffer* renderbuffer, const RenderBufferDesc* desc) { assert(renderbuffer); assert(desc); glGenRenderbuffers(1, &renderbuffer->id); if (!renderbuffer->id) { - gfx_set_error("glGenRenderbuffers failed"); + set_error("glGenRenderbuffers failed"); return false; } glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer->id); - glRenderbufferStorage(GL_RENDERBUFFER, - to_GL_internal_format(desc->texture_format), - desc->width, desc->height); + glRenderbufferStorage( + GL_RENDERBUFFER, to_GL_internal_format(desc->texture_format), desc->width, + desc->height); glBindRenderbuffer(GL_RENDERBUFFER, 0); ASSERT_GL; diff --git a/gfx/src/render/texture.c b/gfx/src/render/texture.c index 31bf636..312aecc 100644 --- a/gfx/src/render/texture.c +++ b/gfx/src/render/texture.c @@ -1,7 +1,6 @@ #include "texture.h" -#include - +#include #include #include @@ -13,7 +12,7 @@ bool gfx_init_texture(Texture* texture, const TextureDesc* desc) { glGenTextures(1, &texture->id); if (!texture->id) { - gfx_set_error("glGenTextures() failed"); + set_error("glGenTextures() failed"); return false; } texture->target = to_GL_dimension(desc->dimension); diff --git a/gfx/src/scene/light.c b/gfx/src/scene/light.c index 2ca1a01..31dca77 100644 --- a/gfx/src/scene/light.c +++ b/gfx/src/scene/light.c @@ -3,14 +3,14 @@ #include "node_impl.h" #include "scene_memory.h" -#include +#include -static void make_environment_light(Light* light, - const EnvironmentLightDesc* desc) { +static void make_environment_light( + Light* light, const EnvironmentLightDesc* desc) { assert(light); assert(desc); - light->type = EnvironmentLightType; + light->type = EnvironmentLightType; light->environment.environment_map = desc->environment_map; } @@ -27,7 +27,7 @@ Light* gfx_make_light(const LightDesc* desc) { make_environment_light(light, &desc->light.environment); break; default: - gfx_set_error("Unhandled light type"); + set_error("Unhandled light type"); gfx_destroy_light(&light); return 0; } diff --git a/gfx/src/util/scene.c b/gfx/src/util/scene.c index a638fa5..5d79cf2 100644 --- a/gfx/src/util/scene.c +++ b/gfx/src/util/scene.c @@ -81,7 +81,6 @@ #include -#include #include #include #include @@ -96,6 +95,7 @@ #include #include +#include #include #include #include @@ -702,7 +702,7 @@ static bool load_texture_and_uniform( textures[texture_index] = gfx_load_texture(render_backend, cmd); if (!textures[texture_index]) { - gfx_prepend_error( + prepend_error( "Failed to load texture: %s", mstring_cstr(&cmd->data.texture.filepath)); return false; diff --git a/gfx/src/util/texture.c b/gfx/src/util/texture.c index 0727dae..7ec0e40 100644 --- a/gfx/src/util/texture.c +++ b/gfx/src/util/texture.c @@ -1,8 +1,9 @@ #include -#include #include +#include + #define STB_IMAGE_IMPLEMENTATION #include @@ -60,7 +61,7 @@ Texture* gfx_load_texture( stbi_set_flip_vertically_on_load(0); pixels[0] = stbi_load(filepath, &width, &height, &components, 0); if (!pixels[0]) { - gfx_set_error("Failed to load texture file: %s", filepath); + set_error("Failed to load texture file: %s", filepath); } break; } @@ -73,13 +74,12 @@ Texture* gfx_load_texture( stbi_uc* image_pixels = stbi_load(filepath, &width, &height, &components, 0); if (!image_pixels) { - gfx_set_error("Failed to load texture file: %s", filepath); + set_error("Failed to load texture file: %s", filepath); break; } if (i > 0 && components != old_components) { - gfx_set_error( - "All textures in a cubemap must have the same number of " - "components"); + set_error("All textures in a cubemap must have the same number of " + "components"); break; } if ((i != 2) && (i != 3)) { @@ -93,7 +93,7 @@ Texture* gfx_load_texture( break; case TextureFromMemory: // TODO: Load textures from memory. - gfx_set_error("Loading textures from memory is not yet implemented"); + set_error("Loading textures from memory is not yet implemented"); return 0; } @@ -132,7 +132,7 @@ Texture* gfx_load_texture( desc.format = TextureSRGB8; break; default: - gfx_set_error("Unsupported texture colour space: %d", cmd->colour_space); + set_error("Unsupported texture colour space: %d", cmd->colour_space); return 0; } break; @@ -145,12 +145,12 @@ Texture* gfx_load_texture( desc.format = TextureSRGBA8; break; default: - gfx_set_error("Unsupported texture colour space: %d", cmd->colour_space); + set_error("Unsupported texture colour space: %d", cmd->colour_space); return 0; } break; default: - gfx_set_error("Unsupported number of texture components: %d", components); + set_error("Unsupported number of texture components: %d", components); return 0; } diff --git a/gltfview/CMakeLists.txt b/gltfview/CMakeLists.txt index 0b0c3cc..de745ce 100644 --- a/gltfview/CMakeLists.txt +++ b/gltfview/CMakeLists.txt @@ -13,6 +13,7 @@ target_include_directories(gltfview PRIVATE target_link_libraries(gltfview PRIVATE cstring + error gfx gfx-app list diff --git a/gltfview/src/game.c b/gltfview/src/game.c index 6d8430b..7470f75 100644 --- a/gltfview/src/game.c +++ b/gltfview/src/game.c @@ -9,11 +9,11 @@ #include "game.h" #include "plugins/plugin.h" -#include #include #include #include +#include #include #include #include @@ -105,7 +105,7 @@ bool game_new(Game* game, int argc, const char** argv) { return true; cleanup: - LOGE("Gfx error: %s", gfx_get_error()); + LOGE("Gfx error: %s", get_error()); game_end(game); return false; } -- cgit v1.2.3