Browse Source

refactoring

Kajetan Johannes Hammerle 5 năm trước cách đây
mục cha
commit
0e6fc2de1c
9 tập tin đã thay đổi với 309 bổ sung411 xóa
  1. 8 1
      MainClient.cpp
  2. 77 74
      client/Client.cpp
  3. 23 17
      client/Client.h
  4. 2 2
      engine/DirectRenderer.cpp
  5. 0 90
      engine/GameEngine.h
  6. 22 22
      engine/Shader.cpp
  7. 0 1
      engine/Texture.cpp
  8. 101 204
      engine/Wrapper.cpp
  9. 76 0
      engine/Wrapper.h

+ 8 - 1
MainClient.cpp

@@ -1,11 +1,18 @@
 #include <iostream>
+
+#include "engine/Wrapper.h"
 #include "client/Client.h"
 
 using namespace std;
 
 int main(int argc, char** argv) 
 {
-    Client::start();
+    if(!Engine::init(1024, 620, "Test"))
+    {
+       return 0; 
+    }
+    Client client;
+    Engine::start(client);
     return 0;
 }
 

+ 77 - 74
client/Client.cpp

@@ -1,48 +1,26 @@
 #include "Client.h"
-#include "../engine/GameEngine.h"
-#include <cmath>
+#include <iostream>
 
-Client* Client::instance;
+using namespace std;
 
-void Client::start()
-{
-    GameEngine::start(1024, 620, "Test Game", []()
-    {
-        Client::instance = new Client();
-    }, []() 
-    {
-        Client::instance->tick();
-    }, [](float lag) 
-    {
-        Client::instance->renderTick(lag);
-    });
-    delete instance;
-}
-
-void Client::stop()
-{
-    GameEngine::stop();
-}
-
-
-Client::Client() : chunk(0, 0), texture("resources/textures.png")
+Client::Client()
 {    
-    //chunk.init();
-    
     position.set(0, 0, -2);
-    GameEngine::get()->getShader().setCamera(position.getX(), position.getY(), position.getZ(), 0, 0);
-    GameEngine::get()->getShader().storeCamera();
+    shader.setCamera(position.getX(), position.getY(), position.getZ(), 0, 0);
+    shader.storeCamera();
+    
+    keyManager.map(KEY_LEFT, GLFW_KEY_A);
+    keyManager.map(KEY_RIGHT, GLFW_KEY_D);
+    keyManager.map(KEY_UP, GLFW_KEY_W);
+    keyManager.map(KEY_DOWN, GLFW_KEY_S);
+    keyManager.map(KEY_JUMP, GLFW_KEY_SPACE);
+    keyManager.map(KEY_SNEAK, GLFW_KEY_LEFT_SHIFT);
+    keyManager.map(KEY_CAM_LEFT, GLFW_KEY_H);
+    keyManager.map(KEY_CAM_RIGHT, GLFW_KEY_K);
+    keyManager.map(KEY_CAM_UP, GLFW_KEY_U);
+    keyManager.map(KEY_CAM_DOWN, GLFW_KEY_J);
     
-    GameEngine::get()->getKeyManager().map(KEY_LEFT, GLFW_KEY_A);
-    GameEngine::get()->getKeyManager().map(KEY_RIGHT, GLFW_KEY_D);
-    GameEngine::get()->getKeyManager().map(KEY_UP, GLFW_KEY_W);
-    GameEngine::get()->getKeyManager().map(KEY_DOWN, GLFW_KEY_S);
-    GameEngine::get()->getKeyManager().map(KEY_JUMP, GLFW_KEY_SPACE);
-    GameEngine::get()->getKeyManager().map(KEY_SNEAK, GLFW_KEY_LEFT_SHIFT);
-    GameEngine::get()->getKeyManager().map(KEY_CAM_LEFT, GLFW_KEY_H);
-    GameEngine::get()->getKeyManager().map(KEY_CAM_RIGHT, GLFW_KEY_K);
-    GameEngine::get()->getKeyManager().map(KEY_CAM_UP, GLFW_KEY_U);
-    GameEngine::get()->getKeyManager().map(KEY_CAM_DOWN, GLFW_KEY_J);
+    mouseManager.map(MOUSE_LEFT, GLFW_MOUSE_BUTTON_1);
 }
 
 Client::~Client()
@@ -54,6 +32,13 @@ float ticker = 0;
 
 void Client::tick()
 {
+    tps.update();
+        
+    if(mouseManager.isDown(MOUSE_LEFT))
+    {
+        cout << mouseManager.getDownTime(MOUSE_LEFT) << endl;
+    }
+    
     oldTicker = ticker;
     ticker += 1;
     if(ticker >= 360)
@@ -62,77 +47,95 @@ void Client::tick()
         ticker = 0;
     }
     
