Replace WinMain with the main function (#16)

Co-authored-by: Alexander Batalov <alex.batalov@gmail.com>
This commit is contained in:
Jan Šimek 2022-05-25 21:41:11 +02:00 committed by GitHub
parent da1808961b
commit 0672161a47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 116 deletions

View File

@ -16,32 +16,10 @@ void argsInit(CommandLineArguments* commandLineArguments)
} }
// 0x4E3BA4 // 0x4E3BA4
bool argsParse(CommandLineArguments* commandLineArguments, char* commandLine) bool argsParse(CommandLineArguments* commandLineArguments, int argc, char* argv[])
{ {
const char* delim = " \t"; 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->argc = argc;
commandLineArguments->argv = (char**)malloc(sizeof(*commandLineArguments->argv) * argc); commandLineArguments->argv = (char**)malloc(sizeof(*commandLineArguments->argv) * argc);
if (commandLineArguments->argv == NULL) { if (commandLineArguments->argv == NULL) {
@ -74,23 +52,8 @@ bool argsParse(CommandLineArguments* commandLineArguments, char* commandLine)
} }
// Copy arguments from command line into argv. // Copy arguments from command line into argv.
if (*commandLine != '\0') { for (int i = 1; i < argc; i++) { // skip argv[0]
char* copy = strdup(commandLine); commandLineArguments->argv[i] = strdup(argv[i]);
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);
} }
return true; return true;

View File

@ -9,7 +9,7 @@ typedef struct CommandLineArguments {
} CommandLineArguments; } CommandLineArguments;
void argsInit(CommandLineArguments* commandLineArguments); void argsInit(CommandLineArguments* commandLineArguments);
bool argsParse(CommandLineArguments* commandLineArguments, char* commandLine); bool argsParse(CommandLineArguments* commandLineArguments, int argc, char* argv[]);
void argsFree(CommandLineArguments* commandLineArguments); void argsFree(CommandLineArguments* commandLineArguments);
#endif /* ARGS_H */ #endif /* ARGS_H */

View File

@ -5,7 +5,7 @@
#include "main.h" #include "main.h"
#include "window_manager.h" #include "window_manager.h"
#include <signal.h> #include <SDL.h>
// 0x51E430 // 0x51E430
DirectSoundCreateProc* gDirectSoundCreateProc = NULL; DirectSoundCreateProc* gDirectSoundCreateProc = NULL;
@ -13,15 +13,6 @@ DirectSoundCreateProc* gDirectSoundCreateProc = NULL;
// 0x51E434 // 0x51E434
HWND gProgramWindow = NULL; HWND gProgramWindow = NULL;
// 0x51E438
HINSTANCE gInstance = NULL;
// 0x51E43C
LPSTR gCmdLine = NULL;
// 0x51E440
int gCmdShow = 0;
// 0x51E444 // 0x51E444
bool gProgramIsActive = false; bool gProgramIsActive = false;
@ -32,28 +23,19 @@ HANDLE _GNW95_mutex = NULL;
HMODULE gDSoundDLL = NULL; HMODULE gDSoundDLL = NULL;
// 0x4DE700 // 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; CommandLineArguments args;
_GNW95_mutex = CreateMutexA(0, TRUE, "GNW95MUTEX"); _GNW95_mutex = CreateMutexA(0, TRUE, "GNW95MUTEX");
if (GetLastError() == ERROR_SUCCESS) { if (GetLastError() == ERROR_SUCCESS) {
ShowCursor(0); SDL_ShowCursor(SDL_DISABLE);
if (_InitInstance()) { if (_LoadDirectX()) {
if (_LoadDirectX()) { argsInit(&args);
gInstance = hInst; if (argsParse(&args, argc, argv)) {
gCmdLine = lpCmdLine; gProgramIsActive = true;
gCmdShow = nCmdShow; falloutMain(args.argc, args.argv);
argsInit(&args); argsFree(&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;
}
} }
} }
CloseHandle(_GNW95_mutex); CloseHandle(_GNW95_mutex);
@ -61,32 +43,6 @@ int WINAPI WinMain(_In_ HINSTANCE hInst, _In_opt_ HINSTANCE hPrevInst, _In_ LPST
return 0; 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 // 0x4DE8D0
bool _LoadDirectX() bool _LoadDirectX()
{ {
@ -106,8 +62,8 @@ bool _LoadDirectX()
err: err:
_UnloadDirectX(); _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; return false;
} }
@ -120,9 +76,3 @@ void _UnloadDirectX(void)
gDSoundDLL = NULL; gDSoundDLL = NULL;
} }
} }
// 0x4DE9F4
void _SignalHandler(int sig)
{
// TODO: Incomplete.
}

