Bladeren bron

Windowing classes now static. Tried to clamp camera rotation.

Thomas Buck 10 jaren geleden
bovenliggende
commit
f41562f7f2

+ 3
- 0
ChangeLog.md Bestand weergeven

@@ -4,6 +4,9 @@
4 4
 
5 5
     [ 20141228 ]
6 6
     * Room Bounding Boxes are now visualized in Wireframe mode (again)
7
+    * Window/WindowSDL/WindowGLFW are now completely static
8
+    * Shader class got some helper methods. Now in own file, system/Shader.
9
+    * Tried to limit camera vertical rotation.
7 10
 
8 11
     [ 20141227 ]
9 12
     * Added rudimentary SDL2 Game Controller support.

+ 1
- 1
include/Camera.h Bestand weergeven

@@ -56,7 +56,7 @@ class Camera {
56 56
     static glm::mat4 projection;
57 57
     static glm::mat4 view;
58 58
     static float rotationDeltaX, rotationDeltaY;
59
-    static bool updateViewFrustum;
59
+    static bool updateViewFrustum, dirty;
60 60
 };
61 61
 
62 62
 #endif

+ 10
- 0
include/Log.h Bestand weergeven

@@ -11,6 +11,8 @@
11 11
 #include <string>
12 12
 #include <sstream>
13 13
 #include <vector>
14
+#include <glm/vec2.hpp>
15
+#include <glm/vec3.hpp>
14 16
 
