OpenTTD Source  20241111-master-gce64d5f5d9
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 "../../string_func.h"
16 #include "../../crashlog.h"
17 #include "../../debug.h"
18 
19 #include "../../safeguards.h"
20 
21 static auto ParseCommandLine(char *line)
22 {
23  std::vector<char *> arguments;
24  for (;;) {
25  /* skip whitespace */
26  while (*line == ' ' || *line == '\t') line++;
27 
28  /* end? */
29  if (*line == '\0') break;
30 
31  /* special handling when quoted */
32  if (*line == '"') {
33  arguments.push_back(++line);
34  while (*line != '"') {
35  if (*line == '\0') return arguments;
36  line++;
37  }
38  } else {
39  arguments.push_back(line);
40  while (*line != ' ' && *line != '\t') {
41  if (*line == '\0') return arguments;
42  line++;
43  }
44  }
45  *line++ = '\0';
46  };
47 
48  return arguments;
49 }
50 
51 void CreateConsole();
52 
53 int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
54 {
55  /* Set system timer resolution to 1ms. */
56  timeBeginPeriod(1);
57 
59 
60  /* Convert the command line to UTF-8. */
61  std::string cmdline = FS2OTTD(GetCommandLine());
62 
63  /* Set the console codepage to UTF-8. */
64  SetConsoleOutputCP(CP_UTF8);
65 
66 #if defined(_DEBUG)
67  CreateConsole();
68 #endif
69 
70  _set_error_mode(_OUT_TO_MSGBOX); // force assertion output to messagebox
71 
72  /* setup random seed to something quite random */
73  SetRandomSeed(GetTickCount());
74 
75  auto arguments = ParseCommandLine(cmdline.data());
76 
77  /* Make sure our arguments contain only valid UTF-8 characters. */
78  for (auto argument : arguments) StrMakeValidInPlace(argument);
79 
80  int ret = openttd_main(arguments);
81 
82  /* Restore system timer resolution. */
83  timeEndPeriod(1);
84 
85  return ret;
86 }
static void InitialiseCrashLog()
Initialiser for crash logs; do the appropriate things so crashes are handled by our crash handler ins...
int openttd_main(std::span< char *const > arguments)
Main entry point for this lovely game.
Definition: openttd.cpp:504
void SetRandomSeed(uint32_t seed)
(Re)set the state of the random number generators.
Definition: random_func.cpp:66
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings)
Scans the string for invalid characters and replaces then with a question mark '?' (if not ignored).
Definition: string.cpp:178
std::string FS2OTTD(const std::wstring &name)
Convert to OpenTTD's encoding from a wide string.
Definition: win32.cpp:337