1 module polyplex.core.render.gl;
2 public import polyplex.core.render.gl.batch;
3 public import polyplex.core.render.gl.debug2d;
4 public import polyplex.core.render.gl.renderbuf;
5 import polyplex.core.render;
6 import polyplex.core.render.gl.gloo;
7 static import win = polyplex.core.window;
8 import polyplex.core.color;
9 import polyplex.utils;
10 import polyplex.math;
11 import polyplex;
12 
13 import bindbc.sdl;
14 import bindbc.opengl;
15 import bindbc.opengl.gl;
16 import std.conv;
17 import std.stdio;
18 
19 public import polyplex.core.render.gl.shader;
20 
21 // TODO: Remove SDL dependency from this.
22 
23 public class Renderer {
24 static:
25 private:
26 	win.Window window;
27 	Rectanglei scissorRect;
28 
29 package(polyplex):
30 	void setWindow(win.Window parent) {
31 		this.window = parent;
32 	}
33 
34 public:
35 	win.Window Window() {
36 		return window;
37 	}
38 
39 	void Init() {
40 		// Create the neccesary rendering backend.
41 		Window.CreateContext(GraphicsBackend.OpenGL);
42 		
43 		loadOpenGL();
44 
45 		SpriteBatch.InitializeSpritebatch();
46 		Debugging2D.PrepDebugging();
47 		
48 		//Default settings for sprite clamping and wrapping
49 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
50 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
51 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
52 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
53 
54 		Logger.Info("OpenGL version: {0}", to!string(glGetString(GL_VERSION)));
55 		GL.Enable (Capability.Blending);
56 		GL.BlendFunc (GL.SourceAlpha, GL.OneMinusSourceAlpha);
57 		
58 		// TODO: reimplement this.
59 		//Crash if system has unsupported opengl version.
60 		//if (glver < GLVersion.gl30) throw new Error("Sorry, your graphics card does not support Open GL 3 or above.");
61 		Logger.Info("OpenGL initialized...");
62 	}
63 	
64 	@property Rectanglei ScissorRectangle() {
65 		return scissorRect;
66 	}
67 
68 	@property void ScissorRectangle(Rectanglei rect) {
69 		scissorRect = rect;
70 		glScissor(
71 			scissorRect.X, 
72 			(Renderer.Window.ClientBounds.Height-scissorRect.Y)-scissorRect.Height, 
73 			scissorRect.Width, 
74 			scissorRect.Height);
75 	}
76 
77 	@property VSyncState VSync() {
78 		return Window.VSync;
79 	}
80 
81 	@property void VSync(VSyncState state) {
82 		Window.VSync = state;
83 	}
84 
85 	void AdjustViewport() {
86 		glViewport(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height);
87 	}
88 
89 	void ClearColor(Color color) {
90 		glClearColor(color.Rf, color.Gf, color.Bf, color.Af);
91 		glClear(GL_COLOR_BUFFER_BIT);
92 		ClearDepth();
93 	}
94 
95 	void ClearDepth() {
96 		glClear(GL_DEPTH_BUFFER_BIT);
97 	}
98 
99 	void SwapBuffers() {
100 		Window.SwapBuffers();
101 	}
102 }