15 17
 class Log {
16 18
   public:
@@ -35,6 +37,14 @@ class Log {
35 37
         return (*this);
36 38
     }
37 39
 
40
+    Log& operator<< (const glm::vec2& v) {
41
+        return (*this) << v.x << " " << v.y;
42
+    }
43
+
44
+    Log& operator<< (const glm::vec3& v) {
45
+        return (*this) << v.x << " " << v.y << " " << v.z;
46
+    }
47
+
38 48
   private:
39 49
     std::vector<std::string> mHistory;
40 50
     std::ostringstream printBuffer;

+ 1
- 1
include/RoomData.h Bestand weergeven

@@ -40,7 +40,7 @@ class BoundingBox {
40 40
         return corner[i];
41 41
     }
42 42
 
43
-    void display(glm::mat4 VP, glm::vec3 color);
43
+    void display(glm::mat4 VP);
44 44
 
45 45
   private:
46 46
     glm::vec3 corner[8];

+ 1
- 1
include/system/Font.h Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file include/Font.h
2
+ * \file include/system/Font.h
3 3
  * \brief Font interface
4 4
  *
5 5
  * \author xythobuz

+ 1
- 1
include/system/FontImGui.h Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file include/FontImGui.h
2
+ * \file include/system/FontImGui.h
3 3
  * \brief Default Font implementation
4 4
  *
5 5
  * \author xythobuz

+ 1
- 1
include/system/FontSDL.h Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file include/FontSDL.h
2
+ * \file include/system/FontSDL.h
3 3
  * \brief SDL Font implementation
4 4
  *
5 5
  * \author xythobuz

+ 1
- 1
include/system/FontTRLE.h Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file include/FontTRLE.h
2
+ * \file include/system/FontTRLE.h
3 3
  * \brief Tomb Raider Level Editor Font loader
4 4
  *
5 5
  * \author xythobuz

+ 56
- 0
include/system/Shader.h Bestand weergeven

@@ -0,0 +1,56 @@
1
+/*!
2
+ * \file include/system/Shader.h
3
+ * \brief OpenGL Shader implementation
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _SHADER_H_
9
+#define _SHADER_H_
10
+
11
+#include <vector>
12
+#include <glm/mat4x4.hpp>
13
+#include <glm/vec2.hpp>
14
+#include <glm/vec4.hpp>
15
+
16
+#include "TextureManager.h"
17
+
18
+class Shader {
19
+  public:
20
+    Shader() : programID(-1) { }
21
+    ~Shader();
22
+
23
+    int compile(const char* vertex, const char* fragment);
24
+    void use();
25
+
26
+    int addUniform(const char* name);
27
+    unsigned int getUniform(int n);
28
+
29
+    void addBuffer(int n = 1);
30
+    unsigned int getBuffer(int n);
31
+
32
+    template<typename T>
33
+    void bufferData(int buff, std::vector<T> v) {
34
+        glBindBuffer(GL_ARRAY_BUFFER, getBuffer(buff));
35
+        glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(T), &v[0], GL_STATIC_DRAW);
36
+    }
37
+
38
+    void loadUniform(int uni, glm::vec2 vec);
39
+    void loadUniform(int uni, glm::vec4 vec);
40
+    void loadUniform(int uni, glm::mat4 mat);
41
+    void loadUniform(int uni, int texture, TextureManager::TextureStorage store, int slot = 0);
42
+
43
+    void bindBuffer(int buff);
44
+    void bindBuffer(int buff, int location, int size);
45
+
46
+    void disableAttribs();
47
+
48
+  private:
49
+    int programID;
50
+    std::vector<unsigned int> uniforms;
51
+    std::vector<unsigned int> buffers;
52
+    std::vector<bool> attribs;
53
+};
54
+
55
+#endif
56
+

+ 19
- 57
include/system/Window.h Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file include/Window.h
2
+ * \file include/system/Window.h
3 3
  * \brief Windowing interface
4 4
  *
5 5
  * \author xythobuz
@@ -14,53 +14,26 @@
14 14
 #include <glm/vec3.hpp>
15 15
 #include <glm/vec4.hpp>
16 16
 
17
-class Shader {
18
-  public:
19
-    Shader() : programID(-1) { }
20
-    ~Shader();
21
-
22
-    int compile(const char* vertex, const char* fragment);
23
-    void use();
24
-
25
-    int addUniform(const char* name);
26
-    unsigned int getUniform(int n);
27
-
28
-    void addBuffer(int n = 1);
29
-    unsigned int getBuffer(int n);
30
-
31
-  private:
32
-    int programID;
33
-    std::vector<unsigned int> uniforms;
34
-    std::vector<unsigned int> buffers;
35
-};
17
+#include "Shader.h"
36 18
 
37 19
 class Window {
38 20
   public:
21
+    static int initialize();
22
+    static void eventHandling();
23
+    static void swapBuffers();
24
+    static void shutdown();
39 25
 
40
-    virtual ~Window() {}
41
-
42
-    virtual void setSize(unsigned int width, unsigned int height) = 0;
43
-    virtual unsigned int getWidth();
44
-    virtual unsigned int getHeight();
45
-
46
-    virtual void setFullscreen(bool fullscreen) = 0;
47
-    virtual bool getFullscreen();
26
+    static void setSize(glm::vec2 s);
27
+    static glm::vec2 getSize();
48 28
 
49
-    virtual void setMousegrab(bool grab) = 0;
50
-    virtual bool getMousegrab();
29
+    static void setFullscreen(bool f);
30
+    static bool getFullscreen();
51 31
 
52
-    virtual int initialize() = 0;
32
+    static void setMousegrab(bool g);
33
+    static bool getMousegrab();
53 34
 
54
-    virtual void eventHandling() = 0;
55
-
56
-    virtual void setTextInput(bool on) = 0;
57
-    virtual bool getTextInput();
58
-
59
-    virtual void swapBuffersGL() = 0;
60
-
61
-    static int initializeGL();
62
-    static void shutdownGL();
63
-    static void resizeGL();
35
+    static void setTextInput(bool t);
36
+    static bool getTextInput();
64 37
 
65 38
     static void drawTextGL(std::vector<glm::vec2>& vertices, std::vector<glm::vec2>& uvs,
66 39
                            glm::vec4 color, unsigned int texture);
@@ -69,20 +42,12 @@ class Window {
69 42
                        std::vector<unsigned short>& indices, glm::mat4 MVP, unsigned int texture);
70 43
 
71 44
     static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
72
-                       std::vector<unsigned short>& indices, glm::mat4 MVP);
73
-
74
-    static void drawLinesGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
75
-                            std::vector<unsigned short>& indices, glm::mat4 MVP);
76
-
77
-  protected:
78
-    bool mInit;
79
-    bool mFullscreen;
80
-    bool mMousegrab;
81
-    bool mTextInput;
82
-    unsigned int mWidth;
83
-    unsigned int mHeight;
45
+                       std::vector<unsigned short>& indices, glm::mat4 MVP, int mode = GL_TRIANGLES);
84 46
 
85 47
   private:
48
+    static int initializeGL();
49
+    static void shutdownGL();
50
+
86 51
     static Shader textShader;
87 52
     static const char* textShaderVertex;
88 53
     static const char* textShaderFragment;
@@ -90,6 +55,7 @@ class Window {
90 55
     static Shader imguiShader;
91 56
     static const char* imguiShaderVertex;
92 57
     static const char* imguiShaderFragment;
58
+    friend class UI;
93 59
 
94 60
     static Shader textureShader;
95 61
     static const char* textureShaderVertex;
@@ -100,11 +66,7 @@ class Window {
100 66
     static const char* colorShaderFragment;
101 67
 
102 68
     static unsigned int vertexArrayID;
103
-
104
-    friend class UI;
105 69
 };
106 70
 
107
-Window& getWindow();
108
-
109 71
 #endif
110 72
 

+ 25
- 18
include/system/WindowGLFW.h Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file include/WindowGLFW.h
2
+ * \file include/system/WindowGLFW.h
3 3
  * \brief GLFW windowing implementation
4 4
  *
5 5
  * \author xythobuz
@@ -10,27 +10,24 @@
10 10
 
11 11
 #include <GLFW/glfw3.h>
12 12
 
13
-#include "system/Window.h"
14
-
15
-class WindowGLFW : public Window {
13
+class WindowGLFW {
16 14
   public:
15
+    static int initialize();
16
+    static void eventHandling();
17
+    static void swapBuffers();
18
+    static void shutdown();
17 19
 
18
-    WindowGLFW();
19
-    virtual ~WindowGLFW();
20
-
21
-    virtual void setSize(unsigned int width, unsigned int height);
22
-
23
-    virtual void setFullscreen(bool fullscreen);
24
-
25
-    virtual void setMousegrab(bool grab);
26
-
27
-    virtual int initialize();
20
+    static void setSize(glm::vec2 s);
21
+    static glm::vec2 getSize() { return size; }
28 22
 
29
-    virtual void eventHandling();
23
+    static void setFullscreen(bool f);
24
+    static bool getFullscreen() { return fullscreen; }
30 25
 
31
-    virtual void setTextInput(bool on);
26
+    static void setMousegrab(bool g);
27
+    static bool getMousegrab() { return mousegrab; }
32 28
 
33
-    virtual void swapBuffersGL();
29
+    static void setTextInput(bool t);
30
+    static bool getTextInput() { return textinput; }
34 31
 
35 32
   private:
36 33
     static void errorCallback(int error, const char* desc);
@@ -42,7 +39,17 @@ class WindowGLFW : public Window {
42 39
 
43 40
     static KeyboardButton convertAsciiButton(int key);
44 41
 
45
-    GLFWwindow* mWindow;
42
+    static glm::vec2 size;
43
+    static bool fullscreen;
44
+    static bool mousegrab;
45
+    static bool textinput;
46
+    static GLFWwindow* window;
47
+    static int lastMouseX;
48
+    static int lastMouseY;
49
+    static bool modShift;
50
+    static bool modControl;
51
+    static bool modAlt;
52
+    static bool modSuper;
46 53
 };
47 54
 
48 55
 #endif

+ 23
- 30
include/system/WindowSDL.h Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file include/WindowSDL.h
2
+ * \file include/system/WindowSDL.h
3 3
  * \brief SDL windowing implementation
4 4
  *
5 5
  * \author xythobuz
@@ -8,44 +8,37 @@
8 8
 #ifndef _WINDOW_SDL_H_
9 9
 #define _WINDOW_SDL_H_
10 10
 
11
-#include "SDL.h"
11
+#include <glm/vec2.hpp>
12 12
 
13
-#include "system/Window.h"
13
+#include "SDL.h"
14 14
 
15
-/*!
16
- * \brief SDL windowing implementation
17
- */
18
-class WindowSDL : public Window {
15
+class WindowSDL {
19 16
   public:
17
+    static int initialize();
18
+    static void eventHandling();
19
+    static void swapBuffers();
20
+    static void shutdown();
20 21
 
21
-    /*!
22
-     * \brief Constructs an object of WindowSDL
23
-     */
24
-    WindowSDL();
25
-
26
-    /*!
27
-     * \brief Deconstructs an object of WindowSDL
28
-     */
29
-    virtual ~WindowSDL();
30
-
31
-    virtual void setSize(unsigned int width, unsigned int height);
32
-
33
-    virtual void setFullscreen(bool fullscreen);
34
-
35
-    virtual void setMousegrab(bool grab);
36
-
37
-    virtual int initialize();
22
+    static void setSize(glm::vec2 s);
23
+    static glm::vec2 getSize() { return size; }
38 24
 
39
-    virtual void eventHandling();
25
+    static void setFullscreen(bool f);
26
+    static bool getFullscreen() { return fullscreen; }
40 27
 
41
-    virtual void setTextInput(bool on);
28
+    static void setMousegrab(bool g);
29
+    static bool getMousegrab() { return mousegrab; }
42 30
 
43
-    virtual void swapBuffersGL();
31
+    static void setTextInput(bool t);
32
+    static bool getTextInput() { return textinput; }
44 33
 
45 34
   private:
46
-    SDL_Window* mWindow;      //!< This is the pointer to the SDL surface
47
-    SDL_GLContext mGLContext; //!< The OpenGL Context
48
-    SDL_GameController* controller;
35
+    static glm::vec2 size;
36
+    static bool fullscreen;
37
+    static bool mousegrab;
38
+    static bool textinput;
39
+    static SDL_Window* window;
40
+    static SDL_GLContext context;
41
+    static SDL_GameController* controller;
49 42
 };
50 43
 
51 44
 #endif

+ 69
- 21
src/Camera.cpp Bestand weergeven

@@ -6,6 +6,7 @@
6 6
  * \author xythobuz
7 7
  */
8 8
 
9
+#include <limits>
9 10
 #include <glm/gtc/epsilon.hpp>
10 11
 #include <glm/gtc/matrix_transform.hpp>
11 12
 #include <glm/gtx/quaternion.hpp>
@@ -15,21 +16,19 @@
15 16
 #include "system/Window.h"
16 17
 #include "Camera.h"
17 18
 
18
-#define NEAR 0
19
-#define FAR 1
20
-#define TOP 2
21
-#define BOTTOM 3
22
-#define LEFT 4
23
-#define RIGHT 5
19
+static bool equal(float a, float b) {
20
+    return glm::epsilonEqual(a, b, std::numeric_limits<float>::epsilon());
21
+}
24 22
 
25
-#define NTL 0
26
-#define NBL 1
27
-#define NBR 2
28
-#define NTR 3
29
-#define FTL 4
30
-#define FBL 5
31
-#define FBR 6
32
-#define FTR 7
23
+static bool equal(glm::vec2 a, float b) {
24
+    return equal(a.x, b) && equal(a.y, b);
25
+}
26
+
27
+static bool equal(glm::vec3 a, float b) {
28
+    return equal(a.x, b) && equal(a.y, b) && equal(a.z, b);
29
+}
30
+
31
+// ----------------------------------------------------------------------------
33 32
 
34 33
 const static float fov = 45.0f;
35 34
 const static float nearDist = 0.1f;
@@ -52,12 +51,14 @@ glm::mat4 Camera::view(1.0f);
52 51
 float Camera::rotationDeltaX = 0.75f;
53 52
 float Camera::rotationDeltaY = 0.75f;
54 53
 bool Camera::updateViewFrustum = true;
54
+bool Camera::dirty = true;
55 55
 
56 56
 void Camera::reset() {
57 57
     pos = glm::vec3(0.0f, 0.0f, 0.0f);
58 58
     quaternion = glm::quat(glm::vec3(0.0f, 0.0f, 0.0f));
59 59
     posSpeed = glm::vec3(0.0f, 0.0f, 0.0f);
60 60
     rotSpeed = glm::vec2(0.0f, 0.0f);
61
+    dirty = true;
61 62
     lastSize = glm::vec2(0.0f, 0.0f);
62 63
     projection = glm::mat4(1.0f);
63 64
     view = glm::mat4(1.0f);
@@ -80,21 +81,39 @@ void Camera::handleAction(ActionEvents action, bool isFinished) {
80 81
         posSpeed += upUnit * maxSpeed * factor;
81 82
     } else if (action == crouchAction) {
82 83
         posSpeed -= upUnit * maxSpeed * factor;
84
+    } else {
85
+        return;
83 86
     }
87
+
88
+    dirty = true;
84 89
 }
85 90
 
86 91
 void Camera::handleMouseMotion(int x, int y) {
87
-    if (x != 0)
88
-        quaternion = glm::quat(upUnit * rotationDeltaX * float(x)) * quaternion;
92
+    if (x != 0) {
93
+        quaternion = glm::quat(upUnit * (rotationDeltaX * x)) * quaternion;
94
+        dirty = true;
95
+    }
96
+
97
+    static int lastDir = 0;
98
+    if (y != 0) {
99
+        float a = glm::dot(upUnit, quaternion * upUnit);
100
+        if (((lastDir >= 0) && (y < 0)) || ((lastDir <= 0) && (y > 0)) || (a > 0.5f)) {
101
+            quaternion = glm::quat(quaternion * -rightUnit * (rotationDeltaY * y)) * quaternion;
102
+            dirty = true;
89 103
 
90
-    if (y != 0)
91
-        quaternion = glm::quat(quaternion * -rightUnit * rotationDeltaY * float(y)) * quaternion;
104
+            // TODO find better way to clamp Y rotation axis!
105
+            if (a > 0.5f)
106
+                lastDir = y;
107
+        }
108
+    }
92 109
 }
93 110
 
94 111
 void Camera::handleControllerAxis(float value, KeyboardButton axis) {
95 112
     if (glm::epsilonEqual(value, 0.0f, controllerDeadZone))
96 113
         value = 0.0f;
97 114
 
115
+    // TODO clamp Y rotation axis somehow...?
116
+
98 117
     if (axis == leftXAxis) {
99 118
         posSpeed.x = -maxSpeed * value;
100 119
     } else if (axis == leftYAxis) {
@@ -103,7 +122,11 @@ void Camera::handleControllerAxis(float value, KeyboardButton axis) {
103 122
         rotSpeed.x = controllerViewFactor * value;
104 123
     } else if (axis == rightYAxis) {
105 124
         rotSpeed.y = controllerViewFactor * value;
125
+    } else {
126
+        return;
106 127
     }
128
+
129
+    dirty = true;
107 130
 }
108 131
 
109 132
 void Camera::handleControllerButton(KeyboardButton button, bool released) {
@@ -119,11 +142,15 @@ void Camera::handleControllerButton(KeyboardButton button, bool released) {
119 142
         handleAction(leftAction, released);
120 143
     } else if (button == padRight) {
121 144
         handleAction(rightAction, released);
145
+    } else {
146
+        return;
122 147
     }
148
+
149
+    dirty = true;
123 150
 }
124 151
 
125 152
 bool Camera::update() {
126
-    glm::vec2 size(getWindow().getWidth(), getWindow().getHeight());
153
+    glm::vec2 size(Window::getSize());
127 154
 
128 155
     if (lastSize != size) {
129 156
         //! \fixme TODO instead of mirroring the Y axis in the shader, scale with -1 here
@@ -131,6 +158,9 @@ bool Camera::update() {
131 158
         lastSize = size;
132 159
     }
133 160
 
161
+    if ((!dirty) && equal(posSpeed, 0.0f) && equal(rotSpeed, 0.0f))
162
+        return false;
163
+
134 164
     float dT = getRunTime().getLastFrameTime();
135 165
     pos += quaternion * posSpeed * dT;
136 166
 
@@ -147,12 +177,14 @@ bool Camera::update() {
147 177
     if (updateViewFrustum)
148 178
         calculateFrustumPlanes();
149 179
 
180
+    dirty = false;
150 181
     return updateViewFrustum;
151 182
 }
152 183
 
153 184
 glm::vec2 Camera::getRotation() {
154
-    glm::vec3 euler = glm::eulerAngles(quaternion);
155
-    return glm::vec2(euler.y, euler.x);
185
+    float x = glm::dot(dirUnit, quaternion * dirUnit);
186
+    float y = glm::dot(upUnit, quaternion * upUnit);
187
+    return glm::vec2(x, y);
156 188
 }
157 189
 
158 190
 // ----------------------------------------------------------------------------
@@ -174,6 +206,22 @@ class FrustumPlane {
174 206
 
175 207
 // ----------------------------------------------------------------------------
176 208
 
209
+#define NEAR 0
210
+#define FAR 1
211
+#define TOP 2
212
+#define BOTTOM 3
213
+#define LEFT 4
214
+#define RIGHT 5
215
+
216
+#define NTL 0
217
+#define NBL 1
218
+#define NBR 2
219
+#define NTR 3
220
+#define FTL 4
221
+#define FBL 5
222
+#define FBR 6
223
+#define FTR 7
224
+
177 225
 static FrustumPlane planes[6];
178 226
 static glm::vec3 frustumColors[6] = {
179 227
     glm::vec3(1.0f, 0.0f, 0.0f), // NEAR, red

+ 4
- 4
src/Game.cpp Bestand weergeven

@@ -53,19 +53,19 @@ void Game::display() {
53 53
     if (getRunTime().getShowFPS()) {
54 54
         std::ostringstream s;
55 55
         s << getRunTime().getFPS() << "FPS";
56
-        Font::drawText(10, getWindow().getHeight() - 25, 0.6f, BLUE, s.str());
56
+        Font::drawText(10, Window::getSize().y - 25, 0.6f, BLUE, s.str());
57 57
 
58 58
         s.str("");
59 59
         s << "X: " << Camera::getPosition().x << " (" << Camera::getRotation().x << ")";
60
-        Font::drawText(10, getWindow().getHeight() - 70, 0.6f, BLUE, s.str());
60
+        Font::drawText(10, Window::getSize().y - 70, 0.6f, BLUE, s.str());
61 61
 
62 62
         s.str("");
63 63
         s << "Y: " << Camera::getPosition().y << " (" << Camera::getRotation().y << ")";
64
-        Font::drawText(10, getWindow().getHeight() - 55, 0.6f, BLUE, s.str());
64
+        Font::drawText(10, Window::getSize().y - 55, 0.6f, BLUE, s.str());
65 65
 
66 66
         s.str("");
67 67
         s << "Z: " << Camera::getPosition().z;
68
-        Font::drawText(10, getWindow().getHeight() - 40, 0.6f, BLUE, s.str());
68
+        Font::drawText(10, Window::getSize().y - 40, 0.6f, BLUE, s.str());
69 69
     }
70 70
 }
71 71
 

+ 6
- 6
src/Menu.cpp Bestand weergeven

@@ -87,7 +87,7 @@ bool Menu::handleMouseScrollDialog(int xrel, int yrel) {
87 87
 
88 88
 void Menu::displayDialog() {
89 89
     if (dialogText.length() > 0) {
90
-        unsigned int wMax = ((unsigned int)(::getWindow().getWidth() * 0.66f));
90
+        unsigned int wMax = ((unsigned int)(Window::getSize().x * 0.66f));
91 91
 
92 92
         unsigned int w0 = Font::widthText(1.0f, dialogText) + 20;
93 93
         if (w0 > wMax)
@@ -139,8 +139,8 @@ void Menu::displayDialog() {
139 139
             hOverlay = h0 + h1;
140 140
         }
141 141
 
142
-        unsigned int xOverlay = (::getWindow().getWidth() - wOverlay) / 2;
143
-        unsigned int yOverlay = (::getWindow().getHeight() - hOverlay) / 2;
142
+        unsigned int xOverlay = (Window::getSize().x - wOverlay) / 2;
143
+        unsigned int yOverlay = (Window::getSize().y - hOverlay) / 2;
144 144
 
145 145
         /*
146 146
         glColor4f(0.0f, 0.0f, 0.0f, 0.75f);
@@ -157,13 +157,13 @@ void Menu::displayDialog() {
157 157
                 Font::drawTextWrapped(xOverlay + 10 + w1, yOverlay + 10 + h0,
158 158
                                       1.0f, dialogState ? RED : BLUE, w2, dialogButton2);
159 159
             } else {
160
-                Font::drawTextWrapped((::getWindow().getWidth() - w1) / 2,
160
+                Font::drawTextWrapped((Window::getSize().x - w1) / 2,
161 161
                                       yOverlay + 10 + h0, 1.0f, dialogState ? BLUE : RED, w1, dialogButton1);
162
-                Font::drawTextWrapped((::getWindow().getWidth() - w2) / 2,
162
+                Font::drawTextWrapped((Window::getSize().x - w2) / 2,
163 163
                                       yOverlay + 10 + h0 + h1, 1.0f, dialogState ? RED : BLUE, w2, dialogButton2);
164 164
             }
165 165
         } else {
166
-            Font::drawTextWrapped((::getWindow().getWidth() - w1) / 2,
166
+            Font::drawTextWrapped((Window::getSize().x - w1) / 2,
167 167
                                   yOverlay + 10 + h0, 1.0f, RED, w1, dialogButton1);
168 168
         }
169 169
     }

+ 7
- 7
src/MenuFolder.cpp Bestand weergeven

@@ -77,18 +77,18 @@ void MenuFolder::display() {
77 77
     if (!visible)
78 78
         return;
79 79
 
80
-    Font::drawTextCentered(0, 10, 1.2f, BLUE, ::getWindow().getWidth(), VERSION);
80
+    Font::drawTextCentered(0, 10, 1.2f, BLUE, Window::getSize().x, VERSION);
81 81
 
82 82
     // Draw half-transparent overlay
83 83
     glm::vec4 color(0.0f, 0.0f, 0.0f, 0.75f);
84
-    Render::drawTexture(0.0f, 0.0f, getWindow().getWidth(), getWindow().getHeight(),
84
+    Render::drawTexture(0.0f, 0.0f, Window::getSize().x, Window::getSize().y,
85 85
                         color, TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
86 86
 
87 87
     // Draw heading
88
-    Font::drawTextCentered(0, 10, 1.2f, BLUE, getWindow().getWidth(), VERSION);
88
+    Font::drawTextCentered(0, 10, 1.2f, BLUE, Window::getSize().x, VERSION);
89 89
 
90 90
     // Estimate displayable number of items
91
-    int items = (getWindow().getHeight() - 60) / 25;
91
+    int items = (Window::getSize().y - 60) / 25;
92 92
 
93 93
     // Print list of "..", folders, files
94 94
     for (long i = mMin; (i < (mMin + items))
@@ -137,7 +137,7 @@ void MenuFolder::handleKeyboard(KeyboardButton key, bool pressed) {
137 137
         return;
138 138
 
139 139
     assert(mapFolder != nullptr);
140
-    int items = (::getWindow().getHeight() - 60) / 25;
140
+    int items = (Window::getSize().y - 60) / 25;
141 141
 
142 142
     if (key == upKey) {
143 143
         if (mCursor > 0)
@@ -168,7 +168,7 @@ void MenuFolder::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton
168 168
     if (handleMouseClickDialog(x, y, button, released))
169 169
         return;
170 170
 
171
-    int items = (::getWindow().getHeight() - 60) / 25;
171
+    int items = (Window::getSize().y - 60) / 25;
172 172
 
173 173
     if (released || (button != leftmouseKey))
174 174
         return;
@@ -189,7 +189,7 @@ void MenuFolder::handleMouseScroll(int xrel, int yrel) {
189 189
 
190 190
     assert((xrel != 0) || (yrel != 0));
191 191
     assert(mapFolder != nullptr);
192
-    int items = (::getWindow().getHeight() - 60) / 25;
192
+    int items = (Window::getSize().y - 60) / 25;
193 193
 
194 194
     if ((mapFolder->folderCount() + mapFolder->fileCount() + 1) > items) {
195 195
         if (yrel < 0) {

+ 4
- 4
src/Render.cpp Bestand weergeven

@@ -53,7 +53,7 @@ void Render::display() {
53 53
 
54 54
     if (mode == RenderMode::LoadScreen) {
55 55
         glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f);
56
-        drawTexture(0.0f, 0.0f, getWindow().getWidth(), getWindow().getHeight(),
56
+        drawTexture(0.0f, 0.0f, Window::getSize().x, Window::getSize().y,
57 57
                     color, TEXTURE_SPLASH, TextureManager::TextureStorage::SYSTEM);
58 58
         return;
59 59
     } else if (mode == RenderMode::Disabled) {
@@ -132,7 +132,7 @@ void Render::buildRoomList(int room) {
132 132
 
133 133
 void Render::screenShot(const char* filenameBase) {
134 134
     /*
135
-    int sz = getWindow().getWidth() * getWindow().getHeight();
135
+    int sz = Window::getSize().x * Window::getSize().y;
136 136
     unsigned char* image = new unsigned char[sz * 3];
137 137
     static int count = 0;
138 138
     bool done = false;
@@ -152,9 +152,9 @@ void Render::screenShot(const char* filenameBase) {
152 152
         }
153 153
     }
154 154
 
155
-    glReadPixels(0, 0, getWindow().getWidth(), getWindow().getHeight(), GL_BGR_EXT, GL_UNSIGNED_BYTE,
155
+    glReadPixels(0, 0, Window::getSize().x, Window::getSize().y, GL_BGR_EXT, GL_UNSIGNED_BYTE,
156 156
                  image);
157
-    tgaSave(filename.str().c_str(), image, getWindow().getWidth(), getWindow().getHeight(), 0);
157
+    tgaSave(filename.str().c_str(), image, Window::getSize().x, Window::getSize().y, 0);
158 158
     delete [] image;
159 159
     */
160 160
 }

+ 1
- 1
src/Room.cpp Bestand weergeven

@@ -25,7 +25,7 @@ void Room::display(glm::mat4 view, glm::mat4 projection) {
25 25
     }
26 26
 
27 27
     if (Render::getMode() == RenderMode::Wireframe)
28
-        bbox->display(projection * view, glm::vec3(0.0f, 1.0f, 0.0f));
28
+        bbox->display(projection * view);
29 29
 }
30 30
 
31 31
 bool Room::isWall(unsigned long sector) {

+ 13
- 3
src/RoomData.cpp Bestand weergeven

@@ -13,14 +13,14 @@
13 13
 #include "system/Window.h"
14 14
 #include "RoomData.h"
15 15
 
16
-void BoundingBox::display(glm::mat4 VP, glm::vec3 color) {
16
+void BoundingBox::display(glm::mat4 VP) {
17 17
     std::vector<glm::vec3> verts;
18 18
     std::vector<glm::vec3> cols;
19 19
     std::vector<unsigned short> inds;
20 20
 
21 21
     for (int i = 0; i < 8; i++) {
22 22
         verts.push_back(corner[i]);
23
-        cols.push_back(color);
23
+        cols.push_back(glm::vec3(0.0f, 1.0f, 0.0f));
24 24
     }
25 25
 
26 26
     inds.push_back(0);
@@ -40,7 +40,17 @@ void BoundingBox::display(glm::mat4 VP, glm::vec3 color) {
40 40
     inds.push_back(5);
41 41
     inds.push_back(2);
42 42
 
43
-    Window::drawLinesGL(verts, cols, inds, VP);
43
+    Window::drawGL(verts, cols, inds, VP, GL_LINE_STRIP);
44
+
45
+    cols.clear();
46
+    inds.clear();
47
+
48
+    for (int i = 0; i < 8; i++) {
49
+        cols.push_back(glm::vec3(1.0f, 0.0f, 0.0f));
50
+        inds.push_back(i);
51
+    }
52
+
53
+    Window::drawGL(verts, cols, inds, VP, GL_POINTS);
44 54
 }
45 55
 
46 56
 // ----------------------------------------------------------------------------

+ 22
- 41
src/UI.cpp Bestand weergeven

@@ -44,7 +44,7 @@ int UI::initialize() {
44 44
     logFilename = getRunTime().getBaseDir() + "/imgui_log.txt";
45 45
 
46 46
     ImGuiIO& io = ImGui::GetIO();
47
-    io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
47
+    io.DisplaySize = ImVec2(Window::getSize().x, Window::getSize().y);
48 48
     io.DeltaTime = 1.0f / 60.0f;
49 49
 
50 50
     io.IniFilename = iniFilename.c_str();
@@ -90,7 +90,7 @@ int UI::initialize() {
90 90
 
91 91
 void UI::eventsFinished() {
92 92
     ImGuiIO& io = ImGui::GetIO();
93
-    io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
93
+    io.DisplaySize = ImVec2(Window::getSize().x, Window::getSize().y);
94 94
 
95 95
     static unsigned long lastTime = 0;
96 96
     io.DeltaTime = ((float)(systemTimerGet() - lastTime)) / 1000.0f;
@@ -175,12 +175,12 @@ void UI::eventsFinished() {
175 175
         visible = false;
176 176
     }
177 177
 
178
-    if (getWindow().getTextInput() != visible)
179
-        getWindow().setTextInput(visible);
178
+    if (Window::getTextInput() != visible)
179
+        Window::setTextInput(visible);
180 180
 
181 181
     bool input = !(visible || getMenu().isVisible());
182
-    if (getWindow().getMousegrab() != input)
183
-        getWindow().setMousegrab(input);
182
+    if (Window::getMousegrab() != input)
183
+        Window::setMousegrab(input);
184 184
 
185 185
     io.MouseWheel = 0;
186 186
 }
@@ -229,9 +229,9 @@ void UI::display() {
229 229
                 Sound::setEnabled(sound);
230 230
             }
231 231
             ImGui::SameLine();
232
-            bool fullscreen = getWindow().getFullscreen();
232
+            bool fullscreen = Window::getFullscreen();
233 233
             if (ImGui::Checkbox("Fullscreen##runtime", &fullscreen)) {
234
-                getWindow().setFullscreen(fullscreen);
234
+                Window::setFullscreen(fullscreen);
235 235
             }
236 236
 
237 237
             bool updateViewFrustum = Camera::getUpdateViewFrustum();
@@ -254,17 +254,17 @@ void UI::display() {
254 254
                 Sound::setVolume(vol);
255 255
             }
256 256
 
257
-            int w = getWindow().getWidth();
257
+            int w = Window::getSize().x;
258 258
             if (ImGui::InputInt("Width##runtime", &w, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
259 259
                 if (w < 1)
260 260
                     w = 1;
261
-                getWindow().setSize(w, getWindow().getHeight());
261
+                Window::setSize(glm::vec2(w, Window::getSize().y));
262 262
             }
263
-            int h = getWindow().getHeight();
263
+            int h = Window::getSize().y;
264 264
             if (ImGui::InputInt("Height##runtime", &h, 10, 100, ImGuiInputTextFlags_EnterReturnsTrue)) {
265 265
                 if (h < 1)
266 266
                     h = 1;
267
-                getWindow().setSize(getWindow().getWidth(), h);
267
+                Window::setSize(glm::vec2(Window::getSize().x, h));
268 268
             }
269 269
 
270 270
             static int fr = 0;
@@ -677,23 +677,11 @@ void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
677 677
     glDisable(GL_DEPTH_TEST);
678 678
 
679 679
     Window::imguiShader.use();
680
-
681
-    glUniform2f(Window::imguiShader.getUniform(0), getWindow().getWidth(), getWindow().getHeight());
682
-
683
-    getTextureManager().bindTextureId(fontTex, TextureManager::TextureStorage::SYSTEM, 0);
684
-    glUniform1i(Window::imguiShader.getUniform(1), 0);
685
-
686
-    glEnableVertexAttribArray(0); // Vertices
687
-    glBindBuffer(GL_ARRAY_BUFFER, Window::imguiShader.getBuffer(0));
688
-    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
689
-
690
-    glEnableVertexAttribArray(1); // UVs
691
-    glBindBuffer(GL_ARRAY_BUFFER, Window::imguiShader.getBuffer(1));
692
-    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
693
-
694
-    glEnableVertexAttribArray(2); // Colors
695
-    glBindBuffer(GL_ARRAY_BUFFER, Window::imguiShader.getBuffer(2));
696
-    glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
680
+    Window::imguiShader.loadUniform(0, Window::getSize());
681
+    Window::imguiShader.loadUniform(1, fontTex, TextureManager::TextureStorage::SYSTEM);
682
+    Window::imguiShader.bindBuffer(0, 0, 2);
683
+    Window::imguiShader.bindBuffer(1, 1, 2);
684
+    Window::imguiShader.bindBuffer(2, 2, 4);
697 685
 
698 686
     std::vector<glm::vec2> vertices;
699 687
     std::vector<glm::vec2> uvs;
@@ -724,17 +712,12 @@ void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
724 712
 
725 713
             offset += commands[n].vtx_count;
726 714
 
727
-            glBindBuffer(GL_ARRAY_BUFFER, Window::imguiShader.getBuffer(0));
728
-            glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec2), &vertices[0], GL_STATIC_DRAW);
729
-
730
-            glBindBuffer(GL_ARRAY_BUFFER, Window::imguiShader.getBuffer(1));
731
-            glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
732
-
733
-            glBindBuffer(GL_ARRAY_BUFFER, Window::imguiShader.getBuffer(2));
734
-            glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec4), &colors[0], GL_STATIC_DRAW);
715
+            Window::imguiShader.bufferData(0, vertices);
716
+            Window::imguiShader.bufferData(1, uvs);
717
+            Window::imguiShader.bufferData(2, colors);
735 718
 
736 719
             glScissor(commands[n].clip_rect.x,
737
-                      getWindow().getHeight() - commands[n].clip_rect.w,
720
+                      Window::getSize().y - commands[n].clip_rect.w,
738 721
                       commands[n].clip_rect.z - commands[n].clip_rect.x,
739 722
                       commands[n].clip_rect.w - commands[n].clip_rect.y);
740 723
 
@@ -746,9 +729,7 @@ void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
746 729
         }
747 730
     }
748 731
 
749
-    glDisableVertexAttribArray(0);
750
-    glDisableVertexAttribArray(1);
751
-    glDisableVertexAttribArray(2);
732
+    Window::imguiShader.disableAttribs();
752 733
 
753 734
     glEnable(GL_DEPTH_TEST);
754 735
     glDisable(GL_SCISSOR_TEST);

+ 4
- 4
src/commands/CommandSet.cpp Bestand weergeven

@@ -77,14 +77,14 @@ int CommandSet::execute(std::istream& args) {
77 77
             getLog() << "set-size-Error: Invalid value(s)" << Log::endl;
78 78
             return -2;
79 79
         }
80
-        getWindow().setSize(w, h);
80
+        Window::setSize(glm::vec2(w, h));
81 81
     } else if (var.compare("fullscreen") == 0) {
82 82
         bool fullscreen = false;
83 83
         if (!(args >> fullscreen)) {
84 84
             getLog() << "set-fullscreen-Error: Invalid value" << Log::endl;
85 85
             return -3;
86 86
         }
87
-        getWindow().setFullscreen(fullscreen);
87
+        Window::setFullscreen(fullscreen);
88 88
     } else if (var.compare("audio") == 0) {
89 89
         bool audio = false;
90 90
         if (!(args >> audio)) {
@@ -181,9 +181,9 @@ int CommandGet::execute(std::istream& args) {
181 181
     args >> var;
182 182
 
183 183
     if (var.compare("size") == 0) {
184
-        getLog() << getWindow().getWidth() << " " << getWindow().getHeight() << Log::endl;
184
+        getLog() << Window::getSize() << Log::endl;
185 185
     } else if (var.compare("fullscreen") == 0) {
186
-        getLog() << getWindow().getFullscreen() << Log::endl;
186
+        getLog() << Window::getFullscreen() << Log::endl;
187 187
     } else if (var.compare("audio") == 0) {
188 188
         getLog() << Sound::getEnabled() << Log::endl;
189 189
     } else if (var.compare("volume") == 0) {

+ 11
- 37
src/main.cpp Bestand weergeven

@@ -28,14 +28,6 @@
28 28
 #include "system/Sound.h"
29 29
 #include "system/Window.h"
30 30
 
31
-#ifdef USING_SDL
32
-#include "system/WindowSDL.h"
33
-#elif defined(USING_GLFW)
34
-#include "system/WindowGLFW.h"
35
-#else
36
-#error No Windowing Library selected!
37
-#endif
38
-
39 31
 static std::string configFileToUse;
40 32
 
41 33
 static std::shared_ptr<Game> gGame;
@@ -43,7 +35,6 @@ static std::shared_ptr<Log> gLog;
43 35
 static std::shared_ptr<MenuFolder> gMenu;
44 36
 static std::shared_ptr<RunTime> gRunTime;
45 37
 static std::shared_ptr<TextureManager> gTextureManager;
46
-static std::shared_ptr<Window> gWindow;
47 38
 static std::shared_ptr<World> gWorld;
48 39
 
49 40
 Game& getGame() {
@@ -66,10 +57,6 @@ TextureManager& getTextureManager() {
66 57
     return *gTextureManager;
67 58
 }
68 59
 
69
-Window& getWindow() {
70
-    return *gWindow;
71
-}
72
-
73 60
 World& getWorld() {
74 61
     return *gWorld;
75 62
 }
@@ -93,35 +80,22 @@ int main(int argc, char* argv[]) {
93 80
     gTextureManager.reset(new TextureManager());
94 81
     gWorld.reset(new World());
95 82
 
96
-#ifdef USING_SDL
97
-    gWindow.reset(new WindowSDL());
98
-#elif defined(USING_GLFW)
99
-    gWindow.reset(new WindowGLFW());
100
-#endif
101
-
102 83
     Command::fillCommandList();
103 84
 
104 85
     getLog() << "Initializing " << VERSION << Log::endl;
105 86
 
106 87
     // Initialize Windowing
107
-    int error = getWindow().initialize();
88
+    int error = Window::initialize();
108 89
     if (error != 0) {
109
-        std::cout << "Could not initialize Window (" << error << ")!" << std::endl;
90
+        std::cout << "Could not initialize Window/GL (" << error << ")!" << std::endl;
110 91
         return -1;
111 92
     }
112 93
 
113
-    // Initialize OpenGL
114
-    error = getWindow().initializeGL();
115
-    if (error != 0) {
116
-        std::cout << "Could not initialize OpenGL (" << error << ")!" << std::endl;
117
-        return -2;
118
-    }
119
-
120 94
     // Initialize Texture Manager
121 95
     error = getTextureManager().initialize();
122 96
     if (error != 0) {
123 97
         std::cout << "Could not initialize TextureManager (" << error << ")!" << std::endl;
124
-        return -3;
98
+        return -2;
125 99
     }
126 100
 
127 101
     if (configFileToUse == "") {
@@ -141,35 +115,35 @@ int main(int argc, char* argv[]) {
141 115
     error = getTextureManager().initializeSplash();
142 116
     if (error != 0) {
143 117
         std::cout << "Coult not load Splash Texture (" << error << ")!" << std::endl;
144
-        return -4;
118
+        return -3;
145 119
     }
146 120
 
147 121
     // Initialize Sound
148 122
     error = Sound::initialize();
149 123
     if (error != 0) {
150 124
         std::cout << "Could not initialize Sound (" << error << ")!" << std::endl;
151
-        return -5;
125
+        return -4;
152 126
     }
153 127
 
154 128
     // Initialize Menu
155 129
     error = getMenu().initialize();
156 130
     if (error != 0) {
157 131
         std::cout << "Could not initialize Menu (" << error << ")!" << std::endl;
158
-        return -6;
132
+        return -5;
159 133
     }
160 134
 
161 135
     // Initialize Debug UI
162 136
     error = UI::initialize();
163 137
     if (error != 0) {
164 138
         std::cout << "Could not initialize Debug UI (" << error << ")!" << std::endl;
165
-        return -7;
139
+        return -6;
166 140
     }
167 141
 
168 142
     // Initialize Game Engine
169 143
     error = getGame().initialize();
170 144
     if (error != 0) {
171 145
         std::cout << "Could not initialize Game (" << error << ")!" << std::endl;
172
-        return -8;
146
+        return -7;
173 147
     }
174 148
 
175 149
     getLog() << "Starting " << VERSION << Log::endl;
@@ -178,14 +152,14 @@ int main(int argc, char* argv[]) {
178 152
     getRunTime().setRunning(true);
179 153
 
180 154
     while (getRunTime().isRunning()) {
181
-        getWindow().eventHandling();
155
+        Window::eventHandling();
182 156
         renderFrame();
183 157
     }
184 158
 
185 159
     UI::shutdown();
186 160
     Font::shutdown();
187 161
     Sound::shutdown();
188
-    Window::shutdownGL();
162
+    Window::shutdown();
189 163
 
190 164
 #ifdef DEBUG
191 165
     std::cout << std::endl;
@@ -203,7 +177,7 @@ void renderFrame() {
203 177
     getGame().display();
204 178
     getMenu().display();
205 179
     UI::display();
206
-    getWindow().swapBuffersGL();
180
+    Window::swapBuffers();
207 181
     getRunTime().updateFPS();
208 182
 }
209 183
 

+ 1
- 0
src/system/CMakeLists.txt Bestand weergeven

@@ -2,6 +2,7 @@
2 2
 set (SYS_SRCS ${SYS_SRCS} "Font.cpp" "../../include/system/Font.h")
3 3
 set (SYS_SRCS ${SYS_SRCS} "FontImGui.cpp" "../../include/system/FontImGui.h")
4 4
 set (SYS_SRCS ${SYS_SRCS} "FontTRLE.cpp" "../../include/system/FontTRLE.h")
5
+set (SYS_SRCS ${SYS_SRCS} "Shader.cpp" "../../include/system/Shader.h")
5 6
 set (SYS_SRCS ${SYS_SRCS} "Sound.cpp" "../../include/system/Sound.h")
6 7
 set (SYS_SRCS ${SYS_SRCS} "Window.cpp" "../../include/system/Window.h")
7 8
 

+ 1
- 1
src/system/Font.cpp Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file src/Font.cpp
2
+ * \file src/system/Font.cpp
3 3
  * \brief Font implementation
4 4
  *
5 5
  * \author xythobuz

+ 1
- 1
src/system/FontImGui.cpp Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file src/FontImGui.cpp
2
+ * \file src/system/FontImGui.cpp
3 3
  * \brief Default Font implementation
4 4
  *
5 5
  * \author xythobuz

+ 1
- 1
src/system/FontSDL.cpp Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file src/FontSDL.cpp
2
+ * \file src/system/FontSDL.cpp
3 3
  * \brief SDL Font implementation
4 4
  *
5 5
  * \author xythobuz

+ 1
- 1
src/system/FontTRLE.cpp Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file src/FontTRLE.cpp
2
+ * \file src/system/FontTRLE.cpp
3 3
  * \brief SDL Font implementation
4 4
  *
5 5
  * \author xythobuz

+ 167
- 0
src/system/Shader.cpp Bestand weergeven

@@ -0,0 +1,167 @@
1
+/*!
2
+ * \file src/system/Shader.cpp
3
+ * \brief OpenGL Shader implementation
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "global.h"
9
+#include "Log.h"
10
+#include "TextureManager.h"
11
+#include "system/Shader.h"
12
+
13
+Shader::~Shader() {
14
+    if (programID >= 0)
15
+        glDeleteProgram(programID);
16
+
17
+    if (!buffers.empty())
18
+        glDeleteBuffers(buffers.size(), &buffers[0]);
19
+}
20
+
21
+int Shader::addUniform(const char* name) {
22
+    assert(programID >= 0);
23
+    int r = glGetUniformLocation(programID, name);
24
+    if (r < 0) {
25
+        getLog() << "Can't find GLSL Uniform \"" << name << "\"!" << Log::endl;
26
+        return -1;
27
+    }
28
+    uniforms.push_back(r);
29
+    return uniforms.size() - 1;
30
+}
31
+
32
+unsigned int Shader::getUniform(int n) {
33
+    assert(n >= 0);
34
+    assert(n < uniforms.size());
35
+    return uniforms.at(n);
36
+}
37
+
38
+void Shader::addBuffer(int n) {
39
+    int s = buffers.size();
40
+    for (int i = 0; i < n; i++)
41
+        buffers.push_back(0);
42
+    glGenBuffers(n, &buffers[s]);
43
+}
44
+
45
+unsigned int Shader::getBuffer(int n) {
46
+    assert(n >= 0);
47
+    assert(n < buffers.size());
48
+    return buffers.at(n);
49
+}
50
+
51
+void Shader::loadUniform(int uni, glm::vec2 vec) {
52
+    glUniform2f(getUniform(uni), vec.x, vec.y);
53
+}
54
+
55
+void Shader::loadUniform(int uni, glm::vec4 vec) {
56
+    glUniform4f(getUniform(uni), vec.r, vec.g, vec.b, vec.a);
57
+}
58
+
59
+void Shader::loadUniform(int uni, glm::mat4 mat) {
60
+    glUniformMatrix4fv(getUniform(uni), 1, GL_FALSE, &mat[0][0]);
61
+}
62
+
63
+void Shader::loadUniform(int uni, int texture, TextureManager::TextureStorage store, int slot) {
64
+    getTextureManager().bindTextureId(texture, store, slot);
65
+    glUniform1i(getUniform(uni), slot);
66
+}
67
+
68
+void Shader::bindBuffer(int buff) {
69
+    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, getBuffer(buff));
70
+}
71
+
72
+void Shader::bindBuffer(int buff, int location, int size) {
73
+    glEnableVertexAttribArray(location);
74
+    glBindBuffer(GL_ARRAY_BUFFER, getBuffer(buff));
75
+    glVertexAttribPointer(location, size, GL_FLOAT, GL_FALSE, 0, nullptr);
76
+
77
+    while (attribs.size() <= location)
78
+        attribs.push_back(false);
79
+    attribs.at(location) = true;
80
+}
81
+
82
+void Shader::disableAttribs() {
83
+    for (int i = 0; i < attribs.size(); i++) {
84
+        if (attribs.at(i)) {
85
+            glDisableVertexAttribArray(i);
86
+            attribs.at(i) = false;
87
+        }
88
+    }
89
+}
90
+
91
+void Shader::use() {
92
+    assert(programID >= 0);
93
+    glUseProgram(programID);
94
+}
95
+
96
+int Shader::compile(const char* vertex, const char* fragment) {
97
+    assert(vertex != nullptr);
98
+    assert(fragment != nullptr);
99
+
100
+    GLuint vertexID = glCreateShader(GL_VERTEX_SHADER);
101
+    GLuint fragmentID = glCreateShader(GL_FRAGMENT_SHADER);
102
+
103
+    GLint result = GL_FALSE;
104
+    GLint logLength = 0;
105
+
106
+    // Compile vertex shader
107
+    glShaderSource(vertexID, 1, &vertex, nullptr);
108
+    glCompileShader(vertexID);
109
+
110
+    // Check vertex shader
111
+    glGetShaderiv(vertexID, GL_COMPILE_STATUS, &result);
112
+    glGetShaderiv(vertexID, GL_INFO_LOG_LENGTH, &logLength);
113
+    if (logLength > 0) {
114
+        std::vector<char> message(logLength + 1);
115
+        glGetShaderInfoLog(vertexID, logLength, nullptr, &message[0]);
116
+        if (result != GL_TRUE)
117
+            getLog() << "Vertex Shader compilation error:" << Log::endl;
118
+        getLog() << &message[0] << Log::endl;
119
+        glDeleteShader(vertexID);
120
+        glDeleteShader(fragmentID);
121
+        return -1;
122
+    }
123
+
124
+    // Compile fragment shader
125
+    glShaderSource(fragmentID, 1, &fragment, nullptr);
126
+    glCompileShader(fragmentID);
127
+
128
+    // Check fragment shader
129
+    glGetShaderiv(fragmentID, GL_COMPILE_STATUS, &result);
130
+    glGetShaderiv(fragmentID, GL_INFO_LOG_LENGTH, &logLength);
131
+    if (logLength > 0) {
132
+        std::vector<char> message(logLength + 1);
133
+        glGetShaderInfoLog(fragmentID, logLength, nullptr, &message[0]);
134
+        if (result != GL_TRUE)
135
+            getLog() << "Fragment Shader compilation error:" << Log::endl;
136
+        getLog() << &message[0] << Log::endl;
137
+        glDeleteShader(vertexID);
138
+        glDeleteShader(fragmentID);
139
+        return -2;
140
+    }
141
+
142
+    // Link both shaders
143
+    programID = glCreateProgram();
144
+    glAttachShader(programID, vertexID);
145
+    glAttachShader(programID, fragmentID);
146
+    glLinkProgram(programID);
147
+
148
+    // Check resulting program
149
+    glGetProgramiv(programID, GL_LINK_STATUS, &result);
150
+    glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &logLength);
151
+    if (logLength > 0) {
152
+        std::vector<char> message(logLength + 1);
153
+        glGetProgramInfoLog(programID, logLength, nullptr, &message[0]);
154
+        if (result != GL_TRUE)
155
+            getLog() << "Shader link error:" << Log::endl;
156
+        getLog() << &message[0] << Log::endl;
157
+        glDeleteShader(vertexID);
158
+        glDeleteShader(fragmentID);
159
+        glDeleteProgram(programID);
160
+        return -3;
161
+    }
162
+
163
+    glDeleteShader(vertexID);
164
+    glDeleteShader(fragmentID);
165
+    return programID;
166
+}
167
+

+ 141
- 244
src/system/Window.cpp Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file src/Window.cpp
2
+ * \file src/system/Window.cpp
3 3
  * \brief windowing implementation
4 4
  *
5 5
  * \author xythobuz
@@ -7,31 +7,132 @@
7 7
 
8 8
 #include "global.h"
9 9
 #include "Log.h"
10
-#include "TextureManager.h"
11 10
 #include "utils/strings.h"
12 11
 #include "system/Window.h"
13 12
 
14
-unsigned int Window::getWidth() {
15
-    return mWidth;
13
+#ifdef USING_SDL
14
+#include "system/WindowSDL.h"
15
+#elif defined(USING_GLFW)
16
+#include "system/WindowGLFW.h"
17
+#else
18
+#error "No windowing library selected!"
19
+#endif
20
+
21
+int Window::initialize() {
22
+    int res = -1;
23
+#ifdef USING_SDL
24
+    res = WindowSDL::initialize();
25
+#elif defined(USING_GLFW)
26
+    res = WindowGLFW::initialize();
27
+#endif
28
+
29
+    initializeGL();
30
+    return res;
16 31
 }
17 32
 
18
-unsigned int Window::getHeight() {
19
-    return mHeight;
33
+void Window::eventHandling() {
34
+#ifdef USING_SDL
35
+    WindowSDL::eventHandling();
36
+#elif defined(USING_GLFW)
37
+    WindowGLFW::eventHandling();
38
+#endif
39
+}
40
+
41
+void Window::swapBuffers() {
42
+#ifdef USING_SDL
43
+    WindowSDL::swapBuffers();
44
+#elif defined(USING_GLFW)
45
+    WindowGLFW::swapBuffers();
46
+#endif
47
+}
48
+
49
+void Window::shutdown() {
50
+    shutdownGL();
51
+
52
+#ifdef USING_SDL
53
+    WindowSDL::shutdown();
54
+#elif defined(USING_GLFW)
55
+    WindowGLFW::shutdown();
56
+#endif
57
+}
58
+
59
+void Window::setSize(glm::vec2 s) {
60
+#ifdef USING_SDL
61
+    WindowSDL::setSize(s);
62
+#elif defined(USING_GLFW)
63
+    WindowGLFW::setSize(s);
64
+#endif
65
+}
66
+
67
+glm::vec2 Window::getSize() {
68
+    glm::vec2 ret(-1, -1);
69
+#ifdef USING_SDL
70
+    ret = WindowSDL::getSize();
71
+#elif defined(USING_GLFW)
72
+    ret = WindowGLFW::getSize();
73
+#endif
74
+
75
+    return ret;
76
+}
77
+
78
+void Window::setFullscreen(bool f) {
79
+#ifdef USING_SDL
80
+    WindowSDL::setFullscreen(f);
81
+#elif defined(USING_GLFW)
82
+    WindowGLFW::setFullscreen(f);
83
+#endif
20 84
 }
21 85
 
22 86
 bool Window::getFullscreen() {
23
-    return mFullscreen;
87
+    bool ret = false;
88
+#ifdef USING_SDL
89
+    ret = WindowSDL::getFullscreen();
90
+#elif defined(USING_GLFW)
91
+    ret = WindowGLFW::getFullscreen();
92
+#endif
93
+
94
+    return ret;
95
+}
96
+
97
+void Window::setMousegrab(bool g) {
98
+#ifdef USING_SDL
99
+    WindowSDL::setMousegrab(g);
100
+#elif defined(USING_GLFW)
101
+    WindowGLFW::setMousegrab(g);
102
+#endif
24 103
 }
25 104
 
26 105
 bool Window::getMousegrab() {
27
-    return mMousegrab;
106
+    bool ret = false;
107
+#ifdef USING_SDL
108
+    ret = WindowSDL::getMousegrab();
109
+#elif defined(USING_GLFW)
110
+    ret = WindowGLFW::getMousegrab();
111
+#endif
112
+
113
+    return ret;
114
+}
115
+
116
+void Window::setTextInput(bool t) {
117
+#ifdef USING_SDL
118
+    WindowSDL::setTextInput(t);
119
+#elif defined(USING_GLFW)
120
+    WindowGLFW::setTextInput(t);
121
+#endif
28 122
 }
29 123
 
30 124
 bool Window::getTextInput() {
31
-    return mTextInput;
125
+    bool ret = false;
126
+#ifdef USING_SDL
127
+    ret = WindowSDL::getTextInput();
128
+#elif defined(USING_GLFW)
129
+    ret = WindowGLFW::getTextInput();
130
+#endif
131
+
132
+    return ret;
32 133
 }
33 134
 
34
-// ----------------------------------------------------------------------------
135
+// --------------------------------------
35 136
 
36 137
 Shader Window::textShader;
37 138
 Shader Window::imguiShader;
@@ -60,6 +161,8 @@ int Window::initializeGL() {
60 161
     // Set up culling
61 162
     //glEnable(GL_CULL_FACE); //! \todo Transparency?
62 163
 
164
+    glPointSize(5.0f);
165
+
63 166
     if (textShader.compile(textShaderVertex, textShaderFragment) < 0)
64 167
         return -1;
65 168
     if (textShader.addUniform("screen") < 0)
@@ -102,44 +205,25 @@ void Window::shutdownGL() {
102 205
     glDeleteVertexArrays(1, &vertexArrayID);
103 206
 }
104 207
 
105
-void Window::resizeGL() {
106
-    // new matrix?
107
-}
108
-
109 208
 void Window::drawTextGL(std::vector<glm::vec2>& vertices, std::vector<glm::vec2>& uvs,
110 209
                         glm::vec4 color, unsigned int texture) {
111 210
     assert(vertices.size() == uvs.size());
112 211
     assert((vertices.size() % 3) == 0);
113 212
 
114
-    glBindBuffer(GL_ARRAY_BUFFER, textShader.getBuffer(0));
115
-    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec2), &vertices[0], GL_STATIC_DRAW);
116
-
117
-    glBindBuffer(GL_ARRAY_BUFFER, textShader.getBuffer(1));
118
-    glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
119
-
213
+    textShader.bufferData(0, vertices);
214
+    textShader.bufferData(1, uvs);
120 215
     textShader.use();
121
-
122
-    glUniform2f(textShader.getUniform(0), getWindow().getWidth(), getWindow().getHeight());
123
-
124
-    getTextureManager().bindTextureId(texture, TextureManager::TextureStorage::SYSTEM, 0);
125
-    glUniform1i(textShader.getUniform(1), 0);
126
-
127
-    glUniform4fv(textShader.getUniform(2), 1, &color.r);
128
-
129
-    glEnableVertexAttribArray(0); // Vertices
130
-    glBindBuffer(GL_ARRAY_BUFFER, textShader.getBuffer(0));
131
-    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
132
-
133
-    glEnableVertexAttribArray(1); // UVs
134
-    glBindBuffer(GL_ARRAY_BUFFER, textShader.getBuffer(1));
135
-    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
216
+    textShader.loadUniform(0, getSize());
217
+    textShader.loadUniform(1, texture, TextureManager::TextureStorage::SYSTEM);
218
+    textShader.loadUniform(2, color);
219
+    textShader.bindBuffer(0, 0, 2);
220
+    textShader.bindBuffer(1, 1, 2);
136 221
 
137 222
     glDisable(GL_DEPTH_TEST);
138 223
     glDrawArrays(GL_TRIANGLES, 0, vertices.size());
139 224
     glEnable(GL_DEPTH_TEST);
140 225
 
141
-    glDisableVertexAttribArray(0);
142
-    glDisableVertexAttribArray(1);
226
+    textShader.disableAttribs();
143 227
 }
144 228
 
145 229
 void Window::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
@@ -147,223 +231,36 @@ void Window::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uv
147 231
     assert(vertices.size() == uvs.size());
148 232
     assert((indices.size() % 3) == 0);
149 233
 
150
-    glBindBuffer(GL_ARRAY_BUFFER, textureShader.getBuffer(0));
151
-    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
152
-
153
-    glBindBuffer(GL_ARRAY_BUFFER, textureShader.getBuffer(1));
154
-    glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
155
-
156
-    glBindBuffer(GL_ARRAY_BUFFER, textureShader.getBuffer(2));
157
-    glBufferData(GL_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW);
158
-
234
+    textureShader.bufferData(0, vertices);
235
+    textureShader.bufferData(1, uvs);
236
+    textureShader.bufferData(2, indices);
159 237
     textureShader.use();
160
-
161
-    glUniformMatrix4fv(textureShader.getUniform(0), 1, GL_FALSE, &MVP[0][0]);
162
-
163
-    getTextureManager().bindTextureId(texture, TextureManager::TextureStorage::GAME, 0);
164
-    glUniform1i(textureShader.getUniform(1), 0);
165
-
166
-    glEnableVertexAttribArray(0);
167
-    glBindBuffer(GL_ARRAY_BUFFER, textureShader.getBuffer(0));
168
-    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
169
-
170
-    glEnableVertexAttribArray(1);
171
-    glBindBuffer(GL_ARRAY_BUFFER, textureShader.getBuffer(1));
172
-    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
173
-
174
-    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, textureShader.getBuffer(2));
175
-
238
+    textureShader.loadUniform(0, MVP);
239
+    textureShader.loadUniform(1, texture, TextureManager::TextureStorage::GAME);
240
+    textureShader.bindBuffer(0, 0, 3);
241
+    textureShader.bindBuffer(1, 1, 2);
242
+    textureShader.bindBuffer(2);
176 243
     glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, nullptr);
177
-
178
-    glDisableVertexAttribArray(0);
179
-    glDisableVertexAttribArray(1);
244
+    textureShader.disableAttribs();
180 245
 }
181 246
 
182 247
 void Window::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
183
-                    std::vector<unsigned short>& indices, glm::mat4 MVP) {
184
-    assert(vertices.size() == colors.size());
185
-    assert((indices.size() % 3) == 0);
186
-
187
-    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(0));
188
-    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
189
-
190
-    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(1));
191
-    glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), &colors[0], GL_STATIC_DRAW);
192
-
193
-    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(2));
194
-    glBufferData(GL_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW);
195
-
196
-    colorShader.use();
197
-
198
-    glUniformMatrix4fv(colorShader.getUniform(0), 1, GL_FALSE, &MVP[0][0]);
199
-
200
-    glEnableVertexAttribArray(0);
201
-    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(0));
202
-    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
203
-
204
-    glEnableVertexAttribArray(1);
205
-    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(1));
206
-    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
207
-
208
-    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, colorShader.getBuffer(2));
209
-
210
-    glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, nullptr);
211
-
212
-    glDisableVertexAttribArray(0);
213
-    glDisableVertexAttribArray(1);
214
-}
215
-
216
-void Window::drawLinesGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
217
-                         std::vector<unsigned short>& indices, glm::mat4 MVP) {
248
+                    std::vector<unsigned short>& indices, glm::mat4 MVP, int mode) {
218 249
     assert(vertices.size() == colors.size());
219 250
 
220
-    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(0));
221
-    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
222
-
223
-    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(1));
224
-    glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), &colors[0], GL_STATIC_DRAW);
225
-
226
-    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(2));
227
-    glBufferData(GL_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW);
228
-
251
+    colorShader.bufferData(0, vertices);
252
+    colorShader.bufferData(1, colors);
253
+    colorShader.bufferData(2, indices);
229 254
     colorShader.use();
230
-
231
-    glUniformMatrix4fv(colorShader.getUniform(0), 1, GL_FALSE, &MVP[0][0]);
232
-
233
-    glEnableVertexAttribArray(0);
234
-    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(0));
235
-    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
236
-
237
-    glEnableVertexAttribArray(1);
238
-    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(1));
239
-    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
240
-
241
-    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, colorShader.getBuffer(2));
242
-
243
-    glDrawElements(GL_LINE_STRIP, indices.size(), GL_UNSIGNED_SHORT, nullptr);
244
-
245
-    glDisableVertexAttribArray(0);
246
-    glDisableVertexAttribArray(1);
247
-}
248
-
249
-// ----------------------------------------------------------------------------
250
-
251
-Shader::~Shader() {
252
-    if (programID >= 0)
253
-        glDeleteProgram(programID);
254
-
255
-    if (!buffers.empty())
256
-        glDeleteBuffers(buffers.size(), &buffers[0]);
257
-}
258
-
259
-int Shader::addUniform(const char* name) {
260
-    assert(programID >= 0);
261
-    int r = glGetUniformLocation(programID, name);
262
-    if (r < 0) {
263
-        getLog() << "Can't find GLSL Uniform \"" << name << "\"!" << Log::endl;
264
-        return -1;
265
-    }
266
-    uniforms.push_back(r);
267
-    return uniforms.size() - 1;
255
+    colorShader.loadUniform(0, MVP);
256
+    colorShader.bindBuffer(0, 0, 3);
257
+    colorShader.bindBuffer(1, 1, 3);
258
+    colorShader.bindBuffer(2);
259
+    glDrawElements(mode, indices.size(), GL_UNSIGNED_SHORT, nullptr);
260
+    colorShader.disableAttribs();
268 261
 }
269 262
 
270
-unsigned int Shader::getUniform(int n) {
271
-    assert(n >= 0);
272
-    assert(n < uniforms.size());
273
-    return uniforms.at(n);
274
-}
275
-
276
-void Shader::addBuffer(int n) {
277
-    int s = buffers.size();
278
-    for (int i = 0; i < n; i++)
279
-        buffers.push_back(0);
280
-    glGenBuffers(n, &buffers[s]);
281
-}
282
-
283
-unsigned int Shader::getBuffer(int n) {
284
-    assert(n >= 0);
285
-    assert(n < buffers.size());
286
-    return buffers.at(n);
287
-}
288
-
289
-void Shader::use() {
290
-    assert(programID >= 0);
291
-    glUseProgram(programID);
292
-}
293
-
294
-int Shader::compile(const char* vertex, const char* fragment) {
295
-    assert(vertex != nullptr);
296
-    assert(fragment != nullptr);
297
-
298
-    GLuint vertexID = glCreateShader(GL_VERTEX_SHADER);
299
-    GLuint fragmentID = glCreateShader(GL_FRAGMENT_SHADER);
300
-
301
-    GLint result = GL_FALSE;
302
-    GLint logLength = 0;
303
-
304
-    // Compile vertex shader
305
-    glShaderSource(vertexID, 1, &vertex, nullptr);
306
-    glCompileShader(vertexID);
307
-
308
-    // Check vertex shader
309
-    glGetShaderiv(vertexID, GL_COMPILE_STATUS, &result);
310
-    glGetShaderiv(vertexID, GL_INFO_LOG_LENGTH, &logLength);
311
-    if (logLength > 0) {
312
-        std::vector<char> message(logLength + 1);
313
-        glGetShaderInfoLog(vertexID, logLength, nullptr, &message[0]);
314
-        if (result != GL_TRUE)
315
-            getLog() << "Vertex Shader compilation error:" << Log::endl;
316
-        getLog() << &message[0] << Log::endl;
317
-        glDeleteShader(vertexID);
318
-        glDeleteShader(fragmentID);
319
-        return -1;
320
-    }
321
-
322
-    // Compile fragment shader
323
-    glShaderSource(fragmentID, 1, &fragment, nullptr);
324
-    glCompileShader(fragmentID);
325
-
326
-    // Check fragment shader
327
-    glGetShaderiv(fragmentID, GL_COMPILE_STATUS, &result);
328
-    glGetShaderiv(fragmentID, GL_INFO_LOG_LENGTH, &logLength);
329
-    if (logLength > 0) {
330
-        std::vector<char> message(logLength + 1);
331
-        glGetShaderInfoLog(fragmentID, logLength, nullptr, &message[0]);
332
-        if (result != GL_TRUE)
333
-            getLog() << "Fragment Shader compilation error:" << Log::endl;
334
-        getLog() << &message[0] << Log::endl;
335
-        glDeleteShader(vertexID);
336
-        glDeleteShader(fragmentID);
337
-        return -2;
338
-    }
339
-
340
-    // Link both shaders
341
-    programID = glCreateProgram();
342
-    glAttachShader(programID, vertexID);
343
-    glAttachShader(programID, fragmentID);
344
-    glLinkProgram(programID);
345
-
346
-    // Check resulting program
347
-    glGetProgramiv(programID, GL_LINK_STATUS, &result);
348
-    glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &logLength);
349
-    if (logLength > 0) {
350
-        std::vector<char> message(logLength + 1);
351
-        glGetProgramInfoLog(programID, logLength, nullptr, &message[0]);
352
-        if (result != GL_TRUE)
353
-            getLog() << "Shader link error:" << Log::endl;
354
-        getLog() << &message[0] << Log::endl;
355
-        glDeleteShader(vertexID);
356
-        glDeleteShader(fragmentID);
357
-        glDeleteProgram(programID);
358
-        return -3;
359
-    }
360
-
361
-    glDeleteShader(vertexID);
362
-    glDeleteShader(fragmentID);
363
-    return programID;
364
-}
365
-
366
-// ----------------------------------------------------------------------------
263
+// --------------------------------------
367 264
 // *INDENT-OFF*
368 265
 
369 266
 const char* Window::textShaderVertex = R"!?!(

+ 52
- 73
src/system/WindowGLFW.cpp Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file src/WindowGLFW.cpp
2
+ * \file src/system/WindowGLFW.cpp
3 3
  * \brief GLFW windowing implementation
4 4
  *
5 5
  * \author xythobuz
@@ -14,32 +14,19 @@
14 14
 #include "utils/strings.h"
15 15
 #include "system/WindowGLFW.h"
16 16
 
17
-static int lastMouseX = 0;
18
-static int lastMouseY = 0;
19
-
20
-WindowGLFW::WindowGLFW() {
21
-    mInit = false;
22
-    mWidth = DEFAULT_WIDTH;
23
-    mHeight = DEFAULT_HEIGHT;
24
-    mFullscreen = false;
25
-    mMousegrab = false;
26
-    mTextInput = false;
27
-    mWindow = nullptr;
28
-}
29
-
30
-WindowGLFW::~WindowGLFW() {
31
-    if (mInit) {
32
-        if (mWindow) {
33
-            glfwDestroyWindow(mWindow);
34
-        }
35
-
36
-        glfwTerminate();
37
-    }
38
-}
17
+glm::vec2 WindowGLFW::size(DEFAULT_WIDTH, DEFAULT_HEIGHT);
18
+bool WindowGLFW::fullscreen = false;
19
+bool WindowGLFW::mousegrab = false;
20
+bool WindowGLFW::textinput = false;
21
+GLFWwindow* WindowGLFW::window = nullptr;
22
+int WindowGLFW::lastMouseX = 0;
23
+int WindowGLFW::lastMouseY = 0;
24
+bool WindowGLFW::modShift = false;
25
+bool WindowGLFW::modControl = false;
26
+bool WindowGLFW::modAlt = false;
27
+bool WindowGLFW::modSuper = false;
39 28
 
40 29
 int WindowGLFW::initialize() {
41
-    assert(mInit == false);
42
-
43 30
     glfwSetErrorCallback(WindowGLFW::errorCallback);
44 31
     if (!glfwInit()) {
45 32
         return -1;
@@ -51,77 +38,74 @@ int WindowGLFW::initialize() {
51 38
     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
52 39
     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
53 40
 
54
-    mWindow = glfwCreateWindow(mWidth, mHeight, VERSION,
55
-                               mFullscreen ? glfwGetPrimaryMonitor() : nullptr, nullptr);
56
-    if (!mWindow) {
41
+    window = glfwCreateWindow(size.x, size.y, VERSION,
42
+                              fullscreen ? glfwGetPrimaryMonitor() : nullptr, nullptr);
43
+    if (!window) {
57 44
         glfwTerminate();
58 45
         return -2;
59 46
     }
60 47
 
61
-    glfwMakeContextCurrent(mWindow);
48
+    glfwMakeContextCurrent(window);
62 49
 
63
-    glfwSetWindowSizeCallback(mWindow, WindowGLFW::sizeCallback);
64
-    glfwSetCursorPosCallback(mWindow, WindowGLFW::cursorCallback);
65
-    glfwSetKeyCallback(mWindow, WindowGLFW::keyCallback);
66
-    glfwSetMouseButtonCallback(mWindow, WindowGLFW::buttonCallback);
67
-    glfwSetScrollCallback(mWindow, WindowGLFW::scrollCallback);
50
+    glfwSetWindowSizeCallback(window, WindowGLFW::sizeCallback);
51
+    glfwSetCursorPosCallback(window, WindowGLFW::cursorCallback);
52
+    glfwSetKeyCallback(window, WindowGLFW::keyCallback);
53
+    glfwSetMouseButtonCallback(window, WindowGLFW::buttonCallback);
54
+    glfwSetScrollCallback(window, WindowGLFW::scrollCallback);
68 55
 
69
-    mInit = true;
70 56
     return 0;
71 57
 }
72 58
 
73 59
 void WindowGLFW::eventHandling() {
74
-    assert(mInit == true);
75
-
76 60
     glfwPollEvents();
77 61
 
78
-    if (glfwWindowShouldClose(mWindow)) {
62
+    if (glfwWindowShouldClose(window)) {
79 63
         getRunTime().setRunning(false);
80 64
     }
81 65
 
82 66
     UI::eventsFinished();
83 67
 }
84 68
 
85
-void WindowGLFW::setSize(unsigned int width, unsigned int height) {
86
-    assert(width > 0);
87
-    assert(height > 0);
69
+void WindowGLFW::swapBuffers() {
70
+    glfwSwapBuffers(window);
71
+}
88 72
 
89
-    if (mInit) {
90
-        if ((mWidth != width) || (mHeight != height)) {
91
-            glfwSetWindowSize(mWindow, width, height);
92
-            getWindow().resizeGL();
93
-        }
73
+void WindowGLFW::shutdown() {
74
+    if (window) {
75
+        glfwDestroyWindow(window);
76
+        glfwTerminate();
77
+        window = nullptr;
94 78
     }
95
-
96
-    mWidth = width;
97
-    mHeight = height;
98 79
 }
99 80
 
100
-void WindowGLFW::setFullscreen(bool fullscreen) {
101
-    mFullscreen = fullscreen;
81
+void WindowGLFW::setSize(glm::vec2 s) {
82
+    assert((s.x > 0) && (s.y > 0));
83
+    if (window) {
84
+        if ((size.x != s.x) || (size.y != s.y)) {
85
+            glfwSetWindowSize(window, s.x, s.y);
86
+        }
87
+    }
88
+    size = s;
89
+}
102 90
 
91
+void WindowGLFW::setFullscreen(bool f) {
92
+    fullscreen = f;
103 93
     //! \todo GLFW does not support toggling fullscreen?!
104 94
 }
105 95
 
106
-void WindowGLFW::setMousegrab(bool grab) {
107
-    mMousegrab = grab;
96
+void WindowGLFW::setMousegrab(bool g) {
97
+    mousegrab = g;
108 98
 
109
-    if (mInit == true) {
110
-        if (mMousegrab)
111
-            glfwSetInputMode(mWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
99
+    if (window) {
100
+        if (mousegrab)
101
+            glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
112 102
         else
113
-            glfwSetInputMode(mWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
103
+            glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
114 104
     }
115 105
 }
116 106
 
117
-void WindowGLFW::setTextInput(bool on) {
118
-    assert(mInit == true);
119
-    mTextInput = on;
120
-}
121
-
122
-void WindowGLFW::swapBuffersGL() {
123
-    assert(mInit == true);
124
-    glfwSwapBuffers(mWindow);
107
+void WindowGLFW::setTextInput(bool t) {
108
+    textinput = t;
125 109
 }
126 110
 
127 111
 void WindowGLFW::errorCallback(int error, const char* desc) {
@@ -129,7 +113,7 @@ void WindowGLFW::errorCallback(int error, const char* desc) {
129 113
 }
130 114
 
131 115
 void WindowGLFW::sizeCallback(GLFWwindow* w, int width, int height) {
132
-    getWindow().setSize(width, height);
116
+    size = glm::vec2(width, height);
133 117
 }
134 118
 
135 119
 void WindowGLFW::cursorCallback(GLFWwindow* w, double xpos, double ypos) {
@@ -140,11 +124,6 @@ void WindowGLFW::cursorCallback(GLFWwindow* w, double xpos, double ypos) {
140 124
     lastMouseY = ypos;
141 125
 }
142 126
 
143
-static bool modShift = false;
144
-static bool modControl = false;
145
-static bool modAlt = false;
146
-static bool modSuper = false;
147
-
148 127
 void WindowGLFW::keyCallback(GLFWwindow* w, int key, int scancode, int action, int mods) {
149 128
     if (((mods & GLFW_MOD_SHIFT) != 0) != modShift) {
150 129
         modShift = (mods & GLFW_MOD_SHIFT) != 0;
@@ -166,8 +145,8 @@ void WindowGLFW::keyCallback(GLFWwindow* w, int key, int scancode, int action, i
166 145
         UI::handleKeyboard(leftguiKey, modSuper);
167 146
     }
168 147
 
169
-    if (getWindow().getTextInput() && (action != GLFW_RELEASE)) {
170
-        //! \todo Handle text input properly!
148
+    if (textinput && (action != GLFW_RELEASE)) {
149
+        //! \todo GLFW does not support UTF8 text input?!
171 150
         if ((key >= '0') && (key <= '9')) {
172 151
             char s[2] = { (char)key, '\0' };
173 152
             UI::handleText(s, false);

+ 74
- 99
src/system/WindowSDL.cpp Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file src/WindowSDL.cpp
2
+ * \file src/system/WindowSDL.cpp
3 3
  * \brief SDL windowing implementation
4 4
  *
5 5
  * \author xythobuz
@@ -16,75 +16,22 @@
16 16
 
17 17
 #define SUBSYSTEMS_USED (SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER)
18 18
 
19
-WindowSDL::WindowSDL() {
20
-    mInit = false;
21
-    mWidth = DEFAULT_WIDTH;
22
-    mHeight = DEFAULT_HEIGHT;
23
-    mFullscreen = false;
24
-    mMousegrab = false;
25
-    mTextInput = false;
26
-    mWindow = nullptr;
27
-    mGLContext = nullptr;
28
-    controller = nullptr;
29
-}
30
-
31
-WindowSDL::~WindowSDL() {
32
-    if (mInit) {
33
-        if (controller)
34
-            SDL_GameControllerClose(controller);
35
-
36
-        SDL_QuitSubSystem(SUBSYSTEMS_USED);
37
-        SDL_Quit();
38
-    }
39
-}
40
-
41
-void WindowSDL::setSize(unsigned int width, unsigned int height) {
42
-    assert(width > 0);
43
-    assert(height > 0);
44
-
45
-    mWidth = width;
46
-    mHeight = height;
47
-
48
-    if (mInit == true) {
49
-        SDL_SetWindowSize(mWindow, mWidth, mHeight);
50
-        resizeGL();
51
-    }
52
-}
53
-
54
-void WindowSDL::setFullscreen(bool fullscreen) {
55
-    mFullscreen = fullscreen;
56
-
57
-    if (mInit == true) {
58
-        if (mFullscreen)
59
-            SDL_SetWindowFullscreen(mWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
60
-        else
61
-            SDL_SetWindowFullscreen(mWindow, 0);
62
-    }
63
-}
64
-
65
-void WindowSDL::setMousegrab(bool grab) {
66
-    mMousegrab = grab;
67
-
68
-    if (mInit == true) {
69
-        if (mMousegrab) {
70
-            SDL_SetRelativeMouseMode(SDL_TRUE);
71
-        } else {
72
-            SDL_SetRelativeMouseMode(SDL_FALSE);
73
-            SDL_ShowCursor(1);
74
-        }
75
-    }
76
-}
19
+glm::vec2 WindowSDL::size(DEFAULT_WIDTH, DEFAULT_HEIGHT);
20
+bool WindowSDL::fullscreen = false;
21
+bool WindowSDL::mousegrab = false;
22
+bool WindowSDL::textinput = false;
23
+SDL_Window* WindowSDL::window = nullptr;
24
+SDL_GLContext WindowSDL::context = nullptr;
25
+SDL_GameController* WindowSDL::controller = nullptr;
77 26
 
78 27
 int WindowSDL::initialize() {
79
-    assert(mInit == false);
80
-
81 28
     if (SDL_Init(SUBSYSTEMS_USED) != 0) {
82 29
         std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
83 30
         return -1;
84 31
     }
85 32
 
86 33
     int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
87
-    if (mFullscreen)
34
+    if (fullscreen)
88 35
         flags |= SDL_WINDOW_FULLSCREEN;
89 36
 
90 37
     if ((SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8) != 0)
@@ -103,45 +50,27 @@ int WindowSDL::initialize() {
103 50
         return -2;
104 51
     }
105 52
 
106
-    mWindow = SDL_CreateWindow(VERSION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
107
-                               mWidth, mHeight, flags);
108
-    if (mWindow == nullptr) {
53
+    window = SDL_CreateWindow(VERSION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
54
+                              size.x, size.y, flags);
55
+    if (!window) {
109 56
         std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
110 57
         return -3;
111 58
     }
112 59
 
113
-    mGLContext = SDL_GL_CreateContext(mWindow);
114
-    if (mGLContext == nullptr) {
60
+    context = SDL_GL_CreateContext(window);
61
+    if (!context) {
115 62
         std::cout << "SDL_GL_CreateContext Error: " << SDL_GetError() << std::endl;
116 63
         return -4;
117 64
     }
118 65
 
119
-    mInit = true;
120
-    setSize(mWidth, mHeight);
121
-    setMousegrab(mMousegrab);
66
+    setMousegrab(mousegrab);
67
+    setTextInput(textinput);
122 68
 
123 69
     if (SDL_NumJoysticks() == 0) {
124 70
         getLog() << "No Joystick found!" << Log::endl;
125 71
         return 0;
126 72
     }
127 73
 
128
-    //! \todo Provide a way for user-defined controller mappings
129
-    /*
130
-        SDL_GameControllerAddMapping("341a0000000000000208000000000000,"
131
-                                     "USB GAMEPAD 8116,"
132
-                                     "a:b0,x:b2,start:b7,back:b6,leftstick:b8,rightstick:b9,"
133
-                                     "leftshoulder:b4,rightshoulder:b5,"
134
-                                     "dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,"
135
-                                     "leftx:a0,lefty:a1,rightx:a3,righty:a2,"
136
-                                     "lefttrigger:,b:b1,y:b3,lefttrigger:a4,righttrigger:a4");
137
-        SDL_GameControllerAddMapping("4c050000000000006802000000000000,"
138
-                                     "PLAYSTATION(R)3 Controller,"
139
-                                     "a:b14,b:b13,y:b12,x:b15,start:b3,guide:b16,back:b0,"
140
-                                     "leftstick:b1,rightstick:b2,leftshoulder:b10,"
141
-                                     "rightshoulder:b11,dpup:b4,dpleft:b7,dpdown:b6,dpright:b5,"
142
-                                     "leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b8,righttrigger:b9");
143
-    */
144
-
145 74
     for (int i = 0; i < SDL_NumJoysticks(); i++) {
146 75
         if (SDL_IsGameController(i)) {
147 76
             controller = SDL_GameControllerOpen(i);
@@ -158,6 +87,7 @@ int WindowSDL::initialize() {
158 87
                      << "\" is no controller!" << Log::endl;
159 88
         }
160 89
     }
90
+
161 91
     return 0;
162 92
 }
163 93
 
@@ -165,8 +95,6 @@ void WindowSDL::eventHandling() {
165 95
     SDL_Event event;
166 96
     KeyboardButton button;
167 97
 
168
-    assert(mInit == true);
169
-
170 98
     while (SDL_PollEvent(&event)) {
171 99
         switch (event.type) {
172 100
             case SDL_CONTROLLERAXISMOTION:
@@ -530,7 +458,7 @@ void WindowSDL::eventHandling() {
530 458
 
531 459
             case SDL_WINDOWEVENT:
532 460
                 if (event.window.event == SDL_WINDOWEVENT_RESIZED)
533
-                    setSize(event.window.data1, event.window.data2);
461
+                    setSize(glm::vec2(event.window.data1, event.window.data2));
534 462
                 break;
535 463
 
536 464
             case SDL_QUIT:
@@ -542,18 +470,65 @@ void WindowSDL::eventHandling() {
542 470
     UI::eventsFinished();
543 471
 }
544 472
 
545
-void WindowSDL::setTextInput(bool on) {
546
-    assert(mInit == true);
473
+void WindowSDL::swapBuffers() {
474
+    SDL_GL_SwapWindow(window);
475
+}
476
+
477
+void WindowSDL::shutdown() {
478
+    if (context) {
479
+        SDL_GL_DeleteContext(context);
480
+        context = nullptr;
481
+    }
482
+
483
+    if (controller) {
484
+        SDL_GameControllerClose(controller);
485
+        controller = nullptr;
486
+    }
487
+
488
+    if (window) {
489
+        SDL_DestroyWindow(window);
490
+        SDL_QuitSubSystem(SUBSYSTEMS_USED);
491
+        SDL_Quit();
492
+        window = nullptr;
493
+    }
494
+}
495
+
496
+void WindowSDL::setSize(glm::vec2 s) {
497
+    assert((s.x > 0) && (s.y > 0));
498
+
499
+    size = s;
500
+    if (window)
501
+        SDL_SetWindowSize(window, size.x, size.y);
502
+}
503
+
504
+void WindowSDL::setFullscreen(bool f) {
505
+    fullscreen = f;
506
+    if (window) {
507
+        if (fullscreen) {
508
+            SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
509
+        } else {
510
+            SDL_SetWindowFullscreen(window, 0);
511
+        }
512
+    }
513
+}
514
+
515
+void WindowSDL::setMousegrab(bool g) {
516
+    mousegrab = g;
517
+    if (window) {
518
+        if (mousegrab) {
519
+            SDL_SetRelativeMouseMode(SDL_TRUE);
520
+        } else {
521
+            SDL_SetRelativeMouseMode(SDL_FALSE);
522
+            SDL_ShowCursor(1);
523
+        }
524
+    }
525
+}
547 526
 
548
-    mTextInput = on;
549
-    if (mTextInput)
527
+void WindowSDL::setTextInput(bool t) {
528
+    textinput = t;
529
+    if (textinput)
550 530
         SDL_StartTextInput();
551 531
     else
552 532
         SDL_StopTextInput();
553 533
 }
554 534
 
555
-void WindowSDL::swapBuffersGL() {
556
-    assert(mInit == true);
557
-    SDL_GL_SwapWindow(mWindow);
558
-}
559
-

Laden…
Annuleren
Opslaan