world previews demo

This commit is contained in:
MihailRis 2024-04-23 01:09:30 +03:00
parent 5afa5304ff
commit 6e2bc9ca95
24 changed files with 153 additions and 59 deletions

View File

@ -1,6 +1,6 @@
function on_open()
local worlds = core.get_worlds_list()
for _, name in ipairs(worlds) do
document.worlds:add(gui.template("world", {name=name}))
local worlds = world.get_list()
for _, info in ipairs(worlds) do
document.worlds:add(gui.template("world", info))
end
end

View File

@ -1,10 +1,11 @@
<container
size='380,46'
size='380,66'
color='#0F1E2DB2'
hover-color='#162B3399'
onclick='core.open_world("%{name}")'
>
<label pos='8,8'>%{name}</label>
<image pos='1' src='%{icon}' size='96,64'></image>
<label pos='104,8'>%{name}</label>
<button pos='350,5'
color='#00000000'
hover-color='#FFFFFF2B'

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.2 KiB

After

Width:  |  Height:  |  Size: 953 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 510 B

View File

@ -178,6 +178,7 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) {
loader.add(AssetType::texture, TEXTURES_FOLDER+"/gui/menubg", "gui/menubg");
loader.add(AssetType::texture, TEXTURES_FOLDER+"/gui/delete_icon", "gui/delete_icon");
loader.add(AssetType::texture, TEXTURES_FOLDER+"/gui/no_icon", "gui/no_icon");
loader.add(AssetType::texture, TEXTURES_FOLDER+"/gui/no_world_icon", "gui/no_world_icon");
loader.add(AssetType::texture, TEXTURES_FOLDER+"/gui/warning", "gui/warning");
loader.add(AssetType::texture, TEXTURES_FOLDER+"/gui/error", "gui/error");
loader.add(AssetType::texture, TEXTURES_FOLDER+"/gui/cross", "gui/cross");

View File

@ -77,8 +77,7 @@ static bool appendAtlas(AtlasBuilder& atlas, const fs::path& file) {
}
auto image = imageio::read(file.string());
image->fixAlphaColor();
atlas.add(name, image.release());
atlas.add(name, std::move(image));
return true;
}

View File

@ -141,6 +141,7 @@ toml::Wrapper* create_wrapper(EngineSettings& settings) {
toml::Section& ui = wrapper->add("ui");
ui.add("language", &settings.ui.language);
ui.add("world-preview-size", &*settings.ui.worldPreviewSize);
return wrapper.release();
}

View File

