diff --git a/src/os/macosx/CMakeLists.txt b/src/os/macosx/CMakeLists.txt index 645a057c7d..6442d1a27b 100644 --- a/src/os/macosx/CMakeLists.txt +++ b/src/os/macosx/CMakeLists.txt @@ -4,6 +4,7 @@ add_files( font_osx.h macos.h macos.mm + osx_main.cpp osx_stdafx.h string_osx.cpp string_osx.h diff --git a/src/os/macosx/osx_main.cpp b/src/os/macosx/osx_main.cpp new file mode 100644 index 0000000000..856d48f9b4 --- /dev/null +++ b/src/os/macosx/osx_main.cpp @@ -0,0 +1,53 @@ +/* + * This file is part of OpenTTD. + * OpenTTD 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, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file unix_main.cpp Main entry for Mac OSX. */ + +#include "../../stdafx.h" +#include "../../openttd.h" +#include "../../crashlog.h" +#include "../../core/random_func.hpp" +#include "../../string_func.h" + +#include +#include + +#if defined(WITH_SDL) +/* the mac implementation needs this file included in the same file as main() */ +# include +#endif +#include "macos.h" + +#include "../../safeguards.h" + +void CocoaSetupAutoreleasePool(); +void CocoaReleaseAutoreleasePool(); + +int CDECL main(int argc, char *argv[]) +{ + /* Make sure our arguments contain only valid UTF-8 characters. */ + for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]); + + CocoaSetupAutoreleasePool(); + /* This is passed if we are launched by double-clicking */ + if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) { + argv[1] = nullptr; + argc = 1; + } + + CrashLog::InitialiseCrashLog(); + + SetRandomSeed(time(nullptr)); + + signal(SIGPIPE, SIG_IGN); + + int ret = openttd_main(argc, argv); + + CocoaReleaseAutoreleasePool(); + + return ret; +} diff --git a/src/os/os2/CMakeLists.txt b/src/os/os2/CMakeLists.txt index 52534dbcbb..fbcd4783b0 100644 --- a/src/os/os2/CMakeLists.txt +++ b/src/os/os2/CMakeLists.txt @@ -1,4 +1,5 @@ add_files( os2.cpp + os2_main.cpp CONDITION OPTION_OS2 ) diff --git a/src/os/os2/os2.cpp b/src/os/os2/os2.cpp index 6e190cfa12..f18e50c4c3 100644 --- a/src/os/os2/os2.cpp +++ b/src/os/os2/os2.cpp @@ -12,8 +12,6 @@ #include "../../gui.h" #include "../../fileio_func.h" #include "../../fios.h" -#include "../../openttd.h" -#include "../../core/random_func.hpp" #include "../../string_func.h" #include "../../textbuf_gui.h" #include "../../thread.h" @@ -169,16 +167,6 @@ void ShowOSErrorBox(const char *buf, bool system) WinTerminate(hab); } -int CDECL main(int argc, char *argv[]) -{ - SetRandomSeed(time(nullptr)); - - /* Make sure our arguments contain only valid UTF-8 characters. */ - for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]); - - return openttd_main(argc, argv); -} - bool GetClipboardContents(char *buffer, const char *last) { /* XXX -- Currently no clipboard support implemented with GCC */ diff --git a/src/os/os2/os2_main.cpp b/src/os/os2/os2_main.cpp new file mode 100644 index 0000000000..a411a07151 --- /dev/null +++ b/src/os/os2/os2_main.cpp @@ -0,0 +1,28 @@ +/* + * This file is part of OpenTTD. + * OpenTTD 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, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file os2_main.cpp Main entry for OS/2. */ + +#include "../../stdafx.h" +#include "../../openttd.h" +#include "../../core/random_func.hpp" +#include "../../string_func.h" + +#include +#include + +#include "../../safeguards.h" + +int CDECL main(int argc, char *argv[]) +{ + SetRandomSeed(time(nullptr)); + + /* Make sure our arguments contain only valid UTF-8 characters. */ + for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]); + + return openttd_main(argc, argv); +} diff --git a/src/os/unix/CMakeLists.txt b/src/os/unix/CMakeLists.txt index 8e74f96643..a8a421163d 100644 --- a/src/os/unix/CMakeLists.txt +++ b/src/os/unix/CMakeLists.txt @@ -1,5 +1,6 @@ add_files( crashlog_unix.cpp + unix_main.cpp CONDITION UNIX AND NOT APPLE AND NOT OPTION_OS2 ) diff --git a/src/os/unix/unix.cpp b/src/os/unix/unix.cpp index 95d6fa1fa5..e3f6496488 100644 --- a/src/os/unix/unix.cpp +++ b/src/os/unix/unix.cpp @@ -9,9 +9,6 @@ #include "../../stdafx.h" #include "../../textbuf_gui.h" -#include "../../openttd.h" -#include "../../crashlog.h" -#include "../../core/random_func.hpp" #include "../../debug.h" #include "../../string_func.h" #include "../../fios.h" @@ -55,11 +52,6 @@ #endif #if defined(__APPLE__) -# if defined(WITH_SDL) - /* the mac implementation needs this file included in the same file as main() */ -# include -# endif - # include "../macosx/macos.h" #endif @@ -235,39 +227,6 @@ void ShowOSErrorBox(const char *buf, bool system) } #endif -#ifdef WITH_COCOA -void CocoaSetupAutoreleasePool(); -void CocoaReleaseAutoreleasePool(); -#endif - -int CDECL main(int argc, char *argv[]) -{ - /* Make sure our arguments contain only valid UTF-8 characters. */ - for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]); - -#ifdef WITH_COCOA - CocoaSetupAutoreleasePool(); - /* This is passed if we are launched by double-clicking */ - if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) { - argv[1] = nullptr; - argc = 1; - } -#endif - CrashLog::InitialiseCrashLog(); - - SetRandomSeed(time(nullptr)); - - signal(SIGPIPE, SIG_IGN); - - int ret = openttd_main(argc, argv); - -#ifdef WITH_COCOA - CocoaReleaseAutoreleasePool(); -#endif - - return ret; -} - #ifndef WITH_COCOA bool GetClipboardContents(char *buffer, const char *last) { diff --git a/src/os/unix/unix_main.cpp b/src/os/unix/unix_main.cpp new file mode 100644 index 0000000000..41ad22ced5 --- /dev/null +++ b/src/os/unix/unix_main.cpp @@ -0,0 +1,33 @@ +/* + * This file is part of OpenTTD. + * OpenTTD 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, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file unix_main.cpp Main entry for Unix. */ + +#include "../../stdafx.h" +#include "../../openttd.h" +#include "../../crashlog.h" +#include "../../core/random_func.hpp" +#include "../../string_func.h" + +#include +#include + +#include "../../safeguards.h" + +int CDECL main(int argc, char *argv[]) +{ + /* Make sure our arguments contain only valid UTF-8 characters. */ + for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]); + + CrashLog::InitialiseCrashLog(); + + SetRandomSeed(time(nullptr)); + + signal(SIGPIPE, SIG_IGN); + + return openttd_main(argc, argv); +} diff --git a/src/os/windows/CMakeLists.txt b/src/os/windows/CMakeLists.txt index 8ac2de7acc..2f41ea16e5 100644 --- a/src/os/windows/CMakeLists.txt +++ b/src/os/windows/CMakeLists.txt @@ -6,5 +6,6 @@ add_files( string_uniscribe.h win32.cpp win32.h + win32_main.cpp CONDITION WIN32 ) diff --git a/src/os/windows/win32.cpp b/src/os/windows/win32.cpp index bb93c01ba0..47cb27a3cc 100644 --- a/src/os/windows/win32.cpp +++ b/src/os/windows/win32.cpp @@ -22,10 +22,7 @@ #include "win32.h" #include "../../fios.h" #include "../../core/alloc_func.hpp" -#include "../../openttd.h" -#include "../../core/random_func.hpp" #include "../../string_func.h" -#include "../../crashlog.h" #include #include #include "../../language.h" @@ -229,37 +226,6 @@ bool FiosGetDiskFreeSpace(const char *path, uint64 *tot) return retval; } -static int ParseCommandLine(char *line, char **argv, int max_argc) -{ - int n = 0; - - do { - /* skip whitespace */ - while (*line == ' ' || *line == '\t') line++; - - /* end? */ - if (*line == '\0') break; - - /* special handling when quoted */ - if (*line == '"') { - argv[n++] = ++line; - while (*line != '"') { - if (*line == '\0') return n; - line++; - } - } else { - argv[n++] = line; - while (*line != ' ' && *line != '\t') { - if (*line == '\0') return n; - line++; - } - } - *line++ = '\0'; - } while (n != max_argc); - - return n; -} - void CreateConsole() { HANDLE hand; @@ -378,47 +344,6 @@ void ShowInfo(const char *str) } } -int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) -{ - int argc; - char *argv[64]; // max 64 command line arguments - - /* Set system timer resolution to 1ms. */ - timeBeginPeriod(1); - - CrashLog::InitialiseCrashLog(); - - /* Convert the command line to UTF-8. We need a dedicated buffer - * for this because argv[] points into this buffer and this needs to - * be available between subsequent calls to FS2OTTD(). */ - char *cmdline = stredup(FS2OTTD(GetCommandLine()).c_str()); - - /* Set the console codepage to UTF-8. */ - SetConsoleOutputCP(CP_UTF8); - -#if defined(_DEBUG) - CreateConsole(); -#endif - - _set_error_mode(_OUT_TO_MSGBOX); // force assertion output to messagebox - - /* setup random seed to something quite random */ - SetRandomSeed(GetTickCount()); - - argc = ParseCommandLine(cmdline, argv, lengthof(argv)); - - /* Make sure our arguments contain only valid UTF-8 characters. */ - for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]); - - openttd_main(argc, argv); - - /* Restore system timer resolution. */ - timeEndPeriod(1); - - free(cmdline); - return 0; -} - char *getcwd(char *buf, size_t size) { wchar_t path[MAX_PATH]; diff --git a/src/os/windows/win32_main.cpp b/src/os/windows/win32_main.cpp new file mode 100644 index 0000000000..f8b13a751f --- /dev/null +++ b/src/os/windows/win32_main.cpp @@ -0,0 +1,91 @@ +/* + * This file is part of OpenTTD. + * OpenTTD 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, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file win32_main.cpp Implementation main for Windows. */ + +#include "../../stdafx.h" +#include +#include +#include "../../openttd.h" +#include "../../core/random_func.hpp" +#include "../../string_func.h" +#include "../../crashlog.h" +#include "../../debug.h" + +#include "../../safeguards.h" + +static int ParseCommandLine(char *line, char **argv, int max_argc) +{ + int n = 0; + + do { + /* skip whitespace */ + while (*line == ' ' || *line == '\t') line++; + + /* end? */ + if (*line == '\0') break; + + /* special handling when quoted */ + if (*line == '"') { + argv[n++] = ++line; + while (*line != '"') { + if (*line == '\0') return n; + line++; + } + } else { + argv[n++] = line; + while (*line != ' ' && *line != '\t') { + if (*line == '\0') return n; + line++; + } + } + *line++ = '\0'; + } while (n != max_argc); + + return n; +} + +void CreateConsole(); + +int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) +{ + /* Set system timer resolution to 1ms. */ + timeBeginPeriod(1); + + CrashLog::InitialiseCrashLog(); + + /* Convert the command line to UTF-8. We need a dedicated buffer + * for this because argv[] points into this buffer and this needs to + * be available between subsequent calls to FS2OTTD(). */ + char *cmdline = stredup(FS2OTTD(GetCommandLine()).c_str()); + + /* Set the console codepage to UTF-8. */ + SetConsoleOutputCP(CP_UTF8); + +#if defined(_DEBUG) + CreateConsole(); +#endif + + _set_error_mode(_OUT_TO_MSGBOX); // force assertion output to messagebox + + /* setup random seed to something quite random */ + SetRandomSeed(GetTickCount()); + + char *argv[64]; // max 64 command line arguments + int argc = ParseCommandLine(cmdline, argv, lengthof(argv)); + + /* Make sure our arguments contain only valid UTF-8 characters. */ + for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]); + + int ret = openttd_main(argc, argv); + + /* Restore system timer resolution. */ + timeEndPeriod(1); + + free(cmdline); + return ret; +}