Annotation of brogue-ce/src/platform/main.c, Revision 1.2
1.1 rubenllo 1: #include <math.h>
2: #include <limits.h>
3: #include "platform.h"
1.2 ! rubenllo 4: #if defined(__OpenBSD__)
! 5: #include <unistd.h> // For pledge() and unveil()
! 6: #endif
! 7:
1.1 rubenllo 8:
9: // Expanding a macro as a string constant requires two levels of macros
10: #define _str(x) #x
11: #define STRINGIFY(x) _str(x)
12:
13: struct brogueConsole currentConsole;
14:
15: char dataDirectory[BROGUE_FILENAME_MAX] = STRINGIFY(DATADIR);
16: boolean serverMode = false;
17: boolean hasGraphics = false;
18: boolean graphicsEnabled = false;
19: boolean isCsvFormat = false;
20:
21: static void printCommandlineHelp() {
22: printf("%s",
23: "--help -h print this help message\n"
24: "--version -V print the version (i.e., " BROGUE_VERSION_STRING ")\n"
25: "--scores dump scores to output and exit immediately\n"
26: "-n start a new game, skipping the menu\n"
27: "-s seed start a new game with the specified numerical seed\n"
28: "-o filename[.broguesave] open a save file (extension optional)\n"
29: "-v recording[.broguerec] view a recording (extension optional)\n"
30: #ifdef BROGUE_WEB
31: "--server-mode run the game in web-brogue server mode\n"
32: #endif
33: #ifdef BROGUE_SDL
34: "--size N starts the game at font size N (1 to 20)\n"
35: "--graphics -G enable graphical tiles\n"
36: "--full-screen -F enable full screen\n"
37: "--no-gpu disable hardware-accelerated graphics and HiDPI\n"
38: #endif
39: #ifdef BROGUE_CURSES
40: "--term -t run in ncurses-based terminal mode\n"
41: #endif
42: "--stealth -S display stealth range\n"
43: "--no-effects -E disable color effects\n"
44: "--wizard -W run in wizard mode, invincible with powerful items\n"
45: "[--csv] --print-seed-catalog [START NUM LEVELS]\n"
46: " (optional csv format)\n"
47: " prints a catalog of the first LEVELS levels of NUM\n"
48: " seeds from seed START (defaults: 1 1000 5)\n"
49: );
50: return;
51: }
52:
53: static void badArgument(const char *arg) {
54: printf("Bad argument: %s\n\n", arg);
55: printCommandlineHelp();
56: }
57:
58: int main(int argc, char *argv[])
59: {
60:
61: #if 0
62: #define TOD(x) ((double) (x) / FP_FACTOR)
63: fixpt y, x1 = 1, x2 = FP_FACTOR * 70 / 100;
64: for (int i=0; i < 10; i++) {
65: y = fp_pow(x2, x1); printf("%.5f ^ %i = %.5f (%lli)\n", TOD(x2), x1, TOD(y), y);
66: // y = fp_sqrt(x1); printf("sqrt(%.5f) = %.5f (%lli)\n", TOD(x1), TOD(y), y);
67: x1 += 1;
68: }
69: exit(0);
70: #endif
71:
72: #ifdef BROGUE_SDL
73: currentConsole = sdlConsole;
74: #elif BROGUE_WEB
75: currentConsole = webConsole;
76: #elif BROGUE_CURSES
77: currentConsole = cursesConsole;
78: #endif
79:
80: rogue.nextGame = NG_NOTHING;
81: rogue.nextGamePath[0] = '\0';
82: rogue.nextGameSeed = 0;
83: rogue.wizard = false;
84: rogue.displayAggroRangeMode = false;
85: rogue.trueColorMode = false;
86:
87: boolean initialGraphics = false;
88:
89: int i;
90: for (i = 1; i < argc; i++) {
1.2 ! rubenllo 91: #if defined(__OpenBSD__)
! 92: if (strcmp(argv[i], "--box") == 0) {
! 93: if (i + 1 < argc) {
! 94: if ( unveil("/usr","r")==-1 ) {
! 95: perror("Unveil failure. Aborting.");
! 96: exit (1);
! 97: }
! 98: if ( unveil(argv[i + 1],"rwc")==-1 ) {
! 99: perror("Unveil failure. Aborting.");
! 100: exit(1);
! 101: }
! 102: if ( chdir(argv[i+1])==-1 ) {
! 103: perror("Could not change directory.");
! 104: exit(1);
! 105: }
! 106: if ( pledge("stdio rpath wpath cpath fattr flock "
! 107: "getpw id tty proc", NULL )==-1 ) {
! 108: perror("Pledge failure. Aborting.");
! 109: exit (1);
! 110: }
! 111: i++;
! 112: continue;
! 113: }
! 114: }
! 115: #endif // OpenBSD
1.1 rubenllo 116: if (strcmp(argv[i], "--scores") == 0) {
117: // just dump the scores and quit!
118: dumpScores();
119: return 0;
120: }
121:
122: if (strcmp(argv[i], "--seed") == 0 || strcmp(argv[i], "-s") == 0) {
123: // pick a seed!
124: if (i + 1 < argc) {
125: unsigned int seed = atof(argv[i + 1]); // plenty of precision in a double, and simpler than any other option
126: if (seed != 0) {
127: i++;
128: rogue.nextGameSeed = seed;
129: rogue.nextGame = NG_NEW_GAME_WITH_SEED;
130: continue;
131: }
132: }
133: }
134:
135: if (strcmp(argv[i], "-n") == 0) {
136: if (rogue.nextGameSeed == 0) {
137: rogue.nextGame = NG_NEW_GAME;
138: } else {
139: rogue.nextGame = NG_NEW_GAME_WITH_SEED;
140: }
141: continue;
142: }
143:
144: if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--open") == 0) {
145: if (i + 1 < argc) {
146: strncpy(rogue.nextGamePath, argv[i + 1], BROGUE_FILENAME_MAX);
147: rogue.nextGamePath[BROGUE_FILENAME_MAX - 1] = '\0';
148: rogue.nextGame = NG_OPEN_GAME;
149:
150: if (!endswith(rogue.nextGamePath, GAME_SUFFIX)) {
151: append(rogue.nextGamePath, GAME_SUFFIX, BROGUE_FILENAME_MAX);
152: }
153:
154: i++;
155: continue;
156: }
157: }
158:
159: if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--view") == 0) {
160: if (i + 1 < argc) {
161: strncpy(rogue.nextGamePath, argv[i + 1], BROGUE_FILENAME_MAX);
162: rogue.nextGamePath[BROGUE_FILENAME_MAX - 1] = '\0';
163: rogue.nextGame = NG_VIEW_RECORDING;
164:
165: if (!endswith(rogue.nextGamePath, RECORDING_SUFFIX)) {
166: append(rogue.nextGamePath, RECORDING_SUFFIX, BROGUE_FILENAME_MAX);
167: }
168:
169: i++;
170: continue;
171: }
172: }
173:
174: if (strcmp(argv[i], "--print-seed-catalog") == 0) {
175: if (i + 3 < argc) {
176: // Use convertions from types the next size up, because they're signed
177: unsigned long startingSeed = atof(argv[i + 1]);
178: unsigned long numberOfSeeds = atoll(argv[i + 2]);
179: unsigned int numberOfLevels = atol(argv[i + 3]);
180:
181: if (startingSeed > 0 && numberOfLevels <= 40) {
182: printSeedCatalog(startingSeed, numberOfSeeds, numberOfLevels, isCsvFormat);
183: return 0;
184: }
185: } else {
186: printSeedCatalog(1, 1000, 5, isCsvFormat);
187: return 0;
188: }
189: }
190:
191: if (strcmp(argv[i], "-V") == 0 || strcmp(argv[i], "--version") == 0) {
192: printf("%s\n", BROGUE_VERSION_STRING);
193: return 0;
194: }
195:
196: if (!(strcmp(argv[i], "-?") && strcmp(argv[i], "-h") && strcmp(argv[i], "--help"))) {
197: printCommandlineHelp();
198: return 0;
199: }
200:
201: if (strcmp(argv[i], "-G") == 0 || strcmp(argv[i], "--graphics") == 0) {
202: initialGraphics = true; // we call setGraphicsEnabled later
203: continue;
204: }
205:
206: if (strcmp(argv[i], "--csv") == 0 ) {
207: isCsvFormat = true; // we call printSeedCatalog later
208: continue;
209: }
210:
211: #ifdef BROGUE_SDL
212: if (strcmp(argv[i], "--size") == 0) {
213: if (i + 1 < argc) {
214: int size = atoi(argv[i + 1]);
215: if (size > 0 && size <= 20) {
216: windowWidth = round(pow(1.1, size) * 620.);
217: // Height set automatically
218: };
219:
220: i++;
221: continue;
222: }
223: }
224:
225: if (strcmp(argv[i], "-F") == 0 || strcmp(argv[i], "--full-screen") == 0) {
226: fullScreen = true;
227: continue;
228: }
229:
230: if (strcmp(argv[i], "--no-gpu") == 0) {
231: softwareRendering = true;
232: continue;
233: }
234: #endif
235:
236: #ifdef BROGUE_CURSES
237: if (strcmp(argv[i], "--term") == 0 || strcmp(argv[i], "-t") == 0) {
238: currentConsole = cursesConsole;
239: continue;
240: }
241: #endif
242:
243: #ifdef BROGUE_WEB
244: if(strcmp(argv[i], "--server-mode") == 0) {
245: currentConsole = webConsole;
246: rogue.nextGame = NG_NEW_GAME;
247: serverMode = true;
248: continue;
249: }
250: #endif
251:
252: if (strcmp(argv[i], "--stealth") == 0 || strcmp(argv[i], "-S") == 0) {
253: rogue.displayAggroRangeMode = true;
254: continue;
255: }
256:
257: if (strcmp(argv[i], "--no-effects") == 0 || strcmp(argv[i], "-E") == 0) {
258: rogue.trueColorMode = true;
259: continue;
260: }
261:
262: if (strcmp(argv[i], "--wizard") == 0 || strcmp(argv[i], "-W") == 0) {
263: rogue.wizard = true;
264: continue;
265: }
266:
267: // maybe it ends with .broguesave or .broguerec, then?
268: if (endswith(argv[i], GAME_SUFFIX)) {
269: strncpy(rogue.nextGamePath, argv[i], BROGUE_FILENAME_MAX);
270: rogue.nextGamePath[BROGUE_FILENAME_MAX - 1] = '\0';
271: rogue.nextGame = NG_OPEN_GAME;
272: continue;
273: }
274:
275: if (endswith(argv[i], RECORDING_SUFFIX)) {
276: strncpy(rogue.nextGamePath, argv[i], BROGUE_FILENAME_MAX);
277: rogue.nextGamePath[BROGUE_FILENAME_MAX - 1] = '\0';
278: rogue.nextGame = NG_VIEW_RECORDING;
279: continue;
280: }
281:
282: badArgument(argv[i]);
283: return 1;
284: }
285:
286: hasGraphics = (currentConsole.setGraphicsEnabled != NULL);
287: // Now actually set graphics. We do this to ensure there is exactly one
288: // call, whether true or false
289: graphicsEnabled = setGraphicsEnabled(initialGraphics);
290:
291: loadKeymap();
292: currentConsole.gameLoop();
293:
294: return 0;
295: }
296:
CVSweb