]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Add configuration files for client and server
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 22 Jan 2022 13:11:34 +0000 (14:11 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 22 Jan 2022 13:11:34 +0000 (14:11 +0100)
20 files changed:
.gitignore
shaders/3d/fragment.glsl
src/CMakeLists.txt
src/client/client_config.c [new file with mode: 0644]
src/client/client_config.h [new file with mode: 0644]
src/client/client_map.c
src/client/debug_menu.c
src/client/debug_menu.h
src/client/game.c
src/client/scene.c
src/client/texture.c
src/client/window.c
src/config.c [new file with mode: 0644]
src/config.h [new file with mode: 0644]
src/server/server.c
src/server/server.h
src/server/server_commands.c
src/server/server_config.c [new file with mode: 0644]
src/server/server_config.h [new file with mode: 0644]
src/server/server_map.c

index 1053609b7c7e2869a441a27759566a8456fe9cc7..2316d5cd4fa3b12033d2f344fa9aac83079cff45 100644 (file)
@@ -16,3 +16,4 @@ world.sqlite-journal
 DragonblocksAlpha-*.zip
 src/version.h
 screenshot-*.png
+*.conf
index 653d579d3c372aa9ac3b322d2d6d2ce38fad5ae8..58b06bd0986fb35a79e1d378609735c04217ef38 100755 (executable)
@@ -13,7 +13,7 @@ uniform sampler2D textures[MAX_TEXTURE_UNITS];
 void main()
 {
        outColor = texture(textures[int(fragmentTextureIndex + 0.5)], fragmentTextureCoords) * vec4(fragmentColor, 1.0);
-       outColor.rgb = mix(outColor.rgb, fogColor, clamp(length(fragmentPosition - cameraPos) / 255.0, 0.0, 1.0));
+       outColor.rgb = mix(outColor.rgb, fogColor, clamp(length(fragmentPosition - cameraPos) / RENDER_DISTANCE, 0.0, 1.0));
 
        if (outColor.a == 0.0)
                discard;
index 4720f08d0621a6781d1499e93027c0e12f7fd8c2..3fa7299a7b1aab8645a55f59edd1d54973154d5c 100644 (file)
@@ -51,6 +51,7 @@ set(DEPS_SOURCES
 
 set(COMMON_SOURCES
        ${DEPS_SOURCES}
+       config.c
        day.c
        environment.c
        map.c
@@ -66,6 +67,7 @@ add_executable(Dragonblocks
        client/camera.c
        client/client.c
        client/client_commands.c
+       client/client_config.c
        client/client_map.c
        client/client_node.c
        client/client_player.c
@@ -105,6 +107,7 @@ add_executable(DragonblocksServer
        server/mapgen.c
        server/server.c
        server/server_commands.c
+       server/server_config.c
        server/server_map.c
        server/trees.c
        server/voxelctx.c
diff --git a/src/client/client_config.c b/src/client/client_config.c
new file mode 100644 (file)
index 0000000..43743c1
--- /dev/null
@@ -0,0 +1,30 @@
+#include "config.h"
+#include "client/client_config.h"
+
+struct ClientConfig client_config = {
+       .antialiasing = 4,
+       .mipmap = true,
+       .render_distance = 255.0,
+};
+
+__attribute__((constructor)) static void client_config_init()
+{
+       config_read("client.conf", (ConfigEntry[]) {
+               {
+                       .type = CT_UINT,
+                       .key = "antialiasing",
+                       .value = &client_config.antialiasing,
+               },
+               {
+                       .type = CT_BOOL,
+                       .key = "mipmap",
+                       .value = &client_config.mipmap,
+               },
+               {
+                       .type = CT_FLOAT,
+                       .key = "render_distance",
+                       .value = &client_config.render_distance,
+               }
+       }, 3);
+}
+
diff --git a/src/client/client_config.h b/src/client/client_config.h
new file mode 100644 (file)
index 0000000..984d5e8
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef _CLIENT_CONFIG_H_
+#define _CLIENT_CONFIG_H_
+
+#include <stdbool.h>
+
+extern struct ClientConfig {
+       unsigned int antialiasing;
+       bool mipmap;
+       double render_distance;
+} client_config;
+
+#endif
index bb513371a46503b462cca66ae4abd81cd8130c04..43077746acea292e2bbf7901792632efa415e5f2 100644 (file)
@@ -4,6 +4,7 @@
 #include "client/facecache.h"
 #include "client/client_map.h"
 #include "client/client_player.h"
+#include "client/debug_menu.h"
 #include "util.h"
 #define MAX_BLOCK_REQUESTS 4
 
index 352222fe2bc733ec93be7e17ac5f622d79e92cc2..04b9256ec62f9669e47fffa61cf36d86b16eaf74 100644 (file)
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <GL/glew.h>
 #include <GL/gl.h>
+#include "client/client_config.h"
+#include "client/client_map.h"
 #include "client/client_player.h"
 #include "client/debug_menu.h"
 #include "client/gui.h"
@@ -30,6 +32,10 @@ typedef enum
        DME_FULLSCREEN,
        DME_OPENGL,
        DME_GPU,
+       DME_ANTIALIASING,
+       DME_MIPMAP,
+       DME_RENDER_DISTANCE,
+       DME_SIMULATION_DISTANCE,
        DME_COUNT,
 } DebugMenuEntry;
 
@@ -156,3 +162,26 @@ void debug_menu_update_gpu()
 {
        gui_set_text(gui_elements[DME_GPU], format_string("%s", glGetString(GL_RENDERER)));
 }
+
+void debug_menu_update_antialiasing()
+{
+       gui_set_text(gui_elements[DME_ANTIALIASING], client_config.antialiasing > 1
+               ? format_string("antialiasing: %u samples", client_config.antialiasing)
+               : format_string("antialiasing: disabled")
+       );
+}
+
+void debug_menu_update_mipmap()
+{
+       gui_set_text(gui_elements[DME_MIPMAP], format_string("mipmap: %s", client_config.mipmap ? "enabled" : "disabled"));
+}
+
+void debug_menu_update_render_distance()
+{
+       gui_set_text(gui_elements[DME_RENDER_DISTANCE], format_string("render distance: %.1lf", client_config.render_distance));
+}
+
+void debug_menu_update_simulation_distance()
+{
+       gui_set_text(gui_elements[DME_SIMULATION_DISTANCE], format_string("simulation distance: %u", client_map.simulation_distance));
+}
index 96700fa53570062007560058d32961e70ba7b358..fdba1d1a3c33c0206d7fcc28fd3705f0ed6992c1 100644 (file)
@@ -23,5 +23,9 @@ void debug_menu_update_timelapse();
 void debug_menu_update_fullscreen();
 void debug_menu_update_opengl();
 void debug_menu_update_gpu();
+void debug_menu_update_antialiasing();
+void debug_menu_update_mipmap();
+void debug_menu_update_render_distance();
+void debug_menu_update_simulation_distance();
 
 #endif
index 182bae751810f2cf834f8e73f573c803c7ed9229..822fe9563992495cd23ebbc91ab74e8d77924e4c 100644 (file)
@@ -134,6 +134,10 @@ bool game(Client *client)
        debug_menu_update_fullscreen();
        debug_menu_update_opengl();
        debug_menu_update_gpu();
+       debug_menu_update_antialiasing();
+       debug_menu_update_mipmap();
+       debug_menu_update_render_distance();
+       debug_menu_update_simulation_distance();
 
        crosshair_init();
 
index 93a569914489e0e02b7cd2066832d61defd985af..a318b28e436ec402f3aedf608563295be168b857 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include "client/camera.h"
 #include "client/client.h"
+#include "client/client_config.h"
 #include "client/frustum.h"
 #include "client/scene.h"
 #include "client/shader.h"
@@ -24,14 +25,20 @@ bool scene_init()
 
        glGetIntegerv(GL_MAX_TEXTURE_UNITS, &scene.max_texture_units);
 
-       char max_texture_units_def[BUFSIZ];
-       sprintf(max_texture_units_def, "#define MAX_TEXTURE_UNITS %d\n", scene.max_texture_units);
+       char *shader_defs = format_string(
+               "#define MAX_TEXTURE_UNITS %d\n"
+               "#define RENDER_DISTANCE %lf\n",
+               scene.max_texture_units,
+               client_config.render_distance
+       );
 
-       if (! shader_program_create(RESSOURCEPATH "shaders/3d", &scene.prog, max_texture_units_def)) {
+       if (! shader_program_create(RESSOURCEPATH "shaders/3d", &scene.prog, shader_defs)) {
                fprintf(stderr, "Failed to create 3D shader program\n");
                return false;
        }
 
+       free(shader_defs);
+
        scene.loc_model = glGetUniformLocation(scene.prog, "model");
        scene.loc_VP = glGetUniformLocation(scene.prog, "VP");
        scene.loc_daylight = glGetUniformLocation(scene.prog, "daylight");
@@ -47,7 +54,6 @@ bool scene_init()
        glProgramUniform1iv(scene.prog, glGetUniformLocation(scene.prog, "textures"), scene.max_texture_units, texture_indices);
 
        scene.fov = 86.1f;
-       scene.render_distance = 255.0f;
 
        return true;
 }
@@ -113,7 +119,7 @@ void scene_render(f64 dtime)
                        object_delete(obj);
                } else {
                        f32 distance = sqrt(pow(obj->pos.x - camera.eye[0], 2) + pow(obj->pos.y - camera.eye[1], 2) + pow(obj->pos.z - camera.eye[2], 2));
-                       if (distance < scene.render_distance && object_before_render(obj, dtime)) {
+                       if (distance < client_config.render_distance && object_before_render(obj, dtime)) {
                                if (obj->transparent)
                                        bintree_insert(&scene.transparent_objects, &distance, obj);
                                else
@@ -129,7 +135,7 @@ void scene_render(f64 dtime)
 
 void scene_on_resize(int width, int height)
 {
-       mat4x4_perspective(scene.projection, scene.fov / 180.0f * M_PI, (float) width / (float) height, 0.01f, scene.render_distance + 28.0f);
+       mat4x4_perspective(scene.projection, scene.fov / 180.0f * M_PI, (float) width / (float) height, 0.01f, client_config.render_distance + 28.0f);
 }
 
 GLuint scene_get_max_texture_units()
index 2e3e461ddbe6e35128160d9b212c4daf14cc1288..b2fbd4c9c31b63e96d185a9dcfaf386fb5cf2d30 100644 (file)
@@ -2,6 +2,7 @@
 #include <stb/stb_image.h>
 #include <stdbool.h>
 #include <dragontype/list.h>
+#include "client/client_config.h"
 #include "client/texture.h"
 #include "util.h"
 
@@ -32,7 +33,7 @@ Texture *texture_create(unsigned char *data, int width, int height, GLenum forma
 
        glBindTexture(GL_TEXTURE_2D, texture->id);
 
-       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipmap ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (mipmap && client_config.mipmap) ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST);
        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);
index c20a0a6c18f3b94dd3c81f6adc0d09181a6b20fb..808c36f304a2249d8ae996d89ac4ceb11ac9d898 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <GL/glew.h>
 #include <GL/gl.h>
+#include "client/client_config.h"
 #include "client/debug_menu.h"
 #include "client/game.h"
 #include "client/gui.h"
@@ -63,7 +64,7 @@ bool window_init(int width, int height)
                return false;
        }
 
-       glfwWindowHint(GLFW_SAMPLES, 8);
+       glfwWindowHint(GLFW_SAMPLES, client_config.antialiasing);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
diff --git a/src/config.c b/src/config.c
new file mode 100644 (file)
index 0000000..0af1827
--- /dev/null
@@ -0,0 +1,84 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+#include "config.h"
+
+void config_read(char *path, ConfigEntry *entries, size_t num_entries)
+{
+       FILE *f = fopen(path, "r");
+
+       if (! f)
+               return;
+
+       printf("Reading config from %s\n", path);
+
+       while (! feof(f)) {
+               char key[BUFSIZ];
+               fscanf(f, "%s", key);
+
+               bool found = false;
+               for (size_t i = 0; i < num_entries; i++) {
+                       ConfigEntry *entry = &entries[i];
+                       if (strcmp(key, entry->key) == 0) {
+                               bool valid = false;
+
+                               switch (entry->type) {
+                                       case CT_STRING: {
+                                               char value[BUFSIZ];
+
+                                               if (! fscanf(f, "%s", value))
+                                                       break;
+
+                                               valid = true;
+                                               *(char **) entry->value = strdup(value);
+                                       } break;
+
+                                       case CT_INT:
+                                               if (fscanf(f, "%d", (int *) entry->value))
+                                                       valid = true;
+
+                                               break;
+
+                                       case CT_UINT:
+                                               if (fscanf(f, "%u", (unsigned int *) entry->value))
+                                                       valid = true;
+
+                                               break;
+
+                                       case CT_FLOAT:
+                                               if (fscanf(f, "%lf", (double *) entry->value))
+                                                       valid = true;
+
+                                               break;
+
+                                       case CT_BOOL: {
+                                               char value[BUFSIZ];
+
+                                               if (! fscanf(f, "%s", value))
+                                                       break;
+
+                                               valid = true;
+
+                                               if (strcmp(value, "on") == 0 || strcmp(value, "yes") == 0 || strcmp(value, "true") == 0)
+                                                       *(bool *) entry->value = true;
+                                               else if (strcmp(value, "off") == 0 || strcmp(value, "no") == 0 || strcmp(value, "false") == 0)
+                                                       *(bool *) entry->value = false;
+                                               else
+                                                       valid = false;
+
+                                       } break;
+                               }
+
+                               if (! valid)
+                                       fprintf(stderr, "Invalid value for setting %s in %s\n", key, path);
+
+                               found = true;
+                               break;
+                       }
+               }
+
+               if (! found)
+                       fprintf(stderr, "Unknown setting %s in %s\n", key, path);
+       }
+}
diff --git a/src/config.h b/src/config.h
new file mode 100644 (file)
index 0000000..c69a5fd
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef _CONFIG_H_
+#define _CONFIG_H_
+
+#include <stddef.h>
+
+typedef enum
+{
+       CT_STRING,
+       CT_INT,
+       CT_UINT,
+       CT_FLOAT,
+       CT_BOOL,
+} ConfigType;
+
+typedef struct
+{
+       ConfigType type;
+       char *key;
+       void *value;
+} ConfigEntry;
+
+void config_read(char *path, ConfigEntry *entries, size_t num_entries);
+
+#endif
index 3f9a584040204780574e7f407da7ac3ced5da189..d0e962c254ef2f179dbfc939711c21945daa3787 100644 (file)
@@ -68,8 +68,6 @@ static void list_disconnect_client(void *key, unused void *value, unused void *a
 // start up the server after socket was created, then accept connections until interrupted, then shutdown server
 static void server_run(int fd)
 {
-       server.config.simulation_distance = 10;
-
        server.sockfd = fd;
        pthread_rwlock_init(&server.clients_rwlck, NULL);
        server.clients = list_create(NULL);
index a565c766266868799fbb73a2b3405eff3b13beb9..3a2ca84c04435b07da417c0b8df8333e00fae134 100644 (file)
@@ -16,9 +16,6 @@ typedef struct
        List clients;                   // Client * -> NULL map with all connected clients
        pthread_rwlock_t players_rwlck; // lock to protect player list
        List players;                   // char * -> Client * map with clients that have finished auth
-       struct {
-               u32 simulation_distance;    // perimeter of the cube that players can see blocks in is simulation_distance * 2 + 1
-       } config;                       // configuration, ToDo: read from config file
 } Server;
 
 typedef struct Client
index 2a437ffdaca76ea2cb7652f440dea810b040a6c5..35e3784f400b621e11b6df04f0b37c3ebb5ba521 100644 (file)
@@ -3,6 +3,7 @@
 #include "day.h"
 #include "server/database.h"
 #include "server/server.h"
+#include "server/server_config.h"
 #include "server/server_map.h"
 #include "perlin.h"
 #include "util.h"
@@ -50,7 +51,7 @@ static bool auth_handler(Client *client, bool good)
        bool ret = write_u32(client->fd, CC_AUTH) && write_u8(client->fd, success);
        if (ret && success)
                ret = ret
-                       && write_u32(client->fd, CC_INFO) && write_u32(client->fd, client->server->config.simulation_distance) && write_s32(client->fd, seed)
+                       && write_u32(client->fd, CC_INFO) && write_u32(client->fd, server_config.simulation_distance) && write_s32(client->fd, seed)
                        && write_u32(client->fd, CC_SETPOS) && write_v3f64(client->fd, client->pos)
                        && write_u32(client->fd, CC_TIMEOFDAY) && write_u64(client->fd, (u64) get_time_of_day());
        pthread_mutex_unlock(&client->mtx);
diff --git a/src/server/server_config.c b/src/server/server_config.c
new file mode 100644 (file)
index 0000000..ec79abc
--- /dev/null
@@ -0,0 +1,18 @@
+#include "config.h"
+#include "server/server_config.h"
+
+struct ServerConfig server_config = {
+       .simulation_distance = 10,
+};
+
+__attribute__((constructor)) static void server_config_init()
+{
+       config_read("server.conf", (ConfigEntry[]) {
+               {
+                       .type = CT_UINT,
+                       .key = "simulation_distance",
+                       .value = &server_config.simulation_distance,
+               },
+       }, 1);
+}
+
diff --git a/src/server/server_config.h b/src/server/server_config.h
new file mode 100644 (file)
index 0000000..992a3c8
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef _SERVER_CONFIG_H_
+#define _SERVER_CONFIG_H_
+
+extern struct ServerConfig {
+       unsigned int simulation_distance;
+} server_config;
+
+#endif
index 7c5a3231b0439143700fe72a74358f0bd98a3941..ae7e76e0c5a92f6af2ef7444270808193defa5bf 100644 (file)
@@ -1,4 +1,3 @@
-#define _GNU_SOURCE
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -6,6 +5,7 @@
 #include "map.h"
 #include "server/database.h"
 #include "server/mapgen.h"
+#include "server/server_config.h"
 #include "server/server_map.h"
 #include "signal_handlers.h"
 #include "util.h"
@@ -52,7 +52,7 @@ static void send_block_to_near(MapBlock *block)
        ITERATE_LIST(&server->players, pair) {
                Client *client = pair->value;
 
-               if (within_simulation_distance(client->pos, block->pos, server->config.simulation_distance))
+               if (within_simulation_distance(client->pos, block->pos, server_config.simulation_distance))
                        send_block(client, block);
        }
        pthread_rwlock_unlock(&server->players_rwlck);
@@ -310,7 +310,7 @@ void server_map_deinit()
 // handle block request from client (thread safe)
 void server_map_requested_block(Client *client, v3s32 pos)
 {
-       if (within_simulation_distance(client->pos, pos, server->config.simulation_distance)) {
+       if (within_simulation_distance(client->pos, pos, server_config.simulation_distance)) {
                MapBlock *block = map_get_block(server_map.map, pos, true);
 
                pthread_mutex_lock(&block->mtx);
@@ -335,7 +335,7 @@ void server_map_requested_block(Client *client, v3s32 pos)
 void server_map_prepare_spawn()
 {
        s32 done = 0;
-       s32 dist = server->config.simulation_distance;
+       s32 dist = server_config.simulation_distance;
        s32 total = (dist * 2 + 1);
        total *= total * total;
        s32 last_percentage = -1;