123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- #include <iostream>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <unordered_map>
- #include <cmath>
- #include "common/utils/Types.h"
- #include "client/GameClient.h"
- #include "client/input/Keys.h"
- #include "client/input/MouseButtons.h"
- #include "client/rendering/Shader.h"
- #include "client/rendering/FontRenderer.h"
- #include "client/rendering/Mesh.h"
- #include "client/rendering/Framebuffer.h"
- #include "client/Utils.h"
- #include "client/math/Camera.h"
- #include "client/math/Matrix.h"
- #include "client/math/MatrixStack.h"
- #include "client/Game.h"
- struct InternGameClient
- {
- ~InternGameClient()
- {
- if(window != nullptr)
- {
- glfwDestroyWindow(window);
- }
- if(glfwInitDone)
- {
- glfwTerminate();
- }
- }
-
- bool glfwInitDone = false;
- GLFWwindow* window = nullptr;
- };
- static const u64 NANOS_PER_TICK = 50000000;
- static const float lagFactor = 1.0f / NANOS_PER_TICK;
- static u64 timeFactor = 1;
- static int width = 0;
- static int height = 0;
- static bool resize = false;
- static float fovY = 60.0f;
- static float nearClip = 0.1f;
- static float farClip = 1000.0f;
- static InternGameClient client;
- static Keys keys;
- static MouseButtons mButtons;
- struct Shaders
- {
- Shaders() :
- world("resources/shader/testVertex.vs", "resources/shader/testFragment.fs"),
- text("resources/shader/textVertex.vs", "resources/shader/textFragment.fs")
- {
- }
-
- Shader world;
- Shader text;
-
- bool isValid() const
- {
- return world.isValid() && text.isValid();
- }
- };
- struct Framebuffers
- {
- Framebuffers(u32 w, u32 h) : worldBuffer(w, h, Framebuffer::COLOR)
- {
- }
-
- void resize(u32 w, u32 h) const
- {
- worldBuffer.resize(w, h);
- }
-
- bool isValid() const
- {
- return worldBuffer.isValid();
- }
-
- Framebuffer worldBuffer;
- };
- struct InternGame
- {
- Game game;
- Camera cam;
- MatrixStack model;
- FontRenderer fontRenderer;
- };
- static u64 getTimeNanos()
- {
- return glfwGetTimerValue() * timeFactor;
- }
- static bool initGLFW()
- {
- client.glfwInitDone = glfwInit();
- if(!client.glfwInitDone)
- {
- std::cout << "could not initialize GLFW\n";
- return true;
- }
- timeFactor = 1000000000 / glfwGetTimerFrequency();
- return false;
- }
- static bool initWindow(int w, int h, const char* windowName)
- {
- width = w;
- height = h;
-
- glfwDefaultWindowHints();
- glfwWindowHint(GLFW_VISIBLE, 0);
- glfwWindowHint(GLFW_RESIZABLE, 1);
-
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
-
- client.window = glfwCreateWindow(width, height, windowName, nullptr, nullptr);
- if(client.window == nullptr)
- {
- std::cout << "could not create window\n";
- return true;
- }
- glfwMakeContextCurrent(client.window);
- glfwSwapInterval(1);
- return false;
- }
- static bool initGLEW()
- {
- GLenum err = glewInit();
- if(err != GLEW_OK)
- {
- std::cout << "could not initialize GLEW: " << glewGetErrorString(err) << "\n";
- return true;
- }
- std::cout << "using GLEW " << glewGetString(GLEW_VERSION) << "\n";
- return false;
- }
- static void initCallbacks()
- {
- // GLFWwindow* w, int key, int scancode, int action, int mod
- glfwSetKeyCallback(client.window, [](GLFWwindow*, int key, int, int action, int)
- {
- if(action == GLFW_PRESS)
- {
- keys.press(key);
- }
- else if(action == GLFW_RELEASE)
- {
- keys.release(key);
- }
- });
- // GLFWwindow* w, int button, int action, int mods
- glfwSetMouseButtonCallback(client.window, [](GLFWwindow*, int button, int action, int)
- {
- if(action == GLFW_PRESS)
- {
- mButtons.press(button);
- }
- else if(action == GLFW_RELEASE)
- {
- mButtons.release(button);
- }
- });
- // GLFWwindow* w, double xpos, double ypos
- glfwSetCursorPosCallback(client.window, [](GLFWwindow*, double x, double y)
- {
- mButtons.move(x, y);
- });
- // GLFWwindow* w, int width, int height
- glfwSetFramebufferSizeCallback(client.window, [](GLFWwindow*, int w, int h)
- {
- glViewport(0, 0, w, h);
- width = w;
- height = h;
- resize = true;
- });
- }
- static void tick(InternGame& game)
- {
- keys.tick();
- mButtons.tick();
- game.game.tick(keys, mButtons, game.cam);
- mButtons.postTick();
- }
- static void renderWorld(float lag, Shaders& shaders, InternGame& game)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glEnable(GL_DEPTH_TEST);
- // use world program
- shaders.world.use();
- // send projection matrix
- float tan = tanf((0.5f * fovY) * M_PI / 180.0f);
- float q = 1.0f / tan;
- float aspect = (float) width / height;
- float proj[] =
- {
- q / aspect, 0.0f, 0.0f, 0.0f,
- 0.0f, q, 0.0f, 0.0f,
- 0.0f, 0.0f, (nearClip + farClip) / (nearClip - farClip), -1.0f,
- 0.0f, 0.0f, (2.0f * nearClip * farClip) / (nearClip - farClip), 0.0f
- };
- shaders.world.setMatrix("proj", proj);
- // send view matrix
- game.cam.update(lag);
- const Vector right = game.cam.getRight();
- const Vector up = game.cam.getUp();
- const Vector back = game.cam.getBack();
- const Vector pos = game.cam.getPosition();
- float view[] =
- {
- right.getX(), up.getX(), back.getX(), 0.0f,
- right.getY(), up.getY(), back.getY(), 0.0f,
- right.getZ(), up.getZ(), back.getZ(), 0.0f,
- right.dotInverse(pos), up.dotInverse(pos), back.dotInverse(pos), 1.0f
- };
- shaders.world.setMatrix("view", view);
- // clear and send model matrix
- game.model.clear();
- shaders.world.setMatrix("model", game.model.get().getValues());
- // call high level world renderer
- game.game.renderWorld(lag, game.model, shaders.world);
- }
- static void renderTextOverlay(float lag, Shaders& shaders, InternGame& game)
- {
- glDisable(GL_DEPTH_TEST);
- shaders.text.use();
-
- Matrix m;
- shaders.text.setMatrix("proj", m.getValues());
- m.translate(-1.0f, 1.0f, 0.0f);
- m.scale(2.0f / width, -2.0f / height, 1.0f);
- shaders.text.setMatrix("view", m.getValues());
- game.model.clear();
- game.model.get().scale(2.0f, 2.0f, 2.0f);
- shaders.text.setMatrix("model", game.model.get().getValues());
-
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glBlendEquation(GL_FUNC_ADD);
- game.game.renderTextOverlay(lag, game.model, shaders.text, game.fontRenderer);
- glDisable(GL_BLEND);
- }
- static void renderTick(float lag, Shaders& shaders, InternGame& game, Framebuffers& fb)
- {
- if(resize)
- {
- fb.resize(width, height);
- resize = false;
- }
- fb.worldBuffer.bind();
- renderWorld(lag, shaders, game);
- glBindFramebuffer(GL_FRAMEBUFFER, 0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
- renderTextOverlay(lag, shaders, game);
- }
- static void loop()
- {
- Shaders shaders;
- if(!shaders.isValid())
- {
- return;
- }
- Framebuffers framebuffers(width, height);
- if(!framebuffers.isValid())
- {
- return;
- }
- InternGame game;
-
- Shader sh("resources/shader/test2Vertex.vs", "resources/shader/test2Fragment.fs");
- Mesh m;
- m.add({-1, -1, 0, 0, 0, 0, 0, 0});
- m.add({-1, 1, 0, 0, 1, 0, 0, 0});
- m.add({ 1, 1, 0, 1, 1, 0, 0, 0});
- m.add({-1, -1, 0, 0, 0, 0, 0, 0});
- m.add({ 1, -1, 0, 1, 0, 0, 0, 0});
- m.add({ 1, 1, 0, 1, 1, 0, 0, 0});
- m.build();
-
- u64 lastTime = getTimeNanos();
- u64 lag = 0;
- while(!glfwWindowShouldClose(client.window))
- {
- renderTick(lag * lagFactor, shaders, game, framebuffers);
- framebuffers.worldBuffer.bindColorTexture(1);
- sh.use();
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glBlendEquation(GL_FUNC_ADD);
- m.draw();
- glDisable(GL_BLEND);
- glfwSwapBuffers(client.window);
-
- u64 newTime = getTimeNanos();
- lag += newTime - lastTime;
- lastTime = newTime;
-
- while(lag >= NANOS_PER_TICK)
- {
- lag -= NANOS_PER_TICK;
- tick(game);
- }
- glfwPollEvents();
- }
- }
- void GameClient::start(int w, int h, const char* windowName)
- {
- if(initGLFW() || initWindow(w, h, windowName) || initGLEW())
- {
- return;
- }
- initCallbacks();
- glfwShowWindow(client.window);
- loop();
- }
|