Subastra
Loading...
Searching...
No Matches
context.h
Go to the documentation of this file.
1#ifndef __H__RENDERING_CONTEXT__
2#define __H__RENDERING_CONTEXT__
3
4#include <GL/glew.h>
5#include <GLFW/glfw3.h>
6
7#include "../defs.h"
8#include "../mat4.h"
9#include "../vec2.h"
10
11typedef struct {
12 i32 width, height;
13 GLFWwindow *window;
15
16static void glfwErrorCallback(int errc, const char *desc) {
17 PANIC("GLFW Error %d: %s", errc, desc);
18}
19
20static void GLAPIENTRY glMessageCallback(GLenum source, GLenum type, GLuint id,
21 GLenum severity, GLsizei length,
22 const GLchar *message,
23 const void *userParam) {
24 fprintf(stderr,
25 "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
26 (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""), type, severity,
27 message);
28}
29
31 glfwSetErrorCallback(glfwErrorCallback);
32
33 glfwInit();
34
35 glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
36 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
37 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
38 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
39 glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
40
41 ctx->window = glfwCreateWindow(640, 480, "Hello, world!", 0, 0);
42 glfwMakeContextCurrent(ctx->window);
43
44 glewInit();
45
46 glEnable(GL_DEBUG_OUTPUT);
47 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
48 glDebugMessageCallback(glMessageCallback, 0);
49}
50
52 glfwShowWindow(ctx->window);
53}
54
56 return glfwWindowShouldClose(ctx->window);
57}
58
60 glfwGetFramebufferSize(ctx->window, &ctx->width, &ctx->height);
61 glViewport(0, 0, ctx->width, ctx->height);
62 glClear(GL_COLOR_BUFFER_BIT);
63}
64
65static void rendering_ctx_set_projection(rendering_ctx_t *ctx, GLuint program,
66 mat4 projection) {
67 GLint location = glGetUniformLocation(program, "uProjection");
68 glUseProgram(program);
69 glUniformMatrix4fv(location, 1, GL_FALSE, projection.data);
70}
71
73 glfwSwapBuffers(ctx->window);
74 glfwPollEvents();
75}
76
78 glfwDestroyWindow(ctx->window);
79 glfwTerminate();
80}
81
82#endif
static void rendering_ctx_frame_begin(rendering_ctx_t *ctx)
Definition context.h:59
static void glfwErrorCallback(int errc, const char *desc)
Definition context.h:16
static void GLAPIENTRY glMessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
Definition context.h:20
static void rendering_ctx_cleanup(rendering_ctx_t *ctx)
Definition context.h:77
static bool rendering_ctx_should_close(rendering_ctx_t *ctx)
Definition context.h:55
static void rendering_ctx_frame_end(rendering_ctx_t *ctx)
Definition context.h:72
static void rendering_ctx_show_window(rendering_ctx_t *ctx)
Definition context.h:51
static void rendering_ctx_set_projection(rendering_ctx_t *ctx, GLuint program, mat4 projection)
Definition context.h:65
static void rendering_ctx_init(rendering_ctx_t *ctx)
Definition context.h:30
int32_t i32
Definition defs.h:47
#define PANIC(msg,...)
Definition defs.h:15
Definition mat4.h:7
float data[16]
Definition mat4.h:8
Definition context.h:11
i32 height
Definition context.h:12
i32 width
Definition context.h:12
GLFWwindow * window
Definition context.h:13