#include #include #include #include #include "GameClient.h" #include "common/utils/Types.h" #include "client/Keys.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 InternGameClient client; static Keys keys; 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); } }); //glfwSetMouseButtonCallback(window, onMouseClick); //glfwSetFramebufferSizeCallback(window, onWindowResize); //glfwSetCursorPosCallback(window, onMouseMove); } static void tick() { std::cout << keys.down << std::endl; keys.tick(); } static void renderTick(float /*lag*/) { } static u64 getTimeNanos() { return glfwGetTimerValue() * timeFactor; } static void loop() { u64 lastTime = getTimeNanos(); u64 lag = 0; while(!glfwWindowShouldClose(client.window)) { renderTick(lag * lagFactor); glfwSwapBuffers(client.window); u64 newTime = getTimeNanos(); lag += newTime - lastTime; lastTime = newTime; while(lag >= NANOS_PER_TICK) { lag -= NANOS_PER_TICK; tick(); } glfwPollEvents(); } } void GameClient::start(int w, int h, const char* windowName) { if(initGLFW() || initWindow(w, h, windowName) || initGLEW()) { return; } initCallbacks(); glfwShowWindow(client.window); loop(); } /*#include #include #include "client/GameClient.h" #include "common/block/Blocks.h" #include "client/rendering/block/BlockRenderers.h" #include "client/rendering/entity/EntityRenderer.h" #include "client/rendering/gui/StartMenu.h" using namespace std; GameClient::GameClient() { BlockRegistry::registerBlocks(); BlockRenderers::init(); keyManager.map(KEY_LEFT, GLFW_KEY_A); keyManager.map(KEY_RIGHT, GLFW_KEY_D); keyManager.map(KEY_UP, GLFW_KEY_W); keyManager.map(KEY_DOWN, GLFW_KEY_S); keyManager.map(KEY_JUMP, GLFW_KEY_SPACE); keyManager.map(KEY_SNEAK, GLFW_KEY_LEFT_SHIFT); keyManager.map(KEY_TEST, GLFW_KEY_T); mouseManager.map(MOUSE_LEFT, GLFW_MOUSE_BUTTON_1); activeGUI = new StartMenu(); } GameClient::~GameClient() { if(activeGUI != nullptr) { delete activeGUI; } } void GameClient::tick() { tps.update(); diffMouseY = 0; diffMouseX = 0; mouseManager.tick(); keyManager.tick(); if(activeGUI != nullptr) { GUI* newGUI = activeGUI->tick(keyManager, mouseManager); if(newGUI != activeGUI) { delete activeGUI; activeGUI = newGUI; } } } void GameClient::render3DTick(float lag) { fps.update(); } void GameClient::render2DTick(float lag) { if(activeGUI != nullptr) { activeGUI->render2DTick(shader, directRenderer, lag); } Engine::setMixMode(); shader.setToIdentity(); shader.updateModelMatrix(); string wusi; wusi = "FPS: " + to_string(fps.getUpdatesPerSecond()); float y = directRenderer.drawString(10, 10, true, wusi); wusi = "TPS: " + to_string(tps.getUpdatesPerSecond()); directRenderer.drawString(10, y, true, wusi); } void GameClient::onKeyEvent(int key, int scancode, int action, int mods) { if(action == GLFW_PRESS) { keyManager.press(key); } else if(action == GLFW_RELEASE) { keyManager.release(key); } } double GameClient::transformMouse(double x) { if(x >= -MOUSE_BORDER && x <= MOUSE_BORDER) { return x * x * x; } else if(x >= MOUSE_BORDER) { const double k = (3 * MOUSE_BORDER * MOUSE_BORDER); const double y = MOUSE_BORDER * MOUSE_BORDER * MOUSE_BORDER; const double d = y - k * MOUSE_BORDER; return k * x + d; } else { const double k = (3 * MOUSE_BORDER * MOUSE_BORDER); const double y = -MOUSE_BORDER * MOUSE_BORDER * MOUSE_BORDER; const double d = y - k * MOUSE_BORDER; return k * x + d; } } void GameClient::onMouseMove(double x, double y) { if(mouseTrapped) { diffMouseX = transformMouse(oldMouseX - x) * (1 / (MOUSE_BORDER * MOUSE_BORDER * 3)); diffMouseY = transformMouse(oldMouseY - y) * (1 / (MOUSE_BORDER * MOUSE_BORDER * 3)); oldMouseX = x; oldMouseY = y; } } void GameClient::onMouseClick(int button, int action, int mods) { if(action == GLFW_PRESS) { mouseManager.press(button); } else if(action == GLFW_RELEASE) { mouseManager.release(button); } }*/