#ifndef GAMEENGINE_H #define GAMEENGINE_H #include #include #include #include #include #include #include "../math/Matrix3D.h" #include "Camera.h" #include "Texture.h" using namespace std; static const uint64_t NANOS_PER_TICK = 50000000; static const int MAX_TICKS_PER_FRAME = 5; typedef void (*InitFunction) (); typedef void (*TickFunction) (); typedef void (*RenderTickFunction) (float); class DirectRenderer; class GameEngine { public: static void start(int width, int height, const char* name, InitFunction init, TickFunction tick, RenderTickFunction renderTick); static Camera& getCamera(); static DirectRenderer& getDirectRenderer(); static void setTextureEnabled(bool use); static void setColorEnabled(bool use); static void setMixColorEnabled(bool use); static void setMixColor(float r, float g, float b, float a); static void printError(); static void stop(); friend Camera; friend Texture; friend DirectRenderer; private: GameEngine(); 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 GLchar* readFile(const char* name); static bool checkShaderErrors(const char* name, GLuint shader); static GLuint compileProgram(const GLchar* vertex, const GLchar* fragment); static GLuint createProgram(); static void updateProjection(); static void updateView(Matrix3D& m); static void onInit(); static void onTick(); static void onTerm(); static void updateScale(); static InitFunction init; static TickFunction tick; static RenderTickFunction renderTick; static GLuint vShader; static GLuint fShader; static GLFWwindow* window; static GLuint program; static int scale; static int width; static int height; static float fovY; static float nearClip; static float farClip; static Matrix3D proj; static Camera camera; static DirectRenderer directRenderer; // uniforms static GLint unifProjMatrix; static GLint unifViewMatrix; static GLint unifUseTexture; static GLint unifUseColor; static GLint unifUseMixColor; static GLint unifMixColorLoc; }; class DirectRenderer { public: DirectRenderer(); DirectRenderer(const DirectRenderer& orig); virtual ~DirectRenderer(); void prepare(); void drawRectangle(float minX, float minY, float maxX, float maxY, float tMinX, float tMinY, float tMaxX, float tMaxY, float r, float g, float b, float a); void drawColoredRectangle(float minX, float minY, float maxX, float maxY, float r, float g, float b, float a); void drawTexturedRectangle(float minX, float minY, float maxX, float maxY, float tMinX, float tMinY, float tMaxX, float tMaxY); friend GameEngine; private: void init(); static const int OBJECT_LENGTH = 144; static const int BUFFER_LENGTH = 256 * 1024 * OBJECT_LENGTH; GLuint vba = 0; GLuint vbo = 0; unsigned int offset = BUFFER_LENGTH - OBJECT_LENGTH; Matrix3D view; Matrix3D proj; }; #endif