-    Shader& c = GameEngine::get()->getShader();
-    c.storeCamera();
-    
-    KeyManager& key = GameEngine::get()->getKeyManager();
+    shader.storeCamera();
     
     float factor = 0.125f;
-    if(key.isDown(KEY_LEFT))
+    if(keyManager.isDown(KEY_LEFT))
     {
-        position.addMul(c.getLeft(), factor);
+        position.addMul(shader.getLeft(), factor);
     }
-    if(key.isDown(KEY_RIGHT))
+    if(keyManager.isDown(KEY_RIGHT))
     {
-        position.addMul(c.getRight(), factor);
+        position.addMul(shader.getRight(), factor);
     }
-    if(key.isDown(KEY_UP))
+    if(keyManager.isDown(KEY_UP))
     {
-        position.addMul(c.getFront(), factor);
+        position.addMul(shader.getFront(), factor);
     }
-    if(key.isDown(KEY_DOWN))
+    if(keyManager.isDown(KEY_DOWN))
     {
-        position.addMul(c.getBack(), factor);
+        position.addMul(shader.getBack(), factor);
     }
-    if(key.isDown(KEY_JUMP))
+    if(keyManager.isDown(KEY_JUMP))
     {
-        position.addMul(c.getUp(), factor);
+        position.addMul(shader.getUp(), factor);
     }
-    if(key.isDown(KEY_SNEAK))
+    if(keyManager.isDown(KEY_SNEAK))
     {
-        position.addMul(c.getDown(), factor);
+        position.addMul(shader.getDown(), factor);
     }
     
-    if(key.isDown(KEY_CAM_LEFT))
+    if(keyManager.isDown(KEY_CAM_LEFT))
     {
         lengthAngle += 2;
     }
-    if(key.isDown(KEY_CAM_RIGHT))
+    if(keyManager.isDown(KEY_CAM_RIGHT))
     {
         lengthAngle -= 2;
     }
-    if(key.isDown(KEY_CAM_UP))
+    if(keyManager.isDown(KEY_CAM_UP))
     {
         widthAngle += 2;
     }
-    if(key.isDown(KEY_CAM_DOWN))
+    if(keyManager.isDown(KEY_CAM_DOWN))
     {
         widthAngle -= 2;
     }
     
-    c.setCamera(position.getX(), position.getY(), position.getZ(), lengthAngle, widthAngle);
+    shader.setCamera(position.getX(), position.getY(), position.getZ(), lengthAngle, widthAngle);
+    
+    mouseManager.tick();
+    keyManager.tick();
 }
 
 void Client::renderTick(float lag)
 {
-    /*GameEngine::getCamera().updateView(lag);
-    texture.bind();
-    GameEngine::setColorEnabled(false);
-    GameEngine::setUseBlending(false);
-    GameEngine::setTextureEnabled(true);
-    chunk.renderTick(lag);*/
+    fps.update();
+    
+    shader.set2DMode();
+    shader.setToIdentity();
+    shader.translateTo(200, 200, 0);
+    shader.rotateZ(interpolate(lag, oldTicker, ticker));
+    shader.updateModelMatrix();
+    shader.setTextMode();
     
-    GameEngine::get()->getShader().set2DMode();
-    GameEngine::get()->getShader().setToIdentity();
-    GameEngine::get()->getShader().translateTo(200, 200, 0);
-    GameEngine::get()->getShader().rotateZ(interpolate(lag, oldTicker, ticker));
-    GameEngine::get()->getShader().updateModelMatrix();
-    GameEngine::get()->getShader().setTextMode();
     string wusi;
     float y = 30;
-    wusi = "FPS: " + to_string(GameEngine::get()->getFramesPerSecond());
-    y = GameEngine::get()->getDirectRenderer().drawString(30, y, true, wusi);
+    wusi = "FPS: " + to_string(fps.getUpdatesPerSecond());
+    y = directRenderer.drawString(30, y, true, wusi);
 }
 
+void Client::onKeyEvent(int key, int scancode, int action, int mods)
+{
+    if(action == GLFW_PRESS)
+    {
+        keyManager.press(key);
+    }
+    else if(action == GLFW_RELEASE)
+    {
+        keyManager.release(key);
+    }
+}
 
+void Client::onMouseClick(int button, int action, int mods)
+{
+    if(action == GLFW_PRESS)
+    {
+        mouseManager.press(button);
+    }
+    else if(action == GLFW_RELEASE)
+    {
+        mouseManager.release(button);
+    }
+}
 

