DEADSOFTWARE

system: game itself do not depends on sdl
[flatwaifu.git] / src / config.c
1 /*
2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
4 Copyright (C) <ARembo@gmail.com> 2011
6 This file is part of the Doom2D:Rembo project.
8 Doom2D:Rembo is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 Doom2D:Rembo is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/> or
19 write to the Free Software Foundation, Inc.,
20 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
23 #include "glob.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "map.h"
28 #include "sound.h"
29 #include "music.h"
30 #include "view.h"
31 #include "monster.h"
32 #include "player.h"
33 #include "menu.h"
34 #include "files.h"
35 #include "render.h"
36 #include "error.h"
37 #include "input.h"
38 #include "my.h"
40 enum{NONE,BYTE,WORD,DWORD,STRING,SW_ON,SW_OFF,FILES,KEY};
42 typedef struct{
43 char *par,*cfg;
44 void *p;
45 byte t,o;
46 }cfg_t;
48 byte cheat=0;
49 byte shot_vga=0;
50 char cd_path[128]="";
51 char cfg_file[128]="default.cfg";
53 static char buf[256];
55 static cfg_t cfg[]={
56 {"file",NULL,NULL,FILES,0},
57 {"cheat",NULL,&cheat,SW_ON,0},
58 {"vga","screenshot",&shot_vga,SW_ON,0},
59 {"sndvol","sound_volume",&snd_vol,WORD,0},
60 {"musvol","music_volume",&mus_vol,WORD,0},
61 // {"fullscr","fullscreen",&fullscreen,SW_ON,0},
62 // {"window",NULL,&fullscreen,SW_OFF,0},
63 {NULL,"sky",&w_horiz,SW_ON,0},
64 {"mon",NULL,&nomon,SW_OFF,0},
65 // {"gamma","gamma",&gammaa,DWORD,0},
66 {"warp",NULL,&_warp,BYTE,0},
67 {"width","screen_width",&SCRW,DWORD,0},
68 {"height","screen_height",&SCRH,DWORD,0},
69 {NULL,"music_random",&music_random,SW_ON,0},
70 {NULL,"music_time",&music_time,DWORD,0},
71 {NULL,"music_fade",&music_fade,DWORD,0},
72 {NULL,"pl1_left", &pl1.kl,KEY,0},
73 {NULL,"pl1_right",&pl1.kr,KEY,0},
74 {NULL,"pl1_up", &pl1.ku,KEY,0},
75 {NULL,"pl1_down", &pl1.kd,KEY,0},
76 {NULL,"pl1_jump", &pl1.kj,KEY,0},
77 {NULL,"pl1_fire", &pl1.kf,KEY,0},
78 {NULL,"pl1_next", &pl1.kwr,KEY,0},
79 {NULL,"pl1_prev", &pl1.kwl,KEY,0},
80 {NULL,"pl1_use", &pl1.kp,KEY,0},
81 {NULL,"pl2_left", &pl2.kl,KEY,0},
82 {NULL,"pl2_right",&pl2.kr,KEY,0},
83 {NULL,"pl2_up", &pl2.ku,KEY,0},
84 {NULL,"pl2_down", &pl2.kd,KEY,0},
85 {NULL,"pl2_jump", &pl2.kj,KEY,0},
86 {NULL,"pl2_fire", &pl2.kf,KEY,0},
87 {NULL,"pl2_next", &pl2.kwr,KEY,0},
88 {NULL,"pl2_prev", &pl2.kwl,KEY,0},
89 {NULL,"pl2_use", &pl2.kp,KEY,0},
90 {"config",NULL,cfg_file,STRING,0},
91 {NULL,NULL,NULL,NONE,0}
92 };
94 void CFG_args(int argc, char *argv[]) {
95 int j;
96 dword n;
97 char *s;
99 logo("CFG_args: проверка командной строки\n");
101 int i;
102 char *pbuf = buf;
103 for (i=1;i<argc;i++){
104 strcpy(pbuf,argv[i]);
105 pbuf+=strlen(argv[i]);
106 strcpy(pbuf," ");
107 pbuf++;
110 for(s=strtok(buf," \r\n\t");s;s=strtok(NULL," \r\n\t")) {//for(s=strtok(getcmd(buf)," \r\n\t");s;s=strtok(NULL," \r\n\t")) {
111 next:
112 if(*s=='/' || *s=='-') ++s;
113 for(j=0;cfg[j].t;++j) if(cfg[j].par) if(strcasecmp(s,cfg[j].par)==0) {
114 switch(cfg[j].t) {
115 case BYTE:
116 n=strtol(s=strtok(NULL," \r\n\t"),NULL,0);
117 *((byte *)cfg[j].p)=(byte)n;
118 break;
119 case WORD:
120 n=strtol(s=strtok(NULL," \r\n\t"),NULL,0);
121 *((word *)cfg[j].p)=(word)n;
122 break;
123 case DWORD:
124 n=strtol(s=strtok(NULL," \r\n\t"),NULL,0);
125 *((dword *)cfg[j].p)=n;
126 break;
127 case STRING:
128 strcpy((char *)cfg[j].p,s=strtok(NULL," \r\n\t"));
129 break;
130 case SW_ON:
131 *((byte *)cfg[j].p)=ON;
132 if(cfg[j+1].t==SW_OFF && cfg[j+1].p==cfg[j].p) cfg[j+1].o=1;
133 if(j>0) if(cfg[j-1].t==SW_OFF && cfg[j-1].p==cfg[j].p) cfg[j-1].o=1;
134 break;
135 case SW_OFF:
136 *((byte *)cfg[j].p)=OFF;
137 if(cfg[j+1].t==SW_ON && cfg[j+1].p==cfg[j].p) cfg[j+1].o=1;
138 if(j>0) if(cfg[j-1].t==SW_ON && cfg[j-1].p==cfg[j].p) cfg[j-1].o=1;
139 break;
140 case FILES:
141 for(s=strtok(NULL," \r\n\t");s;s=strtok(NULL," \r\n\t")) {
142 if(*s=='/' || *s=='-') goto next;
143 #ifdef DEMO
144 logo(" %s НЕ подключен!\n",s);
145 #else
146 F_addwad(s);
147 #endif
148 }break;
149 default:
150 ERR_failinit("!!! Неизвестный тип в cfg !!!");
152 cfg[j].o=1;break;
157 void CFG_load(void) {
158 int j;
159 FILE *h;
160 dword n;
161 char s[128];
162 char *p1,*p2;
164 char pc[50];
165 char *e = getenv("HOME");
166 strncpy(pc, e, 30);
167 strcpy(&pc[strlen(pc)], "/default.cfg");
168 if (!fexists(pc)) {
169 strcpy(pc, "default.cfg");
170 if (!fexists(pc)) {
171 strcpy(pc, "/usr/share/doom2d-rembo/default.cfg");
172 if (!fexists(pc)) {
173 logo("default.cfg not found\n");
174 return;
179 logo("CFG_load: загрузка конфигурации из %s\n",pc);
180 if((h=fopen(pc,"rb"))==NULL) {
181 perror("Cannot open file");return;
183 while(!feof(h)) {
184 F_readstr(h,s,127);
185 if(*s==';' || s[1]==';')
186 continue; // comment
187 if(!(p1=strtok(s,"\r\n\t=;")))
188 continue; //if(!(p1=strtok(s,"\r\n\t =;"))) continue;
189 if(!(p2=strtok(NULL,"\r\n\t=;")))
190 continue;//if(!(p2=strtok(NULL,"\r\n\t =;"))) continue;
191 for(j=0;cfg[j].t;++j) {
192 if(cfg[j].cfg && !cfg[j].o) {
193 if(strcasecmp(p1,cfg[j].cfg)==0) {
194 switch(cfg[j].t) {
195 case BYTE:
196 n=strtol(p2,NULL,0);
197 *((byte *)cfg[j].p)=(byte)n;
198 break;
199 case WORD:
200 n=strtol(p2,NULL,0);
201 *((word *)cfg[j].p)=(word)n;
202 break;
203 case DWORD:
204 n=strtol(p2,NULL,0);
205 *((dword *)cfg[j].p)=n;
206 break;
207 case STRING:
208 strcpy((char *)cfg[j].p,p2);
209 break;
210 case SW_ON:
211 case SW_OFF:
212 if(strcasecmp(p2,"ON")==0) {
213 *((byte *)cfg[j].p)=ON;
214 break;
216 if(strcasecmp(p2,"OFF")==0) {
217 *((byte *)cfg[j].p)=OFF;
218 break;
220 *((byte *)cfg[j].p)=strtol(p2,NULL,0);
221 break;
222 case FILES:
223 break;
224 case KEY:
226 int k = I_string_to_key(p2);
227 if (k != KEY_UNKNOWN) {
228 *((int *)cfg[j].p)=k;
229 } else {
230 int i;
231 logo("Unknown key in cfg: %s=%s\n",p1,p2);
232 logo("List available key names:\n");
233 for(i = 1; i <= KEY__LAST; i++) {
234 logo(" %s\n", I_key_to_string(i));
238 break;
239 default:
240 ERR_failinit("!!! Неизвестный тип в cfg !!!");
241 } // switch
242 break;
243 } // if
244 } // if
245 } // for
246 } // while
247 fclose(h);
250 void CFG_save(void) {
251 /*
252 char s[140],str[140];
253 char *p;
254 FILE *h,*oh;
256 remove("CONFIG.ZZZ");
257 if(rename(cfg_file,"CONFIG.ZZZ")) return;
258 if(!(h=fopen("CONFIG.ZZZ","rt")))
259 {rename("CONFIG.ZZZ",cfg_file);return;}
260 if(!(oh=fopen(cfg_file,"wt")))
261 {fclose(h);rename("CONFIG.ZZZ",cfg_file);return;}
262 for(;;) {
263 if(!fgets(s,128,h)) break;
264 strcpy(str,s);
265 if(!(p=strtok(str,"\r\n\t =;"))) {fprintf(oh,"%s",s);continue;}
266 if(strcasecmp(p,"sound_volume")==0)
267 sprintf(s,"sound_volume=%d\n",snd_vol);
268 else if(strcasecmp(p,"music_volume")==0)
269 sprintf(s,"music_volume=%d\n",mus_vol);
270 else if(strcasecmp(p,"gamma")==0)
271 sprintf(s,"gamma=%d\n",gammaa);
272 else if(strcasecmp(p,"sound_interp")==0)
273 sprintf(s,"sound_interp=%s\n",s_interp?"on":"off");
274 fprintf(oh,"%s",s);
276 fclose(oh);fclose(h);
277 remove("CONFIG.ZZZ");
278 */