88 lines
1.9 KiB
C
88 lines
1.9 KiB
C
/**
|
|
* Copyright (c) 2021 Dominic Msters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "render.h"
|
|
|
|
render_t *RENDER_CURRENT = NULL;
|
|
|
|
render_t * renderInit(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 = WINDOW_WIDTH_DEFAULT;
|
|
render->height = WINDOW_HEIGHT_DEFAULT;
|
|
|
|
// Create the window.
|
|
render->window = glfwCreateWindow(render->width, render->height,
|
|
name, NULL, NULL
|
|
);
|
|
if(!render->window) {
|
|
free(render);
|
|
glfwTerminate();
|
|
return NULL;
|
|
}
|
|
|
|
// Make the window the context current
|
|
glfwMakeContextCurrent(render->window);
|
|
|
|
// Load GLAD
|
|
gladLoadGL((GLADloadproc)glfwGetProcAddress);
|
|
|
|
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);
|
|
} |