networking i guess

This commit is contained in:
2026-02-28 21:07:44 +02:00
parent e83f7cd448
commit 03c560c2b7
68 changed files with 1348 additions and 121 deletions

View File

@@ -25,6 +25,7 @@ public:
FunnyModel_t *LoadModelFromParams( const char *szMesh, const char *szMaterial );
uint32_t LoadShader( const char *szName );
FunnyShader_t *GetShaderByIndex( uint32_t uIndex );
void UnrefShader( uint32_t uIndex );
FunnyModel_t *m_models[MAX_MODEL_COUNT] = {};
@@ -46,6 +47,10 @@ FunnyMaterial_t *CAssetManager::GetMaterialByIndex( uint32_t uIndex )
{
return m_materials[uIndex];
}
FunnyShader_t *CAssetManager::GetShaderByIndex( uint32_t uIndex )
{
return m_shaders[uIndex];
}
uint32_t CAssetManager::LoadModel( const char *szName )
@@ -116,7 +121,7 @@ uint32_t CAssetManager::LoadMaterial( const char *szName )
{
uint32_t u = 0;
uint32_t uFoundIndex = 1;
for ( auto &m: m_models)
for ( auto &m: m_materials)
{
if (m == NULL)
{
@@ -125,17 +130,51 @@ uint32_t CAssetManager::LoadMaterial( const char *szName )
}
if (m->m_szName == szName)
{
m_modelUsages[uFoundIndex]++;
m_materialsUsages[uFoundIndex]++;
return u;
}
u++;
}
for ( auto &m: m_models)
for ( auto &m: m_materials)
{
if (m == NULL)
break;
uFoundIndex++;
}
IFileHandle *pHandle = filesystem->Open(szName, FILEMODE_READ);
CUtlString szProperties = filesystem->ReadString(pHandle);
IJSONValue *pRoot = JSONManager()->ReadString(szProperties);
IJSONObject *pMainObject;
switch (pRoot->GetType())
{
case JSON_PARAMETER_OBJECT:
{
pMainObject = pRoot->GetObject();
if (!pMainObject)
{
V_printf("Failed to load properties\n");
return 0;
}
IJSONValue *pShaderValue = pMainObject->GetValue("shader");
CUtlString szShader = pShaderValue->GetStringValue();
CBaseMaterial *pMaterial = CreateMaterial(szShader);
uint32_t uShaderId = LoadShader(pMaterial->GetShaderPath());
FunnyShader_t *pShader = GetShaderByIndex(uShaderId);
m_materials[uFoundIndex] = new FunnyMaterial_t;
m_materials[uFoundIndex]->m_pShaders = pShader->m_pShader;
m_materials[uFoundIndex]->m_pMaterial = g_pRenderContext->CreateMaterial(pShader->m_pShader);
m_materials[uFoundIndex]->m_pLayout = pMaterial;
m_materialsUsages[uFoundIndex]++;
return uFoundIndex;
}
break;
default:
return 0;
}
return uFoundIndex;
}
void CAssetManager::UnrefMaterial( uint32_t uIndex )
@@ -163,6 +202,8 @@ FunnyModel_t *CAssetManager::LoadModelFromParams( const char *szMesh, const char
pModel->m_szName = szMesh;
pModel->m_pMesh = g_pWorldRenderer->CreateMesh(szMesh);
pModel->m_pMesh->SetVertices(pVertexBuffer);
uint32_t uMaterial = LoadMaterial(szMaterial);
pModel->m_pFunnyMaterial = GetMaterialByIndex(uMaterial);
return pModel;
}
@@ -200,6 +241,7 @@ uint32_t CAssetManager::LoadShader( const char *szName )
FunnyShader_t *pFunnyShader = new FunnyShader_t;
pFunnyShader->m_szName = szName;
pFunnyShader->m_pShader = pShader;
m_shaders[uFoundIndex] = pFunnyShader;
return uFoundIndex;
}

View File

