Initial Commit

This commit is contained in:
2026-02-12 11:46:06 +03:00
commit b044c8d1a5
3973 changed files with 1599881 additions and 0 deletions

172
src/game/game.cpp Normal file
View File

@@ -0,0 +1,172 @@
#include "ifilesystem.h"
#include "core.h"
#include "log.h"
#include "game.h"
#include "game_lua_help.h"
#include "ientity.h"
#include "entitymanager.h"
#include "world.h"
#include <pugixml.hpp>
static Game s_game;
Game* g_game = &s_game;
void consoleMsg(const char* msg)
{
Logger::Msg("%s", msg);
}
void engineError(const char* msg)
{
Core::Error("%s", msg);
}
void engineWarning(const char* msg)
{
Core::Warning("%s", msg);
}
LuaPlus::LuaObject engineCreateEntity(const char* classname)
{
LuaPlus::LuaObject entityObject;
entityObject.AssignNewTable(GetLuaState());
//GameObject* entity = (GameObject*)g_entityManager->createEntity(classname);
//entity->init_from_lua(entityObject);
//entity->init();
return entityObject;
}
void engineAddEntityToWorld(LuaPlus::LuaObject& object)
{
LuaPlus::LuaObject cppclass = object["__cpp_entity"];
// todo
}
void registerEngine()
{
using namespace LuaPlus;
// register engine functions
LuaObject engineTable = GetLuaState().GetGlobals().CreateTable("engine");
engineTable.RegisterDirect("error", &engineError);
engineTable.RegisterDirect("warning", &engineWarning);
engineTable.RegisterDirect("create_entity", &engineCreateEntity);
engineTable.RegisterDirect("add_entity_to_world", &engineAddEntityToWorld);
LuaObject consoleTable = GetLuaState().GetGlobals().CreateTable("console");
consoleTable.RegisterDirect("print", &consoleMsg);
}
void registerClasses()
{
using namespace LuaPlus;
// base thing
GetLuaState().GetGlobals().RegisterDirect("load_script", &luaLoadScript);
registerEngine();
}
void initializeEntityPrototypesFromLua()
{
using namespace LuaPlus;
LuaObject entitiesTable = GetLuaState().GetGlobal("g_entity_table");
for (LuaTableIterator it(entitiesTable); it; it.Next()) {
LuaObject entityTable = it.GetValue();
assert(entityTable.IsTable());
for (LuaTableIterator it2(entityTable); it2; it2.Next()) {
Logger::Msg("%s", it2.GetValue().ToString());
}
}
}
void initializeEntitiesTable()
{
using namespace LuaPlus;
LuaObject entitiesTable = GetLuaState().GetGlobal("g_entity_table");
if (entitiesTable.IsNil()) {
Core::Error("initializeEntitiesTable: failed initialize the entity table! g_entity_table is nil");
}
if (!entitiesTable.IsTable()) {
Core::Error("initializeEntitiesTable: failed initialize the entity table! g_entity_table is not a table");
}
initializeEntityPrototypesFromLua();
}
void initializeLua()
{
using namespace LuaPlus;
// register base classes
registerClasses();
// load game base script
luaLoadScript("game_init.lua");
}
void Game::Init()
{
initializeLua();
initializeEntitiesTable();
}
void Game::InitForNewMap(const char* mapname)
{
LoadLevelXML(mapname);
}
void Game::LoadLevelXML(const char* mapname)
{
// Load XML file of the level
char buffer[kMaxPathLength];
snprintf(buffer, kMaxPathLength, "data/levels/%s/%s.xml", mapname, mapname);
FileHandle_t file = GetFileSystem()->OpenFile(buffer, "rb");
SDL_assert_always(file != kInvalidFileHandleValue);
size_t length = GetFileSystem()->GetFileLength(file);
char* filedata = new char[length + 1];
GetFileSystem()->ReadFile(file, filedata, length);
filedata[length] = '\0';
GetFileSystem()->CloseFile(file);
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_buffer(filedata, length);
delete[] filedata;
if (!result) {
Core::Error("Game::LoadLevelXML: Error while reading level description file '%s'\nError: %s:%i",
buffer, result.description(), result.offset);
}
for (pugi::xml_node entity : doc.child("Entities").children("Entity")) {
pugi::xml_attribute entityname = entity.attribute("name");
pugi::xml_attribute classname = entity.attribute("classname");
if (classname.empty()) {
if (!entityname.empty()) {
Core::Error(" Game::LoadLevelXML: Classname is not specified for entity '%s'", entityname.as_string());
}
else {
Core::Error(" Game::LoadLevelXML: Classname is not specified for entity");
}
}
IEntityBase* entity = g_entityManager->CreateEntity(classname.as_string());
g_world->AddEntity(entity);
}
}
void Game::Shutdown()
{
}

18
src/game/game.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef GAME_H
#define GAME_H
class Game
{
public:
void Init();
void InitForNewMap(const char* mapname);
void LoadLevelXML(const char* mapname);
void Shutdown();
};
extern Game* g_game;
#endif

View File

