Skip to content

Commit

Permalink
Logger is a global now
Browse files Browse the repository at this point in the history
  • Loading branch information
captainurist committed Oct 21, 2023
1 parent 6d0d34d commit 6c69f11
Show file tree
Hide file tree
Showing 35 changed files with 78 additions and 152 deletions.
15 changes: 7 additions & 8 deletions src/Application/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void initDataPath(const std::string &dataPath) {
std::filesystem::create_directory(savesPath);
}

EngineIocContainer::ResolveLogger()->info("Using MM7 directory: {}", dataPath);
logger->info("Using MM7 directory: {}", dataPath);
} else {
std::string message = fmt::format(
"Required file {} not found.\n"
Expand All @@ -125,7 +125,7 @@ void initDataPath(const std::string &dataPath) {
!dataPath.empty() ? dataPath : std::filesystem::current_path().string(),
!dataPath.empty() ? "" : " (current directory)"
);
EngineIocContainer::ResolveLogger()->critical("{}", message);
logger->critical("{}", message);
platform->showMessageBox("CRITICAL ERROR: missing resources", message);
throw Exception("Data folder '{}' validation failed", dataPath);
}
Expand All @@ -134,7 +134,6 @@ void initDataPath(const std::string &dataPath) {
Game::Game(PlatformApplication *application, std::shared_ptr<GameConfig> config) {
_application = application;
_config = config;
_log = EngineIocContainer::ResolveLogger();
_decalBuilder = EngineIocContainer::ResolveDecalBuilder();
_vis = EngineIocContainer::ResolveVis();
_menu = GameIocContainer::ResolveGameMenu();
Expand Down Expand Up @@ -167,21 +166,21 @@ Game::~Game() {

int Game::run() {
_render = IRenderFactory().Create(_config);
::render = _render;
::render = _render.get();

if (!_render) {
_log->error("Render creation failed");
logger->error("Render creation failed");
return -1;
}

if (!_render->Initialize()) {
_log->error("Render failed to initialize");
logger->error("Render failed to initialize");
return -1;
}

_nuklear = Nuklear::Initialize();
if (!_nuklear) {
_log->error("Nuklear failed to initialize");
logger->error("Nuklear failed to initialize");
}
::nuklear = _nuklear;
if (_nuklear) {
Expand All @@ -204,7 +203,7 @@ int Game::run() {
::engine = _engine.get();

if (!_engine) {
_log->error("Engine creation failed");
logger->error("Engine creation failed");
return -1;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Application/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class Game {
std::unique_ptr<NuklearEventHandler> _nuklearHandler;
std::unique_ptr<Engine> _engine;
std::shared_ptr<IRender> _render;
std::shared_ptr<Mouse> _mouse = nullptr;
Logger *_log = nullptr;
std::shared_ptr<Mouse> _mouse;
DecalBuilder *_decalBuilder = nullptr;
Vis *_vis = nullptr;
Menu *_menu = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/Application/GamePathResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static std::string _resolvePath(
}

if (!envPath.empty()) {
EngineIocContainer::ResolveLogger()->info("Path override provided: {}={}", envVarOverride, envPathStr);
logger->info("Path override provided: {}={}", envVarOverride, envPathStr);
return envPath;
}

Expand Down
7 changes: 3 additions & 4 deletions src/Application/GameStarter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ GameStarter::GameStarter(GameStarterOptions options): _options(std::move(options
};
if (_options.logLevel)
setLogLevel(*_options.logLevel);
EngineIocContainer::ResolveLogger()->setBaseLogger(_logger.get());
_globalLogger = std::make_unique<Logger>(_logger.get());
::logger = _globalLogger.get();

// Create platform & init data paths.
if (_options.headless) {
Expand Down Expand Up @@ -71,9 +72,7 @@ GameStarter::GameStarter(GameStarterOptions options): _options(std::move(options
_game = std::make_unique<Game>(_application.get(), _config);
}

GameStarter::~GameStarter() {
EngineIocContainer::ResolveLogger()->setBaseLogger(nullptr);
}
GameStarter::~GameStarter() = default;

void GameStarter::resolveDefaults(Platform *platform, GameStarterOptions* options) {
if (options->dataPath.empty())
Expand Down
2 changes: 2 additions & 0 deletions src/Application/GameStarter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class Platform;
class PlatformLogger;
class Logger;
class PlatformApplication;
class GameConfig;
class Game;
Expand All @@ -31,6 +32,7 @@ class GameStarter {
private:
GameStarterOptions _options;
std::unique_ptr<PlatformLogger> _logger;
std::unique_ptr<Logger> _globalLogger;
std::unique_ptr<Platform> _platform;
std::unique_ptr<PlatformApplication> _application;
std::shared_ptr<GameConfig> _config;
Expand Down
11 changes: 10 additions & 1 deletion src/Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,6 @@ int Engine::_44ED0A_saturate_face_blv(BLVFace *a2, int *a3, signed int a4) {
//----- (0044E4B7) --------------------------------------------------------
Engine::Engine(std::shared_ptr<GameConfig> config) {
this->config = config;
this->log = EngineIocContainer::ResolveLogger();
this->bloodsplat_container = EngineIocContainer::ResolveBloodsplatContainer();
this->decal_builder = EngineIocContainer::ResolveDecalBuilder();
this->spell_fx_renedrer = EngineIocContainer::ResolveSpellFxRenderer();
Expand Down Expand Up @@ -849,6 +848,16 @@ void Engine::SecondaryInitialization() {
}

void Engine::Initialize() {
_indoor = std::make_unique<IndoorLocation>();
_outdoor = std::make_unique<OutdoorLocation>();
_stationaryLights = std::make_unique<LightsStack_StationaryLight_>();
_mobileLights = std::make_unique<LightsStack_MobileLight_>();

::pIndoor = _indoor.get();
::pOutdoor = _outdoor.get();
::pStationaryLightsStack = _stationaryLights.get();
::pMobileLightsStack = _mobileLights.get();

MM7_Initialize();

pEventTimer->Pause();
Expand Down
11 changes: 8 additions & 3 deletions src/Engine/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ class Vis;
class ParticleEngine;
struct ClippingFunctions;
struct stru10;
class Logger;
class GUIMessageQueue;
class GameResourceManager;
class StatusBar;
struct IndoorLocation;
struct OutdoorLocation;
struct LightsStack_StationaryLight_;
struct LightsStack_MobileLight_;

/* 320 */
enum class GameState {
GAME_STATE_PLAYING = 0,
GAME_FINISHED = 1,
Expand Down Expand Up @@ -135,7 +137,6 @@ class Engine {
int uNumStationaryLights_in_pStationaryLightsStack;
float fSaturation;
stru10 *pStru10Instance;
Logger *log = nullptr;
BloodsplatContainer *bloodsplat_container = nullptr;
DecalBuilder *decal_builder = nullptr;
SpellFxRenderer *spell_fx_renedrer = nullptr;
Expand All @@ -154,6 +155,10 @@ class Engine {
std::unique_ptr<GUIMessageQueue> _messageQueue;
std::unique_ptr<GameResourceManager> _gameResourceManager;
std::unique_ptr<StatusBar> _statusBar;
std::unique_ptr<IndoorLocation> _indoor;
std::unique_ptr<OutdoorLocation> _outdoor;
std::unique_ptr<LightsStack_StationaryLight_> _stationaryLights;
std::unique_ptr<LightsStack_MobileLight_> _mobileLights;
};

extern Engine *engine;
Expand Down
7 changes: 0 additions & 7 deletions src/Engine/EngineIocContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ using Io::Mouse;

Logger *logger = nullptr;

Logger *EngineIocContainer::ResolveLogger() {
if (!logger) {
logger = new Logger();
}
return logger;
}

DecalBuilder *EngineIocContainer::ResolveDecalBuilder() {
if (!decal_builder) {
decal_builder = new DecalBuilder();
Expand Down
2 changes: 0 additions & 2 deletions src/Engine/EngineIocContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class Vis;

class EngineIocContainer {
public:
static Logger *ResolveLogger();
static DecalBuilder *ResolveDecalBuilder();
static BloodsplatContainer *ResolveBloodsplatContainer();
static SpellFxRenderer *ResolveSpellFxRenderer();
Expand All @@ -34,5 +33,4 @@ class EngineIocContainer {
static Vis *vis;
};


extern Logger *logger;
3 changes: 1 addition & 2 deletions src/Engine/Graphics/DecalBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void BloodsplatContainer::AddBloodsplat(const Vec3f &pos, float radius, Color co
}

DecalBuilder::DecalBuilder() {
this->log = EngineIocContainer::ResolveLogger();
this->bloodsplat_container = EngineIocContainer::ResolveBloodsplatContainer();
this->DecalsCount = 0;
}
Expand Down Expand Up @@ -86,7 +85,7 @@ char DecalBuilder::BuildAndApplyDecals(int light_level, LocationFlags locationFl
buildsplat->color,
buildsplat->faceDist,
&static_FacePlane, NumFaceVerts, FaceVerts, ClipFlags))
log->warning("Error: Failed to build decal geometry");
logger->warning("Error: Failed to build decal geometry");
}
}
return 1;
Expand Down
3 changes: 0 additions & 3 deletions src/Engine/Graphics/DecalBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

struct ODMFace;
struct Planef;
class Logger;

enum class DecalFlag : int {
DecalFlagsNone = 0x0,
Expand Down Expand Up @@ -124,7 +123,5 @@ struct DecalBuilder {
float field_30C02C = 0;
float flt_30C030 = 0;
float field_30C034 = 0;

Logger *log;
BloodsplatContainer *bloodsplat_container;
};
6 changes: 3 additions & 3 deletions src/Engine/Graphics/IRender.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include "IRender.h"

IRender *render = nullptr;

IRender::IRender(
std::shared_ptr<GameConfig> config,
DecalBuilder *decal_builder,
SpellFxRenderer *spellfx,
std::shared_ptr<ParticleEngine> particle_engine,
Vis *vis,
Logger *logger
Vis *vis
) {
this->config = config;
this->decal_builder = decal_builder;
this->spell_fx_renderer = spellfx;
this->particle_engine = particle_engine;
this->vis = vis;
this->log = logger;

pActiveZBuffer = 0;
uFogColor = Color();
Expand Down
7 changes: 2 additions & 5 deletions src/Engine/Graphics/IRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ struct DecalBuilder;
class ParticleEngine;
struct SpellFxRenderer;
class Vis;
class Logger;

namespace LOD {
class File;
Expand All @@ -40,8 +39,7 @@ class IRender {
DecalBuilder *decal_builder,
SpellFxRenderer *spellfx,
std::shared_ptr<ParticleEngine> particle_engine,
Vis *vis,
Logger *logger
Vis *vis
);
virtual ~IRender();

Expand Down Expand Up @@ -207,14 +205,13 @@ class IRender {

int drawcalls;

Logger *log = nullptr;
DecalBuilder *decal_builder = nullptr;
SpellFxRenderer *spell_fx_renderer = nullptr;
std::shared_ptr<ParticleEngine> particle_engine = nullptr;
Vis *vis = nullptr;
};

extern std::shared_ptr<IRender> render;
extern IRender *render;

extern int uNumDecorationsDrawnThisFrame;
extern RenderBillboard pBillboardRenderList[500];
Expand Down
9 changes: 3 additions & 6 deletions src/Engine/Graphics/IRenderFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ std::shared_ptr<IRender> IRenderFactory::Create(std::shared_ptr<GameConfig> conf
EngineIocContainer::ResolveDecalBuilder(),
EngineIocContainer::ResolveSpellFxRenderer(),
EngineIocContainer::ResolveParticleEngine(),
EngineIocContainer::ResolveVis(),
EngineIocContainer::ResolveLogger()
EngineIocContainer::ResolveVis()
);

case RENDERER_OPENGL_ES:
Expand All @@ -32,8 +31,7 @@ std::shared_ptr<IRender> IRenderFactory::Create(std::shared_ptr<GameConfig> conf
EngineIocContainer::ResolveDecalBuilder(),
EngineIocContainer::ResolveSpellFxRenderer(),
EngineIocContainer::ResolveParticleEngine(),
EngineIocContainer::ResolveVis(),
EngineIocContainer::ResolveLogger()
EngineIocContainer::ResolveVis()
);

case RENDERER_NULL:
Expand All @@ -43,8 +41,7 @@ std::shared_ptr<IRender> IRenderFactory::Create(std::shared_ptr<GameConfig> conf
EngineIocContainer::ResolveDecalBuilder(),
EngineIocContainer::ResolveSpellFxRenderer(),
EngineIocContainer::ResolveParticleEngine(),
EngineIocContainer::ResolveVis(),
EngineIocContainer::ResolveLogger()
EngineIocContainer::ResolveVis()
);

default:
Expand Down
8 changes: 2 additions & 6 deletions src/Engine/Graphics/ImageLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ static Palette MakePaletteColorKey(const Palette &palette, Color key) {
return result;
}

ImageLoader::ImageLoader() {
this->log = EngineIocContainer::ResolveLogger();
}

bool Paletted_Img_Loader::Load(RgbaImage *rgbaImage, GrayscaleImage *indexedImage, Palette *palette) {
Texture_MM7 *tex = lod->loadTexture(resource_name);
if (tex == nullptr)
Expand Down Expand Up @@ -131,7 +127,7 @@ bool PCX_File_Loader::Load(RgbaImage *rgbaImage, GrayscaleImage *indexedImage, P
bool PCX_LOD_Raw_Loader::Load(RgbaImage *rgbaImage, GrayscaleImage *indexedImage, Palette *palette) {
Blob data = lod->read(resource_name);
if (!data) {
log->warning("Unable to load {}", this->resource_name);
logger->warning("Unable to load {}", this->resource_name);
return false;
}

Expand All @@ -141,7 +137,7 @@ bool PCX_LOD_Raw_Loader::Load(RgbaImage *rgbaImage, GrayscaleImage *indexedImage
bool PCX_LOD_Compressed_Loader::Load(RgbaImage *rgbaImage, GrayscaleImage *indexedImage, Palette *palette) {
Blob pcx_data = blob_func();
if (!pcx_data) {
log->warning("Unable to load {}", resource_name);
logger->warning("Unable to load {}", resource_name);
return false;
}

Expand Down
3 changes: 0 additions & 3 deletions src/Engine/Graphics/ImageLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
class LodSpriteCache;
class LodTextureCache;
class LodReader;
class Logger;

class ImageLoader {
public:
ImageLoader();
virtual ~ImageLoader() = default;
virtual std::string GetResourceName() const { return this->resource_name; }
virtual std::string *GetResourceNamePtr() { return &this->resource_name; }
Expand All @@ -23,7 +21,6 @@ class ImageLoader {

protected:
std::string resource_name;
Logger *log;
};

class Paletted_Img_Loader : public ImageLoader {
Expand Down
7 changes: 2 additions & 5 deletions src/Engine/Graphics/Indoor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@
#include "Utility/Math/FixPoint.h"
#include "Utility/Exception.h"

// TODO(pskelton): make this neater
static DecalBuilder *decal_builder = EngineIocContainer::ResolveDecalBuilder();

IndoorLocation *pIndoor = new IndoorLocation;
IndoorLocation *pIndoor = nullptr;
BLVRenderParams *pBLVRenderParams = new BLVRenderParams;

static constexpr IndexedArray<uint16_t, MAP_FIRST, MAP_LAST> pDoorSoundIDsByLocationID = {
Expand Down Expand Up @@ -790,7 +787,7 @@ void UpdateActors_BLV() {
if (pMonsterStats->pInfos[actor.monsterInfo.uID].bBloodSplatOnDeath) {
if (engine->config->graphics.BloodSplats.value()) {
float splatRadius = actor.radius * engine->config->graphics.BloodSplatsMultiplier.value();
decal_builder->AddBloodsplat(Vec3f(actor.pos.x, actor.pos.y, floorZ + 30), colorTable.Red, splatRadius);
EngineIocContainer::ResolveDecalBuilder()->AddBloodsplat(Vec3f(actor.pos.x, actor.pos.y, floorZ + 30), colorTable.Red, splatRadius);
}
actor.donebloodsplat = true;
}
Expand Down
Loading

0 comments on commit 6c69f11

Please sign in to comment.