Skip to content

Commit

Permalink
Refactor and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Nov 11, 2023
1 parent b4a3608 commit 63b5f4b
Show file tree
Hide file tree
Showing 25 changed files with 391 additions and 275 deletions.
2 changes: 1 addition & 1 deletion src/coders/png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
50 changes: 13 additions & 37 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<json::JObject> 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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
27 changes: 1 addition & 26 deletions src/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <memory>
#include <stdexcept>
#include "typedefs.h"
#include "settings.h"

class Assets;
class Level;
Expand All @@ -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) {}
Expand Down
14 changes: 13 additions & 1 deletion src/frontend/gui/GUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,3 +91,7 @@ bool GUI::isFocusCaught() const {
void GUI::add(shared_ptr<UINode> panel) {
container->add(panel);
}

void GUI::interval(float interval, ontimeout callback) {
intervalEvents.push_back({callback, interval, 0.0f});
}
13 changes: 12 additions & 1 deletion src/frontend/gui/GUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <memory>
#include <vector>
#include <glm/glm.hpp>
#include <functional>

class Batch2D;
class Assets;
Expand Down Expand Up @@ -43,21 +44,31 @@ namespace gui {
class UINode;
class Container;

typedef std::function<void()> ontimeout;
struct IntervalEvent {
ontimeout callback;
float interval;
float timer;
};

class GUI {
Container* container;
std::shared_ptr<UINode> hover = nullptr;
std::shared_ptr<UINode> pressed = nullptr;
std::shared_ptr<UINode> focus = nullptr;
std::vector<IntervalEvent> intervalEvents;
public:
GUI();
~GUI();

std::shared_ptr<UINode> getFocused() const;
bool isFocusCaught() const;

void act();
void act(float delta);
void draw(Batch2D* batch, Assets* assets);
void add(std::shared_ptr<UINode> panel);

void interval(float interval, ontimeout callback);
};
}

Expand Down
24 changes: 0 additions & 24 deletions src/frontend/gui/Panel.cpp

This file was deleted.

24 changes: 0 additions & 24 deletions src/frontend/gui/Panel.h

This file was deleted.

41 changes: 25 additions & 16 deletions src/frontend/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"-");
Expand All @@ -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<Label>(create_label([this](){
Expand All @@ -74,19 +74,24 @@ HudRenderer::HudRenderer(GUI* gui, Level* level, Assets* assets) : level(level),
})));
for (int ax = 0; ax < 3; ax++){
Panel* sub = new Panel(vec2(10, 27), vec4(0.0f));
sub->orientation(gui::Orientation::horizontal);
sub->orientation(Orientation::horizontal);

Label* label = new Label(wstring({L'x'+ax})+L": ");
label->margin(vec4(2, 3, 2, 3));
sub->add(shared_ptr<UINode>(label));
sub->color(vec4(0.0f));

// Coordinate input
TextBox* box = new TextBox(L"");
box->textSupplier([this, ax]() {
Hitbox* hitbox = this->level->player->hitbox;
return std::to_wstring((int)hitbox->position[ax]);
});
box->textConsumer([this, ax](wstring text) {
try {
this->level->player->hitbox->position[ax] = std::stoi(text);
vec3 position = this->level->player->hitbox->position;
position[ax] = std::stoi(text);
this->level->player->teleport(position);
} catch (std::invalid_argument& _){
}
});
Expand Down Expand Up @@ -125,12 +130,7 @@ HudRenderer::~HudRenderer() {

void HudRenderer::drawDebug(int fps, bool occlusion){
this->occlusion = occlusion;
if (fpsFrame % 60 == 0) {
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
fpsMin = fps;
fpsMax = fps;
}
fpsFrame++;
this->fps = fps;
fpsMin = min(fps, fpsMin);
fpsMax = max(fps, fpsMax);
}
Expand Down Expand Up @@ -240,6 +240,7 @@ void HudRenderer::draw(){
batch->line(width/2-5, height/2-5, width/2+5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
batch->line(width/2+5, height/2-5, width/2-5, height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
}
Player* player = level->player;

batch->rect(Window::width/2-128-4, Window::height-80-4, 256+8, 64+8,
0.95f, 0.95f, 0.95f, 0.85f, 0.85f, 0.85f,
Expand All @@ -259,7 +260,7 @@ void HudRenderer::draw(){
0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 2);

batch->texture(blocks);
Player* player = level->player;

{
Block* cblock = Block::blocks[player->choosenBlock];
if (cblock->model == BLOCK_MODEL_CUBE){
Expand Down Expand Up @@ -296,3 +297,11 @@ void HudRenderer::draw(){
}
batch->render();
}

bool HudRenderer::isInventoryOpen() const {
return inventoryOpen;
}

bool HudRenderer::isPause() const {
return pause;
}
5 changes: 4 additions & 1 deletion src/frontend/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class HudRenderer {
Batch2D* batch;
Camera* uicamera;

int fps = 60;
int fpsMin = 60;
int fpsMax = 60;
int fpsFrame = 0;
std::wstring fpsString;
bool occlusion;
bool inventoryOpen = false;
Expand All @@ -38,6 +38,9 @@ class HudRenderer {
void drawInventory(Player* player);
void draw();
void drawDebug(int fps, bool occlusion);

bool isInventoryOpen() const;
bool isPause() const;
};

#endif /* SRC_HUD_H_ */
Loading

0 comments on commit 63b5f4b

Please sign in to comment.