graphics api abstraction
[demo] / src / opengl / opengl.cc
1 #include <GL/glew.h>
2 #include <stdio.h>
3
4 #include "gfxapi.h"
5 #include "opengl/opengl.h"
6
7 extern GLFWwindow *win;
8 extern int win_h;
9 extern int win_w;
10
11 static void clear(float r, float g, float b);
12 static void viewport(int x, int y, int width, int height);
13
14 bool init_opengl()
15 {
16         if(!glfwInit()) {
17                 fprintf(stderr, "Failed to initialize GLFW.\n");
18                 return false;
19         }
20
21         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
22         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
23         glfwWindowHint(GLFW_SRGB_CAPABLE, GLFW_TRUE);
24
25         if(!(win = glfwCreateWindow(win_w, win_h, "glcow", 0, 0))) {
26                 fprintf(stderr, "Failed to create window.\n");
27                 return false;
28         }
29         glfwMakeContextCurrent(win);
30
31         glewInit();
32
33         glEnable(GL_DEPTH_TEST);
34         glEnable(GL_CULL_FACE);
35         glEnable(GL_FRAMEBUFFER_SRGB); // linear colorspace
36
37         gfx_clear = clear;
38         gfx_viewport = viewport;
39
40         return true;
41 }
42
43 void cleanup_opengl()
44 {
45         if(win) {
46                 glfwDestroyWindow(win);
47         }
48         glfwTerminate();
49 }
50
51 static void clear(float r, float g, float b)
52 {
53         glClearColor(r, g, b, 1.0);
54         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
55 }
56
57 static void viewport(int x, int y, int width, int height)
58 {
59         glViewport(x, y, width, height);
60 }