Dawn/test/display/render.c
2021-02-16 19:22:12 +11:00

75 lines
1.6 KiB
C

#include "render.h"
render_t *RENDER_CURRENT = NULL;
render_t * renderInit(int32_t width, int32_t height, char *name) {
// Can't contextualize twice
if(RENDER_CURRENT != NULL) return NULL;
// Attempt to init GLFW
if(!glfwInit()) return NULL;
// Initialize the renderer
render_t *render = malloc(sizeof(render_t));
if(!render) return NULL;
render->width = width;
render->height = height;
// Create the window.
render->window = glfwCreateWindow(width, height, name, NULL, NULL);
if(!render->window) {
free(render);
glfwTerminate();
return NULL;
}
// Make the window the context current
glfwMakeContextCurrent(render->window);
RENDER_CURRENT = render;
// Listeners
glfwSetWindowSizeCallback(render->window, &renderOnResize);
//GLEnable Things
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LEQUAL);
return render;
}
void renderFrame(render_t *render) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f(-1, -1, 0);
glColor3f(0, 1, 0);
glVertex3f(0, 1, 0);
glColor3f(0, 0, 1);
glVertex3f(1, -1, 0);
glEnd();
}
bool renderDispose(render_t *render) {
// Cleanup the callback
glfwSetWindowSizeCallback(render->window, NULL);
// Free up the renderer
free(render);
RENDER_CURRENT = NULL;
// Terminate the GLFW context.
glfwTerminate();
return true;
}
void renderOnResize(GLFWwindow *window, int32_t width, int32_t height) {
RENDER_CURRENT->width = width;
RENDER_CURRENT->height = height;
glViewport(0, 0, width, height);
}