GLES2N64: Enabled Framebuffer (and lowres) rendering
[mupen64plus-pandora.git] / source / gles2n64 / src / Config.cpp
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  *   Mupen64plus - Config_nogui.cpp                                        *
3  *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
4  *   Copyright (C) 2008 Tillin9                                            *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
20  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21
22 #include <errno.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #include "Config.h"
28 #include "gles2N64.h"
29 #include "RSP.h"
30 #include "Textures.h"
31 #include "OpenGL.h"
32
33 #include "Config.h"
34 #include "Common.h"
35
36
37 Config config;
38
39 struct Option
40 {
41     const char* name;
42     int*  data;
43     const int   initial;
44 };
45
46
47 #define CONFIG_VERSION 2
48
49 Option configOptions[] =
50 {
51     {"#gles2n64 Graphics Plugin for N64", NULL, 0},
52     {"#by Orkin / glN64 developers and Adventus.", NULL, 0},
53
54     {"config version", &config.version, 0},
55     {"", NULL, 0},
56
57     {"#Window Settings:", NULL, 0},
58     {"window xpos", &config.window.xpos, 0},
59     {"window ypos", &config.window.ypos, 0},
60     {"window width", &config.window.width, 800},
61     {"window height", &config.window.height, 480},
62     {"window refwidth", &config.window.refwidth, 800},
63     {"window refheight", &config.window.refheight, 480},
64     {"", NULL, 0},
65
66     {"#Framebuffer Settings:",NULL,0},
67     {"framebuffer enable", &config.framebuffer.enable, 0},
68     {"framebuffer bilinear", &config.framebuffer.bilinear, 0},
69     {"framebuffer width", &config.framebuffer.width, 400},
70     {"framebuffer height", &config.framebuffer.height, 240},
71 //    {"framebuffer width", &config.framebuffer.width, 800},
72 //    {"framebuffer height", &config.framebuffer.height, 480},
73     {"", NULL, 0},
74
75     {"#VI Settings:", NULL, 0},
76     {"video force", &config.video.force, 0},
77     {"video width", &config.video.width, 320},
78     {"video height", &config.video.height, 240},
79     {"video stretch", &config.stretchVideo, 0},
80     {"", NULL, 0},
81
82     {"#Render Settings:", NULL, 0},
83     {"enable fog", &config.enableFog, 0},
84     {"enable primitive z", &config.enablePrimZ, 1},
85     {"enable lighting", &config.enableLighting, 1},
86     {"enable alpha test", &config.enableAlphaTest, 1},
87     {"enable clipping", &config.enableClipping, 0},
88     {"enable face culling", &config.enableFaceCulling, 1},
89     {"enable noise", &config.enableNoise, 0},
90     {"", NULL, 0},
91
92     {"#Texture Settings:", NULL, 0},
93     {"texture 2xSAI", &config.texture.sai2x, 0},
94     {"texture force bilinear", &config.texture.forceBilinear, 0},
95     {"texture max anisotropy", &config.texture.maxAnisotropy, 0},
96     {"texture use IA", &config.texture.useIA, 0},
97     {"texture fast CRC", &config.texture.fastCRC, 1},
98     {"texture pow2", &config.texture.pow2, 1},
99     {"", NULL, 0},
100
101     {"#Frame skip:", NULL, 0},
102     {"auto frameskip", &config.autoFrameSkip, 1},
103     {"max frameskip", &config.maxFrameSkip, 3},
104     {"target FPS", &config.targetFPS, 20},
105     {"frame render rate", &config.frameRenderRate, 1},
106     {"vertical sync", &config.verticalSync, 0},
107     {"", NULL, 0},
108
109     {"#Other Settings:", NULL, 0},
110     {"update mode", &config.updateMode, SCREEN_UPDATE_AT_VI_UPDATE },
111     {"ignore offscreen rendering", &config.ignoreOffscreenRendering, 0},
112     {"force screen clear", &config.forceBufferClear, 0},
113     {"flip vertical", &config.screen.flipVertical, 0},
114 // paulscode: removed from pre-compile to a config option
115 //// (part of the Galaxy S Zelda crash-fix
116     {"tribuffer opt", &config.tribufferOpt, 1},
117 //
118     {"", NULL, 0},
119
120     {"#Hack Settings:", NULL, 0},
121     {"hack banjo tooie", &config.hackBanjoTooie, 0},
122     {"hack zelda", &config.hackZelda, 0},
123     {"hack alpha", &config.hackAlpha, 0},
124     {"hack z", &config.zHack, 0}
125
126 };
127
128 const int configOptionsSize = sizeof(configOptions) / sizeof(Option);
129
130 void Config_WriteConfig(const char *filename)
131 {
132     config.version = CONFIG_VERSION;
133     FILE* f = fopen(filename, "w");
134     if (!f)
135     {
136         LOG(LOG_ERROR, "Could Not Open %s for writing\n", filename);
137     }
138
139     for(int i=0; i<configOptionsSize; i++)
140     {
141         Option *o = &configOptions[i];
142         fprintf(f, "%s", o->name);
143         if (o->data) fprintf(f,"=%i", *(o->data));
144         fprintf(f, "\n");
145     }
146
147
148     fclose(f);
149 }
150
151 void Config_SetDefault()
152 {
153     for(int i=0; i < configOptionsSize; i++)
154     {
155         Option *o = &configOptions[i];
156         if (o->data) *(o->data) = o->initial;
157     }
158 }
159
160 void Config_SetOption(char* line, char* val)
161 {
162     for(int i=0; i< configOptionsSize; i++)
163     {
164         Option *o = &configOptions[i];
165         if (strcasecmp(line, o->name) == 0)
166         {
167             if (o->data)
168             {
169                 int v = atoi(val);
170                 *(o->data) = v;
171                 LOG(LOG_VERBOSE, "Config Option: %s = %i\n", o->name, v);
172             }
173             break;
174         }
175     }
176 }
177
178 void Config_LoadRomConfig(unsigned char* header)
179 {
180     char line[4096];
181
182     // get the name of the ROM
183     for (int i=0; i<20; i++) config.romName[i] = header[0x20+i];
184     config.romName[20] = '\0';
185     while (config.romName[strlen(config.romName)-1] == ' ')
186     {
187         config.romName[strlen(config.romName)-1] = '\0';
188     }
189
190     switch(header[0x3e])
191     {
192         // PAL codes
193         case 0x44:
194         case 0x46:
195         case 0x49:
196         case 0x50:
197         case 0x53:
198         case 0x55:
199         case 0x58:
200         case 0x59:
201             config.romPAL = true;
202             break;
203
204         // NTSC codes
205         case 0x37:
206         case 0x41:
207         case 0x45:
208         case 0x4a:
209             config.romPAL = false;
210             break;
211
212         // Fallback for unknown codes
213         default:
214             config.romPAL = false;
215     }
216
217     LOG(LOG_MINIMAL, "Rom is %s\n", config.romPAL ? "PAL" : "NTSC");
218 printf("Rom is %s\n", config.romPAL ? "PAL" : "NTSC");
219
220     const char *filename = ConfigGetSharedDataFilepath("gles2n64rom.conf");
221     FILE *f = fopen(filename,"r");
222     if (!f)
223     {
224         LOG(LOG_MINIMAL, "Could not find %s Rom settings file, using global.\n", filename);
225 printf("Could not find %s Rom settings file, using global.\n", filename);
226         return;
227     }
228     else
229     {
230         LOG(LOG_MINIMAL, "[gles2N64]: Searching %s Database for \"%s\" ROM\n", filename, config.romName);
231 printf("[gles2N64]: Searching %s Database for \"%s\" ROM\n", filename, config.romName);
232         bool isRom = false;
233         while (!feof(f))
234         {
235             fgets(line, 4096, f);
236             if (line[0] == '\n') continue;
237
238             if (strncmp(line,"rom name=", 9) == 0)
239             {
240                 //Depending on the editor, end lines could be terminated by "LF" or "CRLF"
241                 char* lf = strchr(line, '\n'); //Line Feed
242                 char* cr = strchr(line, '\r'); //Carriage Return
243                 if (lf) *lf='\0';
244                 if (cr) *cr='\0';
245                 isRom = (strcasecmp(config.romName, line+9) == 0);
246             }
247             else
248             {
249                 if (isRom)
250                 {
251                     char* val = strchr(line, '=');
252                     if (!val) continue;
253                     *val++ = '\0';
254                     Config_SetOption(line,val);
255                     LOG(LOG_MINIMAL, "%s = %s", line, val);
256 printf("%s = %s", line, val);
257                 }
258             }
259         }
260     }
261         
262     fclose(f);
263 }
264
265 void Config_LoadConfig()
266 {
267     FILE *f;
268     char line[4096];
269
270     // default configuration
271     Config_SetDefault();
272
273     // read configuration
274     const char *filename = ConfigGetSharedDataFilepath("gles2n64.conf");
275     f = fopen(filename, "r");
276     if (!f)
277     {
278         LOG(LOG_MINIMAL, "[gles2N64]: Couldn't open config file '%s' for reading: %s\n", filename, strerror( errno ) );
279         LOG(LOG_MINIMAL, "[gles2N64]: Attempting to write new Config \n");
280 printf("[gles2N64]: Couldn't open config file '%s' for reading: %s\n", filename, strerror( errno ) );
281 printf("[gles2N64]: Attempting to write new Config \n");
282         Config_WriteConfig(filename);
283     }
284     else
285     {
286         LOG(LOG_MINIMAL, "[gles2n64]: Loading Config from %s \n", filename);
287 printf("[gles2n64]: Loading Config from %s \n", filename);
288
289         while (!feof( f ))
290         {
291             char *val;
292             fgets( line, 4096, f );
293
294             if (line[0] == '#' || line[0] == '\n')
295                 continue;
296
297             val = strchr( line, '=' );
298             if (!val) continue;
299
300             *val++ = '\0';
301
302              Config_SetOption(line,val);
303         }
304
305         if (config.version < CONFIG_VERSION)
306         {
307             LOG(LOG_WARNING, "[gles2N64]: Wrong config version, rewriting config with defaults\n");
308 printf("[gles2N64]: Wrong config version, rewriting config with defaults\n");
309             Config_SetDefault();
310             Config_WriteConfig(filename);
311         }
312
313         fclose(f);
314     }
315 }
316