@@ -7,6 +7,7 @@
#include "baseentity.h"
#include "datamap.h"
#include "tier0/lib.h"
#include "cglm/cglm.h"
C_BaseEntity::~C_BaseEntity()
{
@@ -19,11 +20,20 @@ void C_BaseEntity::Precache()
void C_BaseEntity::Spawn()
{
Precache();
SetAbsOrigin({0, 0, 0});
SetAbsAngles(0, 0, 0);
SetScale(1);
}
void C_BaseEntity::SetAbsAngles( float fPitch, float fYaw, float fRoll )
{
versor q;
glm_euler_yzx_quat((vec3){fPitch, fYaw, fRoll}, q);
m_vRotation.x = q[0];
m_vRotation.y = q[1];
m_vRotation.z = q[2];
m_vRotation.w = q[3];
}
void C_BaseEntity::SetAbsOrigin( Vector origin )
@@ -39,19 +49,24 @@ void C_BaseEntity::SetScale( float fScale )
}
QAngle C_BaseEntity::GetAbsAngles( void )
QAngle C_BaseEntity::GetAbsQAngles( void )
{
}
Quat C_BaseEntity::GetAbsAngles( void )
{
return m_vRotation;
}
Vector C_BaseEntity::GetAbsOrigin( void )
{
return m_vPosition;
}
float C_BaseEntity::GetScale( void )
{
return m_vScale.x;
}
void C_BaseEntity::SetThinkImpl( fnThink pfnThink )
@@ -111,3 +126,9 @@ BEGIN_DATADESC_NOBASE(C_BaseEntity)
DEFINE_KEYFIELD(m_vRotation, FIELD_QUATERNION, "angles")
DEFINE_KEYFIELD(m_vScale, FIELD_FLOAT3, "scales")
END_DATADESC()
IMPLEMENT_RECV_DT_NOBASE(C_BaseEntity)
NetPropFloat3(m_vPosition)
END_RECV_DT()
IMPLEMENT_EMPTY_SEND_DT_NOBASE(C_BaseEntity)

View File

@@ -10,6 +10,8 @@
#include "entitysystem.h"
#include "datamap.h"
#include "trig.h"
#include "netmap.h"
#include "tier1/utlstring.h"
#define DECLARE_CLASS_NOBASE( className ) \
typedef className ThisClass;
@@ -18,7 +20,7 @@
typedef className ThisClass;
#define LINK_ENTITY_TO_CLASS( mapClassName, DLLClassName) \
static CEntityFactory<DLLClassName> g_EntityFactory_##mapClassName( #mapClassName );
static CEntityFactory<DLLClassName> g_EntityFactory_##mapClassName( #mapClassName ); \
class C_BaseEntity;
@@ -50,6 +52,7 @@ class C_BaseEntity
public:
DECLARE_CLASS_NOBASE(C_BaseEntity);
DECLARE_DATADESC_NOBASE()
DECLARE_CLIENTCLASS_NOBASE()
typedescription_t *FindDataByName( const char *szName );
typedescription_t *FindDataByMapName( const char *szName );
@@ -62,7 +65,8 @@ public:
virtual void SetAbsOrigin( Vector origin );
virtual void SetScale( float fScale );
virtual QAngle GetAbsAngles( void );
virtual QAngle GetAbsQAngles( void );
virtual Quat GetAbsAngles( void );
virtual Vector GetAbsOrigin( void );
virtual float GetScale( void );
@@ -71,6 +75,7 @@ public:
virtual void SetNextThink( float fThink );
fnThink m_pfnThink = NULL;
CUtlString m_szClassName;
private:
Vector m_vPosition;
Quat m_vRotation;

View File

@@ -0,0 +1,34 @@
#include "basemodelentity.h"
void C_BaseModelEntity::Precache()
{
}
void C_BaseModelEntity::Spawn()
{
BaseClass::Spawn();
SetThink(Think);
}
void C_BaseModelEntity::Think( float fDelta )
{
m_pInstance->SetPosition(GetAbsOrigin());
m_pInstance->SetRotation(GetAbsAngles());
m_pInstance->SetScale({GetScale(),GetScale(),GetScale()});
}
void C_BaseModelEntity::SetModel( const char *szName )
{
if (m_uModelIndex)
{
g_pAssetManager->UnrefModel(m_uModelIndex);
}
m_uModelIndex = g_pAssetManager->LoadModel(szName);
m_pModel = g_pAssetManager->GetModelByIndex(m_uModelIndex);
m_pInstance = g_pWorldRenderer->CreateInstance(m_pModel->m_pMesh);
}
BEGIN_DATADESC(C_BaseModelEntity)
END_DATADESC()

View File

@@ -0,0 +1,27 @@
#ifndef BASE_MODEL_ENTITY_H
#define BASE_MODEL_ENTITY_H
#include "baseentity.h"
#include "worldrender.h"
#include "assetmgr.h"
class C_BaseModelEntity: public C_BaseEntity
{
public:
DECLARE_CLASS(C_BaseModelEntity, C_BaseEntity);
DECLARE_DATADESC();
virtual void Precache() override;
virtual void Spawn() override;
void Think( float fDelta );
void SetModel( const char *szName );
private:
uint32_t m_uModelIndex;
FunnyModel_t *m_pModel;
IMeshInstance *m_pInstance;
};
#endif

View File

@@ -18,6 +18,7 @@ DECLARE_BUILD_STAGE(Client)
"game.cpp",
"baseentity.cpp",
"basemodelentity.cpp",
"entitysystem.cpp",
"worldrender.cpp",

View File

