GLES2N64 (from mupen64plus-ae) plugin. Compile and run on the OpenPandora
[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     {"", NULL, 0},
80
81     {"#Render Settings:", NULL, 0},
82     {"enable fog", &config.enableFog, 0},
83     {"enable primitive z", &config.enablePrimZ, 1},
84     {"enable lighting", &config.enableLighting, 1},
85     {"enable alpha test", &config.enableAlphaTest, 1},
86     {"enable clipping", &config.enableClipping, 0},
87     {"enable face culling", &config.enableFaceCulling, 1},
88     {"enable noise", &config.enableNoise, 0},
89     {"", NULL, 0},
90
91     {"#Texture Settings:", NULL, 0},
92     {"texture 2xSAI", &config.texture.sai2x, 0},
93     {"texture force bilinear", &config.texture.forceBilinear, 0},
94     {"texture max anisotropy", &config.texture.maxAnisotropy, 0},
95     {"texture use IA", &config.texture.useIA, 0},
96     {"texture fast CRC", &config.texture.fastCRC, 1},
97     {"texture pow2", &config.texture.pow2, 1},
98     {"", NULL, 0},
99
100     {"#Frame skip:", NULL, 0},
101     {"auto frameskip", &config.autoFrameSkip, 0},
102     {"max frameskip", &config.maxFrameSkip, 0},
103     {"target FPS", &config.targetFPS, 20},
104     {"frame render rate", &config.frameRenderRate, 1},
105     {"vertical sync", &config.verticalSync, 0},
106     {"", NULL, 0},
107
108     {"#Other Settings:", NULL, 0},
109     {"update mode", &config.updateMode, SCREEN_UPDATE_AT_VI_UPDATE },
110     {"ignore offscreen rendering", &config.ignoreOffscreenRendering, 0},
111     {"force screen clear", &config.forceBufferClear, 0},
112     {"flip vertical", &config.screen.flipVertical, 0},
113 // paulscode: removed from pre-compile to a config option
114 //// (part of the Galaxy S Zelda crash-fix
115     {"tribuffer opt", &config.tribufferOpt, 1},
116 //
117     {"", NULL, 0},
118
119     {"#Hack Settings:", NULL, 0},
120     {"hack banjo tooie", &config.hackBanjoTooie, 0},
121     {"hack zelda", &config.hackZelda, 0},
122     {"hack alpha", &config.hackAlpha, 0},
123     {"hack z", &config.zHack, 0},
124
125 };
126
127 const int configOptionsSize = sizeof(configOptions) / sizeof(Option);
128
129 void Config_WriteConfig(const char *filename)
130 {
131     config.version = CONFIG_VERSION;
132     FILE* f = fopen(filename, "w");
133     if (!f)
134     {
135         LOG(LOG_ERROR, "Could Not Open %s for writing\n", filename);
136     }
137
138     for(int i=0; i<configOptionsSize; i++)
139     {
140         Option *o = &configOptions[i];
141         fprintf(f, "%s", o->name);
142         if (o->data) fprintf(f,"=%i", *(o->data));
143         fprintf(f, "\n");
144     }
145
146
147     fclose(f);
148 }
149
150 void Config_SetDefault()
151 {
152     for(int i=0; i < configOptionsSize; i++)
153     {
154         Option *o = &configOptions[i];
155         if (o->data) *(o->data) = o->initial;
156     }
157 }
158
159 void Config_SetOption(char* line, char* val)
160 {
161     for(int i=0; i< configOptionsSize; i++)
162     {
163         Option *o = &configOptions[i];
164         if (strcasecmp(line, o->name) == 0)
165         {
166             if (o->data)
167             {
168                 int v = atoi(val);
169                 *(o->data) = v;
170                 LOG(LOG_VERBOSE, "Config Option: %s = %i\n", o->name, v);
171             }
172             break;
173         }
174     }
175 }
176
177 void Config_LoadRomConfig(unsigned char* header)
178 {
179     char line[4096];
180
181     // get the name of the ROM
182     for (int i=0; i<20; i++) config.romName[i] = header[0x20+i];
183     config.romName[20] = '\0';
184     while (config.romName[strlen(config.romName)-1] == ' ')
185     {
186         config.romName[strlen(config.romName)-1] = '\0';
187     }
188
189     switch(header[0x3e])
190     {
191         // PAL codes
192         case 0x44:
193         case 0x46:
194         case 0x49:
195         case 0x50:
196         case 0x53:
197         case 0x55:
198         case 0x58:
199         case 0x59:
200             config.romPAL = true;
201             break;
202
203         // NTSC codes
204         case 0x37:
205         case 0x41:
206         case 0x45:
207         case 0x4a:
208             config.romPAL = false;
209             break;
210
211         // Fallback for unknown codes
212         default:
213             config.romPAL = false;
214     }
215
216     LOG(LOG_MINIMAL, "Rom is %s\n", config.romPAL ? "PAL" : "NTSC");
217
218     const char *filename = ConfigGetSharedDataFilepath("gles2n64rom.conf");
219     FILE *f = fopen(filename,"r");
220     if (!f)
221     {
222         LOG(LOG_MINIMAL, "Could not find %s Rom settings file, using global.\n", filename);
223         return;
224     }
225     else
226     {
227         LOG(LOG_MINIMAL, "[gles2N64]: Searching %s Database for \"%s\" ROM\n", filename, config.romName);
228         bool isRom = false;
229         while (!feof(f))
230         {
231             fgets(line, 4096, f);
232             if (line[0] == '\n') continue;
233
234             if (strncmp(line,"rom name=", 9) == 0)
235             {
236                 //Depending on the editor, end lines could be terminated by "LF" or "CRLF"
237                 char* lf = strchr(line, '\n'); //Line Feed
238                 char* cr = strchr(line, '\r'); //Carriage Return
239                 if (lf) *lf='\0';
240                 if (cr) *cr='\0';
241                 isRom = (strcasecmp(config.romName, line+9) == 0);
242             }
243             else
244             {
245                 if (isRom)
246                 {
247                     char* val = strchr(line, '=');
248                     if (!val) continue;
249                     *val++ = '\0';
250                     Config_SetOption(line,val);
251                     LOG(LOG_MINIMAL, "%s = %s", line, val);
252                 }
253             }
254         }
255     }
256         
257     fclose(f);
258 }
259
260 void Config_LoadConfig()
261 {
262     FILE *f;
263     char line[4096];
264
265     // default configuration
266     Config_SetDefault();
267
268     // read configuration
269     const char *filename = ConfigGetSharedDataFilepath("gles2n64.conf");
270     f = fopen(filename, "r");
271     if (!f)
272     {
273         LOG(LOG_MINIMAL, "[gles2N64]: Couldn't open config file '%s' for reading: %s\n", filename, strerror( errno ) );
274         LOG(LOG_MINIMAL, "[gles2N64]: Attempting to write new Config \n");
275         Config_WriteConfig(filename);
276     }
277     else
278     {
279         LOG(LOG_MINIMAL, "[gles2n64]: Loading Config from %s \n", filename);
280
281         while (!feof( f ))
282         {
283             char *val;
284             fgets( line, 4096, f );
285
286             if (line[0] == '#' || line[0] == '\n')
287                 continue;
288
289             val = strchr( line, '=' );
290             if (!val) continue;
291
292             *val++ = '\0';
293
294              Config_SetOption(line,val);
295         }
296
297         if (config.version < CONFIG_VERSION)
298         {
299             LOG(LOG_WARNING, "[gles2N64]: Wrong config version, rewriting config with defaults\n");
300             Config_SetDefault();
301             Config_WriteConfig(filename);
302         }
303
304         fclose(f);
305     }
306 }
307