Refactor: added Screen interface class, LevelScreen

This commit is contained in:
MihailRis 2023-11-15 21:18:10 +03:00
parent 79aa754e66
commit fee25abb5e
11 changed files with 64 additions and 84 deletions

View File

@ -2,6 +2,7 @@
#include <memory>
#include <iostream>
#include <assert.h>
#include <glm/glm.hpp>
#define GLEW_STATIC
@ -24,11 +25,13 @@
#include "frontend/world_render.h"
#include "frontend/hud.h"
#include "frontend/gui/GUI.h"
#include "frontend/screens.h"
#include "util/platform.h"
#include "coders/json.h"
#include "coders/png.h"
#include "files/files.h"
#include "files/engine_files.h"
using std::shared_ptr;
using glm::vec3;
@ -51,15 +54,17 @@ Engine::Engine(const EngineSettings& settings_) {
throw initialize_error("could not to initialize assets");
}
}
std::cout << "-- loading world" << std::endl;
vec3 playerPosition = vec3(0, 64, 0);
Camera* camera = new Camera(playerPosition, radians(90.0f));
World* world = new World("world-1", "world/", 42, settings);
Player* player = new Player(playerPosition, 4.0f, camera);
level = world->loadLevel(player, settings);
Audio::initialize();
gui = new GUI();
std::cout << "-- initializing finished" << std::endl;
std::cout << "-- loading world" << std::endl;
vec3 playerPosition = vec3(0, 64, 0);
Camera* camera = new Camera(playerPosition, radians(90.0f));
World* world = new World("world-1", enginefs::get_worlds_folder()/"world", 42, settings);
Player* player = new Player(playerPosition, 4.0f, camera);
setScreen(new LevelScreen(this, world->loadLevel(player, settings)));
}
void Engine::updateTimers() {
@ -70,54 +75,29 @@ void Engine::updateTimers() {
}
void Engine::updateHotkeys() {
if (Events::jpressed(keycode::O)) {
occlusion = !occlusion;
}
if (Events::jpressed(keycode::F2)) {
ImageData* image = Window::takeScreenshot();
image->flipY();
std::string filename = platform::get_screenshot_file("png");
std::string filename = enginefs::get_screenshot_file("png");
png::write_image(filename, image);
delete image;
std::cout << "saved screenshot as " << filename << std::endl;
}
if (Events::jpressed(keycode::F3)) {
level->player->debug = !level->player->debug;
}
if (Events::jpressed(keycode::F5)) {
for (uint i = 0; i < level->chunks->volume; i++) {
shared_ptr<Chunk> chunk = level->chunks->chunks[i];
if (chunk != nullptr && chunk->isReady()) {
chunk->setModified(true);
}
}
}
}
void Engine::mainloop() {
std::cout << "-- preparing systems" << std::endl;
Camera* camera = level->player->camera;
WorldRenderer worldRenderer(level, assets);
HudRenderer hud(gui, level, assets);
Batch2D batch(1024);
lastTime = Window::time();
while (!Window::isShouldClose()){
assert(screen != nullptr);
updateTimers();
updateHotkeys();
bool inputLocked = hud.isPause() || hud.isInventoryOpen() || gui->isFocusCaught();
level->updatePlayer(delta, !inputLocked, hud.isPause(), !inputLocked);
level->update();
level->chunksController->update(settings.chunks.loadSpeed);
float fovFactor = 18.0f / (float)settings.chunks.loadDistance;
worldRenderer.draw(camera, occlusion, fovFactor, settings.graphics.fogCurve);
hud.draw();
if (level->player->debug) {
hud.drawDebug( 1 / delta, occlusion);
}
screen->update(delta);
screen->draw(delta);
gui->act(delta);
gui->draw(&batch, assets);
@ -127,18 +107,32 @@ void Engine::mainloop() {
}
Engine::~Engine() {
delete screen;
delete gui;
Audio::finalize();
World* world = level->world;
std::cout << "-- saving world" << std::endl;
world->write(level, !settings.debug.generatorTestMode);
delete level;
delete world;
std::cout << "-- shutting down" << std::endl;
delete assets;
Window::terminate();
std::cout << "-- engine finished" << std::endl;
}
GUI* Engine::getGUI() {
return gui;
}
EngineSettings& Engine::getSettings() {
return settings;
}
Assets* Engine::getAssets() {
return assets;
}
void Engine::setScreen(Screen* screen) {
if (this->screen != nullptr) {
delete this->screen;
}
this->screen = screen;
}

View File

@ -9,6 +9,7 @@
class Assets;
class Level;
class Screen;
namespace gui {
class GUI;
@ -21,13 +22,12 @@ public:
class Engine {
Assets* assets;
Level* level;
Screen* screen = nullptr;
EngineSettings settings;
uint64_t frame = 0;
double lastTime = 0.0;
double delta = 0.0;
bool occlusion = true;
gui::GUI* gui;
public:
@ -37,6 +37,11 @@ public:
void updateTimers();
void updateHotkeys();
void mainloop();
Assets* getAssets();
gui::GUI* getGUI();
EngineSettings& getSettings();
void setScreen(Screen* screen);
};
#endif // SRC_ENGINE_H_

View File

@ -16,7 +16,6 @@
#include <memory>
#include <fstream>
#include <iostream>
#include <filesystem>
#define SECTION_POSITION 1
#define SECTION_ROTATION 2
@ -28,6 +27,7 @@ using glm::ivec2;
using glm::vec3;
using std::ios;
using std::unique_ptr;
using std::filesystem::path;
int bytes2Int(const ubyte* src, size_t offset){
return (src[offset] << 24) | (src[offset+1] << 16) | (src[offset+2] << 8) | (src[offset+3]);
@ -56,7 +56,7 @@ float bytes2Float(ubyte* src, uint offset){
return *(float*)(&value);
}
WorldFiles::WorldFiles(std::string directory, size_t mainBufferCapacity, bool generatorTestMode)
WorldFiles::WorldFiles(path directory, size_t mainBufferCapacity, bool generatorTestMode)
: directory(directory), generatorTestMode(generatorTestMode) {
compressionBuffer = new ubyte[CHUNK_DATA_LEN * 2];
}
@ -116,11 +116,11 @@ void WorldFiles::put(Chunk* chunk){
}
std::string WorldFiles::getRegionFile(int x, int y) {
return directory + std::to_string(x) + "_" + std::to_string(y) + ".bin";
return directory/(std::to_string(x) + "_" + std::to_string(y) + ".bin");
}
std::string WorldFiles::getPlayerFile() {
return directory + "/player.bin";
return directory/"player.bin";
}
ubyte* WorldFiles::getChunk(int x, int y){
@ -201,7 +201,7 @@ ubyte* WorldFiles::readChunkData(int x, int y, uint32_t& length){
void WorldFiles::write(){
if (!std::filesystem::is_directory(directory)) {
std::filesystem::create_directory(directory);
std::filesystem::create_directories(directory);
}
if (generatorTestMode)
return;

View File

@ -5,6 +5,7 @@
#include <string>
#include <unordered_map>
#include <string>
#include <filesystem>
#include <glm/glm.hpp>
#define GLM_ENABLE_EXPERIMENTAL
@ -29,11 +30,11 @@ struct WorldRegion {
class WorldFiles {
public:
std::unordered_map<glm::ivec2, WorldRegion> regions;
std::string directory;
std::filesystem::path directory;
ubyte* compressionBuffer;
bool generatorTestMode;
WorldFiles(std::string directory, size_t mainBufferCapacity, bool generatorTestMode);
WorldFiles(std::filesystem::path directory, size_t mainBufferCapacity, bool generatorTestMode);
~WorldFiles();
void put(Chunk* chunk);

View File

@ -77,6 +77,7 @@ void GUI::act(float delta) {
}
void GUI::draw(Batch2D* batch, Assets* assets) {
batch->begin();
container->draw(batch, assets);
}

View File

@ -127,7 +127,6 @@ HudRenderer::HudRenderer(GUI* gui, Level* level, Assets* assets) : level(level),
HudRenderer::~HudRenderer() {
delete batch;
delete uicamera;
delete guiController;
}
void HudRenderer::drawDebug(int fps, bool occlusion){

View File

@ -71,10 +71,10 @@ bool WorldRenderer::drawChunk(size_t index, Camera* camera, Shader* shader, bool
void WorldRenderer::draw(Camera* camera, bool occlusion, float fogFactor, float fogCurve){
Chunks* chunks = level->chunks;
vec4 skyColor(0.7f, 0.81f, 1.0f, 1.0f);
glClearColor(skyColor.r, skyColor.g, skyColor.b, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
vec3 skyColor(0.7f, 0.81f, 1.0f);
Window::setBgColor(skyColor);
Window::clear();
Window::viewport(0, 0, Window::width, Window::height);
glEnable(GL_DEPTH_TEST);

View File

@ -8,7 +8,6 @@
#include "../typedefs.h"
#define SETTINGS_FILE "settings.toml"
#define SCREENSHOTS_FOLDER "screenshots"
using std::string;
@ -17,30 +16,6 @@ string platform::get_settings_file() {
return SETTINGS_FILE;
}
string platform::get_screenshot_file(string ext) {
std::string folder = SCREENSHOTS_FOLDER;
if (!std::filesystem::is_directory(folder)) {
std::filesystem::create_directory(folder);
}
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
const char* format = "%d-%m-%Y_%H-%M-%S";
std::stringstream ss;
ss << std::put_time(&tm, format);
string datetimestr = ss.str();
string filename = folder+"/screenshot-"+datetimestr+"."+ext;
uint index = 0;
while (std::filesystem::exists(filename)) {
filename = folder+"/screenshot-"+datetimestr+"-"+std::to_string(index)+"."+ext;
index++;
}
return filename;
}
#ifdef WIN32
#include <Windows.h>

View File

@ -6,7 +6,6 @@
namespace platform {
extern void configure_encoding();
extern std::string get_settings_file();
extern std::string get_screenshot_file(std::string ext);
}
#endif // UTIL_PLATFORM_H_

View File

@ -110,9 +110,14 @@ int Window::initialize(DisplaySettings& settings){
}
void Window::clear() {
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Window::setBgColor(glm::vec3 color) {
glClearColor(color.r, color.g, color.b, 1.0f);
}
void Window::viewport(int x, int y, int width, int height){
glViewport(x, y, width, height);
}

View File

@ -34,6 +34,7 @@ public:
static void resetScissor();
static void clear();
static void setBgColor(glm::vec3 color);
static double time();
static ImageData* takeScreenshot();