1 /*
2  * Kuklomenos
3  * Copyright (C) 2008-2009 Martin Bays <mbays@sdf.lonestar.org>
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 http://www.gnu.org/licenses/.
17  */
18 
19 #include "config.h"
20 
21 #include "settings.h"
22 #include "conffile.h"
23 #include "keybindings.h"
24 #include "SDL_gfxPrimitivesDirty.h"
25 #include <getopt.h>
26 #include <SDL/SDL.h>
27 #include <vector>
28 
29 string bgTypeStrings[] = { "none", "stars", "solar" };
30 
Settings()31 Settings::Settings() :
32     debug(false), wizard(false), invuln(false),
33     useAA(AA_YES), showGrid(true), zoomEnabled(true), rotatingView(true),
34     turnRateFactor(1.0), requestedRating(0), speed(0), stopMotion(false),
35     keybindings(defaultKeybindings()), commandToBind(C_NONE),
36     bgType(BG_NONE),
37     fps(30), showFPS(true), width(0), height(0), bpp(16),
38     videoFlags(SDL_RESIZABLE | SDL_SWSURFACE), sound(true), volume(1.0),
39     soundFreq(44100),
40     clockRate(1000)
41 {
42 }
43 
44 Settings settings;
45 
rateOfSpeed(int speed)46 int rateOfSpeed(int speed)
47 { return 1000 + 500*speed; }
48 
speedString(int speed)49 const char* speedString(int speed)
50 { return speed == 0 ? "Normal" : speed == 1 ? "Fast" : "Very Fast"; }
51 
speedStringShort(int speed)52 const char* speedStringShort(int speed)
53 { return speed == 0 ? "N" : speed == 1 ? "F" : "VF"; }
54 
55 #ifndef __APPLE__
load_settings(int argc,char ** argv)56 void load_settings(int argc, char** argv)
57 {
58     config.read();
59     config.exportSettings(settings);
60 
61     while (1)
62     {
63 	int c;
64 	static const struct option long_options[] =
65 	{
66 	    {"width", 1, 0, 'W'},
67 	    {"height", 1, 0, 'H'},
68 	    {"bpp", 1, 0, 'b'},
69 	    {"fps", 1, 0, 'f'},
70 	    {"rating", 1, 0, 'r'},
71 	    {"gamma", 1, 0, 'g' << 8},
72 	    {"noantialias", 0, 0, 'A'},
73 	    {"nogrid", 0, 0, 'G'},
74 	    {"nozoom", 0, 0, 'Z'},
75 	    {"norotate", 0, 0, 'R'},
76 	    {"antialias", 0, 0, 'a'},
77 	    {"grid", 0, 0, 'g'},
78 	    {"zoom", 0, 0, 'z'},
79 	    {"rotate", 0, 0, '@'},
80 	    {"turnrate", 1, 0, 't'},
81 	    {"fullscreen", 0, 0, 'F'},
82 	    {"hwsurface", 0, 0, 's'},
83 	    {"hwpalette", 0, 0, 'P'},
84 	    {"noresizable", 0, 0, 'S'},
85 	    {"nosound", 0, 0, 'q'},
86 	    {"debug", 0, 0, 'd'},
87 	    {"xyzzy", 0, 0, '+'},
88 	    {"invulnerable", 0, 0, 'i'},
89 	    {"stopmotion", 0, 0, 'M'},
90 	    {"speed", 1, 0, 'p'},
91 	    {"aispeed", 1, 0, '-'},
92 	    {"version", 0, 0, 'V'},
93 	    {"help", 0, 0, 'h'},
94 	    {0,0,0,0}
95 	};
96 	c = getopt_long(argc, argv, "W:H:b:f:r:t:I:p:AGZRagzFsPSqdiMVh",
97 		long_options, NULL);
98 	if (c == -1)
99 	    break;
100 	switch (c)
101 	{
102 	    case 'W':
103 		settings.width = atoi(optarg);
104 		break;
105 	    case 'H':
106 		settings.height = atoi(optarg);
107 		break;
108 	    case 'b':
109 		settings.bpp = atoi(optarg);
110 		break;
111 	    case 'f':
112 		settings.fps = atoi(optarg);
113 		break;
114 	    case 'r':
115 		if (1.0 <= atof(optarg))
116 		    settings.requestedRating = atof(optarg);
117 		else
118 		{
119 		    printf("bad rating\n");
120 		    exit(1);
121 		}
122 		break;
123 	    case 'g'<<8:
124 		antialiasGamma = atoi(optarg);
125 		break;
126 	    case 'F':
127 		settings.videoFlags |= SDL_FULLSCREEN;
128 		break;
129 	    case 'S':
130 		settings.videoFlags &= ~SDL_RESIZABLE;
131 		break;
132 	    case 'P':
133 		settings.videoFlags |= SDL_HWPALETTE;
134 		break;
135 	    case 's':
136 		settings.videoFlags |= SDL_HWSURFACE;
137 		break;
138 	    case 'Z':
139 		settings.zoomEnabled = false;
140 		break;
141 	    case 'R':
142 		settings.rotatingView = false;
143 		break;
144 	    case 'A':
145 		settings.useAA =
146 		    (settings.useAA == AA_FORCE) ? AA_YES :
147 		    AA_NO;
148 		break;
149 	    case 'G':
150 		settings.showGrid = false;
151 		break;
152 	    case 'z':
153 		settings.zoomEnabled = true;
154 		break;
155 	    case '@':
156 		settings.rotatingView = true;
157 		break;
158 	    case 't':
159 		if (0 < atof(optarg) <= 1.0)
160 		    settings.turnRateFactor = atof(optarg);
161 		break;
162 	    case 'a':
163 		settings.useAA =
164 		    (settings.useAA == AA_NO) ? AA_YES :
165 		    AA_FORCE;
166 		break;
167 	    case 'g':
168 		settings.showGrid = true;
169 		break;
170 	    case 'q':
171 		settings.sound = false;
172 		break;
173 	    case 'd':
174 		settings.debug = true;
175 		break;
176 	    case '+':
177 		settings.wizard = true;
178 		settings.debug = true;
179 		break;
180 	    case 'i':
181 		settings.invuln = true;
182 		settings.debug = true;
183 		break;
184 	    case 'M':
185 		settings.stopMotion = true;
186 		settings.debug = true;
187 		break;
188 	    case 'p':
189 		settings.speed = (int)(atof(optarg));
190 		if (settings.speed < 0) settings.speed = 0;
191 		if (settings.speed > 2) settings.speed = 2;
192 		break;
193 	    case 'V':
194 		printf("%s\n", PACKAGE_STRING);
195 		exit(0);
196 	    case 'h':
197 		printf("Options:\n\t"
198 			"-W --width WIDTH\n\t-H --height HEIGHT\n\t-b --bpp BITS\n\t-f --fps FPS\n\t"
199 			"-F --fullscreen\n\t-S --noresizable\n\t-P --hwpalette\n\t-s --hwsurface\n\t"
200 			"-Z,-z --[no]zoom\n\t-R --[no]rotate\n\t-G,-g --[no]grid\n\t-A,-a --[no]antialias\n\t"
201 			"-t --turnrate 0.1-1.0\n\t"
202 			"-r --rating RATING\t\t1: harmless... 4: average... 9: elite\n\t"
203 			"--gamma GAMMA\n\t"
204 #ifdef SOUND
205 			"-q --nosound\n\t"
206 #endif
207 			"-p --speed 0-2\n\n\t"
208 			"-d --debug\n\t-i --invulnerable\t\timplies --debug\n\t-M --stopmotion\t\t\timplies --debug\n\n\t"
209 			"-V --version\n\t-h --help\n");
210 		exit(0);
211 		break;
212 
213 	    case '?':
214 		exit(1);
215 		break;
216 
217 	    default:
218 		printf("getopt returned unexpected character %c\n", c);
219 	}
220     }
221 }
222 #endif
223 
224 // lexicographic comparison, widthfirst
cmp(const Rect & r1,const Rect & r2)225 int Rect::cmp(const Rect& r1, const Rect& r2)
226 {
227     if (r1.w > r2.w)
228 	return 1;
229     if (r1.w < r2.w)
230 	return -1;
231     if (r1.h > r2.h)
232 	return 1;
233     if (r1.h < r2.h)
234 	return -1;
235     return 0;
236 }
237 
getSDLModes()238 std::vector<Rect> getSDLModes()
239 {
240     SDL_Rect** SDLModes = SDL_ListModes(NULL, settings.videoFlags);
241     std::vector<Rect> modes;
242 
243     if (SDLModes == NULL)
244 	return modes;
245     if (SDLModes == (SDL_Rect**)(-1))
246     {
247 	// "any mode allowed" - return some standard modes
248 	modes.push_back(Rect(640, 480));
249 	modes.push_back(Rect(800, 600));
250 	modes.push_back(Rect(1024, 768));
251 	modes.push_back(Rect(1280, 1024));
252 	return modes;
253     }
254 
255     for (int i=0; SDLModes[i]; i++)
256 	modes.push_back(*SDLModes[i]);
257     return modes;
258 }
259