@@ -1,12 +0,0 @@
#ifndef ENGINE_H
#define ENGINE_H
#include "tier2/ifilesystem.h"
#include "materialsystem/imaterialsystem.h"
#include "materialsystem/igamewindow.h"
extern IFileSystem *filesystem;
extern IRenderContext *g_pRenderContext;
extern IGameWindow *g_pMainWindow;
#endif

View File

@@ -81,10 +81,30 @@ C_BaseEntity *CEntitySystem::CreateByClassname( const char *szName )
}
pEntity = pFactory->Create();
pEntity->m_szClassName = szName;
m_pEntities[iSelectedSlot] = pEntity;
m_nEntityCount++;
return pEntity;
}
C_BaseEntity *CEntitySystem::CreateByClassnameWithIndex( const char *szName, uint32_t uIndex )
{
IEntityFactory *pFactory;
C_BaseEntity *pEntity;
// Do not create such shit
if (uIndex >= MAX_EDICTS)
return NULL;
pFactory = GetFactoryByClassname(szName);
if ( !pFactory )
return NULL;
pEntity = pFactory->Create();
pEntity->m_szClassName = szName;
m_pEntities[uIndex] = pEntity;
m_nEntityCount++;
return pEntity;
}
IEntityFactory *CEntitySystem::GetFactoryByClassname( const char *szName )
{
@@ -118,3 +138,8 @@ void CEntitySystem::Think()
(pEntity->*pEntity->m_pfnThink)(0);
}
}
C_BaseEntity **CEntitySystem::GetEntities()
{
return m_pEntities;
};

View File

@@ -7,6 +7,8 @@
#ifndef ENTITIES_H
#define ENTITIES_H
#include "stdint.h"
class IEntityFactory;
class C_BaseEntity;
@@ -19,10 +21,12 @@ public:
virtual void RegisterEntityClass( IEntityFactory *pEntityFactory, const char *szClassName );
virtual C_BaseEntity *CreateByClassname( const char *szName );
virtual C_BaseEntity *CreateByClassnameWithIndex( const char *szName, uint32_t uIndex );
virtual IEntityFactory *GetFactoryByClassname( const char *szName );
virtual void Think();
virtual C_BaseEntity **GetEntities();
private:
C_BaseEntity *m_pEntities[MAX_EDICTS];
int m_nEntityCount;

View File

@@ -7,12 +7,16 @@
#include "game.h"
#include "cglm/mat4.h"
#include "cglm/cglm.h"
#include "inetworkclient.h"
#include "netprotocol.h"
IFileSystem *filesystem;
IRenderContext *g_pRenderContext;
IGameWindow *g_pMainWindow;
static CEngineVars s_vars;
CEngineVars *g_pEngineVars = &s_vars;
EngineConsts_t *g_pEngineConstants;
INetworkBase *g_pServerBridge;
class CFunnyGameBridge: public IEngineBridge
{
@@ -34,18 +38,90 @@ EXPOSE_INTERFACE_FN(EngineBridge, IEngineBridge, ENGINE_BRIDGE_INTERFACE_VERSION
void CFunnyGameBridge::Init()
{
g_pWorldRenderer->Init();
C_BaseEntity *pEntity = EntitySystem()->CreateByClassname("player");
pEntity->Spawn();
g_pServerBridge = g_pEngineConstants->ConnectLocalBridge(0);
PlayerJoined_t join = {
MESSAGE_PLAYER_JOINED,
"LocalPlayer"
};
g_pServerBridge->SendPacket({&join, sizeof(join)});
}
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::Frame( float fDelta )
{
g_pEngineVars->m_fTime += fDelta;
g_pEngineVars->m_fDeltaTime = fDelta;
g_pServerBridge->NetThink();
while ( g_pServerBridge->BHasUpdates() )
{
NetPacket_t packet = g_pServerBridge->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:
pEntity = EntitySystem()->CreateByClassnameWithIndex(
(char*)pPacket->m_entityClass.m_szEntityName, pPacket->m_entityClass.m_uIndex
);
pEntity->Spawn();
g_pServerBridge->RecievePacket();
break;
case MESSAGE_ENTITY_DATA_SYNC:
pEntity = EntitySystem()->GetEntities()[pPacket->m_entityData.m_uIndex];
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;
}
g_pServerBridge->RecievePacket();
break;
default:
continue;
}
}
EntitySystem()->Think();
g_pWorldRenderer->Frame(fDelta);
}
@@ -61,5 +137,6 @@ void CFunnyGameBridge::ConnectInterface( const char *psz, void *pInterface )
CONNECT_INTERFACE(RENDER_CONTEXT_INTERFACE_VERSION, g_pRenderContext);
CONNECT_INTERFACE(FILESYSTEM_INTERFACE_VERSION, filesystem);
CONNECT_INTERFACE("MainWindow", g_pMainWindow);
CONNECT_INTERFACE("EngineConstants", g_pEngineConstants);
}

