GameEngine.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef GAMEENGINE_H
  2. #define GAMEENGINE_H
  3. #include <GL/glew.h>
  4. #include <GLFW/glfw3.h>
  5. #include <iostream>
  6. #include <string>
  7. #include <fstream>
  8. #include <cstring>
  9. #include "../math/Matrix3D.h"
  10. #include "Camera.h"
  11. #include "Texture.h"
  12. using namespace std;
  13. static const uint64_t NANOS_PER_TICK = 50000000;
  14. static const int MAX_TICKS_PER_FRAME = 5;
  15. typedef void (*InitFunction) ();
  16. typedef void (*TickFunction) ();
  17. typedef void (*RenderTickFunction) (float);
  18. class DirectRenderer;
  19. class GameEngine
  20. {
  21. public:
  22. static void start(int width, int height, const char* name, InitFunction init, TickFunction tick, RenderTickFunction renderTick);
  23. static Camera& getCamera();
  24. static DirectRenderer& getDirectRenderer();
  25. static void setTextureEnabled(bool use);
  26. static void setColorEnabled(bool use);
  27. static void setMixColorEnabled(bool use);
  28. static void setMixColor(float r, float g, float b, float a);
  29. static void printError();
  30. static void stop();
  31. friend Camera;
  32. friend Texture;
  33. friend DirectRenderer;
  34. private:
  35. GameEngine();
  36. static void onKeyEvent(GLFWwindow* w, int key, int scancode, int action, int mods);
  37. static void onMouseClick(GLFWwindow* w, int button, int action, int mods);
  38. static void onWindowResize(GLFWwindow* w, int width, int height);
  39. static GLchar* readFile(const char* name);
  40. static bool checkShaderErrors(const char* name, GLuint shader);
  41. static GLuint compileProgram(const GLchar* vertex, const GLchar* fragment);
  42. static GLuint createProgram();
  43. static void updateProjection();
  44. static void updateView(Matrix3D& m);
  45. static void onInit();
  46. static void onTick();
  47. static void onTerm();
  48. static void updateScale();
  49. static InitFunction init;
  50. static TickFunction tick;
  51. static RenderTickFunction renderTick;
  52. static GLuint vShader;
  53. static GLuint fShader;
  54. static GLFWwindow* window;
  55. static GLuint program;
  56. static int scale;
  57. static int width;
  58. static int height;
  59. static float fovY;
  60. static float nearClip;
  61. static float farClip;
  62. static Matrix3D proj;
  63. static Camera camera;
  64. static DirectRenderer directRenderer;
  65. // uniforms
  66. static GLint unifProjMatrix;
  67. static GLint unifViewMatrix;
  68. static GLint unifUseTexture;
  69. static GLint unifUseColor;
  70. static GLint unifUseMixColor;
  71. static GLint unifMixColorLoc;
  72. };
  73. class DirectRenderer
  74. {
  75. public:
  76. DirectRenderer();
  77. DirectRenderer(const DirectRenderer& orig);
  78. virtual ~DirectRenderer();
  79. void prepare();
  80. void drawRectangle(float minX, float minY, float maxX, float maxY,
  81. float tMinX, float tMinY, float tMaxX, float tMaxY,
  82. float r, float g, float b, float a);
  83. void drawColoredRectangle(float minX, float minY, float maxX, float maxY,
  84. float r, float g, float b, float a);
  85. void drawTexturedRectangle(float minX, float minY, float maxX, float maxY,
  86. float tMinX, float tMinY, float tMaxX, float tMaxY);
  87. friend GameEngine;
  88. private:
  89. void init();
  90. static const int OBJECT_LENGTH = 144;
  91. static const int BUFFER_LENGTH = 256 * 1024 * OBJECT_LENGTH;
  92. GLuint vba = 0;
  93. GLuint vbo = 0;
  94. unsigned int offset = BUFFER_LENGTH - OBJECT_LENGTH;
  95. Matrix3D view;
  96. Matrix3D proj;
  97. };
  98. #endif