1 // nazghul - an old-school RPG engine
2 // Copyright (C) 2002, 2003 Gordon McNutt
4 // This program is free software; you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License as published by the Free
6 // Software Foundation; either version 2 of the License, or (at your option)
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 // You should have received a copy of the GNU General Public License along with
15 // this program; if not, write to the Free Foundation, Inc., 59 Temple Place,
16 // Suite 330, Boston, MA 02111-1307 USA
19 // gmcnutt@users.sourceforge.net
21 #include "../config.h"
23 #include "constants.h"
25 #include "dimensions.h"
37 #include "formation.h"
56 #include <SDL_image.h>
57 #include <SDL_thread.h>
61 /* Name of the file to load the game from. */
62 static char *nazghul_load_fname = 0;
64 int FullScreenMode = 0;
65 int DeveloperMode = 0;
68 static char program_name[] = "nazghul";
70 static void print_version(void)
72 printf("%s %s\n", program_name, PACKAGE_VERSION);
73 printf("Copyright (C) 2003 Gordon McNutt, Sam Glasby\n"
74 "%s comes with NO WARRANTY,\n"
75 "to the extent permitted by law.\n"
76 "You may redistribute copies of %s\n"
77 "under the terms of the GNU General Public License.\n"
78 "For more information about these matters,\n"
79 "see the file named COPYING.\n",
80 program_name, program_name
84 static void print_usage(void)
86 printf("Usage: %s [options] <load-file>\n"
90 " -d: developer mode\n"
91 " -f: fullscreen mode\n"
92 " -t: tick <period in msec> \n"
93 " -a: animation <period in ticks> \n"
94 " -s: sound <0 to disable> \n"
95 " -R: recorder <filename> \n"
96 " -P: playback <filename> \n"
97 " -S: speed <playback ms delay> \n"
98 " -I: game data dir\n"
99 " -G: save game dir\n"
100 " -r: screen size <pixels> (eg, 640x480)\n"
101 " -T: show all terrain\n"
106 static void parse_args(int argc, char **argv)
110 TickMilliseconds = MS_PER_TICK;
111 AnimationTicks = ANIMATION_TICKS;
113 while ((c = getopt(argc, argv, "t:a:s:TdfR:S:P:I:G:vhr:")) != -1) {
116 TickMilliseconds = atoi(optarg);
119 AnimationTicks = atoi(optarg);
122 cfg_set("sound-enabled",
123 atoi(optarg) != 0 ? "yes" : "no");
135 /* Set the filename for recording keystrokes. */
136 cfg_set("record-filename", optarg);
139 /* Set the speed to play back recorded keystrokes. */
140 cfg_set("playback-speed", optarg);
143 /* Set the file to play back keystrokes from. */
144 cfg_set("playback-filename", optarg);
147 /* Set the directory for read-only game and cfg
149 cfg_set("include-dirname", optarg);
152 /* Set the directory for read-write game and cfg
154 cfg_set("saved-games-dirname", optarg);
164 /* set the screen dimensions */
165 cfg_set("screen-dims", optarg);
175 // --------------------------------------------------------------------
176 // Any remaining option is assumed to be the save-file to load the game
177 // from. If there is none then abort.
178 // --------------------------------------------------------------------
181 nazghul_load_fname = argv[optind];
186 * This initializes the various submodules.
188 static void nazghul_init_internal_libs(void)
195 struct lib_entry libs[] = {
196 { "commonInit", commonInit },
197 { "screenInit", screenInit },
198 { "asciiInit", asciiInit },
199 { "sprite_init", sprite_init },
200 { "eventInit", eventInit },
201 { "windInit", windInit },
202 { "formation_init", formation_init },
203 { "astar_init", astar_init },
204 { "cmdwin_init", cmdwin_init },
205 { "consoleInit", consoleInit },
206 { "mapInit", mapInit },
207 { "vmask_init", vmask_init },
208 { "combatInit", combatInit },
209 { "statusInit", statusInit },
210 { "foogodInit", foogodInit },
211 { "menu_init", menu_init },
212 { "conv_init", conv_init }
217 for (i = 0; i < array_sz(libs); i++) {
218 if (libs[i].init() < 0) {
219 /* this will not return: */
220 menu_startup_error("Error in %s", libs[i].name);
226 if (!strcmp("yes", cfg_get("sound-enabled"))) {
229 set_music_volume(cfg_get("music-volume"));
234 /* nazghul_splash -- show the splash image */
235 void nazghul_splash(void)
237 static SDL_Surface *splash = 0;
240 /* The first time through load the splash image from a file. */
242 char *dims = cfg_get("screen-dims");
243 const char *suffix = "-splash-image-filename";
248 /* Build a key for the splash image filename based on the
249 * screen dimensions. */
250 key = (char*)malloc(strlen(dims) + strlen(suffix) + 1);
255 /* Lookup the filename. */
256 basename = cfg_get(key);
258 warn("cfg_get: no key matches '%s'", key);
264 /* Look for the splash image, check the include dir first, then
265 * check the current working dir */
266 filename = file_mkpath(cfg_get("include-dirname"), basename);
268 splash = IMG_Load(filename);
272 warn("IMG_Load failed: %s\n", SDL_GetError());
284 /* Fill out the screen destination rect */
285 rect.x = max(0, (MAP_W - splash->w) / 2) + MAP_X;
286 rect.y = max(0, (MAP_H - splash->h) / 2) + MAP_Y;
287 rect.w = min(splash->w, MAP_W);
288 rect.h = min(splash->h, MAP_H);
290 screenBlit(splash, NULL, &rect);
294 /* init_default_cfg -- initialize the global cfg settings to start-up defaults
295 * and prepare it for loading the cfg script */
296 static void init_default_cfg()
299 cfg_set("init-script-filename", "kern-init.scm");
300 cfg_set("options-script-filename", "options.scm");
301 cfg_set("splash-image-filename", "splash.png");
302 cfg_set("screen-dims", "1280x960" /*"640x480"*/);
303 cfg_set("sound-enabled", "yes");
304 cfg_set("music-volume", "100%");
305 cfg_set("keyword-highlighting", "yes");
308 int main(int argc, char **argv)
310 int print_version = 1;
312 /* Initialize the cfg environment before parsing args. */
315 parse_args(argc, argv);
317 /* Load the cfg script after parsing args */
318 if (file_load_from_include_dir(cfg_get("init-script-filename"))) {
319 menu_startup_error("Error loading %s: %s",
320 cfg_get("init-script-filename"),
324 /* Load the options script */
325 if (file_exists_in_save_dir(cfg_get("options-script-filename"))) {
326 if (file_load_from_save_dir
327 (cfg_get("options-script-filename"))) {
328 warn("Could not load options script\n");
332 if (dimensions_init()) {
333 err("dimensions_init() failed\n");
337 nazghul_init_internal_libs();
339 tick_start(TickMilliseconds);
342 /* blank out the whole screen */
346 /* pause animation tick generation */
349 /* Show the splash screen on startup */
352 /* The first time we start print the vesrsion info. */
355 log_banner("^c+bNazghul^c- ^c+G%s ÆüËܸìÈÇ^c-", PACKAGE_VERSION );
358 /* paint the border for the first time */
359 screen_repaint_frame();
361 /* if no load file specified on the command line then run the main
363 if (! nazghul_load_fname)
364 nazghul_load_fname = main_menu();
366 /* Clear out the vmask cache */
369 /* run the game, don't return until the user quits */
370 playRun(nazghul_load_fname);
372 /* cleanup modules that need it */
375 /* FIXME: need to free nazghul_load_fname */
376 /* reset save file so main menu runs */
377 nazghul_load_fname=0;
379 /* ExitProgram is set when the player closes the window; in that case
380 * drop out of the program without showing the main menu again. */
382 /* In the past, memory leaks made this a bad idea. Let's give
383 * it another go and try to work through them if they crop up