@@ -0,0 +1,91 @@
#include <string>
#include "core.h"
#include "game_lua_help.h"
class LuaStateWrapper
{
public:
LuaStateWrapper()
{
m_luaState = LuaPlus::LuaState::Create();
m_luaState->OpenLibs();
}
~LuaStateWrapper()
{
LuaPlus::LuaState::Destroy(m_luaState);
}
LuaPlus::LuaState& operator()()
{
return *m_luaState;
}
private:
LuaPlus::LuaState* m_luaState;
};
LuaPlus::LuaState& GetLuaState()
{
static LuaStateWrapper instance;
return instance();
}
void luaError(int errorcode)
{
LuaPlus::LuaState* state = &GetLuaState();
LuaPlus::LuaStackObject stackObj(state, -1);
const char* errorStr = stackObj.GetString();
if (errorStr) {
GetLuaState().SetTop(0);
}
else {
errorStr = "Unknown lua error";
}
Core::Error(errorStr);
}
void luaError(int errorcode, const char* filename)
{
LuaPlus::LuaState* state = &GetLuaState();
LuaPlus::LuaStackObject stackObj(state, -1);
std::string errorStr = stackObj.GetString();
if (!errorStr.empty()) {
GetLuaState().SetTop(0);
}
else {
errorStr = "Unknown lua error";
}
Core::Error(errorStr.c_str());
}
void luaLoadScript(const char* filename)
{
char buffer[260];
snprintf(buffer, sizeof(buffer), "data/scripts/%s", filename);
int result = 0;
result = GetLuaState().DoFile(buffer);
if (result != 0) {
luaError(result, buffer);
}
}
void luaDoString(const char* str)
{
int result = 0;
result = GetLuaState().DoString(str);
if (result != 0) {
luaError(result);
}
}
glm::mat4 getMatrixFromLua(LuaPlus::LuaObject& matrix)
{
glm::mat4 return_matrix = glm::identity<glm::mat4>();
return return_matrix;
}

17
src/game/game_lua_help.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef GAME_LUA_HELP_H
#define GAME_LUA_HELP_H
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <LuaPlus.h>
LuaPlus::LuaState& GetLuaState();
void luaLoadScript(const char* filename);
void luaDoString(const char* str);
glm::mat4 getMatrixFromLua(LuaPlus::LuaObject& matrix);
#endif

122
src/game/game_object.cpp Normal file
View File

@@ -0,0 +1,122 @@
#include "inputmanager.h"
#include "game_object.h"
#include <SDL3/SDL.h>
REGISTER_ENTITY(Entity);
Entity::Entity() :
m_model(nullptr)
{
}
Entity::~Entity()
{
}
void Entity::Render()
{
if (m_model)
m_model->Draw(GetWorldTransform());
}
void Entity::LoadModel(const char* filename)
{
m_model = g_pModelSystem->LoadModel(filename);
}
REGISTER_ENTITY(TempPlayer);
TempPlayer::TempPlayer()
{
}
TempPlayer::~TempPlayer()
{
}
void TempPlayer::Update(float dt)
{
g_cameraManager.SetActiveCamera(&m_camera);
UpdateCameraLook();
UpdateCameraMovement(dt);
}
void TempPlayer::UpdateCameraMovement(float dt)
{
// calculate player movement
float speed = 19.0f * dt;
uint32_t movementDir = GenMovementDir();
if (movementDir & EMovementDir_Forward)
m_position += speed * m_camera.GetFront();
if (movementDir & EMovementDir_Backward)
m_position -= speed * m_camera.GetFront();
if (movementDir & EMovementDir_Left)
m_position -= glm::normalize(glm::cross(m_camera.GetFront(), m_camera.GetUp())) * speed;
if (movementDir & EMovementDir_Right)
m_position += glm::normalize(glm::cross(m_camera.GetFront(), m_camera.GetUp())) * speed;
// set position back to camera for calculation view matrix
m_camera.SetPosition(m_position);
}
void TempPlayer::UpdateCameraLook()
{
g_inputManager.SetRelativeMouseMode(true);
glm::ivec2 mousePos = g_inputManager.GetMousePos();
// calculate yaw and pitch
static float yaw = 0.0f, pitch = 0.0f;
int deltaX = mousePos.x;
int deltaY = mousePos.y;
float sensitivity = 0.15f;
yaw += deltaX * sensitivity;
pitch -= deltaY * sensitivity;
if (pitch > 89.0f) pitch = 89.0f;
if (pitch < -89.0f) pitch = -89.0f;
m_camera.SetYawPitch(yaw, pitch);
}
uint32_t TempPlayer::GenMovementDir()
{
uint32_t movementDir = EMovementDir_None;
if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_W)) {
movementDir |= EMovementDir_Forward;
}
if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_S)) {
movementDir |= EMovementDir_Backward;
}
if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_A)) {
movementDir |= EMovementDir_Left;
}
if (g_inputManager.GetKeyboard().IsKeyDown(SDLK_D)) {
movementDir |= EMovementDir_Right;
}
return movementDir;
}
Weapon::Weapon()
{
}
Weapon::~Weapon()
{
}
void Weapon::Update(float dt)
{
}

60
src/game/game_object.h Normal file
View File

@@ -0,0 +1,60 @@
#ifndef GAME_OBJECT_H
#define GAME_OBJECT_H
#include "engine/ientity.h"
#include "engine/camera.h"
#include "render/modelsystem.h"
enum EMovmentDir
{
EMovementDir_None = 1 << 0,
EMovementDir_Forward = 1 << 1,
EMovementDir_Backward = 1 << 2,
EMovementDir_Left = 1 << 3,
EMovementDir_Right = 1 << 4
};
class Entity : public IEntityBase
{
public:
Entity();
virtual ~Entity();
virtual void Render();
virtual void LoadModel(const char* filename);
protected:
Model* m_model;
};
class TempPlayer : public Entity
{
public:
TempPlayer();
~TempPlayer();
virtual void Update(float dt);
void UpdateCameraMovement(float dt);
void UpdateCameraLook();
private:
uint32_t GenMovementDir();
private:
Camera m_camera;
};
class Weapon : public Entity
{
public:
Weapon();
~Weapon();
virtual void Update(float dt);
};
#endif // !GAME_OBJECT_H