LumixEngine/src/engine/engine.cpp

514 lines
11 KiB
C++
Raw Normal View History

2015-08-11 23:55:58 +02:00
#include "lumix.h"
#include "engine.h"
2014-11-26 22:40:37 +01:00
#include "core/blob.h"
2014-06-16 21:18:15 +02:00
#include "core/crc32.h"
#include "core/input_system.h"
#include "core/log.h"
2015-08-17 23:45:26 +02:00
#include "core/path.h"
#include "core/profiler.h"
2014-06-16 21:18:15 +02:00
#include "core/resource_manager.h"
#include "core/timer.h"
#include "core/fs/disk_file_device.h"
#include "core/fs/file_system.h"
#include "core/fs/memory_file_device.h"
#include "core/mtjd/manager.h"
#include "debug/allocator.h"
2014-11-12 21:51:01 +01:00
#include "debug/debug.h"
#include "engine/iplugin.h"
2015-08-18 22:57:22 +02:00
#include "engine/property_descriptor.h"
2015-08-11 23:55:58 +02:00
#include "plugin_manager.h"
2014-10-13 22:50:34 +02:00
#include "universe/hierarchy.h"
#include "universe/universe.h"
2014-06-29 15:20:21 +02:00
#include <cstdio>
2014-06-16 21:18:15 +02:00
namespace Lumix
{
static const uint32_t SERIALIZED_ENGINE_MAGIC = 0x5f4c454e; // == '_LEN'
enum class SerializedEngineVersion : int32_t
{
BASE,
SPARSE_TRANFORMATIONS,
2015-10-17 00:18:47 +02:00
FOG_PARAMS,
LATEST // must be the last one
};
#pragma pack(1)
class SerializedEngineHeader
{
public:
uint32_t m_magic;
SerializedEngineVersion m_version;
uint32_t m_reserved; // for crc
};
#pragma pack()
2015-08-12 22:57:22 +02:00
IScene* UniverseContext::getScene(uint32_t hash) const
{
for (auto* scene : m_scenes)
{
if (crc32(scene->getPlugin().getName()) == hash)
{
return scene;
}
}
return nullptr;
}
class EngineImpl : public Engine
{
public:
2015-08-02 15:36:52 +02:00
EngineImpl(FS::FileSystem* fs, IAllocator& allocator)
: m_allocator(allocator)
, m_resource_manager(m_allocator)
, m_mtjd_manager(m_allocator)
, m_fps(0)
2015-08-12 22:57:22 +02:00
, m_is_game_running(false)
2015-08-18 22:57:22 +02:00
, m_component_properties(m_allocator)
, m_component_types(m_allocator)
2014-06-16 21:18:15 +02:00
{
if (!fs)
{
m_file_system = FS::FileSystem::create(m_allocator);
2014-08-21 01:22:57 +02:00
m_mem_file_device =
m_allocator.newObject<FS::MemoryFileDevice>(m_allocator);
m_disk_file_device =
m_allocator.newObject<FS::DiskFileDevice>(m_allocator);
2014-08-21 01:22:57 +02:00
m_file_system->mount(m_mem_file_device);
m_file_system->mount(m_disk_file_device);
m_file_system->setDefaultDevice("memory:disk");
m_file_system->setSaveGameDevice("memory:disk");
}
else
{
m_file_system = fs;
2015-07-25 19:33:19 +02:00
m_mem_file_device = nullptr;
m_disk_file_device = nullptr;
}
2014-08-21 01:22:57 +02:00
m_resource_manager.create(*m_file_system);
2014-09-29 22:58:36 +02:00
m_timer = Timer::create(m_allocator);
m_fps_timer = Timer::create(m_allocator);
2015-07-25 19:33:19 +02:00
m_fps_frame = 0;
}
2014-06-16 21:18:15 +02:00
2015-08-17 23:45:26 +02:00
bool create()
{
m_plugin_manager = PluginManager::create(*this);
if (!m_plugin_manager)
{
return false;
}
if (!m_input_system.create(m_allocator))
{
return false;
}
2014-06-16 21:18:15 +02:00
return true;
}
2014-06-16 21:18:15 +02:00
virtual ~EngineImpl()
{
2015-08-18 22:57:22 +02:00
for (int j = 0; j < m_component_properties.size(); ++j)
{
Array<IPropertyDescriptor*>& props = m_component_properties.at(j);
for (int i = 0, c = props.size(); i < c; ++i)
{
m_allocator.deleteObject(props[i]);
}
}
Timer::destroy(m_timer);
Timer::destroy(m_fps_timer);
PluginManager::destroy(m_plugin_manager);
m_input_system.destroy();
if (m_disk_file_device)
{
FS::FileSystem::destroy(m_file_system);
m_allocator.deleteObject(m_mem_file_device);
m_allocator.deleteObject(m_disk_file_device);
}
2015-10-03 01:14:38 +02:00
m_resource_manager.destroy();
}
2014-06-16 21:18:15 +02:00
virtual IAllocator& getAllocator() override { return m_allocator; }
2014-11-07 22:18:47 +01:00
2015-08-18 22:57:22 +02:00
virtual const char* getComponentTypeName(int index) override
{
return m_component_types[index].m_name.c_str();
}
virtual const char* getComponentTypeID(int index) override
{
return m_component_types[index].m_id.c_str();
}
virtual int getComponentTypesCount() const override
{
return m_component_types.size();
}
Array<IPropertyDescriptor*>& getPropertyDescriptors(uint32_t type)
{
int props_index = m_component_properties.find(type);
if (props_index < 0)
{
m_component_properties.insert(
type, Array<IPropertyDescriptor*>(m_allocator));
props_index = m_component_properties.find(type);
}
return m_component_properties.at(props_index);
}
virtual const IPropertyDescriptor&
getPropertyDescriptor(uint32_t type, uint32_t name_hash) override
{
Array<IPropertyDescriptor*>& props = getPropertyDescriptors(type);
for (int i = 0; i < props.size(); ++i)
{
if (props[i]->getNameHash() == name_hash)
{
return *props[i];
}
}
ASSERT(false);
return *props[0];
}
virtual IPropertyDescriptor* getProperty(const char* component_type,
const char* property_name) override
{
auto& props = getPropertyDescriptors(crc32(component_type));
auto name_hash = crc32(property_name);
for (int i = 0; i < props.size(); ++i)
{
if (props[i]->getNameHash() == name_hash)
{
return props[i];
}
}
return nullptr;
}
virtual void registerComponentType(const char* id, const char* name) override
{
ComponentType& type = m_component_types.emplace(m_allocator);
type.m_name = name;
type.m_id = id;
}
virtual void registerProperty(const char* component_type,
IPropertyDescriptor* descriptor) override
{
ASSERT(descriptor);
getPropertyDescriptors(crc32(component_type)).push(descriptor);
}
2015-08-12 22:57:22 +02:00
virtual UniverseContext& createUniverse() override
{
2015-08-12 22:57:22 +02:00
UniverseContext* context =
m_allocator.newObject<UniverseContext>(m_allocator);
context->m_universe = m_allocator.newObject<Universe>(m_allocator);
context->m_hierarchy =
Hierarchy::create(*context->m_universe, m_allocator);
const Array<IPlugin*>& plugins = m_plugin_manager->getPlugins();
2015-07-24 22:38:11 +02:00
for (auto* plugin : plugins)
{
2015-08-12 22:57:22 +02:00
IScene* scene = plugin->createScene(*context);
if (scene)
2014-08-21 01:22:57 +02:00
{
2015-08-12 22:57:22 +02:00
context->m_scenes.push(scene);
2014-08-21 01:22:57 +02:00
}
}
2015-08-12 22:57:22 +02:00
return *context;
}
2014-06-16 21:18:15 +02:00
virtual MTJD::Manager& getMTJDManager() override { return m_mtjd_manager; }
2014-09-29 22:58:36 +02:00
2015-08-12 22:57:22 +02:00
virtual void destroyUniverse(UniverseContext& context) override
{
2015-08-12 22:57:22 +02:00
for (int i = context.m_scenes.size() - 1; i >= 0; --i)
{
2015-08-12 22:57:22 +02:00
context.m_scenes[i]->getPlugin().destroyScene(context.m_scenes[i]);
}
2015-08-12 22:57:22 +02:00
Hierarchy::destroy(context.m_hierarchy);
m_allocator.deleteObject(context.m_universe);
m_allocator.deleteObject(&context);
}
2014-06-16 21:18:15 +02:00
virtual PluginManager& getPluginManager() override
{
return *m_plugin_manager;
}
2014-06-16 21:18:15 +02:00
virtual FS::FileSystem& getFileSystem() override { return *m_file_system; }
2015-08-12 22:57:22 +02:00
virtual void startGame(UniverseContext& context) override
2015-08-12 21:54:47 +02:00
{
2015-08-12 22:57:22 +02:00
ASSERT(!m_is_game_running);
m_is_game_running = true;
for (auto* scene : context.m_scenes)
2015-08-12 21:54:47 +02:00
{
scene->startGame();
}
}
2015-08-12 22:57:22 +02:00
virtual void stopGame(UniverseContext& context) override
2015-08-12 21:54:47 +02:00
{
2015-08-12 22:57:22 +02:00
ASSERT(m_is_game_running);
m_is_game_running = false;
2015-08-12 22:57:22 +02:00
for (auto* scene : context.m_scenes)
2015-08-12 21:54:47 +02:00
{
scene->stopGame();
}
}
2015-08-14 22:12:51 +02:00
virtual void update(UniverseContext& context) override
{
PROFILE_FUNCTION();
float dt;
2015-08-14 22:12:51 +02:00
++m_fps_frame;
2015-09-21 11:43:31 +02:00
if (m_fps_timer->getTimeSinceTick() > 0.5f)
{
2015-09-21 11:43:31 +02:00
m_fps = m_fps_frame / m_fps_timer->tick();
m_fps_frame = 0;
}
2015-08-14 22:12:51 +02:00
dt = m_timer->tick();
m_last_time_delta = dt;
2015-08-18 22:57:22 +02:00
for (int i = 0; i < context.m_scenes.size(); ++i)
{
2015-08-18 22:57:22 +02:00
context.m_scenes[i]->update(dt);
}
2015-08-18 22:57:22 +02:00
m_plugin_manager->update(dt);
m_input_system.update(dt);
getFileSystem().updateAsyncTransactions();
}
2014-06-16 21:18:15 +02:00
virtual InputSystem& getInputSystem() override { return m_input_system; }
2014-06-16 21:18:15 +02:00
virtual ResourceManager& getResourceManager() override
{
return m_resource_manager;
}
2014-06-16 21:18:15 +02:00
virtual float getFPS() const override { return m_fps; }
2014-06-16 21:18:15 +02:00
void serializePluginList(OutputBlob& serializer)
{
serializer.write((int32_t)m_plugin_manager->getPlugins().size());
for (auto* plugin : m_plugin_manager->getPlugins())
{
serializer.writeString(plugin->getName());
}
}
bool hasSerializedPlugins(InputBlob& serializer)
{
int32_t count;
serializer.read(count);
for (int i = 0; i < count; ++i)
{
char tmp[32];
serializer.readString(tmp, sizeof(tmp));
if (!m_plugin_manager->getPlugin(tmp))
{
g_log_error.log("engine") << "Missing plugin " << tmp;
return false;
}
}
return true;
}
2015-08-12 22:57:22 +02:00
virtual uint32_t serialize(UniverseContext& ctx,
OutputBlob& serializer) override
{
SerializedEngineHeader header;
header.m_magic = SERIALIZED_ENGINE_MAGIC; // == '_LEN'
header.m_version = SerializedEngineVersion::LATEST;
header.m_reserved = 0;
serializer.write(header);
serializePluginList(serializer);
g_path_manager.serialize(serializer);
int pos = serializer.getSize();
2015-08-12 22:57:22 +02:00
ctx.m_universe->serialize(serializer);
ctx.m_hierarchy->serialize(serializer);
m_plugin_manager->serialize(serializer);
2015-08-12 22:57:22 +02:00
serializer.write((int32_t)ctx.m_scenes.size());
for (int i = 0; i < ctx.m_scenes.size(); ++i)
{
2015-08-12 22:57:22 +02:00
serializer.writeString(ctx.m_scenes[i]->getPlugin().getName());
ctx.m_scenes[i]->serialize(serializer);
}
uint32_t crc = crc32((const uint8_t*)serializer.getData() + pos,
serializer.getSize() - pos);
return crc;
}
2014-06-16 21:18:15 +02:00
2015-08-12 22:57:22 +02:00
virtual bool deserialize(UniverseContext& ctx,
InputBlob& serializer) override
{
SerializedEngineHeader header;
serializer.read(header);
if (header.m_magic != SERIALIZED_ENGINE_MAGIC)
{
g_log_error.log("engine") << "Wrong or corrupted file";
return false;
}
if (header.m_version != SerializedEngineVersion::LATEST)
{
g_log_error.log("engine") << "Unsupported version";
return false;
}
if (!hasSerializedPlugins(serializer))
{
return false;
}
g_path_manager.deserialize(serializer);
2015-08-12 22:57:22 +02:00
ctx.m_universe->deserialize(serializer);
ctx.m_hierarchy->deserialize(serializer);
m_plugin_manager->deserialize(serializer);
int32_t scene_count;
serializer.read(scene_count);
for (int i = 0; i < scene_count; ++i)
{
char tmp[32];
serializer.readString(tmp, sizeof(tmp));
2015-08-25 17:25:58 +02:00
IScene* scene = ctx.getScene(crc32(tmp));
scene->deserialize(serializer);
}
2015-08-14 22:12:51 +02:00
g_path_manager.clear();
return true;
}
2014-08-22 22:02:53 +02:00
virtual float getLastTimeDelta() override { return m_last_time_delta; }
2014-11-07 22:18:47 +01:00
2015-08-18 22:57:22 +02:00
private:
struct ComponentType
{
ComponentType(IAllocator& allocator)
: m_name(allocator)
, m_id(allocator)
{
}
string m_name;
string m_id;
};
2014-08-21 01:22:57 +02:00
private:
Debug::Allocator m_allocator;
2014-08-21 01:22:57 +02:00
FS::FileSystem* m_file_system;
FS::MemoryFileDevice* m_mem_file_device;
FS::DiskFileDevice* m_disk_file_device;
2014-09-29 22:58:36 +02:00
ResourceManager m_resource_manager;
2015-08-18 22:57:22 +02:00
MTJD::Manager m_mtjd_manager;
2014-06-16 21:18:15 +02:00
2015-08-18 22:57:22 +02:00
AssociativeArray<uint32_t, Array<IPropertyDescriptor*>>
m_component_properties;
Array<ComponentType> m_component_types;
PluginManager* m_plugin_manager;
InputSystem m_input_system;
Timer* m_timer;
Timer* m_fps_timer;
int m_fps_frame;
float m_fps;
float m_last_time_delta;
2015-08-12 22:57:22 +02:00
bool m_is_game_running;
2014-06-16 21:18:15 +02:00
private:
void operator=(const EngineImpl&);
EngineImpl(const EngineImpl&);
};
2014-06-16 21:18:15 +02:00
2014-08-21 20:05:18 +02:00
static void showLogInVS(const char*, const char* message)
{
Debug::debugOutput(message);
Debug::debugOutput("\n");
}
2014-06-16 21:18:15 +02:00
static FILE* g_error_file = nullptr;
static void logErrorToFile(const char*, const char* message)
{
fputs(message, g_error_file);
fflush(g_error_file);
}
2015-08-17 23:45:26 +02:00
Engine* Engine::create(FS::FileSystem* fs, IAllocator& allocator)
{
2015-08-02 15:36:52 +02:00
installUnhandledExceptionHandler();
2014-06-16 21:18:15 +02:00
g_error_file = fopen("error.log", "wb");
g_log_error.getCallback().bind<logErrorToFile>();
g_log_info.getCallback().bind<showLogInVS>();
g_log_warning.getCallback().bind<showLogInVS>();
g_log_error.getCallback().bind<showLogInVS>();
2014-06-16 21:18:15 +02:00
2015-08-12 22:57:22 +02:00
EngineImpl* engine = allocator.newObject<EngineImpl>(fs, allocator);
2015-08-17 23:45:26 +02:00
if (!engine->create())
2014-06-16 21:18:15 +02:00
{
allocator.deleteObject(engine);
2015-07-25 19:33:19 +02:00
return nullptr;
2014-06-16 21:18:15 +02:00
}
return engine;
}
2015-09-21 21:39:28 +02:00
void Engine::destroy(Engine* engine, IAllocator& allocator)
{
2015-09-21 21:39:28 +02:00
allocator.deleteObject(engine);
fclose(g_error_file);
g_error_file = nullptr;
}
2014-06-16 21:18:15 +02:00
} // ~namespace Lumix