TombEngine/TR5Main/Specific/winmain.cpp

457 lines
11 KiB
C++
Raw Normal View History

#include "framework.h"
2018-08-19 09:46:58 +02:00
#include "winmain.h"
#include "init.h"
#include "resource.h"
#include "draw.h"
#include "sound.h"
#include "inventory.h"
#include "control.h"
#include "gameflow.h"
#include "savegame.h"
#include "level.h"
#include "configuration.h"
#include "Renderer11.h"
#include <CommCtrl.h>
#include <fcntl.h>
#include <process.h>
#include <corecrt_io.h>
using namespace T5M::Renderer;
using std::exception;
using std::string;
using std::cout;
using std::endl;
WINAPP App;
unsigned int ThreadID;
uintptr_t ThreadHandle;
2018-08-19 09:46:58 +02:00
HACCEL hAccTable;
byte receivedWmClose = false;
2019-01-06 12:27:56 +01:00
bool Debug = false;
2020-04-23 19:22:01 +02:00
HWND WindowsHandle;
2020-04-24 19:15:05 +02:00
int App_Unk00D9ABFD;
2019-12-02 09:11:21 +01:00
extern int IsLevelLoading;
extern GameFlow* g_GameFlow;
extern GameScript* g_GameScript;
extern GameConfiguration g_Configuration;
DWORD DebugConsoleThreadID;
DWORD MainThreadID;
bool BlockAllInput = true;
int skipLoop = -1;
int skipFrames = 2;
2020-06-11 23:03:05 -03:00
int lockInput = 0;
int newSkipLoop = -1;
int newSkipFrames = 2;
int newLockInput = 0;
bool newSkipFramesValue = false;
bool newSkipLoopValue = false;
bool newLockInputValue = false;
2020-08-18 20:26:24 +02:00
#if _DEBUG
string commit;
#endif
2020-08-25 19:25:28 -03:00
int lua_exception_handler(lua_State* L, sol::optional<const exception&> maybe_exception, sol::string_view description)
2020-04-28 12:24:10 -03:00
{
return luaL_error(L, description.data());
}
void WinProcMsg()
2018-08-19 09:46:58 +02:00
{
MSG Msg;
2018-08-19 09:46:58 +02:00
do
{
GetMessage(&Msg, 0, 0, 0);
if (!TranslateAccelerator(WindowsHandle, hAccTable, &Msg))
2018-08-19 09:46:58 +02:00
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
2018-08-19 09:46:58 +02:00
}
}
while (!ThreadEnded && Msg.message != WM_QUIT);
2018-08-19 09:46:58 +02:00
}
void CALLBACK HandleWmCommand(unsigned short wParam)
{
if (wParam == 8)
{
if (!IsLevelLoading)
{
SuspendThread((HANDLE)ThreadHandle);
2020-08-09 15:25:56 +02:00
g_Renderer.toggleFullScreen();
ResumeThread((HANDLE)ThreadHandle);
2020-08-09 15:25:56 +02:00
if (g_Renderer.isFullsScreen())
{
SetCursor(0);
ShowCursor(false);
}
else
{
SetCursor(LoadCursorA(App.hInstance, (LPCSTR)0x68));
ShowCursor(true);
}
}
}
}
2020-08-18 20:26:24 +02:00
void getCurrentCommit() {
LPSTR cmdLine = {TEXT("git.exe log -1 --oneline")};
SECURITY_ATTRIBUTES sa = {0};
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
HANDLE hStdOutRd, hStdOutWr;
HANDLE hStdErrRd, hStdErrWr;
if(!CreatePipe(&hStdOutRd, &hStdOutWr, &sa, 0)){
// error handling...
}
if(!CreatePipe(&hStdErrRd, &hStdErrWr, &sa, 0)){
// error handling...
}
SetHandleInformation(hStdOutRd, HANDLE_FLAG_INHERIT, 0);
SetHandleInformation(hStdErrRd, HANDLE_FLAG_INHERIT, 0);
STARTUPINFO si = {};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = hStdOutWr;
si.hStdError = hStdErrWr;
PROCESS_INFORMATION pi = {};
if(!CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)){
// error handling...
} else{
CHAR buf[256];
DWORD n;
BOOL success = ReadFile(hStdOutRd, buf, 256, &n, NULL);
if(!success || n == 0){
std::cout << "Failed to call ReadFile" << std::endl;
}
commit = std::string(buf, buf + n);
// read from hStdOutRd and hStdErrRd as needed until the process is terminated...
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
CloseHandle(hStdOutRd);
CloseHandle(hStdOutWr);
CloseHandle(hStdErrRd);
CloseHandle(hStdErrWr);
}
void HandleScriptMessage(WPARAM wParam)
{
string ErrorMessage;
string message = *(string*)(wParam);
bool status = false;
//check whether line starts with "lua "
if (message.find("lua ") == 0) {
string scriptSubstring = message.substr(4);
status = g_GameScript->ExecuteScript(scriptSubstring, ErrorMessage);
}
else {
2020-06-11 23:03:05 -03:00
if (message.find("SL=") == 0)
{
string scriptSubstring = message.substr(3);
newSkipLoop = stoi(scriptSubstring);
newSkipLoopValue = true;
}
else if (message.find("SF=") == 0)
{
2020-06-11 23:03:05 -03:00
string scriptSubstring = message.substr(3);
newSkipFrames = stoi(scriptSubstring);
newSkipFramesValue = true;
}
2020-06-11 23:03:05 -03:00
else if (message.find("LI=") == 0)
{
2020-06-11 23:03:05 -03:00
string scriptSubstring = message.substr(3);
newLockInput = stoi(scriptSubstring);
newLockInputValue = true;
}
else
{
status = g_GameScript->ExecuteString(message, ErrorMessage);
}
}
if (!status)
cout << ErrorMessage << endl;
}
LRESULT CALLBACK WinAppProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_USER + 0)
{
HandleScriptMessage(wParam);
return 0;
}
2019-03-17 11:05:07 +01:00
// Disables ALT + SPACE
if (msg == WM_SYSCOMMAND && wParam == SC_KEYMENU)
2019-03-17 11:05:07 +01:00
return 0;
if (msg > 0x10)
{
if (msg == WM_COMMAND)
2019-12-02 09:11:21 +01:00
HandleWmCommand((unsigned short)wParam);
return DefWindowProcA(hWnd, msg, wParam, (LPARAM)lParam);
}
if (msg == WM_CLOSE)
{
receivedWmClose = true;
PostQuitMessage(0);
DoTheGame = false;
2018-12-29 14:27:39 +01:00
return DefWindowProcA(hWnd, 0x10u, wParam, (LPARAM)lParam);
}
if (msg != WM_ACTIVATE)
return DefWindowProcA(hWnd, msg, wParam, (LPARAM)lParam);
if (receivedWmClose)
{
return DefWindowProcA(hWnd, msg, wParam, (LPARAM)lParam);
}
2019-12-02 09:11:21 +01:00
if ((short)wParam)
{
2019-12-02 09:11:21 +01:00
if ((signed int)(unsigned short)wParam > 0 && (signed int)(unsigned short)wParam <= 2)
{
//DB_Log(6, "WM_ACTIVE");
BlockAllInput = false;
2019-01-06 12:27:56 +01:00
if (!Debug)
ResumeThread((HANDLE)ThreadHandle);
2019-01-06 12:27:56 +01:00
2018-12-31 17:13:20 +01:00
App_Unk00D9ABFD = 0;
return 0;
}
}
else
{
//DB_Log(6, "WM_INACTIVE");
//DB_Log(5, "HangGameThread");
BlockAllInput = true;
2018-12-31 17:13:20 +01:00
App_Unk00D9ABFD = 1;
2019-01-06 12:27:56 +01:00
if (!Debug)
SuspendThread((HANDLE)ThreadHandle);
}
2018-12-31 17:13:20 +01:00
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
2018-08-19 09:46:58 +02:00
{
2020-08-18 20:26:24 +02:00
if constexpr (DebugBuild)
getCurrentCommit();
2018-08-19 09:46:58 +02:00
int RetVal;
int n;
2018-12-29 13:19:18 +01:00
// Process the command line
bool setup = false;
2020-05-23 19:59:58 +02:00
2020-05-24 08:31:36 +02:00
//TrLevel* level = new TrLevel(string("Data\\andrea1.t5m"));
//level->Load();
2019-01-06 12:27:56 +01:00
2018-12-29 13:19:18 +01:00
LPWSTR* argv;
2019-12-02 09:11:21 +01:00
int argc;
2018-12-29 13:19:18 +01:00
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
2019-12-02 09:11:21 +01:00
for (int i = 1; i < argc; i++)
2018-12-29 13:19:18 +01:00
{
if (wcscmp(argv[i], L"/setup") == 0)
setup = true;
if (wcscmp(argv[i], L"/debug") == 0)
2019-01-06 12:27:56 +01:00
Debug = true;
2018-12-29 13:19:18 +01:00
}
LocalFree(argv);
2018-08-19 09:46:58 +02:00
// Clear Application Structure
memset(&App, 0, sizeof(WINAPP));
2018-09-02 09:29:36 +02:00
// Initialise the new scripting system
sol::state luaState;
luaState.open_libraries(sol::lib::base);
2020-04-28 12:24:10 -03:00
luaState.set_exception_handler(lua_exception_handler);
g_GameFlow = new GameFlow(&luaState);
LoadScript();
g_GameScript = new GameScript(&luaState);
2018-09-02 09:29:36 +02:00
2020-09-28 10:02:24 -03:00
luaState.set_function("GetItemByID", &GameScript::GetItemById, g_GameScript);
luaState.set_function("GetItemByName", &GameScript::GetItemByName, g_GameScript);
luaState.set_function("CreatePosition", &GameScript::CreatePosition, g_GameScript);
luaState.set_function("CreateRotation", &GameScript::CreateRotation, g_GameScript);
luaState.set_function("CalculateDistance", &GameScript::CalculateDistance, g_GameScript);
luaState.set_function("CalculateHorizontalDistance", &GameScript::CalculateHorizontalDistance, g_GameScript);
2018-10-09 00:02:14 +02:00
// Initialise chunks for savegames
SaveGame::Start();
2019-05-09 13:50:55 +02:00
INITCOMMONCONTROLSEX commCtrlInit;
commCtrlInit.dwSize = sizeof(INITCOMMONCONTROLSEX);
commCtrlInit.dwICC = ICC_USEREX_CLASSES | ICC_STANDARD_CLASSES;
InitCommonControlsEx(&commCtrlInit);
// Initialise main window
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 = reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH));
2018-08-19 09:46:58 +02:00
App.WindowClass.hInstance = hInstance;
App.WindowClass.style = CS_VREDRAW | CS_HREDRAW;
App.WindowClass.lpfnWndProc = WinAppProc;
2018-08-19 09:46:58 +02:00
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");
2019-11-21 07:43:34 +01:00
return false;
2018-08-19 09:46:58 +02:00
}
// Create the renderer and enumerate adapters and video modes
g_Renderer.Create();
g_Renderer.EnumerateVideoModes();
// Load configuration and optionally show the setup dialog
InitDefaultConfiguration();
2018-12-29 13:19:18 +01:00
if (setup || !LoadConfiguration())
{
if (!SetupDialog())
{
WinClose();
return 0;
}
LoadConfiguration();
}
RECT Rect;
2018-08-19 09:46:58 +02:00
Rect.left = 0;
Rect.top = 0;
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(
2019-03-17 11:05:07 +01:00
0,
2018-08-19 09:46:58 +02:00
"TR5Main",
g_GameFlow->GetSettings()->WindowTitle.c_str(),
2019-03-17 11:05:07 +01:00
WS_POPUP,
CW_USEDEFAULT, // TODO: change this to center of screen !!!
CW_USEDEFAULT,
2018-08-19 09:46:58 +02:00
Rect.right - Rect.left,
Rect.bottom - Rect.top,
2019-03-17 11:05:07 +01:00
NULL,
NULL,
2018-08-19 09:46:58 +02:00
App.hInstance,
2019-03-17 11:05:07 +01:00
NULL
2018-08-19 09:46:58 +02:00
);
if (!App.WindowHandle)
{
printf("Unable To Create Window: %d\n", GetLastError());
2019-11-21 07:43:34 +01:00
return false;
2018-08-19 09:46:58 +02:00
}
WindowsHandle = App.WindowHandle;
2018-12-28 17:19:54 +01:00
// Initialise the renderer
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
if (g_Configuration.EnableSound)
Sound_Init();
2018-08-19 09:46:58 +02:00
// Initialise the new inventory
//g_Inventory = Inventory();
2018-08-19 09:46:58 +02:00
App.bNoFocus = false;
App.isInScene = false;
UpdateWindow(WindowsHandle);
ShowWindow(WindowsHandle, nShowCmd);
//Create debug script terminal
if (Debug)
{
MainThreadID = GetWindowThreadProcessId(WindowsHandle, NULL);
AllocConsole();
HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
DWORD consoleModeIn;
int hCrt = _open_osfhandle((long)handle_in, _O_BINARY);
FILE* hf_in = _fdopen(hCrt, "r");
setvbuf(hf_in, NULL, _IONBF, 512);
GetConsoleMode(handle_in, &consoleModeIn);
consoleModeIn = consoleModeIn | ENABLE_LINE_INPUT;
SetConsoleMode(handle_in, consoleModeIn);
2020-05-04 22:10:01 +02:00
freopen_s(&hf_in, "CONIN$", "r", stdin);
HANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
int SystemOutput = _open_osfhandle(intptr_t(ConsoleOutput), _O_TEXT);
FILE* COutputHandle = _fdopen(SystemOutput, "w");
freopen_s(&COutputHandle, "CONOUT$", "w", stdout);
LPTHREAD_START_ROUTINE readConsoleLoop = [](LPVOID params) -> DWORD {
DWORD read;
CHAR buffer[4096];
while (true)
{
BOOL success = ReadFile(params, &buffer, 4096, &read, NULL);
if (success && read > 2)
{
//Only send the actual written message minus \r\n
string msg(buffer, read-2);
SendMessage(WindowsHandle, WM_USER, (WPARAM)&msg, NULL);
}
};
return 0;
};
CreateThread(NULL, 0, readConsoleLoop, handle_in, 0, &DebugConsoleThreadID);
}
SetCursor(NULL);
ShowCursor(FALSE);
hAccTable = LoadAccelerators(hInstance, (LPCSTR)0x65);
2018-08-19 09:46:58 +02:00
2019-01-13 21:57:16 +01:00
//g_Renderer->Test();
2018-08-19 09:46:58 +02:00
SoundActive = false;
DoTheGame = true;
ThreadEnded = false;
ThreadHandle = BeginThread(GameMain, ThreadID);
2018-08-19 09:46:58 +02:00
WinProcMsg();
ThreadEnded = true;
2018-08-19 09:46:58 +02:00
while (DoTheGame);
WinClose();
exit(EXIT_SUCCESS);
2018-08-19 09:46:58 +02:00
}
void WinClose()
2018-08-19 09:46:58 +02:00
{
DestroyAcceleratorTable(hAccTable);
if (g_Configuration.EnableSound)
Sound_DeInit();
2018-08-19 09:46:58 +02:00
2020-04-28 12:24:10 -03:00
delete g_GameScript;
delete g_GameFlow;
2018-08-19 09:46:58 +02:00
2018-10-09 00:02:14 +02:00
SaveGame::End();
}