Tutorial_BaseSystem.cpp

Tutorial_BaseSystem.cpp
/*
* Copyright 2016 Autodesk, Inc. All rights reserved.
* Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
* or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
*/
#include "testfwk/Fwk.h"
#include "common/basesystemenv.h"
#include "labengine/base/kaimlogimplementation.h"
namespace
{
#define KT_TEST_ENV_CLASS NavTest::Env
KT_TEST_ENV {}
KT_TEST // Basic Initialization of Kaim::BaseSystem
{
// The base system needs to be initialized before any Autodesk Navigation objects are created, or allocations are made.
// To set up the system, use Kaim::BaseSystem::Config.
// Step 1
// Set up the logging system (optional).
// A custom log can be passed to receive Autodesk Navigation messages in your logging system.
LabEngine::NavLog customLog;
config.m_log = &customLog;
// OR use Kaim::DefaultLog by specifying nullptr and defining where you want to send the log.
// NOTE: if Kaim::DefaultLog::LogToFile is specified then Kaim::DefaultLog uses Kaim::DefaultFileOpener by default.
// To avoid using Kaim::DefaultFileOpener, explicitly set your custom FileOpener in Kaim::DefaultLog::Instance().
// Ensure a call is made Kaim::DefaultLog::SetupLogFile to indicate where to send the log output.
config.m_log = nullptr;
config.m_defaultLogToMode = (Kaim::DefaultLog::LogToMode)(Kaim::DefaultLog::LogToStdout | Kaim::DefaultLog::LogToIde | Kaim::DefaultLog::LogToFile);
LabEngine::KaimFileOpener customFileOpener;
std::string absolutePathToLogFolder = KT_ENV.OutputDir();
Kaim::DefaultLog::Instance().SetupLogFile(absolutePathToLogFolder.c_str(), &customFileOpener);
// Step 2
// Set up a custom performance marker interface to track Autodesk Navigation performances (optional).
KyGlue::PerfMarkerInterface customPerfMarkerInterface;
config.m_perfMarkerInterface = &customPerfMarkerInterface;
// Instantiating the BaseSystem is a good practice. Doing this allows it to be properly destroyed if you have several exit points in your code.
Kaim::BaseSystem baseSystem(config);
if (baseSystem.IsInitialized() == false)
{
return; // BaseSystem is destroyed implicitly, and Kaim::BaseSystem::Destroy() is called.
}
// At this point you can run your game/level.
return;
// The base system is destroyed here: allocation, creation, deallocation, or destruction with Autodesk Navigation objects must not occur.
}
KT_TEST // Alternative initialization
{
// An alternative approach to instantiating the BaseSystem is using a static initialization and destruction.
// This approach requires an explicit call of BaseSystem::Destroy() before exiting
LabEngine::NavLog customLog;
config.m_log = &customLog;
{
// The base system is destroyed here: allocation, creation, deallocation, or destruction with Autodesk Navigation objects must not occur.
return;
}
// ... run the game ...
// The base system is destroyed here: allocation, creation, deallocation, or destruction with Autodesk Navigation objects must not occur.
}
// This class defines a custom implementation of Kaim::SysAlloc.
// It is based simply on malloc and free, but you can add your own allocator using this class.
class TutorialSysAlloc : public Kaim::SysAlloc
{
public:
virtual ~TutorialSysAlloc() {}
// Implementation based on malloc and free.
virtual void* Alloc(Kaim::UPInt size, Kaim::UPInt align) { return Kaim::PageAlloc::Alloc(size, align); }
virtual void Free(void* ptr, Kaim::UPInt size, Kaim::UPInt align) { return Kaim::PageAlloc::Free(ptr, size, align); }
virtual void* Realloc(void* oldPtr, Kaim::UPInt oldSize, Kaim::UPInt newSize, Kaim::UPInt align) { return Kaim::PageAlloc::Realloc(oldPtr, oldSize, newSize, align); }
};
KT_TEST // Set up a custom allocator.
{
// Create and initialize a custom allocator.
TutorialSysAlloc tutorialSysAlloc;
// Set up the BaseSystem config.
LabEngine::NavLog customLog;
config.m_log = &customLog;
// Initialize the BaseSystem using your custom allocation system.
// NOTE: it is recommended to use Kaim::GeneratorSysAllocMalloc::InitSystemSingleton as follows:
// config.m_sysAlloc = Kaim::GeneratorSysAllocMalloc::InitSystemSingleton(); // this allocator is optimized for the generation
config.m_sysAlloc = &tutorialSysAlloc;
// Use Kaim::GeneratorBaseSystem instead of Kaim::BaseSystem when doing generation work
// Kaim::GeneratorBaseSystem::Init(config);
{
return;
}
}
}