OpenTTD Source 20250523-master-g321f7e8683
win32_main.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "../../stdafx.h"
11#include <windows.h>
12#include <mmsystem.h>
13#include "../../openttd.h"
14#include "../../core/random_func.hpp"
15#include "../../core/string_consumer.hpp"
16#include "../../string_func.h"
17#include "../../crashlog.h"
18#include "../../debug.h"
19
20#include "../../safeguards.h"
21
22static auto ParseCommandLine(std::string_view line)
23{
24 std::vector<std::string_view> arguments;
25
26 StringConsumer consumer{line};
27 while (consumer.AnyBytesLeft()) {
29 if (!consumer.AnyBytesLeft()) break;
30
31 std::string_view argument;
32 if (consumer.ReadIf("\"")) {
33 /* special handling when quoted */
34 argument = consumer.ReadUntil("\"", StringConsumer::SKIP_ONE_SEPARATOR);
35 } else {
36 argument = consumer.ReadUntilCharIn(StringConsumer::WHITESPACE_NO_NEWLINE);
37 }
38
39 arguments.push_back(argument);
40 };
41
42 return arguments;
43}
44
45void CreateConsole();
46
47int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
48{
49 /* Set system timer resolution to 1ms. */
50 timeBeginPeriod(1);
51
53
54 /* Convert the command line to valid UTF-8. */
55 std::string cmdline = StrMakeValid(FS2OTTD(GetCommandLine()));
56
57 /* Set the console codepage to UTF-8. */
58 SetConsoleOutputCP(CP_UTF8);
59
60#if defined(_DEBUG)
61 CreateConsole();
62#endif
63
64 _set_error_mode(_OUT_TO_MSGBOX); // force assertion output to messagebox
65
66 /* setup random seed to something quite random */
67 SetRandomSeed(GetTickCount());
68
69 auto arguments = ParseCommandLine(cmdline);
70 int ret = openttd_main(arguments);
71
72 /* Restore system timer resolution. */
73 timeEndPeriod(1);
74
75 return ret;
76}
static void InitialiseCrashLog()
Initialiser for crash logs; do the appropriate things so crashes are handled by our crash handler ins...
Parse data from a string / buffer.
@ SKIP_ONE_SEPARATOR
Read and discard one separator, do not include it in the result.
static const std::string_view WHITESPACE_NO_NEWLINE
ASCII whitespace characters, excluding new-line.
void SkipUntilCharNotIn(std::string_view chars)
Skip 8-bit chars, while they are in 'chars', until they are not.
int openttd_main(std::span< std::string_view > arguments)
Main entry point for this lovely game.
Definition openttd.cpp:500
void SetRandomSeed(uint32_t seed)
(Re)set the state of the random number generators.
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Definition string.cpp:117
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.
Definition win32.cpp:337