+ 23 - 17
client/Client.h

@@ -1,25 +1,26 @@
 #ifndef CLIENT_H
 #define CLIENT_H
 
-#include "../engine/Mesh.h"
-#include "../math/Vector3D.h"
-#include "../world/Chunk.h"
-#include "../engine/Texture.h"
+#include "../engine/Wrapper.h"
+#include "../engine/KeyManager.h"
+#include "../engine/MouseManager.h"
+#include "../engine/Clock.h"
+#include "../engine/Shader.h"
+#include "../engine/DirectRenderer.h"
 
 using namespace std;
 
-class Client
+class Client : public IClient
 {
 public:
-    static void start();
-    static void stop();
-    static Client& get();
-    
-    virtual ~Client();
-private:
     Client();
-    static Client* instance;
+    virtual ~Client();
     
+    void tick() override;
+    void renderTick(float lag) override;
+    void onKeyEvent(int key, int scancode, int action, int mods) override;
+    void onMouseClick(int button, int action, int mods) override;
+private:
     static const int KEY_LEFT = 0;
     static const int KEY_RIGHT = 1;
     static const int KEY_UP = 2;
@@ -31,13 +32,18 @@ private:
     static const int KEY_CAM_RIGHT = 7;
     static const int KEY_CAM_UP = 8;
     static const int KEY_CAM_DOWN = 9;
-
-    void tick();
-    void renderTick(float lag);
     
-    Texture texture;
-    Chunk chunk;
+    static const int MOUSE_LEFT = 0;
+    
+    Clock tps;
+    Clock fps;
     
+    KeyManager keyManager;
+    MouseManager mouseManager;
+    
+    Shader shader;
+    DirectRenderer directRenderer;
+
     Vector3D position;
     float lengthAngle = 0;
     float widthAngle = 0;

+ 2 - 2
engine/DirectRenderer.cpp

@@ -1,5 +1,5 @@
 #include "DirectRenderer.h"
-#include "GameEngine.h"
+#include "Wrapper.h"
 #include <cmath>
 
 DirectRenderer::DirectRenderer()
@@ -118,7 +118,7 @@ float DirectRenderer::drawString(float x, float y, bool shadow, string& s)
     glUnmapBuffer(GL_ARRAY_BUFFER);
 
     glBindVertexArray(vba);
-    FONTS[min(GameEngine::get()->getScale() - 1, FONTS_LENGTH - 1)].bind();
+    FONTS[min(Engine::getScale() - 1, FONTS_LENGTH - 1)].bind();
     glDrawArrays(GL_TRIANGLE_STRIP, offset / (OBJECT_LENGTH / 4), (index + 1) / 6);
     offset += size;
     

+ 0 - 90
engine/GameEngine.h

@@ -1,90 +0,0 @@
-#ifndef GAMEENGINE_H
-#define GAMEENGINE_H
-
-#include <GL/glew.h>
-#include <GLFW/glfw3.h>
-#include <iostream>
-#include <string>
-#include <fstream>
-#include <cstring>
-#include "KeyManager.h"
-#include "MouseManager.h"
-#include "Shader.h"
-#include "DirectRenderer.h"
-#include "Clock.h"
-
-using namespace std;
-
-typedef void (*InitFunction) ();
-typedef void (*TickFunction) ();
-typedef void (*RenderTickFunction) (float);
-
-class GameEngine
-{
-public:
-    static void start(int width, int height, const char* name, InitFunction init, TickFunction tick, RenderTickFunction renderTick);
-    static void stop();
-    static GameEngine* get();
-    
-    virtual ~GameEngine();
-    
-    KeyManager& getKeyManager();
-    MouseManager& getMouseManager();
-    Shader& getShader();
-    DirectRenderer& getDirectRenderer();
-    
-    void printError();
-    
-    double getTicksPerSecond() const;
-    double getFramesPerSecond() const;
-    
-    int getScale() const;
-    int getWidth() const;
-    int getHeight() const;
-    
-    GLint getUniformLocation(const GLchar* name) const;
-    void setMatrix(GLint location, const GLfloat* m);
-    void setInt(GLint location, GLint i);
-    void setFloat(GLint location, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4);
-private:
-    GameEngine(GLuint program, GLuint vShader, GLuint fShader, GLFWwindow* window, int width, int height, TickFunction tick, RenderTickFunction renderTick);
-    static GLuint setInstance(GLuint program, GameEngine* en);
-    static GameEngine* instance;
-    static const uint64_t NANOS_PER_TICK = 50000000;
-    static const int MAX_TICKS_PER_FRAME = 5;
-    void run();
-
-    void onKeyEvent(GLFWwindow* w, int key, int scancode, int action, int mods);
-    void onMouseClick(GLFWwindow* w, int button, int action, int mods);
-    void onWindowResize(GLFWwindow* w, int width, int height);
-
-    static GLchar* readFile(const char* name);
-    static bool checkShaderErrors(const char* name, GLuint shader);
-    static GLuint compileProgram(GLuint& vShader, GLuint& fShader, const GLchar* vertex, const GLchar* fragment);
-    static GLuint createProgram(GLuint& vShader, GLuint& fShader);
-
-    void updateScale();
-
-    TickFunction tick;
-    RenderTickFunction renderTick;
-    
-    Clock tps;
-    Clock fps;
-    
-    GLFWwindow* window;
-    GLuint program = 0;
-    GLuint vShader = 0;
-    GLuint fShader = 0;
-
-    int scale = 1;
-    int width;
-    int height;
-    
-    KeyManager keyManager;
-    MouseManager mouseManager;
-    Shader shader;
-    DirectRenderer directRenderer;
-};
-
-#endif
-

+ 22 - 22
engine/Shader.cpp

@@ -1,18 +1,18 @@
 #include "Shader.h"
-#include "GameEngine.h"
 #include "Utils.h"
 #include <cmath>
+#include "Wrapper.h"
 
 Shader::Shader()
 {
-    unifProjMatrix = GameEngine::get()->getUniformLocation("projMatrix");
-    unifViewMatrix = GameEngine::get()->getUniformLocation("viewMatrix");
-    unifModelMatrix = GameEngine::get()->getUniformLocation("modelMatrix");
+    unifProjMatrix = Engine::getUniformLocation("projMatrix");
+    unifViewMatrix = Engine::getUniformLocation("viewMatrix");
+    unifModelMatrix = Engine::getUniformLocation("modelMatrix");
     
-    unifUseTexture = GameEngine::get()->getUniformLocation("useTexture");
-    unifUseColor = GameEngine::get()->getUniformLocation("useColor");
-    unifUseMixColor = GameEngine::get()->getUniformLocation("useMixColor");
-    unifMixColorLoc = GameEngine::get()->getUniformLocation("mixColor");
+    unifUseTexture = Engine::getUniformLocation("useTexture");
+    unifUseColor = Engine::getUniformLocation("useColor");
+    unifUseMixColor = Engine::getUniformLocation("useMixColor");
+    unifMixColorLoc = Engine::getUniformLocation("mixColor");
 }
 
 Shader::Shader(const Shader& orig)
@@ -27,14 +27,14 @@ void Shader::set3DMode(float lag)
 {
     float q = 1.0f / tanf((0.5f * fovY) * M_PI / 180.0f);
 
-    proj.set(0, 0, (q * GameEngine::get()->getHeight()) / GameEngine::get()->getWidth());
+    proj.set(0, 0, (q * Engine::getHeight()) / Engine::getWidth());
     proj.set(1, 1, q);
     proj.set(2, 2, (nearClip + farClip) / (nearClip - farClip));
     proj.set(3, 2, -1.0f);
     proj.set(2, 3, (2.0f * nearClip * farClip) / (nearClip - farClip));
     proj.set(3, 3, 0);   
     
-    GameEngine::get()->setMatrix(unifProjMatrix, proj.getValues());
+    Engine::setMatrix(unifProjMatrix, proj.getValues());
     
     // -------------------------------------------------------------------------
     // calculate vectors for the view matrix
@@ -108,25 +108,25 @@ void Shader::set3DMode(float lag)
     // down
     down.setInverse(up);
     
-    GameEngine::get()->setMatrix(unifViewMatrix, view.getValues());
+    Engine::setMatrix(unifViewMatrix, view.getValues());
 }
 
 void Shader::set2DMode()
 {
     proj.setToIdentity();
-    GameEngine::get()->setMatrix(unifProjMatrix, proj.getValues());
+    Engine::setMatrix(unifProjMatrix, proj.getValues());
     
-    int scale = GameEngine::get()->getScale();
-    view.set(0, 0, (2.0f * scale) / GameEngine::get()->getWidth());
-    view.set(1, 1, (-2.0f * scale) / GameEngine::get()->getHeight());
-    view.set(2, 2, (-1.0f * scale) / GameEngine::get()->getHeight());
+    int scale = Engine::getScale();
+    view.set(0, 0, (2.0f * scale) / Engine::getWidth());
+    view.set(1, 1, (-2.0f * scale) / Engine::getHeight());
+    view.set(2, 2, (-1.0f * scale) / Engine::getHeight());
     
     view.set(0, 3, -1.0f);
     view.set(1, 3, 1.0f);
     view.set(2, 3, 0.5f);
     view.set(3, 3, 1.0f);
     
-    GameEngine::get()->setMatrix(unifViewMatrix, view.getValues());
+    Engine::setMatrix(unifViewMatrix, view.getValues());
 }
 
 void Shader::storeCamera()
@@ -175,22 +175,22 @@ const Vector3D& Shader::getDown() const
 
 void Shader::setTextureEnabled(bool use)
 {
-    GameEngine::get()->setInt(unifUseTexture, use);
+    Engine::setInt(unifUseTexture, use);
 }
 
 void Shader::setColorEnabled(bool use)
 {
-    GameEngine::get()->setInt(unifUseColor, use);
+    Engine::setInt(unifUseColor, use);
 }
 
 void Shader::setMixColorEnabled(bool use)
 {
-    GameEngine::get()->setInt(unifUseMixColor, use);
+    Engine::setInt(unifUseMixColor, use);
 }
 
 void Shader::setMixColor(float r, float g, float b, float a)
 {
-    GameEngine::get()->setFloat(unifUseMixColor, r, g, b, a);
+    Engine::setFloat(unifUseMixColor, r, g, b, a);
 }
 
 void Shader::setTextMode()
@@ -282,5 +282,5 @@ void Shader::rotateZ(float degrees)
 
 void Shader::updateModelMatrix()
 {
-    GameEngine::get()->setMatrix(unifModelMatrix, model.get().getValues());
+    Engine::setMatrix(unifModelMatrix, model.get().getValues());
 }

+ 0 - 1
engine/Texture.cpp

@@ -1,5 +1,4 @@
 #include "Texture.h"
-#include "GameEngine.h"
 #include <png.h>
 
 using namespace std;

+ 101 - 204
engine/GameEngine.cpp → engine/Wrapper.cpp

@@ -1,26 +1,42 @@
-#include "GameEngine.h"
-#include "MouseManager.h"
-#include <cmath>
+#include "Wrapper.h"
+#include <fstream>
+#include <cstring>
+#include <iostream>
 
-GameEngine* GameEngine::instance = nullptr;
+using namespace std;
 
-void GameEngine::start(int width, int height, const char* name, InitFunction init, TickFunction tick, RenderTickFunction renderTick)
-{    
+DummyClient DummyClient::dummy;
+
+IClient* Engine::client = &DummyClient::dummy;
+GLFWwindow* Engine::window = nullptr;
+GLuint Engine::vShader = 0;
+GLuint Engine::fShader = 0;
+GLuint Engine::program = 0;
+int Engine::scale = 1;
+int Engine::width = 0;
+int Engine::height = 0;
+
+bool Engine::init(int width, int height, const char* name)
+{
+    Engine::width = width;
+    Engine::height = height;
+    updateScale();
+    
     if(!glfwInit())
     {
         cout << "could not initialize GLFW" << endl;
-        return;
+        return false;
     }
 
     glfwDefaultWindowHints();
     glfwWindowHint(GLFW_VISIBLE, 0);
     glfwWindowHint(GLFW_RESIZABLE, 1);
-    GLFWwindow* window = glfwCreateWindow(width, height, name, nullptr, nullptr);
+    window = glfwCreateWindow(width, height, name, nullptr, nullptr);
     if(!window)
     {
         cout << "could not create window" << endl;
         glfwTerminate();
-        return;
+        return false;
     }
 
     glfwMakeContextCurrent(window);
@@ -32,13 +48,13 @@ void GameEngine::start(int width, int height, const char* name, InitFunction ini
     if(GLEW_OK != err)
     {
         cout << "could not initialize GLEW: " << glewGetErrorString(err) << endl;
-        return;
+        return false;
     }
     cout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << endl;
 
-    GLuint vShader = 0;
-    GLuint fShader = 0;
-    GLuint program = createProgram(vShader, program);
+    vShader = 0;
+    fShader = 0;
+    program = createProgram(vShader, program);
     if(program == 0)
     {
         if(vShader != 0)
@@ -51,40 +67,21 @@ void GameEngine::start(int width, int height, const char* name, InitFunction ini
         }
         glfwDestroyWindow(window);
         glfwTerminate();
-        return;
+        return false;
     }
-    
-    GameEngine::instance = new GameEngine(program, vShader, fShader, window, width, height, tick, renderTick);
-    glfwSetKeyCallback(window, [](GLFWwindow* w, int key, int scancode, int action, int mods) 
-    {
-        GameEngine::instance->onKeyEvent(w, key, scancode, action, mods);
-    });
-    glfwSetMouseButtonCallback(window, [](GLFWwindow* w, int button, int action, int mods)
-    {
-        GameEngine::instance->onMouseClick(w, button, action, mods);
-    });
-    glfwSetFramebufferSizeCallback(window, [](GLFWwindow* w, int width, int height)
-    {
-        GameEngine::instance->onWindowResize(w, width, height);
-    }); 
+
+    glfwSetKeyCallback(window, onKeyEvent);
+    glfwSetMouseButtonCallback(window, onMouseClick);
+    glfwSetFramebufferSizeCallback(window, onWindowResize); 
     //glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
     //glfwSetCursorPosCallback(window, onMouseMove);
-    
-    init();
-    instance->run();
-    delete instance;
+    return true;
 }
 
-void GameEngine::stop()
+void Engine::start(IClient& client)
 {
-    if(GameEngine::instance != nullptr)
-    {
-        glfwSetWindowShouldClose(GameEngine::instance->window, 1);
-    }
-}
-
-void GameEngine::run()
-{    
+    Engine::client = &client;
+    
     glEnable(GL_CULL_FACE);
     glEnable(GL_DEPTH_TEST);
     glDepthFunc(GL_LEQUAL);
@@ -105,10 +102,7 @@ void GameEngine::run()
         {
             lag -= NANOS_PER_TICK;
 
-            tps.update();
-            tick();
-            keyManager.tick();
-            mouseManager.tick();
+            Engine::client->tick();
             ticksPerFrame++;
 
             if(ticksPerFrame >= MAX_TICKS_PER_FRAME)
@@ -123,8 +117,7 @@ void GameEngine::run()
             }
         }
 
-        fps.update();
-        renderTick((float) lag / NANOS_PER_TICK);
+        Engine::client->renderTick((float) lag / NANOS_PER_TICK);
 
         glfwSwapBuffers(window);
         glfwPollEvents();
@@ -137,89 +130,12 @@ void GameEngine::run()
     glfwTerminate();
 }
 
-GLuint GameEngine::setInstance(GLuint program, GameEngine* en)
-{
-    GameEngine::instance = en;
-    return program;
-}
-
-GameEngine::GameEngine(GLuint program, GLuint vShader, GLuint fShader, GLFWwindow* window, 
-        int width, int height, TickFunction tick, RenderTickFunction renderTick) :
-    program(setInstance(program, this)), vShader(vShader), fShader(fShader), window(window), width(width), height(height), tick(tick), renderTick(renderTick)
-{
-    updateScale();
-}
-
-GameEngine::~GameEngine()
-{
-}
-
-void GameEngine::onKeyEvent(GLFWwindow* w, int key, int scancode, int action, int mods)
-{
-    if(action == GLFW_RELEASE)
-    {
-        if(key == GLFW_KEY_ESCAPE)
-        {
-            //activeFocus = 0;
-            glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
-        }
-        keyManager.release(key);
-    }
-    else if(action == GLFW_PRESS)
-    {
-        keyManager.press(key);
-    }
-}
-
-void GameEngine::onMouseClick(GLFWwindow* w, int button, int action, int mods)
+void Engine::stop()
 {
-    if(action == GLFW_PRESS)
-    {
-        /*if(!activeFocus)
-        {
-            glfwSetInputMode(w, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
-            oldMouseX = 0;
-            oldMouseY = 0;
-            activeFocus = 1;
-        }
-        else
-        {*/
-            mouseManager.press(button);
-        //}
-    }
-    else if(action == GLFW_RELEASE)
-    {
-        mouseManager.release(button);
-    }
-}
-
-/*void GameEngine::onMouseMove(GLFWwindow* w, double x, double y)
-{
-    if(activeFocus)
-    {
-        if(oldMouseX == 0 && oldMouseY == 0)
-        {
-            oldMouseX = x;
-            oldMouseY = y;
-        }
-        else
-        {
-            mouseMove(x - oldMouseX, y - oldMouseY);
-            oldMouseX = x;
-            oldMouseY = y;
-        }
-    }
-}*/
-
-void GameEngine::onWindowResize(GLFWwindow* w, int width, int height)
-{
-    glViewport(0, 0, width, height);
-    GameEngine::width = width;
-    GameEngine::height = height;
-    updateScale();
+    glfwSetWindowShouldClose(window, 1);
 }
 
-GLchar* GameEngine::readFile(const char* name)
+GLchar* Engine::readFile(const char* name)
 {
     ifstream in;
     in.open(name);
@@ -257,7 +173,7 @@ GLchar* GameEngine::readFile(const char* name)
     return nullptr;
 }
 
-bool GameEngine::checkShaderErrors(const char* name, GLuint shader)
+bool Engine::checkShaderErrors(const char* name, GLuint shader)
 {
     bool returnValue = false;
     
@@ -294,7 +210,7 @@ bool GameEngine::checkShaderErrors(const char* name, GLuint shader)
     return returnValue;
 }
 
-GLuint GameEngine::compileProgram(GLuint& vShader, GLuint& fShader, const GLchar* vertex, const GLchar* fragment)
+GLuint Engine::compileProgram(GLuint& vShader, GLuint& fShader, const GLchar* vertex, const GLchar* fragment)
 {
     vShader = glCreateShader(GL_VERTEX_SHADER);
     glShaderSource(vShader, 1, &vertex, nullptr);
@@ -355,7 +271,7 @@ GLuint GameEngine::compileProgram(GLuint& vShader, GLuint& fShader, const GLchar
     return program;
 }
 
-GLuint GameEngine::createProgram(GLuint& vShader, GLuint& fShader)
+GLuint Engine::createProgram(GLuint& vShader, GLuint& fShader)
 {
     GLchar* vertex = readFile("shader/vertex.vs");
     if(vertex == nullptr)
@@ -377,117 +293,98 @@ GLuint GameEngine::createProgram(GLuint& vShader, GLuint& fShader)
     return program;
 }
 
-void GameEngine::printError()
-{
-    GLenum error = glGetError();
-    switch(error)
-    {
-        case GL_NO_ERROR: 
-            cout << "> No error has been recorded." << endl;
-            break;
-        case GL_INVALID_ENUM: 
-            cout << "> An unacceptable value is specified for an enumerated argument." << endl;
-            break;
-        case GL_INVALID_VALUE: 
-            cout << "> A numeric argument is out of range." << endl;
-            break;
-        case GL_INVALID_OPERATION: 
-            cout << "> The specified operation is not allowed in the current state." << endl;
-            break;
-        case GL_INVALID_FRAMEBUFFER_OPERATION: 
-            cout << "> The framebuffer object is not complete." << endl;
-            break;
-        case GL_OUT_OF_MEMORY: 
-            cout << "> There is not enough memory left to execute the command." << endl;
-            break;
-        case GL_STACK_UNDERFLOW: 
-            cout << "> An attempt has been made to perform an operation that would cause an internal stack to underflow." << endl;
-            break;
-        case GL_STACK_OVERFLOW: 
-            cout << "> An attempt has been made to perform an operation that would cause an internal stack to overflow." << endl;
-            break;
-        default:
-            cout << "> Unknown OpenGL error: " << error << endl;
-    }
-}
-
-void GameEngine::updateScale()
-{
-    scale = 1;
-    while(width / (scale + 1) >= 400 && height / (scale + 1) >= 300)
-    {
-        scale++;
-    }
-}
-
-GameEngine* GameEngine::get()
-{
-    return instance;
-}
-
-KeyManager& GameEngine::getKeyManager()
+void Engine::onKeyEvent(GLFWwindow* w, int key, int scancode, int action, int mods)
 {
-    return keyManager;
+    client->onKeyEvent(key, scancode, action, mods);
 }
 
-MouseManager& GameEngine::getMouseManager()
+void Engine::onMouseClick(GLFWwindow* w, int button, int action, int mods)
 {
-    return mouseManager;
+    client->onMouseClick(button, action, mods);
 }
 
-Shader& GameEngine::getShader()
+void Engine::onWindowResize(GLFWwindow* w, int width, int height)
 {
-    return shader;
-}
-
-DirectRenderer& GameEngine::getDirectRenderer()
-{
-    return directRenderer;
-}
-
-double GameEngine::getTicksPerSecond() const
-{
-    return tps.getUpdatesPerSecond();
+    glViewport(0, 0, width, height);
+    Engine::width = width;
+    Engine::height = height;
+    updateScale();
 }
 
-double GameEngine::getFramesPerSecond() const
+void Engine::updateScale()
 {
-    return fps.getUpdatesPerSecond();
+    scale = 1;
+    while(width / (scale + 1) >= 400 && height / (scale + 1) >= 300)
+    {
+        scale++;
+    }
 }
 
-int GameEngine::getScale() const
+int Engine::getScale()
 {
     return scale;
 }
 
-int GameEngine::getWidth() const
+int Engine::getWidth()
 {
     return width;
 }
 
-int GameEngine::getHeight() const
+int Engine::getHeight()
 {
     return height;
 }
 
-GLint GameEngine::getUniformLocation(const GLchar* name) const
+GLint Engine::getUniformLocation(const GLchar* name)
 {
     return glGetUniformLocation(program, name);
 }
 
-void GameEngine::setMatrix(GLint location, const GLfloat* m)
+void Engine::setMatrix(GLint location, const GLfloat* m)
 {
     glUniformMatrix4fv(location, 1, 0, m);
 }
 
-void GameEngine::setInt(GLint location, GLint i)
+void Engine::setInt(GLint location, GLint i)
 {
     glUniform1i(location, i);
 }
 
-void GameEngine::setFloat(GLint location, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4)
+void Engine::setFloat(GLint location, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4)
 {
     glUniform4f(location, f1, f2, f3, f4);
 }
 
-
+void Engine::printError()
+{
+    GLenum error = glGetError();
+    switch(error)
+    {
+        case GL_NO_ERROR: 
+            cout << "> No error has been recorded." << endl;
+            break;
+        case GL_INVALID_ENUM: 
+            cout << "> An unacceptable value is specified for an enumerated argument." << endl;
+            break;
+        case GL_INVALID_VALUE: 
+            cout << "> A numeric argument is out of range." << endl;
+            break;
+        case GL_INVALID_OPERATION: 
+            cout << "> The specified operation is not allowed in the current state." << endl;
+            break;
+        case GL_INVALID_FRAMEBUFFER_OPERATION: 
+            cout << "> The framebuffer object is not complete." << endl;
+            break;
+        case GL_OUT_OF_MEMORY: 
+            cout << "> There is not enough memory left to execute the command." << endl;
+            break;
+        case GL_STACK_UNDERFLOW: 
+            cout << "> An attempt has been made to perform an operation that would cause an internal stack to underflow." << endl;
+            break;
+        case GL_STACK_OVERFLOW: 
+            cout << "> An attempt has been made to perform an operation that would cause an internal stack to overflow." << endl;
+            break;
+        default:
+            cout << "> Unknown OpenGL error: " << error << endl;
+    }
+}

+ 76 - 0
engine/Wrapper.h

@@ -0,0 +1,76 @@
+#ifndef WRAPPER_H
+#define WRAPPER_H
+
+#include <GL/glew.h>
+#include <GLFW/glfw3.h>
+
+#include <iostream>
+
+using namespace std;
+
+class IClient
+{
+public:
+    virtual void tick() = 0;
+    virtual void renderTick(float lag) = 0;
+    virtual void onKeyEvent(int key, int scancode, int action, int mods) = 0;
+    virtual void onMouseClick(int button, int action, int mods) = 0;
+};
+
+class DummyClient : public IClient
+{
+public:
+    static DummyClient dummy;
+    
+    void tick() override { cout << "Dummy tick" << endl; };
+    void renderTick(float lag) override { cout << "Dummy renderTick" << endl; };
+    void onKeyEvent(int key, int scancode, int action, int mods) override { cout << "Dummy onKeyEvent" << endl; };
+    void onMouseClick(int button, int action, int mods) override { cout << "Dummy onMouseClick" << endl; };
+private:
+    DummyClient() {};
+};
+
+class Engine
+{
+public:
+    static bool init(int width, int height, const char* name);
+    static void start(IClient& client);
+    static void stop();
+    
+    void printError();
+    
+    static int getScale();
+    static int getWidth();
+    static int getHeight();
+    
+    static GLint getUniformLocation(const GLchar* name);
+    static void setMatrix(GLint location, const GLfloat* m);
+    static void setInt(GLint location, GLint i);
+    static void setFloat(GLint location, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4);
+private:
+    static const uint64_t NANOS_PER_TICK = 50000000;
+    static const int MAX_TICKS_PER_FRAME = 5;
+    
+    static GLchar* readFile(const char* name);
+    static bool checkShaderErrors(const char* name, GLuint shader);
+    static GLuint compileProgram(GLuint& vShader, GLuint& fShader, const GLchar* vertex, const GLchar* fragment);
+    static GLuint createProgram(GLuint& vShader, GLuint& fShader);
+    
+    static void onKeyEvent(GLFWwindow* w, int key, int scancode, int action, int mods);
+    static void onMouseClick(GLFWwindow* w, int button, int action, int mods);
+    static void onWindowResize(GLFWwindow* w, int width, int height);
+    
+    static void updateScale();
+    
+    static IClient* client;    
+    static GLFWwindow* window;   
+    static GLuint vShader;
+    static GLuint fShader;
+    static GLuint program;
+    static int scale;
+    static int width;
+    static int height;
+};
+
+#endif
+