Commits

Kévin Delbrayelle committed 466cb3a

settings

Comments (0)

Files changed (3)

game/inc/io/settings.h

+#ifndef _IO_SETTINGS_H_
+#define _IO_SETTINGS_H_
+
+#include <stdint.h>
+
+
+namespace io {
+
+
+enum settings_default
+{
+    SETTINGS_DEFAULT_DISPLAY_WIDTH  = 640,
+    SETTINGS_DEFAULT_DISPLAY_HEIGHT = 480,
+};
+
+
+struct Settings
+{
+    uint16_t    display_width;
+    uint16_t    display_height;
+    float       display_pixel_ratio;
+    uint8_t     display_fullscreen;
+
+    int read(const char * filename);
+    int save();
+};
+
+
+} // namespace io
+
+
+#endif // _IO_SETTINGS_H_

game/settings/game.cfg

+#----------------------
+# Configuration File
+#----------------------
+
+display:
+{
+    fullscreen = true;
+    size = { width = 1280; height = 720; };
+    pixel_ratio = 1.0;
+};
+
+input:
+{
+
+};

game/src/io/settings.cpp

+#include "io/settings.h"
+
+#include <cassert>
+#include <libconfig.h>
+#include <cstring>
+
+
+namespace io
+{
+
+
+int Settings::read(const char * filename)
+{
+    config_t config;
+    config_setting_t * root_setting_p;
+    config_setting_t * display_setting_p;
+    config_setting_t * display_size_setting_p;
+    config_setting_t * setting_p;
+
+    config_init(&config);
+
+    FILE * stream = fopen (filename, "r");
+    assert(stream);
+    config_read(&config, stream);
+    fclose(stream);
+
+    root_setting_p = config_root_setting (&config);
+
+    if ( config_setting_is_root(root_setting_p) )
+    {
+        int value;
+
+        // get display settings
+        display_setting_p      = config_setting_get_member(root_setting_p, "display");
+        setting_p              = config_setting_get_member(display_setting_p, "fullscreen");
+        display_fullscreen     = config_setting_get_bool(setting_p);
+
+        // get pixel ratio
+        setting_p              = config_setting_get_member(display_setting_p, "pixel_ratio");
+        display_pixel_ratio    = config_setting_get_float(setting_p);
+        display_pixel_ratio    = display_pixel_ratio > 0.0 ? display_pixel_ratio : 1.0;
+
+        // get display size
+        display_size_setting_p = config_setting_get_member(display_setting_p, "size");
+
+        setting_p              = config_setting_get_member(display_size_setting_p, "width");
+        value                  = config_setting_get_int(setting_p);
+        display_width          = value > 0 ? value : SETTINGS_DEFAULT_DISPLAY_WIDTH;
+
+        setting_p              = config_setting_get_member(display_size_setting_p, "height");
+        value                  = config_setting_get_int(setting_p);
+        display_height         = value > 0 ? value : SETTINGS_DEFAULT_DISPLAY_HEIGHT;
+    }
+
+    config_destroy(&config);
+
+    return 0;
+}
+
+
+}