0
game/client/mapmgr.cpp Normal file
View File

0
game/client/mapmgr.h Normal file
View File

View File

View File

View File

View File

View File

@@ -4,21 +4,29 @@
void C_MOBAPlayer::Precache()
{
uint32_t uIndex = g_pAssetManager->LoadModel("game/core/models/cube.fmdl");
V_printf("%u\n", uIndex);
pModel = g_pAssetManager->GetModelByIndex(uIndex);
SetModel("game/core/models/cube.fmdl");
}
void C_MOBAPlayer::Spawn()
{
Precache();
BaseClass::Spawn();
SetThink(Think);
g_pWorldRenderer->SetCameraPosition({0, 0, -20});
};
void C_MOBAPlayer::Think( float fDelta )
{
BaseClass::Think(fDelta);
};
LINK_ENTITY_TO_CLASS(player, C_MOBAPlayer)
BEGIN_DATADESC(C_MOBAPlayer)
END_DATADESC()
IMPLEMENT_RECV_DT(C_MOBAPlayer)
NetPropFloat(m_fTimer),
END_RECV_DT()
IMPLEMENT_EMPTY_SEND_DT(C_MOBAPlayer)

View File

@@ -1,17 +1,21 @@
#include "baseentity.h"
#include "worldrender.h"
#include "game.h"
#include "assetmgr.h"
#ifndef MILMOBA_PLAYER_H
#define MILMOBA_PLAYER_H
#include "basemodelentity.h"
class C_MOBAPlayer: public C_BaseEntity
class C_MOBAPlayer: public C_BaseModelEntity
{
public:
DECLARE_CLASS(C_MOBAPlayer, C_BaseEntity)
DECLARE_CLASS(C_MOBAPlayer, C_BaseModelEntity);
DECLARE_DATADESC();
DECLARE_CLIENTCLASS()
virtual void Precache ( void ) override;
virtual void Spawn( void ) override;
virtual void Think( float fDelta );
void Think( float fDelta );
FunnyModel_t *pModel;
float m_fTimer;
};
#endif

View File

@@ -134,6 +134,8 @@ private:
ViewBuffer_t *m_pViewBufferData;
ITextureArray *m_pTextures;
IShader *m_pRasterShader;
IMaterial *m_pRasterMaterial;
vec3 m_vPos;
versor m_vRot;
@@ -192,6 +194,10 @@ void CFunnyWorldRenderer::Init()
IMAGE_FORMAT_D32_SFLOAT,
MULTISAMPLE_TYPE_4_SAMPLES
);
m_pRasterShader = g_pRenderContext->CreateShader("game/core/shaders/mesh_raster.shader_c");
ConfigureShader(m_pRasterShader);
m_pRasterShader->Build();
m_pRasterMaterial = g_pRenderContext->CreateMaterial(m_pRasterShader);
g_pMainWindow->SetOutputImage(m_pResolvedOutputImage);
@@ -291,16 +297,16 @@ void CFunnyWorldRenderer::Frame( float fDelta )
}
pDataBuffer->Unmap();
pDataBuffer->Unlock();
mesh->m_pMaterial->VSSetConstantsBuffer(0, m_pViewBuffer);
mesh->m_pMaterial->VSSetConstantsBuffer(1, pDataBuffer);
mesh->m_pMaterial->PSSetTextureArray(1, m_pTextures);
m_pRasterMaterial->VSSetConstantsBuffer(0, m_pViewBuffer);
m_pRasterMaterial->VSSetConstantsBuffer(1, pDataBuffer);
m_pRasterMaterial->PSSetTextureArray(1, m_pTextures);
g_pRenderContext->DestroyBuffer(pDataBuffer);
}
for ( auto mesh: m_pMeshes)
{
if (mesh->m_instances.GetSize()==0)
continue;
m_pRasterCommandList->SetMaterial(mesh->m_pMaterial);
m_pRasterCommandList->SetMaterial(m_pRasterMaterial);
m_pRasterCommandList->SetVertexBuffer(0, mesh->m_pVertexBuffer);
m_pRasterCommandList->DrawPrimitives(mesh->m_pVertexBuffer->GetSize()/32, 0, mesh->m_instances.GetSize(), 0);
}

View File

@@ -16,6 +16,16 @@ public:
virtual void SetMaterial( IMaterial *pMaterial ) = 0;
};
abstract_class IPointLight
{
public:
virtual void SetColor( Vector vColor ) = 0;
virtual void SetRadius( Vector vColor ) = 0;
virtual void SetPosition( Vector vPosition ) = 0;
virtual void SetRotation( Quat vRotation ) = 0;
virtual void SetScale( Vector vScale ) = 0;
};
abstract_class IMeshInstance
{
public: