Wrapper.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef WRAPPER_H
  2. #define WRAPPER_H
  3. #include <GL/glew.h>
  4. #include <GLFW/glfw3.h>
  5. #include <iostream>
  6. using namespace std;
  7. class IClient
  8. {
  9. public:
  10. virtual void tick() = 0;
  11. virtual void renderTick(float lag) = 0;
  12. virtual void onKeyEvent(int key, int scancode, int action, int mods) = 0;
  13. virtual void onMouseClick(int button, int action, int mods) = 0;
  14. };
  15. class DummyClient : public IClient
  16. {
  17. public:
  18. static DummyClient dummy;
  19. void tick() override { cout << "Dummy tick" << endl; };
  20. void renderTick(float lag) override { cout << "Dummy renderTick" << endl; };
  21. void onKeyEvent(int key, int scancode, int action, int mods) override { cout << "Dummy onKeyEvent" << endl; };
  22. void onMouseClick(int button, int action, int mods) override { cout << "Dummy onMouseClick" << endl; };
  23. private:
  24. DummyClient() {};
  25. };
  26. class Engine
  27. {
  28. public:
  29. static bool init(int width, int height, const char* name);
  30. static void start(IClient* client);
  31. static void stop();
  32. static void printError();
  33. static int getScale();
  34. static int getWidth();
  35. static int getHeight();
  36. static GLint getUniformLocation(const GLchar* name);
  37. static void setMatrix(GLint location, const GLfloat* m);
  38. static void setInt(GLint location, GLint i);
  39. static void setFloat(GLint location, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4);
  40. static void setLineMode(bool mode);
  41. private:
  42. static const uint64_t NANOS_PER_TICK = 50000000;
  43. static const int MAX_TICKS_PER_FRAME = 5;
  44. static void generateFramebuffer();
  45. static void generatePostData();
  46. static void destroyPostData();
  47. static GLchar* readFile(const char* name);
  48. static bool checkShaderErrors(const char* name, GLuint shader);
  49. static GLuint compileProgram(GLuint& vShader, GLuint& fShader, const GLchar* v, const GLchar* f);
  50. static GLuint createProgram(const char* v, const char* f, GLuint& vShader, GLuint& fShader);
  51. static void onKeyEvent(GLFWwindow* w, int key, int scancode, int action, int mods);
  52. static void onMouseClick(GLFWwindow* w, int button, int action, int mods);
  53. static void onWindowResize(GLFWwindow* w, int width, int height);
  54. static void updateScale();
  55. static IClient* client;
  56. static GLFWwindow* window;
  57. static GLuint vShader;
  58. static GLuint fShader;
  59. static GLuint program;
  60. static GLuint postVba;
  61. static GLuint postVbo;
  62. static GLuint postVShader;
  63. static GLuint postFShader;
  64. static GLuint postProgram;
  65. static GLuint frameBuffer;
  66. static GLuint frameTexture;
  67. static GLuint depthTexture;
  68. static int scale;
  69. static int width;
  70. static int height;
  71. static bool lineMode;
  72. };
  73. #endif