From c1a8776a154d91657e7288df46855df932fcbf37 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 3 Aug 2021 12:30:44 +0200 Subject: [PATCH] - updated common code from screenjob branch. That is, all parts not related to cutscenes. --- src/common/audio/music/music.cpp | 9 ++- src/common/audio/music/s_music.h | 1 + src/common/audio/sound/s_sound.cpp | 9 ++- src/common/audio/sound/s_soundinternal.h | 2 + src/common/console/c_commandbuffer.cpp | 6 +- src/common/console/c_console.cpp | 5 +- src/common/engine/i_interface.h | 2 + src/common/filesystem/filesystem.cpp | 2 +- src/common/rendering/v_framebuffer.cpp | 11 ++-- .../scripting/interface/stringformat.cpp | 14 ++++ src/common/scripting/interface/vmnatives.cpp | 66 +++++++++++++++++++ src/common/utility/i_time.cpp | 9 ++- src/common/utility/i_time.h | 3 + src/d_main.cpp | 9 +++ src/playsim/bots/b_func.cpp | 2 +- wadsrc/static/zscript/engine/base.zs | 34 ++++++++-- 16 files changed, 163 insertions(+), 21 deletions(-) diff --git a/src/common/audio/music/music.cpp b/src/common/audio/music/music.cpp index 4ae668d92..1b36041da 100644 --- a/src/common/audio/music/music.cpp +++ b/src/common/audio/music/music.cpp @@ -102,6 +102,11 @@ void S_SetMusicCallbacks(MusicCallbacks* cb) if (mus_cb.OpenMusic == nullptr) mus_cb.OpenMusic = DefaultOpenMusic; // without this we are dead in the water. } +int MusicEnabled() // int return is for scripting +{ + return mus_enabled && !nomusic; +} + //========================================================================== // // @@ -632,7 +637,7 @@ static void CheckReplayGain(const char *musicname, EMidiDevice playertype, const bool S_ChangeMusic(const char* musicname, int order, bool looping, bool force) { - if (nomusic) return false; // skip the entire procedure if music is globally disabled. + if (!MusicEnabled()) return false; // skip the entire procedure if music is globally disabled. if (!force && PlayList.GetNumSongs()) { // Don't change if a playlist is active @@ -854,7 +859,7 @@ void S_StopMusic (bool force) CCMD (changemus) { - if (!nomusic) + if (MusicEnabled()) { if (argv.argc() > 1) { diff --git a/src/common/audio/music/s_music.h b/src/common/audio/music/s_music.h index a76189fc1..fc1b86ca0 100644 --- a/src/common/audio/music/s_music.h +++ b/src/common/audio/music/s_music.h @@ -11,6 +11,7 @@ class FileReader; class SoundStream; +int MusicEnabled(); typedef bool(*StreamCallback)(SoundStream* stream, void* buff, int len, void* userdata); SoundStream *S_CreateCustomStream(size_t size, int samplerate, int numchannels, StreamCallback cb, void *userdata); void S_StopCustomStream(SoundStream* stream); diff --git a/src/common/audio/sound/s_sound.cpp b/src/common/audio/sound/s_sound.cpp index 8ec5fc76b..f6acbb885 100644 --- a/src/common/audio/sound/s_sound.cpp +++ b/src/common/audio/sound/s_sound.cpp @@ -43,7 +43,14 @@ #include "s_music.h" #include "m_random.h" #include "printf.h" +#include "c_cvars.h" +CVARD(Bool, snd_enabled, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG, "enables/disables sound effects") + +int SoundEnabled() +{ + return snd_enabled && !nosound && !nosfx; +} enum { @@ -382,7 +389,7 @@ FSoundChan *SoundEngine::StartSound(int type, const void *source, FVector3 pos, vel; FRolloffInfo *rolloff; - if (sound_id <= 0 || volume <= 0 || nosfx || nosound || blockNewSounds) + if (sound_id <= 0 || volume <= 0 || nosfx || !SoundEnabled() || blockNewSounds) return NULL; // prevent crashes. diff --git a/src/common/audio/sound/s_soundinternal.h b/src/common/audio/sound/s_soundinternal.h index df6943c5d..d35cb524a 100644 --- a/src/common/audio/sound/s_soundinternal.h +++ b/src/common/audio/sound/s_soundinternal.h @@ -429,3 +429,5 @@ inline int S_FindSound(const char* name) { return soundEngine->FindSound(name); } + +int SoundEnabled(); diff --git a/src/common/console/c_commandbuffer.cpp b/src/common/console/c_commandbuffer.cpp index 313df7567..0ca96ade3 100644 --- a/src/common/console/c_commandbuffer.cpp +++ b/src/common/console/c_commandbuffer.cpp @@ -98,8 +98,7 @@ unsigned FCommandBuffer::CalcCellSize(unsigned length) unsigned cellcount = 0; for (unsigned i = 0; i < length; i++) { - int w; - NewConsoleFont->GetChar(Text[i], CR_UNTRANSLATED, &w); + int w = NewConsoleFont->GetCharWidth(Text[i]); cellcount += w / 9; } return cellcount; @@ -112,8 +111,7 @@ unsigned FCommandBuffer::CharsForCells(unsigned cellin, bool *overflow) int cells = cellin; while (cells > 0) { - int w; - NewConsoleFont->GetChar(Text[chars++], CR_UNTRANSLATED, &w); + int w = NewConsoleFont->GetCharWidth(Text[chars++]); cells -= w / 9; } *overflow = (cells < 0); diff --git a/src/common/console/c_console.cpp b/src/common/console/c_console.cpp index 48c7b86de..5cafbb080 100644 --- a/src/common/console/c_console.cpp +++ b/src/common/console/c_console.cpp @@ -592,8 +592,7 @@ void C_DrawConsole () oldbottom = ConBottom; - if (ConsoleState == c_up && gamestate != GS_INTRO && gamestate != GS_INTERMISSION && - gamestate != GS_FULLCONSOLE && gamestate != GS_MENUSCREEN) + if (ConsoleState == c_up && gamestate == GS_LEVEL) { if (NotifyStrings) NotifyStrings->Draw(); return; @@ -731,7 +730,7 @@ void C_ToggleConsole () } if (gamestate == GS_MENUSCREEN) { - gameaction = ga_fullconsole; + if (sysCallbacks.ToggleFullConsole) sysCallbacks.ToggleFullConsole(); togglestate = c_down; } else if (!chatmodeon && (ConsoleState == c_up || ConsoleState == c_rising) && menuactive == MENU_Off) diff --git a/src/common/engine/i_interface.h b/src/common/engine/i_interface.h index 46cecba41..dd3ea5862 100644 --- a/src/common/engine/i_interface.h +++ b/src/common/engine/i_interface.h @@ -32,6 +32,8 @@ struct SystemCallbacks void (*ConsoleToggled)(int state); bool (*PreBindTexture)(FRenderState* state, FGameTexture*& tex, EUpscaleFlags& flags, int& scaleflags, int& clampmode, int& translation, int& overrideshader); void (*FontCharCreated)(FGameTexture* base, FGameTexture* untranslated); + void (*ToggleFullConsole)(); + void (*StartCutscene)(bool blockui); }; extern SystemCallbacks sysCallbacks; diff --git a/src/common/filesystem/filesystem.cpp b/src/common/filesystem/filesystem.cpp index d700679f0..4c5a5b910 100644 --- a/src/common/filesystem/filesystem.cpp +++ b/src/common/filesystem/filesystem.cpp @@ -635,7 +635,7 @@ int FileSystem::GetNumForFullName (const char *name) // // FindFile // -// Looks up a file by name, either eith or without path and extension +// Looks up a file by name, either with or without path and extension // //========================================================================== diff --git a/src/common/rendering/v_framebuffer.cpp b/src/common/rendering/v_framebuffer.cpp index 747a37c6e..d1f148f1b 100644 --- a/src/common/rendering/v_framebuffer.cpp +++ b/src/common/rendering/v_framebuffer.cpp @@ -291,16 +291,19 @@ FMaterial* DFrameBuffer::CreateMaterial(FGameTexture* tex, int scaleflags) // //========================================================================== -DEFINE_ACTION_FUNCTION(_Screen, GetWidth) +static int ScreenGetWidth() { return twod->GetWidth(); } +static int ScreenGetHeight() { return twod->GetHeight(); } + +DEFINE_ACTION_FUNCTION_NATIVE(_Screen, GetWidth, ScreenGetWidth) { PARAM_PROLOGUE; - ACTION_RETURN_INT(screen->GetWidth()); + ACTION_RETURN_INT(twod->GetWidth()); } -DEFINE_ACTION_FUNCTION(_Screen, GetHeight) +DEFINE_ACTION_FUNCTION_NATIVE(_Screen, GetHeight, ScreenGetHeight) { PARAM_PROLOGUE; - ACTION_RETURN_INT(screen->GetHeight()); + ACTION_RETURN_INT(twod->GetHeight()); } DEFINE_ACTION_FUNCTION(_Screen, PaletteColor) diff --git a/src/common/scripting/interface/stringformat.cpp b/src/common/scripting/interface/stringformat.cpp index da667cbbf..87bfbb415 100644 --- a/src/common/scripting/interface/stringformat.cpp +++ b/src/common/scripting/interface/stringformat.cpp @@ -564,6 +564,20 @@ DEFINE_ACTION_FUNCTION_NATIVE(FStringStruct, Substitute, StringSubst) return 0; } +static void StringStripRight(FString* self, const FString& junk) +{ + if (junk.IsNotEmpty()) self->StripRight(junk); + else self->StripRight(); +} + +DEFINE_ACTION_FUNCTION_NATIVE(FStringStruct, StripRight, StringStripRight) +{ + PARAM_SELF_STRUCT_PROLOGUE(FString); + PARAM_STRING(junk); + StringStripRight(self, junk); + return 0; +} + static void StringSplit(FString* self, TArray* tokens, const FString& delimiter, int keepEmpty) { self->Split(*tokens, delimiter, static_cast(keepEmpty)); diff --git a/src/common/scripting/interface/vmnatives.cpp b/src/common/scripting/interface/vmnatives.cpp index d7d4519e6..6b1806b28 100644 --- a/src/common/scripting/interface/vmnatives.cpp +++ b/src/common/scripting/interface/vmnatives.cpp @@ -50,6 +50,8 @@ #include "i_interface.h" #include "base_sbar.h" #include "image.h" +#include "s_soundinternal.h" +#include "i_time.h" //========================================================================== // @@ -696,6 +698,18 @@ DEFINE_ACTION_FUNCTION_NATIVE(FFont, GetGlyphHeight, GetGlyphHeight) PARAM_INT(code); ACTION_RETURN_INT(GetGlyphHeight(self, code)); } + +static int GetDefaultKerning(FFont* font) +{ + return font->GetDefaultKerning(); +} + +DEFINE_ACTION_FUNCTION_NATIVE(FFont, GetDefaultKerning, GetDefaultKerning) +{ + PARAM_SELF_STRUCT_PROLOGUE(FFont); + ACTION_RETURN_INT(self->GetDefaultKerning()); +} + //========================================================================== // // file system @@ -995,6 +1009,58 @@ DEFINE_ACTION_FUNCTION(_Console, Printf) return 0; } +static void StopAllSounds() +{ + soundEngine->StopAllChannels(); +} + +DEFINE_ACTION_FUNCTION_NATIVE(_System, StopAllSounds, StopAllSounds) +{ + StopAllSounds(); + return 0; +} + +static int PlayMusic(const FString& musname, int order, int looped) +{ + return S_ChangeMusic(musname, order, !!looped, true); +} + +DEFINE_ACTION_FUNCTION_NATIVE(_System, PlayMusic, PlayMusic) +{ + PARAM_PROLOGUE; + PARAM_STRING(name); + PARAM_INT(order); + PARAM_BOOL(looped); + ACTION_RETURN_BOOL(PlayMusic(name, order, looped)); +} + +static void Mus_Stop() +{ + S_StopMusic(true); +} + +DEFINE_ACTION_FUNCTION_NATIVE(_System, StopMusic, Mus_Stop) +{ + Mus_Stop(); + return 0; +} + +DEFINE_ACTION_FUNCTION_NATIVE(_System, SoundEnabled, SoundEnabled) +{ + ACTION_RETURN_INT(SoundEnabled()); +} + +DEFINE_ACTION_FUNCTION_NATIVE(_System, MusicEnabled, MusicEnabled) +{ + ACTION_RETURN_INT(MusicEnabled()); +} + +DEFINE_ACTION_FUNCTION_NATIVE(_System, GetTimeFrac, I_GetTimeFrac) +{ + ACTION_RETURN_FLOAT(I_GetTimeFrac()); +} + + DEFINE_GLOBAL_NAMED(mus_playing, musplaying); DEFINE_FIELD_X(MusPlayingInfo, MusPlayingInfo, name); DEFINE_FIELD_X(MusPlayingInfo, MusPlayingInfo, baseorder); diff --git a/src/common/utility/i_time.cpp b/src/common/utility/i_time.cpp index 61f7baecb..0a7179660 100644 --- a/src/common/utility/i_time.cpp +++ b/src/common/utility/i_time.cpp @@ -195,9 +195,16 @@ void I_FreezeTime(bool frozen) else { assert(FreezeTime != 0); - FirstFrameStartTime += GetClockTimeNS() - FreezeTime; + if (FirstFrameStartTime != 0) FirstFrameStartTime += GetClockTimeNS() - FreezeTime; FreezeTime = 0; I_SetFrameTime(); } } +void I_ResetFrameTime() +{ + // Reset the starting point of the current frame to now. For use after lengthy operations that should not result in tic accumulation. + auto ft = CurrentFrameStartTime; + I_SetFrameTime(); + FirstFrameStartTime += (CurrentFrameStartTime - ft); +} diff --git a/src/common/utility/i_time.h b/src/common/utility/i_time.h index 4c63515ac..8b4eff05c 100644 --- a/src/common/utility/i_time.h +++ b/src/common/utility/i_time.h @@ -38,3 +38,6 @@ uint64_t I_msTimeFS(); // Nanosecond-accurate time uint64_t I_nsTime(); + +// Reset the timer after a lengthy operation +void I_ResetFrameTime(); diff --git a/src/d_main.cpp b/src/d_main.cpp index 5014bfb62..245922beb 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -3043,6 +3043,11 @@ static void GC_MarkGameRoots() GC::Mark(NextToThink); } +static void System_ToggleFullConsole() +{ + gameaction = ga_fullconsole; +} + bool CheckSkipGameOptionBlock(const char* str); //========================================================================== @@ -3091,6 +3096,10 @@ static int D_DoomMain_Internal (void) nullptr, CheckSkipGameOptionBlock, System_ConsoleToggled, + nullptr, + nullptr, + System_ToggleFullConsole, + nullptr, }; diff --git a/src/playsim/bots/b_func.cpp b/src/playsim/bots/b_func.cpp index f9925f534..b38fd1723 100644 --- a/src/playsim/bots/b_func.cpp +++ b/src/playsim/bots/b_func.cpp @@ -180,7 +180,7 @@ void DBot::Dofire (ticcmd_t *cmd) if (player->ReadyWeapon == NULL) return; - if (player->damagecount > skill.isp) + if (player->damagecount > (unsigned)skill.isp) { first_shot = true; return; diff --git a/wadsrc/static/zscript/engine/base.zs b/wadsrc/static/zscript/engine/base.zs index 21115a193..ad8dfb9e4 100644 --- a/wadsrc/static/zscript/engine/base.zs +++ b/wadsrc/static/zscript/engine/base.zs @@ -76,11 +76,14 @@ enum EGameState GS_INTERMISSION, GS_FINALE, GS_DEMOSCREEN, + GS_FULLCONSOLE, // [RH] Fullscreen console + GS_HIDECONSOLE, // [RH] The menu just did something that should hide fs console + GS_STARTUP, // [RH] Console is fullscreen, and game is just starting + GS_TITLELEVEL, // [RH] A combination of GS_LEVEL and GS_DEMOSCREEN + GS_INTRO, + GS_CUTSCENE, + GS_MENUSCREEN = GS_DEMOSCREEN, - GS_FULLCONSOLE, - GS_HIDECONSOLE, - GS_STARTUP, - GS_TITLELEVEL, } const TEXTCOLOR_BRICK = "\034A"; @@ -182,6 +185,26 @@ struct _ native // These are the global variables, the struct is only here to av native readonly double NotifyFontScale; } +struct System native +{ + native static void StopMusic(); + native static void StopAllSounds(); + native static bool SoundEnabled(); + native static bool MusicEnabled(); + native static double GetTimeFrac(); + + static bool specialKeyEvent(InputEvent ev) + { + if (ev.type == InputEvent.Type_KeyDown || ev.type == InputEvent.Type_KeyUp) + { + int key = ev.KeyScan; + if (key == InputEvent.KEY_VOLUMEDOWN || key == InputEvent.KEY_VOLUMEUP || (key > InputEvent.KEY_LASTJOYBUTTON && key < InputEvent.KEY_PAD_LTHUMB_RIGHT)) return true; + } + return false; + } + +} + struct MusPlayingInfo native { native String name; @@ -389,6 +412,7 @@ struct Screen native native static Color PaletteColor(int index); native static int GetWidth(); native static int GetHeight(); + native static Vector2 GetTextScreenSize(); native static void Clear(int left, int top, int right, int bottom, Color color, int palcolor = -1); native static void Dim(Color col, double amount, int x, int y, int w, int h); @@ -495,6 +519,7 @@ struct Font native native static Font GetFont(Name fontname); native BrokenLines BreakLines(String text, int maxlen); native int GetGlyphHeight(int code); + native int GetDefaultKerning(); } struct Console native @@ -665,6 +690,7 @@ struct StringStruct native native int CodePointCount() const; native int, int GetNextCodePoint(int position) const; native void Substitute(String str, String replace); + native void StripRight(String junk = ""); } struct Translation version("2.4")