/** * Load the introduction game. * @param load_newgrfs Whether to load the NewGRFs or not. */ static void LoadIntroGame(bool load_newgrfs = true) { _game_mode = GM_MENU; if (load_newgrfs) ResetGRFConfig(false); /* Setup main window */ ResetWindowSystem(); SetupColoursAndInitialWindow(); /* Load the default opening screen savegame */ if (SaveOrLoad("opntitle.dat", SLO_LOAD, DFT_GAME_FILE, BASESET_DIR) != SL_OK) { GenerateWorld(GWM_EMPTY, 64, 64); // if failed loading, make empty world. WaitTillGeneratedWorld(); SetLocalCompany(COMPANY_SPECTATOR); } else { SetLocalCompany(COMPANY_FIRST); } _pause_mode = PM_UNPAUSED; _cursor.fix_at = false; CheckForMissingGlyphs(); MusicLoop(); // ensure music is correct }
/** * Load the introduction game. * @param load_newgrfs Whether to load the NewGRFs or not. */ static void LoadIntroGame(bool load_newgrfs = true) { _game_mode = GM_MENU; if (load_newgrfs) ResetGRFConfig(false); /* Setup main window */ ResetWindowSystem(); SetupColoursAndInitialWindow(); /* Load the default opening screen savegame */ if (SaveOrLoad("opntitle.dat", SL_LOAD, BASESET_DIR) != SL_OK) { GenerateWorld(GWM_EMPTY, 64, 64); // if failed loading, make empty world. WaitTillGeneratedWorld(); SetLocalCompany(COMPANY_SPECTATOR); } else { SetLocalCompany(COMPANY_FIRST); } _pause_mode = PM_UNPAUSED; _cursor.fix_at = false; if (load_newgrfs) CheckForMissingSprites(); CheckForMissingGlyphs(); /* Play main theme */ if (MusicDriver::GetInstance()->IsSongPlaying()) ResetMusic(); }
/** * Handle all procedures for bootstrapping OpenTTD without a base graphics set. * This requires all kinds of trickery that is needed to avoid the use of * sprites from the base graphics set which are pretty interwoven. * @return True if a base set exists, otherwise false. */ bool HandleBootstrap() { if (BaseGraphics::GetUsedSet() != NULL) return true; /* No user interface, bail out with an error. */ if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0) goto failure; /* If there is no network or no freetype, then there is nothing we can do. Go straight to failure. */ #if defined(ENABLE_NETWORK) && defined(WITH_FREETYPE) && !defined(__APPLE__) && (defined(WITH_FONTCONFIG) || defined(WIN32)) if (!_network_available) goto failure; /* First tell the game we're bootstrapping. */ _game_mode = GM_BOOTSTRAP; /* Initialise the freetype font code. */ InitializeUnicodeGlyphMap(); /* Next "force" finding a suitable freetype font as the local font is missing. */ CheckForMissingGlyphs(false); /* Initialise the palette. The biggest step is 'faking' some recolour sprites. * This way the mauve and gray colours work and we can show the user interface. */ GfxInitPalettes(); static const int offsets[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0, 0, 0x04, 0x08 }; for (uint i = 0; i != 16; i++) { for (int j = 0; j < 8; j++) { _colour_gradient[i][j] = offsets[i] + j; } } /* Finally ask the question. */ new BootstrapBackground(); new BootstrapAskForDownloadWindow(); /* Process the user events. */ _video_driver->MainLoop(); /* _exit_game is used to get out of the video driver's main loop. * In case GM_BOOTSTRAP is still set we did not exit it via the * "download complete" event, so it was a manual exit. Obey it. */ _exit_game = _game_mode == GM_BOOTSTRAP; if (_exit_game) return false; /* Try to probe the graphics. Should work this time. */ if (!BaseGraphics::SetSet(NULL)) goto failure; /* Finally we can continue heading for the menu. */ _game_mode = GM_MENU; return true; #endif /* Failure to get enough working to get a graphics set. */ failure: usererror("Failed to find a graphics set. Please acquire a graphics set for OpenTTD. See section 4.1 of readme.txt."); return false; }
/** * Main entry point for this lovely game. * @param argc The number of arguments passed to this game. * @param argv The values of the arguments. * @return 0 when there is no error. */ int openttd_main(int argc, char *argv[]) { char *musicdriver = NULL; char *sounddriver = NULL; char *videodriver = NULL; char *blitter = NULL; char *graphics_set = NULL; char *sounds_set = NULL; char *music_set = NULL; Dimension resolution = {0, 0}; /* AfterNewGRFScan sets save_config to true after scanning completed. */ bool save_config = false; AfterNewGRFScan *scanner = new AfterNewGRFScan(&save_config); #if defined(ENABLE_NETWORK) bool dedicated = false; char *debuglog_conn = NULL; extern bool _dedicated_forks; _dedicated_forks = false; #endif /* ENABLE_NETWORK */ _game_mode = GM_MENU; _switch_mode = SM_MENU; _config_file = NULL; GetOptData mgo(argc - 1, argv + 1, _options); int ret = 0; int i; while ((i = mgo.GetOpt()) != -1) { switch (i) { case 'I': free(graphics_set); graphics_set = stredup(mgo.opt); break; case 'S': free(sounds_set); sounds_set = stredup(mgo.opt); break; case 'M': free(music_set); music_set = stredup(mgo.opt); break; case 'm': free(musicdriver); musicdriver = stredup(mgo.opt); break; case 's': free(sounddriver); sounddriver = stredup(mgo.opt); break; case 'v': free(videodriver); videodriver = stredup(mgo.opt); break; case 'b': free(blitter); blitter = stredup(mgo.opt); break; #if defined(ENABLE_NETWORK) case 'D': free(musicdriver); free(sounddriver); free(videodriver); free(blitter); musicdriver = stredup("null"); sounddriver = stredup("null"); videodriver = stredup("dedicated"); blitter = stredup("null"); dedicated = true; SetDebugString("net=6"); if (mgo.opt != NULL) { /* Use the existing method for parsing (openttd -n). * However, we do ignore the #company part. */ const char *temp = NULL; const char *port = NULL; ParseConnectionString(&temp, &port, mgo.opt); if (!StrEmpty(mgo.opt)) scanner->dedicated_host = mgo.opt; if (port != NULL) scanner->dedicated_port = atoi(port); } break; case 'f': _dedicated_forks = true; break; case 'n': scanner->network_conn = mgo.opt; // optional IP parameter, NULL if unset break; case 'l': debuglog_conn = mgo.opt; break; case 'p': scanner->join_server_password = mgo.opt; break; case 'P': scanner->join_company_password = mgo.opt; break; #endif /* ENABLE_NETWORK */ case 'r': ParseResolution(&resolution, mgo.opt); break; case 't': scanner->startyear = atoi(mgo.opt); break; case 'd': { #if defined(WIN32) CreateConsole(); #endif if (mgo.opt != NULL) SetDebugString(mgo.opt); break; } case 'e': _switch_mode = (_switch_mode == SM_LOAD_GAME || _switch_mode == SM_LOAD_SCENARIO ? SM_LOAD_SCENARIO : SM_EDITOR); break; case 'g': if (mgo.opt != NULL) { _file_to_saveload.SetName(mgo.opt); bool is_scenario = _switch_mode == SM_EDITOR || _switch_mode == SM_LOAD_SCENARIO; _switch_mode = is_scenario ? SM_LOAD_SCENARIO : SM_LOAD_GAME; _file_to_saveload.SetMode(SLO_LOAD, is_scenario ? FT_SCENARIO : FT_SAVEGAME, DFT_GAME_FILE); /* if the file doesn't exist or it is not a valid savegame, let the saveload code show an error */ const char *t = strrchr(_file_to_saveload.name, '.'); if (t != NULL) { FiosType ft = FiosGetSavegameListCallback(SLO_LOAD, _file_to_saveload.name, t, NULL, NULL); if (ft != FIOS_TYPE_INVALID) _file_to_saveload.SetMode(ft); } break; } _switch_mode = SM_NEWGAME; /* Give a random map if no seed has been given */ if (scanner->generation_seed == GENERATE_NEW_SEED) { scanner->generation_seed = InteractiveRandom(); } break; case 'q': { DeterminePaths(argv[0]); if (StrEmpty(mgo.opt)) { ret = 1; goto exit_noshutdown; } char title[80]; title[0] = '\0'; FiosGetSavegameListCallback(SLO_LOAD, mgo.opt, strrchr(mgo.opt, '.'), title, lastof(title)); _load_check_data.Clear(); SaveOrLoadResult res = SaveOrLoad(mgo.opt, SLO_CHECK, DFT_GAME_FILE, SAVE_DIR, false); if (res != SL_OK || _load_check_data.HasErrors()) { fprintf(stderr, "Failed to open savegame\n"); if (_load_check_data.HasErrors()) { char buf[256]; SetDParamStr(0, _load_check_data.error_data); GetString(buf, _load_check_data.error, lastof(buf)); fprintf(stderr, "%s\n", buf); } goto exit_noshutdown; } WriteSavegameInfo(title); goto exit_noshutdown; } case 'G': scanner->generation_seed = atoi(mgo.opt); break; case 'c': free(_config_file); _config_file = stredup(mgo.opt); break; case 'x': scanner->save_config = false; break; case 'h': i = -2; // Force printing of help. break; } if (i == -2) break; } if (i == -2 || mgo.numleft > 0) { /* Either the user typed '-h', he made an error, or he added unrecognized command line arguments. * In all cases, print the help, and exit. * * The next two functions are needed to list the graphics sets. We can't do them earlier * because then we cannot show it on the debug console as that hasn't been configured yet. */ DeterminePaths(argv[0]); TarScanner::DoScan(TarScanner::BASESET); BaseGraphics::FindSets(); BaseSounds::FindSets(); BaseMusic::FindSets(); ShowHelp(); goto exit_noshutdown; } DeterminePaths(argv[0]); TarScanner::DoScan(TarScanner::BASESET); #if defined(ENABLE_NETWORK) if (dedicated) DEBUG(net, 0, "Starting dedicated version %s", _openttd_revision); if (_dedicated_forks && !dedicated) _dedicated_forks = false; #if defined(UNIX) && !defined(__MORPHOS__) /* We must fork here, or we'll end up without some resources we need (like sockets) */ if (_dedicated_forks) DedicatedFork(); #endif #endif LoadFromConfig(true); if (resolution.width != 0) _cur_resolution = resolution; /* * The width and height must be at least 1 pixel and width times * height times bytes per pixel must still fit within a 32 bits * integer, even for 32 bpp video modes. This way all internal * drawing routines work correctly. */ _cur_resolution.width = ClampU(_cur_resolution.width, 1, UINT16_MAX / 2); _cur_resolution.height = ClampU(_cur_resolution.height, 1, UINT16_MAX / 2); /* Assume the cursor starts within the game as not all video drivers * get an event that the cursor is within the window when it is opened. * Saying the cursor is there makes no visible difference as it would * just be out of the bounds of the window. */ _cursor.in_window = true; /* enumerate language files */ InitializeLanguagePacks(); /* Initialize the regular font for FreeType */ InitFreeType(false); /* This must be done early, since functions use the SetWindowDirty* calls */ InitWindowSystem(); BaseGraphics::FindSets(); if (graphics_set == NULL && BaseGraphics::ini_set != NULL) graphics_set = stredup(BaseGraphics::ini_set); if (!BaseGraphics::SetSet(graphics_set)) { if (!StrEmpty(graphics_set)) { BaseGraphics::SetSet(NULL); ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_BASE_GRAPHICS_NOT_FOUND); msg.SetDParamStr(0, graphics_set); ScheduleErrorMessage(msg); } } free(graphics_set); /* Initialize game palette */ GfxInitPalettes(); DEBUG(misc, 1, "Loading blitter..."); if (blitter == NULL && _ini_blitter != NULL) blitter = stredup(_ini_blitter); _blitter_autodetected = StrEmpty(blitter); /* Activate the initial blitter. * This is only some initial guess, after NewGRFs have been loaded SwitchNewGRFBlitter may switch to a different one. * - Never guess anything, if the user specified a blitter. (_blitter_autodetected) * - Use 32bpp blitter if baseset or 8bpp-support settings says so. * - Use 8bpp blitter otherwise. */ if (!_blitter_autodetected || (_support8bpp != S8BPP_NONE && (BaseGraphics::GetUsedSet() == NULL || BaseGraphics::GetUsedSet()->blitter == BLT_8BPP)) || BlitterFactory::SelectBlitter("32bpp-anim") == NULL) { if (BlitterFactory::SelectBlitter(blitter) == NULL) { StrEmpty(blitter) ? usererror("Failed to autoprobe blitter") : usererror("Failed to select requested blitter '%s'; does it exist?", blitter); } } free(blitter); if (videodriver == NULL && _ini_videodriver != NULL) videodriver = stredup(_ini_videodriver); DriverFactoryBase::SelectDriver(videodriver, Driver::DT_VIDEO); free(videodriver); InitializeSpriteSorter(); /* Initialize the zoom level of the screen to normal */ _screen.zoom = ZOOM_LVL_NORMAL; NetworkStartUp(); // initialize network-core #if defined(ENABLE_NETWORK) if (debuglog_conn != NULL && _network_available) { const char *not_used = NULL; const char *port = NULL; uint16 rport; rport = NETWORK_DEFAULT_DEBUGLOG_PORT; ParseConnectionString(¬_used, &port, debuglog_conn); if (port != NULL) rport = atoi(port); NetworkStartDebugLog(NetworkAddress(debuglog_conn, rport)); } #endif /* ENABLE_NETWORK */ if (!HandleBootstrap()) { ShutdownGame(); goto exit_bootstrap; } VideoDriver::GetInstance()->ClaimMousePointer(); /* initialize screenshot formats */ InitializeScreenshotFormats(); BaseSounds::FindSets(); if (sounds_set == NULL && BaseSounds::ini_set != NULL) sounds_set = stredup(BaseSounds::ini_set); if (!BaseSounds::SetSet(sounds_set)) { if (StrEmpty(sounds_set) || !BaseSounds::SetSet(NULL)) { usererror("Failed to find a sounds set. Please acquire a sounds set for OpenTTD. See section 4.1 of README.md."); } else { ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_BASE_SOUNDS_NOT_FOUND); msg.SetDParamStr(0, sounds_set); ScheduleErrorMessage(msg); } } free(sounds_set); BaseMusic::FindSets(); if (music_set == NULL && BaseMusic::ini_set != NULL) music_set = stredup(BaseMusic::ini_set); if (!BaseMusic::SetSet(music_set)) { if (StrEmpty(music_set) || !BaseMusic::SetSet(NULL)) { usererror("Failed to find a music set. Please acquire a music set for OpenTTD. See section 4.1 of README.md."); } else { ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_BASE_MUSIC_NOT_FOUND); msg.SetDParamStr(0, music_set); ScheduleErrorMessage(msg); } } free(music_set); if (sounddriver == NULL && _ini_sounddriver != NULL) sounddriver = stredup(_ini_sounddriver); DriverFactoryBase::SelectDriver(sounddriver, Driver::DT_SOUND); free(sounddriver); if (musicdriver == NULL && _ini_musicdriver != NULL) musicdriver = stredup(_ini_musicdriver); DriverFactoryBase::SelectDriver(musicdriver, Driver::DT_MUSIC); free(musicdriver); /* Take our initial lock on whatever we might want to do! */ _modal_progress_paint_mutex->BeginCritical(); _modal_progress_work_mutex->BeginCritical(); GenerateWorld(GWM_EMPTY, 64, 64); // Make the viewport initialization happy WaitTillGeneratedWorld(); LoadIntroGame(false); CheckForMissingGlyphs(); /* ScanNewGRFFiles now has control over the scanner. */ ScanNewGRFFiles(scanner); scanner = NULL; VideoDriver::GetInstance()->MainLoop(); WaitTillSaved(); /* only save config if we have to */ if (save_config) { SaveToConfig(); SaveHotkeysToConfig(); WindowDesc::SaveToConfig(); SaveToHighScore(); } /* Reset windowing system, stop drivers, free used memory, ... */ ShutdownGame(); goto exit_normal; exit_noshutdown: /* These three are normally freed before bootstrap. */ free(graphics_set); free(videodriver); free(blitter); exit_bootstrap: /* These are normally freed before exit, but after bootstrap. */ free(sounds_set); free(music_set); free(musicdriver); free(sounddriver); exit_normal: free(BaseGraphics::ini_set); free(BaseSounds::ini_set); free(BaseMusic::ini_set); free(_ini_musicdriver); free(_ini_sounddriver); free(_ini_videodriver); free(_ini_blitter); delete scanner; #ifdef ENABLE_NETWORK extern FILE *_log_fd; if (_log_fd != NULL) { fclose(_log_fd); } #endif /* ENABLE_NETWORK */ return ret; }