1 module meld.window;
2 
3 import std.stdio;
4 import std.conv;
5 import derelict.opengl3.gl3;
6 import derelict.glfw3.glfw3;
7 import core.thread;
8 import meld;
9 import std..string;
10 
11 import meld.fileWatcher;
12 
13 import std.c.stdio : fputs, fputc, stderr;
14 
15 extern(C) nothrow void glfwPrintError(int error, const(char)* description) {
16   fputs(description, stderr);
17   fputc('\n', stderr);
18 }
19 
20 class Window
21 {
22 package:
23 	static GLFWwindow* m_window;
24 
25 public:
26 	this(string name, int width, int height)
27 	{
28 		DerelictGL3.load();
29 		DerelictGLFW3.load();
30 
31 		glfwSetErrorCallback(&glfwPrintError);
32 		if (!glfwInit())
33 		{
34 			glfwTerminate();
35 			throw new Exception("Failed to create gl context");
36 		}
37 
38 		glfwWindowHint(GLFW_SAMPLES, 4);
39 		glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
40 		glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
41 		glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
42 		glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
43 		glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
44 
45 		m_window = glfwCreateWindow(width, height, name.toStringz, null, null);
46 
47 		glfwMakeContextCurrent(m_window);
48 
49 		DerelictGL3.reload();
50 
51 		writefln("Vendor:   %s",   to!string(glGetString(GL_VENDOR)));
52 		writefln("Renderer: %s",   to!string(glGetString(GL_RENDERER)));
53 		writefln("Version:  %s",   to!string(glGetString(GL_VERSION)));
54 		writefln("GLSL:     %s\n", to!string(glGetString(GL_SHADING_LANGUAGE_VERSION)));
55 
56 		//Enable culling and depth
57 		glEnable(GL_CULL_FACE);
58 		glCullFace(GL_FRONT);
59 		glEnable(GL_DEPTH_TEST);
60 	}
61 
62 	bool IsRunning()
63 	{
64 		if (glfwWindowShouldClose(m_window))
65 		{
66 			glfwDestroyWindow(m_window);
67 			glfwTerminate();
68 			return false;
69 		}
70 
71 		return true;
72 	}
73 
74 	void Swap()
75 	{
76 		glfwSwapBuffers(m_window);
77 		glfwPollEvents();
78 
79 		FileWatcher.Update();
80 	}
81 
82 	double Time()
83 	{
84 		return glfwGetTime();
85 	}
86 }