GameEngine.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include "DirectRenderer.h"
  13. #include "Clock.h"
  14. using namespace std;
  15. static const uint64_t NANOS_PER_TICK = 50000000;
  16. static const int MAX_TICKS_PER_FRAME = 5;
  17. typedef void (*InitFunction) ();
  18. typedef void (*TickFunction) ();
  19. typedef void (*RenderTickFunction) (float);
  20. class DirectRenderer;
  21. class GameEngine
  22. {
  23. public:
  24. static void start(int width, int height, const char* name, InitFunction init, TickFunction tick, RenderTickFunction renderTick);
  25. static Camera& getCamera();
  26. static DirectRenderer& getDirectRenderer();
  27. static void setTextureEnabled(bool use);
  28. static void setColorEnabled(bool use);
  29. static void setMixColorEnabled(bool use);
  30. static void setMixColor(float r, float g, float b, float a);
  31. static void setUseBlending(bool use);
  32. static void printError();
  33. static void stop();
  34. static double getTicksPerSecond();
  35. static double getFramesPerSecond();
  36. friend Camera;
  37. friend Texture;
  38. friend DirectRenderer;
  39. private:
  40. GameEngine();
  41. static void onKeyEvent(GLFWwindow* w, int key, int scancode, int action, int mods);
  42. static void onMouseClick(GLFWwindow* w, int button, int action, int mods);
  43. static void onWindowResize(GLFWwindow* w, int width, int height);
  44. static GLchar* readFile(const char* name);
  45. static bool checkShaderErrors(const char* name, GLuint shader);
  46. static GLuint compileProgram(const GLchar* vertex, const GLchar* fragment);
  47. static GLuint createProgram();
  48. static void updateProjection();
  49. static void updateView(Matrix3D& m);
  50. static void onInit();
  51. static void onTick();
  52. static void onTerm();
  53. static void updateScale();
  54. static Clock tps;
  55. static Clock fps;
  56. static InitFunction init;
  57. static TickFunction tick;
  58. static RenderTickFunction renderTick;
  59. static GLuint vShader;
  60. static GLuint fShader;
  61. static GLFWwindow* window;
  62. static GLuint program;
  63. static int scale;
  64. static int width;
  65. static int height;
  66. static float fovY;
  67. static float nearClip;
  68. static float farClip;
  69. static Matrix3D proj;
  70. static Camera camera;
  71. static DirectRenderer directRenderer;
  72. // uniforms
  73. static GLint unifProjMatrix;
  74. static GLint unifViewMatrix;
  75. static GLint unifUseTexture;
  76. static GLint unifUseColor;
  77. static GLint unifUseMixColor;
  78. static GLint unifMixColorLoc;
  79. };
  80. #endif