2018-08-19 09:46:58 +02:00
|
|
|
#include "game.h"
|
2018-08-30 10:32:32 +03:00
|
|
|
#include "init.h"
|
2018-08-19 09:46:58 +02:00
|
|
|
#include "winmain.h"
|
2018-12-28 17:19:54 +01:00
|
|
|
#include <CommCtrl.h>
|
2018-12-29 11:44:44 +01:00
|
|
|
|
2018-12-28 17:19:54 +01:00
|
|
|
#include "..\resource.h"
|
2018-09-22 23:54:36 +02:00
|
|
|
|
2018-08-19 09:46:58 +02:00
|
|
|
#include <process.h>
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#include <stdio.h>
|
2018-09-22 23:54:36 +02:00
|
|
|
#include <sol.hpp>
|
|
|
|
|
2018-08-19 09:46:58 +02:00
|
|
|
#include "..\Game\draw.h"
|
|
|
|
#include "..\Game\sound.h"
|
|
|
|
#include "..\Game\inventory.h"
|
|
|
|
#include "..\Game\control.h"
|
|
|
|
#include "..\Game\gameflow.h"
|
2018-10-09 00:02:14 +02:00
|
|
|
#include "..\Game\savegame.h"
|
2018-12-28 12:12:58 +01:00
|
|
|
#include "..\Specific\roomload.h"
|
2018-08-19 09:46:58 +02:00
|
|
|
|
2018-12-29 11:44:44 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2018-08-19 09:46:58 +02:00
|
|
|
WINAPP App;
|
|
|
|
unsigned int threadId;
|
|
|
|
uintptr_t hThread;
|
|
|
|
HACCEL hAccTable;
|
2018-12-28 12:12:58 +01:00
|
|
|
byte receivedWmClose = false;
|
2018-08-19 09:46:58 +02:00
|
|
|
|
2018-12-28 12:12:58 +01:00
|
|
|
extern __int32 IsLevelLoading;
|
2018-09-22 23:54:36 +02:00
|
|
|
extern GameFlow* g_GameFlow;
|
|
|
|
extern GameScript* g_GameScript;
|
2018-12-29 11:44:44 +01:00
|
|
|
extern GameConfiguration g_Configuration;
|
2018-09-22 23:54:36 +02:00
|
|
|
|
2018-08-19 09:46:58 +02:00
|
|
|
__int32 __cdecl WinProcMsg()
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
struct tagMSG Msg;
|
|
|
|
|
|
|
|
DB_Log(2, "WinProcMsg");
|
|
|
|
do
|
|
|
|
{
|
|
|
|
GetMessageA(&Msg, 0, 0, 0);
|
|
|
|
if (!TranslateAcceleratorA(WindowsHandle, hAccTable, &Msg))
|
|
|
|
{
|
|
|
|
TranslateMessage(&Msg);
|
|
|
|
DispatchMessageA(&Msg);
|
|
|
|
}
|
|
|
|
result = Unk_876C48;
|
|
|
|
} while (!Unk_876C48 && Msg.message != WM_QUIT);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-28 12:12:58 +01:00
|
|
|
void __stdcall HandleWmCommand(unsigned __int16 wParam)
|
|
|
|
{
|
|
|
|
if (wParam == 8)
|
|
|
|
{
|
|
|
|
DB_Log(5, "Pressed ALT + ENTER");
|
|
|
|
|
|
|
|
if (!IsLevelLoading)
|
|
|
|
{
|
|
|
|
SuspendThread((HANDLE)hThread);
|
|
|
|
DB_Log(5, "Game thread suspended");
|
|
|
|
|
|
|
|
g_Renderer->ToggleFullScreen();
|
|
|
|
|
|
|
|
ResumeThread((HANDLE)hThread);
|
|
|
|
DB_Log(5, "Game thread resumed");
|
|
|
|
|
|
|
|
if (g_Renderer->IsFullsScreen())
|
|
|
|
{
|
|
|
|
SetCursor(0);
|
|
|
|
ShowCursor(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetCursor(LoadCursorA(App.hInstance, (LPCSTR)0x68));
|
|
|
|
ShowCursor(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT CALLBACK WinAppProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
if (msg > 0x10)
|
|
|
|
{
|
|
|
|
if (msg == WM_COMMAND)
|
|
|
|
{
|
|
|
|
DB_Log(6, "WM_COMMAND");
|
|
|
|
HandleWmCommand((unsigned __int16)wParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
return DefWindowProcA(hWnd, msg, wParam, (LPARAM)lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg == WM_CLOSE)
|
|
|
|
{
|
|
|
|
DB_Log(6, "WM_CLOSE");
|
2018-12-29 14:27:39 +01:00
|
|
|
|
2018-12-28 12:12:58 +01:00
|
|
|
receivedWmClose = true;
|
|
|
|
PostQuitMessage(0);
|
|
|
|
|
2018-12-29 14:27:39 +01:00
|
|
|
return DefWindowProcA(hWnd, 0x10u, wParam, (LPARAM)lParam);
|
2018-12-28 12:12:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (msg != WM_ACTIVATE)
|
|
|
|
{
|
|
|
|
return DefWindowProcA(hWnd, msg, wParam, (LPARAM)lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
DB_Log(6, "WM_ACTIVATE");
|
|
|
|
|
|
|
|
if (receivedWmClose)
|
|
|
|
{
|
|
|
|
return DefWindowProcA(hWnd, msg, wParam, (LPARAM)lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (App_Unk00D9AC2B)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((__int16)wParam)
|
|
|
|
{
|
|
|
|
if ((signed __int32)(unsigned __int16)wParam > 0 && (signed __int32)(unsigned __int16)wParam <= 2)
|
|
|
|
{
|
|
|
|
DB_Log(6, "WM_ACTIVE");
|
|
|
|
|
|
|
|
if (App_Unk00D9AC19)
|
|
|
|
{
|
|
|
|
//AcquireDirectInput(true);
|
|
|
|
ResumeThread((HANDLE)hThread);
|
|
|
|
App_Unk00D9ABFD = 0;
|
|
|
|
|
|
|
|
DB_Log(5, "Game Thread Resumed");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DB_Log(6, "WM_INACTIVE");
|
|
|
|
|
|
|
|
if (App_Unk00D9AC19)
|
|
|
|
{
|
|
|
|
//AcquireDirectInput(false);
|
|
|
|
|
|
|
|
DB_Log(5, "HangGameThread");
|
|
|
|
|
|
|
|
//if (ptr_ctx->isInScene)
|
|
|
|
// WaitForInSceneMaybe();
|
|
|
|
App_Unk00D9ABFD = 1;
|
|
|
|
//if (!ptr_ctx->isInScene)
|
|
|
|
// WaitForInSceneMaybe();
|
|
|
|
|
|
|
|
SuspendThread((HANDLE)hThread);
|
|
|
|
|
|
|
|
DB_Log(5, "Game Thread Suspended");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-19 09:46:58 +02:00
|
|
|
__int32 __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, __int32 nShowCmd)
|
|
|
|
{
|
|
|
|
int RetVal;
|
|
|
|
int n;
|
|
|
|
|
2018-12-29 13:19:18 +01:00
|
|
|
// Process the command line
|
|
|
|
bool setup = false;
|
|
|
|
bool debug = false;
|
|
|
|
|
|
|
|
LPWSTR* argv;
|
|
|
|
__int32 argc;
|
|
|
|
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
|
|
|
|
|
|
|
|
for (__int32 i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (wcscmp(argv[i], L"/setup") == 0)
|
|
|
|
setup = true;
|
|
|
|
if (wcscmp(argv[i], L"/debug") == 0)
|
|
|
|
debug = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalFree(argv);
|
|
|
|
|
2018-08-19 09:46:58 +02:00
|
|
|
// Clear Application Structure
|
|
|
|
memset(&App, 0, sizeof(WINAPP));
|
|
|
|
|
|
|
|
_CrtSetReportMode(0, 2);
|
|
|
|
_CrtSetDbgFlag(-1);
|
2018-09-22 23:54:36 +02:00
|
|
|
|
2018-09-04 13:24:50 +02:00
|
|
|
// TODO: deprecated
|
2018-08-19 09:46:58 +02:00
|
|
|
LoadGameflow();
|
2018-12-30 11:41:48 +01:00
|
|
|
//LoadSettings();
|
2018-08-19 09:46:58 +02:00
|
|
|
|
2018-09-02 09:29:36 +02:00
|
|
|
// Initialise the new scripting system
|
2018-09-22 23:54:36 +02:00
|
|
|
sol::state luaState;
|
|
|
|
luaState.open_libraries(sol::lib::base);
|
|
|
|
|
|
|
|
g_GameFlow = new GameFlow(&luaState);
|
|
|
|
g_GameFlow->ExecuteScript("Scripts\\English.lua");
|
|
|
|
g_GameFlow->ExecuteScript("Scripts\\Settings.lua");
|
|
|
|
g_GameFlow->ExecuteScript("Scripts\\Gameflow.lua");
|
|
|
|
|
|
|
|
g_GameScript = new GameScript(&luaState);
|
2018-09-02 09:29:36 +02:00
|
|
|
|
2018-10-09 00:02:14 +02:00
|
|
|
// Initialise chunks for savegames
|
|
|
|
SaveGame::Start();
|
|
|
|
|
2018-08-19 09:46:58 +02:00
|
|
|
App.hInstance = hInstance;
|
|
|
|
App.WindowClass.hIcon = NULL;
|
|
|
|
App.WindowClass.lpszMenuName = NULL;
|
|
|
|
App.WindowClass.lpszClassName = "TR5Main";
|
|
|
|
App.WindowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
|
|
|
|
App.WindowClass.hInstance = hInstance;
|
|
|
|
App.WindowClass.style = CS_VREDRAW | CS_HREDRAW;
|
|
|
|
App.WindowClass.lpfnWndProc = (WNDPROC)WinAppProc;
|
|
|
|
App.WindowClass.cbClsExtra = 0;
|
|
|
|
App.WindowClass.cbWndExtra = 0;
|
|
|
|
App.WindowClass.hCursor = LoadCursor(App.hInstance, IDC_ARROW);
|
|
|
|
|
|
|
|
if (!RegisterClass(&App.WindowClass))
|
|
|
|
{
|
|
|
|
printf("Unable To Register Window Class\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-12-29 11:44:44 +01:00
|
|
|
// Create the renderer and enumerate adapters and video modes
|
|
|
|
g_Renderer = new Renderer();
|
|
|
|
g_Renderer->Create();
|
|
|
|
g_Renderer->EnumerateVideoModes();
|
|
|
|
|
|
|
|
// Load configuration and optionally show the setup dialog
|
2018-12-29 13:19:18 +01:00
|
|
|
if (setup || !LoadConfiguration())
|
2018-12-29 11:44:44 +01:00
|
|
|
{
|
|
|
|
if (!SetupDialog())
|
|
|
|
{
|
|
|
|
WinClose();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadConfiguration();
|
|
|
|
}
|
|
|
|
|
2018-08-19 09:46:58 +02:00
|
|
|
tagRECT Rect;
|
|
|
|
|
|
|
|
Rect.left = 0;
|
|
|
|
Rect.top = 0;
|
2018-12-29 11:44:44 +01:00
|
|
|
Rect.right = g_Configuration.Width;
|
|
|
|
Rect.bottom = g_Configuration.Height;
|
2018-08-19 09:46:58 +02:00
|
|
|
|
|
|
|
AdjustWindowRect(&Rect, WS_CAPTION, false);
|
|
|
|
|
|
|
|
App.WindowHandle = CreateWindowEx(
|
|
|
|
WS_THICKFRAME,
|
|
|
|
"TR5Main",
|
2018-09-22 23:54:36 +02:00
|
|
|
g_GameFlow->GetSettings()->WindowTitle.c_str(),
|
2018-08-19 09:46:58 +02:00
|
|
|
WS_BORDER,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
Rect.right - Rect.left,
|
|
|
|
Rect.bottom - Rect.top,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
App.hInstance,
|
|
|
|
nullptr
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!App.WindowHandle)
|
|
|
|
{
|
2018-12-28 12:12:58 +01:00
|
|
|
printf("Unable To Create Window: %d\n", GetLastError());
|
2018-08-19 09:46:58 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-12-29 11:44:44 +01:00
|
|
|
PhdWidth = g_Configuration.Width;
|
|
|
|
PhdHeight = g_Configuration.Height;
|
2018-11-06 23:01:00 +01:00
|
|
|
|
2018-12-28 17:19:54 +01:00
|
|
|
// Initialise the renderer
|
2018-12-31 10:38:34 +01:00
|
|
|
g_Renderer->Initialise(g_Configuration.Width, g_Configuration.Height, g_Configuration.RefreshRate,
|
|
|
|
g_Configuration.Windowed, App.WindowHandle);
|
2018-08-19 09:46:58 +02:00
|
|
|
|
|
|
|
// Initialize audio
|
2018-12-31 10:38:34 +01:00
|
|
|
if (g_Configuration.EnableSound)
|
2018-12-29 11:44:44 +01:00
|
|
|
Sound_Init();
|
2018-08-19 09:46:58 +02:00
|
|
|
|
|
|
|
// Initialise the new inventory
|
|
|
|
g_Inventory = new Inventory();
|
|
|
|
|
|
|
|
SetWindowPos(App.WindowHandle, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
|
|
|
|
|
2018-09-04 13:24:50 +02:00
|
|
|
WindowsHandle = App.WindowHandle;
|
2018-08-19 09:46:58 +02:00
|
|
|
|
|
|
|
App.bNoFocus = false;
|
|
|
|
App.isInScene = false;
|
|
|
|
|
|
|
|
UpdateWindow(WindowsHandle);
|
|
|
|
ShowWindow(WindowsHandle, nShowCmd);
|
|
|
|
|
|
|
|
SetCursor(0);
|
|
|
|
ShowCursor(0);
|
|
|
|
hAccTable = LoadAcceleratorsA(hInstance, (LPCSTR)0x65);
|
|
|
|
|
|
|
|
SoundActive = false;
|
|
|
|
DoTheGame = true;
|
|
|
|
|
|
|
|
Unk_876C48 = false;
|
|
|
|
hThread = _beginthreadex(0, 0, &GameMain, 0, 0, &threadId);
|
|
|
|
WinProcMsg();
|
|
|
|
Unk_876C48 = true;
|
|
|
|
|
|
|
|
while (DoTheGame);
|
|
|
|
|
|
|
|
WinClose();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
__int32 __cdecl WinClose()
|
|
|
|
{
|
|
|
|
DB_Log(2, "WinClose - DLL");
|
2018-09-04 13:24:50 +02:00
|
|
|
|
2018-08-19 09:46:58 +02:00
|
|
|
DestroyAcceleratorTable(hAccTable);
|
2018-12-29 11:44:44 +01:00
|
|
|
|
2018-12-31 10:38:34 +01:00
|
|
|
if (g_Configuration.EnableSound)
|
2018-12-29 11:44:44 +01:00
|
|
|
Sound_DeInit();
|
2018-08-19 09:46:58 +02:00
|
|
|
|
|
|
|
delete g_Renderer;
|
|
|
|
delete g_Inventory;
|
2018-09-22 23:54:36 +02:00
|
|
|
delete g_GameFlow;
|
2018-08-19 09:46:58 +02:00
|
|
|
|
2018-10-09 00:02:14 +02:00
|
|
|
SaveGame::End();
|
|
|
|
|
2018-08-19 09:46:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Inject_WinMain()
|
|
|
|
{
|
|
|
|
INJECT(0x004D23E0, WinClose);
|
|
|
|
INJECT(0x004D24C0, WinProcMsg);
|
|
|
|
INJECT(0x004D1C00, WinMain);
|
|
|
|
}
|