diff --git a/src/coders/png.cpp b/src/coders/png.cpp index 190897622..19ba6120a 100644 --- a/src/coders/png.cpp +++ b/src/coders/png.cpp @@ -106,7 +106,7 @@ int _png_load(const char* file, int* width, int* height){ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); diff --git a/src/engine.cpp b/src/engine.cpp index 52d455d0a..3c29967dd 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -30,40 +30,12 @@ using std::shared_ptr; using glm::vec3; using gui::GUI; -void load_settings(EngineSettings& settings, std::string filename) { - std::string source = files::read_string(filename); - std::unique_ptr obj(json::parse(filename, source)); - obj->num("display-width", settings.displayWidth); - obj->num("display-height", settings.displayHeight); - obj->num("display-samples", settings.displaySamples); - obj->num("display-swap-interval", settings.displaySwapInterval); - obj->num("chunks-load-distance", settings.chunksLoadDistance); - obj->num("chunks-load-speed", settings.chunksLoadSpeed); - obj->num("chunks-padding", settings.chunksPadding); - obj->num("fog-curve", settings.fogCurve); -} -void save_settings(EngineSettings& settings, std::string filename) { - json::JObject obj; - obj.put("display-width", settings.displayWidth); - obj.put("display-height", settings.displayHeight); - obj.put("display-samples", settings.displaySamples); - obj.put("display-swap-interval", settings.displaySwapInterval); - obj.put("chunks-load-distance", settings.chunksLoadDistance); - obj.put("chunks-load-speed", settings.chunksLoadSpeed); - obj.put("chunks-padding", settings.chunksPadding); - obj.put("fog-curve", settings.fogCurve); - files::write_string(filename, json::stringify(&obj, true, " ")); -} -Engine::Engine(const EngineSettings& settings) { - this->settings = settings; +Engine::Engine(const EngineSettings& settings_) { + this->settings = settings_; - Window::initialize(settings.displayWidth, - settings.displayHeight, - settings.displayTitle, - settings.displaySamples); - Window::swapInterval(settings.displaySwapInterval); + Window::initialize(settings.display); assets = new Assets(); std::cout << "-- loading assets" << std::endl; @@ -82,7 +54,7 @@ Engine::Engine(const EngineSettings& settings) { Camera* camera = new Camera(playerPosition, radians(90.0f)); World* world = new World("world-1", "world/", 42); Player* player = new Player(playerPosition, 4.0f, camera); - level = world->loadLevel(player, settings.chunksLoadDistance, settings.chunksPadding); + level = world->loadLevel(player, settings); std::cout << "-- initializing finished" << std::endl; @@ -116,8 +88,9 @@ void Engine::updateHotkeys() { } void Engine::mainloop() { - Camera* camera = level->player->camera; std::cout << "-- preparing systems" << std::endl; + + Camera* camera = level->player->camera; WorldRenderer worldRenderer(level, assets); HudRenderer hud(gui, level, assets); Batch2D batch(1024); @@ -127,15 +100,18 @@ void Engine::mainloop() { updateTimers(); updateHotkeys(); - level->update(delta, Events::_cursor_locked, Events::_cursor_locked); - level->chunksController->update(settings.chunksLoadSpeed); + bool inputLocked = hud.isPause() || hud.isInventoryOpen() || gui->isFocusCaught(); + level->updatePlayer(delta, !inputLocked, hud.isPause(), !inputLocked); + level->update(); + level->chunksController->update(settings.chunks.loadSpeed); - worldRenderer.draw(camera, occlusion, 1.6f / (float)settings.chunksLoadDistance, settings.fogCurve); + float fovFactor = 1.6f / (float)settings.chunks.loadDistance; + worldRenderer.draw(camera, occlusion, fovFactor, settings.fogCurve); hud.draw(); if (level->player->debug) { hud.drawDebug( 1 / delta, occlusion); } - gui->act(); + gui->act(delta); gui->draw(&batch, assets); Window::swapBuffers(); diff --git a/src/engine.h b/src/engine.h index 4d1ff5a66..2a2ce28f3 100644 --- a/src/engine.h +++ b/src/engine.h @@ -5,6 +5,7 @@ #include #include #include "typedefs.h" +#include "settings.h" class Assets; class Level; @@ -13,32 +14,6 @@ namespace gui { class GUI; } -struct EngineSettings { - /* Window width (pixels) */ - int displayWidth; - /* Window height (pixels) */ - int displayHeight; - /* Anti-aliasing samples */ - int displaySamples; - /* GLFW swap interval value, 0 - unlimited fps, 1 - vsync*/ - int displaySwapInterval; - /* Window title */ - const char* displayTitle; - /* Max milliseconds that engine uses for chunks loading only */ - uint chunksLoadSpeed; - /* Radius of chunks loading zone (chunk is unit) */ - uint chunksLoadDistance; - /* Buffer zone where chunks are not unloading (chunk is unit)*/ - uint chunksPadding; - /* Fog opacity is calculated as `pow(depth*k, fogCurve)` where k depends on chunksLoadDistance. - Use values in range [1.0 - 2.0] where 1.0 is linear, 2.0 is quadratic - */ - float fogCurve; -}; - -void load_settings(EngineSettings& settings, std::string filename); -void save_settings(EngineSettings& settings, std::string filename); - class initialize_error : public std::runtime_error { public: initialize_error(const std::string& message) : std::runtime_error(message) {} diff --git a/src/frontend/gui/GUI.cpp b/src/frontend/gui/GUI.cpp index dc80d7f86..d2cbb85be 100644 --- a/src/frontend/gui/GUI.cpp +++ b/src/frontend/gui/GUI.cpp @@ -20,7 +20,15 @@ GUI::~GUI() { delete container; } -void GUI::act() { +void GUI::act(float delta) { + for (IntervalEvent& event : intervalEvents) { + event.timer += delta; + if (event.timer > event.interval) { + event.callback(); + event.timer = fmod(event.timer, event.interval); + } + } + container->size(vec2(Window::width, Window::height)); int mx = Events::x; int my = Events::y; @@ -83,3 +91,7 @@ bool GUI::isFocusCaught() const { void GUI::add(shared_ptr panel) { container->add(panel); } + +void GUI::interval(float interval, ontimeout callback) { + intervalEvents.push_back({callback, interval, 0.0f}); +} \ No newline at end of file diff --git a/src/frontend/gui/GUI.h b/src/frontend/gui/GUI.h index 6cc5bb11f..4ce8f858e 100644 --- a/src/frontend/gui/GUI.h +++ b/src/frontend/gui/GUI.h @@ -4,6 +4,7 @@ #include #include #include +#include class Batch2D; class Assets; @@ -43,11 +44,19 @@ namespace gui { class UINode; class Container; + typedef std::function ontimeout; + struct IntervalEvent { + ontimeout callback; + float interval; + float timer; + }; + class GUI { Container* container; std::shared_ptr hover = nullptr; std::shared_ptr pressed = nullptr; std::shared_ptr focus = nullptr; + std::vector intervalEvents; public: GUI(); ~GUI(); @@ -55,9 +64,11 @@ namespace gui { std::shared_ptr getFocused() const; bool isFocusCaught() const; - void act(); + void act(float delta); void draw(Batch2D* batch, Assets* assets); void add(std::shared_ptr panel); + + void interval(float interval, ontimeout callback); }; } diff --git a/src/frontend/gui/Panel.cpp b/src/frontend/gui/Panel.cpp deleted file mode 100644 index 61ea914ad..000000000 --- a/src/frontend/gui/Panel.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "Panel.h" - -#include "../../graphics/Batch2D.h" - -using gui::Panel; - -Panel::Panel(glm::vec2 coord, glm::vec2 size, glm::vec4 color) - : coord(coord), size(size), color(color) { - -} - -void Panel::draw(Batch2D* batch) { - batch->texture(nullptr); - batch->color = color; - batch->rect(coord.x, coord.y, size.x, size.y); -} - -bool Panel::isVisible() const { - return visible; -} - -void Panel::setVisible(bool flag) { - visible = flag; -} \ No newline at end of file diff --git a/src/frontend/gui/Panel.h b/src/frontend/gui/Panel.h deleted file mode 100644 index 8cbd9621b..000000000 --- a/src/frontend/gui/Panel.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef FRONTEND_GUI_PANEL_H_ -#define FRONTEND_GUI_PANEL_H_ - -#include - -class Batch2D; - -namespace gui { - class Panel { - glm::vec2 coord; - glm::vec2 size; - glm::vec4 color; - bool visible = true; - public: - Panel(glm::vec2 coord, glm::vec2 size, glm::vec4 color); - - void draw(Batch2D* batch); - - void setVisible(bool flag); - bool isVisible() const; - }; -} - -#endif // FRONTEND_GUI_PANEL_H_ \ No newline at end of file diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index ccaedc1c7..55e186c72 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -34,12 +34,7 @@ using std::shared_ptr; using glm::vec2; using glm::vec3; using glm::vec4; -using gui::GUI; -using gui::UINode; -using gui::Panel; -using gui::Label; -using gui::Button; -using gui::TextBox; +using namespace gui; inline Label* create_label(gui::wstringsupplier supplier) { Label* label = new Label(L"-"); @@ -49,10 +44,15 @@ inline Label* create_label(gui::wstringsupplier supplier) { HudRenderer::HudRenderer(GUI* gui, Level* level, Assets* assets) : level(level), assets(assets), guiController(gui) { batch = new Batch2D(1024); - uicamera = new Camera(glm::vec3(), Window::height); + uicamera = new Camera(vec3(), Window::height); uicamera->perspective = false; uicamera->flipped = true; + gui->interval(1.0f, [this]() { + fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin); + fpsMin = fps; + fpsMax = fps; + }); Panel* panel = new Panel(vec2(200, 200), vec4(5.0f), 1.0f); panel->setCoord(vec2(10, 10)); panel->add(shared_ptr