From 0672161a4783920d0fe854e4c09d01357f830165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0imek?= Date: Wed, 25 May 2022 21:41:11 +0200 Subject: [PATCH] Replace WinMain with the main function (#16) Co-authored-by: Alexander Batalov --- src/args.cc | 43 ++------------------ src/args.h | 2 +- src/win32.cc | 72 +++++---------------------------- src/win32.h | 5 --- src/window_manager.cc | 20 +++++---- third_party/sdl2/CMakeLists.txt | 2 +- 6 files changed, 28 insertions(+), 116 deletions(-) diff --git a/src/args.cc b/src/args.cc index bb9448f..0142fc4 100644 --- a/src/args.cc +++ b/src/args.cc @@ -16,32 +16,10 @@ void argsInit(CommandLineArguments* commandLineArguments) } // 0x4E3BA4 -bool argsParse(CommandLineArguments* commandLineArguments, char* commandLine) +bool argsParse(CommandLineArguments* commandLineArguments, int argc, char* argv[]) { const char* delim = " \t"; - int argc = 0; - - // Get the number of arguments in command line. - if (*commandLine != '\0') { - char* copy = strdup(commandLine); - if (copy == NULL) { - argsFree(commandLineArguments); - return false; - } - - char* tok = strtok(copy, delim); - while (tok != NULL) { - argc++; - tok = strtok(NULL, delim); - } - - free(copy); - } - - // Make a room for argv[0] - program name. - argc++; - commandLineArguments->argc = argc; commandLineArguments->argv = (char**)malloc(sizeof(*commandLineArguments->argv) * argc); if (commandLineArguments->argv == NULL) { @@ -74,23 +52,8 @@ bool argsParse(CommandLineArguments* commandLineArguments, char* commandLine) } // Copy arguments from command line into argv. - if (*commandLine != '\0') { - char* copy = strdup(commandLine); - if (copy == NULL) { - argsFree(commandLineArguments); - return false; - } - - int arg = 1; - - char* tok = strtok(copy, delim); - while (tok != NULL) { - commandLineArguments->argv[arg] = strdup(tok); - tok = strtok(NULL, delim); - arg++; - } - - free(copy); + for (int i = 1; i < argc; i++) { // skip argv[0] + commandLineArguments->argv[i] = strdup(argv[i]); } return true; diff --git a/src/args.h b/src/args.h index 00c3930..20f9986 100644 --- a/src/args.h +++ b/src/args.h @@ -9,7 +9,7 @@ typedef struct CommandLineArguments { } CommandLineArguments; void argsInit(CommandLineArguments* commandLineArguments); -bool argsParse(CommandLineArguments* commandLineArguments, char* commandLine); +bool argsParse(CommandLineArguments* commandLineArguments, int argc, char* argv[]); void argsFree(CommandLineArguments* commandLineArguments); #endif /* ARGS_H */ diff --git a/src/win32.cc b/src/win32.cc index 1a39731..0054f3e 100644 --- a/src/win32.cc +++ b/src/win32.cc @@ -5,7 +5,7 @@ #include "main.h" #include "window_manager.h" -#include +#include // 0x51E430 DirectSoundCreateProc* gDirectSoundCreateProc = NULL; @@ -13,15 +13,6 @@ DirectSoundCreateProc* gDirectSoundCreateProc = NULL; // 0x51E434 HWND gProgramWindow = NULL; -// 0x51E438 -HINSTANCE gInstance = NULL; - -// 0x51E43C -LPSTR gCmdLine = NULL; - -// 0x51E440 -int gCmdShow = 0; - // 0x51E444 bool gProgramIsActive = false; @@ -32,28 +23,19 @@ HANDLE _GNW95_mutex = NULL; HMODULE gDSoundDLL = NULL; // 0x4DE700 -int WINAPI WinMain(_In_ HINSTANCE hInst, _In_opt_ HINSTANCE hPrevInst, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) +int main(int argc, char* argv[]) { CommandLineArguments args; _GNW95_mutex = CreateMutexA(0, TRUE, "GNW95MUTEX"); if (GetLastError() == ERROR_SUCCESS) { - ShowCursor(0); - if (_InitInstance()) { - if (_LoadDirectX()) { - gInstance = hInst; - gCmdLine = lpCmdLine; - gCmdShow = nCmdShow; - argsInit(&args); - if (argsParse(&args, lpCmdLine)) { - signal(1, _SignalHandler); - signal(3, _SignalHandler); - signal(5, _SignalHandler); - gProgramIsActive = true; - falloutMain(args.argc, args.argv); - argsFree(&args); - return 1; - } + SDL_ShowCursor(SDL_DISABLE); + if (_LoadDirectX()) { + argsInit(&args); + if (argsParse(&args, argc, argv)) { + gProgramIsActive = true; + falloutMain(args.argc, args.argv); + argsFree(&args); } } CloseHandle(_GNW95_mutex); @@ -61,32 +43,6 @@ int WINAPI WinMain(_In_ HINSTANCE hInst, _In_opt_ HINSTANCE hPrevInst, _In_ LPST return 0; } -// 0x4DE864 -bool _InitInstance() -{ - OSVERSIONINFOA osvi; - bool result; - - osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA); - -#pragma warning(suppress : 4996 28159) - if (!GetVersionExA(&osvi)) { - return true; - } - - result = true; - - if (osvi.dwPlatformId == 0 || osvi.dwPlatformId == 2 && osvi.dwMajorVersion < 4) { - result = false; - } - - if (!result) { - MessageBoxA(NULL, "This program requires Windows 95 or Windows NT version 4.0 or greater.", "Wrong Windows Version", MB_ICONSTOP); - } - - return result; -} - // 0x4DE8D0 bool _LoadDirectX() { @@ -106,8 +62,8 @@ bool _LoadDirectX() err: _UnloadDirectX(); - - MessageBoxA(NULL, "This program requires Windows 95 with DirectX 3.0a or later or Windows NT version 4.0 with Service Pack 3 or greater.", "Could not load DirectX", MB_ICONSTOP); + + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Could not load DirectX", "This program requires DirectX 3.0a or later.", NULL); return false; } @@ -120,9 +76,3 @@ void _UnloadDirectX(void) gDSoundDLL = NULL; } } - -// 0x4DE9F4 -void _SignalHandler(int sig) -{ - // TODO: Incomplete. -} diff --git a/src/win32.h b/src/win32.h index d54cbab..8f7a7b6 100644 --- a/src/win32.h +++ b/src/win32.h @@ -15,16 +15,11 @@ typedef HRESULT(__stdcall DirectSoundCreateProc)(GUID*, LPDIRECTSOUND*, IUnknown extern DirectSoundCreateProc* gDirectSoundCreateProc; extern HWND gProgramWindow; -extern HINSTANCE gInstance; -extern LPSTR gCmdLine; -extern int gCmdShow; extern bool gProgramIsActive; extern HANDLE _GNW95_mutex; extern HMODULE gDSoundDLL; -bool _InitInstance(); bool _LoadDirectX(); void _UnloadDirectX(void); -void _SignalHandler(int sig); #endif /* WIN32_H */ diff --git a/src/window_manager.cc b/src/window_manager.cc index 0bd4512..5728f5a 100644 --- a/src/window_manager.cc +++ b/src/window_manager.cc @@ -10,6 +10,8 @@ #include "text_font.h" #include "window_manager_private.h" +#include + static_assert(sizeof(struc_177) == 572, "wrong size"); static_assert(sizeof(Window) == 68, "wrong size"); static_assert(sizeof(Button) == 124, "wrong size"); @@ -1242,8 +1244,8 @@ void programWindowSetTitle(const char* title) strncpy(gProgramWindowTitle, title, 256); gProgramWindowTitle[256 - 1] = '\0'; - if (gProgramWindow != NULL) { - SetWindowTextA(gProgramWindow, gProgramWindowTitle); + if (gSdlWindow != nullptr) { + SDL_SetWindowTitle(gSdlWindow, gProgramWindowTitle); } } @@ -1296,12 +1298,14 @@ int paletteCloseFileImpl(int fd) // 0x4D8200 bool showMesageBox(const char* text) { - HCURSOR cursor = LoadCursorA(gInstance, MAKEINTRESOURCEA(IDC_ARROW)); - HCURSOR prev = SetCursor(cursor); - ShowCursor(TRUE); - MessageBoxA(NULL, text, NULL, MB_ICONSTOP); - ShowCursor(FALSE); - SetCursor(prev); + SDL_Cursor* prev = SDL_GetCursor(); + SDL_Cursor* cursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW); + SDL_SetCursor(cursor); + SDL_ShowCursor(SDL_ENABLE); + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, NULL, text, NULL); + SDL_ShowCursor(SDL_DISABLE); + SDL_SetCursor(prev); + SDL_FreeCursor(cursor); return true; } diff --git a/third_party/sdl2/CMakeLists.txt b/third_party/sdl2/CMakeLists.txt index 25de7ea..8bfdb32 100644 --- a/third_party/sdl2/CMakeLists.txt +++ b/third_party/sdl2/CMakeLists.txt @@ -20,4 +20,4 @@ endif() add_subdirectory(${sdl2_SOURCE_DIR} ${sdl2_BINARY_DIR} EXCLUDE_FROM_ALL) target_include_directories(fallout2-ce PUBLIC ${sdl2_SOURCE_DIR} ${sdl2_BINARY_DIR}) -target_link_libraries(fallout2-ce SDL2-static) +target_link_libraries(fallout2-ce SDL2-static SDL2::SDL2main)