From 12f4fa44b85ba314da84d680046362f8484c0e1c Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 05:11:54 +0100 Subject: [PATCH 01/16] Add const in codec.cpp --- Source/codec.cpp | 6 +++--- Source/codec.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/codec.cpp b/Source/codec.cpp index 3b232c6b9..b7698145b 100644 --- a/Source/codec.cpp +++ b/Source/codec.cpp @@ -14,7 +14,7 @@ typedef struct CodecSignature { #define BLOCKSIZE 64 -static void codec_init_key(int unused, char *pszPassword) +static void codec_init_key(int unused, const char *pszPassword) { int i, ch, n; char key[136]; // last 64 bytes are the SHA1 @@ -50,7 +50,7 @@ static void codec_init_key(int unused, char *pszPassword) memset(key, 0, sizeof(key)); } -int codec_decode(BYTE *pbSrcDst, DWORD size, char *pszPassword) +int codec_decode(BYTE *pbSrcDst, DWORD size, const char *pszPassword) { char buf[128]; char dst[SHA1HashSize]; @@ -101,7 +101,7 @@ DWORD codec_get_encoded_len(DWORD dwSrcBytes) return dwSrcBytes + sizeof(CodecSignature); } -void codec_encode(BYTE *pbSrcDst, DWORD size, int size_64, char *pszPassword) +void codec_encode(BYTE *pbSrcDst, DWORD size, int size_64, const char *pszPassword) { char buf[128]; char tmp[SHA1HashSize]; diff --git a/Source/codec.h b/Source/codec.h index 4a3175783..2325500a8 100644 --- a/Source/codec.h +++ b/Source/codec.h @@ -6,8 +6,8 @@ #ifndef __CODEC_H__ #define __CODEC_H__ -int codec_decode(BYTE *pbSrcDst, DWORD size, char *pszPassword); +int codec_decode(BYTE *pbSrcDst, DWORD size, const char *pszPassword); DWORD codec_get_encoded_len(DWORD dwSrcBytes); -void codec_encode(BYTE *pbSrcDst, DWORD size, int size_64, char *pszPassword); +void codec_encode(BYTE *pbSrcDst, DWORD size, int size_64, const char *pszPassword); #endif /* __CODEC_H__ */ From 40350a201b2a426a3fbf0e704c5b2d9e6cce10ef Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 05:12:57 +0100 Subject: [PATCH 02/16] Reorder diablo.cpp --- Source/diablo.cpp | 1206 ++++++++++++++++++++++----------------------- Source/diablo.h | 51 -- 2 files changed, 603 insertions(+), 654 deletions(-) diff --git a/Source/diablo.cpp b/Source/diablo.cpp index d6ddf48ed..594271dc6 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -93,310 +93,7 @@ const char *const spszMsgTbl[4] = { /** INI files variable names for quick message keys */ const char *const spszMsgHotKeyTbl[4] = { "F9", "F10", "F11", "F12" }; -void FreeGameMem() -{ - music_stop(); - - MemFreeDbg(pDungeonCels); - MemFreeDbg(pMegaTiles); - MemFreeDbg(pLevelPieces); - MemFreeDbg(pSpecialCels); - MemFreeDbg(pSpeedCels); - - FreeMissiles(); - FreeMonsters(); - FreeObjectGFX(); - FreeMonsterSnd(); - FreeTownerGFX(); -} - -BOOL StartGame(BOOL bNewGame, BOOL bSinglePlayer) -{ - BOOL fExitProgram; - unsigned int uMsg; - - gbSelectProvider = TRUE; - - do { - fExitProgram = FALSE; -#ifndef HELLFIRE - gbLoadGame = FALSE; -#endif - - if (!NetInit(bSinglePlayer, &fExitProgram)) { - gbRunGameResult = !fExitProgram; - break; - } - - gbSelectProvider = FALSE; - - if (bNewGame || !gbValidSaveFile) { - InitLevels(); - InitQuests(); - InitPortals(); - InitDungMsgs(myplr); -#ifndef HELLFIRE - } - if (!gbValidSaveFile || !gbLoadGame) { -#else - if (!gbValidSaveFile && gbLoadGame) - inv_diablo_to_hellfire(myplr); -#endif - uMsg = WM_DIABNEWGAME; - } else { - uMsg = WM_DIABLOADGAME; - } - run_game_loop(uMsg); - NetClose(); -#ifndef HELLFIRE - pfile_create_player_description(0, 0); - } while (gbRunGameResult); -#else - } while (gbMaxPlayers == 1 || !gbRunGameResult); -#endif - - SNetDestroy(); - return gbRunGameResult; -} - -void run_game_loop(unsigned int uMsg) -{ - BOOL bLoop; - WNDPROC saveProc; - MSG msg; - - nthread_ignore_mutex(TRUE); - start_game(uMsg); - assert(ghMainWnd); - saveProc = SetWindowProc(GM_Game); - control_update_life_mana(); - run_delta_info(); - gbRunGame = TRUE; - gbProcessPlayers = TRUE; - gbRunGameResult = TRUE; - force_redraw = 255; - DrawAndBlit(); - PaletteFadeIn(8); - force_redraw = 255; - gbGameLoopStartup = TRUE; - nthread_ignore_mutex(FALSE); - - while (gbRunGame) { - diablo_color_cyc_logic(); - if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { - SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL); - while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { - if (msg.message == WM_QUIT) { - gbRunGameResult = FALSE; - gbRunGame = FALSE; - break; - } - TranslateMessage(&msg); - DispatchMessage(&msg); - } - bLoop = gbRunGame && nthread_has_500ms_passed(FALSE); - SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL); - if (!bLoop) { - continue; - } - } else if (!nthread_has_500ms_passed(FALSE)) { -#ifdef SLEEPFIX - Sleep(1); -#endif - continue; - } - multi_process_network_packets(); - game_loop(gbGameLoopStartup); -#ifndef HELLFIRE - msgcmd_send_chat(); -#endif - gbGameLoopStartup = FALSE; - DrawAndBlit(); - } - - if (gbMaxPlayers > 1) { - pfile_write_hero(); - } - - pfile_flush_W(); - PaletteFadeOut(8); - SetCursor_(CURSOR_NONE); - ClearScreenBuffer(); - force_redraw = 255; - scrollrt_draw_game_screen(TRUE); - saveProc = SetWindowProc(saveProc); - assert(saveProc == GM_Game); - free_game(); - - if (cineflag) { - cineflag = FALSE; - DoEnding(); - } -} - -void start_game(unsigned int uMsg) -{ - zoomflag = TRUE; - cineflag = FALSE; - InitCursor(); - InitLightTable(); - LoadDebugGFX(); - assert(ghMainWnd); - music_stop(); - ShowProgress(uMsg); - gmenu_init_menu(); - InitLevelCursor(); - sgnTimeoutCurs = CURSOR_NONE; - sgbMouseDown = 0; - track_repeat_walk(FALSE); -} - -void free_game() -{ - int i; - - FreeControlPan(); - FreeInvGFX(); - FreeGMenu(); - FreeQuestText(); - FreeStoreMem(); - - for (i = 0; i < MAX_PLRS; i++) - FreePlayerGFX(i); - - FreeItemGFX(); - FreeCursor(); - FreeLightTable(); - FreeDebugGFX(); - FreeGameMem(); -} - -BOOL diablo_get_not_running() -{ - SetLastError(0); - CreateEvent(NULL, FALSE, FALSE, "DiabloEvent"); -#ifdef HELLFIRE - CreateEvent(NULL, FALSE, FALSE, "HellfireEvent"); -#endif - return GetLastError() != ERROR_ALREADY_EXISTS; -} - -/** - * @brief Main entry point, check env, initialize systesm, play intros, start main menu, shut down - * @param hInstance A handle to the current instance of the application. - * @param hPrevInstance Always null - * @param lpCmdLine The command line for the application - * @param nCmdShow Initial window state - */ -int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) -{ - HINSTANCE hInst; - int nData; - char szFileName[MAX_PATH]; - BOOL bNoEvent; -#ifdef HELLFIRE - char content[256]; - FILE *file; -#endif - - hInst = hInstance; -#ifndef DEBUGGER - diablo_reload_process(hInstance); -#endif - ghInst = hInst; - -#ifndef HELLFIRE - if (RestrictedTest()) - ErrOkDlg(IDD_DIALOG10, 0, "C:\\Src\\Diablo\\Source\\DIABLO.CPP", 877); - if (ReadOnlyTest()) { - if (!GetModuleFileName(ghInst, szFileName, sizeof(szFileName))) - szFileName[0] = '\0'; - DirErrorDlg(szFileName); - } -#endif - - ShowCursor(FALSE); - srand(GetTickCount()); - InitHash(); -#ifdef HELLFIRE - alloc_plr(); - lpTopLevelExceptionFilter = SetUnhandledExceptionFilter(diablo_TopLevelExceptionFilter); -#else - fault_get_filter(); -#endif - - bNoEvent = diablo_get_not_running(); -#ifdef HELLFIRE - if (diablo_find_window("DIABLO")) - return 0; -#endif - if (diablo_find_window(GAME_NAME) || !bNoEvent) - return 0; - -#ifdef _DEBUG - SFileEnableDirectAccess(TRUE); -#endif - diablo_init_screen(); -#ifdef HELLFIRE - if (lpCmdLine[0] == '\0') { - content[0] = '\0'; - if (file = fopen("command.txt", "r")) { - fgets(content, sizeof(content) / sizeof(char), file); - lpCmdLine = content; - fclose(file); - } - } -#endif - diablo_parse_flags(lpCmdLine); - init_create_window(nCmdShow); - ui_sound_init(); - UiInitialize(); -#ifdef SPAWN - UiSetSpawned(TRUE); -#endif - -#ifdef _DEBUG - if (showintrodebug) -#endif - play_movie("gendata\\logo.smk", TRUE); - -#ifndef SPAWN - { - char szValueName[] = "Intro"; - if (!SRegLoadValue(APP_NAME, szValueName, 0, &nData)) - nData = 1; -#ifndef HELLFIRE - if (nData) - play_movie("gendata\\diablo1.smk", TRUE); -#else - play_movie("gendata\\Hellfire.smk", TRUE); -#endif - SRegSaveValue(APP_NAME, szValueName, 0, 0); - } -#endif - -#ifdef _DEBUG - if (showintrodebug) { -#endif - UiTitleDialog(7); - BlackPalette(); -#ifdef _DEBUG - } -#endif - - mainmenu_loop(); - UiDestroy(); - SaveGamma(); - - if (ghMainWnd) { - Sleep(300); - DestroyWindow(ghMainWnd); - } - - return 0; -} - -void diablo_parse_flags(char *args) +static void diablo_parse_flags(char *args) { char c; #ifdef _DEBUG @@ -591,7 +288,185 @@ void diablo_parse_flags(char *args) } } -void diablo_init_screen() +void FreeGameMem() +{ + music_stop(); + + MemFreeDbg(pDungeonCels); + MemFreeDbg(pMegaTiles); + MemFreeDbg(pLevelPieces); + MemFreeDbg(pSpecialCels); + MemFreeDbg(pSpeedCels); + + FreeMissiles(); + FreeMonsters(); + FreeObjectGFX(); + FreeMonsterSnd(); + FreeTownerGFX(); +} + +static void start_game(unsigned int uMsg) +{ + zoomflag = TRUE; + cineflag = FALSE; + InitCursor(); + InitLightTable(); + LoadDebugGFX(); + assert(ghMainWnd); + music_stop(); + ShowProgress(uMsg); + gmenu_init_menu(); + InitLevelCursor(); + sgnTimeoutCurs = CURSOR_NONE; + sgbMouseDown = 0; + track_repeat_walk(FALSE); +} + +static void free_game() +{ + int i; + + FreeControlPan(); + FreeInvGFX(); + FreeGMenu(); + FreeQuestText(); + FreeStoreMem(); + + for (i = 0; i < MAX_PLRS; i++) + FreePlayerGFX(i); + + FreeItemGFX(); + FreeCursor(); + FreeLightTable(); + FreeDebugGFX(); + FreeGameMem(); +} + +static void run_game_loop(unsigned int uMsg) +{ + BOOL bLoop; + WNDPROC saveProc; + MSG msg; + + nthread_ignore_mutex(TRUE); + start_game(uMsg); + assert(ghMainWnd); + saveProc = SetWindowProc(GM_Game); + control_update_life_mana(); + run_delta_info(); + gbRunGame = TRUE; + gbProcessPlayers = TRUE; + gbRunGameResult = TRUE; + force_redraw = 255; + DrawAndBlit(); + PaletteFadeIn(8); + force_redraw = 255; + gbGameLoopStartup = TRUE; + nthread_ignore_mutex(FALSE); + + while (gbRunGame) { + diablo_color_cyc_logic(); + if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) { + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL); + while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { + if (msg.message == WM_QUIT) { + gbRunGameResult = FALSE; + gbRunGame = FALSE; + break; + } + TranslateMessage(&msg); + DispatchMessage(&msg); + } + bLoop = gbRunGame && nthread_has_500ms_passed(FALSE); + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL); + if (!bLoop) { + continue; + } + } else if (!nthread_has_500ms_passed(FALSE)) { +#ifdef SLEEPFIX + Sleep(1); +#endif + continue; + } + multi_process_network_packets(); + game_loop(gbGameLoopStartup); +#ifndef HELLFIRE + msgcmd_send_chat(); +#endif + gbGameLoopStartup = FALSE; + DrawAndBlit(); + } + + if (gbMaxPlayers > 1) { + pfile_write_hero(); + } + + pfile_flush_W(); + PaletteFadeOut(8); + SetCursor_(CURSOR_NONE); + ClearScreenBuffer(); + force_redraw = 255; + scrollrt_draw_game_screen(TRUE); + saveProc = SetWindowProc(saveProc); + assert(saveProc == GM_Game); + free_game(); + + if (cineflag) { + cineflag = FALSE; + DoEnding(); + } +} + +BOOL StartGame(BOOL bNewGame, BOOL bSinglePlayer) +{ + BOOL fExitProgram; + unsigned int uMsg; + + gbSelectProvider = TRUE; + + do { + fExitProgram = FALSE; +#ifndef HELLFIRE + gbLoadGame = FALSE; +#endif + + if (!NetInit(bSinglePlayer, &fExitProgram)) { + gbRunGameResult = !fExitProgram; + break; + } + + gbSelectProvider = FALSE; + + if (bNewGame || !gbValidSaveFile) { + InitLevels(); + InitQuests(); + InitPortals(); + InitDungMsgs(myplr); +#ifndef HELLFIRE + } + if (!gbValidSaveFile || !gbLoadGame) { +#else + if (!gbValidSaveFile && gbLoadGame) + inv_diablo_to_hellfire(myplr); +#endif + uMsg = WM_DIABNEWGAME; + } else { + uMsg = WM_DIABLOADGAME; + } + run_game_loop(uMsg); + NetClose(); +#ifndef HELLFIRE + pfile_create_player_description(0, 0); + } while (gbRunGameResult); +#else + } while (gbMaxPlayers == 1 || !gbRunGameResult); +#endif + + SNetDestroy(); + return gbRunGameResult; +} + +static void diablo_init_screen() { int i; @@ -610,7 +485,7 @@ void diablo_init_screen() } #ifdef HELLFIRE -LONG __stdcall diablo_TopLevelExceptionFilter(PEXCEPTION_POINTERS pExc) +static LONG __stdcall diablo_TopLevelExceptionFilter(PEXCEPTION_POINTERS pExc) { dx_cleanup(); init_cleanup(FALSE); @@ -621,7 +496,17 @@ LONG __stdcall diablo_TopLevelExceptionFilter(PEXCEPTION_POINTERS pExc) } #endif -BOOL diablo_find_window(LPCSTR lpClassName) +BOOL diablo_get_not_running() +{ + SetLastError(0); + CreateEvent(NULL, FALSE, FALSE, "DiabloEvent"); +#ifdef HELLFIRE + CreateEvent(NULL, FALSE, FALSE, "HellfireEvent"); +#endif + return GetLastError() != ERROR_ALREADY_EXISTS; +} + +static BOOL diablo_find_window(LPCSTR lpClassName) { HWND hWnd, active; @@ -643,7 +528,7 @@ BOOL diablo_find_window(LPCSTR lpClassName) return TRUE; } -void diablo_reload_process(HINSTANCE hInstance) +static void diablo_reload_process(HINSTANCE hInstance) { DWORD dwSize, dwProcessId; BOOL bNoExist; @@ -727,244 +612,122 @@ void diablo_reload_process(HINSTANCE hInstance) } } -BOOL PressEscKey() -{ - BOOL rv = FALSE; - - if (doomflag) { - doom_close(); - rv = TRUE; - } - if (helpflag) { - helpflag = FALSE; - rv = TRUE; - } - - if (qtextflag) { - qtextflag = FALSE; - stream_stop(); - rv = TRUE; - } else if (stextflag) { - STextESC(); - rv = TRUE; - } - - if (msgflag) { - msgdelay = 0; - rv = TRUE; - } - if (talkflag) { - control_reset_talk(); - rv = TRUE; - } - if (dropGoldFlag) { - control_drop_gold(VK_ESCAPE); - rv = TRUE; - } - if (spselflag) { - spselflag = FALSE; - rv = TRUE; - } - - return rv; -} - -LRESULT CALLBACK DisableInputWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) +/** + * @brief Main entry point, check env, initialize systesm, play intros, start main menu, shut down + * @param hInstance A handle to the current instance of the application. + * @param hPrevInstance Always null + * @param lpCmdLine The command line for the application + * @param nCmdShow Initial window state + */ +int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { - switch (uMsg) { - case WM_KEYDOWN: - case WM_KEYUP: - case WM_CHAR: - case WM_SYSKEYDOWN: - case WM_SYSCOMMAND: - case WM_MOUSEMOVE: - return 0; - case WM_LBUTTONDOWN: - if (sgbMouseDown != 0) - return 0; - sgbMouseDown = 1; - SetCapture(hWnd); - return 0; - case WM_LBUTTONUP: - if (sgbMouseDown != 1) - return 0; - sgbMouseDown = 0; - ReleaseCapture(); - return 0; - case WM_RBUTTONDOWN: - if (sgbMouseDown != 0) - return 0; - sgbMouseDown = 2; - SetCapture(hWnd); - return 0; - case WM_RBUTTONUP: - if (sgbMouseDown != 2) - return 0; - sgbMouseDown = 0; - ReleaseCapture(); - return 0; - case WM_CAPTURECHANGED: - if (hWnd == (HWND)lParam) - return 0; - sgbMouseDown = 0; - return 0; - } + HINSTANCE hInst; + int nData; + char szFileName[MAX_PATH]; + BOOL bNoEvent; +#ifdef HELLFIRE + char content[256]; + FILE *file; +#endif - return MainWndProc(hWnd, uMsg, wParam, lParam); -} + hInst = hInstance; +#ifndef DEBUGGER + diablo_reload_process(hInstance); +#endif + ghInst = hInst; -LRESULT CALLBACK GM_Game(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) -{ - switch (uMsg) { - case WM_KEYDOWN: - PressKey(wParam); - return 0; - case WM_KEYUP: - ReleaseKey(wParam); - return 0; - case WM_CHAR: - PressChar(wParam); - return 0; - case WM_SYSKEYDOWN: - if (PressSysKey(wParam)) - return 0; - break; - case WM_SYSCOMMAND: - if (wParam == SC_CLOSE) { - gbRunGame = FALSE; - gbRunGameResult = FALSE; - return 0; - } - break; - case WM_MOUSEMOVE: - MouseX = LOWORD(lParam); // BUGFIX (short)LOWORD coords are signed - MouseY = HIWORD(lParam); // BUGFIX (short)HIWORD coords are signed - gmenu_on_mouse_move(); - return 0; - case WM_LBUTTONDOWN: - MouseX = LOWORD(lParam); // BUGFIX (short)LOWORD coords are signed - MouseY = HIWORD(lParam); // BUGFIX (short)HIWORD coords are signed - if (sgbMouseDown == 0) { - sgbMouseDown = 1; - SetCapture(hWnd); - track_repeat_walk(LeftMouseDown(wParam)); - } - return 0; - case WM_LBUTTONUP: - MouseX = LOWORD(lParam); // BUGFIX (short)LOWORD coords are signed - MouseY = HIWORD(lParam); // BUGFIX (short)HIWORD coords are signed - if (sgbMouseDown == 1) { - sgbMouseDown = 0; - LeftMouseUp(); - track_repeat_walk(FALSE); - ReleaseCapture(); - } - return 0; - case WM_RBUTTONDOWN: - MouseX = LOWORD(lParam); // BUGFIX (short)LOWORD coords are signed - MouseY = HIWORD(lParam); // BUGFIX (short)HIWORD coords are signed - if (sgbMouseDown == 0) { - sgbMouseDown = 2; - SetCapture(hWnd); - RightMouseDown(); - } +#ifndef HELLFIRE + if (RestrictedTest()) + ErrOkDlg(IDD_DIALOG10, 0, "C:\\Src\\Diablo\\Source\\DIABLO.CPP", 877); + if (ReadOnlyTest()) { + if (!GetModuleFileName(ghInst, szFileName, sizeof(szFileName))) + szFileName[0] = '\0'; + DirErrorDlg(szFileName); + } +#endif + + ShowCursor(FALSE); + srand(GetTickCount()); + InitHash(); +#ifdef HELLFIRE + alloc_plr(); + lpTopLevelExceptionFilter = SetUnhandledExceptionFilter(diablo_TopLevelExceptionFilter); +#else + fault_get_filter(); +#endif + + bNoEvent = diablo_get_not_running(); +#ifdef HELLFIRE + if (diablo_find_window("DIABLO")) return 0; - case WM_RBUTTONUP: - MouseX = LOWORD(lParam); // BUGFIX (short)LOWORD coords are signed - MouseY = HIWORD(lParam); // BUGFIX (short)HIWORD coords are signed - if (sgbMouseDown == 2) { - sgbMouseDown = 0; - ReleaseCapture(); - } +#endif + if (diablo_find_window(GAME_NAME) || !bNoEvent) return 0; - case WM_CAPTURECHANGED: - if (hWnd != (HWND)lParam) { - sgbMouseDown = 0; - track_repeat_walk(FALSE); + +#ifdef _DEBUG + SFileEnableDirectAccess(TRUE); +#endif + diablo_init_screen(); +#ifdef HELLFIRE + if (lpCmdLine[0] == '\0') { + content[0] = '\0'; + if (file = fopen("command.txt", "r")) { + fgets(content, sizeof(content) / sizeof(char), file); + lpCmdLine = content; + fclose(file); } - break; - case WM_DIABNEXTLVL: - case WM_DIABPREVLVL: - case WM_DIABRTNLVL: - case WM_DIABSETLVL: - case WM_DIABWARPLVL: - case WM_DIABTOWNWARP: - case WM_DIABTWARPUP: - case WM_DIABRETOWN: - if (gbMaxPlayers > 1) - pfile_write_hero(); - nthread_ignore_mutex(TRUE); - PaletteFadeOut(8); - sound_stop(); - music_stop(); - track_repeat_walk(FALSE); - sgbMouseDown = 0; - ReleaseCapture(); - ShowProgress(uMsg); - force_redraw = 255; - DrawAndBlit(); - if (gbRunGame) - PaletteFadeIn(8); - nthread_ignore_mutex(FALSE); - gbGameLoopStartup = TRUE; - return 0; } +#endif + diablo_parse_flags(lpCmdLine); + init_create_window(nCmdShow); + ui_sound_init(); + UiInitialize(); +#ifdef SPAWN + UiSetSpawned(TRUE); +#endif - return MainWndProc(hWnd, uMsg, wParam, lParam); -} +#ifdef _DEBUG + if (showintrodebug) +#endif + play_movie("gendata\\logo.smk", TRUE); -BOOL LeftMouseDown(int wParam) -{ - if (!gmenu_left_mouse(TRUE) && !control_check_talk_btn() && sgnTimeoutCurs == CURSOR_NONE) { - if (deathflag) { - control_check_btn_press(); - } else if (PauseMode != 2) { - if (doomflag) { - doom_close(); - } else if (spselflag) { - SetSpell(); - } else if (stextflag != STORE_NONE) { - CheckStoreBtn(); - } else if (MouseY < PANEL_TOP) { - if (!gmenu_is_active() && !TryIconCurs()) { - if (questlog && MouseX > 32 && MouseX < 288 && MouseY > 32 && MouseY < 308) { - QuestlogESC(); - } else if (qtextflag) { - qtextflag = FALSE; - stream_stop(); - } else if (chrflag && MouseX < SPANEL_WIDTH) { - CheckChrBtns(); - } else if (invflag && MouseX > RIGHT_PANEL) { - if (!dropGoldFlag) - CheckInvItem(); - } else if (sbookflag && MouseX > RIGHT_PANEL) { - CheckSBook(); - } else if (pcurs >= CURSOR_FIRSTITEM) { - if (TryInvPut()) { - NetSendCmdPItem(TRUE, CMD_PUTITEM, cursmx, cursmy); - NewCursor(CURSOR_HAND); - } - } else { - if (plr[myplr]._pStatPts != 0 && !spselflag) - CheckLvlBtn(); - if (!lvlbtndown) - return LeftMouseCmd(wParam == MK_SHIFT + MK_LBUTTON); - } - } - } else { - if (!talkflag && !dropGoldFlag && !gmenu_is_active()) - CheckInvScrn(); - DoPanBtn(); - if (pcurs > CURSOR_HAND && pcurs < CURSOR_FIRSTITEM) - NewCursor(CURSOR_HAND); - } - } +#ifndef SPAWN + { + char szValueName[] = "Intro"; + if (!SRegLoadValue(APP_NAME, szValueName, 0, &nData)) + nData = 1; +#ifndef HELLFIRE + if (nData) + play_movie("gendata\\diablo1.smk", TRUE); +#else + play_movie("gendata\\Hellfire.smk", TRUE); +#endif + SRegSaveValue(APP_NAME, szValueName, 0, 0); } +#endif - return FALSE; +#ifdef _DEBUG + if (showintrodebug) { +#endif + UiTitleDialog(7); + BlackPalette(); +#ifdef _DEBUG + } +#endif + + mainmenu_loop(); + UiDestroy(); + SaveGamma(); + + if (ghMainWnd) { + Sleep(300); + DestroyWindow(ghMainWnd); + } + + return 0; } -BOOL LeftMouseCmd(BOOL bShift) +static BOOL LeftMouseCmd(BOOL bShift) { BOOL bNear; @@ -1019,7 +782,7 @@ BOOL LeftMouseCmd(BOOL bShift) return FALSE; } -BOOL TryIconCurs() +static BOOL TryIconCurs() { if (pcurs == CURSOR_RESURRECT) { NetSendCmdParam1(TRUE, CMD_RESURRECT, pcursplr); @@ -1078,7 +841,58 @@ BOOL TryIconCurs() return FALSE; } -void LeftMouseUp() +static BOOL LeftMouseDown(int wParam) +{ + if (!gmenu_left_mouse(TRUE) && !control_check_talk_btn() && sgnTimeoutCurs == CURSOR_NONE) { + if (deathflag) { + control_check_btn_press(); + } else if (PauseMode != 2) { + if (doomflag) { + doom_close(); + } else if (spselflag) { + SetSpell(); + } else if (stextflag != STORE_NONE) { + CheckStoreBtn(); + } else if (MouseY < PANEL_TOP) { + if (!gmenu_is_active() && !TryIconCurs()) { + if (questlog && MouseX > 32 && MouseX < 288 && MouseY > 32 && MouseY < 308) { + QuestlogESC(); + } else if (qtextflag) { + qtextflag = FALSE; + stream_stop(); + } else if (chrflag && MouseX < SPANEL_WIDTH) { + CheckChrBtns(); + } else if (invflag && MouseX > RIGHT_PANEL) { + if (!dropGoldFlag) + CheckInvItem(); + } else if (sbookflag && MouseX > RIGHT_PANEL) { + CheckSBook(); + } else if (pcurs >= CURSOR_FIRSTITEM) { + if (TryInvPut()) { + NetSendCmdPItem(TRUE, CMD_PUTITEM, cursmx, cursmy); + NewCursor(CURSOR_HAND); + } + } else { + if (plr[myplr]._pStatPts != 0 && !spselflag) + CheckLvlBtn(); + if (!lvlbtndown) + return LeftMouseCmd(wParam == MK_SHIFT + MK_LBUTTON); + } + } + } else { + if (!talkflag && !dropGoldFlag && !gmenu_is_active()) + CheckInvScrn(); + DoPanBtn(); + if (pcurs > CURSOR_HAND && pcurs < CURSOR_FIRSTITEM) + NewCursor(CURSOR_HAND); + } + } + } + + return FALSE; +} + +static void LeftMouseUp() { gmenu_left_mouse(FALSE); control_release_talk_btn(); @@ -1092,7 +906,7 @@ void LeftMouseUp() ReleaseStoreBtn(); } -void RightMouseDown() +static void RightMouseDown() { if (!gmenu_is_active() && sgnTimeoutCurs == CURSOR_NONE && PauseMode != 2 && !plr[myplr]._pInvincible) { if (doomflag) { @@ -1119,15 +933,21 @@ void RightMouseDown() } } -BOOL PressSysKey(int wParam) +static void diablo_pause_game() { - if (gmenu_is_active() || wParam != VK_F10) - return FALSE; - diablo_hotkey_msg(1); - return TRUE; + if (gbMaxPlayers <= 1) { + if (PauseMode) { + PauseMode = 0; + } else { + PauseMode = 2; + sound_stop(); + track_repeat_walk(FALSE); + } + force_redraw = 255; + } } -void diablo_hotkey_msg(DWORD dwMsg) +static void diablo_hotkey_msg(DWORD dwMsg) { char *s; char szFileName[MAX_PATH]; @@ -1140,24 +960,74 @@ void diablo_hotkey_msg(DWORD dwMsg) app_fatal("Can't get program name"); } - s = strrchr(szFileName, '\\'); - if (s != NULL) { - *s = '\0'; + s = strrchr(szFileName, '\\'); + if (s != NULL) { + *s = '\0'; + } + + strcat(szFileName, "\\Diablo.ini"); + assert(dwMsg < sizeof(spszMsgTbl) / sizeof(spszMsgTbl[0])); + GetPrivateProfileString("NetMsg", spszMsgHotKeyTbl[dwMsg], spszMsgTbl[dwMsg], szMsg, sizeof(szMsg), szFileName); + NetSendCmdString(-1, szMsg); +} + +static BOOL PressSysKey(int wParam) +{ + if (gmenu_is_active() || wParam != VK_F10) + return FALSE; + diablo_hotkey_msg(1); + return TRUE; +} + +static void ReleaseKey(int vkey) +{ + if (vkey == VK_SNAPSHOT) + CaptureScreen(); +} + +BOOL PressEscKey() +{ + BOOL rv = FALSE; + + if (doomflag) { + doom_close(); + rv = TRUE; + } + if (helpflag) { + helpflag = FALSE; + rv = TRUE; + } + + if (qtextflag) { + qtextflag = FALSE; + stream_stop(); + rv = TRUE; + } else if (stextflag) { + STextESC(); + rv = TRUE; } - strcat(szFileName, "\\Diablo.ini"); - assert(dwMsg < sizeof(spszMsgTbl) / sizeof(spszMsgTbl[0])); - GetPrivateProfileString("NetMsg", spszMsgHotKeyTbl[dwMsg], spszMsgTbl[dwMsg], szMsg, sizeof(szMsg), szFileName); - NetSendCmdString(-1, szMsg); -} + if (msgflag) { + msgdelay = 0; + rv = TRUE; + } + if (talkflag) { + control_reset_talk(); + rv = TRUE; + } + if (dropGoldFlag) { + control_drop_gold(VK_ESCAPE); + rv = TRUE; + } + if (spselflag) { + spselflag = FALSE; + rv = TRUE; + } -void ReleaseKey(int vkey) -{ - if (vkey == VK_SNAPSHOT) - CaptureScreen(); + return rv; } -void PressKey(int vkey) +static void PressKey(int vkey) { if (gmenu_presskeys(vkey) || control_presskeys(vkey)) { return; @@ -1360,24 +1230,10 @@ void PressKey(int vkey) } } -void diablo_pause_game() -{ - if (gbMaxPlayers <= 1) { - if (PauseMode) { - PauseMode = 0; - } else { - PauseMode = 2; - sound_stop(); - track_repeat_walk(FALSE); - } - force_redraw = 255; - } -} - /** * @internal `return` must be used instead of `break` to be bin exact as C++ */ -void PressChar(int vkey) +static void PressChar(int vkey) { if (gmenu_is_active() || control_talk_last_key(vkey) || sgnTimeoutCurs != CURSOR_NONE || deathflag) { return; @@ -1652,6 +1508,150 @@ void PressChar(int vkey) } } +LRESULT CALLBACK DisableInputWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + switch (uMsg) { + case WM_KEYDOWN: + case WM_KEYUP: + case WM_CHAR: + case WM_SYSKEYDOWN: + case WM_SYSCOMMAND: + case WM_MOUSEMOVE: + return 0; + case WM_LBUTTONDOWN: + if (sgbMouseDown != 0) + return 0; + sgbMouseDown = 1; + SetCapture(hWnd); + return 0; + case WM_LBUTTONUP: + if (sgbMouseDown != 1) + return 0; + sgbMouseDown = 0; + ReleaseCapture(); + return 0; + case WM_RBUTTONDOWN: + if (sgbMouseDown != 0) + return 0; + sgbMouseDown = 2; + SetCapture(hWnd); + return 0; + case WM_RBUTTONUP: + if (sgbMouseDown != 2) + return 0; + sgbMouseDown = 0; + ReleaseCapture(); + return 0; + case WM_CAPTURECHANGED: + if (hWnd == (HWND)lParam) + return 0; + sgbMouseDown = 0; + return 0; + } + + return MainWndProc(hWnd, uMsg, wParam, lParam); +} + +LRESULT CALLBACK GM_Game(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + switch (uMsg) { + case WM_KEYDOWN: + PressKey(wParam); + return 0; + case WM_KEYUP: + ReleaseKey(wParam); + return 0; + case WM_CHAR: + PressChar(wParam); + return 0; + case WM_SYSKEYDOWN: + if (PressSysKey(wParam)) + return 0; + break; + case WM_SYSCOMMAND: + if (wParam == SC_CLOSE) { + gbRunGame = FALSE; + gbRunGameResult = FALSE; + return 0; + } + break; + case WM_MOUSEMOVE: + MouseX = LOWORD(lParam); // BUGFIX (short)LOWORD coords are signed + MouseY = HIWORD(lParam); // BUGFIX (short)HIWORD coords are signed + gmenu_on_mouse_move(); + return 0; + case WM_LBUTTONDOWN: + MouseX = LOWORD(lParam); // BUGFIX (short)LOWORD coords are signed + MouseY = HIWORD(lParam); // BUGFIX (short)HIWORD coords are signed + if (sgbMouseDown == 0) { + sgbMouseDown = 1; + SetCapture(hWnd); + track_repeat_walk(LeftMouseDown(wParam)); + } + return 0; + case WM_LBUTTONUP: + MouseX = LOWORD(lParam); // BUGFIX (short)LOWORD coords are signed + MouseY = HIWORD(lParam); // BUGFIX (short)HIWORD coords are signed + if (sgbMouseDown == 1) { + sgbMouseDown = 0; + LeftMouseUp(); + track_repeat_walk(FALSE); + ReleaseCapture(); + } + return 0; + case WM_RBUTTONDOWN: + MouseX = LOWORD(lParam); // BUGFIX (short)LOWORD coords are signed + MouseY = HIWORD(lParam); // BUGFIX (short)HIWORD coords are signed + if (sgbMouseDown == 0) { + sgbMouseDown = 2; + SetCapture(hWnd); + RightMouseDown(); + } + return 0; + case WM_RBUTTONUP: + MouseX = LOWORD(lParam); // BUGFIX (short)LOWORD coords are signed + MouseY = HIWORD(lParam); // BUGFIX (short)HIWORD coords are signed + if (sgbMouseDown == 2) { + sgbMouseDown = 0; + ReleaseCapture(); + } + return 0; + case WM_CAPTURECHANGED: + if (hWnd != (HWND)lParam) { + sgbMouseDown = 0; + track_repeat_walk(FALSE); + } + break; + case WM_DIABNEXTLVL: + case WM_DIABPREVLVL: + case WM_DIABRTNLVL: + case WM_DIABSETLVL: + case WM_DIABWARPLVL: + case WM_DIABTOWNWARP: + case WM_DIABTWARPUP: + case WM_DIABRETOWN: + if (gbMaxPlayers > 1) + pfile_write_hero(); + nthread_ignore_mutex(TRUE); + PaletteFadeOut(8); + sound_stop(); + music_stop(); + track_repeat_walk(FALSE); + sgbMouseDown = 0; + ReleaseCapture(); + ShowProgress(uMsg); + force_redraw = 255; + DrawAndBlit(); + if (gbRunGame) + PaletteFadeIn(8); + nthread_ignore_mutex(FALSE); + gbGameLoopStartup = TRUE; + return 0; + } + + return MainWndProc(hWnd, uMsg, wParam, lParam); +} + void LoadLvlGFX() { assert(! pDungeonCels); @@ -2035,26 +2035,7 @@ void LoadGameLevel(BOOL firstflag, int lvldir) #endif } -void game_loop(BOOL bStartup) -{ - int i; - - i = bStartup ? 60 : 3; - - while (i--) { - if (!multi_handle_delta()) { - timeout_cursor(TRUE); - break; - } else { - timeout_cursor(FALSE); - game_logic(); - } - if (!gbRunGame || gbMaxPlayers == 1 || !nthread_has_500ms_passed(TRUE)) - break; - } -} - -void game_logic() +static void game_logic() { if (PauseMode == 2) { return; @@ -2101,7 +2082,7 @@ void game_logic() pfile_update(FALSE); } -void timeout_cursor(BOOL bTimeout) +static void timeout_cursor(BOOL bTimeout) { if (bTimeout) { if (sgnTimeoutCurs == CURSOR_NONE && !sgbMouseDown) { @@ -2122,6 +2103,25 @@ void timeout_cursor(BOOL bTimeout) } } +void game_loop(BOOL bStartup) +{ + int i; + + i = bStartup ? 60 : 3; + + while (i--) { + if (!multi_handle_delta()) { + timeout_cursor(TRUE); + break; + } else { + timeout_cursor(FALSE); + game_logic(); + } + if (!gbRunGame || gbMaxPlayers == 1 || !nthread_has_500ms_passed(TRUE)) + break; + } +} + void diablo_color_cyc_logic() { DWORD tc; @@ -2149,18 +2149,7 @@ void diablo_color_cyc_logic() } #ifdef HELLFIRE -void alloc_plr() -{ - plr = get_plr_mem(NULL); - - if(plr == NULL) { - app_fatal("Unable to initialize memory"); - } - - memset(plr, 0, sizeof(PlayerStruct) * MAX_PLRS); -} - -PlayerStruct *get_plr_mem(PlayerStruct *p) +static PlayerStruct *get_plr_mem(PlayerStruct *p) { void *r; PlayerStruct *pPlayer; @@ -2181,4 +2170,15 @@ PlayerStruct *get_plr_mem(PlayerStruct *p) return pPlayer; } + +void alloc_plr() +{ + plr = get_plr_mem(NULL); + + if(plr == NULL) { + app_fatal("Unable to initialize memory"); + } + + memset(plr, 0, sizeof(PlayerStruct) * MAX_PLRS); +} #endif diff --git a/Source/diablo.h b/Source/diablo.h index e9baa44f4..19d9a6d13 100644 --- a/Source/diablo.h +++ b/Source/diablo.h @@ -9,20 +9,8 @@ extern HWND ghMainWnd; extern DWORD glSeedTbl[NUMLEVELS]; extern int gnLevelTypeTbl[NUMLEVELS]; -#ifndef HELLFIRE -extern int glEndSeed[NUMLEVELS]; -extern int glMid1Seed[NUMLEVELS]; -extern int glMid2Seed[NUMLEVELS]; -extern int glMid3Seed[NUMLEVELS]; -#else -extern int glEndSeed[NUMLEVELS + 1]; -extern int glMid1Seed[NUMLEVELS + 1]; -extern int glMid2Seed[NUMLEVELS + 1]; -extern int glMid3Seed[NUMLEVELS + 1]; -#endif extern int MouseX; extern int MouseY; -extern BOOL gbGameLoopStartup; extern BOOL gbRunGame; extern BOOL gbRunGameResult; extern BOOL zoomflag; @@ -34,13 +22,10 @@ extern BOOLEAN cineflag; extern int force_redraw; extern BOOL visiondebug; /** unused */ -extern BOOL scrollflag; extern BOOL light4flag; extern BOOL leveldebug; extern BOOL monstdebug; /** unused */ -extern BOOL trigdebug; -extern int setseed; extern int debugmonsttypes; extern int PauseMode; #ifdef HELLFIRE @@ -52,59 +37,26 @@ extern BOOLEAN UseBarbarianTest; extern BOOLEAN UseMultiTest; #endif extern char sgbMouseDown; -extern int color_cycle_timer; void FreeGameMem(); BOOL StartGame(BOOL bNewGame, BOOL bSinglePlayer); -void run_game_loop(unsigned int uMsg); -void start_game(unsigned int uMsg); -void free_game(); -BOOL diablo_get_not_running(); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow); -void diablo_parse_flags(char *args); -#ifdef HELLFIRE -LONG __stdcall diablo_TopLevelExceptionFilter(PEXCEPTION_POINTERS pExc); -#endif -void diablo_init_screen(); -BOOL diablo_find_window(LPCSTR lpClassName); -void diablo_reload_process(HINSTANCE hInstance); BOOL PressEscKey(); LRESULT CALLBACK DisableInputWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); LRESULT CALLBACK GM_Game(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); -BOOL LeftMouseDown(int wParam); -BOOL LeftMouseCmd(BOOL bShift); -BOOL TryIconCurs(); -void LeftMouseUp(); -void RightMouseDown(); -void j_gmenu_on_mouse_move(LPARAM lParam); -BOOL PressSysKey(int wParam); -void diablo_hotkey_msg(DWORD dwMsg); -void ReleaseKey(int vkey); void PressKey(int vkey); -void diablo_pause_game(); -void PressChar(int vkey); -void LoadLvlGFX(); -void LoadAllGFX(); -void CreateLevel(int lvldir); void LoadGameLevel(BOOL firstflag, int lvldir); void game_loop(BOOL bStartup); -void game_logic(); -void timeout_cursor(BOOL bTimeout); void diablo_color_cyc_logic(); #ifdef HELLFIRE void alloc_plr(); -PlayerStruct *get_plr_mem(PlayerStruct *p); #endif -/* data */ - /* rdata */ extern BOOL fullscreen; #ifdef _DEBUG -extern int showintrodebug; extern int questdebug; -extern int debug_mode_key_s; extern int debug_mode_key_w; extern int debug_mode_key_inverted_v; extern int debug_mode_dollar_sign; @@ -113,14 +65,11 @@ extern int debug_mode_key_i; extern int dbgplr; extern int dbgqst; extern int dbgmon; -extern int arrowdebug; extern int frameflag; extern int frameend; extern int framerate; extern int framestart; #endif extern BOOL FriendlyMode; -extern const char *const spszMsgTbl[4]; -extern const char *const spszMsgHotKeyTbl[4]; #endif /* __DIABLO_H__ */ From 70a9045aeafdedc9086bc860b67fdf3d4b89fc04 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 05:23:51 +0100 Subject: [PATCH 03/16] Reorder doom.cpp --- Source/doom.cpp | 32 ++++++++++++++++---------------- Source/doom.h | 17 ----------------- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/Source/doom.cpp b/Source/doom.cpp index b7250830b..40093fc54 100644 --- a/Source/doom.cpp +++ b/Source/doom.cpp @@ -40,10 +40,22 @@ int doom_get_frame_from_time() return DoomQuestState / 1200; } +void doom_cleanup() +{ +#ifdef HELLFIRE + if (pDoomCel != NULL) { + MemFreeDbg(pDoomCel); + pDoomCel = NULL; + } +#else + MemFreeDbg(pDoomCel); +#endif +} + #ifdef HELLFIRE -BOOLEAN doom_alloc_cel() +static BOOLEAN doom_alloc_cel() #else -void doom_alloc_cel() +static void doom_alloc_cel() #endif { #ifdef HELLFIRE @@ -55,22 +67,10 @@ void doom_alloc_cel() #endif } -void doom_cleanup() -{ -#ifdef HELLFIRE - if (pDoomCel != NULL) { - MemFreeDbg(pDoomCel); - pDoomCel = NULL; - } -#else - MemFreeDbg(pDoomCel); -#endif -} - #ifdef HELLFIRE -BOOLEAN doom_load_graphics() +static BOOLEAN doom_load_graphics() #else -void doom_load_graphics() +static void doom_load_graphics() #endif { #ifdef HELLFIRE diff --git a/Source/doom.h b/Source/doom.h index 4dbf96b8a..ddb144d17 100644 --- a/Source/doom.h +++ b/Source/doom.h @@ -6,26 +6,9 @@ #ifndef __DOOM_H__ #define __DOOM_H__ -extern int doom_quest_time; -extern int doom_stars_drawn; -extern BYTE *pDoomCel; extern DIABOOL doomflag; extern int DoomQuestState; -/* -void doom_reset_state(); -void doom_play_movie(); -*/ -int doom_get_frame_from_time(); -#ifdef HELLFIRE -BOOLEAN doom_alloc_cel(); -void doom_cleanup(); -BOOLEAN doom_load_graphics(); -#else -void doom_alloc_cel(); -void doom_cleanup(); -void doom_load_graphics(); -#endif void doom_init(); void doom_close(); void doom_draw(); From a0476b45a0c6f7e32620dccd38d1bd67025e681c Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 05:31:23 +0100 Subject: [PATCH 04/16] Reorder dthread.cpp --- Source/dthread.cpp | 76 +++++++++++++++++++++++----------------------- Source/dthread.h | 4 --- 2 files changed, 38 insertions(+), 42 deletions(-) diff --git a/Source/dthread.cpp b/Source/dthread.cpp index 1e96a605e..e052f1276 100644 --- a/Source/dthread.cpp +++ b/Source/dthread.cpp @@ -15,6 +15,44 @@ HANDLE sghWorkToDoEvent; /* rdata */ static HANDLE sghThread = INVALID_HANDLE_VALUE; +static unsigned int __stdcall dthread_handler(void *data) +{ + const char *error_buf; + TMegaPkt *pkt; + DWORD dwMilliseconds; + + while (dthread_running) { + if (!sgpInfoHead && WaitForSingleObject(sghWorkToDoEvent, INFINITE) == WAIT_FAILED) { + error_buf = TraceLastError(); + app_fatal("dthread4:\n%s", error_buf); + } + + sgMemCrit.Enter(); + pkt = sgpInfoHead; + if (sgpInfoHead) + sgpInfoHead = sgpInfoHead->pNext; + else + ResetEvent(sghWorkToDoEvent); + sgMemCrit.Leave(); + + if (pkt) { + if (pkt->dwSpaceLeft != MAX_PLRS) + multi_send_zero_packet(pkt->dwSpaceLeft, pkt->data[0], &pkt->data[8], *(DWORD *)&pkt->data[4]); + + dwMilliseconds = 1000 * *(DWORD *)&pkt->data[4] / gdwDeltaBytesSec; + if (dwMilliseconds >= 1) + dwMilliseconds = 1; + + mem_free_dbg(pkt); + + if (dwMilliseconds) + Sleep(dwMilliseconds); + } + } + + return 0; +} + void dthread_remove_player(int pnum) { TMegaPkt *pkt; @@ -76,44 +114,6 @@ void dthread_start() } } -unsigned int __stdcall dthread_handler(void *data) -{ - const char *error_buf; - TMegaPkt *pkt; - DWORD dwMilliseconds; - - while (dthread_running) { - if (!sgpInfoHead && WaitForSingleObject(sghWorkToDoEvent, INFINITE) == WAIT_FAILED) { - error_buf = TraceLastError(); - app_fatal("dthread4:\n%s", error_buf); - } - - sgMemCrit.Enter(); - pkt = sgpInfoHead; - if (sgpInfoHead) - sgpInfoHead = sgpInfoHead->pNext; - else - ResetEvent(sghWorkToDoEvent); - sgMemCrit.Leave(); - - if (pkt) { - if (pkt->dwSpaceLeft != MAX_PLRS) - multi_send_zero_packet(pkt->dwSpaceLeft, pkt->data[0], &pkt->data[8], *(DWORD *)&pkt->data[4]); - - dwMilliseconds = 1000 * *(DWORD *)&pkt->data[4] / gdwDeltaBytesSec; - if (dwMilliseconds >= 1) - dwMilliseconds = 1; - - mem_free_dbg(pkt); - - if (dwMilliseconds) - Sleep(dwMilliseconds); - } - } - - return 0; -} - void dthread_cleanup() { const char *error_buf; diff --git a/Source/dthread.h b/Source/dthread.h index f4dc99c6f..481410464 100644 --- a/Source/dthread.h +++ b/Source/dthread.h @@ -6,13 +6,9 @@ #ifndef __DTHREAD_H__ #define __DTHREAD_H__ -extern unsigned int glpDThreadId; -extern BOOLEAN dthread_running; - void dthread_remove_player(int pnum); void dthread_send_delta(int pnum, char cmd, void *pbSrc, int dwLen); void dthread_start(); -unsigned int __stdcall dthread_handler(void *data); void dthread_cleanup(); /* data */ From 95173539ce68a4831330ca74bb9e3af75ae16104 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 15:50:09 +0100 Subject: [PATCH 05/16] Reorder effects.cpp --- Source/effects.cpp | 216 ++++++++++++++++++++++----------------------- Source/effects.h | 14 --- 2 files changed, 108 insertions(+), 122 deletions(-) diff --git a/Source/effects.cpp b/Source/effects.cpp index 8c3ca1410..509a54c8c 100644 --- a/Source/effects.cpp +++ b/Source/effects.cpp @@ -1122,6 +1122,57 @@ void stream_stop() } } +static void stream_play(TSFX *pSFX, int lVolume, int lPan) +{ + BOOL success; + + assert(pSFX); + assert(pSFX->bFlags & sfx_STREAM); + stream_stop(); + lVolume += sound_get_or_set_sound_volume(1); + if (lVolume >= VOLUME_MIN) { + if (lVolume > VOLUME_MAX) + lVolume = VOLUME_MAX; +#ifdef _DEBUG + SFileEnableDirectAccess(FALSE); +#endif + success = SFileOpenFile(pSFX->pszName, &sghStream); +#ifdef _DEBUG + SFileEnableDirectAccess(TRUE); +#endif + if (!success) { + sghStream = NULL; + } else { + if (!SFileDdaBeginEx(sghStream, 0x40000, 0, 0, lVolume, lPan, 0)) + stream_stop(); + else + sgpStreamSFX = pSFX; + } + } +} + +static void stream_update() +{ + DWORD current, end; + + if (sghStream != NULL && SFileDdaGetPos(sghStream, ¤t, &end) && current >= end) { + stream_stop(); + } +} + +static void sfx_stop() +{ + int i; + TSFX *snd; + + snd = &sgSFX[0]; + for (i = 0; i < sizeof(sgSFX) / sizeof(TSFX); i++) { + if (snd->pSnd) + snd_stop_snd(snd->pSnd); + snd++; + } +} + void InitMonsterSND(int monst) { TSnd *pSnd; @@ -1174,33 +1225,7 @@ void FreeMonsterSnd() } } -void PlayEffect(int i, int mode) -{ - int sndIdx, mi, lVolume, lPan; - TSnd *snd; - - if (plr[myplr].pLvlLoad) { - return; - } - - sndIdx = random_(164, 2); - if (!gbSndInited || !gbSoundOn || gbBufferMsgs) { - return; - } - - mi = monster[i]._mMTidx; - snd = Monsters[mi].Snds[mode][sndIdx]; - if (!snd || snd_playing(snd)) { - return; - } - - if (!calc_snd_position(monster[i]._mx, monster[i]._my, &lVolume, &lPan)) - return; - - snd_play_snd(snd, lVolume, lPan); -} - -BOOL calc_snd_position(int x, int y, int *plVolume, int *plPan) +static BOOL calc_snd_position(int x, int y, int *plVolume, int *plPan) { int pan, volume; @@ -1225,13 +1250,7 @@ BOOL calc_snd_position(int x, int y, int *plVolume, int *plPan) return TRUE; } -void PlaySFX(int psfx) -{ - psfx = RndSFX(psfx); - PlaySFX_priv(&sgSFX[psfx], FALSE, 0, 0); -} - -void PlaySFX_priv(TSFX *pSFX, BOOL loc, int x, int y) +static void PlaySFX_priv(TSFX *pSFX, BOOL loc, int x, int y) { int lPan, lVolume; @@ -1264,36 +1283,33 @@ void PlaySFX_priv(TSFX *pSFX, BOOL loc, int x, int y) snd_play_snd(pSFX->pSnd, lVolume, lPan); } -void stream_play(TSFX *pSFX, int lVolume, int lPan) +void PlayEffect(int i, int mode) { - BOOL success; + int sndIdx, mi, lVolume, lPan; + TSnd *snd; - assert(pSFX); - assert(pSFX->bFlags & sfx_STREAM); - stream_stop(); - lVolume += sound_get_or_set_sound_volume(1); - if (lVolume >= VOLUME_MIN) { - if (lVolume > VOLUME_MAX) - lVolume = VOLUME_MAX; -#ifdef _DEBUG - SFileEnableDirectAccess(FALSE); -#endif - success = SFileOpenFile(pSFX->pszName, &sghStream); -#ifdef _DEBUG - SFileEnableDirectAccess(TRUE); -#endif - if (!success) { - sghStream = NULL; - } else { - if (!SFileDdaBeginEx(sghStream, 0x40000, 0, 0, lVolume, lPan, 0)) - stream_stop(); - else - sgpStreamSFX = pSFX; - } + if (plr[myplr].pLvlLoad) { + return; + } + + sndIdx = random_(164, 2); + if (!gbSndInited || !gbSoundOn || gbBufferMsgs) { + return; + } + + mi = monster[i]._mMTidx; + snd = Monsters[mi].Snds[mode][sndIdx]; + if (!snd || snd_playing(snd)) { + return; } + + if (!calc_snd_position(monster[i]._mx, monster[i]._my, &lVolume, &lPan)) + return; + + snd_play_snd(snd, lVolume, lPan); } -int RndSFX(int psfx) +static int RndSFX(int psfx) { int nRand; @@ -1334,6 +1350,12 @@ int RndSFX(int psfx) return psfx + random_(165, nRand); } +void PlaySFX(int psfx) +{ + psfx = RndSFX(psfx); + PlaySFX_priv(&sgSFX[psfx], FALSE, 0, 0); +} + void PlaySfxLoc(int psfx, int x, int y) { TSnd *pSnd; @@ -1366,19 +1388,6 @@ void sound_stop() } } -void sfx_stop() -{ - int i; - TSFX *snd; - - snd = &sgSFX[0]; - for (i = 0; i < sizeof(sgSFX) / sizeof(TSFX); i++) { - if (snd->pSnd) - snd_stop_snd(snd->pSnd); - snd++; - } -} - void sound_update() { if (!gbSndInited) { @@ -1389,15 +1398,6 @@ void sound_update() stream_update(); } -void stream_update() -{ - DWORD current, end; - - if (sghStream != NULL && SFileDdaGetPos(sghStream, ¤t, &end) && current >= end) { - stream_stop(); - } -} - void effects_cleanup_sfx() { DWORD i; @@ -1412,33 +1412,7 @@ void effects_cleanup_sfx() } } -void sound_init() -{ - BYTE mask = 0; - if (gbMaxPlayers > 1) { - mask = PLRSFXS; - } else if (plr[myplr]._pClass == PC_WARRIOR) { - mask = sfx_WARRIOR; - } else if (plr[myplr]._pClass == PC_ROGUE) { - mask = sfx_ROGUE; - } else if (plr[myplr]._pClass == PC_SORCERER) { - mask = sfx_SORCEROR; -#ifdef HELLFIRE - } else if (plr[myplr]._pClass == PC_MONK) { - mask = sfx_MONK; - } else if (plr[myplr]._pClass == PC_BARD) { - mask = sfx_ROGUE; - } else if (plr[myplr]._pClass == PC_BARBARIAN) { - mask = sfx_WARRIOR; -#endif - } else { - app_fatal("effects:1"); - } - - priv_sound_init(mask); -} - -void priv_sound_init(BYTE bLoadMask) +static void priv_sound_init(BYTE bLoadMask) { BYTE pc; DWORD i; @@ -1471,6 +1445,32 @@ void priv_sound_init(BYTE bLoadMask) } } +void sound_init() +{ + BYTE mask = 0; + if (gbMaxPlayers > 1) { + mask = PLRSFXS; + } else if (plr[myplr]._pClass == PC_WARRIOR) { + mask = sfx_WARRIOR; + } else if (plr[myplr]._pClass == PC_ROGUE) { + mask = sfx_ROGUE; + } else if (plr[myplr]._pClass == PC_SORCERER) { + mask = sfx_SORCEROR; +#ifdef HELLFIRE + } else if (plr[myplr]._pClass == PC_MONK) { + mask = sfx_MONK; + } else if (plr[myplr]._pClass == PC_BARD) { + mask = sfx_ROGUE; + } else if (plr[myplr]._pClass == PC_BARBARIAN) { + mask = sfx_WARRIOR; +#endif + } else { + app_fatal("effects:1"); + } + + priv_sound_init(mask); +} + void ui_sound_init() { priv_sound_init(sfx_UI); diff --git a/Source/effects.h b/Source/effects.h index 4c41573f3..34662a51a 100644 --- a/Source/effects.h +++ b/Source/effects.h @@ -8,34 +8,20 @@ extern int sfxdelay; extern int sfxdnum; -extern HANDLE sghStream; -extern TSFX *sgpStreamSFX; BOOL effect_is_playing(int nSFX); void stream_stop(); void InitMonsterSND(int monst); void FreeMonsterSnd(); void PlayEffect(int i, int mode); -BOOL calc_snd_position(int x, int y, int *plVolume, int *plPan); void PlaySFX(int psfx); -void PlaySFX_priv(TSFX *pSFX, BOOL loc, int x, int y); -void stream_play(TSFX *pSFX, int lVolume, int lPan); -int RndSFX(int psfx); void PlaySfxLoc(int psfx, int x, int y); void sound_stop(); void sfx_stop(); void sound_update(); -void stream_update(); void effects_cleanup_sfx(); void sound_init(); -void priv_sound_init(BYTE bLoadMask); void ui_sound_init(); void __stdcall effects_play_sound(const char *snd_file); -/* rdata */ - -extern const char MonstSndChar[]; - -/* data */ - #endif /* __EFFECTS_H__ */ From 57b6d13b5567167d84a78648f7cc40ecc68317f3 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 15:56:32 +0100 Subject: [PATCH 06/16] Reorder encrypt.cpp --- Source/encrypt.cpp | 58 +++++++++++++++++++++++----------------------- Source/encrypt.h | 4 ---- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/Source/encrypt.cpp b/Source/encrypt.cpp index f19fde8f0..ddf1ac4e4 100644 --- a/Source/encrypt.cpp +++ b/Source/encrypt.cpp @@ -70,6 +70,35 @@ void InitHash() } } +static unsigned int __cdecl PkwareBufferRead(char *buf, unsigned int *size, void *param) +{ + TDataInfo *pInfo; + DWORD sSize; + + pInfo = (TDataInfo *)param; + + if (*size >= pInfo->size - pInfo->srcOffset) { + sSize = pInfo->size - pInfo->srcOffset; + } else { + sSize = *size; + } + + memcpy(buf, pInfo->srcData + pInfo->srcOffset, sSize); + pInfo->srcOffset += sSize; + + return sSize; +} + +static void __cdecl PkwareBufferWrite(char *buf, unsigned int *size, void *param) +{ + TDataInfo *pInfo; + + pInfo = (TDataInfo *)param; + + memcpy(pInfo->destData + pInfo->destOffset, buf, *size); + pInfo->destOffset += *size; +} + int PkwareCompress(BYTE *srcData, int size) { BYTE *destData; @@ -106,35 +135,6 @@ int PkwareCompress(BYTE *srcData, int size) return size; } -unsigned int __cdecl PkwareBufferRead(char *buf, unsigned int *size, void *param) -{ - TDataInfo *pInfo; - DWORD sSize; - - pInfo = (TDataInfo *)param; - - if (*size >= pInfo->size - pInfo->srcOffset) { - sSize = pInfo->size - pInfo->srcOffset; - } else { - sSize = *size; - } - - memcpy(buf, pInfo->srcData + pInfo->srcOffset, sSize); - pInfo->srcOffset += sSize; - - return sSize; -} - -void __cdecl PkwareBufferWrite(char *buf, unsigned int *size, void *param) -{ - TDataInfo *pInfo; - - pInfo = (TDataInfo *)param; - - memcpy(pInfo->destData + pInfo->destOffset, buf, *size); - pInfo->destOffset += *size; -} - void PkwareDecompress(BYTE *pbInBuff, int recv_size, int dwMaxBytes) { char *ptr; diff --git a/Source/encrypt.h b/Source/encrypt.h index 6d02bf6e4..327328a7e 100644 --- a/Source/encrypt.h +++ b/Source/encrypt.h @@ -6,15 +6,11 @@ #ifndef __ENCRYPT_H__ #define __ENCRYPT_H__ -extern DWORD hashtable[5][256]; - void Decrypt(DWORD *castBlock, DWORD size, DWORD key); void Encrypt(DWORD *castBlock, DWORD size, DWORD key); DWORD Hash(const char *s, int type); void InitHash(); int PkwareCompress(BYTE *srcData, int size); -unsigned int __cdecl PkwareBufferRead(char *buf, unsigned int *size, void *param); -void __cdecl PkwareBufferWrite(char *buf, unsigned int *size, void *param); void PkwareDecompress(BYTE *pbInBuff, int recv_size, int dwMaxBytes); #endif /* __ENCRYPT_H__ */ From 2dc33b58ea87223a2c2c2934ae074ad77c804525 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 16:22:11 +0100 Subject: [PATCH 07/16] Reorder engine.cpp --- Source/engine.cpp | 743 +++++++++++++++++++++++----------------------- Source/engine.h | 25 -- 2 files changed, 373 insertions(+), 395 deletions(-) diff --git a/Source/engine.cpp b/Source/engine.cpp index 10e15ed1c..bd4b7fc29 100644 --- a/Source/engine.cpp +++ b/Source/engine.cpp @@ -19,7 +19,9 @@ /** automap pixel color 8-bit (palette entry) */ char gbPixelCol; -BOOL gbRotateMap; // flip - if y < x +/** flip - if y < x */ +BOOL gbRotateMap; +/** Seed value before the most recent call to SetRndSeed() */ int orgseed; /** Width of sprite being blitted */ int sgnWidth; @@ -28,7 +30,8 @@ int sglGameSeed; static CCritSect sgMemCrit; /** Number of times the current seed has been fetched */ int SeedCount; -BOOL gbNotInView; // valid - if x/y are in bounds +/** valid - if x/y are in bounds */ +BOOL gbNotInView; /** * Specifies the increment used in the Borland C/C++ pseudo-random. @@ -2726,53 +2729,6 @@ void Cl2ApplyTrans(BYTE *p, BYTE *ttbl, int nCel) } } -/** - * @brief Blit CL2 sprite, to the back buffer at the given coordianates - * @param sx Back buffer coordinate - * @param sy Back buffer coordinate - * @param pCelBuff CL2 buffer - * @param nCel CL2 frame number - * @param nWidth Width of sprite - * @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8 - * @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8 - */ -void Cl2Draw(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap) -{ - BYTE *pRLEBytes; - DWORD *pFrameTable; - int nDataStart, nDataSize; - - assert(gpBuffer != NULL); - if (gpBuffer == NULL) - return; - assert(pCelBuff != NULL); - if (pCelBuff == NULL) - return; - assert(nCel > 0); - if (nCel <= 0) - return; - - pFrameTable = (DWORD *)pCelBuff; - assert(nCel <= (int)pFrameTable[0]); - pRLEBytes = &pCelBuff[pFrameTable[nCel]]; - nDataStart = *(WORD *)&pRLEBytes[CelSkip]; - if (!nDataStart) - return; - - if (CelCap == 8) - nDataSize = 0; - else - nDataSize = *(WORD *)&pRLEBytes[CelCap]; - if (!nDataSize) - nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel]; - - Cl2Blit( - &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]], - pRLEBytes + nDataStart, - nDataSize - nDataStart, - nWidth); -} - /** * @brief Blit CL2 sprite to the given buffer * @param pDecodeTo The output buffer @@ -2780,7 +2736,7 @@ void Cl2Draw(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, * @param nDataSize Size of CL2 in bytes * @param nWidth Width of sprite */ -void Cl2Blit(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth) +static void Cl2Blit(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth) { #ifdef USE_ASM __asm { @@ -2921,55 +2877,6 @@ void Cl2Blit(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth) #endif } -/** - * @brief Blit a solid colder shape one pixel larger then the given sprite shape, to the back buffer at the given coordianates - * @param col Color index from current palette - * @param sx Back buffer coordinate - * @param sy Back buffer coordinate - * @param pCelBuff CL2 buffer - * @param nCel CL2 frame number - * @param nWidth Width of sprite - * @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8 - * @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8 - */ -void Cl2DrawOutline(char col, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap) -{ - int nDataStart, nDataSize; - BYTE *pRLEBytes; - DWORD *pFrameTable; - - assert(gpBuffer != NULL); - if (gpBuffer == NULL) - return; - assert(pCelBuff != NULL); - if (pCelBuff == NULL) - return; - assert(nCel > 0); - if (nCel <= 0) - return; - - pFrameTable = (DWORD *)pCelBuff; - assert(nCel <= (int)pFrameTable[0]); - pRLEBytes = &pCelBuff[pFrameTable[nCel]]; - nDataStart = *(WORD *)&pRLEBytes[CelSkip]; - if (!nDataStart) - return; - - if (CelCap == 8) - nDataSize = 0; - else - nDataSize = *(WORD *)&pRLEBytes[CelCap]; - if (!nDataSize) - nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel]; - - Cl2BlitOutline( - &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]], - pRLEBytes + nDataStart, - nDataSize - nDataStart, - nWidth, - col); -} - /** * @brief Blit a solid colder shape one pixel larger then the given sprite shape, to the given buffer * @param pDecodeTo The output buffer @@ -2978,7 +2885,7 @@ void Cl2DrawOutline(char col, int sx, int sy, BYTE *pCelBuff, int nCel, int nWid * @param nWidth Width of sprite * @param col Color index from current palette */ -void Cl2BlitOutline(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth, char col) +static void Cl2BlitOutline(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth, char col) { #ifdef USE_ASM __asm { @@ -3140,65 +3047,6 @@ void Cl2BlitOutline(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth, #endif } -/** - * @brief Blit CL2 sprite, and apply a given lighting, to the back buffer at the given coordianates - * @param sx Back buffer coordinate - * @param sy Back buffer coordinate - * @param pCelBuff CL2 buffer - * @param nCel CL2 frame number - * @param nWidth Width of sprite - * @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8 - * @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8 - * @param light Light shade to use - */ -void Cl2DrawLightTbl(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap, char light) -{ - int nDataStart, nDataSize, idx, nSize; - BYTE *pRLEBytes, *pDecodeTo; - DWORD *pFrameTable; - - assert(gpBuffer != NULL); - if (gpBuffer == NULL) - return; - assert(pCelBuff != NULL); - if (pCelBuff == NULL) - return; - assert(nCel > 0); - if (nCel <= 0) - return; - - pFrameTable = (DWORD *)pCelBuff; - assert(nCel <= (int)pFrameTable[0]); - pRLEBytes = &pCelBuff[pFrameTable[nCel]]; - nDataStart = *(WORD *)&pRLEBytes[CelSkip]; - if (!nDataStart) - return; - - if (CelCap == 8) - nDataSize = 0; - else - nDataSize = *(WORD *)&pRLEBytes[CelCap]; - if (!nDataSize) - nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel]; - - nSize = nDataSize - nDataStart; - pRLEBytes += nDataStart; - pDecodeTo = &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]]; - - idx = light4flag ? 1024 : 4096; - if (light == 2) - idx += 256; // gray colors - if (light >= 4) - idx += (light - 1) << 8; - - Cl2BlitLight( - pDecodeTo, - pRLEBytes, - nSize, - nWidth, - &pLightTbl[idx]); -} - /** * @brief Blit CL2 sprite, and apply lighting, to the given buffer * @param pDecodeTo The output buffer @@ -3207,7 +3055,7 @@ void Cl2DrawLightTbl(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int C * @param nWidth Width of sprite * @param pTable Light color table */ -void Cl2BlitLight(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth, BYTE *pTable) +static void Cl2BlitLight(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth, BYTE *pTable) { #ifdef USE_ASM __asm { @@ -3357,103 +3205,6 @@ void Cl2BlitLight(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth, B #endif } -/** - * @brief Blit CL2 sprite, and apply lighting, to the back buffer at the given coordinates - * @param sx Back buffer coordinate - * @param sy Back buffer coordinate - * @param pCelBuff CL2 buffer - * @param nCel CL2 frame number - * @param nWidth Width of sprite - * @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8 - * @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8 - */ -void Cl2DrawLight(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap) -{ - int nDataStart, nDataSize, nSize; - BYTE *pRLEBytes, *pDecodeTo; - DWORD *pFrameTable; - - assert(gpBuffer != NULL); - if (gpBuffer == NULL) - return; - assert(pCelBuff != NULL); - if (pCelBuff == NULL) - return; - assert(nCel > 0); - if (nCel <= 0) - return; - - pFrameTable = (DWORD *)pCelBuff; - assert(nCel <= (int)pFrameTable[0]); - pRLEBytes = &pCelBuff[pFrameTable[nCel]]; - nDataStart = *(WORD *)&pRLEBytes[CelSkip]; - if (!nDataStart) - return; - - if (CelCap == 8) - nDataSize = 0; - else - nDataSize = *(WORD *)&pRLEBytes[CelCap]; - if (!nDataSize) - nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel]; - - nSize = nDataSize - nDataStart; - pRLEBytes += nDataStart; - pDecodeTo = &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]]; - - if (light_table_index) - Cl2BlitLight(pDecodeTo, pRLEBytes, nSize, nWidth, &pLightTbl[light_table_index * 256]); - else - Cl2Blit(pDecodeTo, pRLEBytes, nSize, nWidth); -} - -/** - * @brief Same as Cl2Draw but checks for drawing outside the buffer - * @param sx Back buffer coordinate - * @param sy Back buffer coordinate - * @param pCelBuff CL2 buffer - * @param nCel CL2 frame number - * @param nWidth Width of sprite - * @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8 - * @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8 - */ -void Cl2DrawSafe(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap) -{ - BYTE *pRLEBytes; - DWORD *pFrameTable; - int nDataStart, nDataSize; - - assert(gpBuffer != NULL); - if (gpBuffer == NULL) - return; - assert(pCelBuff != NULL); - if (pCelBuff == NULL) - return; - assert(nCel > 0); - if (nCel <= 0) - return; - - pFrameTable = (DWORD *)pCelBuff; - assert(nCel <= (int)pFrameTable[0]); - pRLEBytes = &pCelBuff[pFrameTable[nCel]]; - nDataStart = *(WORD *)&pRLEBytes[CelSkip]; - if (!nDataStart) - return; - - if (CelCap == 8) - nDataSize = 0; - else - nDataSize = *(WORD *)&pRLEBytes[CelCap]; - if (!nDataSize) - nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel]; - - Cl2BlitSafe( - &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]], - pRLEBytes + nDataStart, - nDataSize - nDataStart, - nWidth); -} - /** * @brief Same as Cl2Blit but checks for drawing outside the buffer * @param pDecodeTo The output buffer @@ -3461,7 +3212,7 @@ void Cl2DrawSafe(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSk * @param nDataSize Size of CL2 in bytes * @param nWidth Width of sprite */ -void Cl2BlitSafe(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth) +static void Cl2BlitSafe(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth) { #ifdef USE_ASM __asm { @@ -3615,57 +3366,6 @@ void Cl2BlitSafe(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth) #endif } -/** - * @brief Same as Cl2DrawOutline but checks for drawing outside the buffer - * @param col Color index from current palette - * @param sx Back buffer coordinate - * @param sy Back buffer coordinate - * @param pCelBuff CL2 buffer - * @param nCel CL2 frame number - * @param nWidth Width of sprite - * @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8 - * @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8 - */ -void Cl2DrawOutlineSafe(char col, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap) -{ - int nDataStart, nDataSize; - BYTE *pRLEBytes; - DWORD *pFrameTable; - - assert(gpBuffer != NULL); - if (gpBuffer == NULL) - return; - assert(pCelBuff != NULL); - if (pCelBuff == NULL) - return; - assert(nCel > 0); - if (nCel <= 0) - return; - - pFrameTable = (DWORD *)pCelBuff; - assert(nCel <= (int)pFrameTable[0]); - pRLEBytes = &pCelBuff[pFrameTable[nCel]]; - nDataStart = *(WORD *)&pRLEBytes[CelSkip]; - if (!nDataStart) - return; - - if (CelCap == 8) - nDataSize = 0; - else - nDataSize = *(WORD *)&pRLEBytes[CelCap]; - if (!nDataSize) - nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel]; - - gpBufEnd -= BUFFER_WIDTH; - Cl2BlitOutlineSafe( - &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]], - pRLEBytes + nDataStart, - nDataSize - nDataStart, - nWidth, - col); - gpBufEnd += BUFFER_WIDTH; -} - /** * @brief Same as Cl2BlitOutline but checks for drawing outside the buffer * @param pDecodeTo The output buffer @@ -3674,7 +3374,7 @@ void Cl2DrawOutlineSafe(char col, int sx, int sy, BYTE *pCelBuff, int nCel, int * @param nWidth Width of sprite * @param col Color index from current palette */ -void Cl2BlitOutlineSafe(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth, char col) +static void Cl2BlitOutlineSafe(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth, char col) { #ifdef USE_ASM __asm { @@ -3848,65 +3548,6 @@ void Cl2BlitOutlineSafe(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWi #endif } -/** - * @brief Same as Cl2DrawLightTbl but checks for drawing outside the buffer - * @param sx Back buffer coordinate - * @param sy Back buffer coordinate - * @param pCelBuff CL2 buffer - * @param nCel CL2 frame number - * @param nWidth Width of sprite - * @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8 - * @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8 - * @param light light shade to use - */ -void Cl2DrawLightTblSafe(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap, char light) -{ - int nDataStart, nDataSize, idx, nSize; - BYTE *pRLEBytes, *pDecodeTo; - DWORD *pFrameTable; - - assert(gpBuffer != NULL); - if (gpBuffer == NULL) - return; - assert(pCelBuff != NULL); - if (pCelBuff == NULL) - return; - assert(nCel > 0); - if (nCel <= 0) - return; - - pFrameTable = (DWORD *)pCelBuff; - assert(nCel <= (int)pFrameTable[0]); - pRLEBytes = &pCelBuff[pFrameTable[nCel]]; - nDataStart = *(WORD *)&pRLEBytes[CelSkip]; - if (!nDataStart) - return; - - if (CelCap == 8) - nDataSize = 0; - else - nDataSize = *(WORD *)&pRLEBytes[CelCap]; - if (!nDataSize) - nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel]; - - nSize = nDataSize - nDataStart; - pRLEBytes += nDataStart; - pDecodeTo = &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]]; - - idx = light4flag ? 1024 : 4096; - if (light == 2) - idx += 256; // gray colors - if (light >= 4) - idx += (light - 1) << 8; - - Cl2BlitLightSafe( - pDecodeTo, - pRLEBytes, - nSize, - nWidth, - &pLightTbl[idx]); -} - /** * @brief Same as Cl2BlitLight but checks for drawing outside the buffer * @param pDecodeTo The output buffer @@ -3915,7 +3556,7 @@ void Cl2DrawLightTblSafe(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, i * @param nWidth With of CL2 sprite * @param pTable Light color table */ -void Cl2BlitLightSafe(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth, BYTE *pTable) +static void Cl2BlitLightSafe(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth, BYTE *pTable) { #ifdef USE_ASM __asm { @@ -4078,6 +3719,368 @@ void Cl2BlitLightSafe(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidt #endif } +/** + * @brief Blit CL2 sprite, to the back buffer at the given coordianates + * @param sx Back buffer coordinate + * @param sy Back buffer coordinate + * @param pCelBuff CL2 buffer + * @param nCel CL2 frame number + * @param nWidth Width of sprite + * @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8 + * @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8 + */ +void Cl2Draw(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap) +{ + BYTE *pRLEBytes; + DWORD *pFrameTable; + int nDataStart, nDataSize; + + assert(gpBuffer != NULL); + if (gpBuffer == NULL) + return; + assert(pCelBuff != NULL); + if (pCelBuff == NULL) + return; + assert(nCel > 0); + if (nCel <= 0) + return; + + pFrameTable = (DWORD *)pCelBuff; + assert(nCel <= (int)pFrameTable[0]); + pRLEBytes = &pCelBuff[pFrameTable[nCel]]; + nDataStart = *(WORD *)&pRLEBytes[CelSkip]; + if (!nDataStart) + return; + + if (CelCap == 8) + nDataSize = 0; + else + nDataSize = *(WORD *)&pRLEBytes[CelCap]; + if (!nDataSize) + nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel]; + + Cl2Blit( + &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]], + pRLEBytes + nDataStart, + nDataSize - nDataStart, + nWidth); +} + +/** + * @brief Blit a solid colder shape one pixel larger then the given sprite shape, to the back buffer at the given coordianates + * @param col Color index from current palette + * @param sx Back buffer coordinate + * @param sy Back buffer coordinate + * @param pCelBuff CL2 buffer + * @param nCel CL2 frame number + * @param nWidth Width of sprite + * @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8 + * @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8 + */ +void Cl2DrawOutline(char col, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap) +{ + int nDataStart, nDataSize; + BYTE *pRLEBytes; + DWORD *pFrameTable; + + assert(gpBuffer != NULL); + if (gpBuffer == NULL) + return; + assert(pCelBuff != NULL); + if (pCelBuff == NULL) + return; + assert(nCel > 0); + if (nCel <= 0) + return; + + pFrameTable = (DWORD *)pCelBuff; + assert(nCel <= (int)pFrameTable[0]); + pRLEBytes = &pCelBuff[pFrameTable[nCel]]; + nDataStart = *(WORD *)&pRLEBytes[CelSkip]; + if (!nDataStart) + return; + + if (CelCap == 8) + nDataSize = 0; + else + nDataSize = *(WORD *)&pRLEBytes[CelCap]; + if (!nDataSize) + nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel]; + + Cl2BlitOutline( + &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]], + pRLEBytes + nDataStart, + nDataSize - nDataStart, + nWidth, + col); +} + +/** + * @brief Blit CL2 sprite, and apply a given lighting, to the back buffer at the given coordianates + * @param sx Back buffer coordinate + * @param sy Back buffer coordinate + * @param pCelBuff CL2 buffer + * @param nCel CL2 frame number + * @param nWidth Width of sprite + * @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8 + * @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8 + * @param light Light shade to use + */ +void Cl2DrawLightTbl(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap, char light) +{ + int nDataStart, nDataSize, idx, nSize; + BYTE *pRLEBytes, *pDecodeTo; + DWORD *pFrameTable; + + assert(gpBuffer != NULL); + if (gpBuffer == NULL) + return; + assert(pCelBuff != NULL); + if (pCelBuff == NULL) + return; + assert(nCel > 0); + if (nCel <= 0) + return; + + pFrameTable = (DWORD *)pCelBuff; + assert(nCel <= (int)pFrameTable[0]); + pRLEBytes = &pCelBuff[pFrameTable[nCel]]; + nDataStart = *(WORD *)&pRLEBytes[CelSkip]; + if (!nDataStart) + return; + + if (CelCap == 8) + nDataSize = 0; + else + nDataSize = *(WORD *)&pRLEBytes[CelCap]; + if (!nDataSize) + nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel]; + + nSize = nDataSize - nDataStart; + pRLEBytes += nDataStart; + pDecodeTo = &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]]; + + idx = light4flag ? 1024 : 4096; + if (light == 2) + idx += 256; // gray colors + if (light >= 4) + idx += (light - 1) << 8; + + Cl2BlitLight( + pDecodeTo, + pRLEBytes, + nSize, + nWidth, + &pLightTbl[idx]); +} + +/** + * @brief Blit CL2 sprite, and apply lighting, to the back buffer at the given coordinates + * @param sx Back buffer coordinate + * @param sy Back buffer coordinate + * @param pCelBuff CL2 buffer + * @param nCel CL2 frame number + * @param nWidth Width of sprite + * @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8 + * @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8 + */ +void Cl2DrawLight(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap) +{ + int nDataStart, nDataSize, nSize; + BYTE *pRLEBytes, *pDecodeTo; + DWORD *pFrameTable; + + assert(gpBuffer != NULL); + if (gpBuffer == NULL) + return; + assert(pCelBuff != NULL); + if (pCelBuff == NULL) + return; + assert(nCel > 0); + if (nCel <= 0) + return; + + pFrameTable = (DWORD *)pCelBuff; + assert(nCel <= (int)pFrameTable[0]); + pRLEBytes = &pCelBuff[pFrameTable[nCel]]; + nDataStart = *(WORD *)&pRLEBytes[CelSkip]; + if (!nDataStart) + return; + + if (CelCap == 8) + nDataSize = 0; + else + nDataSize = *(WORD *)&pRLEBytes[CelCap]; + if (!nDataSize) + nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel]; + + nSize = nDataSize - nDataStart; + pRLEBytes += nDataStart; + pDecodeTo = &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]]; + + if (light_table_index) + Cl2BlitLight(pDecodeTo, pRLEBytes, nSize, nWidth, &pLightTbl[light_table_index * 256]); + else + Cl2Blit(pDecodeTo, pRLEBytes, nSize, nWidth); +} + +/** + * @brief Same as Cl2Draw but checks for drawing outside the buffer + * @param sx Back buffer coordinate + * @param sy Back buffer coordinate + * @param pCelBuff CL2 buffer + * @param nCel CL2 frame number + * @param nWidth Width of sprite + * @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8 + * @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8 + */ +void Cl2DrawSafe(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap) +{ + BYTE *pRLEBytes; + DWORD *pFrameTable; + int nDataStart, nDataSize; + + assert(gpBuffer != NULL); + if (gpBuffer == NULL) + return; + assert(pCelBuff != NULL); + if (pCelBuff == NULL) + return; + assert(nCel > 0); + if (nCel <= 0) + return; + + pFrameTable = (DWORD *)pCelBuff; + assert(nCel <= (int)pFrameTable[0]); + pRLEBytes = &pCelBuff[pFrameTable[nCel]]; + nDataStart = *(WORD *)&pRLEBytes[CelSkip]; + if (!nDataStart) + return; + + if (CelCap == 8) + nDataSize = 0; + else + nDataSize = *(WORD *)&pRLEBytes[CelCap]; + if (!nDataSize) + nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel]; + + Cl2BlitSafe( + &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]], + pRLEBytes + nDataStart, + nDataSize - nDataStart, + nWidth); +} + +/** + * @brief Same as Cl2DrawOutline but checks for drawing outside the buffer + * @param col Color index from current palette + * @param sx Back buffer coordinate + * @param sy Back buffer coordinate + * @param pCelBuff CL2 buffer + * @param nCel CL2 frame number + * @param nWidth Width of sprite + * @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8 + * @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8 + */ +void Cl2DrawOutlineSafe(char col, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap) +{ + int nDataStart, nDataSize; + BYTE *pRLEBytes; + DWORD *pFrameTable; + + assert(gpBuffer != NULL); + if (gpBuffer == NULL) + return; + assert(pCelBuff != NULL); + if (pCelBuff == NULL) + return; + assert(nCel > 0); + if (nCel <= 0) + return; + + pFrameTable = (DWORD *)pCelBuff; + assert(nCel <= (int)pFrameTable[0]); + pRLEBytes = &pCelBuff[pFrameTable[nCel]]; + nDataStart = *(WORD *)&pRLEBytes[CelSkip]; + if (!nDataStart) + return; + + if (CelCap == 8) + nDataSize = 0; + else + nDataSize = *(WORD *)&pRLEBytes[CelCap]; + if (!nDataSize) + nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel]; + + gpBufEnd -= BUFFER_WIDTH; + Cl2BlitOutlineSafe( + &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]], + pRLEBytes + nDataStart, + nDataSize - nDataStart, + nWidth, + col); + gpBufEnd += BUFFER_WIDTH; +} + +/** + * @brief Same as Cl2DrawLightTbl but checks for drawing outside the buffer + * @param sx Back buffer coordinate + * @param sy Back buffer coordinate + * @param pCelBuff CL2 buffer + * @param nCel CL2 frame number + * @param nWidth Width of sprite + * @param CelSkip Skip lower parts of sprite, must be multiple of 2, max 8 + * @param CelCap Amount of sprite to render from lower to upper, must be multiple of 2, max 8 + * @param light light shade to use + */ +void Cl2DrawLightTblSafe(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap, char light) +{ + int nDataStart, nDataSize, idx, nSize; + BYTE *pRLEBytes, *pDecodeTo; + DWORD *pFrameTable; + + assert(gpBuffer != NULL); + if (gpBuffer == NULL) + return; + assert(pCelBuff != NULL); + if (pCelBuff == NULL) + return; + assert(nCel > 0); + if (nCel <= 0) + return; + + pFrameTable = (DWORD *)pCelBuff; + assert(nCel <= (int)pFrameTable[0]); + pRLEBytes = &pCelBuff[pFrameTable[nCel]]; + nDataStart = *(WORD *)&pRLEBytes[CelSkip]; + if (!nDataStart) + return; + + if (CelCap == 8) + nDataSize = 0; + else + nDataSize = *(WORD *)&pRLEBytes[CelCap]; + if (!nDataSize) + nDataSize = pFrameTable[nCel + 1] - pFrameTable[nCel]; + + nSize = nDataSize - nDataStart; + pRLEBytes += nDataStart; + pDecodeTo = &gpBuffer[sx + PitchTbl[sy - 16 * CelSkip]]; + + idx = light4flag ? 1024 : 4096; + if (light == 2) + idx += 256; // gray colors + if (light >= 4) + idx += (light - 1) << 8; + + Cl2BlitLightSafe( + pDecodeTo, + pRLEBytes, + nSize, + nWidth, + &pLightTbl[idx]); +} + /** * @brief Same as Cl2DrawLight but checks for drawing outside the buffer * @param sx Back buffer coordinate diff --git a/Source/engine.h b/Source/engine.h index bbcb7f47c..d926c12d9 100644 --- a/Source/engine.h +++ b/Source/engine.h @@ -13,20 +13,6 @@ #ifndef __ENGINE_H__ #define __ENGINE_H__ -//offset 0 -//pCelBuff->pFrameTable[0] - -/** automap pixel color 8-bit (palette entry) */ -extern char gbPixelCol; -/** flip - if y < x */ -extern BOOL gbRotateMap; -/** Seed value before the most recent call to SetRndSeed() */ -extern int orgseed; -/** Track number of calls to GetRndSeed() since last call to SetRndSeed() */ -extern int SeedCount; -/** valid - if x/y are in bounds */ -extern BOOL gbNotInView; - __FINLINE BYTE *CelGetFrame(BYTE *pCelBuff, int nCel, int *nDataSize); void CelBlit(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth); @@ -65,24 +51,13 @@ BYTE *LoadFileInMem(const char *pszName, DWORD *pdwFileLen); DWORD LoadFileWithMem(const char *pszName, BYTE *p); void Cl2ApplyTrans(BYTE *p, BYTE *ttbl, int nCel); void Cl2Draw(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap); -void Cl2Blit(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth); void Cl2DrawOutline(char col, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap); -void Cl2BlitOutline(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth, char col); void Cl2DrawLightTbl(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap, char light); -void Cl2BlitLight(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth, BYTE *pTable); void Cl2DrawLight(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap); void Cl2DrawSafe(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap); -void Cl2BlitSafe(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth); void Cl2DrawOutlineSafe(char col, int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap); -void Cl2BlitOutlineSafe(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth, char col); void Cl2DrawLightTblSafe(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap, char light); -void Cl2BlitLightSafe(BYTE *pDecodeTo, BYTE *pRLEBytes, int nDataSize, int nWidth, BYTE *pTable); void Cl2DrawLightSafe(int sx, int sy, BYTE *pCelBuff, int nCel, int nWidth, int CelSkip, int CelCap); void PlayInGameMovie(const char *pszMovie); -/* rdata */ - -extern const int RndInc; -extern const int RndMult; - #endif /* __ENGINE_H__ */ From d6540adfeb7e08cd6e4f814e73d83735655e713c Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 17:04:58 +0100 Subject: [PATCH 08/16] Clean up error.h --- Source/error.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Source/error.h b/Source/error.h index 60e52a981..bd924760b 100644 --- a/Source/error.h +++ b/Source/error.h @@ -6,16 +6,11 @@ #ifndef __ERROR_H__ #define __ERROR_H__ -extern char msgtable[MAX_SEND_STR_LEN]; extern char msgdelay; extern char msgflag; -extern char msgcnt; void InitDiabloMsg(char e); void ClrDiabloMsg(); void DrawDiabloMsg(); -/* data */ -extern const char *const MsgStrings[]; - #endif /* __ERROR_H__ */ From 2867b811c0df5f328b44b25fda901bfa31c87a7a Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 18:28:24 +0100 Subject: [PATCH 09/16] Fix incorrectly mapped functions in hellfire --- Source/monster.cpp | 4 ++-- Source/towners.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/monster.cpp b/Source/monster.cpp index 561de73fb..000354e3e 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -1848,7 +1848,7 @@ void SpawnLoot(int i, BOOL sendmsg) CreateTypeItem(Monst->_mx + 1, Monst->_my + 1, TRUE, ITYPE_MACE, IMISC_NONE, TRUE, FALSE); } else if (Monst->mName == UniqMonst[UMT_DEFILER].mName) { if (effect_is_playing(USFX_DEFILER8)) - sfx_stop(); + stream_stop(); quests[Q_DEFILER]._qlog = 0; SpawnMapOfDoom(Monst->_mx, Monst->_my); } else if (Monst->mName == UniqMonst[UMT_HORKDMN].mName) { @@ -1863,7 +1863,7 @@ void SpawnLoot(int i, BOOL sendmsg) if (UseCowFarmer) nSFX = USFX_NAKRUL6; if (effect_is_playing(nSFX)) - sfx_stop(); + stream_stop(); quests[Q_NAKRUL]._qlog = 0; UberDiabloMonsterIndex = -2; CreateMagicWeapon(Monst->_mx, Monst->_my, ITYPE_SWORD, ICURS_GREAT_SWORD, FALSE, TRUE); diff --git a/Source/towners.cpp b/Source/towners.cpp index e8a90b33e..efd692a0a 100644 --- a/Source/towners.cpp +++ b/Source/towners.cpp @@ -539,7 +539,7 @@ void TownCtrlMsg(int i) if (dx >= 2 || dy >= 2) { towner[i]._tbtcnt = 0; qtextflag = FALSE; - sfx_stop(); + stream_stop(); } #else if (dx >= 2 || dy >= 2) From 14fda0ee7acf947d80d861f34c83d2c84aaeb51e Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 18:49:52 +0100 Subject: [PATCH 10/16] Reorder fault.cpp --- Source/fault.cpp | 149 ++++++++++++++++++++++++----------------------- Source/fault.h | 15 ----- 2 files changed, 77 insertions(+), 87 deletions(-) diff --git a/Source/fault.cpp b/Source/fault.cpp index 43e14f5e7..e456737a2 100644 --- a/Source/fault.cpp +++ b/Source/fault.cpp @@ -5,88 +5,55 @@ */ #include "all.h" +typedef struct STACK_FRAME { + struct STACK_FRAME *pNext; + void *pCallRet; +} STACK_FRAME; + LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter; int fault_unused; -#ifndef _MSC_VER -__attribute__((constructor)) -#endif -static void -fault_c_init(void) +static void *fault_set_filter(void *unused) { - fault_init_filter(); - fault_cleanup_filter_atexit(); + lpTopLevelExceptionFilter = SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)TopLevelExceptionFilter); + return unused; } -SEG_ALLOCATE(SEGMENT_C_INIT) -_PVFV exception_c_init_funcs[] = { &fault_c_init }; - -void fault_init_filter() +static LPTOP_LEVEL_EXCEPTION_FILTER fault_reset_filter(void *unused) { - fault_set_filter(&fault_unused); + return SetUnhandledExceptionFilter(lpTopLevelExceptionFilter); } -void fault_cleanup_filter_atexit() +static LPTOP_LEVEL_EXCEPTION_FILTER __cdecl fault_cleanup_filter() { - atexit((void(__cdecl *)(void))fault_cleanup_filter); + return fault_reset_filter(&fault_unused); } -LPTOP_LEVEL_EXCEPTION_FILTER __cdecl fault_cleanup_filter() +static void fault_init_filter() { - return fault_reset_filter(&fault_unused); + fault_set_filter(&fault_unused); } -LONG __stdcall TopLevelExceptionFilter(PEXCEPTION_POINTERS ExceptionInfo) +static void fault_cleanup_filter_atexit() { - PEXCEPTION_RECORD xcpt; - char szExceptionNameBuf[MAX_PATH]; - char szModuleName[MAX_PATH]; - char *pszExceptionName; - int sectionNumber, sectionOffset; - PCONTEXT ctx; - - log_dump_computer_info(); - xcpt = ExceptionInfo->ExceptionRecord; - pszExceptionName = fault_get_error_type(ExceptionInfo->ExceptionRecord->ExceptionCode, szExceptionNameBuf, sizeof(szExceptionNameBuf)); - log_printf("Exception code: %08X %s\r\n", xcpt->ExceptionCode, pszExceptionName); - - fault_unknown_module(xcpt->ExceptionAddress, szModuleName, MAX_PATH, §ionNumber, §ionOffset); - log_printf("Fault address:\t%08X %02X:%08X %s\r\n", xcpt->ExceptionAddress, sectionNumber, sectionOffset, szModuleName); - - ctx = ExceptionInfo->ContextRecord; - - log_printf("\r\nRegisters:\r\n"); - log_printf( - "EAX:%08X\r\nEBX:%08X\r\nECX:%08X\r\nEDX:%08X\r\nESI:%08X\r\nEDI:%08X\r\n", - ctx->Eax, - ctx->Ebx, - ctx->Ecx, - ctx->Edx, - ctx->Esi, - ctx->Edi); - log_printf("CS:EIP:%04X:%08X\r\n", ctx->SegCs, ctx->Eip); - log_printf("SS:ESP:%04X:%08X EBP:%08X\r\n", ctx->SegSs, ctx->Esp, ctx->Ebp); - log_printf("DS:%04X ES:%04X FS:%04X GS:%04X\r\n", ctx->SegDs, ctx->SegEs, ctx->SegFs, ctx->SegGs); - - log_printf("Flags:%08X\r\n", ctx->EFlags); - fault_call_stack((void *)ctx->Eip, (STACK_FRAME *)ctx->Ebp); - - log_printf("Stack bytes:\r\n"); - fault_hex_format((BYTE *)ctx->Esp, 768); - - log_printf("Code bytes:\r\n"); - fault_hex_format((BYTE *)ctx->Eip, 16); - - log_printf("\r\n"); - log_flush(TRUE); + atexit((void(__cdecl *)(void))fault_cleanup_filter); +} - if (lpTopLevelExceptionFilter) - return lpTopLevelExceptionFilter(ExceptionInfo); - return EXCEPTION_CONTINUE_SEARCH; +#ifndef _MSC_VER +__attribute__((constructor)) +#endif +static void +fault_c_init(void) +{ + fault_init_filter(); + fault_cleanup_filter_atexit(); } -void fault_hex_format(BYTE *ptr, DWORD numBytes) +SEG_ALLOCATE(SEGMENT_C_INIT) +_PVFV exception_c_init_funcs[] = { &fault_c_init }; + +static void fault_hex_format(BYTE *ptr, DWORD numBytes) { DWORD i, bytesRead; const char *fmt; @@ -127,7 +94,7 @@ void fault_hex_format(BYTE *ptr, DWORD numBytes) log_printf("\r\n"); } -void fault_unknown_module(LPCVOID lpAddress, LPSTR lpModuleName, int iMaxLength, int *sectionNum, int *sectionOffset) +static void fault_unknown_module(LPCVOID lpAddress, LPSTR lpModuleName, int iMaxLength, int *sectionNum, int *sectionOffset) { MEMORY_BASIC_INFORMATION memInfo; PIMAGE_DOS_HEADER dosHeader; @@ -178,7 +145,7 @@ void fault_unknown_module(LPCVOID lpAddress, LPSTR lpModuleName, int iMaxLength, } } -void fault_call_stack(void *instr, STACK_FRAME *stackFrame) +static void fault_call_stack(void *instr, STACK_FRAME *stackFrame) { STACK_FRAME *oldStackFrame; char szModuleName[MAX_PATH]; @@ -203,7 +170,7 @@ void fault_call_stack(void *instr, STACK_FRAME *stackFrame) log_printf("\r\n"); } -char *fault_get_error_type(DWORD dwMessageId, LPSTR lpString1, DWORD nSize) +static char *fault_get_error_type(DWORD dwMessageId, LPSTR lpString1, DWORD nSize) { const char *s; @@ -285,15 +252,53 @@ char *fault_get_error_type(DWORD dwMessageId, LPSTR lpString1, DWORD nSize) return lpString1; } -void *fault_set_filter(void *unused) +LONG __stdcall TopLevelExceptionFilter(PEXCEPTION_POINTERS ExceptionInfo) { - lpTopLevelExceptionFilter = SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)TopLevelExceptionFilter); - return unused; -} + PEXCEPTION_RECORD xcpt; + char szExceptionNameBuf[MAX_PATH]; + char szModuleName[MAX_PATH]; + char *pszExceptionName; + int sectionNumber, sectionOffset; + PCONTEXT ctx; -LPTOP_LEVEL_EXCEPTION_FILTER fault_reset_filter(void *unused) -{ - return SetUnhandledExceptionFilter(lpTopLevelExceptionFilter); + log_dump_computer_info(); + xcpt = ExceptionInfo->ExceptionRecord; + pszExceptionName = fault_get_error_type(ExceptionInfo->ExceptionRecord->ExceptionCode, szExceptionNameBuf, sizeof(szExceptionNameBuf)); + log_printf("Exception code: %08X %s\r\n", xcpt->ExceptionCode, pszExceptionName); + + fault_unknown_module(xcpt->ExceptionAddress, szModuleName, MAX_PATH, §ionNumber, §ionOffset); + log_printf("Fault address:\t%08X %02X:%08X %s\r\n", xcpt->ExceptionAddress, sectionNumber, sectionOffset, szModuleName); + + ctx = ExceptionInfo->ContextRecord; + + log_printf("\r\nRegisters:\r\n"); + log_printf( + "EAX:%08X\r\nEBX:%08X\r\nECX:%08X\r\nEDX:%08X\r\nESI:%08X\r\nEDI:%08X\r\n", + ctx->Eax, + ctx->Ebx, + ctx->Ecx, + ctx->Edx, + ctx->Esi, + ctx->Edi); + log_printf("CS:EIP:%04X:%08X\r\n", ctx->SegCs, ctx->Eip); + log_printf("SS:ESP:%04X:%08X EBP:%08X\r\n", ctx->SegSs, ctx->Esp, ctx->Ebp); + log_printf("DS:%04X ES:%04X FS:%04X GS:%04X\r\n", ctx->SegDs, ctx->SegEs, ctx->SegFs, ctx->SegGs); + + log_printf("Flags:%08X\r\n", ctx->EFlags); + fault_call_stack((void *)ctx->Eip, (STACK_FRAME *)ctx->Ebp); + + log_printf("Stack bytes:\r\n"); + fault_hex_format((BYTE *)ctx->Esp, 768); + + log_printf("Code bytes:\r\n"); + fault_hex_format((BYTE *)ctx->Eip, 16); + + log_printf("\r\n"); + log_flush(TRUE); + + if (lpTopLevelExceptionFilter) + return lpTopLevelExceptionFilter(ExceptionInfo); + return EXCEPTION_CONTINUE_SEARCH; } LPTOP_LEVEL_EXCEPTION_FILTER fault_get_filter() diff --git a/Source/fault.h b/Source/fault.h index 4ed85ea17..192287ce8 100644 --- a/Source/fault.h +++ b/Source/fault.h @@ -6,24 +6,9 @@ #ifndef __FAULT_H__ #define __FAULT_H__ -typedef struct STACK_FRAME { - struct STACK_FRAME *pNext; - void *pCallRet; -} STACK_FRAME; - -extern int fault_unused; extern LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter; -void fault_init_filter(); -void fault_cleanup_filter_atexit(); -LPTOP_LEVEL_EXCEPTION_FILTER __cdecl fault_cleanup_filter(); LONG __stdcall TopLevelExceptionFilter(PEXCEPTION_POINTERS ExceptionInfo); -void fault_hex_format(BYTE *ptr, DWORD numBytes); -void fault_unknown_module(LPCVOID lpAddress, LPSTR lpModuleName, int iMaxLength, int *sectionNum, int *sectionOffset); -void fault_call_stack(void *instr, STACK_FRAME *stackAddr); -char *fault_get_error_type(DWORD dwMessageId, LPSTR lpString1, DWORD nSize); -void *fault_set_filter(void *unused); -LPTOP_LEVEL_EXCEPTION_FILTER fault_reset_filter(void *unused); LPTOP_LEVEL_EXCEPTION_FILTER fault_get_filter(); #endif /* __FAULT_H__ */ From 3d0f92f19d1e923bcbded7d9483cb071319fe685 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 19:32:00 +0100 Subject: [PATCH 11/16] Reorder gamemenu.h --- Source/gamemenu.cpp | 96 ++++++++++++++++++++++----------------------- Source/gamemenu.h | 21 ---------- 2 files changed, 48 insertions(+), 69 deletions(-) diff --git a/Source/gamemenu.cpp b/Source/gamemenu.cpp index 68bf1f660..a733fc9fd 100644 --- a/Source/gamemenu.cpp +++ b/Source/gamemenu.cpp @@ -79,17 +79,7 @@ char *jogging_title = "Fast Walk"; const char *const color_cycling_toggle_names[] = { "Color Cycling Off", "Color Cycling On" }; #endif -void gamemenu_on() -{ - if (gbMaxPlayers == 1) { - gmenu_set_items(sgSingleMenu, gamemenu_update_single); - } else { - gmenu_set_items(sgMultiMenu, gamemenu_update_multi); - } - PressEscKey(); -} - -void gamemenu_update_single(TMenuItem *pMenuItems) +static void gamemenu_update_single(TMenuItem *pMenuItems) { BOOL enable; @@ -102,11 +92,21 @@ void gamemenu_update_single(TMenuItem *pMenuItems) gmenu_enable(&sgSingleMenu[0], enable); } -void gamemenu_update_multi(TMenuItem *pMenuItems) +static void gamemenu_update_multi(TMenuItem *pMenuItems) { gmenu_enable(&sgMultiMenu[2], deathflag); } +void gamemenu_on() +{ + if (gbMaxPlayers == 1) { + gmenu_set_items(sgSingleMenu, gamemenu_update_single); + } else { + gmenu_set_items(sgMultiMenu, gamemenu_update_multi); + } + PressEscKey(); +} + void gamemenu_off() { gmenu_set_items(NULL, NULL); @@ -208,26 +208,7 @@ void gamemenu_restart_town(BOOL bActivate) NetSendCmd(TRUE, CMD_RETOWN); } -void gamemenu_options(BOOL bActivate) -{ - gamemenu_get_music(); - gamemenu_get_sound(); -#ifdef HELLFIRE - gamemenu_jogging(); -#endif - gamemenu_get_gamma(); -#ifndef HELLFIRE - gamemenu_get_color_cycling(); -#endif - gmenu_set_items(sgOptionsMenu, NULL); -} - -void gamemenu_get_music() -{ - gamemenu_sound_music_toggle(music_toggle_names, sgOptionsMenu, sound_get_or_set_music_volume(1)); -} - -void gamemenu_sound_music_toggle(const char *const *names, TMenuItem *menu_item, int volume) +static void gamemenu_sound_music_toggle(const char *const *names, TMenuItem *menu_item, int volume) { if (gbSndInited) { menu_item->dwFlags |= GMENU_ENABLED | GMENU_SLIDER; @@ -241,13 +222,23 @@ void gamemenu_sound_music_toggle(const char *const *names, TMenuItem *menu_item, menu_item->pszStr = names[1]; } -void gamemenu_get_sound() +static int gamemenu_slider_music_sound(TMenuItem *menu_item) +{ + return gmenu_slider_get(menu_item, VOLUME_MIN, VOLUME_MAX); +} + +static void gamemenu_get_music() +{ + gamemenu_sound_music_toggle(music_toggle_names, sgOptionsMenu, sound_get_or_set_music_volume(1)); +} + +static void gamemenu_get_sound() { gamemenu_sound_music_toggle(sound_toggle_names, &sgOptionsMenu[1], sound_get_or_set_sound_volume(1)); } #ifdef HELLFIRE -void gamemenu_jogging() +static void gamemenu_jogging() { gmenu_slider_steps(&sgOptionsMenu[3], 2); gmenu_slider_set(&sgOptionsMenu[3], 0, 1, jogging_opt); @@ -255,17 +246,36 @@ void gamemenu_jogging() } #endif +static void gamemenu_get_gamma() +{ + gmenu_slider_steps(&sgOptionsMenu[2], 15); + gmenu_slider_set(&sgOptionsMenu[2], 30, 100, UpdateGamma(0)); +} + #ifndef HELLFIRE -void gamemenu_get_color_cycling() +static void gamemenu_get_color_cycling() { sgOptionsMenu[3].pszStr = color_cycling_toggle_names[palette_get_color_cycling() & 1]; } #endif -void gamemenu_get_gamma() +static int gamemenu_slider_gamma() { - gmenu_slider_steps(&sgOptionsMenu[2], 15); - gmenu_slider_set(&sgOptionsMenu[2], 30, 100, UpdateGamma(0)); + return gmenu_slider_get(&sgOptionsMenu[2], 30, 100); +} + +void gamemenu_options(BOOL bActivate) +{ + gamemenu_get_music(); + gamemenu_get_sound(); +#ifdef HELLFIRE + gamemenu_jogging(); +#endif + gamemenu_get_gamma(); +#ifndef HELLFIRE + gamemenu_get_color_cycling(); +#endif + gmenu_set_items(sgOptionsMenu, NULL); } void gamemenu_music_volume(BOOL bActivate) @@ -322,11 +332,6 @@ void gamemenu_music_volume(BOOL bActivate) gamemenu_get_music(); } -int gamemenu_slider_music_sound(TMenuItem *menu_item) -{ - return gmenu_slider_get(menu_item, VOLUME_MIN, VOLUME_MAX); -} - void gamemenu_sound_volume(BOOL bActivate) { int volume; @@ -384,11 +389,6 @@ void gamemenu_gamma(BOOL bActivate) gamemenu_get_gamma(); } -int gamemenu_slider_gamma() -{ - return gmenu_slider_get(&sgOptionsMenu[2], 30, 100); -} - #ifndef HELLFIRE void gamemenu_color_cycling(BOOL bActivate) { diff --git a/Source/gamemenu.h b/Source/gamemenu.h index a1da138c3..451008d71 100644 --- a/Source/gamemenu.h +++ b/Source/gamemenu.h @@ -12,8 +12,6 @@ extern BOOL jogging_opt; #endif void gamemenu_on(); -void gamemenu_update_single(TMenuItem *pMenuItems); -void gamemenu_update_multi(TMenuItem *pMenuItems); void gamemenu_off(); void gamemenu_handle_previous(); void gamemenu_previous(BOOL bActivate); @@ -23,33 +21,14 @@ void gamemenu_load_game(BOOL bActivate); void gamemenu_save_game(BOOL bActivate); void gamemenu_restart_town(BOOL bActivate); void gamemenu_options(BOOL bActivate); -void gamemenu_get_music(); -void gamemenu_sound_music_toggle(const char *const *names, TMenuItem *menu_item, int gamma); -void gamemenu_get_sound(); -#ifdef HELLFIRE -void gamemenu_jogging(); -#endif -#ifndef HELLFIRE -void gamemenu_get_color_cycling(); -#endif -void gamemenu_get_gamma(); void gamemenu_music_volume(BOOL bActivate); -int gamemenu_slider_music_sound(TMenuItem *menu_item); void gamemenu_sound_volume(BOOL bActivate); #ifdef HELLFIRE void gamemenu_loadjog(BOOL bActivate); #endif void gamemenu_gamma(BOOL bActivate); -int gamemenu_slider_gamma(); #ifndef HELLFIRE void gamemenu_color_cycling(BOOL bActivate); #endif -/* rdata */ -extern const char *const music_toggle_names[]; -extern const char *const sound_toggle_names[]; -#ifndef HELLFIRE -extern const char *const color_cycling_toggle_names[]; -#endif - #endif /* __GAMEMENU_H__ */ From 66f5178e9384fb8e59342e66e63b628f4f0fdf66 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 20:01:42 +0100 Subject: [PATCH 12/16] Reorder gmenu.cpp --- Source/gmenu.cpp | 242 +++++++++++++++++++++++------------------------ Source/gmenu.h | 23 ----- 2 files changed, 121 insertions(+), 144 deletions(-) diff --git a/Source/gmenu.cpp b/Source/gmenu.cpp index 1fb04af15..0aa313bac 100644 --- a/Source/gmenu.cpp +++ b/Source/gmenu.cpp @@ -49,17 +49,7 @@ const BYTE lfontkern[] = { 11, 10, 12, 11, 21, 23 }; -void gmenu_draw_pause() -{ - if (currlevel != 0) - RedBack(); - if (!sgpCurrentMenu) { - light_table_index = 0; - gmenu_print_text(316 + PANEL_LEFT, 336, "Pause"); - } -} - -void gmenu_print_text(int x, int y, const char *pszStr) +static void gmenu_print_text(int x, int y, const char *pszStr) { BYTE c; @@ -72,6 +62,16 @@ void gmenu_print_text(int x, int y, const char *pszStr) } } +void gmenu_draw_pause() +{ + if (currlevel != 0) + RedBack(); + if (!sgpCurrentMenu) { + light_table_index = 0; + gmenu_print_text(316 + PANEL_LEFT, 336, "Pause"); + } +} + void FreeGMenu() { MemFreeDbg(sgpLogo); @@ -108,30 +108,7 @@ BOOL gmenu_is_active() return sgpCurrentMenu != NULL; } -void gmenu_set_items(TMenuItem *pItem, void (*gmFunc)(TMenuItem *)) -{ - int i; - - PauseMode = 0; - mouseNavigation = FALSE; - sgpCurrentMenu = pItem; - dword_63447C = gmFunc; - if (gmFunc) { - dword_63447C(sgpCurrentMenu); - pItem = sgpCurrentMenu; - } - sgCurrentMenuIdx = 0; - if (sgpCurrentMenu) { - for (i = 0; sgpCurrentMenu[i].fnMenu; i++) { - sgCurrentMenuIdx++; - } - } - // BUGFIX: OOB access when sgCurrentMenuIdx is 0; should be set to NULL instead. - sgpCurrItem = &sgpCurrentMenu[sgCurrentMenuIdx - 1]; - gmenu_up_down(TRUE); -} - -void gmenu_up_down(BOOL isDown) +static void gmenu_up_down(BOOL isDown) { int i; @@ -161,50 +138,58 @@ void gmenu_up_down(BOOL isDown) } } -void gmenu_draw() +void gmenu_set_items(TMenuItem *pItem, void (*gmFunc)(TMenuItem *)) { - int y; - TMenuItem *i; - DWORD ticks; + int i; + PauseMode = 0; + mouseNavigation = FALSE; + sgpCurrentMenu = pItem; + dword_63447C = gmFunc; + if (gmFunc) { + dword_63447C(sgpCurrentMenu); + pItem = sgpCurrentMenu; + } + sgCurrentMenuIdx = 0; if (sgpCurrentMenu) { - if (dword_63447C) - dword_63447C(sgpCurrentMenu); -#ifdef HELLFIRE - ticks = GetTickCount(); - if ((int)(ticks - LogoAnim_tick) > 25) { - LogoAnim_frame++; - if (LogoAnim_frame > 16) - LogoAnim_frame = 1; - LogoAnim_tick = ticks; - } - CelDraw((SCREEN_WIDTH - 430) / 2 + SCREEN_X, 102 + SCREEN_Y, sgpLogo, LogoAnim_frame, 430); -#else - CelDraw((SCREEN_WIDTH - 296) / 2 + SCREEN_X, 102 + SCREEN_Y, sgpLogo, 1, 296); -#endif - y = 160 + SCREEN_Y; - i = sgpCurrentMenu; - if (sgpCurrentMenu->fnMenu) { - while (i->fnMenu) { - gmenu_draw_menu_item(i, y); - i++; - y += 45; - } + for (i = 0; sgpCurrentMenu[i].fnMenu; i++) { + sgCurrentMenuIdx++; } + } + // BUGFIX: OOB access when sgCurrentMenuIdx is 0; should be set to NULL instead. + sgpCurrItem = &sgpCurrentMenu[sgCurrentMenuIdx - 1]; + gmenu_up_down(TRUE); +} -#ifndef HELLFIRE - ticks = GetTickCount(); -#endif - if ((int)(ticks - PentSpin_tick) > 25) { // BUGFIX: thould be 50ms - PentSpin_frame++; - if (PentSpin_frame == 9) - PentSpin_frame = 1; - PentSpin_tick = ticks; - } +static void gmenu_clear_buffer(int x, int y, int width, int height) +{ + BYTE *i; + + i = gpBuffer + PitchTbl[y] + x; + while (height--) { + memset(i, 205, width); + i -= BUFFER_WIDTH; + } +} + +static int gmenu_get_lfont(TMenuItem *pItem) +{ + const char *text; + int i; + BYTE c; + + if (pItem->dwFlags & GMENU_SLIDER) + return 490; + text = pItem->pszStr; + i = 0; + while (*text) { + c = gbFontTransTbl[(BYTE)*text++]; + i += lfontkern[lfontframe[c]] + 2; } + return i - 2; } -void gmenu_draw_menu_item(TMenuItem *pItem, int y) +static void gmenu_draw_menu_item(TMenuItem *pItem, int y) { DWORD w, x, nSteps, step, pos, t; #ifndef HELLFIRE @@ -239,32 +224,68 @@ void gmenu_draw_menu_item(TMenuItem *pItem, int y) } } -void gmenu_clear_buffer(int x, int y, int width, int height) +void gmenu_draw() { - BYTE *i; + int y; + TMenuItem *i; + DWORD ticks; - i = gpBuffer + PitchTbl[y] + x; - while (height--) { - memset(i, 205, width); - i -= BUFFER_WIDTH; + if (sgpCurrentMenu) { + if (dword_63447C) + dword_63447C(sgpCurrentMenu); +#ifdef HELLFIRE + ticks = GetTickCount(); + if ((int)(ticks - LogoAnim_tick) > 25) { + LogoAnim_frame++; + if (LogoAnim_frame > 16) + LogoAnim_frame = 1; + LogoAnim_tick = ticks; + } + CelDraw((SCREEN_WIDTH - 430) / 2 + SCREEN_X, 102 + SCREEN_Y, sgpLogo, LogoAnim_frame, 430); +#else + CelDraw((SCREEN_WIDTH - 296) / 2 + SCREEN_X, 102 + SCREEN_Y, sgpLogo, 1, 296); +#endif + y = 160 + SCREEN_Y; + i = sgpCurrentMenu; + if (sgpCurrentMenu->fnMenu) { + while (i->fnMenu) { + gmenu_draw_menu_item(i, y); + i++; + y += 45; + } + } + +#ifndef HELLFIRE + ticks = GetTickCount(); +#endif + if ((int)(ticks - PentSpin_tick) > 25) { // BUGFIX: thould be 50ms + PentSpin_frame++; + if (PentSpin_frame == 9) + PentSpin_frame = 1; + PentSpin_tick = ticks; + } } } -int gmenu_get_lfont(TMenuItem *pItem) +static void gmenu_left_right(BOOL isRight) { - const char *text; - int i; - BYTE c; + int step; - if (pItem->dwFlags & GMENU_SLIDER) - return 490; - text = pItem->pszStr; - i = 0; - while (*text) { - c = gbFontTransTbl[(BYTE)*text++]; - i += lfontkern[lfontframe[c]] + 2; + if (sgpCurrItem->dwFlags & GMENU_SLIDER) { + step = sgpCurrItem->dwFlags & 0xFFF; + if (isRight) { + if (step == (int)(sgpCurrItem->dwFlags & 0xFFF000) >> 12) + return; + step++; + } else { + if (!step) + return; + step--; + } + sgpCurrItem->dwFlags &= 0xFFFFF000; + sgpCurrItem->dwFlags |= step; + sgpCurrItem->fnMenu(FALSE); } - return i - 2; } BOOL gmenu_presskeys(int vkey) @@ -300,25 +321,19 @@ BOOL gmenu_presskeys(int vkey) return TRUE; } -void gmenu_left_right(BOOL isRight) +static BOOLEAN gmenu_get_mouse_slider(int *plOffset) { - int step; - - if (sgpCurrItem->dwFlags & GMENU_SLIDER) { - step = sgpCurrItem->dwFlags & 0xFFF; - if (isRight) { - if (step == (int)(sgpCurrItem->dwFlags & 0xFFF000) >> 12) - return; - step++; - } else { - if (!step) - return; - step--; - } - sgpCurrItem->dwFlags &= 0xFFFFF000; - sgpCurrItem->dwFlags |= step; - sgpCurrItem->fnMenu(FALSE); + *plOffset = 282; + if (MouseX < 282 + PANEL_LEFT) { + *plOffset = 0; + return FALSE; } + if (MouseX > 538 + PANEL_LEFT) { + *plOffset = 256; + return FALSE; + } + *plOffset = MouseX - 282 - PANEL_LEFT; + return TRUE; } BOOL gmenu_on_mouse_move() @@ -338,21 +353,6 @@ BOOL gmenu_on_mouse_move() return TRUE; } -BOOLEAN gmenu_get_mouse_slider(int *plOffset) -{ - *plOffset = 282; - if (MouseX < 282 + PANEL_LEFT) { - *plOffset = 0; - return FALSE; - } - if (MouseX > 538 + PANEL_LEFT) { - *plOffset = 256; - return FALSE; - } - *plOffset = MouseX - 282 - PANEL_LEFT; - return TRUE; -} - BOOL gmenu_left_mouse(BOOL isDown) { TMenuItem *pItem; diff --git a/Source/gmenu.h b/Source/gmenu.h index 0cd15635d..f02901a04 100644 --- a/Source/gmenu.h +++ b/Source/gmenu.h @@ -6,41 +6,18 @@ #ifndef __GMENU_H__ #define __GMENU_H__ -extern BYTE *optbar_cel; -extern BOOLEAN mouseNavigation; -extern BYTE *PentSpin_cel; -extern BYTE *BigTGold_cel; -extern int dword_634474; -extern char byte_634478; -extern void (*dword_63447C)(TMenuItem *); -extern TMenuItem *sgpCurrentMenu; -extern BYTE *option_cel; -extern int sgCurrentMenuIdx; - void gmenu_draw_pause(); -void gmenu_print_text(int x, int y, const char *pszStr); void FreeGMenu(); void gmenu_init_menu(); BOOL gmenu_is_active(); void gmenu_set_items(TMenuItem *pItem, void (*gmFunc)(TMenuItem *)); -void gmenu_up_down(BOOL isDown); void gmenu_draw(); -void gmenu_draw_menu_item(TMenuItem *pItem, int y); -void gmenu_clear_buffer(int x, int y, int width, int height); -int gmenu_get_lfont(TMenuItem *pItem); BOOL gmenu_presskeys(int vkey); -void gmenu_left_right(BOOL isRight); BOOL gmenu_on_mouse_move(); -BOOLEAN gmenu_get_mouse_slider(int *plOffset); BOOL gmenu_left_mouse(BOOL isDown); void gmenu_enable(TMenuItem *pMenuItem, BOOL enable); void gmenu_slider_set(TMenuItem *pItem, int min, int max, int gamma); int gmenu_slider_get(TMenuItem *pItem, int min, int max); void gmenu_slider_steps(TMenuItem *pItem, int dwTicks); -/* rdata */ - -extern const BYTE lfontframe[]; -extern const BYTE lfontkern[]; - #endif /* __GMENU_H__ */ From e9ab91789f5cd118453cec4d08fcf92db261557a Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 20:06:42 +0100 Subject: [PATCH 13/16] Reorder help.cpp --- Source/help.cpp | 40 ++++++++++++++++++++-------------------- Source/help.h | 8 -------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/Source/help.cpp b/Source/help.cpp index ba01dff56..b7194e613 100644 --- a/Source/help.cpp +++ b/Source/help.cpp @@ -447,6 +447,26 @@ void InitHelp() displayinghelp[0] = 0; } +static void DrawHelpLine(int x, int y, char *text, char color) +{ + int off, width; + BYTE c; + + width = 0; + off = PitchTbl[SStringY[y] + 44 + SCREEN_Y] + x + 32 + PANEL_X; + while (*text) { + c = gbFontTransTbl[(BYTE)*text]; + text++; + c = fontframe[c]; + width += fontkern[c] + 1; + if (c) { + if (width <= 577) + PrintChar(off, c, color); + } + off += fontkern[c] + 1; + } +} + void DrawHelp() { int i, c, w; @@ -541,26 +561,6 @@ void DrawHelp() PrintSString(0, 23, TRUE, "Press ESC to end or the arrow keys to scroll.", COL_GOLD, 0); } -void DrawHelpLine(int x, int y, char *text, char color) -{ - int off, width; - BYTE c; - - width = 0; - off = PitchTbl[SStringY[y] + 44 + SCREEN_Y] + x + 32 + PANEL_X; - while (*text) { - c = gbFontTransTbl[(BYTE)*text]; - text++; - c = fontframe[c]; - width += fontkern[c] + 1; - if (c) { - if (width <= 577) - PrintChar(off, c, color); - } - off += fontkern[c] + 1; - } -} - void DisplayHelp() { help_select_line = 0; diff --git a/Source/help.h b/Source/help.h index 32175f9a6..e7a16583d 100644 --- a/Source/help.h +++ b/Source/help.h @@ -6,20 +6,12 @@ #ifndef __HELP_H__ #define __HELP_H__ -extern int help_select_line; -extern int dword_634494; extern BOOL helpflag; -extern int displayinghelp[22]; -extern int HelpTop; void InitHelp(); void DrawHelp(); -void DrawHelpLine(int x, int y, char *text, char color); void DisplayHelp(); void HelpScrollUp(); void HelpScrollDown(); -/* rdata */ -extern const char gszHelpText[]; - #endif /* __HELP_H__ */ From 1f1e689db0878889aec95045bb24dd98e3420262 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 20:21:44 +0100 Subject: [PATCH 14/16] Reorder init.cpp --- Source/init.cpp | 550 ++++++++++++++++++++++++------------------------ Source/init.h | 34 --- 2 files changed, 275 insertions(+), 309 deletions(-) diff --git a/Source/init.cpp b/Source/init.cpp index 0194ac4fa..0e76365da 100644 --- a/Source/init.cpp +++ b/Source/init.cpp @@ -51,85 +51,7 @@ HANDLE hfopt2_mpq; char gszVersionNumber[MAX_PATH] = "internal version unknown"; char gszProductName[MAX_PATH] = "Diablo v1.09"; -void init_cleanup(BOOL show_cursor) -{ - pfile_flush_W(); - init_disable_screensaver(FALSE); - init_run_office_from_start_menu(); - - if (diabdat_mpq) { - SFileCloseArchive(diabdat_mpq); - diabdat_mpq = NULL; - } - if (patch_rt_mpq) { - SFileCloseArchive(patch_rt_mpq); - patch_rt_mpq = NULL; - } - if (hellfire_mpq) { - SFileCloseArchive(hellfire_mpq); - hellfire_mpq = NULL; - } -#ifdef HELLFIRE - if (hfmonk_mpq) { - SFileCloseArchive(hfmonk_mpq); - hfmonk_mpq = NULL; - } - if (hfbard_mpq) { - SFileCloseArchive(hfbard_mpq); - hfbard_mpq = NULL; - } - if (hfbarb_mpq) { - SFileCloseArchive(hfbarb_mpq); - hfbarb_mpq = NULL; - } - if (hfmusic_mpq) { - SFileCloseArchive(hfmusic_mpq); - hfmusic_mpq = NULL; - } - if (hfvoice_mpq) { - SFileCloseArchive(hfvoice_mpq); - hfvoice_mpq = NULL; - } - if (hfopt1_mpq) { - SFileCloseArchive(hfopt1_mpq); - hfopt1_mpq = NULL; - } - if (hfopt2_mpq) { - SFileCloseArchive(hfopt2_mpq); - hfopt2_mpq = NULL; - } -#endif - UiDestroy(); - effects_cleanup_sfx(); - sound_cleanup(); - NetClose(); - dx_cleanup(); - engine_debug_trap(show_cursor); - StormDestroy(); - - if (show_cursor) - ShowCursor(TRUE); -} - -void init_run_office_from_start_menu() -{ - LPITEMIDLIST idl; - - if (!killed_mom_parent) { - return; - } - - killed_mom_parent = FALSE; - char szPath[256] = ""; /// BUGFIX: size should be at least 'MAX_PATH' - idl = NULL; - - if (SHGetSpecialFolderLocation(GetDesktopWindow(), CSIDL_STARTMENU, &idl) == NOERROR) { - SHGetPathFromIDList(idl, szPath); - init_run_office(szPath); - } -} - -void init_run_office(char *dir) +static void init_run_office(char *dir) { HANDLE hSearch; WIN32_FIND_DATA find; @@ -168,7 +90,65 @@ void init_run_office(char *dir) FindClose(hSearch); } -void init_disable_screensaver(BOOLEAN disable) +static HWND init_find_mom_parent() +{ + HWND i, handle; + char ClassName[256]; + + for (i = GetForegroundWindow();; i = GetWindow(handle, GW_HWNDNEXT)) { + handle = i; + if (!i) + break; + GetClassName(i, ClassName, 255); + if (!_strcmpi(ClassName, "MOM Parent")) + break; + } + return handle; +} + +static void init_kill_mom_parent() +{ + HWND handle; + + handle = init_find_mom_parent(); + if (handle != NULL) { + PostMessage(handle, WM_CLOSE, 0, 0); + killed_mom_parent = TRUE; + } +} + +static void init_await_mom_parent_exit() +{ + DWORD tick; + + tick = GetTickCount(); + if (!init_find_mom_parent()) { + return; + } + do { + Sleep(250); + } while (GetTickCount() - tick <= 4000 && init_find_mom_parent() != NULL); +} + +static void init_run_office_from_start_menu() +{ + LPITEMIDLIST idl; + + if (!killed_mom_parent) { + return; + } + + killed_mom_parent = FALSE; + char szPath[256] = ""; /// BUGFIX: size should be at least 'MAX_PATH' + idl = NULL; + + if (SHGetSpecialFolderLocation(GetDesktopWindow(), CSIDL_STARTMENU, &idl) == NOERROR) { + SHGetPathFromIDList(idl, szPath); + init_run_office(szPath); + } +} + +static void init_disable_screensaver(BOOLEAN disable) { BOOLEAN enabled; char Data[16]; @@ -200,89 +180,189 @@ void init_disable_screensaver(BOOLEAN disable) RegCloseKey(phkResult); } -void init_create_window(int nCmdShow) +void init_cleanup(BOOL show_cursor) { - int nWidth, nHeight; - HWND hWnd; - WNDCLASSEXA wcex; + pfile_flush_W(); + init_disable_screensaver(FALSE); + init_run_office_from_start_menu(); - init_kill_mom_parent(); - pfile_init_save_directory(); - memset(&wcex, 0, sizeof(wcex)); - wcex.cbSize = sizeof(wcex); - wcex.style = CS_HREDRAW | CS_VREDRAW; - wcex.lpfnWndProc = WindowProc; - wcex.hInstance = ghInst; - wcex.hIcon = LoadIcon(ghInst, MAKEINTRESOURCE(IDI_ICON1)); - wcex.hCursor = LoadCursor(NULL, IDC_ARROW); - wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); - wcex.lpszMenuName = GAME_NAME; - wcex.lpszClassName = "DIABLO"; - wcex.hIconSm = (HICON)LoadImage(ghInst, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR); - if (!RegisterClassEx(&wcex)) - app_fatal("Unable to register window class"); - if (GetSystemMetrics(SM_CXSCREEN) < SCREEN_WIDTH) - nWidth = SCREEN_WIDTH; - else - nWidth = GetSystemMetrics(SM_CXSCREEN); - if (GetSystemMetrics(SM_CYSCREEN) < SCREEN_HEIGHT) - nHeight = SCREEN_HEIGHT; - else - nHeight = GetSystemMetrics(SM_CYSCREEN); - hWnd = CreateWindowEx(0, "DIABLO", GAME_NAME, WS_POPUP, 0, 0, nWidth, nHeight, NULL, NULL, ghInst, NULL); - if (!hWnd) - app_fatal("Unable to create main window"); - ShowWindow(hWnd, SW_SHOWNORMAL); // nCmdShow used only in beta: ShowWindow(hWnd, nCmdShow) - UpdateWindow(hWnd); - init_await_mom_parent_exit(); - dx_init(hWnd); - BlackPalette(); - snd_init(hWnd); - init_archives(); - init_disable_screensaver(TRUE); + if (diabdat_mpq) { + SFileCloseArchive(diabdat_mpq); + diabdat_mpq = NULL; + } + if (patch_rt_mpq) { + SFileCloseArchive(patch_rt_mpq); + patch_rt_mpq = NULL; + } + if (hellfire_mpq) { + SFileCloseArchive(hellfire_mpq); + hellfire_mpq = NULL; + } +#ifdef HELLFIRE + if (hfmonk_mpq) { + SFileCloseArchive(hfmonk_mpq); + hfmonk_mpq = NULL; + } + if (hfbard_mpq) { + SFileCloseArchive(hfbard_mpq); + hfbard_mpq = NULL; + } + if (hfbarb_mpq) { + SFileCloseArchive(hfbarb_mpq); + hfbarb_mpq = NULL; + } + if (hfmusic_mpq) { + SFileCloseArchive(hfmusic_mpq); + hfmusic_mpq = NULL; + } + if (hfvoice_mpq) { + SFileCloseArchive(hfvoice_mpq); + hfvoice_mpq = NULL; + } + if (hfopt1_mpq) { + SFileCloseArchive(hfopt1_mpq); + hfopt1_mpq = NULL; + } + if (hfopt2_mpq) { + SFileCloseArchive(hfopt2_mpq); + hfopt2_mpq = NULL; + } +#endif + UiDestroy(); + effects_cleanup_sfx(); + sound_cleanup(); + NetClose(); + dx_cleanup(); + engine_debug_trap(show_cursor); + StormDestroy(); + + if (show_cursor) + ShowCursor(TRUE); } -void init_kill_mom_parent() +static void init_strip_trailing_slash(char *path) { - HWND handle; + char *result; - handle = init_find_mom_parent(); - if (handle != NULL) { - PostMessage(handle, WM_CLOSE, 0, 0); - killed_mom_parent = TRUE; + result = strrchr(path, '\\'); + if (result) { + if (!result[1]) + *result = 0; + } +} + +static BOOL init_read_test_file(char *pszPath, const char *pszArchive, int dwPriority, HANDLE *phArchive) +{ + DWORD dwSize; + char *pszDrive, *pszRoot; + char szDrive[MAX_PATH]; + + dwSize = GetLogicalDriveStrings(sizeof(szDrive), szDrive); + if (dwSize == 0 || dwSize > sizeof(szDrive)) { + return FALSE; + } + + while (*pszArchive == '\\') { + pszArchive++; + } + + pszDrive = szDrive; + if (*pszDrive == '\0') { + return FALSE; + } + while (1) { + pszRoot = pszDrive; + while (*pszDrive++ != '\0') + ; + if (GetDriveType(pszRoot) == DRIVE_CDROM) { + strcpy(pszPath, pszRoot); + strcat(pszPath, pszArchive); + if (SFileOpenArchive(pszPath, dwPriority, FS_CD, phArchive)) { + break; + } + } + if (*pszDrive == '\0') { + return FALSE; + } } + + return TRUE; } -HWND init_find_mom_parent() +static HANDLE init_test_access(char *mpq_path, const char *mpq_name, const char *reg_loc, int dwPriority, int fs) { - HWND i, handle; - char ClassName[256]; + char *last_slash_pos; + char Filename[MAX_PATH]; + char Buffer[MAX_PATH]; + char archive_path[MAX_PATH]; + HANDLE archive; - for (i = GetForegroundWindow();; i = GetWindow(handle, GW_HWNDNEXT)) { - handle = i; - if (!i) - break; - GetClassName(i, ClassName, 255); - if (!_strcmpi(ClassName, "MOM Parent")) - break; + if (!GetCurrentDirectory(sizeof(Buffer), Buffer)) + app_fatal("Can't get program path"); + init_strip_trailing_slash(Buffer); + if (!SFileSetBasePath(Buffer)) + app_fatal("SFileSetBasePath"); + if (!GetModuleFileName(ghInst, Filename, sizeof(Filename))) + app_fatal("Can't get program name"); + last_slash_pos = strrchr(Filename, '\\'); + if (last_slash_pos) + *last_slash_pos = '\0'; + init_strip_trailing_slash(Filename); + strcpy(mpq_path, Buffer); + strcat(mpq_path, mpq_name); + if (SFileOpenArchive(mpq_path, dwPriority, fs, &archive)) + return archive; + if (strcmp(Filename, Buffer)) { + strcpy(mpq_path, Filename); + strcat(mpq_path, mpq_name); + if (SFileOpenArchive(mpq_path, dwPriority, fs, &archive)) + return archive; } - return handle; + archive_path[0] = '\0'; + if (reg_loc) { + if (SRegLoadString("Archives", reg_loc, 0, archive_path, sizeof(archive_path))) { + init_strip_trailing_slash(archive_path); + strcpy(mpq_path, archive_path); + strcat(mpq_path, mpq_name); + if (SFileOpenArchive(mpq_path, dwPriority, fs, &archive)) + return archive; + } + } + if (fs != FS_PC && init_read_test_file(archive_path, mpq_name, dwPriority, &archive)) { + strcpy(mpq_path, archive_path); + return archive; + } + return NULL; } -void init_await_mom_parent_exit() +static void init_get_file_info() { - DWORD tick; + DWORD dwLen; + void *pBlock; + unsigned int uBytes; + DWORD dwHandle; + VS_FIXEDFILEINFO *lpBuffer; - tick = GetTickCount(); - if (!init_find_mom_parent()) { - return; + if (GetModuleFileName(ghInst, diablo_exe_path, sizeof(diablo_exe_path))) { + dwLen = GetFileVersionInfoSize(diablo_exe_path, &dwHandle); + if (dwLen) { + pBlock = DiabloAllocPtr(dwLen); + if (GetFileVersionInfo(diablo_exe_path, 0, dwLen, pBlock)) { + if (VerQueryValue(pBlock, "\\", (LPVOID *)&lpBuffer, &uBytes)) + sprintf( + gszVersionNumber, + "version %d.%d.%d.%d", + lpBuffer->dwProductVersionMS >> 16, + lpBuffer->dwProductVersionMS & 0xFFFF, + lpBuffer->dwProductVersionLS >> 16, + lpBuffer->dwProductVersionLS & 0xFFFF); + } + mem_free_dbg(pBlock); + } } - do { - Sleep(250); - } while (GetTickCount() - tick <= 4000 && init_find_mom_parent() != NULL); } -void init_archives() +static void init_archives() { HANDLE fh; #ifdef COPYPROT @@ -339,125 +419,66 @@ void init_archives() #endif } -HANDLE init_test_access(char *mpq_path, const char *mpq_name, const char *reg_loc, int dwPriority, int fs) -{ - char *last_slash_pos; - char Filename[MAX_PATH]; - char Buffer[MAX_PATH]; - char archive_path[MAX_PATH]; - HANDLE archive; - - if (!GetCurrentDirectory(sizeof(Buffer), Buffer)) - app_fatal("Can't get program path"); - init_strip_trailing_slash(Buffer); - if (!SFileSetBasePath(Buffer)) - app_fatal("SFileSetBasePath"); - if (!GetModuleFileName(ghInst, Filename, sizeof(Filename))) - app_fatal("Can't get program name"); - last_slash_pos = strrchr(Filename, '\\'); - if (last_slash_pos) - *last_slash_pos = '\0'; - init_strip_trailing_slash(Filename); - strcpy(mpq_path, Buffer); - strcat(mpq_path, mpq_name); - if (SFileOpenArchive(mpq_path, dwPriority, fs, &archive)) - return archive; - if (strcmp(Filename, Buffer)) { - strcpy(mpq_path, Filename); - strcat(mpq_path, mpq_name); - if (SFileOpenArchive(mpq_path, dwPriority, fs, &archive)) - return archive; - } - archive_path[0] = '\0'; - if (reg_loc) { - if (SRegLoadString("Archives", reg_loc, 0, archive_path, sizeof(archive_path))) { - init_strip_trailing_slash(archive_path); - strcpy(mpq_path, archive_path); - strcat(mpq_path, mpq_name); - if (SFileOpenArchive(mpq_path, dwPriority, fs, &archive)) - return archive; - } - } - if (fs != FS_PC && init_read_test_file(archive_path, mpq_name, dwPriority, &archive)) { - strcpy(mpq_path, archive_path); - return archive; - } - return NULL; -} - -void init_strip_trailing_slash(char *path) +void init_create_window(int nCmdShow) { - char *result; + int nWidth, nHeight; + HWND hWnd; + WNDCLASSEXA wcex; - result = strrchr(path, '\\'); - if (result) { - if (!result[1]) - *result = 0; - } + init_kill_mom_parent(); + pfile_init_save_directory(); + memset(&wcex, 0, sizeof(wcex)); + wcex.cbSize = sizeof(wcex); + wcex.style = CS_HREDRAW | CS_VREDRAW; + wcex.lpfnWndProc = WindowProc; + wcex.hInstance = ghInst; + wcex.hIcon = LoadIcon(ghInst, MAKEINTRESOURCE(IDI_ICON1)); + wcex.hCursor = LoadCursor(NULL, IDC_ARROW); + wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); + wcex.lpszMenuName = GAME_NAME; + wcex.lpszClassName = "DIABLO"; + wcex.hIconSm = (HICON)LoadImage(ghInst, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR); + if (!RegisterClassEx(&wcex)) + app_fatal("Unable to register window class"); + if (GetSystemMetrics(SM_CXSCREEN) < SCREEN_WIDTH) + nWidth = SCREEN_WIDTH; + else + nWidth = GetSystemMetrics(SM_CXSCREEN); + if (GetSystemMetrics(SM_CYSCREEN) < SCREEN_HEIGHT) + nHeight = SCREEN_HEIGHT; + else + nHeight = GetSystemMetrics(SM_CYSCREEN); + hWnd = CreateWindowEx(0, "DIABLO", GAME_NAME, WS_POPUP, 0, 0, nWidth, nHeight, NULL, NULL, ghInst, NULL); + if (!hWnd) + app_fatal("Unable to create main window"); + ShowWindow(hWnd, SW_SHOWNORMAL); // nCmdShow used only in beta: ShowWindow(hWnd, nCmdShow) + UpdateWindow(hWnd); + init_await_mom_parent_exit(); + dx_init(hWnd); + BlackPalette(); + snd_init(hWnd); + init_archives(); + init_disable_screensaver(TRUE); } -BOOL init_read_test_file(char *pszPath, const char *pszArchive, int dwPriority, HANDLE *phArchive) +static void init_activate_window(HWND hWnd, BOOL bActive) { - DWORD dwSize; - char *pszDrive, *pszRoot; - char szDrive[MAX_PATH]; - - dwSize = GetLogicalDriveStrings(sizeof(szDrive), szDrive); - if (dwSize == 0 || dwSize > sizeof(szDrive)) { - return FALSE; - } - - while (*pszArchive == '\\') { - pszArchive++; - } + LONG dwNewLong; - pszDrive = szDrive; - if (*pszDrive == '\0') { - return FALSE; - } - while (1) { - pszRoot = pszDrive; - while (*pszDrive++ != '\0') - ; - if (GetDriveType(pszRoot) == DRIVE_CDROM) { - strcpy(pszPath, pszRoot); - strcat(pszPath, pszArchive); - if (SFileOpenArchive(pszPath, dwPriority, FS_CD, phArchive)) { - break; - } - } - if (*pszDrive == '\0') { - return FALSE; - } - } + gbActive = bActive; + UiAppActivate(bActive); + dwNewLong = GetWindowLong(hWnd, GWL_STYLE); - return TRUE; -} + if (gbActive && fullscreen) + dwNewLong &= ~WS_SYSMENU; + else + dwNewLong |= WS_SYSMENU; -void init_get_file_info() -{ - DWORD dwLen; - void *pBlock; - unsigned int uBytes; - DWORD dwHandle; - VS_FIXEDFILEINFO *lpBuffer; + SetWindowLong(hWnd, GWL_STYLE, dwNewLong); - if (GetModuleFileName(ghInst, diablo_exe_path, sizeof(diablo_exe_path))) { - dwLen = GetFileVersionInfoSize(diablo_exe_path, &dwHandle); - if (dwLen) { - pBlock = DiabloAllocPtr(dwLen); - if (GetFileVersionInfo(diablo_exe_path, 0, dwLen, pBlock)) { - if (VerQueryValue(pBlock, "\\", (LPVOID *)&lpBuffer, &uBytes)) - sprintf( - gszVersionNumber, - "version %d.%d.%d.%d", - lpBuffer->dwProductVersionMS >> 16, - lpBuffer->dwProductVersionMS & 0xFFFF, - lpBuffer->dwProductVersionLS >> 16, - lpBuffer->dwProductVersionLS & 0xFFFF); - } - mem_free_dbg(pBlock); - } + if (gbActive) { + force_redraw = 255; + ResetPal(); } } @@ -503,27 +524,6 @@ LRESULT __stdcall MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) return DefWindowProc(hWnd, Msg, wParam, lParam); } -void init_activate_window(HWND hWnd, BOOL bActive) -{ - LONG dwNewLong; - - gbActive = bActive; - UiAppActivate(bActive); - dwNewLong = GetWindowLong(hWnd, GWL_STYLE); - - if (gbActive && fullscreen) - dwNewLong &= ~WS_SYSMENU; - else - dwNewLong |= WS_SYSMENU; - - SetWindowLong(hWnd, GWL_STYLE, dwNewLong); - - if (gbActive) { - force_redraw = 255; - ResetPal(); - } -} - LRESULT __stdcall WindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { if (CurrentProc) diff --git a/Source/init.h b/Source/init.h index 70babe5b6..898772ecf 100644 --- a/Source/init.h +++ b/Source/init.h @@ -8,53 +8,19 @@ extern _SNETVERSIONDATA fileinfo; extern int gbActive; -extern char diablo_exe_path[MAX_PATH]; -extern HANDLE hellfire_mpq; -extern char patch_rt_mpq_path[MAX_PATH]; extern WNDPROC CurrentProc; extern HANDLE diabdat_mpq; -extern char diabdat_mpq_path[MAX_PATH]; -extern HANDLE patch_rt_mpq; -extern BOOL killed_mom_parent; -extern BOOLEAN screensaver_enabled_prev; #ifdef HELLFIRE -extern char hellfire_mpq_path[MAX_PATH]; -extern char hfmonk_mpq_path[MAX_PATH]; -extern char hfbard_mpq_path[MAX_PATH]; -extern char hfbarb_mpq_path[MAX_PATH]; -extern char hfmusic_mpq_path[MAX_PATH]; -extern char hfvoice_mpq_path[MAX_PATH]; -extern char hfopt1_mpq_path[MAX_PATH]; -extern char hfopt2_mpq_path[MAX_PATH]; -extern HANDLE hfmonk_mpq; extern HANDLE hfbard_mpq; extern HANDLE hfbarb_mpq; -extern HANDLE hfmusic_mpq; -extern HANDLE hfvoice_mpq; -extern HANDLE hfopt1_mpq; -extern HANDLE hfopt2_mpq; #endif void init_cleanup(BOOL show_cursor); -void init_run_office_from_start_menu(); -void init_run_office(char *dir); -void init_disable_screensaver(BOOLEAN disable); void init_create_window(int nCmdShow); -void init_kill_mom_parent(); -HWND init_find_mom_parent(); -void init_await_mom_parent_exit(); -void init_archives(); -HANDLE init_test_access(char *mpq_path, const char *mpq_name, const char *reg_loc, int flags, int fs); -void init_strip_trailing_slash(char *path); -BOOL init_read_test_file(char *pszPath, const char *pszArchive, int flags, HANDLE *phArchive); -void init_get_file_info(); LRESULT __stdcall MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); -void init_activate_window(HWND hWnd, BOOL bActive); LRESULT __stdcall WindowProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); WNDPROC SetWindowProc(WNDPROC NewProc); -/* rdata */ - /* data */ extern char gszVersionNumber[MAX_PATH]; From 31ec800160f8433a9430e05da4e32051e5d39b40 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 2 Nov 2020 20:38:48 +0100 Subject: [PATCH 15/16] Reorder Interface.cpp --- Source/interfac.cpp | 456 ++++++++++++++++++++++---------------------- Source/interfac.h | 11 -- 2 files changed, 228 insertions(+), 239 deletions(-) diff --git a/Source/interfac.cpp b/Source/interfac.cpp index 875113dd6..6aa1faf5c 100644 --- a/Source/interfac.cpp +++ b/Source/interfac.cpp @@ -15,238 +15,12 @@ const BYTE BarColor[3] = { 138, 43, 254 }; /** The screen position of the top left corner of the progress bar. */ const int BarPos[3][2] = { { 53, 37 }, { 53, 421 }, { 53, 37 } }; -void interface_msg_pump() -{ - MSG Msg; - - while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) { - if (Msg.message != WM_QUIT) { - TranslateMessage(&Msg); - DispatchMessage(&Msg); - } - } -} - -BOOL IncProgress() -{ - interface_msg_pump(); - sgdwProgress += 15; - if ((DWORD)sgdwProgress > 534) - sgdwProgress = 534; - if (sgpBackCel) - DrawCutscene(); - return (DWORD)sgdwProgress >= 534; -} - -void DrawCutscene() -{ - DWORD i; - - lock_buf(1); - CelDraw(PANEL_X, 480 + SCREEN_Y - 1, sgpBackCel, 1, 640); - - for (i = 0; i < sgdwProgress; i++) { - DrawProgress( - BarPos[progress_id][0] + i + PANEL_X, - BarPos[progress_id][1] + SCREEN_Y, - progress_id); - } - - unlock_buf(1); - force_redraw = 255; - scrollrt_draw_game_screen(FALSE); -} - -void DrawProgress(int screen_x, int screen_y, int progress_id) -{ - BYTE *dst; - int i; - - dst = &gpBuffer[screen_x + PitchTbl[screen_y]]; - for (i = 0; i < 22; i++) { - *dst = BarColor[progress_id]; - dst += BUFFER_WIDTH; - } -} - -void ShowProgress(unsigned int uMsg) -{ - WNDPROC saveProc; - - gbSomebodyWonGameKludge = FALSE; - plrmsg_delay(TRUE); - - assert(ghMainWnd); - saveProc = SetWindowProc(DisableInputWndProc); - - interface_msg_pump(); - ClearScreenBuffer(); - scrollrt_draw_game_screen(TRUE); - InitCutscene(uMsg); - BlackPalette(); - DrawCutscene(); - PaletteFadeIn(8); - IncProgress(); - sound_init(); - IncProgress(); - - switch (uMsg) { - case WM_DIABLOADGAME: - IncProgress(); - LoadGame(TRUE); - IncProgress(); - break; - case WM_DIABNEWGAME: - IncProgress(); - FreeGameMem(); - IncProgress(); - pfile_remove_temp_files(); - LoadGameLevel(TRUE, ENTRY_MAIN); - IncProgress(); - break; - case WM_DIABNEXTLVL: - IncProgress(); - if (gbMaxPlayers == 1) { - SaveLevel(); - } else { - DeltaSaveLevel(); - } - FreeGameMem(); - currlevel++; - leveltype = gnLevelTypeTbl[currlevel]; - assert(plr[myplr].plrlevel == currlevel); - IncProgress(); - LoadGameLevel(FALSE, ENTRY_MAIN); - IncProgress(); - break; - case WM_DIABPREVLVL: - IncProgress(); - if (gbMaxPlayers == 1) { - SaveLevel(); - } else { - DeltaSaveLevel(); - } - IncProgress(); - FreeGameMem(); - currlevel--; - leveltype = gnLevelTypeTbl[currlevel]; - assert(plr[myplr].plrlevel == currlevel); - IncProgress(); - LoadGameLevel(FALSE, ENTRY_PREV); - IncProgress(); - break; - case WM_DIABSETLVL: - SetReturnLvlPos(); - if (gbMaxPlayers == 1) { - SaveLevel(); - } else { - DeltaSaveLevel(); - } - setlevel = TRUE; - leveltype = setlvltype; - FreeGameMem(); - IncProgress(); - LoadGameLevel(FALSE, ENTRY_SETLVL); - IncProgress(); - break; - case WM_DIABRTNLVL: - if (gbMaxPlayers == 1) { - SaveLevel(); - } else { - DeltaSaveLevel(); - } - setlevel = FALSE; - FreeGameMem(); - IncProgress(); - GetReturnLvlPos(); - LoadGameLevel(FALSE, ENTRY_RTNLVL); - IncProgress(); - break; - case WM_DIABWARPLVL: - IncProgress(); - if (gbMaxPlayers == 1) { - SaveLevel(); - } else { - DeltaSaveLevel(); - } - FreeGameMem(); - GetPortalLevel(); - IncProgress(); - LoadGameLevel(FALSE, ENTRY_WARPLVL); - IncProgress(); - break; - case WM_DIABTOWNWARP: - IncProgress(); - if (gbMaxPlayers == 1) { - SaveLevel(); - } else { - DeltaSaveLevel(); - } - FreeGameMem(); - currlevel = plr[myplr].plrlevel; - leveltype = gnLevelTypeTbl[currlevel]; - assert(plr[myplr].plrlevel == currlevel); - IncProgress(); - LoadGameLevel(FALSE, ENTRY_TWARPDN); - IncProgress(); - break; - case WM_DIABTWARPUP: - IncProgress(); - if (gbMaxPlayers == 1) { - SaveLevel(); - } else { - DeltaSaveLevel(); - } - FreeGameMem(); - currlevel = plr[myplr].plrlevel; - leveltype = gnLevelTypeTbl[currlevel]; - assert(plr[myplr].plrlevel == currlevel); - IncProgress(); - LoadGameLevel(FALSE, ENTRY_TWARPUP); - IncProgress(); - break; - case WM_DIABRETOWN: - IncProgress(); - if (gbMaxPlayers == 1) { - SaveLevel(); - } else { - DeltaSaveLevel(); - } - FreeGameMem(); - currlevel = plr[myplr].plrlevel; - leveltype = gnLevelTypeTbl[currlevel]; - assert(plr[myplr].plrlevel == currlevel); - IncProgress(); - LoadGameLevel(FALSE, ENTRY_MAIN); - IncProgress(); - break; - } - - assert(ghMainWnd); - - PaletteFadeOut(8); - FreeInterface(); - - saveProc = SetWindowProc(saveProc); - assert(saveProc == DisableInputWndProc); - - NetSendCmdLocParam1(TRUE, CMD_PLAYER_JOINLEVEL, plr[myplr]._px, plr[myplr]._py, plr[myplr].plrlevel); - plrmsg_delay(FALSE); - ResetPal(); - - if (gbSomebodyWonGameKludge && plr[myplr].plrlevel == 16) { - PrepDoEnding(); - } - - gbSomebodyWonGameKludge = FALSE; -} - -void FreeInterface() +static void FreeInterface() { MemFreeDbg(sgpBackCel); } -void InitCutscene(unsigned int uMsg) +static void InitCutscene(unsigned int uMsg) { assert(!sgpBackCel); @@ -476,3 +250,229 @@ void InitCutscene(unsigned int uMsg) sgdwProgress = 0; } + +static void DrawProgress(int screen_x, int screen_y, int progress_id) +{ + BYTE *dst; + int i; + + dst = &gpBuffer[screen_x + PitchTbl[screen_y]]; + for (i = 0; i < 22; i++) { + *dst = BarColor[progress_id]; + dst += BUFFER_WIDTH; + } +} + +static void DrawCutscene() +{ + DWORD i; + + lock_buf(1); + CelDraw(PANEL_X, 480 + SCREEN_Y - 1, sgpBackCel, 1, 640); + + for (i = 0; i < sgdwProgress; i++) { + DrawProgress( + BarPos[progress_id][0] + i + PANEL_X, + BarPos[progress_id][1] + SCREEN_Y, + progress_id); + } + + unlock_buf(1); + force_redraw = 255; + scrollrt_draw_game_screen(FALSE); +} + +void interface_msg_pump() +{ + MSG Msg; + + while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) { + if (Msg.message != WM_QUIT) { + TranslateMessage(&Msg); + DispatchMessage(&Msg); + } + } +} + +BOOL IncProgress() +{ + interface_msg_pump(); + sgdwProgress += 15; + if ((DWORD)sgdwProgress > 534) + sgdwProgress = 534; + if (sgpBackCel) + DrawCutscene(); + return (DWORD)sgdwProgress >= 534; +} + +void ShowProgress(unsigned int uMsg) +{ + WNDPROC saveProc; + + gbSomebodyWonGameKludge = FALSE; + plrmsg_delay(TRUE); + + assert(ghMainWnd); + saveProc = SetWindowProc(DisableInputWndProc); + + interface_msg_pump(); + ClearScreenBuffer(); + scrollrt_draw_game_screen(TRUE); + InitCutscene(uMsg); + BlackPalette(); + DrawCutscene(); + PaletteFadeIn(8); + IncProgress(); + sound_init(); + IncProgress(); + + switch (uMsg) { + case WM_DIABLOADGAME: + IncProgress(); + LoadGame(TRUE); + IncProgress(); + break; + case WM_DIABNEWGAME: + IncProgress(); + FreeGameMem(); + IncProgress(); + pfile_remove_temp_files(); + LoadGameLevel(TRUE, ENTRY_MAIN); + IncProgress(); + break; + case WM_DIABNEXTLVL: + IncProgress(); + if (gbMaxPlayers == 1) { + SaveLevel(); + } else { + DeltaSaveLevel(); + } + FreeGameMem(); + currlevel++; + leveltype = gnLevelTypeTbl[currlevel]; + assert(plr[myplr].plrlevel == currlevel); + IncProgress(); + LoadGameLevel(FALSE, ENTRY_MAIN); + IncProgress(); + break; + case WM_DIABPREVLVL: + IncProgress(); + if (gbMaxPlayers == 1) { + SaveLevel(); + } else { + DeltaSaveLevel(); + } + IncProgress(); + FreeGameMem(); + currlevel--; + leveltype = gnLevelTypeTbl[currlevel]; + assert(plr[myplr].plrlevel == currlevel); + IncProgress(); + LoadGameLevel(FALSE, ENTRY_PREV); + IncProgress(); + break; + case WM_DIABSETLVL: + SetReturnLvlPos(); + if (gbMaxPlayers == 1) { + SaveLevel(); + } else { + DeltaSaveLevel(); + } + setlevel = TRUE; + leveltype = setlvltype; + FreeGameMem(); + IncProgress(); + LoadGameLevel(FALSE, ENTRY_SETLVL); + IncProgress(); + break; + case WM_DIABRTNLVL: + if (gbMaxPlayers == 1) { + SaveLevel(); + } else { + DeltaSaveLevel(); + } + setlevel = FALSE; + FreeGameMem(); + IncProgress(); + GetReturnLvlPos(); + LoadGameLevel(FALSE, ENTRY_RTNLVL); + IncProgress(); + break; + case WM_DIABWARPLVL: + IncProgress(); + if (gbMaxPlayers == 1) { + SaveLevel(); + } else { + DeltaSaveLevel(); + } + FreeGameMem(); + GetPortalLevel(); + IncProgress(); + LoadGameLevel(FALSE, ENTRY_WARPLVL); + IncProgress(); + break; + case WM_DIABTOWNWARP: + IncProgress(); + if (gbMaxPlayers == 1) { + SaveLevel(); + } else { + DeltaSaveLevel(); + } + FreeGameMem(); + currlevel = plr[myplr].plrlevel; + leveltype = gnLevelTypeTbl[currlevel]; + assert(plr[myplr].plrlevel == currlevel); + IncProgress(); + LoadGameLevel(FALSE, ENTRY_TWARPDN); + IncProgress(); + break; + case WM_DIABTWARPUP: + IncProgress(); + if (gbMaxPlayers == 1) { + SaveLevel(); + } else { + DeltaSaveLevel(); + } + FreeGameMem(); + currlevel = plr[myplr].plrlevel; + leveltype = gnLevelTypeTbl[currlevel]; + assert(plr[myplr].plrlevel == currlevel); + IncProgress(); + LoadGameLevel(FALSE, ENTRY_TWARPUP); + IncProgress(); + break; + case WM_DIABRETOWN: + IncProgress(); + if (gbMaxPlayers == 1) { + SaveLevel(); + } else { + DeltaSaveLevel(); + } + FreeGameMem(); + currlevel = plr[myplr].plrlevel; + leveltype = gnLevelTypeTbl[currlevel]; + assert(plr[myplr].plrlevel == currlevel); + IncProgress(); + LoadGameLevel(FALSE, ENTRY_MAIN); + IncProgress(); + break; + } + + assert(ghMainWnd); + + PaletteFadeOut(8); + FreeInterface(); + + saveProc = SetWindowProc(saveProc); + assert(saveProc == DisableInputWndProc); + + NetSendCmdLocParam1(TRUE, CMD_PLAYER_JOINLEVEL, plr[myplr]._px, plr[myplr]._py, plr[myplr].plrlevel); + plrmsg_delay(FALSE); + ResetPal(); + + if (gbSomebodyWonGameKludge && plr[myplr].plrlevel == 16) { + PrepDoEnding(); + } + + gbSomebodyWonGameKludge = FALSE; +} diff --git a/Source/interfac.h b/Source/interfac.h index a88a864ea..7854f8f51 100644 --- a/Source/interfac.h +++ b/Source/interfac.h @@ -6,19 +6,8 @@ #ifndef __INTERFAC_H__ #define __INTERFAC_H__ -extern int progress_id; - void interface_msg_pump(); BOOL IncProgress(); -void DrawCutscene(); -void DrawProgress(int screen_x, int screen_y, int progress_id); void ShowProgress(unsigned int uMsg); -void FreeInterface(); -void InitCutscene(unsigned int uMsg); - -/* rdata */ - -extern const BYTE BarColor[3]; -extern const int BarPos[3][2]; #endif /* __INTERFAC_H__ */ From c3380d3104ed6ac1b9ec7f4e41210e88b23f2270 Mon Sep 17 00:00:00 2001 From: qndel Date: Tue, 3 Nov 2020 00:00:13 +0100 Subject: [PATCH 16/16] some cleanups --- Source/diablo.cpp | 202 ++++++++++++++++++++++++++------------------- Source/objects.cpp | 2 +- enums.h | 6 ++ 3 files changed, 124 insertions(+), 86 deletions(-) diff --git a/Source/diablo.cpp b/Source/diablo.cpp index 594271dc6..e582279a1 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -318,7 +318,7 @@ static void start_game(unsigned int uMsg) gmenu_init_menu(); InitLevelCursor(); sgnTimeoutCurs = CURSOR_NONE; - sgbMouseDown = 0; + sgbMouseDown = CLICK_NONE; track_repeat_walk(FALSE); } @@ -787,53 +787,64 @@ static BOOL TryIconCurs() if (pcurs == CURSOR_RESURRECT) { NetSendCmdParam1(TRUE, CMD_RESURRECT, pcursplr); return TRUE; - } else if (pcurs == CURSOR_HEALOTHER) { + } + + if (pcurs == CURSOR_HEALOTHER) { NetSendCmdParam1(TRUE, CMD_HEALOTHER, pcursplr); return TRUE; - } else if (pcurs == CURSOR_TELEKINESIS) { + } + + if (pcurs == CURSOR_TELEKINESIS) { DoTelekinesis(); return TRUE; - } else if (pcurs == CURSOR_IDENTIFY) { - if (pcursinvitem != -1) { + } + + if (pcurs == CURSOR_IDENTIFY) { + if (pcursinvitem != -1) CheckIdentify(myplr, pcursinvitem); - } else { + else NewCursor(CURSOR_HAND); - } return TRUE; - } else if (pcurs == CURSOR_REPAIR) { - if (pcursinvitem != -1) { + } + + if (pcurs == CURSOR_REPAIR) { + if (pcursinvitem != -1) DoRepair(myplr, pcursinvitem); - } else { + else NewCursor(CURSOR_HAND); - } return TRUE; - } else if (pcurs == CURSOR_RECHARGE) { - if (pcursinvitem != -1) { + } + + if (pcurs == CURSOR_RECHARGE) { + if (pcursinvitem != -1) DoRecharge(myplr, pcursinvitem); - } else { + else NewCursor(CURSOR_HAND); - } return TRUE; + } + #ifdef HELLFIRE - } else if (pcurs == CURSOR_OIL) { - if (pcursinvitem != -1) { + if (pcurs == CURSOR_OIL) { + if (pcursinvitem != -1) DoOil(myplr, pcursinvitem); - } else { + else SetCursor_(CURSOR_HAND); - } return TRUE; + } + #endif - } else if (pcurs == CURSOR_TELEPORT) { - if (pcursmonst != -1) { + if (pcurs == CURSOR_TELEPORT) { + if (pcursmonst != -1) NetSendCmdParam3(TRUE, CMD_TSPELLID, pcursmonst, plr[myplr]._pTSpell, GetSpellLevel(myplr, plr[myplr]._pTSpell)); - } else if (pcursplr != -1) { + else if (pcursplr != -1) NetSendCmdParam3(TRUE, CMD_TSPELLPID, pcursplr, plr[myplr]._pTSpell, GetSpellLevel(myplr, plr[myplr]._pTSpell)); - } else { + else NetSendCmdLocParam2(TRUE, CMD_TSPELLXY, cursmx, cursmy, plr[myplr]._pTSpell, GetSpellLevel(myplr, plr[myplr]._pTSpell)); - } NewCursor(CURSOR_HAND); return TRUE; - } else if (pcurs == CURSOR_DISARM && pcursobj == -1) { + } + + if (pcurs == CURSOR_DISARM && pcursobj == -1) { NewCursor(CURSOR_HAND); return TRUE; } @@ -843,51 +854,72 @@ static BOOL TryIconCurs() static BOOL LeftMouseDown(int wParam) { - if (!gmenu_left_mouse(TRUE) && !control_check_talk_btn() && sgnTimeoutCurs == CURSOR_NONE) { - if (deathflag) { - control_check_btn_press(); - } else if (PauseMode != 2) { - if (doomflag) { - doom_close(); - } else if (spselflag) { - SetSpell(); - } else if (stextflag != STORE_NONE) { - CheckStoreBtn(); - } else if (MouseY < PANEL_TOP) { - if (!gmenu_is_active() && !TryIconCurs()) { - if (questlog && MouseX > 32 && MouseX < 288 && MouseY > 32 && MouseY < 308) { - QuestlogESC(); - } else if (qtextflag) { - qtextflag = FALSE; - stream_stop(); - } else if (chrflag && MouseX < SPANEL_WIDTH) { - CheckChrBtns(); - } else if (invflag && MouseX > RIGHT_PANEL) { - if (!dropGoldFlag) - CheckInvItem(); - } else if (sbookflag && MouseX > RIGHT_PANEL) { - CheckSBook(); - } else if (pcurs >= CURSOR_FIRSTITEM) { - if (TryInvPut()) { - NetSendCmdPItem(TRUE, CMD_PUTITEM, cursmx, cursmy); - NewCursor(CURSOR_HAND); - } - } else { - if (plr[myplr]._pStatPts != 0 && !spselflag) - CheckLvlBtn(); - if (!lvlbtndown) - return LeftMouseCmd(wParam == MK_SHIFT + MK_LBUTTON); - } + if (gmenu_left_mouse(TRUE)) + return FALSE; + + if (control_check_talk_btn()) + return FALSE; + + if (sgnTimeoutCurs != CURSOR_NONE) + return FALSE; + + if (deathflag) { + control_check_btn_press(); + return FALSE; + } + + if (PauseMode == 2) { + return FALSE; + } + if (doomflag) { + doom_close(); + return FALSE; + } + + if (spselflag) { + SetSpell(); + return FALSE; + } + + if (stextflag != STORE_NONE) { + CheckStoreBtn(); + return FALSE; + } + + if (MouseY < PANEL_TOP) { + if (!gmenu_is_active() && !TryIconCurs()) { + if (questlog && MouseX > 32 && MouseX < 288 && MouseY > 32 && MouseY < 308) { + QuestlogESC(); + } else if (qtextflag) { + qtextflag = FALSE; + stream_stop(); + } else if (chrflag && MouseX < SPANEL_WIDTH) { + CheckChrBtns(); + } else if (invflag && MouseX > RIGHT_PANEL) { + if (!dropGoldFlag) + CheckInvItem(); + } else if (sbookflag && MouseX > RIGHT_PANEL) { + CheckSBook(); + } else if (pcurs >= CURSOR_FIRSTITEM) { + if (TryInvPut()) { + NetSendCmdPItem(TRUE, CMD_PUTITEM, cursmx, cursmy); + NewCursor(CURSOR_HAND); } } else { - if (!talkflag && !dropGoldFlag && !gmenu_is_active()) - CheckInvScrn(); - DoPanBtn(); - if (pcurs > CURSOR_HAND && pcurs < CURSOR_FIRSTITEM) - NewCursor(CURSOR_HAND); + if (plr[myplr]._pStatPts != 0 && !spselflag) + CheckLvlBtn(); + if (!lvlbtndown) + return LeftMouseCmd(wParam == MK_SHIFT + MK_LBUTTON); } } + } else { + if (!talkflag && !dropGoldFlag && !gmenu_is_active()) + CheckInvScrn(); + DoPanBtn(); + if (pcurs > CURSOR_HAND && pcurs < CURSOR_FIRSTITEM) + NewCursor(CURSOR_HAND); } + return FALSE; } @@ -1519,33 +1551,33 @@ LRESULT CALLBACK DisableInputWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM case WM_MOUSEMOVE: return 0; case WM_LBUTTONDOWN: - if (sgbMouseDown != 0) + if (sgbMouseDown != CLICK_NONE) return 0; - sgbMouseDown = 1; + sgbMouseDown = CLICK_LEFT; SetCapture(hWnd); return 0; case WM_LBUTTONUP: - if (sgbMouseDown != 1) + if (sgbMouseDown != CLICK_LEFT) return 0; - sgbMouseDown = 0; + sgbMouseDown = CLICK_NONE; ReleaseCapture(); return 0; case WM_RBUTTONDOWN: - if (sgbMouseDown != 0) + if (sgbMouseDown != CLICK_NONE) return 0; - sgbMouseDown = 2; + sgbMouseDown = CLICK_RIGHT; SetCapture(hWnd); return 0; case WM_RBUTTONUP: - if (sgbMouseDown != 2) + if (sgbMouseDown != CLICK_RIGHT) return 0; - sgbMouseDown = 0; + sgbMouseDown = CLICK_NONE; ReleaseCapture(); return 0; case WM_CAPTURECHANGED: if (hWnd == (HWND)lParam) return 0; - sgbMouseDown = 0; + sgbMouseDown = CLICK_NONE; return 0; } @@ -1583,8 +1615,8 @@ LRESULT CALLBACK GM_Game(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) case WM_LBUTTONDOWN: MouseX = LOWORD(lParam); // BUGFIX (short)LOWORD coords are signed MouseY = HIWORD(lParam); // BUGFIX (short)HIWORD coords are signed - if (sgbMouseDown == 0) { - sgbMouseDown = 1; + if (sgbMouseDown == CLICK_NONE) { + sgbMouseDown = CLICK_LEFT; SetCapture(hWnd); track_repeat_walk(LeftMouseDown(wParam)); } @@ -1592,8 +1624,8 @@ LRESULT CALLBACK GM_Game(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) case WM_LBUTTONUP: MouseX = LOWORD(lParam); // BUGFIX (short)LOWORD coords are signed MouseY = HIWORD(lParam); // BUGFIX (short)HIWORD coords are signed - if (sgbMouseDown == 1) { - sgbMouseDown = 0; + if (sgbMouseDown == CLICK_LEFT) { + sgbMouseDown = CLICK_NONE; LeftMouseUp(); track_repeat_walk(FALSE); ReleaseCapture(); @@ -1602,8 +1634,8 @@ LRESULT CALLBACK GM_Game(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) case WM_RBUTTONDOWN: MouseX = LOWORD(lParam); // BUGFIX (short)LOWORD coords are signed MouseY = HIWORD(lParam); // BUGFIX (short)HIWORD coords are signed - if (sgbMouseDown == 0) { - sgbMouseDown = 2; + if (sgbMouseDown == CLICK_NONE) { + sgbMouseDown = CLICK_RIGHT; SetCapture(hWnd); RightMouseDown(); } @@ -1611,14 +1643,14 @@ LRESULT CALLBACK GM_Game(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) case WM_RBUTTONUP: MouseX = LOWORD(lParam); // BUGFIX (short)LOWORD coords are signed MouseY = HIWORD(lParam); // BUGFIX (short)HIWORD coords are signed - if (sgbMouseDown == 2) { - sgbMouseDown = 0; + if (sgbMouseDown == CLICK_RIGHT) { + sgbMouseDown = CLICK_NONE; ReleaseCapture(); } return 0; case WM_CAPTURECHANGED: if (hWnd != (HWND)lParam) { - sgbMouseDown = 0; + sgbMouseDown = CLICK_NONE; track_repeat_walk(FALSE); } break; @@ -1637,7 +1669,7 @@ LRESULT CALLBACK GM_Game(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) sound_stop(); music_stop(); track_repeat_walk(FALSE); - sgbMouseDown = 0; + sgbMouseDown = CLICK_NONE; ReleaseCapture(); ShowProgress(uMsg); force_redraw = 255; @@ -2085,7 +2117,7 @@ static void game_logic() static void timeout_cursor(BOOL bTimeout) { if (bTimeout) { - if (sgnTimeoutCurs == CURSOR_NONE && !sgbMouseDown) { + if (sgnTimeoutCurs == CURSOR_NONE && sgbMouseDown == CLICK_NONE) { sgnTimeoutCurs = pcurs; multi_net_ping(); ClearPanel(); diff --git a/Source/objects.cpp b/Source/objects.cpp index 4af8d813b..e3c28dfe3 100644 --- a/Source/objects.cpp +++ b/Source/objects.cpp @@ -1938,7 +1938,7 @@ void Obj_Circle(int i) quests[Q_BETRAYER]._qvar1 = 4; AddMissile(plr[myplr]._px, plr[myplr]._py, 35, 46, plr[myplr]._pdir, MIS_RNDTELEPORT, 0, myplr, 0, 0); track_repeat_walk(FALSE); - sgbMouseDown = 0; + sgbMouseDown = CLICK_NONE; ReleaseCapture(); ClrPlrPath(myplr); StartStand(myplr, 0); diff --git a/enums.h b/enums.h index 1cdbc62d3..ca0e4a052 100644 --- a/enums.h +++ b/enums.h @@ -2291,6 +2291,12 @@ typedef enum dflag { BFLAG_EXPLORED = 0x80, } dflag; +typedef enum clicktype { + CLICK_NONE = 0, + CLICK_LEFT = 1, + CLICK_RIGHT = 2, +}; + /* First 5 bits store level 6th bit stores onlygood flag