forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSDL3Main.cpp
More file actions
286 lines (239 loc) · 7.61 KB
/
Copy pathSDL3Main.cpp
File metadata and controls
286 lines (239 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// USER INCLUDES //////////////////////////////////////////////////////////////
#include "SDL3Main.h"
#include "Common/CopyProtection.h"
#include "Common/CriticalSection.h"
#include "Common/Debug.h"
#include "Common/GameEngine.h"
#include "Common/GameMemory.h"
#include "Common/GameSounds.h"
#include "Common/GlobalData.h"
#include "Common/MessageStream.h"
#include "Common/Registry.h"
#include "Common/StackDump.h"
#include "Common/Team.h"
#include "Common/Version.h"
#include "GameClient/GameClient.h"
#include "GameClient/IMEManager.h"
#include "GameClient/InGameUI.h"
#include "GameClient/Mouse.h"
#include "GameLogic/GameLogic.h" ///< @todo for demo, remove
#include "Lib/BaseType.h"
#include "SDL3Device/Common/SDL3GameEngine.h"
#include "SDL3Device/GameClient/SDL3Mouse.h"
// #include "BuildVersion.h"
// #include "GeneratedVersion.h"
#include <rts/profile.h>
#include <filesystem>
#include <SDL3/SDL.h>
#include <SDL3/SDL_vulkan.h>
#include <SDL3_image/SDL_image.h>
#include <d3d8.h>
#define DXVK_WSI_SDL3 1
#include <wsi/native_wsi.h>
// GLOBALS
HINSTANCE ApplicationHInstance; ///< our application instance
HWND ApplicationHWnd = NULL; ///< our application window handle
Bool ApplicationIsWindowed = false;
Bool ShowSplashScreen = true;
Win32Mouse *TheWin32Mouse = NULL; ///< for the WndProc() only
DWORD TheMessageTime = 0; ///< For getting the time that a message was posted from Windows.
const Char *g_strFile = "data\\Generals.str";
const Char *g_csfFile = "data\\%s\\Generals.csf";
const char *gAppPrefix = ""; /// So WB can have a different debug log file name.
static HANDLE GeneralsMutex = NULL;
#define GENERALS_GUID "685EAFF2-3216-4265-B047-251C5F4B82F3"
#define DEFAULT_XRESOLUTION 800
#define DEFAULT_YRESOLUTION 600
// SDL3 specific
#include "GameNetwork/WOLBrowser/WebBrowser.h"
WebBrowser *TheWebBrowser;
SDL_Window *TheSDL3Window = NULL;
CComModule _Module;
SDL_Window *SplashWindow = NULL;
SDL_Surface *SplashSurface = NULL;
static void postInit() {
if (SplashWindow) {
SDL_DestroyWindow(SplashWindow);
SplashWindow = NULL;
}
if (TheSDL3Window)
SDL_ShowWindow(TheSDL3Window);
}
char *nextParam(char *newSource, const char *seps)
{
static char *source = NULL;
if (newSource)
{
source = newSource;
}
if (!source)
{
return NULL;
}
// find first separator
char *first = source;//strpbrk(source, seps);
if (first)
{
// go past separator
char *firstSep = strpbrk(first, seps);
char firstChar[2] = {0,0};
if (firstSep == first)
{
firstChar[0] = *first;
while (*first == firstChar[0]) first++;
}
// find end
char *end;
if (firstChar[0])
end = strpbrk(first, firstChar);
else
end = strpbrk(first, seps);
// trim string & save next start pos
if (end)
{
source = end+1;
*end = 0;
if (!*source)
source = NULL;
}
else
{
source = NULL;
}
if (first && !*first)
first = NULL;
}
return first;
}
static Bool initializeAppWindows(Bool runWindowed, Bool runSplash) {
Int startWidth = DEFAULT_XRESOLUTION, startHeight = DEFAULT_YRESOLUTION;
SDL_InitSubSystem(SDL_INIT_VIDEO);
if (!SDL_Vulkan_LoadLibrary(nullptr)) {
DEBUG_LOG(("Failed to load Vulkan library"));
return false;
}
if (runWindowed) {
// Makes the normal debug 800x600 window center in the screen.
startWidth = DEFAULT_XRESOLUTION;
startHeight = DEFAULT_YRESOLUTION;
}
TheSDL3Window = SDL_CreateWindow(
"Command and Conquer Generals", startWidth, startHeight,
SDL_WINDOW_VULKAN | SDL_WINDOW_HIDDEN);
if(!TheSDL3Window) {
DEBUG_LOG(("Failed to create window"));
return false;
}
SDL_IOStream* icoStream = SDL_IOFromFile("GeneralsZH.ico", "rb");
if (icoStream) {
SDL_Surface* icon = IMG_LoadICO_IO(icoStream);
SDL_CloseIO(icoStream);
if (icon) {
SDL_SetWindowIcon(TheSDL3Window, icon);
SDL_DestroySurface(icon);
}
}
// save our window handle for future use
ApplicationHWnd = TheSDL3Window;
// Center the window
SDL_SetWindowPosition(TheSDL3Window, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED);
setenv("DXVK_WSI_DRIVER", "SDL3", 1);
if (runSplash) {
SplashWindow =
SDL_CreateWindow("Splash", SplashSurface->w, SplashSurface->h,
SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALWAYS_ON_TOP);
} else if (TheSDL3Window) {
// If we aren't going to show the splash screen, go ahead and show the empty window
SDL_ShowWindow(TheSDL3Window);
}
if (SplashWindow) {
// Center the window
SDL_SetWindowPosition(SplashWindow, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED);
SDL_Renderer* ren = SDL_CreateRenderer(SplashWindow, SDL_SOFTWARE_RENDERER);
SDL_Texture* splashTexture = SDL_CreateTextureFromSurface(ren, SplashSurface);
SDL_RenderTexture(ren, splashTexture, NULL, NULL);
SDL_RenderPresent(ren);
SDL_DestroySurface(SplashSurface);
SplashSurface = NULL;
}
return true; // success
}
int main(int argc, char *argv[]) {
#ifdef _PROFILE
Profile::StartRange("init");
#endif
// This is similar to WinMain, where it looked up a few cmd line arguments before using the CommandLine module
// Combine argv into a single string like lpCmdLine
std::string cmdline;
for (int i = 1; i < argc; ++i) {
if (i > 1) cmdline += " ";
if (strchr(argv[i], ' ')) {
cmdline += "\"";
cmdline += argv[i];
cmdline += "\"";
} else {
cmdline += argv[i];
}
}
char *cmdlineCStr = cmdline.data();
char *token = nextParam(cmdlineCStr, "\" ");
int local_argc = 1;
char *local_argv[20];
local_argv[0] = NULL;
while (local_argc < 20 && token != NULL) {
local_argv[local_argc++] = token;
if (strcasecmp(token, "-nosplash") == 0)
ShowSplashScreen = false;
token = nextParam(NULL, "\" ");
}
// Check if Install_Final.bmp exists, if not, print an error and exit
if (!std::filesystem::exists("Install_Final.bmp")) {
std::string errorMessage = "Error: Install_Final.bmp not found. Make sure you are running the game from the correct directory.\n";
fprintf(stderr, "%s", errorMessage.c_str());
// Attempt to print using a message box as well
bool bSuccess = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", errorMessage.c_str(), NULL);
if(!bSuccess)
fprintf(stderr, "Failed to show SDL message box: %s\n", SDL_GetError());
return 1;
}
if(ShowSplashScreen) {
SplashSurface = SDL_LoadBMP("Install_Final.bmp");
if (!SplashSurface) {
fprintf(stderr, "Failed to load splash screen image Install_Final.bmp: %s\n", SDL_GetError());
ShowSplashScreen = false;
}
}
// register windows class and create application window
if (initializeAppWindows(ApplicationIsWindowed, ShowSplashScreen) == false)
return 0;
// start the log
DEBUG_INIT(DEBUG_FLAGS_DEFAULT);
initMemoryManager();
// Set up version info
TheVersion = NEW Version;
GameMain(argc, argv);
delete TheVersion;
TheVersion = NULL;
#ifdef MEMORYPOOL_DEBUG
TheMemoryPoolFactory->debugMemoryReport(
REPORT_POOLINFO | REPORT_POOL_OVERFLOW | REPORT_SIMPLE_LEAKS, 0, 0);
#endif
#if defined(_DEBUG) || defined(_INTERNAL)
TheMemoryPoolFactory->memoryPoolUsageReport("AAAMemStats");
#endif
// close the log
shutdownMemoryManager();
DEBUG_SHUTDOWN();
return 0;
}
GameEngine *CreateGameEngine(void) {
SDL3GameEngine *engine;
engine = NEW SDL3GameEngine;
// game engine may not have existed when app got focus so make sure it
// knows about current focus state.
engine->setIsActive(true);
engine->setPostInitCallback(&postInit);
return engine;
} // end CreateGameEngine