OpenTTD Source: src/os/windows/win32_main.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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()) {
28 consumer.SkipUntilCharNotIn(StringConsumer::WHITESPACE_NO_NEWLINE);
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
52 CrashLog::InitialiseCrashLog();
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}
CrashLog::InitialiseCrashLog
static void InitialiseCrashLog()
Initialiser for crash logs; do the appropriate things so crashes are handled by our crash handler ins...
Definition crashlog_osx.cpp:232
StringConsumer
Parse data from a string / buffer.
Definition string_consumer.hpp:27
StringConsumer::SKIP_ONE_SEPARATOR
@ SKIP_ONE_SEPARATOR
Read and discard one separator, do not include it in the result.
Definition string_consumer.hpp:696
StringConsumer::WHITESPACE_NO_NEWLINE
static const std::string_view WHITESPACE_NO_NEWLINE
ASCII whitespace characters, excluding new-line.
Definition string_consumer.hpp:40
StringConsumer::SkipUntilCharNotIn
void SkipUntilCharNotIn(std::string_view chars)
Skip 8-bit chars, while they are in 'chars', until they are not.
Definition string_consumer.hpp:683
openttd_main
int openttd_main(std::span< std::string_view > arguments)
Main entry point for this lovely game.
Definition openttd.cpp:500
SetRandomSeed
void SetRandomSeed(uint32_t seed)
(Re)set the state of the random number generators.
Definition random_func.cpp:65
StrMakeValid
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Definition string.cpp:119
FS2OTTD
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.
Definition win32.cpp:340