fullscreen setting checkbox + smth

This commit is contained in:
MihailRis 2024-04-26 00:34:15 +03:00
parent 8be0d157c2
commit 4861fedf1e
17 changed files with 97 additions and 76 deletions

View File

@ -36,6 +36,7 @@ function on_open()
create_setting("graphics.fog-curve", "Fog Curve", 0.1)
create_setting("graphics.gamma", "Gamma", 0.05)
create_setting("camera.fov", "FOV", 1, "°")
create_checkbox("display.fullscreen", "Fullscreen")
create_checkbox("display.vsync", "V-Sync")
create_checkbox("graphics.backlight", "Backlight")
create_checkbox("camera.shaking", "Camera Shaking")

View File

@ -42,6 +42,7 @@ settings.Load Speed=Скорость Загрузки
settings.Fog Curve=Кривая Тумана
settings.Backlight=Подсветка
settings.Gamma=Гамма
settings.Fullscreen=Полноэкранный
settings.V-Sync=Вертикальная Синхронизация
settings.Camera Shaking=Тряска Камеры
settings.Master Volume=Общая Громкость

View File

@ -206,7 +206,14 @@ public:
observers.notify(value);
}
observer_handler observe(consumer<bool> callback) {
void toggle() {
set(!get());
}
observer_handler observe(consumer<bool> callback, bool callOnStart=false) {
if (callOnStart) {
callback(value);
}
return observers.observe(callback);
}

View File

@ -60,7 +60,7 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
: settings(settings), settingsHandler(settings), paths(paths)
{
controller = std::make_unique<EngineController>(this);
if (Window::initialize(settings.display)){
if (Window::initialize(&this->settings.display)){
throw initialize_error("could not initialize window");
}
audio::initialize(settings.audio.enabled);
@ -102,7 +102,7 @@ void Engine::updateHotkeys() {
saveScreenshot();
}
if (Events::jpressed(keycode::F11)) {
Window::toggleFullscreen();
settings.display.fullscreen.toggle();
}
}

View File

@ -1,13 +1,14 @@
#include "settings_io.h"
#include <memory>
#include <iostream>
#include "../window/Events.h"
#include "../window/input.h"
#include "../coders/toml.h"
#include "../coders/json.h"
#include "../debug/Logger.hpp"
#include <memory>
static debug::Logger logger("settings_io");
SettingsHandler::SettingsHandler(EngineSettings& settings) {
// public settings
@ -18,9 +19,11 @@ SettingsHandler::SettingsHandler(EngineSettings& settings) {
map.emplace("audio.volume-music", &settings.audio.volumeMusic);
map.emplace("display.vsync", &settings.display.vsync);
map.emplace("display.fullscreen", &settings.display.fullscreen);
map.emplace("camera.sensitivity", &settings.camera.sensitivity);
map.emplace("camera.fov", &settings.camera.fov);
map.emplace("camera.fov-effects", &settings.camera.fovEffects);
map.emplace("camera.shaking", &settings.camera.shaking);
map.emplace("chunks.load-distance", &settings.chunks.loadDistance);
@ -111,7 +114,7 @@ toml::Wrapper* create_wrapper(EngineSettings& settings) {
audio.add("volume-music", &*settings.audio.volumeMusic);
toml::Section& display = wrapper->add("display");
display.add("fullscreen", &settings.display.fullscreen);
display.add("fullscreen", &*settings.display.fullscreen);
display.add("width", &settings.display.width);
display.add("height", &settings.display.height);
display.add("samples", &settings.display.samples);
@ -123,7 +126,7 @@ toml::Wrapper* create_wrapper(EngineSettings& settings) {
chunks.add("padding", &*settings.chunks.padding);
toml::Section& camera = wrapper->add("camera");
camera.add("fov-effects", &settings.camera.fovEvents);
camera.add("fov-effects", &*settings.camera.fovEffects);
camera.add("fov", &*settings.camera.fov);
camera.add("shaking", &*settings.camera.shaking);
camera.add("sensitivity", &*settings.camera.sensitivity);
@ -132,8 +135,8 @@ toml::Wrapper* create_wrapper(EngineSettings& settings) {
graphics.add("gamma", &*settings.graphics.gamma);
graphics.add("fog-curve", &*settings.graphics.fogCurve);
graphics.add("backlight", &*settings.graphics.backlight);
graphics.add("frustum-culling", &settings.graphics.frustumCulling);
graphics.add("skybox-resolution", &settings.graphics.skyboxResolution);
graphics.add("frustum-culling", &*settings.graphics.frustumCulling);
graphics.add("skybox-resolution", &*settings.graphics.skyboxResolution);
toml::Section& debug = wrapper->add("debug");
debug.add("generator-test-mode", &settings.debug.generatorTestMode);
@ -179,7 +182,7 @@ void load_controls(std::string filename, std::string source) {
} else if (typestr == "mouse") {
type = inputtype::mouse;
} else {
std::cerr << "unknown input type '" << typestr << "'" << std::endl;
logger.error() << "unknown input type '" << typestr << "'";
continue;
}
binding.type = type;

View File

@ -63,7 +63,7 @@ std::shared_ptr<UINode> create_debug_panel(
}));
panel->add(create_label([=](){
auto& settings = engine->getSettings();
bool culling = settings.graphics.frustumCulling;
bool culling = settings.graphics.frustumCulling.get();
return L"frustum-culling: "+std::wstring(culling ? L"on" : L"off");
}));
panel->add(create_label([=]() {

View File

@ -450,7 +450,7 @@ void Hud::draw(const GfxContext& ctx){
// Crosshair
if (!pause && !inventoryOpen && !player->debug) {
GfxContext chctx = ctx.sub();
chctx.setBlendMode(blendmode::inversion);
chctx.setBlendMode(BlendMode::inversion);
auto texture = assets->getTexture("gui/crosshair");
batch->texture(texture);
int chsizex = texture != nullptr ? texture->getWidth() : 16;

View File

@ -93,7 +93,7 @@ void LevelScreen::saveWorldPreview() {
void LevelScreen::updateHotkeys() {
auto& settings = engine->getSettings();
if (Events::jpressed(keycode::O)) {
settings.graphics.frustumCulling = !settings.graphics.frustumCulling;
settings.graphics.frustumCulling.toggle();
}
if (Events::jpressed(keycode::F1)) {
hudVisible = !hudVisible;

View File

@ -5,6 +5,21 @@
#include "Batch2D.hpp"
#include "Framebuffer.hpp"
static void set_blend_mode(BlendMode mode) {
switch (mode) {
case BlendMode::normal:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
break;
case BlendMode::addition:
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
break;
case BlendMode::inversion:
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);
break;
}
}
GfxContext::GfxContext(
const GfxContext* parent,
const Viewport& viewport,
@ -53,7 +68,7 @@ GfxContext::~GfxContext() {
else glEnable(GL_CULL_FACE);
}
if (blendMode != parent->blendMode) {
Window::setBlendMode(parent->blendMode);
set_blend_mode(parent->blendMode);
}
}
@ -119,11 +134,11 @@ void GfxContext::setCullFace(bool flag) {
}
}
void GfxContext::setBlendMode(blendmode mode) {
void GfxContext::setBlendMode(BlendMode mode) {
if (blendMode == mode)
return;
blendMode = mode;
Window::setBlendMode(mode);
set_blend_mode(mode);
}
void GfxContext::setScissors(glm::vec4 area) {

View File

@ -1,6 +1,7 @@
#ifndef GRAPHICS_CORE_GFX_CONTEXT_HPP_
#define GRAPHICS_CORE_GFX_CONTEXT_HPP_
#include "commons.hpp"
#include "Viewport.hpp"
#include "../../window/Window.h"
#include "../../typedefs.h"
@ -16,7 +17,7 @@ class GfxContext {
bool depthMask = true;
bool depthTest = false;
bool cullFace = false;
blendmode blendMode = blendmode::normal;
BlendMode blendMode = BlendMode::normal;
int scissorsCount = 0;
public:
GfxContext(const GfxContext* parent, const Viewport& viewport, Batch2D* g2d);
@ -31,7 +32,7 @@ public:
void setDepthMask(bool flag);
void setDepthTest(bool flag);
void setCullFace(bool flag);
void setBlendMode(blendmode mode);
void setBlendMode(BlendMode mode);
void setScissors(glm::vec4 area);
};

View File

@ -7,4 +7,8 @@ enum class DrawPrimitive {
triangle,
};
enum class BlendMode {
normal, addition, inversion
};
#endif // GRAPHICS_CORE_COMMONS_HPP_

View File

@ -104,7 +104,7 @@ void Skybox::draw(
drawBackground(camera, assets, width, height);
GfxContext ctx = pctx.sub();
ctx.setBlendMode(blendmode::addition);
ctx.setBlendMode(BlendMode::addition);
Shader* shader = assets->getShader("ui3d");
shader->use();

View File

@ -57,7 +57,7 @@ WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* pl
);
auto assets = engine->getAssets();
skybox = std::make_unique<Skybox>(
settings.graphics.skyboxResolution,
settings.graphics.skyboxResolution.get(),
assets->getShader("skybox_gen")
);
}
@ -127,7 +127,7 @@ void WorldRenderer::drawChunks(Chunks* chunks, Camera* camera, Shader* shader) {
(b->z + 0.5f - pz)*(b->z + 0.5f - pz));
});
bool culling = engine->getSettings().graphics.frustumCulling;
bool culling = engine->getSettings().graphics.frustumCulling.get();
if (culling) {
frustumCulling->update(camera->getProjView());
}

View File

@ -142,7 +142,7 @@ void CameraControl::update(PlayerInput& input, float delta, Chunks* chunks) {
if (settings.shaking.get() && !input.cheat) {
offset += updateCameraShaking(delta);
}
if (settings.fovEvents){
if (settings.fovEffects.get()){
updateFovEffects(input, delta);
}
if (input.cameraMode) {

View File

@ -1,5 +1,5 @@
#ifndef SRC_SETTINGS_H_
#define SRC_SETTINGS_H_
#ifndef SETTINGS_H_
#define SETTINGS_H_
#include <string>
#include <unordered_map>
@ -21,7 +21,7 @@ struct AudioSettings {
struct DisplaySettings {
/// @brief Is window in full screen mode
bool fullscreen = false;
FlagSetting fullscreen {false};
/// @brief Window width (pixels)
int width = 1280;
/// @brief Window height (pixels)
@ -29,8 +29,8 @@ struct DisplaySettings {
/// @brief Anti-aliasing samples
int samples = 0;
/// @brief VSync on
FlagSetting vsync = {true};
/// @brief Window title */
FlagSetting vsync {true};
/// @brief Window title
std::string title = "VoxelEngine-Cpp v" +
std::to_string(ENGINE_VERSION_MAJOR) + "." +
std::to_string(ENGINE_VERSION_MINOR);
@ -38,18 +38,18 @@ struct DisplaySettings {
struct ChunksSettings {
/// @brief Max milliseconds that engine uses for chunks loading only
IntegerSetting loadSpeed = {4, 1, 32};
IntegerSetting loadSpeed {4, 1, 32};
/// @brief Radius of chunks loading zone (chunk is unit)
IntegerSetting loadDistance = {22, 3, 66};
IntegerSetting loadDistance {22, 3, 66};
/// @brief Buffer zone where chunks are not unloading (chunk is unit)
IntegerSetting padding = {2, 1, 8};
IntegerSetting padding {2, 1, 8};
};
struct CameraSettings {
/// @brief Camera dynamic field of view effects
bool fovEvents = true;
FlagSetting fovEffects {true};
/// @brief Camera movement shake
FlagSetting shaking = {true};
FlagSetting shaking {true};
/// @brief Camera field of view
NumberSetting fov {90.0f, 10, 120};
/// @brief Camera sensitivity
@ -60,12 +60,12 @@ struct GraphicsSettings {
/// @brief Fog opacity is calculated as `pow(depth*k, fogCurve)` where k depends on chunksLoadDistance.
/// 1.0 is linear, 2.0 is quadratic
NumberSetting fogCurve {1.6f, 1.0f, 6.0f};
NumberSetting gamma = {1.0f, 0.5f, 2.0f};
NumberSetting gamma {1.0f, 0.5f, 2.0f};
/// @brief Enable blocks backlight to prevent complete darkness
FlagSetting backlight = {true};
FlagSetting backlight {true};
/// @brief Enable chunks frustum culling
bool frustumCulling = true;
int skyboxResolution = 64 + 32;
FlagSetting frustumCulling {true}; // redutant?
IntegerSetting skyboxResolution {64 + 32, 64, 128};
};
struct DebugSettings {
@ -90,4 +90,4 @@ struct EngineSettings {
UiSettings ui;
};
#endif // SRC_SETTINGS_H_
#endif // SETTINGS_H_

View File

@ -4,6 +4,8 @@
#include "../debug/Logger.hpp"
#include "../graphics/core/ImageData.hpp"
#include "../graphics/core/Texture.hpp"
#include "../settings.h"
#include "../util/ObjectsKeeper.hpp"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
@ -18,6 +20,9 @@ uint Window::width = 0;
uint Window::height = 0;
int Window::posX = 0;
int Window::posY = 0;
bool Window::fullscreen = false;
static util::ObjectsKeeper observers_keeper;
void cursor_position_callback(GLFWwindow*, double xpos, double ypos) {
Events::setPosition(xpos, ypos);
@ -103,10 +108,10 @@ void error_callback(int error, const char* description) {
}
}
int Window::initialize(DisplaySettings& settings){
Window::settings = &settings;
Window::width = settings.width;
Window::height = settings.height;
int Window::initialize(DisplaySettings* settings){
Window::settings = settings;
Window::width = settings->width;
Window::height = settings->height;
glfwSetErrorCallback(error_callback);
if (glfwInit() == GLFW_FALSE) {
@ -124,9 +129,9 @@ int Window::initialize(DisplaySettings& settings){
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
#endif
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
glfwWindowHint(GLFW_SAMPLES, settings.samples);
glfwWindowHint(GLFW_SAMPLES, settings->samples);
window = glfwCreateWindow(width, height, settings.title.c_str(), nullptr, nullptr);
window = glfwCreateWindow(width, height, settings->title.c_str(), nullptr, nullptr);
if (window == nullptr){
logger.error() << "failed to create GLFW window";
glfwTerminate();
@ -159,12 +164,15 @@ int Window::initialize(DisplaySettings& settings){
glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetCharCallback(window, character_callback);
glfwSetScrollCallback(window, scroll_callback);
if (settings.fullscreen) {
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, GLFW_DONT_CARE);
}
glfwSwapInterval(settings.vsync.get());
observers_keeper = util::ObjectsKeeper();
observers_keeper.keepAlive(settings->fullscreen.observe([=](bool value) {
if (value != isFullscreen()) {
toggleFullscreen();
}
}, true));
glfwSwapInterval(settings->vsync.get());
const GLubyte* vendor = glGetString(GL_VENDOR);
const GLubyte* renderer = glGetString(GL_RENDERER);
logger.info() << "GL Vendor: " << (char*)vendor;
@ -173,20 +181,6 @@ int Window::initialize(DisplaySettings& settings){
return 0;
}
void Window::setBlendMode(blendmode mode) {
switch (mode) {
case blendmode::normal:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
break;
case blendmode::addition:
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
break;
case blendmode::inversion:
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);
break;
}
}
void Window::clear() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
@ -263,6 +257,7 @@ void Window::popScissor() {
}
void Window::terminate(){
observers_keeper = util::ObjectsKeeper();
glfwTerminate();
}
@ -279,14 +274,14 @@ void Window::swapInterval(int interval){
}
void Window::toggleFullscreen(){
settings->fullscreen = !settings->fullscreen;
fullscreen = !fullscreen;
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
if (Events::_cursor_locked) Events::toggleCursor();
if (settings->fullscreen) {
if (fullscreen) {
glfwGetWindowPos(window, &posX, &posY);
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, GLFW_DONT_CARE);
}
@ -301,7 +296,7 @@ void Window::toggleFullscreen(){
}
bool Window::isFullscreen() {
return settings->fullscreen;
return fullscreen;
}
void Window::swapBuffers(){

View File

@ -2,7 +2,6 @@
#define WINDOW_WINDOW_H_
#include "../typedefs.h"
#include "../settings.h"
#include <stack>
#include <vector>
@ -14,15 +13,12 @@ struct DisplaySettings;
struct GLFWwindow;
struct GLFWmonitor;
enum class blendmode {
normal, addition, inversion
};
class Window {
static GLFWwindow* window;
static DisplaySettings* settings;
static std::stack<glm::vec4> scissorStack;
static glm::vec4 scissorArea;
static bool fullscreen;
static bool tryToMaximize(GLFWwindow* window, GLFWmonitor* monitor);
public:
@ -30,7 +26,7 @@ public:
static int posY;
static uint width;
static uint height;
static int initialize(DisplaySettings& settings);
static int initialize(DisplaySettings* settings);
static void terminate();
static void viewport(int x, int y, int width, int height);
@ -58,8 +54,6 @@ public:
static void setClipboardText(const char* text);
static DisplaySettings* getSettings();
static void setBlendMode(blendmode mode);
static glm::vec2 size() {
return glm::vec2(width, height);
}