Common refactor

This commit is contained in:
MihailRis 2023-12-13 14:18:24 +03:00
parent 4de8759ea2
commit cb13b42960
10 changed files with 31 additions and 201 deletions

View File

@ -20,6 +20,7 @@
#include "graphics/ImageData.h"
#include "frontend/gui/GUI.h"
#include "frontend/screens.h"
#include "frontend/menu.h"
#include "util/platform.h"
#include "coders/json.h"
@ -67,7 +68,7 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths, Content* content)
if (settings.ui.language == "auto") {
settings.ui.language = platform::detect_locale();
}
langs::setup(resdir, settings.ui.language, contentPacks);
setLanguage(settings.ui.language);
std::cout << "-- initializing finished" << std::endl;
}
@ -153,4 +154,10 @@ vector<ContentPack>& Engine::getContentPacks() {
EnginePaths* Engine::getPaths() {
return paths;
}
}
void Engine::setLanguage(string locale) {
settings.ui.language = locale;
langs::setup(paths->getResources(), locale, contentPacks);
menus::create_menus(this, gui->getMenu());
}

View File

@ -53,6 +53,7 @@ public:
EnginePaths* getPaths();
const Content* getContent() const;
std::vector<ContentPack>& getContentPacks();
void setLanguage(std::string locale);
};
#endif // SRC_ENGINE_H_

View File

