1 /*
2 Project: Starfighter
3 Copyright (C) 2003 Parallel Realities
4 Copyright (C) 2011, 2012 Guus Sliepen
5 Copyright (C) 2015-2020 The Diligent Circle <diligentcircle@riseup.net>
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 3
10 of the License, or (at your option) any later version.
11 
12 This program 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.
16 
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/>.
19 */
20 
21 #include <libintl.h>
22 #include <locale.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <time.h>
27 #include <unistd.h>
28 
29 #ifdef __APPLE__
30 #include "CoreFoundation/CoreFoundation.h"
31 #endif
32 
33 #include "SDL.h"
34 
35 #ifndef NOSOUND
36 #include "SDL_mixer.h"
37 #endif
38 
39 #include "colors.h"
40 #include "defs.h"
41 #include "structs.h"
42 
43 #include "alien.h"
44 #include "audio.h"
45 #include "cutscene.h"
46 #include "engine.h"
47 #include "game.h"
48 #include "gfx.h"
49 #include "intermission.h"
50 #include "renderer.h"
51 #include "screen.h"
52 #include "title.h"
53 #include "weapons.h"
54 
main(int argc,char ** argv)55 int main(int argc, char **argv)
56 {
57 	int cheatAttempt;
58 	int cheatCount;
59 	int section;
60 
61 #ifdef __APPLE__
62 	// This makes relative paths work in Xcode by changing directory to the Resources folder inside the .app bundle
63 	CFBundleRef mainBundle = CFBundleGetMainBundle();
64 	CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
65 	char path[PATH_MAX];
66 	if (CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
67 	{
68 		chdir(path);
69 		printf("Current directory \"%s\"\n", path);
70 	}
71 	else
72 	{
73 		if (chdir(DATADIR) == -1)
74 			printf("Warning: failed to change directory to \"%s\"\n", DATADIR);
75 	}
76 #else
77 	if (chdir(DATADIR) == -1)
78 		printf("Warning: failed to change directory to \"%s\"\n", DATADIR);
79 #endif
80 
81 	engine_init(); // Must do this first!
82 
83 	cheatAttempt = 0;
84 	cheatCount = 0;
85 
86 	if (argc > 1)
87 	{
88 		if (strcmp("--help", argv[1]) == 0)
89 		{
90 			printf("\nProject: Starfighter %s\n", VERSION);
91 			printf("Copyright Parallel Realities 2003\n");
92 			printf("Copyright Guus Sliepen, Astrid S. de Wijn and others 2012\n");
93 			printf("Additional Commands\n");
94 			printf("\t-noaudio     Disables sound and music\n");
95 			printf("\t-mono        Mono sound output (best for headphones)\n\n");
96 			printf("https://pr-starfighter.github.io\n");
97 			printf("\n");
98 			exit(0);
99 		}
100 	}
101 
102 	for (int i = 1 ; i < argc ; i++)
103 	{
104 		if (strcmp(argv[i], "-cheat") == 0)
105 			cheatAttempt = 1;
106 		if (strcmp(argv[i], "-noaudio") == 0)
107 		{
108 			printf("No Audio\n");
109 			engine.useAudio = 0;
110 		}
111 		if ((strcmp(argv[i], "humans") == 0) && (cheatCount == 0))
112 			cheatCount = 1;
113 		if ((strcmp(argv[i], "do") == 0) && (cheatCount == 1))
114 			cheatCount = 2;
115 		if ((strcmp(argv[i], "it") == 0) && (cheatCount == 2))
116 			cheatCount = 3;
117 		if (((strcmp(argv[i], "better") == 0) && (cheatCount == 3))
118 				|| (strcmp(argv[i], "humansdoitbetter") == 0))
119 		{
120 			printf("Humans do it better! Cheats enabled.\n");
121 			engine.cheat = 1;
122 		}
123 	}
124 
125 	atexit(engine_cleanup);
126 
127 	gfx_init();
128 	engine_setMode(); // Settings get loaded here
129 	gfx_loadFont();
130 
131 	if ((strcmp(engine.lang, "default") == 0)
132 			|| (strcmp(engine.lang, "") == 0))
133 		setlocale(LC_ALL, "");
134 	else
135 		setlocale(LC_ALL, engine.lang);
136 
137 	bindtextdomain("pr-starfighter", "./locale/");
138 	textdomain("pr-starfighter");
139 
140 	if (cheatAttempt && !engine.cheat)
141 	{
142 		screen_clear(black);
143 		screen_renderString("That doesn't work anymore", -1, 285, FONT_WHITE);
144 		screen_renderString("Try harder...", -1, 315, FONT_WHITE);
145 		renderer_update();
146 		SDL_Delay(2000);
147 		screen_clear(black);
148 		renderer_update();
149 		SDL_Delay(500);
150 	}
151 
152 	gfx_free();
153 	audio_loadSounds();
154 
155 	weapons_init();
156 
157 	srand(time(NULL));
158 
159 #ifndef NOSOUND
160 	if (engine.useAudio)
161 	{
162 		Mix_Volume(-1, 100);
163 		Mix_VolumeMusic(engine.musicVolume);
164 	}
165 #endif
166 
167 	alien_defs_init();
168 
169 	colors_init();
170 
171 	// Determine which part of the game we will go to...
172 	section = 0;
173 
174 	game.difficulty = DIFFICULTY_EASY;
175 	game_init();
176 
177 	while (1)
178 	{
179 		switch (section)
180 		{
181 			case 0:
182 				section = title_show();
183 				break;
184 
185 			case 1:
186 				section = intermission();
187 				break;
188 
189 			case 2:
190 				if (game.stationedPlanet == -1)
191 					cutscene_init(0);
192 				section = game_mainLoop();
193 				break;
194 		}
195 	}
196 
197 	return(0);
198 }
199