rubber band in platform-specific code
[retroray] / src / options.c
1 /*
2 RetroRay - integrated standalone vintage modeller/renderer
3 Copyright (C) 2023  John Tsiombikas <nuclear@mutantstargoat.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <string.h>
20 #include <errno.h>
21 #include "options.h"
22 #include "treestor.h"
23 #include "logger.h"
24
25 #define DEF_XRES                640
26 #define DEF_YRES                480
27 #define DEF_VSYNC               1
28 #define DEF_FULLSCR             0
29 #define DEF_MOUSE_SPEED 50
30 #define DEF_SBALL_SPEED 50
31
32
33 struct options opt = {
34         DEF_XRES, DEF_YRES,
35         DEF_VSYNC,
36         DEF_FULLSCR,
37         DEF_MOUSE_SPEED, DEF_SBALL_SPEED,
38 };
39
40 int load_options(const char *fname)
41 {
42         struct ts_node *cfg;
43
44         if(!(cfg = ts_load(fname))) {
45                 return -1;
46         }
47         infomsg("loaded config: %s\n", fname);
48
49         opt.xres = ts_lookup_int(cfg, "options.video.xres", DEF_XRES);
50         opt.yres = ts_lookup_int(cfg, "options.video.yres", DEF_YRES);
51         opt.vsync = ts_lookup_int(cfg, "options.video.vsync", DEF_VSYNC);
52         opt.fullscreen = ts_lookup_int(cfg, "options.video.fullscreen", DEF_FULLSCR);
53
54         opt.mouse_speed = ts_lookup_int(cfg, "options.input.mousespeed", DEF_MOUSE_SPEED);
55         opt.sball_speed = ts_lookup_int(cfg, "options.input.sballspeed", DEF_SBALL_SPEED);
56
57         ts_free_tree(cfg);
58         return 0;
59 }
60
61 #define WROPT(lvl, fmt, val, defval) \
62         do { \
63                 int i; \
64                 for(i=0; i<lvl; i++) fputc('\t', fp); \
65                 if((val) == (defval)) fputc('#', fp); \
66                 fprintf(fp, fmt "\n", val); \
67         } while(0)
68
69 int save_options(const char *fname)
70 {
71         FILE *fp;
72
73         printf("writing config: %s\n", fname);
74
75         if(!(fp = fopen(fname, "wb"))) {
76                 fprintf(stderr, "failed to save options (%s): %s\n", fname, strerror(errno));
77         }
78         fprintf(fp, "options {\n");
79         fprintf(fp, "\tvideo {\n");
80         WROPT(2, "xres = %d", opt.xres, DEF_XRES);
81         WROPT(2, "yres = %d", opt.yres, DEF_YRES);
82         WROPT(2, "vsync = %d", opt.vsync, DEF_VSYNC);
83         WROPT(2, "fullscreen = %d", opt.fullscreen, DEF_FULLSCR);
84         fprintf(fp, "\t}\n");
85
86         fprintf(fp, "\tinput {\n");
87         WROPT(2, "mousespeed = %d", opt.mouse_speed, DEF_MOUSE_SPEED);
88         WROPT(2, "sballspeed = %d", opt.sball_speed, DEF_SBALL_SPEED);
89         fprintf(fp, "\t}\n");
90
91         fprintf(fp, "}\n");
92         fprintf(fp, "# v" "i:ts=4 sts=4 sw=4 noexpandtab:\n");
93
94         fclose(fp);
95         return 0;
96 }