Wrapper.h 2.9 KB

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