Tutorial_BaseSystem.cpp

Tutorial_BaseSystem.cpp
/*
* Copyright 2015 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 <common/basesystemenv.h>
#include "LabEngine/base/kaimlogimplementation.h"
//RUN_THIS_FILE
namespace
{
#define TEST_ENV_CLASS TestEnv
TEST_ENV {}
TEST // Basic Initialization of Kaim::BaseSystem
{
KY_UNUSED(env);
// The base system needs to be initialized before any Gameware Navigation objects are created, or allocations are made.
// To set up the system, use Kaim::BaseSystem::Config.
// Step 1
// Use the configuration to pass the Gameware key provided by Autodesk.
// It is recommended the key be embedded in your code rather than linked to as a file.
// NOTE: to run tools and examples without having to rebuild the exceutable,
// you use the following code snippet which links to a file containing the key:
// config.m_gamewareKeyConfig.m_m_gamewareKeyFileName = "/absolutePathTo/navigation.gamewarekey";
// But you should copy the key directly in your code:
config.m_gamewareKeyConfig.m_gamewareKey = "1GAMEWARE1KEY1PROVIDED1BY1AUTODESK1";
// Step 2
// Set up the logging system (optional).
// A custom log can be passed to receive Gameware Navigation messages in your logging system.
LabEngine::KaimLogImplementation customLog;
config.m_log = &customLog;
// OR use Kaim::DefaultLog by specifying KY_NULL 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 = KY_NULL;
config.m_defaultLogToMode = (Kaim::DefaultLog::LogToMode) (Kaim::DefaultLog::LogToStdout | Kaim::DefaultLog::LogToIde | Kaim::DefaultLog::LogToFile);
LabEngine::KaimFileOpener customFileOpener;
std::string absolutePathToLogFolder = env.OutputDir();
Kaim::DefaultLog::Instance().SetupLogFile(absolutePathToLogFolder.c_str(), &customFileOpener);
// Step 3
// Set up a custom performance marker interface to track Gameware Navigation performances (optional).
PerfMarkerInterface customPerformanceMarkerInterface;
config.m_perfMarkerInterface = &customPerformanceMarkerInterface;
// 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 Gameware Navigation objects must not occur.
}
TEST // Alternative initialization
{
KY_UNUSED(env);
// 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::KaimLogImplementation customLog;
config.m_log = &customLog;
Kaim::DefaultFileOpener defaultFileOpener;
std::string absolutePathToGamewareKey = env.RootDir() + "navigation.gamewarekey";
config.m_gamewareKeyConfig.m_gamewareKeyFileName = absolutePathToGamewareKey.c_str();
{
// The base system is destroyed here: allocation, creation, deallocation, or destruction with Gameware Navigation objects must not occur.
return;
}
// ... run the game ...
// The base system is destroyed here: allocation, creation, deallocation, or destruction with Gameware 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::StraightSysAllocMalloc::Alloc(size, align); }
virtual void Free(void* ptr, Kaim::UPInt size, Kaim::UPInt align)
{ return Kaim::StraightSysAllocMalloc::Free(ptr, size, align); }
virtual void* Realloc(void* oldPtr, Kaim::UPInt oldSize, Kaim::UPInt newSize, Kaim::UPInt align)
{ return Kaim::StraightSysAllocMalloc::Realloc(oldPtr, oldSize, newSize, align); }
};
TEST // Set up a custom allocator.
{
KY_UNUSED(env);
// Create and initialize a custom allocator.
TutorialSysAlloc tutorialSysAlloc;
// Set up the BaseSystem config.
LabEngine::KaimLogImplementation customLog;
config.m_log = &customLog;
std::string absolutePathToGamewareKey = env.RootDir() + "navigation.gamewarekey";
config.m_gamewareKeyConfig.m_gamewareKeyFileName = absolutePathToGamewareKey.c_str();
// 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;
}
}
}