View File

@ -15,16 +15,11 @@ typedef HRESULT(__stdcall DirectSoundCreateProc)(GUID*, LPDIRECTSOUND*, IUnknown
extern DirectSoundCreateProc* gDirectSoundCreateProc; extern DirectSoundCreateProc* gDirectSoundCreateProc;
extern HWND gProgramWindow; extern HWND gProgramWindow;
extern HINSTANCE gInstance;
extern LPSTR gCmdLine;
extern int gCmdShow;
extern bool gProgramIsActive; extern bool gProgramIsActive;
extern HANDLE _GNW95_mutex; extern HANDLE _GNW95_mutex;
extern HMODULE gDSoundDLL; extern HMODULE gDSoundDLL;
bool _InitInstance();
bool _LoadDirectX(); bool _LoadDirectX();
void _UnloadDirectX(void); void _UnloadDirectX(void);
void _SignalHandler(int sig);
#endif /* WIN32_H */ #endif /* WIN32_H */

View File

@ -10,6 +10,8 @@
#include "text_font.h" #include "text_font.h"
#include "window_manager_private.h" #include "window_manager_private.h"
#include <SDL.h>
static_assert(sizeof(struc_177) == 572, "wrong size"); static_assert(sizeof(struc_177) == 572, "wrong size");
static_assert(sizeof(Window) == 68, "wrong size"); static_assert(sizeof(Window) == 68, "wrong size");
static_assert(sizeof(Button) == 124, "wrong size"); static_assert(sizeof(Button) == 124, "wrong size");
@ -1242,8 +1244,8 @@ void programWindowSetTitle(const char* title)
strncpy(gProgramWindowTitle, title, 256); strncpy(gProgramWindowTitle, title, 256);
gProgramWindowTitle[256 - 1] = '\0'; gProgramWindowTitle[256 - 1] = '\0';
if (gProgramWindow != NULL) { if (gSdlWindow != nullptr) {
SetWindowTextA(gProgramWindow, gProgramWindowTitle); SDL_SetWindowTitle(gSdlWindow, gProgramWindowTitle);
} }
} }
@ -1296,12 +1298,14 @@ int paletteCloseFileImpl(int fd)
// 0x4D8200 // 0x4D8200
bool showMesageBox(const char* text) bool showMesageBox(const char* text)
{ {
HCURSOR cursor = LoadCursorA(gInstance, MAKEINTRESOURCEA(IDC_ARROW)); SDL_Cursor* prev = SDL_GetCursor();
HCURSOR prev = SetCursor(cursor); SDL_Cursor* cursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
ShowCursor(TRUE); SDL_SetCursor(cursor);
MessageBoxA(NULL, text, NULL, MB_ICONSTOP); SDL_ShowCursor(SDL_ENABLE);
ShowCursor(FALSE); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, NULL, text, NULL);
SetCursor(prev); SDL_ShowCursor(SDL_DISABLE);
SDL_SetCursor(prev);
SDL_FreeCursor(cursor);
return true; return true;
} }

View File

@ -20,4 +20,4 @@ endif()
add_subdirectory(${sdl2_SOURCE_DIR} ${sdl2_BINARY_DIR} EXCLUDE_FROM_ALL) add_subdirectory(${sdl2_SOURCE_DIR} ${sdl2_BINARY_DIR} EXCLUDE_FROM_ALL)
target_include_directories(fallout2-ce PUBLIC ${sdl2_SOURCE_DIR} ${sdl2_BINARY_DIR}) 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)