1 /*
2  *******************************************************************************************
3  * Dgame (a D game framework) - Copyright (c) Randy Schütt
4  *
5  * This software is provided 'as-is', without any express or implied warranty.
6  * In no event will the authors be held liable for any damages arising from
7  * the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not claim
14  *    that you wrote the original software. If you use this software in a product,
15  *    an acknowledgment in the product documentation would be appreciated but is
16  *    not required.
17  *
18  * 2. Altered source versions must be plainly marked as such, and must not be
19  *    misrepresented as being the original software.
20  *
21  * 3. This notice may not be removed or altered from any source distribution.
22  *******************************************************************************************
23  */
24 module Dgame.Internal.core;
25 
26 private import derelict.opengl3.gl;
27 
28 /**
29  * Current Version
30  */
31 enum DgVersion : ubyte {
32 	Major = 0,
33 	Minor = 4,
34 	PatchLevel = 1
35 }
36 
37 /**
38  * Returns a readable version number.
39  */
40 string getDgVersion() pure {
41 	import std..string : format;
42 
43 	return format("%d.%d.%d", DgVersion.Major, DgVersion.Minor, DgVersion.PatchLevel);
44 }
45 
46 //struct gl {
47 //	static auto ref opDispatch(string name, Args...)(Args args) {
48 //		scope (exit) glcheckGLError();
49 //		return mixin("gl"~name~"(args)");
50 //	}
51 //}
52 
53 /**
54  *
55  */
56 void glCheck(lazy void Func, string filename = __FILE__, size_t line_number = __LINE__) {
57 	Func();
58 	scope(exit) GLCheckError(filename, line_number);
59 }
60 
61 /**
62  *
63  */
64 void GLCheckError(string filename, size_t line_number) {
65 	import std..string : format;
66 
67 	// Get the last error
68 	GLenum ErrorCode = glGetError();
69 
70 	if (ErrorCode != GL_NO_ERROR) {
71 		string Error = "unknown error";
72 		string Desc  = "no description";
73 
74 		// Decode the error code
75 		switch (ErrorCode) {
76 			case GL_INVALID_ENUM:
77 				Error = "GL_INVALID_ENUM";
78 				Desc  = "an unacceptable value has been specified for an enumerated argument";
79 				break;
80 
81 			case GL_INVALID_VALUE:
82 				Error = "GL_INVALID_VALUE";
83 				Desc  = "a numeric argument is out of range";
84 				break;
85 
86 			case GL_INVALID_OPERATION:
87 				Error = "GL_INVALID_OPERATION";
88 				Desc  = "the specified operation is not allowed in the current state";
89 				break;
90 
91 			case GL_STACK_OVERFLOW:
92 				Error = "GL_STACK_OVERFLOW";
93 				Desc  = "this command would cause a stack overflow";
94 				break;
95 
96 			case GL_STACK_UNDERFLOW:
97 				Error = "GL_STACK_UNDERFLOW";
98 				Desc  = "this command would cause a stack underflow";
99 				break;
100 
101 			case GL_OUT_OF_MEMORY:
102 				Error = "GL_OUT_OF_MEMORY";
103 				Desc  = "there is not enough memory left to execute the command";
104 				break;
105 				/*
106 				 case GL_INVALID_FRAMEBUFFER_OPERATION:
107 				 Error = "GL_INVALID_FRAMEBUFFER_OPERATION_EXT";
108 				 Desc  = "the object bound to FRAMEBUFFER_BINDING_EXT is not
109 				 \"framebuffer complete\"";
110 				 break;
111 				 */
112 			default: break;
113 		}
114 
115 		throw new Exception(format("An internal OpenGL call failed: %s -> %s in file %s on line %d",
116 		                            Error, Desc, filename, line_number));
117 	}
118 }