@ -218,14 +218,6 @@ path WorldFiles::getIndicesFile() const {
return directory/path("indices.json");
}
path WorldFiles::getOldPlayerFile() const {
return directory/path("player.bin");
}
path WorldFiles::getOldWorldFile() const {
return directory/path("world.bin");
}
ubyte* WorldFiles::getChunk(int x, int z){
return getData(regions, getRegionsFolder(), x, z);
}
@ -405,74 +397,9 @@ void WorldFiles::writeWorldInfo(const World* world) {
files::write_string(getWorldFile(), json::stringify(&root, true, " "));
}
// TODO: remove in v0.16
bool WorldFiles::readOldWorldInfo(World* world) {
size_t length = 0;
ubyte* data = (ubyte*)files::read_bytes(getOldWorldFile(), length);
assert(data != nullptr);
BinaryReader inp(data, length);
inp.checkMagic(WORLD_FORMAT_MAGIC, 8);
/*ubyte version = */inp.get();
while (inp.hasNext()) {
ubyte section = inp.get();
switch (section) {
case WORLD_SECTION_MAIN:
world->seed = inp.getInt64();
world->name = inp.getString();
break;
case WORLD_SECTION_DAYNIGHT:
world->daytime = inp.getFloat32();
world->daytimeSpeed = inp.getFloat32();
break;
}
}
return true;
}
bool WorldFiles::readOldPlayer(Player* player) {
size_t length = 0;
ubyte* data = (ubyte*)files::read_bytes(getOldPlayerFile(), length);
if (data == nullptr){
std::cerr << "could not to read player.bin (ignored)" << std::endl;
return false;
}
vec3 position = player->hitbox->position;
BinaryReader inp(data, length);
while (inp.hasNext()) {
ubyte section = inp.get();
switch (section) {
case SECTION_POSITION:
position.x = inp.getFloat32();
position.y = inp.getFloat32();
position.z = inp.getFloat32();
break;
case SECTION_ROTATION:
player->camX = inp.getFloat32();
player->camY = inp.getFloat32();
break;
case SECTION_FLAGS:
{
ubyte flags = inp.get();
player->flight = flags & PLAYER_FLAG_FLIGHT;
player->noclip = flags & PLAYER_FLAG_NOCLIP;
}
break;
}
}
player->hitbox->position = position;
player->camera->position = position + vec3(0, 1, 0);
return true;
}
// ----- // ----- //
bool WorldFiles::readWorldInfo(World* world) {
path file = getWorldFile();
if (!fs::is_regular_file(file)) {
// TODO: remove in v0.16
file = getOldWorldFile();
if (fs::is_regular_file(file)) {
return readOldWorldInfo(world);
}
std::cerr << "warning: world.json does not exists" << std::endl;
return false;
}
@ -519,11 +446,6 @@ void WorldFiles::writePlayer(Player* player){
bool WorldFiles::readPlayer(Player* player) {
path file = getPlayerFile();
if (!fs::is_regular_file(file)) {
// TODO: remove in v0.16
file = getOldPlayerFile();
if (fs::is_regular_file(file)) {
readOldPlayer(player);
}
std::cerr << "warning: player.json does not exists" << std::endl;
return false;
}

View File

@ -54,14 +54,7 @@ class WorldFiles {
std::filesystem::path getPlayerFile() const;
std::filesystem::path getWorldFile() const;
std::filesystem::path getIndicesFile() const;
// TODO: remove in 0.16
std::filesystem::path getOldPlayerFile() const;
std::filesystem::path getOldWorldFile() const;
bool readOldWorldInfo(World* world);
bool readOldPlayer(Player* player);
// --------------------
WorldRegion* getRegion(std::unordered_map<glm::ivec2, WorldRegion*>& regions,
int x, int z);

View File

@ -45,6 +45,19 @@ Panel* create_settings_panel(Engine* engine, PagesControl* menu);
Panel* create_pause_panel(Engine* engine, PagesControl* menu);
Panel* create_languages_panel(Engine* engine, PagesControl* menu);
void menus::create_menus(Engine* engine, PagesControl* menu) {
menu->add("new-world", create_new_world_panel(engine, menu));
menu->add("settings", create_settings_panel(engine, menu));
menu->add("controls", create_controls_panel(engine, menu));
menu->add("pause", create_pause_panel(engine, menu));
menu->add("languages", create_languages_panel(engine, menu));
menu->add("main", create_main_menu_panel(engine, menu));
}
void menus::refresh_menus(Engine* engine, PagesControl* menu) {
menu->add("main", create_main_menu_panel(engine, menu));
}
void show_content_missing(GUI* gui, const Content* content, ContentLUT* lut) {
PagesControl* menu = gui->getMenu();
Panel* panel = new Panel(vec2(500, 200), vec4(8.0f), 8.0f);
@ -98,15 +111,6 @@ void show_convert_request(GUI* gui, const Content* content, ContentLUT* lut,
}, L"", langs::get(L"Cancel"));
}
void create_menus(Engine* engine, PagesControl* menu) {
menu->add("new-world", create_new_world_panel(engine, menu));
menu->add("settings", create_settings_panel(engine, menu));
menu->add("controls", create_controls_panel(engine, menu));
menu->add("pause", create_pause_panel(engine, menu));
menu->add("languages", create_languages_panel(engine, menu));
menu->add("main", create_main_menu_panel(engine, menu));
}
Panel* create_languages_panel(Engine* engine, PagesControl* menu) {
Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f);
panel->scrollable(true);
@ -122,9 +126,7 @@ Panel* create_languages_panel(Engine* engine, PagesControl* menu) {
Button* button = new Button(util::str2wstr_utf8(fullName), vec4(10.f));
button->listenAction([=](GUI*) {
auto resdir = engine->getPaths()->getResources();
langs::setup(resdir, name, engine->getContentPacks());
engine->getSettings().ui.language = name;
create_menus(engine, menu);
engine->setLanguage(name);
menu->back();
});
panel->add(button);

View File

@ -7,6 +7,9 @@ namespace gui {
class PagesControl;
}
void create_menus(Engine* engine, gui::PagesControl* menu);
namespace menus {
void create_menus(Engine* engine, gui::PagesControl* menu);
void refresh_menus(Engine* engine, gui::PagesControl* menu);
}
#endif // FRONTEND_MENU_H_

View File

@ -43,9 +43,7 @@ using std::shared_ptr;
MenuScreen::MenuScreen(Engine* engine_) : Screen(engine_) {
auto menu = engine->getGUI()->getMenu();
// Create pages if not created yet
create_menus(engine, menu);
menus::refresh_menus(engine, menu);
menu->reset();
menu->set("main");
@ -125,23 +123,6 @@ void LevelScreen::updateHotkeys() {
if (Events::jpressed(keycode::F5)) {
level->chunks->saveAndClear();
}
// TODO: remove in v0.16
if (Events::jpressed(keycode::F9)) {
blockid_t woodid = level->content->requireBlock("base:wood")->rt.id;
for (size_t i = 0; i < level->chunks->volume; i++){
Chunk* chunk = level->chunks->chunks[i].get();
if (chunk) {
for (uint i = 0; i < CHUNK_VOL; i++) {
auto& vox = chunk->voxels[i];
if (vox.id == woodid) {
vox.states = BLOCK_DIR_UP;
}
}
}
}
level->chunks->saveAndClear();
}
}
void LevelScreen::update(float delta) {

View File

@ -303,78 +303,6 @@ void Batch2D::sprite(float x, float y, float w, float h, int atlasRes, int index
rect(x, y, w, h, u, v, scale, scale, tint.r, tint.g, tint.b, tint.a);
}
#include <iostream>
void Batch2D::blockSprite(float x, float y, float w, float h, const UVRegion regions[], vec4 tint, vec3 size){
// TODO: replace it using actual 3D with ortho projection
float uu = (regions[3].u1);
float vu = (regions[3].v1);
float uf = (regions[0].u1);
float vf = (regions[0].v1);
float scalex = regions[3].u2-regions[3].u1;
float scaley = regions[3].v2-regions[3].v1;
if (this->index + 18*B2D_VERTEX_SIZE >= capacity)
render();
float d = (w + h) * 0.5f;
float ar = 0.88f;
float ox = x + (w * 0.5f);
float sx = w * 0.5f * ar;
float hh = h * 0.25f;
float ww = w * 0.25f;
float dd = d * 0.25f;
vec3 half = size * h * 0.25f;
y += hh * 2.0f;
vec2 points[7] = {vec2(ox-ww+half.x+dd-half.z, y+hh-half.y), // center
vec2(ox-sx+ww-half.x+dd-half.z, y-half.y+ww-half.x), // left
vec2(ox+ww-half.x-dd+half.z, y-hh-half.y+ww-half.x+dd-half.z), // top
vec2(ox+sx-ww+half.x-dd+half.z, y-half.y+dd-half.z), // right
vec2(ox+sx-ww+half.x-dd+half.z, y+half.y+dd-half.z), // b-right
vec2(ox-ww+half.x+dd-half.z, y+hh+half.y), // bottom
vec2(ox-sx+ww-half.x+dd-half.z, y+half.y+ww-half.x)}; // b-left
vec2 uvpoints[8] = {vec2(uu, vu),
vec2(uu+scalex, vu),
vec2(uu+scalex, vu+scalex),
vec2(uu, vu+scalex),
vec2(uf, vf),
vec2(uf+scaley, vf),
vec2(uf+scaley, vf+scaley),
vec2(uf, vf+scaley)};
vertex(points[0], uvpoints[3], tint.r, tint.g, tint.b, tint.a);
vertex(points[1], uvpoints[0], tint.r, tint.g, tint.b, tint.a);
vertex(points[2], uvpoints[1], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[3], tint.r, tint.g, tint.b, tint.a);
vertex(points[2], uvpoints[1], tint.r, tint.g, tint.b, tint.a);
vertex(points[3], uvpoints[2], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[7], tint.r, tint.g, tint.b, tint.a);
vertex(points[3], uvpoints[6], tint.r, tint.g, tint.b, tint.a);
vertex(points[4], uvpoints[5], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[7], tint.r, tint.g, tint.b, tint.a);
vertex(points[4], uvpoints[5], tint.r, tint.g, tint.b, tint.a);
vertex(points[5], uvpoints[4], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[6], tint.r, tint.g, tint.b, tint.a);
vertex(points[5], uvpoints[5], tint.r, tint.g, tint.b, tint.a);
vertex(points[6], uvpoints[4], tint.r, tint.g, tint.b, tint.a);
vertex(points[0], uvpoints[6], tint.r, tint.g, tint.b, tint.a);
vertex(points[6], uvpoints[4], tint.r, tint.g, tint.b, tint.a);
vertex(points[1], uvpoints[7], tint.r, tint.g, tint.b, tint.a);
}
void Batch2D::render(unsigned int gl_primitive) {
mesh->reload(buffer, index / B2D_VERTEX_SIZE);
mesh->draw(gl_primitive);

View File

@ -38,7 +38,6 @@ public:
void sprite(float x, float y, float w, float h, const UVRegion& region, glm::vec4 tint);
void sprite(Sprite* sprite);
void sprite(float x, float y, float w, float h, int atlasRes, int index, glm::vec4 tint);
void blockSprite(float x, float y, float w, float h, const UVRegion regions[], glm::vec4 tint, glm::vec3 size=glm::vec3(1.0f, 1.0f, 1.0f));
void point(float x, float y, float r, float g, float b, float a);
void line(float x1, float y1, float x2, float y2, float r, float g, float b, float a);
void rect(float x, float y,

View File

@ -177,7 +177,7 @@ void Chunks::set(int x, int y, int z, int id, uint8_t states){
if (lz == CHUNK_D-1 && (chunk = getChunk(cx+ox, cz+oz+1)))
chunk->setModified(true);
}
#include "../util/timeutil.h"
voxel* Chunks::rayCast(vec3 start,
vec3 dir,
float maxDist,
@ -321,8 +321,6 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) {
float tyMax = (tyDelta < infinity) ? tyDelta * ydist : infinity;
float tzMax = (tzDelta < infinity) ? tzDelta * zdist : infinity;
int steppedIndex = -1;
while (t <= maxDist) {
voxel* voxel = get(ix, iy, iz);
if (!voxel) { return vec3(px + t * dx, py + t * dy, pz + t * dz); }
@ -349,13 +347,11 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) {
ix += stepx;
t = txMax;
txMax += txDelta;
steppedIndex = 0;
}
else {
iz += stepz;
t = tzMax;
tzMax += tzDelta;
steppedIndex = 2;
}
}
else {
@ -363,13 +359,11 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) {
iy += stepy;
t = tyMax;
tyMax += tyDelta;
steppedIndex = 1;
}
else {
iz += stepz;
t = tzMax;
tzMax += tzDelta;
steppedIndex = 2;
}
}
}