From 99f68e655f1925fa4abf062d7e10851ab16991bc Mon Sep 17 00:00:00 2001 From: kotofyt Date: Thu, 5 Mar 2026 21:25:59 +0200 Subject: [PATCH] some stuff --- engine/build.cpp | 1 + engine/cl_dll.cpp | 4 +- engine/cvar.cpp | 119 +-------------- engine/engine.cpp | 11 ++ engine/humandevice.cpp | 73 ++++++++- engine/sv_dll.cpp | 12 +- external/GameNetworkingSockets | 2 +- external/SDL | 2 +- external/Vulkan-Headers | 2 +- external/Vulkan-Utility-Libraries | 2 +- external/VulkanMemoryAllocator | 2 +- external/cglm | 2 +- external/slang | 2 +- external/stb | 2 +- external/volk | 2 +- external/xtool | 2 +- game/client/baseentity.h | 2 + game/client/build.cpp | 2 + game/client/entitysystem.cpp | 91 +++++++++++ game/client/entitysystem.h | 7 + game/client/game.cpp | 144 +++++++----------- game/client/milmoba/player.cpp | 26 +++- game/client/milmoba/player.h | 1 - game/client/worldrender.cpp | 2 +- game/server/build.cpp | 2 + game/server/entitysystem.cpp | 7 +- game/server/entitysystem.h | 2 +- game/server/game.cpp | 57 ++++--- game/server/milmoba/player.cpp | 8 +- game/server/milmoba/player.h | 6 +- game/server/physicsprop.cpp | 1 + game/shared/engine.h | 28 ---- game/shared/game.cpp | 40 +++++ game/shared/game.h | 33 +++- game/shared/netprotocol.h | 11 +- materialsystem/gamewindow_sdl.cpp | 109 +++++++++++++ public/enginebridge.h | 2 + public/icvar.h | 118 +++++++++++++- public/ihumandevice.h | 44 ++++++ .../materialsystem/humandevice_enums.h | 40 +++-- public/materialsystem/igamewindow.h | 7 + 41 files changed, 706 insertions(+), 324 deletions(-) delete mode 100644 game/shared/engine.h create mode 100644 game/shared/game.cpp create mode 100644 public/ihumandevice.h rename engine/humandevice.h => public/materialsystem/humandevice_enums.h (69%) diff --git a/engine/build.cpp b/engine/build.cpp index 95be044..8bcb34a 100644 --- a/engine/build.cpp +++ b/engine/build.cpp @@ -23,6 +23,7 @@ DECLARE_BUILD_STAGE(engine) "localnetwork.cpp", "socketnetwork.cpp", "steamnetwork.cpp", + "humandevice.cpp", "sv_dll.cpp", "cl_dll.cpp", diff --git a/engine/cl_dll.cpp b/engine/cl_dll.cpp index 49bb729..cee6158 100644 --- a/engine/cl_dll.cpp +++ b/engine/cl_dll.cpp @@ -3,6 +3,7 @@ #include "tier0/platform.h" #include "icvar.h" #include "tier2/ifilesystem.h" +#include "ihumandevice.h" void CClientGameDLL::Init() { @@ -18,8 +19,9 @@ void CClientGameDLL::Init() CreateInterfaceFn pfnServerFactory = Sys_GetFactory(pLib); IEngineBridge *pEngineBridge = (IEngineBridge*)pfnServerFactory(ENGINE_BRIDGE_INTERFACE_VERSION, NULL); - pEngineBridge->ConnectInterface(RENDER_CONTEXT_INTERFACE_VERSION, m_pRenderContext); pEngineBridge->ConnectInterface(FILESYSTEM_INTERFACE_VERSION, filesystem); + pEngineBridge->ConnectInterface(RENDER_CONTEXT_INTERFACE_VERSION, m_pRenderContext); + pEngineBridge->ConnectInterface(HUMAN_DEVICE_MANAGER_INTERFACE_VERSION, g_pHumanDeviceManager); pEngineBridge->ConnectInterface("MainWindow", m_pGameWindow); pEngineBridge->ConnectInterface("EngineConstants", m_pEngineConsts); pEngineBridge->Init(); diff --git a/engine/cvar.cpp b/engine/cvar.cpp index 7290b50..bc205bf 100644 --- a/engine/cvar.cpp +++ b/engine/cvar.cpp @@ -20,7 +20,7 @@ struct ConsoleMessage_t void Msg( const char* message ) { - printf(message); + printf("%s\n", message); } @@ -57,13 +57,18 @@ public: virtual void AddCommand( const char *psz ) override; virtual void InsertCommand( const char *psz ) override; + + CUtlVector m_convars; + CUtlVector m_commands; }; IConsole *Console() { static CConsole s_console; - return &s_console; + IConsole *pConsole = &s_console; + return pConsole; } +EXPOSE_INTERFACE_FN(Console, IConsole, CONSOLE_INTERFACE_VERSION); void CConsole::Init() { @@ -207,115 +212,6 @@ void CConsole::InsertCommand( const char *psz ) g_commandBuffer.AppendHead(psz); }; -ConVar::ConVar( const char *pName, const char *pDefaultValue, int flags ) - : ConVar(pName, pDefaultValue, flags, 0) -{ - -} -ConVar::ConVar( const char *pName, const char *pDefaultValue, int flags, - const char *pHelpString ) - : ConVar(pName, pDefaultValue, flags, pHelpString, 0) -{ -} -ConVar::ConVar( const char *pName, const char *pDefaultValue, int flags, - const char *pHelpString, ConCommandFn callback ) -{ - m_szName = pName; - m_flags = flags; - m_szValue = pDefaultValue; - m_fValue = V_atof(pDefaultValue); - m_nValue = V_atoi(pDefaultValue); - Console()->RegisterVar(this); -} - -bool ConVar::IsFlagSet( int flag ) -{ - -} -const char *ConVar::GetHelpText( void ) -{ - return m_szHelpString; -} -bool ConVar::IsRegistered( void ) -{ - -} -const char *ConVar::GetName( void ) -{ - return m_szName; -} -void ConVar::AddFlags( int flags ) -{ - -} -bool ConVar::IsCommand( void ) -{ - -} - -void ConVar::InstallChangeCallback( ConCommandFn ) -{ - -} - -float ConVar::GetFloat( void ) -{ - return m_fValue; -} -int ConVar::GetInt( void ) -{ - return m_nValue; -} -bool ConVar::GetBool( void ) -{ - return m_nValue; -} -const char *ConVar::GetString( void ) -{ - -} - -void ConVar::SetValue( const char *szValue ) -{ - if (!szValue) - return; - m_szValue = szValue; - m_fValue = V_atof(szValue); - m_nValue = V_atoi(szValue); -} -void ConVar::SetValue( float fValue ) -{ - m_fValue = fValue; - m_nValue = fValue; - m_szValue = CUtlString("%f\n",fValue); -} -void ConVar::SetValue( int iValue ) -{ - m_fValue = iValue; - m_nValue = iValue; - m_szValue = CUtlString("%i\n",iValue); -} - -ConCommand::ConCommand(const char *pName, ConCommandFn callback, - const char *pHelpString, int flags) -{ - m_szName = pName; - m_callback = callback; - m_flags = flags; - Console()->RegisterCommand(this); -}; -const char *ConCommand::GetHelpText( void ) -{ - return m_szHelpString; -} -const char *ConCommand::GetName( void ) -{ - return m_szName; -} -ConCommandFn ConCommand::GetCallback( void ) -{ - return m_callback; -} @@ -330,6 +226,7 @@ void IConsole_Exec( int argc, char **argv) CUtlBuffer b(filesystem->Size(f)+1); filesystem->Read(f, b, b.GetSize()); b[b.GetSize()-1] = 0; + Console()->AddCommand(b); Console()->AddCommand(";"); Console()->Execute(); diff --git a/engine/engine.cpp b/engine/engine.cpp index 3b054d3..954febe 100644 --- a/engine/engine.cpp +++ b/engine/engine.cpp @@ -9,6 +9,7 @@ #include "cl_dll.h" #include "inetworkserver.h" #include "inetworkclient.h" +#include "ihumandevice.h" #ifdef STEAM #include "steam/steam_api.h" @@ -22,6 +23,14 @@ IGameWindowManager *g_pWindowManager; CServerGameDLL *g_pServerGame; CClientGameDLL *g_pClientGame; +static void CallKeyEvent( EInputDeviceType eDevice, EInputButton eButton, bool bIsPressed ) +{ + if (bIsPressed) + g_pHumanDeviceManager->SetButtonPressed(eButton); + else + g_pHumanDeviceManager->SetButtonUnpressed(eButton); +} + extern "C" void __cdecl SteamAPIDebug( ESteamNetworkingSocketsDebugOutputType nType, const char *pszMsg ) { V_printf("STEAM: %s\n", pszMsg); @@ -67,6 +76,7 @@ extern "C" void FunnyMain( int argc, char **argv ) pWindow = g_pWindowManager->CreateWindow(); pWindow->Init(); + pWindow->SetKeyCallback(CallKeyEvent); g_pRenderContext = (IRenderContext*)pRenderSystemFactory(RENDER_CONTEXT_INTERFACE_VERSION, NULL); g_pRenderContext->SetMainWindowManager(g_pWindowManager); @@ -98,6 +108,7 @@ extern "C" void FunnyMain( int argc, char **argv ) double fDelta = fCurrent-fPrevious; fPrevious = fCurrent; + g_pHumanDeviceManager->Frame(); g_pServerGame->m_pBridge->Frame(fDelta); if (!stConstants.m_bIsDedicated) diff --git a/engine/humandevice.cpp b/engine/humandevice.cpp index e8698f7..18acfdb 100644 --- a/engine/humandevice.cpp +++ b/engine/humandevice.cpp @@ -1,24 +1,93 @@ -#include "humandevice.h" +#include "ihumandevice.h" #include "tier1/interface.h" +#include "tier1/utlvector.h" class CHumanDeviceManager: public IHumanDeviceManager { public: + virtual void Frame() override; + + virtual void SetDefaultInput( IHumanDeviceInput *pInput ) override; + virtual void PushInput( IHumanDeviceInput *pInput ) override; + virtual void PopInput() override; + virtual IHumanDeviceInput *GetCurrentInput() override; + virtual void SetButtonPressed( EInputButton eButton ) override; virtual void SetButtonUnpressed( EInputButton eButton ) override; - bool m_abIsButtonPressed[k_EInputButton_Count]; + virtual void WriteUTF8( uint32_t uCode ) override; + + bool m_abIsButtonPressed[k_EInputButton_Count] = {}; + bool m_abWasButtonPressed[k_EInputButton_Count] = {}; + CUtlVector m_apInputStack; + IHumanDeviceInput *m_pDefaultInput; }; +CHumanDeviceManager s_manager; +IHumanDeviceManager *g_pHumanDeviceManager = &s_manager; + +void CHumanDeviceManager::Frame() +{ + IHumanDeviceInput *pInput = GetCurrentInput(); + if (pInput) + { + for ( int i = 0; i < k_EInputButton_Count; i++ ) + { + if (m_abIsButtonPressed[i] == m_abWasButtonPressed[i]) + continue; + pInput->OnGameButton(k_EInputDevice_Keyboard, + (EInputButton)i, m_abIsButtonPressed[i]); + } + } + V_memcpy(m_abWasButtonPressed, m_abIsButtonPressed, sizeof(m_abIsButtonPressed)); +} + + +void CHumanDeviceManager::SetDefaultInput( IHumanDeviceInput *pInput ) +{ + m_pDefaultInput = pInput; +} + +void CHumanDeviceManager::PushInput( IHumanDeviceInput *pInput ) +{ + m_apInputStack.AppendTail(pInput); +} + +void CHumanDeviceManager::PopInput() +{ + m_apInputStack.RemoveTail(); +} + +IHumanDeviceInput *CHumanDeviceManager::GetCurrentInput() +{ + if (m_apInputStack.GetSize() == 0) + return m_pDefaultInput; + return m_apInputStack[m_apInputStack.GetSize()-1]; +} + + void CHumanDeviceManager::SetButtonPressed( EInputButton eButton ) { m_abIsButtonPressed[eButton] = true; + IHumanDeviceInput *pInput = GetCurrentInput(); + if (pInput) + pInput->OnButton(k_EInputDevice_Keyboard, eButton, true); + } void CHumanDeviceManager::SetButtonUnpressed( EInputButton eButton ) { m_abIsButtonPressed[eButton] = false; + IHumanDeviceInput *pInput = GetCurrentInput(); + if (pInput) + pInput->OnButton(k_EInputDevice_Keyboard, eButton, false); +} + +void CHumanDeviceManager::WriteUTF8( uint32_t uCode ) +{ + } + EXPOSE_INTERFACE_GLOBALVAR(CHumanDeviceManager, IHumanDeviceManager, HUMAN_DEVICE_MANAGER_INTERFACE_VERSION, g_pHumanDeviceManager) diff --git a/engine/sv_dll.cpp b/engine/sv_dll.cpp index 73bc4e0..cf5dc37 100644 --- a/engine/sv_dll.cpp +++ b/engine/sv_dll.cpp @@ -6,19 +6,11 @@ void CServerGameDLL::Init() { - void *pLib; - -#ifdef __linux - pLib = Plat_LoadLibrary("libserver.so"); -#endif - if (!pLib) - Plat_FatalErrorFunc("Failed to load server library\n"); - m_pLibrary = pLib; - - CreateInterfaceFn pfnServerFactory = Sys_GetFactory(pLib); + CreateInterfaceFn pfnServerFactory = Sys_GetFactory("server"); IEngineBridge *pEngineBridge = (IEngineBridge*)pfnServerFactory(ENGINE_BRIDGE_INTERFACE_VERSION, NULL); pEngineBridge->ConnectInterface(FILESYSTEM_INTERFACE_VERSION, filesystem); pEngineBridge->ConnectInterface("EngineConstants", m_pEngineConsts); + pEngineBridge->ConnectInterface(FILESYSTEM_INTERFACE_VERSION, filesystem); pEngineBridge->Init(); m_pBridge = pEngineBridge; } diff --git a/external/GameNetworkingSockets b/external/GameNetworkingSockets index 517fff0..725e273 160000 --- a/external/GameNetworkingSockets +++ b/external/GameNetworkingSockets @@ -1 +1 @@ -Subproject commit 517fff0cf6866ba163f4f016b0ef28f365c06c05 +Subproject commit 725e273c7442bac7a8bc903c0b210b1c15c34d92 diff --git a/external/SDL b/external/SDL index 77f4a8e..7c11a8c 160000 --- a/external/SDL +++ b/external/SDL @@ -1 +1 @@ -Subproject commit 77f4a8e2b8066d3bc376eab1ce443e57bc04d4ee +Subproject commit 7c11a8cb9f66a2cac63f9a24ab2f49f6d4bf12a0 diff --git a/external/Vulkan-Headers b/external/Vulkan-Headers index ad9ce12..2e0a6e6 160000 --- a/external/Vulkan-Headers +++ b/external/Vulkan-Headers @@ -1 +1 @@ -Subproject commit ad9ce1235e88dc09287e19171dfac384db8ec32c +Subproject commit 2e0a6e699e35c9609bde2ca4abb0d380c0378639 diff --git a/external/Vulkan-Utility-Libraries b/external/Vulkan-Utility-Libraries index 738ec97..4f4c0b6 160000 --- a/external/Vulkan-Utility-Libraries +++ b/external/Vulkan-Utility-Libraries @@ -1 +1 @@ -Subproject commit 738ec97a3f659dd6469bff3c4078ef981b0a343f +Subproject commit 4f4c0b6c61223b703f1c753a404578d7d63932ad diff --git a/external/VulkanMemoryAllocator b/external/VulkanMemoryAllocator index e722e57..f0969e9 160000 --- a/external/VulkanMemoryAllocator +++ b/external/VulkanMemoryAllocator @@ -1 +1 @@ -Subproject commit e722e57c891a8fbe3cc73ca56c19dd76be242759 +Subproject commit f0969e908b01104f66bec2f203103de2012354c9 diff --git a/external/cglm b/external/cglm index 83d5b2c..a886d6e 160000 --- a/external/cglm +++ b/external/cglm @@ -1 +1 @@ -Subproject commit 83d5b2c97378e61a4666825fd3a5548955c83add +Subproject commit a886d6e170292a1d534a7a2c0471be5953b16455 diff --git a/external/slang b/external/slang index 6128e51..5d775e2 160000 --- a/external/slang +++ b/external/slang @@ -1 +1 @@ -Subproject commit 6128e511c77a2517e77826f8e2b4b5173ea0f332 +Subproject commit 5d775e2829ceef5bd34ec89aa3eafa4968a74c8e diff --git a/external/stb b/external/stb index f1c79c0..802cd45 160000 --- a/external/stb +++ b/external/stb @@ -1 +1 @@ -Subproject commit f1c79c02822848a9bed4315b12c8c8f3761e1296 +Subproject commit 802cd454f25469d3123e678af41364153c132c2a diff --git a/external/volk b/external/volk index 87f4f07..d64d20b 160000 --- a/external/volk +++ b/external/volk @@ -1 +1 @@ -Subproject commit 87f4f07894d9fa44667031d2906dcf32c47b137c +Subproject commit d64d20b4eaf2698296e085d9d9638c69af280e3e diff --git a/external/xtool b/external/xtool index f12a84e..22655f3 160000 --- a/external/xtool +++ b/external/xtool @@ -1 +1 @@ -Subproject commit f12a84e960fa9bced1249a8d233ab8eafca28f6f +Subproject commit 22655f3e70b87bef9be736e9625984e21bf8c697 diff --git a/game/client/baseentity.h b/game/client/baseentity.h index 3d6e313..8ffd63f 100644 --- a/game/client/baseentity.h +++ b/game/client/baseentity.h @@ -81,6 +81,8 @@ private: Vector m_vPosition; Quat m_vRotation; Vector m_vScale; + + uint64_t m_uSlot; }; #endif diff --git a/game/client/build.cpp b/game/client/build.cpp index 4a836ac..1238752 100644 --- a/game/client/build.cpp +++ b/game/client/build.cpp @@ -15,6 +15,8 @@ DECLARE_BUILD_STAGE(Client) compileProject.m_szName = "client"; compileProject.files = { + "../shared/game.cpp", + "game.cpp", "baseentity.cpp", diff --git a/game/client/entitysystem.cpp b/game/client/entitysystem.cpp index f85be41..b354ee6 100644 --- a/game/client/entitysystem.cpp +++ b/game/client/entitysystem.cpp @@ -10,6 +10,8 @@ #include "string.h" #include "stdlib.h" +#include "netprotocol.h" + CEntitySystem *EntitySystem() { @@ -155,7 +157,96 @@ void CEntitySystem::Think() } } +static void *UTIL_GetNetMapData(C_BaseEntity *pEntity, netmap_t *pMap, uint32_t uIndex ) +{ + netmap_t *pCurrentMap = pMap; + uint32_t uCurrentIndex = uIndex; +searchIndex: + if (uCurrentIndex >= pCurrentMap->m_uFieldCount || pCurrentMap->m_pBase ) + { + uCurrentIndex -= pCurrentMap->m_uFieldCount; + pCurrentMap = pCurrentMap->m_pBase; + if (!pCurrentMap) + return NULL; + goto searchIndex; + } + + return (char*)pEntity+pCurrentMap->m_pFields[uCurrentIndex].m_uOffset; +} + + +static C_BaseEntity *s_pLocalEntity; + +void CEntitySystem::NetRecvPacket( NetPacket_t *pPacket ) +{ + PlayerPacket_t *pPlayerPacket = (PlayerPacket_t*)pPacket->pData; + C_BaseEntity *pEntity; + switch (pPlayerPacket->m_eType) + { + case MESSAGE_ENTITY_CLASS_SYNC: + + V_printf("MESSAGE_ENTITY_CLASS_SYNC: %u = %s\n", + (uint32_t)pPlayerPacket->m_entityClass.m_uIndex, + pPlayerPacket->m_entityClass.m_szEntityName); + pEntity = CreateByClassnameWithIndex( + (char*)pPlayerPacket->m_entityClass.m_szEntityName, + pPlayerPacket->m_entityClass.m_uIndex + ); + if (pEntity == NULL) + break; + pEntity->Spawn(); + break; + case MESSAGE_ENTITY_DATA_SYNC: + pEntity = m_pEntities[pPlayerPacket->m_entityData.m_uIndex]; + if (pEntity == NULL) + break; + union { + void *pData; + char *pcCurrentData; + EntityDataSyncValue_t *pcSyncValue; + }; + pData = pPlayerPacket; + pcCurrentData += sizeof(EntityDataSync_t); + + // too bad + // this shall be reworked + for ( uint32_t u = 0; u < pPlayerPacket->m_entityData.m_uCount; u++ ) + { + + uint32_t uVariableSize = pcSyncValue->m_uVariableSize; + void *pValueData = (float*)UTIL_GetNetMapData( + pEntity, + pEntity->GetRecvMap(), + pcSyncValue->m_uVariableIndex); + + pcCurrentData += sizeof(EntityDataSyncValue_t); + if (pValueData) + V_memcpy(pValueData, pcCurrentData, uVariableSize); + pcCurrentData += (uVariableSize+7) & ~7; + } + break; + case k_EMessage_PlayerSetLocalEntity: + V_printf("k_EMessage_PlayerSetLocalEntity: %u\n",(uint32_t)pPlayerPacket->m_setLocalEntity.m_uIndex); + if (pPlayerPacket->m_setLocalEntity.m_uIndex > MAX_EDICTS) + break; + s_pLocalEntity = m_pEntities[pPlayerPacket->m_setLocalEntity.m_uIndex]; + default: + break; + } +} + +void CEntitySystem::NetSendThink() +{ + +} + + C_BaseEntity **CEntitySystem::GetEntities() { return m_pEntities; }; + +C_BaseEntity *UTIL_GetLocalPlayer() +{ + return s_pLocalEntity; +} diff --git a/game/client/entitysystem.h b/game/client/entitysystem.h index 3baf152..be26587 100644 --- a/game/client/entitysystem.h +++ b/game/client/entitysystem.h @@ -8,6 +8,7 @@ #define ENTITIES_H #include "stdint.h" +#include "networkbase.h" class IEntityFactory; class C_BaseEntity; @@ -29,6 +30,10 @@ public: virtual void DestroyEntityByPtr( C_BaseEntity *pEntity ); virtual void Think(); + + virtual void NetRecvPacket( NetPacket_t *pPacket ); + virtual void NetSendThink(); + virtual C_BaseEntity **GetEntities(); private: C_BaseEntity *m_pEntities[MAX_EDICTS]; @@ -37,4 +42,6 @@ private: CEntitySystem *EntitySystem(); +C_BaseEntity *UTIL_GetLocalPlayer(); + #endif diff --git a/game/client/game.cpp b/game/client/game.cpp index 3d288a1..4854701 100644 --- a/game/client/game.cpp +++ b/game/client/game.cpp @@ -15,21 +15,6 @@ #include "steam/steam_gameserver.h" #endif -IFileSystem *filesystem; - -IRenderContext *g_pRenderContext; -IGameWindow *g_pMainWindow; - -static CEngineVars s_vars; -CEngineVars *g_pEngineVars = &s_vars; -EngineConsts_t *g_pEngineConstants; - -INetworkBase *g_pServerBridge; -INetworkBase *g_pServerConnection; - -IPhysics *g_pPhysics; -IPhysicsWorld *g_pPhysicsWorld; - class CFunnyGameBridge: public IEngineBridge { virtual void Init() override; @@ -44,6 +29,35 @@ class CFunnyGameBridge: public IEngineBridge }; +class CFunnyInput: public IHumanDeviceInput +{ + + virtual EInputType GetInputType() override { return k_EInput_Game; }; + + virtual void OnGameButton( EInputDeviceType eDevice, EInputButton eScancode, bool bIsPressed ) override; + virtual void OnGameAxis( EInputDeviceType eDevice, EInputAxis eAxis, float fValue) override; + virtual void OnGameAxisDiff( EInputDeviceType eDevice, EInputAxis eAxis, float fValue ) override; + + virtual void OnButton( EInputDeviceType eDevice, EInputButton eScancode, bool bIsPressed ) override {}; + virtual void OnAxis( EInputDeviceType eDevice, EInputAxis eAxis, float fValue) override {}; + virtual void OnAxisDiff( EInputDeviceType eDevice, EInputAxis eAxis, float fValue ) override {}; + + virtual void OnTextWriteUTF8( uint32_t uCode ) override {}; +}; + +void CFunnyInput::OnGameButton( EInputDeviceType eDevice, EInputButton eScancode, bool bIsPressed ) +{ + +} +void CFunnyInput::OnGameAxis( EInputDeviceType eDevice, EInputAxis eAxis, float fValue) +{ + +} +void CFunnyInput::OnGameAxisDiff( EInputDeviceType eDevice, EInputAxis eAxis, float fValue ) +{ + +} + IEngineBridge *EngineBridge() { static CFunnyGameBridge s_bridge; @@ -52,6 +66,7 @@ IEngineBridge *EngineBridge() EXPOSE_INTERFACE_FN(EngineBridge, IEngineBridge, ENGINE_BRIDGE_INTERFACE_VERSION) +static CFunnyInput s_mainInput; void CFunnyGameBridge::Init() { @@ -79,30 +94,14 @@ void CFunnyGameBridge::Init() CreateInterfaceFn fnPhysicsFactory = Sys_GetFactory("RapierPhysics"); g_pPhysics = (IPhysics*)fnPhysicsFactory(PHYSICS_INTERFACE_VERSION, NULL); g_pPhysicsWorld = g_pPhysics->CreateWorld(); + + g_pHumanDeviceManager->SetDefaultInput(&s_mainInput); } void CFunnyGameBridge::Tick( float fDelta ) { } -void *ENT_GetNetMapData(C_BaseEntity *pEntity, netmap_t *pMap, uint32_t uIndex ) -{ - netmap_t *pCurrentMap = pMap; - uint32_t uCurrentIndex = uIndex; -searchIndex: - if (uCurrentIndex >= pCurrentMap->m_uFieldCount) - { - if (!pCurrentMap->m_uFieldCount) - return NULL; - uCurrentIndex -= pCurrentMap->m_uFieldCount; - pCurrentMap = pCurrentMap->m_pBase; - if (!pCurrentMap) - return NULL; - goto searchIndex; - } - - return (char*)pEntity+pCurrentMap->m_pFields[uCurrentIndex].m_uOffset; -} void CFunnyGameBridge::TryToConnectToServer() { @@ -111,27 +110,25 @@ void CFunnyGameBridge::TryToConnectToServer() { if (m_bIsConnectedToSteamRelay != 0 ) return; - if ( SteamNetworkingUtils()->GetRelayNetworkStatus(NULL) == k_ESteamNetworkingAvailability_Current) + if ( SteamNetworkingUtils()->GetRelayNetworkStatus(NULL) != k_ESteamNetworkingAvailability_Current) + return; + m_bIsConnectedToSteamRelay = 1; + if (!CommandLine()->ParamValue("-steam-connect")) + return; + V_printf("%llu\n", SteamUser()->GetSteamID().ConvertToUint64()); + char *pEnd = NULL; + uint64_t uValue = strtoull(CommandLine()->ParamValue("-steam-connect"), &pEnd, 10); + g_pServerConnection = g_pEngineConstants->ConnectSteamServer(uValue, FUNNY_SECURE_PORT); + if (g_pServerConnection) { - m_bIsConnectedToSteamRelay = 1; - V_printf("%llu\n", SteamUser()->GetSteamID().ConvertToUint64()); - if (CommandLine()->ParamValue("-steam-connect")) - { - char *pEnd = NULL; - uint64_t uValue = strtoull(CommandLine()->ParamValue("-steam-connect"), &pEnd, 10); - g_pServerConnection = g_pEngineConstants->ConnectSteamServer(uValue, FUNNY_SECURE_PORT); - if (g_pServerConnection) - { - m_bIsConnectedToServer = true; - C_BaseEntity **ppEntities = EntitySystem()->GetEntities(); - for ( int i = 0; i < MAX_EDICTS; i++ ) - { - EntitySystem()->DestroyEntityByIndex(i); - } - } - return; + m_bIsConnectedToServer = true; + C_BaseEntity **ppEntities = EntitySystem()->GetEntities(); + for ( int i = 0; i < MAX_EDICTS; i++ ) + { + EntitySystem()->DestroyEntityByIndex(i); } } + return; } #endif } @@ -158,53 +155,19 @@ void CFunnyGameBridge::Frame( float fDelta ) while ( pCurrentServer->BHasUpdates() ) { NetPacket_t packet = pCurrentServer->PeekPacket(); + // discard it if (packet.uSize < sizeof (EMessageType)) continue; + PlayerPacket_t *pPacket = (PlayerPacket_t*)packet.pData; C_BaseEntity *pEntity; switch (pPacket->m_eType) { case MESSAGE_ENTITY_CLASS_SYNC: - V_printf("MESSAGE_ENTITY_CLASS_SYNC: %u = %s\n", (uint32_t)pPacket->m_entityClass.m_uIndex, pPacket->m_entityClass.m_szEntityName); - pEntity = EntitySystem()->CreateByClassnameWithIndex( - (char*)pPacket->m_entityClass.m_szEntityName, pPacket->m_entityClass.m_uIndex - ); - if (pEntity == NULL) - { - pCurrentServer->RecievePacket(); - continue; - } - pEntity->Spawn(); - pCurrentServer->RecievePacket(); - break; case MESSAGE_ENTITY_DATA_SYNC: - pEntity = EntitySystem()->GetEntities()[pPacket->m_entityData.m_uIndex]; - if (pEntity == NULL) - { - pCurrentServer->RecievePacket(); - continue; - } - union { - void *pData; - char *pcCurrentData; - EntityDataSyncValue_t *pcSyncValue; - }; - pData = pPacket; - pcCurrentData += sizeof(EntityDataSync_t); - for ( uint32_t u = 0; u < pPacket->m_entityData.m_uCount; u++ ) - { - uint32_t uVariableSize = pcSyncValue->m_uVariableSize; - void *pValueData = (float*)ENT_GetNetMapData( - pEntity, - pEntity->GetRecvMap(), - pcSyncValue->m_uVariableIndex); - - pcCurrentData += sizeof(EntityDataSyncValue_t); - if (pValueData) - V_memcpy(pValueData, pcCurrentData, uVariableSize); - pcCurrentData += (uVariableSize+7) & ~7; - } + case k_EMessage_PlayerSetLocalEntity: + EntitySystem()->NetRecvPacket(&packet); pCurrentServer->RecievePacket(); break; default: @@ -227,8 +190,9 @@ void CFunnyGameBridge::Shutdown() #define CONNECT_INTERFACE(szName, pGlobal) if (!V_strcmp(psz, szName)) { pGlobal = (typeof(pGlobal))pInterface; return; } void CFunnyGameBridge::ConnectInterface( const char *psz, void *pInterface ) { - CONNECT_INTERFACE(RENDER_CONTEXT_INTERFACE_VERSION, g_pRenderContext); CONNECT_INTERFACE(FILESYSTEM_INTERFACE_VERSION, filesystem); + CONNECT_INTERFACE(RENDER_CONTEXT_INTERFACE_VERSION, g_pRenderContext); + CONNECT_INTERFACE(HUMAN_DEVICE_MANAGER_INTERFACE_VERSION, g_pHumanDeviceManager); CONNECT_INTERFACE("MainWindow", g_pMainWindow); CONNECT_INTERFACE("EngineConstants", g_pEngineConstants); } diff --git a/game/client/milmoba/player.cpp b/game/client/milmoba/player.cpp index 22806dc..6698837 100644 --- a/game/client/milmoba/player.cpp +++ b/game/client/milmoba/player.cpp @@ -1,6 +1,7 @@ #include "player.h" #include "cglm/cglm.h" #include "assetmgr.h" +#include "game.h" void C_MOBAPlayer::Precache() { @@ -12,12 +13,19 @@ void C_MOBAPlayer::Spawn() { BaseClass::Spawn(); SetThink(Think); - g_pWorldRenderer->SetCameraPosition({0, 0, -20}); }; void C_MOBAPlayer::Think( float fDelta ) { + BaseClass::Think(fDelta); + C_MOBAPlayer *pEntity = (C_MOBAPlayer*)UTIL_GetLocalPlayer(); + if (pEntity == this) + { + Vector vCameraPos = GetAbsOrigin(); + vCameraPos.z -= 20; + g_pWorldRenderer->SetCameraPosition(vCameraPos); + } }; LINK_ENTITY_TO_CLASS(player, C_MOBAPlayer) @@ -26,7 +34,21 @@ BEGIN_DATADESC(C_MOBAPlayer) END_DATADESC() IMPLEMENT_RECV_DT(C_MOBAPlayer) - NetPropFloat(m_fTimer), END_RECV_DT() IMPLEMENT_EMPTY_SEND_DT(C_MOBAPlayer) + +static void IN_ForwardDown( int c, char **v ) { + +} +static ConCommand startforward("+forward", IN_ForwardDown); + +static void IN_ForwardUp( int c, char **v ) { + C_MOBAPlayer *pEntity = (C_MOBAPlayer*)UTIL_GetLocalPlayer(); + if (pEntity) + { + + } + +} +static ConCommand endforward("-forward", IN_ForwardUp); diff --git a/game/client/milmoba/player.h b/game/client/milmoba/player.h index c642d45..8a65412 100644 --- a/game/client/milmoba/player.h +++ b/game/client/milmoba/player.h @@ -15,7 +15,6 @@ public: virtual void Spawn( void ) override; void Think( float fDelta ); - float m_fTimer; int m_bIsShooting; float m_fFBWalkingDirection; float m_fLRWalkingDirection; diff --git a/game/client/worldrender.cpp b/game/client/worldrender.cpp index 4639d0e..ded4b9d 100644 --- a/game/client/worldrender.cpp +++ b/game/client/worldrender.cpp @@ -1,10 +1,10 @@ #include "worldrender.h" #include "tier1/utlstring.h" -#include "engine.h" #include "cglm/cglm.h" #include "cglm/quat.h" #include "cglm/mat4.h" +#include "game.h" struct ViewBuffer_t { diff --git a/game/server/build.cpp b/game/server/build.cpp index 3ed3338..7f0362c 100644 --- a/game/server/build.cpp +++ b/game/server/build.cpp @@ -16,6 +16,8 @@ DECLARE_BUILD_STAGE(Server) compileProject.m_szName = "server"; compileProject.files = { + "../shared/game.cpp", + "game.cpp", "assetmgr.cpp", diff --git a/game/server/entitysystem.cpp b/game/server/entitysystem.cpp index 27c1f85..78f3a30 100644 --- a/game/server/entitysystem.cpp +++ b/game/server/entitysystem.cpp @@ -9,7 +9,7 @@ #include "stddef.h" #include "string.h" #include "stdlib.h" -#include "engine.h" +#include "game.h" #include "netprotocol.h" @@ -56,7 +56,7 @@ void CEntitySystem::RegisterEntityClass( IEntityFactory *pEntityFactory, const c s_pEntitiesRegistry = pRegistry; } -CBaseEntity *CEntitySystem::CreateByClassname( const char *szName ) +CBaseEntity *CEntitySystem::CreateByClassname( const char *szName, int *pOutputIndex ) { IEntityFactory *pFactory; CBaseEntity *pEntity; @@ -87,6 +87,9 @@ CBaseEntity *CEntitySystem::CreateByClassname( const char *szName ) m_pEntities[iSelectedSlot] = pEntity; m_nEntityCount++; + if (pOutputIndex) + *pOutputIndex = iSelectedSlot; + EntityClass_t stClassSync = { MESSAGE_ENTITY_CLASS_SYNC, iSelectedSlot, diff --git a/game/server/entitysystem.h b/game/server/entitysystem.h index ee864df..99f89bf 100644 --- a/game/server/entitysystem.h +++ b/game/server/entitysystem.h @@ -20,7 +20,7 @@ public: CEntitySystem(); virtual void RegisterEntityClass( IEntityFactory *pEntityFactory, const char *szClassName ); - virtual CBaseEntity *CreateByClassname( const char *szName ); + virtual CBaseEntity *CreateByClassname( const char *szName, int *pOutputIndex ); virtual IEntityFactory *GetFactoryByClassname( const char *szName ); diff --git a/game/server/game.cpp b/game/server/game.cpp index ee9e169..53e482b 100644 --- a/game/server/game.cpp +++ b/game/server/game.cpp @@ -12,23 +12,6 @@ #include "steam/steam_gameserver.h" #endif - -IFileSystem *filesystem; - -IRenderContext *g_pRenderContext; -IGameWindow *g_pMainWindow; - -static CEngineVars s_vars; -CEngineVars *g_pEngineVars = &s_vars; -EngineConsts_t *g_pEngineConstants; - -INetworkBase *g_pClientBridge; -INetworkBase *g_pPublicConnection; -INetworkBase *g_pCurrentConnection; - -IPhysics *g_pPhysics; -IPhysicsWorld *g_pPhysicsWorld; - class CFunnyGameBridge: public IEngineBridge { virtual void Init() override; @@ -81,9 +64,17 @@ uint32_t NET_ServerCallback( NetCallback_t *pCallback ) g_pCurrentConnection->SendPacket({&stClassSync, sizeof(stClassSync), pCallback->m_ullUserConnection, PACKET_MUST_ARRIVE}); } + int iIndex = -1; CBaseEntity *pEntity; - pEntity = EntitySystem()->CreateByClassname("player"); + pEntity = EntitySystem()->CreateByClassname("player", &iIndex); pEntity->Spawn(); + + SetLocalEntity_t stLocalEntity = { + k_EMessage_PlayerSetLocalEntity, + iIndex, + }; + if (g_pCurrentConnection) + g_pCurrentConnection->SendPacket({&stLocalEntity, sizeof(stLocalEntity), pCallback->m_ullUserConnection, PACKET_MUST_ARRIVE}); return 1; } return 0; @@ -149,13 +140,29 @@ void NET_ProcessPacket( INetworkBase *pBase ) switch (pPacket->m_eType) { case MESSAGE_PLAYER_JOINED: - // online packets don't generate these - if (g_pEngineConstants->m_bIsDedicated) - break; - pBase->RecievePacket(); - V_printf("Hi %s\n",pPacket->m_playerJoined.m_szPlayerName); - pEntity = EntitySystem()->CreateByClassname("player"); - pEntity->Spawn(); + { + + // online packets don't generate these + if (g_pEngineConstants->m_bIsDedicated) + break; + + pBase->RecievePacket(); + + int iIndex = -1; + V_printf("Hi %s\n",pPacket->m_playerJoined.m_szPlayerName); + for ( int i = 0; i < 2; i++ ) + { + pEntity = EntitySystem()->CreateByClassname("player", &iIndex); + pEntity->Spawn(); + } + + SetLocalEntity_t stLocalEntity = { + k_EMessage_PlayerSetLocalEntity, + iIndex, + }; + if (g_pCurrentConnection) + g_pCurrentConnection->SendPacket({&stLocalEntity, sizeof(stLocalEntity), 0, PACKET_MUST_ARRIVE}); + } return; default: break; diff --git a/game/server/milmoba/player.cpp b/game/server/milmoba/player.cpp index 39c4e70..f0392f5 100644 --- a/game/server/milmoba/player.cpp +++ b/game/server/milmoba/player.cpp @@ -1,19 +1,20 @@ #include "player.h" #include "game.h" +#include "entitysystem.h" void CMOBAPlayer::Spawn() { + CPhysicsProp::Spawn(); SetModel("game/core/models/cube.fmdl"); + SetScale(1); SetThink(Think); - m_fTimer = 0; }; void CMOBAPlayer::Think( float fDelta ) { - BaseClass::Think(fDelta); - SetScale(1); + CPhysicsProp::Think(fDelta); }; LINK_ENTITY_TO_CLASS(player, CMOBAPlayer) @@ -22,7 +23,6 @@ BEGIN_DATADESC(CMOBAPlayer) END_DATADESC() IMPLEMENT_SEND_DT(CMOBAPlayer) - NetPropFloat(m_fTimer) END_SEND_DT() IMPLEMENT_EMPTY_RECV_DT(CMOBAPlayer) diff --git a/game/server/milmoba/player.h b/game/server/milmoba/player.h index 88fce5e..dc288bd 100644 --- a/game/server/milmoba/player.h +++ b/game/server/milmoba/player.h @@ -1,12 +1,12 @@ #ifndef MILMOBA_PLAYER_H #define MILMOBA_PLAYER_H -#include "basemodelentity.h" +#include "physicsprop.h" -class CMOBAPlayer: public CBaseModelEntity +class CMOBAPlayer: public CPhysicsProp { public: - DECLARE_CLASS(CMOBAPlayer, CBaseModelEntity); + DECLARE_CLASS(CMOBAPlayer, CPhysicsProp); DECLARE_DATADESC(); DECLARE_SERVERCLASS() diff --git a/game/server/physicsprop.cpp b/game/server/physicsprop.cpp index 0fb4d14..2467790 100644 --- a/game/server/physicsprop.cpp +++ b/game/server/physicsprop.cpp @@ -9,6 +9,7 @@ void CPhysicsProp::Precache() void CPhysicsProp::Spawn() { + CBaseEntity::Spawn(); SetThink(Think); } diff --git a/game/shared/engine.h b/game/shared/engine.h deleted file mode 100644 index 11b9998..0000000 --- a/game/shared/engine.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef ENGINE_H -#define ENGINE_H - -#include "tier2/ifilesystem.h" -#include "materialsystem/imaterialsystem.h" -#include "materialsystem/igamewindow.h" -#include "networkbase.h" - -struct EngineConsts_t -{ - bool m_bIsDedicated; - bool m_bIsSteam; - INetworkBase *(LaunchLocalBridge)(uint16_t uPort); - INetworkBase *(LaunchServer)(uint16_t uPort); - INetworkBase *(ConnectLocalBridge)(uint16_t uPort); - INetworkBase *(ConnectSteamServer)(uint64_t uServer, uint16_t uPort); -}; - -extern IFileSystem *filesystem; -extern IRenderContext *g_pRenderContext; -extern IGameWindow *g_pMainWindow; -extern EngineConsts_t *g_pEngineConstants; -extern INetworkBase *g_pServerBridge; -extern INetworkBase *g_pClientBridge; -extern INetworkBase *g_pPublicConnection; -extern INetworkBase *g_pCurrentConnection; - -#endif diff --git a/game/shared/game.cpp b/game/shared/game.cpp new file mode 100644 index 0000000..417ac61 --- /dev/null +++ b/game/shared/game.cpp @@ -0,0 +1,40 @@ +#include "game.h" + +IFileSystem *filesystem; +IRenderContext *g_pRenderContext; +IGameWindow *g_pMainWindow; + +IHumanDeviceManager *g_pHumanDeviceManager; + +INetworkBase *g_pServerBridge; +INetworkBase *g_pClientBridge; +INetworkBase *g_pServerConnection; + +INetworkBase *g_pPublicConnection; +INetworkBase *g_pCurrentConnection; + + +static CEngineVars s_vars; +CEngineVars *g_pEngineVars = &s_vars; +EngineConsts_t *g_pEngineConstants; + +IPhysics *g_pPhysics; +IPhysicsWorld *g_pPhysicsWorld; + +CreateInterfaceFn GetEngineFactory() +{ + return Sys_GetFactory("engine"); +}; + +// we need it to run before everything else +static IConsole *s_pConsole = + (IConsole*)GetEngineFactory()(CONSOLE_INTERFACE_VERSION, NULL); +IConsole *g_pConsole = s_pConsole; +EXPOSE_INTERFACE_GLOBALVAR(IConsole, IConsole, CONSOLE_INTERFACE_VERSION, g_pConsole) + +IConsole *Console() +{ + static IConsole *s_pConsole = + (IConsole*)GetEngineFactory()(CONSOLE_INTERFACE_VERSION, NULL); + return s_pConsole; +} diff --git a/game/shared/game.h b/game/shared/game.h index 78957ab..44c88fb 100644 --- a/game/shared/game.h +++ b/game/shared/game.h @@ -1,11 +1,14 @@ #ifndef GAME_H #define GAME_H + #include "tier2/ifilesystem.h" +#include "enginebridge.h" +#include "icvar.h" #include "materialsystem/imaterialsystem.h" +#include "materialsystem/igamewindow.h" +#include "networkbase.h" #include "iphysics.h" -extern IRenderContext *g_pRenderContext; -extern IFileSystem *filesystem; -extern IGameWindowManager *g_pWindowManager; +#include "ihumandevice.h" class CEngineVars { @@ -14,7 +17,24 @@ public: double m_fDeltaTime; }; +extern IFileSystem *filesystem; + + +extern IRenderContext *g_pRenderContext; +extern IGameWindow *g_pMainWindow; + + +extern INetworkBase *g_pServerBridge; +extern INetworkBase *g_pClientBridge; + +extern INetworkBase *g_pServerConnection; +extern INetworkBase *g_pPublicConnection; +extern INetworkBase *g_pCurrentConnection; + + extern CEngineVars *g_pEngineVars; +extern EngineConsts_t *g_pEngineConstants; + extern IPhysics *g_pPhysics; extern IPhysicsWorld *g_pPhysicsWorld; @@ -22,4 +42,11 @@ extern IPhysicsWorld *g_pPhysicsWorld; #define FUNNY_SECURE_PORT 27015 #define FUNNY_QUERY_PORT 27016 +#define CON_COMMAND( name, description ) \ + static void name( int c, char **v ); \ + static ConCommand name##_command(#name, name, description); \ + static void name( int c, char **v ) \ + +CreateInterfaceFn GetEngineFactory(); + #endif diff --git a/game/shared/netprotocol.h b/game/shared/netprotocol.h index a6d6d03..ea92465 100644 --- a/game/shared/netprotocol.h +++ b/game/shared/netprotocol.h @@ -23,6 +23,8 @@ enum EMessageType: uint32_t MESSAGE_PLAYER_LEFT, MESSAGE_ENTITY_CLASS_SYNC, MESSAGE_ENTITY_DATA_SYNC, + + k_EMessage_PlayerSetLocalEntity, }; struct PlayerJoined_t @@ -53,7 +55,7 @@ struct EntityClass_t struct EntityDataSync_t { EMessageType m_eType; - CNetworkUInt32 m_uIndex; + CNetworkUInt32 m_uIndex; CNetworkUInt32 m_uCount; }; @@ -63,6 +65,12 @@ struct EntityDataSyncValue_t CNetworkUInt32 m_uVariableSize; }; +struct SetLocalEntity_t +{ + EMessageType m_eType; + CNetworkUInt32 m_uIndex; +}; + union PlayerPacket_t { EMessageType m_eType; @@ -71,6 +79,7 @@ union PlayerPacket_t PlayerLeft_t m_playerLeft; EntityClass_t m_entityClass; EntityDataSync_t m_entityData; + SetLocalEntity_t m_setLocalEntity; }; #endif diff --git a/materialsystem/gamewindow_sdl.cpp b/materialsystem/gamewindow_sdl.cpp index d647d7d..63f126f 100644 --- a/materialsystem/gamewindow_sdl.cpp +++ b/materialsystem/gamewindow_sdl.cpp @@ -25,11 +25,17 @@ public: virtual void SetOutputImage( IImage *pImage ) override; virtual IImage *GetOutputImage() override; + virtual void SetKeyCallback( KeyCallbackFn fn ) override; + virtual void SetAxisCallback( AxisCallbackFn fn ) override; + virtual void *CreateVulkanSurface( void *pInstance ) override; virtual void DestroyVulkanSurface( void *pInstance ) override; SDL_WindowID WindowID(); + KeyCallbackFn m_fnKeyCallback; + AxisCallbackFn m_fnAxisCallback; + bool m_bWindowSizeUpdated; uint32_t m_uRenderWidth; uint32_t m_uRenderHeight; @@ -87,6 +93,16 @@ IImage *CSDLGameWindow::GetOutputImage() } +void CSDLGameWindow::SetKeyCallback( KeyCallbackFn fn ) +{ + m_fnKeyCallback = fn; +} + +void CSDLGameWindow::SetAxisCallback( AxisCallbackFn fn ) +{ + m_fnAxisCallback = fn; +} + void *CSDLGameWindow::CreateVulkanSurface( void *pInstance ) { @@ -98,6 +114,7 @@ void CSDLGameWindow::DestroyVulkanSurface( void *pInstance ) { SDL_Vulkan_DestroySurface((VkInstance)pInstance, (VkSurfaceKHR)m_hSurface, NULL); } + SDL_WindowID CSDLGameWindow::WindowID() { @@ -132,6 +149,76 @@ void CSDLGameWindowManager::Init() if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_GAMEPAD)) Plat_FatalErrorFunc("SDL_Init: %s\n", SDL_GetError()); } +static EInputButton GetKeyButton( SDL_Keycode eCode ) +{ + switch(eCode) + { + case SDLK_ESCAPE: return k_EInputButton_ESCAPE; + case SDLK_TAB: return k_EInputButton_TAB; + case SDLK_RETURN: return k_EInputButton_ENTER; + case SDLK_RCTRL: return k_EInputButton_CONTROL; + case SDLK_LCTRL: return k_EInputButton_CONTROL; + case SDLK_RSHIFT: return k_EInputButton_SHIFT; + case SDLK_LSHIFT: return k_EInputButton_SHIFT; + case SDLK_RALT: return k_EInputButton_ALT; + case SDLK_LALT: return k_EInputButton_ALT; + case SDLK_SPACE: return k_EInputButton_SPACE; + + case SDLK_GRAVE: return k_EInputButton_TILDE; + + case SDLK_F1: return k_EInputButton_F1; + case SDLK_F2: return k_EInputButton_F2; + case SDLK_F3: return k_EInputButton_F3; + case SDLK_F4: return k_EInputButton_F4; + case SDLK_F5: return k_EInputButton_F5; + case SDLK_F6: return k_EInputButton_F6; + case SDLK_F7: return k_EInputButton_F7; + case SDLK_F8: return k_EInputButton_F8; + case SDLK_F9: return k_EInputButton_F9; + case SDLK_F10: return k_EInputButton_F10; + case SDLK_F11: return k_EInputButton_F11; + case SDLK_F12: return k_EInputButton_F12; + + case SDLK_1: return k_EInputButton_1; + case SDLK_2: return k_EInputButton_2; + case SDLK_3: return k_EInputButton_3; + case SDLK_4: return k_EInputButton_4; + case SDLK_5: return k_EInputButton_5; + case SDLK_6: return k_EInputButton_6; + case SDLK_7: return k_EInputButton_7; + case SDLK_8: return k_EInputButton_8; + case SDLK_9: return k_EInputButton_9; + case SDLK_0: return k_EInputButton_0; + + case SDLK_A: return k_EInputButton_A; + case SDLK_B: return k_EInputButton_B; + case SDLK_C: return k_EInputButton_C; + case SDLK_D: return k_EInputButton_D; + case SDLK_E: return k_EInputButton_E; + case SDLK_F: return k_EInputButton_F; + case SDLK_G: return k_EInputButton_G; + case SDLK_H: return k_EInputButton_H; + case SDLK_I: return k_EInputButton_I; + case SDLK_J: return k_EInputButton_J; + case SDLK_K: return k_EInputButton_K; + case SDLK_L: return k_EInputButton_L; + case SDLK_M: return k_EInputButton_M; + case SDLK_N: return k_EInputButton_N; + case SDLK_O: return k_EInputButton_O; + case SDLK_P: return k_EInputButton_P; + case SDLK_Q: return k_EInputButton_Q; + case SDLK_R: return k_EInputButton_R; + case SDLK_S: return k_EInputButton_S; + case SDLK_T: return k_EInputButton_T; + case SDLK_U: return k_EInputButton_U; + case SDLK_V: return k_EInputButton_V; + case SDLK_W: return k_EInputButton_W; + case SDLK_X: return k_EInputButton_X; + case SDLK_Y: return k_EInputButton_Y; + case SDLK_Z: return k_EInputButton_Z; + } + return k_EInputButton_NONE; +} void CSDLGameWindowManager::Frame( float fDelta ) { @@ -159,6 +246,28 @@ void CSDLGameWindowManager::Frame( float fDelta ) pWindow->m_uRenderWidth = event.window.data1; pWindow->m_uRenderHeight = event.window.data2; break; + case SDL_EVENT_KEY_UP: + for (auto a: m_pWindows) + { + if (a->WindowID() != event.window.windowID) + break; + pWindow = a; + break; + } + if (pWindow->m_fnKeyCallback) + pWindow->m_fnKeyCallback(k_EInputDevice_Keyboard, GetKeyButton(event.key.key), false); + break; + case SDL_EVENT_KEY_DOWN: + for (auto a: m_pWindows) + { + if (a->WindowID() != event.window.windowID) + break; + pWindow = a; + break; + } + if (pWindow->m_fnKeyCallback) + pWindow->m_fnKeyCallback(k_EInputDevice_Keyboard, GetKeyButton(event.key.key), true); + break; case SDL_EVENT_QUIT: Plat_Exit(0); break; diff --git a/public/enginebridge.h b/public/enginebridge.h index 343e007..c1bbdea 100644 --- a/public/enginebridge.h +++ b/public/enginebridge.h @@ -3,6 +3,8 @@ #include "gamesystem.h" #include "networkbase.h" +#include "icvar.h" +#include "tier1/interface.h" struct EngineConsts_t { diff --git a/public/icvar.h b/public/icvar.h index c2c9960..031f607 100644 --- a/public/icvar.h +++ b/public/icvar.h @@ -5,6 +5,7 @@ #include "tier0/platform.h" #include "tier1/utlstring.h" #include "tier1/utlvector.h" +#include "tier1/interface.h" class ConVar; class ConCommand; @@ -32,12 +33,10 @@ public: virtual void AddCommand( const char *psz ) = 0; virtual void InsertCommand( const char *psz ) = 0; - - CUtlVector m_convars; - CUtlVector m_commands; }; IConsole *Console(); +#define CONSOLE_INTERFACE_VERSION "Console001" #define FCVAR_NONE 0 @@ -91,6 +90,92 @@ private: int m_flags; }; +inline ConVar::ConVar( const char *pName, const char *pDefaultValue, int flags ) + : ConVar(pName, pDefaultValue, flags, 0) +{ + +} +inline ConVar::ConVar( const char *pName, const char *pDefaultValue, int flags, + const char *pHelpString ) + : ConVar(pName, pDefaultValue, flags, pHelpString, 0) +{ +} +inline ConVar::ConVar( const char *pName, const char *pDefaultValue, int flags, + const char *pHelpString, ConCommandFn callback ) +{ + m_szName = pName; + m_flags = flags; + SetValue(pDefaultValue); +} + +inline bool ConVar::IsFlagSet( int flag ) +{ + return (m_flags & flag) > 0; +} +inline const char *ConVar::GetHelpText( void ) +{ + return m_szHelpString; +} +inline bool ConVar::IsRegistered( void ) +{ + +} +inline const char *ConVar::GetName( void ) +{ + return m_szName; +} +inline void ConVar::AddFlags( int flags ) +{ + +} +inline bool ConVar::IsCommand( void ) +{ + +} + +inline void ConVar::InstallChangeCallback( ConCommandFn ) +{ + +} + +inline float ConVar::GetFloat( void ) +{ + return m_fValue; +} +inline int ConVar::GetInt( void ) +{ + return m_nValue; +} +inline bool ConVar::GetBool( void ) +{ + return m_nValue; +} +inline const char *ConVar::GetString( void ) +{ + +} + +inline void ConVar::SetValue( const char *szValue ) +{ + if (!szValue) + return; + m_szValue = szValue; + m_fValue = V_atof(szValue); + m_nValue = V_atoi(szValue); +} +inline void ConVar::SetValue( float fValue ) +{ + m_fValue = fValue; + m_nValue = fValue; + m_szValue = CUtlString("%f\n",fValue); +} +inline void ConVar::SetValue( int iValue ) +{ + m_fValue = iValue; + m_nValue = iValue; + m_szValue = CUtlString("%i\n",iValue); +} + class ConCommand { @@ -109,11 +194,28 @@ private: int m_flags; }; -#undef V_printf -#define V_printf(...) Msg(CUtlString(__VA_ARGS__).GetString()) +inline ConCommand::ConCommand(const char *pName, ConCommandFn callback, + const char *pHelpString, int flags) +{ + m_szName = pName; + m_callback = callback; + m_flags = flags; + Console()->RegisterCommand(this); +}; -void Msg( const char* message ); -void Warning( const char* message ); -void Error( const char* message ); +inline const char *ConCommand::GetHelpText( void ) +{ + return m_szHelpString; +} + +inline const char *ConCommand::GetName( void ) +{ + return m_szName; +} + +inline ConCommandFn ConCommand::GetCallback( void ) +{ + return m_callback; +} #endif diff --git a/public/ihumandevice.h b/public/ihumandevice.h new file mode 100644 index 0000000..d4fa8d2 --- /dev/null +++ b/public/ihumandevice.h @@ -0,0 +1,44 @@ +#ifndef KEYBINDINGS_H +#define KEYBINDINGS_H + +#include "tier0/platform.h" +#include "materialsystem/humandevice_enums.h" + + +abstract_class IHumanDeviceInput +{ +public: + virtual EInputType GetInputType() = 0; + + virtual void OnGameButton( EInputDeviceType eDevice, EInputButton eScancode, bool bIsPressed ) = 0; + virtual void OnGameAxis( EInputDeviceType eDevice, EInputAxis eAxis, float fValue) = 0; + virtual void OnGameAxisDiff( EInputDeviceType eDevice, EInputAxis eAxis, float fValue ) = 0; + + virtual void OnButton( EInputDeviceType eDevice, EInputButton eScancode, bool bIsPressed ) = 0; + virtual void OnAxis( EInputDeviceType eDevice, EInputAxis eAxis, float fValue) = 0; + virtual void OnAxisDiff( EInputDeviceType eDevice, EInputAxis eAxis, float fValue ) = 0; + + virtual void OnTextWriteUTF8( uint32_t uCode ) = 0; +}; + +abstract_class IHumanDeviceManager +{ +public: + virtual void Frame() = 0; + + virtual void SetDefaultInput( IHumanDeviceInput *pInput ) = 0; + virtual void PushInput( IHumanDeviceInput *pInput ) = 0; + virtual void PopInput() = 0; + virtual IHumanDeviceInput *GetCurrentInput() = 0; + + virtual void SetButtonPressed( EInputButton eButton ) = 0; + virtual void SetButtonUnpressed( EInputButton eButton ) = 0; + + virtual void WriteUTF8( uint32_t uCode ) = 0; +}; + +#define HUMAN_DEVICE_MANAGER_INTERFACE_VERSION "HumanDeviceMgr001" + +extern IHumanDeviceManager *g_pHumanDeviceManager; + +#endif diff --git a/engine/humandevice.h b/public/materialsystem/humandevice_enums.h similarity index 69% rename from engine/humandevice.h rename to public/materialsystem/humandevice_enums.h index 3aeded8..1435025 100644 --- a/engine/humandevice.h +++ b/public/materialsystem/humandevice_enums.h @@ -1,17 +1,16 @@ -#ifndef KEYBINDINGS_H -#define KEYBINDINGS_H +#ifndef HD_ENUMS_H +#define HD_ENUMS_H -#include "tier0/platform.h" - -enum EInputButton +enum EInputButton { k_EInputButton_NONE = 0, - k_EInputButton_MOUSE_BUTTON_0, - k_EInputButton_MOUSE_BUTTON_1, - k_EInputButton_MOUSE_BUTTON_2, - k_EInputButton_MOUSE_BUTTON_3, - k_EInputButton_MOUSE_BUTTON_4, + k_EInputButton_Mouse_Left, + k_EInputButton_Mouse_Right, + k_EInputButton_Mouse_3, + k_EInputButton_Mouse_4, + k_EInputButton_Mouse_ScrollUp, + k_EInputButton_Mouse_ScrollDown, k_EInputButton_ESCAPE, k_EInputButton_TAB, @@ -86,6 +85,12 @@ enum EInputButton k_EInputButton_Count = k_EInputButton_MAX - 1, }; +enum EInputAxis +{ + k_EInputAxis_MouseX, + k_EInputAxis_MouseY, +}; + enum EInputType { k_EInput_Game, @@ -93,18 +98,11 @@ enum EInputType k_EInput_InputBox, }; -abstract_class IHumanDeviceManager +enum EInputDeviceType { -public: - virtual void SetDefaultInputType( EInputType eType ) = 0; - virtual void PushInputType( EInputType eType ) = 0; - virtual void PopInputType() = 0; - virtual void SetButtonPressed( EInputButton eButton ) = 0; - virtual void SetButtonUnpressed( EInputButton eButton ) = 0; + k_EInputDevice_Keyboard, + k_EInputDevice_Mouse, + k_EInputDevice_Gamepad, }; -#define HUMAN_DEVICE_MANAGER_INTERFACE_VERSION "HumanDeviceMgr001" - -extern IHumanDeviceManager *g_pHumanDeviceManager; - #endif diff --git a/public/materialsystem/igamewindow.h b/public/materialsystem/igamewindow.h index c257f8e..099179c 100644 --- a/public/materialsystem/igamewindow.h +++ b/public/materialsystem/igamewindow.h @@ -3,6 +3,10 @@ #include "tier0/platform.h" #include "tier2/iappsystem.h" +#include "materialsystem/humandevice_enums.h" + +typedef void ( *KeyCallbackFn )( EInputDeviceType eDevice, EInputButton eButton, bool bIsPressed ); +typedef void ( *AxisCallbackFn )( EInputDeviceType eDevice, EInputAxis eAxis, float fd ); class IImage; abstract_class IGameWindow: public IAppSystem @@ -17,6 +21,9 @@ public: virtual void SetOutputImage( IImage *pImage ) = 0; virtual IImage *GetOutputImage() = 0; + virtual void SetKeyCallback( KeyCallbackFn fn ) = 0; + virtual void SetAxisCallback( AxisCallbackFn fn ) = 0; + virtual void *CreateVulkanSurface( void *pInstance ) = 0; virtual void DestroyVulkanSurface( void *pInstance ) = 0; };