summaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authorpryazha <pryadeiniv@mail.ru>2025-08-02 13:21:42 +0500
committerpryazha <pryadeiniv@mail.ru>2025-08-02 13:21:42 +0500
commit2e64d3c5d6eb1eb04168d39d5eb5f2d89af1a8b0 (patch)
tree85c8a586a1343469cdcdab5dccad023053571ecd /libs
parent33d5f67044d104d69cb2d11e78e6a79bc20d4c4e (diff)
debug chapterHEADmaster
Diffstat (limited to 'libs')
-rw-r--r--libs/common.h127
1 files changed, 125 insertions, 2 deletions
diff --git a/libs/common.h b/libs/common.h
index 72b44e7..08e753a 100644
--- a/libs/common.h
+++ b/libs/common.h
@@ -181,6 +181,7 @@ typedef struct {
GLFWwindow *window;
S32 width;
S32 height;
+ S32 debug;
} state_t;
Input input_init()
@@ -190,7 +191,7 @@ Input input_init()
return(input);
}
-state_t init_state(S32 width, S32 height)
+state_t init_state(S32 width, S32 height, S32 debug)
{
state_t state = {
.arena = arena_alloc(Megabytes(64)),
@@ -204,7 +205,8 @@ state_t init_state(S32 width, S32 height)
.pitch = 0.0f
},
.width = width,
- .height = height
+ .height = height,
+ .debug = debug
};
return state;
}
@@ -664,6 +666,8 @@ void init_glfw(state_t *state) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+ if (state->debug)
+ glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
glfwWindowHint(GLFW_POSITION_X, xpos + monitor_width / 2.0f - state->width / 2.0f);
@@ -680,10 +684,91 @@ void init_glfw(state_t *state) {
glfwSwapInterval(0);
}
+void gl_debug_output(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
+{
+ /* ignore these non-significant error codes */
+ if(id == 131169 || id == 131185 || id == 131218 || id == 131204)
+ return;
+ info("debug message (%d): %s", id, message);
+ switch (source) {
+ case GL_DEBUG_SOURCE_API:
+ info("source: api");
+ break;
+ case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
+ info("source: window system");
+ break;
+ case GL_DEBUG_SOURCE_SHADER_COMPILER:
+ info("source: shader compiler");
+ break;
+ case GL_DEBUG_SOURCE_THIRD_PARTY:
+ info("source: third party");
+ break;
+ case GL_DEBUG_SOURCE_APPLICATION:
+ info("source: application");
+ break;
+ case GL_DEBUG_SOURCE_OTHER:
+ info("source: other");
+ break;
+ }
+ switch (type) {
+ case GL_DEBUG_TYPE_ERROR:
+ info("type: error");
+ break;
+ case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
+ info("type: deprecated behaviour");
+ break;
+ case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
+ info("type: undefined behaviour");
+ break;
+ case GL_DEBUG_TYPE_PORTABILITY:
+ info("type: portability");
+ break;
+ case GL_DEBUG_TYPE_PERFORMANCE:
+ info("type: performance");
+ break;
+ case GL_DEBUG_TYPE_MARKER:
+ info("type: marker");
+ break;
+ case GL_DEBUG_TYPE_PUSH_GROUP:
+ info("type: push group");
+ break;
+ case GL_DEBUG_TYPE_POP_GROUP:
+ info("type: pop group");
+ break;
+ case GL_DEBUG_TYPE_OTHER:
+ info("type: other");
+ break;
+ }
+ switch (severity) {
+ case GL_DEBUG_SEVERITY_HIGH:
+ info("severity: high");
+ break;
+ case GL_DEBUG_SEVERITY_MEDIUM:
+ info("severity: medium");
+ break;
+ case GL_DEBUG_SEVERITY_LOW:
+ info("severity: low");
+ break;
+ case GL_DEBUG_SEVERITY_NOTIFICATION:
+ info("severity: notification");
+ break;
+ }
+ putc('\n', stdout);
+}
+
void init_gl(void)
{
if (glewInit() != GLEW_OK)
die("failed to initialize glew");
+ int flags;
+ glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
+ if (flags & GL_CONTEXT_FLAG_DEBUG_BIT) {
+ info("debug context initialized successfully");
+ glEnable(GL_DEBUG_OUTPUT);
+ glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
+ glDebugMessageCallback(gl_debug_output, 0);
+ glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
+ }
glEnable(GL_DEPTH_TEST);
}
@@ -1056,4 +1141,42 @@ F32 lerp(F32 a, F32 b, F32 f)
return a + f * (b - a);
}
+U32 check_gl_errors_(const char *file, int line)
+{
+ U32 code;
+ while ((code = glGetError()) != GL_NO_ERROR) {
+ char *error;
+ switch (code) {
+ case GL_INVALID_ENUM:
+ error = "invalid enum";
+ break;
+ case GL_INVALID_VALUE:
+ error = "invalid value";
+ break;
+ case GL_INVALID_OPERATION:
+ error = "invalid operation";
+ break;
+ case GL_STACK_OVERFLOW:
+ error = "stack overflow";
+ break;
+ case GL_STACK_UNDERFLOW:
+ error = "stack underflow";
+ break;
+ case GL_OUT_OF_MEMORY:
+ error = "out of memory";
+ break;
+ case GL_INVALID_FRAMEBUFFER_OPERATION:
+ error = "invalid framebuffer operation";
+ break;
+ default:
+ error = "undefined error";
+ break;
+ }
+ info("%s:%d: %s", file, line, error);
+ }
+ return code;
+}
+
+#define check_gl_errors() check_gl_errors_(__FILE__, __LINE__)
+
#endif /* COMMON_H */