1 module evael.renderer.gl.gl_device;
2 
3 import bindbc.glfw;
4 
5 import evael.renderer.graphics_device;
6 import evael.renderer.gl.gl_wrapper;
7 import evael.renderer.gl.gl_enum_converter;
8 import evael.renderer.gl.gl_shader;
9 import evael.renderer.gl.gl_texture;
10 import evael.renderer.gl.gl_context_settings;
11 
12 import evael.core.game_config : GraphicsSettings;
13 
14 import evael.lib.image.image;
15 import evael.lib.memory;
16 import evael.lib.containers.array;
17 
18 debug 
19 {
20     import std.experimental.logger;
21 }
22 
23 class GLDevice : GraphicsDevice
24 {
25     private uint m_vao;
26 
27     /**
28      * GLDevice constructor.
29      */
30     @nogc
31     public this(in ref GraphicsSettings graphicsSettings)
32     {
33         super(graphicsSettings);
34 
35         this.initializeGLFW();
36     }
37 
38     /**
39      * GLDevice destructor.
40      */
41     @nogc
42     public ~this()
43     {
44 
45     }
46     
47     @nogc
48     @property
49     public override void window(GLFWwindow* win)
50     {
51         super.window = win;
52         glfwMakeContextCurrent(this.m_window);
53 
54         this.initialize();
55     }
56 
57     @nogc
58     public override void beginFrame(in Color color = Color.LightGrey)
59     {
60         auto colorf = color.asFloat();
61 
62         gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
63         gl.ClearColor(colorf[0], colorf[1], colorf[2], 1.0f); 
64     }
65 
66     @nogc
67     public override void endFrame()
68     {
69 
70     }
71 
72     @nogc
73     private void initializeGLFW()
74     {
75         immutable contextSettings = GLContextSettings();
76 
77         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, contextSettings.ver / 10);
78         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, contextSettings.ver % 10);
79         glfwWindowHint(GLFW_OPENGL_PROFILE, contextSettings.profile);
80         glfwWindowHint(GLFW_SAMPLES, 4);
81 
82         glfwSwapInterval(this.m_graphicsSettings.vsync);
83     }
84 
85     @nogc
86     private void initialize()
87     {
88         debug infof("OpenGL:%s", loadOpenGL());
89         
90         gl.GenVertexArrays(1, &this.m_vao);
91         gl.BindVertexArray(this.m_vao);
92     }
93 }