Files
OpenRCT2-Web/src/OpenRCT2.cpp
T

361 lines
9.4 KiB
C++
Raw Normal View History

#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
2014-10-09 00:30:22 +01:00
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
2014-10-09 00:30:22 +01:00
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
2014-10-09 00:30:22 +01:00
*****************************************************************************/
#pragma endregion
2014-10-09 00:30:22 +01:00
#include "audio/audio.h"
#include "audio/mixer.h"
#include "config.h"
#include "editor.h"
2015-07-05 12:12:34 +01:00
#include "game.h"
2015-07-18 17:57:42 -06:00
#include "interface/chat.h"
2016-01-29 19:39:31 +00:00
#include "interface/themes.h"
2015-07-04 13:00:32 -06:00
#include "interface/window.h"
2015-07-09 11:00:46 -06:00
#include "interface/viewport.h"
2016-05-13 21:36:07 +01:00
#include "intro.h"
2014-10-09 00:30:22 +01:00
#include "localisation/localisation.h"
2015-05-25 20:36:40 +01:00
#include "network/http.h"
2015-02-12 11:30:57 +00:00
#include "network/network.h"
2016-06-15 14:59:01 +02:00
#include "object_list.h"
2016-12-11 00:12:29 +00:00
#include "OpenRCT2.h"
2016-04-09 23:30:10 +01:00
#include "platform/crash.h"
#include "platform/platform.h"
2016-12-10 19:34:33 +00:00
#include "rct2/interop.h"
2015-07-10 20:29:50 +01:00
#include "ride/ride.h"
2015-06-24 12:22:12 -04:00
#include "title.h"
#include "util/util.h"
#include "version.h"
#include "world/mapgen.h"
2014-10-09 00:30:22 +01:00
2016-12-11 00:08:37 +00:00
#define UPDATE_TIME_MS 25 // (1000 / 40fps) = 25ms
int gExitCode;
2016-09-17 21:07:17 +01:00
2014-10-09 00:30:22 +01:00
int gOpenRCT2StartupAction = STARTUP_ACTION_TITLE;
2015-08-01 16:40:15 +01:00
utf8 gOpenRCT2StartupActionPath[512] = { 0 };
utf8 gExePath[MAX_PATH];
utf8 gCustomUserDataPath[MAX_PATH] = { 0 };
utf8 gCustomOpenrctDataPath[MAX_PATH] = { 0 };
utf8 gCustomRCT2DataPath[MAX_PATH] = { 0 };
utf8 gCustomPassword[MAX_PATH] = { 0 };
2014-10-09 00:30:22 +01:00
2015-05-29 20:45:21 +01:00
// This should probably be changed later and allow a custom selection of things to initialise like SDL_INIT
bool gOpenRCT2Headless = false;
2015-06-13 13:30:50 +01:00
bool gOpenRCT2ShowChangelog;
bool gOpenRCT2SilentBreakpad;
2015-06-13 13:30:50 +01:00
#ifndef DISABLE_NETWORK
// OpenSSL's message digest context used for calculating sprite checksums
EVP_MD_CTX *gHashCTX = NULL;
#endif // DISABLE_NETWORK
2014-10-09 14:31:51 +01:00
/** If set, will end the OpenRCT2 game loop. Intentially private to this module so that the flag can not be set back to 0. */
int _finished;
static void openrct2_loop();
void openrct2_write_full_version_info(utf8 *buffer, size_t bufferSize)
{
utf8 *ch = buffer;
// Name and version
2016-09-25 21:24:29 -05:00
safe_strcpy(ch, OPENRCT2_NAME ", v" OPENRCT2_VERSION, bufferSize - (ch - buffer));
ch = strchr(ch, '\0');
// Build information
if (!str_is_null_or_empty(gGitBranch)) {
2016-09-25 21:24:29 -05:00
snprintf(ch, bufferSize - (ch - buffer), "-%s", gGitBranch);
ch = strchr(ch, '\0');
}
if (!str_is_null_or_empty(gCommitSha1Short)) {
2016-09-25 21:24:29 -05:00
snprintf(ch, bufferSize - (ch - buffer), " build %s", gCommitSha1Short);
ch = strchr(ch, '\0');
}
if (!str_is_null_or_empty(gBuildServer)) {
2016-09-25 21:24:29 -05:00
snprintf(ch, bufferSize - (ch - buffer), " provided by %s", gBuildServer);
ch = strchr(ch, '\0');
}
#if DEBUG
2016-09-25 21:24:29 -05:00
snprintf(ch, bufferSize - (ch - buffer), " (DEBUG)");
#endif
}
static void openrct2_set_exe_path()
{
2016-09-25 21:24:29 -05:00
platform_get_exe_path(gExePath, sizeof(gExePath));
2015-12-01 21:38:35 +01:00
log_verbose("Setting exe path to %s", gExePath);
}
bool openrct2_initialise()
2014-10-09 00:30:22 +01:00
{
utf8 userPath[MAX_PATH];
2015-02-14 02:16:03 +00:00
#ifndef DISABLE_NETWORK
gHashCTX = EVP_MD_CTX_create();
assert(gHashCTX != NULL);
#endif // DISABLE_NETWORK
platform_resolve_openrct_data_path();
platform_resolve_user_data_path();
2016-09-25 21:24:29 -05:00
platform_get_user_directory(userPath, NULL, sizeof(userPath));
2015-02-14 02:16:03 +00:00
if (!platform_ensure_directory_exists(userPath)) {
log_fatal("Could not create user directory (do you have write access to your documents folder?)");
return false;
2015-02-14 02:16:03 +00:00
}
2016-04-09 23:30:10 +01:00
crash_init();
2016-04-07 00:10:12 +02:00
2016-12-10 19:34:33 +00:00
if (!rct2_interop_setup_segment()) {
2015-09-24 19:03:11 +01:00
log_fatal("Unable to load RCT2 data sector");
return false;
2015-09-22 23:36:05 +02:00
}
openrct2_set_exe_path();
2015-02-16 22:47:11 +00:00
config_set_defaults();
if (!config_open_default()) {
if (!config_find_or_browse_install_directory()) {
2016-01-11 22:46:29 +01:00
gConfigGeneral.last_run_version = strndup(OPENRCT2_VERSION, strlen(OPENRCT2_VERSION));
config_save_default();
utf8 path[MAX_PATH];
2016-09-25 21:24:29 -05:00
config_get_default_path(path, sizeof(path));
2016-01-11 22:46:29 +01:00
log_fatal("An RCT2 install directory must be specified! Please edit \"game_path\" in %s.", path);
return false;
2015-02-16 22:47:11 +00:00
}
}
2015-06-13 13:30:50 +01:00
gOpenRCT2ShowChangelog = true;
if (gConfigGeneral.last_run_version != NULL && (strcmp(gConfigGeneral.last_run_version, OPENRCT2_VERSION) == 0))
gOpenRCT2ShowChangelog = false;
2016-01-02 00:08:18 +01:00
gConfigGeneral.last_run_version = strndup(OPENRCT2_VERSION, strlen(OPENRCT2_VERSION));
2015-02-16 22:47:11 +00:00
config_save_default();
// TODO add configuration option to allow multiple instances
2015-02-12 11:30:57 +00:00
// if (!gOpenRCT2Headless && !platform_lock_single_instance()) {
// log_fatal("OpenRCT2 is already running.");
// return false;
// }
if (!rct2_init_directories()) {
return false;
}
if (!rct2_startup_checks()) {
return false;
}
2015-05-29 20:45:21 +01:00
if (!gOpenRCT2Headless) {
audio_init();
2015-11-16 22:39:47 +00:00
audio_populate_devices();
2015-05-29 20:45:21 +01:00
}
2016-06-15 14:59:01 +02:00
2015-11-26 18:28:52 +00:00
if (!language_open(gConfigGeneral.language)) {
log_error("Failed to open configured language...");
if (!language_open(LANGUAGE_ENGLISH_UK)) {
log_fatal("Failed to open fallback language...");
return false;
}
}
2015-05-25 20:36:40 +01:00
http_init();
2014-10-09 00:30:22 +01:00
2016-01-29 19:39:31 +00:00
theme_manager_initialise();
2015-06-24 12:22:12 -04:00
title_sequences_set_default();
title_sequences_load_presets();
2015-05-29 16:42:57 -04:00
2016-12-10 19:34:33 +00:00
rct2_interop_setup_hooks();
if (!rct2_init())
return false;
2015-07-18 17:57:42 -06:00
chat_init();
2016-12-11 12:10:35 +00:00
rct2_copy_original_user_files_over();
return true;
}
2014-10-09 00:30:22 +01:00
/**
* Launches the game, after command line arguments have been parsed and processed.
*/
void openrct2_launch()
{
if (openrct2_initialise()) {
2016-05-13 21:36:07 +01:00
gIntroState = INTRO_STATE_NONE;
if((gOpenRCT2StartupAction == STARTUP_ACTION_TITLE) && gConfigGeneral.play_intro)
gOpenRCT2StartupAction = STARTUP_ACTION_INTRO;
2015-06-03 17:11:19 +01:00
switch (gOpenRCT2StartupAction) {
case STARTUP_ACTION_INTRO:
2016-05-13 21:36:07 +01:00
gIntroState = INTRO_STATE_PUBLISHER_BEGIN;
2016-10-03 19:06:13 +01:00
title_load();
2015-06-03 17:11:19 +01:00
break;
case STARTUP_ACTION_TITLE:
2016-10-03 19:06:13 +01:00
title_load();
2015-06-03 17:11:19 +01:00
break;
case STARTUP_ACTION_OPEN:
assert(gOpenRCT2StartupActionPath != NULL);
2016-03-05 12:20:02 +00:00
if (!rct2_open_file(gOpenRCT2StartupActionPath)) {
2016-10-03 19:06:13 +01:00
fprintf(stderr, "Failed to load '%s'", gOpenRCT2StartupActionPath);
title_load();
break;
2016-03-05 12:20:02 +00:00
}
2014-10-09 00:30:22 +01:00
2016-04-23 11:16:46 +01:00
gScreenFlags = SCREEN_FLAGS_PLAYING;
#ifndef DISABLE_NETWORK
if (gNetworkStart == NETWORK_MODE_SERVER) {
if (gNetworkStartPort == 0) {
gNetworkStartPort = gConfigNetwork.default_port;
}
if (str_is_null_or_empty(gCustomPassword)) {
network_set_password(gConfigNetwork.default_password);
}
else {
network_set_password(gCustomPassword);
}
network_begin_server(gNetworkStartPort);
}
#endif // DISABLE_NETWORK
2015-06-03 17:11:19 +01:00
break;
case STARTUP_ACTION_EDIT:
if (strlen(gOpenRCT2StartupActionPath) == 0) {
editor_load();
} else {
2016-10-03 19:06:13 +01:00
if (!editor_load_landscape(gOpenRCT2StartupActionPath)) {
title_load();
}
2015-06-03 17:11:19 +01:00
}
break;
2015-02-12 11:30:57 +00:00
}
#ifndef DISABLE_NETWORK
2015-07-10 13:53:41 -06:00
if (gNetworkStart == NETWORK_MODE_CLIENT) {
if (gNetworkStartPort == 0) {
gNetworkStartPort = gConfigNetwork.default_port;
}
2015-02-12 11:30:57 +00:00
network_begin_client(gNetworkStartHost, gNetworkStartPort);
}
#endif // DISABLE_NETWORK
2015-02-12 11:30:57 +00:00
openrct2_loop();
2014-10-09 00:30:22 +01:00
}
openrct2_dispose();
2014-10-09 00:30:22 +01:00
// HACK Some threads are still running which causes the game to not terminate. Investigation required!
exit(gExitCode);
2014-10-09 14:31:51 +01:00
}
void openrct2_dispose()
{
2015-02-12 11:30:57 +00:00
network_close();
http_dispose();
2015-06-08 17:16:18 +01:00
language_close_all();
rct2_dispose();
config_release();
#ifndef DISABLE_NETWORK
EVP_MD_CTX_destroy(gHashCTX);
#endif // DISABLE_NETWORK
2016-12-10 19:34:33 +00:00
rct2_interop_dispose();
platform_free();
}
2014-10-09 14:31:51 +01:00
/**
* Run the main game loop until the finished flag is set at 40fps (25ms interval).
*/
static void openrct2_loop()
{
uint32 currentTick, ticksElapsed, lastTick = 0;
2015-07-06 15:21:25 -06:00
static uint32 uncapTick = 0;
2015-07-04 13:00:32 -06:00
static int fps = 0;
static uint32 secondTick = 0;
2014-10-09 14:31:51 +01:00
log_verbose("begin openrct2 loop");
2014-10-09 14:31:51 +01:00
_finished = 0;
do {
2016-06-17 09:13:15 +02:00
bool is_minimised = (SDL_GetWindowFlags(gWindow) & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_HIDDEN)) != 0;
if (gConfigGeneral.uncap_fps && gGameSpeed <= 4 && !gOpenRCT2Headless && !is_minimised) {
2015-07-04 13:00:32 -06:00
currentTick = SDL_GetTicks();
2015-07-06 15:21:25 -06:00
if (uncapTick == 0) {
// Reset sprite locations
2015-07-04 13:00:32 -06:00
uncapTick = SDL_GetTicks();
2016-12-11 00:08:37 +00:00
sprite_position_tween_reset();
2015-07-04 13:00:32 -06:00
}
2015-08-05 13:01:25 +01:00
// Limit number of updates per loop (any long pauses or debugging can make this update for a very long time)
2016-12-11 00:08:37 +00:00
if (currentTick - uncapTick > UPDATE_TIME_MS * 60) {
uncapTick = currentTick - UPDATE_TIME_MS - 1;
2015-08-05 13:01:25 +01:00
}
2015-10-03 13:22:28 -06:00
platform_process_messages();
2016-12-11 00:08:37 +00:00
while (uncapTick <= currentTick && currentTick - uncapTick > UPDATE_TIME_MS) {
// Get the original position of each sprite
2016-12-11 00:08:37 +00:00
sprite_position_tween_store_a();
2015-08-04 01:08:42 +02:00
// Update the game so the sprite positions update
2015-07-04 13:00:32 -06:00
rct2_update();
// Get the next position of each sprite
2016-12-11 00:08:37 +00:00
sprite_position_tween_store_b();
2016-12-11 00:08:37 +00:00
uncapTick += UPDATE_TIME_MS;
2015-07-04 13:00:32 -06:00
}
// Tween the position of each sprite from the last position to the new position based on the time between the last
// tick and the next tick.
2016-12-11 00:08:37 +00:00
float nudge = 1 - ((float)(currentTick - uncapTick) / UPDATE_TIME_MS);
sprite_position_tween_all(nudge);
2016-06-17 09:13:15 +02:00
platform_draw();
2015-07-04 13:00:32 -06:00
fps++;
if (SDL_GetTicks() - secondTick >= 1000) {
fps = 0;
secondTick = SDL_GetTicks();
}
2016-12-11 00:08:37 +00:00
sprite_position_tween_restore();
2015-07-04 13:00:32 -06:00
} else {
2015-07-06 15:21:25 -06:00
uncapTick = 0;
2015-07-04 13:00:32 -06:00
currentTick = SDL_GetTicks();
ticksElapsed = currentTick - lastTick;
2016-12-11 00:08:37 +00:00
if (ticksElapsed < UPDATE_TIME_MS) {
SDL_Delay(UPDATE_TIME_MS - ticksElapsed);
lastTick += UPDATE_TIME_MS;
2016-10-18 16:13:10 -05:00
} else {
lastTick = currentTick;
2015-07-04 13:00:32 -06:00
}
platform_process_messages();
2015-07-04 13:00:32 -06:00
rct2_update();
2016-06-17 09:13:15 +02:00
if (!is_minimised) {
platform_draw();
}
2014-10-09 14:31:51 +01:00
}
} while (!_finished);
}
/**
* Causes the OpenRCT2 game loop to finish.
*/
void openrct2_finish()
{
_finished = 1;
2015-07-06 15:21:25 -06:00
}