@ -2,9 +2,13 @@
#include "../hud.h"
#include "../LevelFrontend.h"
#include "../../debug/Logger.h"
#include "../../audio/audio.h"
#include "../../coders/imageio.h"
#include "../../graphics/core/PostProcessing.h"
#include "../../graphics/core/GfxContext.h"
#include "../../graphics/core/Viewport.h"
#include "../../graphics/core/ImageData.h"
#include "../../graphics/ui/GUI.h"
#include "../../graphics/ui/elements/Menu.hpp"
#include "../../graphics/render/WorldRenderer.h"
@ -18,7 +22,11 @@
#include "../../window/Events.h"
#include "../../engine.h"
static debug::Logger logger("level-screen");
LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine) {
postProcessing = std::make_unique<PostProcessing>();
auto& settings = engine->getSettings();
auto assets = engine->getAssets();
auto menu = engine->getGUI()->getMenu();
@ -53,11 +61,31 @@ LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine) {
}
LevelScreen::~LevelScreen() {
saveWorldPreview();
scripting::on_frontend_close();
controller->onWorldQuit();
engine->getPaths()->setWorldFolder(fs::path());
}
void LevelScreen::saveWorldPreview() {
try {
logger.info() << "saving world preview";
auto paths = engine->getPaths();
auto player = controller->getPlayer();
auto camera = player->camera;
auto& settings = engine->getSettings();
int previewSize = settings.ui.worldPreviewSize.get();
Viewport viewport(previewSize * 1.5, previewSize);
GfxContext ctx(nullptr, viewport, batch.get());
worldRenderer->draw(ctx, camera.get(), false, postProcessing.get());
auto image = postProcessing->toImage();
image->flipY();
imageio::write(paths->resolve("world:preview.png"), image.get());
} catch (const std::exception& err) {
logger.error() << err.what();
}
}
void LevelScreen::updateHotkeys() {
auto& settings = engine->getSettings();
if (Events::jpressed(keycode::O)) {
@ -111,7 +139,7 @@ void LevelScreen::draw(float delta) {
Viewport viewport(Window::width, Window::height);
GfxContext ctx(nullptr, viewport, batch.get());
worldRenderer->draw(ctx, camera.get(), hudVisible);
worldRenderer->draw(ctx, camera.get(), hudVisible, postProcessing.get());
if (hudVisible) {
hud->draw(ctx);

View File

@ -11,6 +11,7 @@ class Hud;
class LevelController;
class WorldRenderer;
class TextureAnimator;
class PostProcessing;
class Level;
class LevelScreen : public Screen {
@ -19,6 +20,9 @@ class LevelScreen : public Screen {
std::unique_ptr<LevelController> controller;
std::unique_ptr<WorldRenderer> worldRenderer;
std::unique_ptr<TextureAnimator> animator;
std::unique_ptr<PostProcessing> postProcessing;
void saveWorldPreview();
bool hudVisible = true;
void updateHotkeys();

View File

@ -41,8 +41,8 @@ ImageData* Atlas::getImage() const {
return image.get();
}
void AtlasBuilder::add(std::string name, ImageData* image) {
entries.push_back(atlasentry{name, std::shared_ptr<ImageData>(image)});
void AtlasBuilder::add(std::string name, std::unique_ptr<ImageData> image) {
entries.push_back(atlasentry{name, std::shared_ptr<ImageData>(image.release())});
names.insert(name);
}

View File

@ -46,7 +46,7 @@ class AtlasBuilder {
std::set<std::string> names;
public:
AtlasBuilder() {}
void add(std::string name, ImageData* image);
void add(std::string name, std::unique_ptr<ImageData> image);
bool has(const std::string& name) const;
const std::set<std::string>& getNames() { return names; };

View File

@ -13,17 +13,10 @@ Framebuffer::Framebuffer(uint fbo, uint depth, std::unique_ptr<Texture> texture)
width = 0;
height = 0;
}
}
}
Framebuffer::Framebuffer(uint width, uint height, bool alpha)
: width(width), height(height)
{
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// Setup color attachment (texture)
static std::unique_ptr<Texture> create_texture(int width, int height, int format) {
GLuint tex;
format = alpha ? GL_RGBA : GL_RGB;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, nullptr);
@ -32,7 +25,19 @@ Framebuffer::Framebuffer(uint width, uint height, bool alpha)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
texture = std::make_unique<Texture>(tex, width, height);
return std::make_unique<Texture>(tex, width, height);
}
Framebuffer::Framebuffer(uint width, uint height, bool alpha)
: width(width), height(height)
{
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
format = alpha ? GL_RGBA : GL_RGB;
// Setup color attachment (texture)
texture = create_texture(width, height, format);
// Setup depth attachment
glGenRenderbuffers(1, &depth);
@ -64,11 +69,11 @@ void Framebuffer::resize(uint width, uint height) {
glBindRenderbuffer(GL_RENDERBUFFER, depth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
texture->bind();
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, nullptr);
texture->unbind();
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
texture = create_texture(width, height, format);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
Texture* Framebuffer::getTexture() const {

View File

@ -40,3 +40,11 @@ void PostProcessing::render(const GfxContext& context, Shader* screenShader) {
fbo->getTexture()->bind();
quadMesh->draw();
}
std::unique_ptr<ImageData> PostProcessing::toImage() {
return fbo->getTexture()->readData();
}
Framebuffer* PostProcessing::getFramebuffer() const {
return fbo.get();
}

View File

@ -32,6 +32,11 @@ public:
/// @param screenShader shader used for fullscreen quad
/// @throws std::runtime_error if use(...) wasn't called before
void render(const GfxContext& context, Shader* screenShader);
/// @brief Make an image from the last rendered frame
std::unique_ptr<ImageData> toImage();
Framebuffer* getFramebuffer() const;
};
#endif // GRAPHICS_CORE_POST_PROCESSING_H_

View File

@ -49,12 +49,12 @@ void Texture::reload(ubyte* data){
glBindTexture(GL_TEXTURE_2D, 0);
}
ImageData* Texture::readData() {
std::unique_ptr<ImageData> Texture::readData() {
std::unique_ptr<ubyte[]> data (new ubyte[width * height * 4]);
glBindTexture(GL_TEXTURE_2D, id);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data.get());
glBindTexture(GL_TEXTURE_2D, 0);
return new ImageData(ImageFormat::rgba8888, width, height, data.release());
return std::make_unique<ImageData>(ImageFormat::rgba8888, width, height, data.release());
}
void Texture::setNearestFilter() {

View File

@ -2,6 +2,7 @@
#define GRAPHICS_CORE_TEXTURE_H_
#include <string>
#include <memory>
#include "../../typedefs.h"
#include "ImageData.h"
@ -23,7 +24,7 @@ public:
void setNearestFilter();
virtual ImageData* readData();
virtual std::unique_ptr<ImageData> readData();
virtual uint getWidth() const;
virtual uint getHeight() const;

View File

@ -17,7 +17,7 @@
#include "../core/Texture.h"
#include "../core/Viewport.h"
ImageData* BlocksPreview::draw(
std::unique_ptr<ImageData> BlocksPreview::draw(
const ContentGfxCache* cache,
Shader* shader,
Framebuffer* fbo,

View File

@ -17,7 +17,7 @@ class Shader;
class ContentGfxCache;
class BlocksPreview {
static ImageData* draw(
static std::unique_ptr<ImageData> draw(
const ContentGfxCache* cache,
Shader* shader,
Framebuffer* framebuffer,

View File

@ -42,7 +42,6 @@ WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* pl
level(frontend->getLevel()),
player(player)
{
postProcessing = std::make_unique<PostProcessing>();
frustumCulling = std::make_unique<Frustum>();
lineBatch = std::make_unique<LineBatch>();
renderer = std::make_unique<ChunksRenderer>(
@ -148,6 +147,7 @@ void WorldRenderer::renderLevel(
Assets* assets = engine->getAssets();
Atlas* atlas = assets->getAtlas("blocks");
Shader* shader = assets->getShader("main");
auto indices = level->content->getIndices();
float fogFactor = 15.0f / ((float)settings.chunks.loadDistance.get()-2);
@ -265,7 +265,15 @@ void WorldRenderer::renderDebugLines(
lineBatch->render();
}
void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible){
void WorldRenderer::draw(
const GfxContext& pctx,
Camera* camera,
bool hudVisible,
PostProcessing* postProcessing
){
const Viewport& vp = pctx.getViewport();
camera->aspect = vp.getWidth() / static_cast<float>(vp.getHeight());
EngineSettings& settings = engine->getSettings();
skybox->refresh(pctx, level->getWorld()->daytime, 1.0f+fog*2.0f, 4);

View File

@ -31,7 +31,6 @@ class WorldRenderer {
Engine* engine;
Level* level;
Player* player;
std::unique_ptr<PostProcessing> postProcessing;
std::unique_ptr<Frustum> frustumCulling;
std::unique_ptr<LineBatch> lineBatch;
std::unique_ptr<ChunksRenderer> renderer;
@ -40,16 +39,6 @@ class WorldRenderer {
bool drawChunk(size_t index, Camera* camera, Shader* shader, bool culling);
void drawChunks(Chunks* chunks, Camera* camera, Shader* shader);
/// @brief Render level without diegetic interface
/// @param context graphics context
/// @param camera active camera
/// @param settings engine settings
void renderLevel(
const GfxContext& context,
Camera* camera,
const EngineSettings& settings
);
/// @brief Render block selection lines
/// @param camera active camera
/// @param linesShader shader used
@ -70,9 +59,24 @@ public:
WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player);
~WorldRenderer();
void draw(const GfxContext& context, Camera* camera, bool hudVisible);
void draw(
const GfxContext& context,
Camera* camera,
bool hudVisible,
PostProcessing* postProcessing
);
void drawBorders(int sx, int sy, int sz, int ex, int ey, int ez);
/// @brief Render level without diegetic interface
/// @param context graphics context
/// @param camera active camera
/// @param settings engine settings
void renderLevel(
const GfxContext& context,
Camera* camera,
const EngineSettings& settings
);
static float fog;
};

View File

@ -19,18 +19,6 @@ namespace scripting {
extern lua::LuaState* state;
}
static int l_get_worlds_list(lua_State* L) {
auto paths = scripting::engine->getPaths();
auto worlds = paths->scanForWorlds();
lua_createtable(L, worlds.size(), 0);
for (size_t i = 0; i < worlds.size(); i++) {
lua_pushstring(L, worlds[i].filename().u8string().c_str());
lua_rawseti(L, -2, i + 1);
}
return 1;
}
static int l_new_world(lua_State* L) {
auto name = lua_tostring(L, 1);
auto seed = lua_tostring(L, 2);
@ -183,7 +171,6 @@ static int l_get_generators(lua_State* L) {
}
const luaL_Reg corelib [] = {
{"get_worlds_list", lua_wrap_errors<l_get_worlds_list>},
{"new_world", lua_wrap_errors<l_new_world>},
{"open_world", lua_wrap_errors<l_open_world>},
{"close_world", lua_wrap_errors<l_close_world>},

View File

@ -75,12 +75,15 @@ static int l_pack_get_info(lua_State* L, const ContentPack& pack, const Content*
lua_pushstring(L, pack.version.c_str());
lua_setfield(L, -2, "version");
// hmm
// FIXME: hmm
auto assets = scripting::engine->getAssets();
std::string icon = pack.id+".icon";
if (assets->getTexture(icon) == nullptr) {
auto iconfile = pack.folder/fs::path("icon.png");
if (fs::is_regular_file(iconfile)) {
if (!fs::exists(iconfile)) {
iconfile = pack.folder/fs::path("preview.png");
}
if (fs::exists(iconfile)) {
auto image = imageio::read(iconfile.string());
assets->store(Texture::from(image.get()), icon);
} else {

View File

@ -1,6 +1,10 @@
#include "lua_commons.h"
#include "api_lua.h"
#include "../scripting.h"
#include "../../../assets/Assets.h"
#include "../../../coders/imageio.h"
#include "../../../graphics/core/Texture.h"
#include "../../../files/engine_paths.h"
#include "../../../world/Level.h"
#include "../../../world/World.h"
#include "../../../engine.h"
@ -10,6 +14,39 @@
namespace fs = std::filesystem;
static int l_world_get_list(lua_State* L) {
auto paths = scripting::engine->getPaths();
auto worlds = paths->scanForWorlds();
lua_createtable(L, worlds.size(), 0);
for (size_t i = 0; i < worlds.size(); i++) {
lua_createtable(L, 0, 1);
auto name = worlds[i].filename().u8string();
lua_pushstring(L, name.c_str());
lua_setfield(L, -2, "name");
// FIXME: hmm
auto assets = scripting::engine->getAssets();
std::string icon = "world:"+name+".icon";
if (assets->getTexture(icon) == nullptr) {
auto iconfile = worlds[i]/fs::path("preview.png");
if (fs::is_regular_file(iconfile)) {
auto image = imageio::read(iconfile.string());
assets->store(Texture::from(image.get()), icon);
} else {
icon = "gui/no_world_icon";
}
}
lua_pushstring(L, icon.c_str());
lua_setfield(L, -2, "icon");
lua_rawseti(L, -2, i + 1);
}
return 1;
}
static int l_world_get_total_time(lua_State* L) {
lua_pushnumber(L, scripting::level->getWorld()->totalTime);
return 1;
@ -39,6 +76,7 @@ static int l_world_exists(lua_State* L) {
}
const luaL_Reg worldlib [] = {
{"get_list", lua_wrap_errors<l_world_get_list>},
{"get_total_time", lua_wrap_errors<l_world_get_total_time>},
{"get_day_time", lua_wrap_errors<l_world_get_day_time>},
{"set_day_time", lua_wrap_errors<l_world_set_day_time>},

View File

@ -77,6 +77,7 @@ struct DebugSettings {
struct UiSettings {
std::string language = "auto";
IntegerSetting worldPreviewSize {64, 1, 512};
};
struct EngineSettings {