Kaynağa Gözat

Simplify Device

Daniele Bartolini 11 yıl önce
ebeveyn
işleme
17499d3138
2 değiştirilmiş dosya ile 145 ekleme ve 394 silme
  1. 93 298
      engine/device.cpp
  2. 52 96
      engine/device.h

+ 93 - 298
engine/device.cpp

@@ -1,4 +1,3 @@
-
 /*
 Copyright (c) 2013 Daniele Bartolini, Michele Rossi
 Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
@@ -26,29 +25,14 @@ OTHER DEALINGS IN THE SOFTWARE.
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include <cstdlib>
-
 #include "config.h"
 #include "device.h"
-#include "args.h"
-#include "disk_file.h"
-#include "disk_filesystem.h"
-#include "json_parser.h"
-#include "keyboard.h"
 #include "log.h"
 #include "lua_environment.h"
-#include "memory.h"
-#include "mouse.h"
-#include "os.h"
 #include "resource_manager.h"
-#include "string_setting.h"
-#include "string_utils.h"
-#include "touch.h"
 #include "types.h"
 #include "bundle.h"
-#include "temp_allocator.h"
 #include "resource_package.h"
-#include "console_server.h"
 #include "world.h"
 #include "lua_stack.h"
 #include "world_manager.h"
@@ -56,60 +40,33 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "lua_system.h"
 #include "debug_line.h"
 #include "material_manager.h"
-#include "gui.h"
-
-#if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
-	#include "bundle_compiler.h"
-#endif
-
-#if CROWN_PLATFORM_ANDROID
-	#include "apk_filesystem.h"
-#endif
+#include <cstdlib>
 
 #define MAX_SUBSYSTEMS_HEAP 8 * 1024 * 1024
 
 namespace crown
 {
 //-----------------------------------------------------------------------------
-Device::Device()
-	: m_allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
-
-	, m_argc(0)
-	, m_argv(NULL)
-
-	, m_fileserver(0)
-	, m_console_port(10001)
-
-	, m_is_init(false)
-	, m_is_running(false)
-	, m_is_paused(false)
-
-	, m_frame_count(0)
-
-	, m_last_time(0)
-	, m_current_time(0)
-	, m_last_delta_time(0.0f)
-	, m_time_since_start(0.0)
-
-	, m_filesystem(NULL)
-	, m_lua_environment(NULL)
-	, m_renderer(NULL)
-
-	, m_bundle_compiler(NULL)
-	, m_console(NULL)
-	, m_resource_manager(NULL)
-	, m_resource_bundle(NULL)
-
-	, m_world_manager(NULL)
-{
-	// Bundle dir is current dir by default.
-	os::getcwd(m_bundle_dir, MAX_PATH_LENGTH);
-	string::strncpy(m_source_dir, "", MAX_PATH_LENGTH);
-	string::strncpy(m_boot_file, "lua/game", MAX_PATH_LENGTH);
-}
-
-//-----------------------------------------------------------------------------
-Device::~Device()
+Device::Device(Filesystem& fs, StringId64 boot_package, StringId64 boot_script)
+	: _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
+	, _width(0)
+	, _height(0)
+	, _is_init(false)
+	, _is_running(false)
+	, _is_paused(false)
+	, _frame_count(0)
+	, _last_time(0)
+	, _current_time(0)
+	, _last_delta_time(0.0f)
+	, _time_since_start(0.0)
+	, _fs(fs)
+	, _boot_package_id(boot_package)
+	, _boot_script_id(boot_script)
+	, _boot_package(NULL)
+	, _lua_environment(NULL)
+	, _resource_manager(NULL)
+	, _resource_bundle(NULL)
+	, _world_manager(NULL)
 {
 }
 
@@ -117,271 +74,146 @@ Device::~Device()
 void Device::init()
 {
 	// Initialize
-	CE_LOGI("Initializing Crown Engine %d.%d.%d...", CROWN_VERSION_MAJOR, CROWN_VERSION_MINOR, CROWN_VERSION_MICRO);
-
-	CE_LOGD("Creating filesystem...");
-	// Default bundle filesystem
-	#if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
-		if (m_fileserver == 1)
-		{
-			m_filesystem = CE_NEW(m_allocator, NetworkFilesystem)(NetAddress(127, 0, 0, 1), 10001);
-		}
-		else
-		{
-			m_filesystem = CE_NEW(m_allocator, DiskFilesystem)(m_bundle_dir);
-		}
-	#elif CROWN_PLATFORM_ANDROID
-		if (m_fileserver == 1)
-		{
-			m_filesystem = CE_NEW(m_allocator, NetworkFilesystem)(NetAddress(192, 168, 0, 7), 10001);
-		}
-		else
-		{
-			m_filesystem = CE_NEW(m_allocator, ApkFilesystem)();
-		}
-	#endif
-
-	m_resource_bundle = Bundle::create(m_allocator, *m_filesystem);
+	CE_LOGI("Initializing Crown Engine %s...", version());
+	_resource_bundle = Bundle::create(_allocator, _fs);
 
 	// Create resource manager
 	CE_LOGD("Creating resource manager...");
-	m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle);
+	_resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_bundle);
 
 	// Create world manager
 	CE_LOGD("Creating world manager...");
-	m_world_manager = CE_NEW(m_allocator, WorldManager)();
-
-	// Create input devices
-	m_keyboard = CE_NEW(m_allocator, Keyboard);
-	m_mouse = CE_NEW(m_allocator, Mouse);
-	m_touch = CE_NEW(m_allocator, Touch);
-
-	// Create renderer
-	CE_LOGD("Creating renderer...");
-	graphics_system::init();
-	debug_line::init();
-	Gui::init();
+	_world_manager = CE_NEW(_allocator, WorldManager)();
 
 	CE_LOGD("Creating material manager...");
 	material_manager::init();
 
 	CE_LOGD("Creating lua system...");
 	lua_system::init();
-	m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)(lua_system::state());
-
-	CE_LOGD("Creating physics...");
-	physics_system::init();
-
-	CE_LOGD("Creating audio...");
-	audio_system::init();
+	_lua_environment = CE_NEW(_allocator, LuaEnvironment)(lua_system::state());
 
 	CE_LOGD("Crown Engine initialized.");
 	CE_LOGD("Initializing Game...");
 
-	m_is_init = true;
-	start();
+	_is_init = true;
+	_is_running = true;
+	_last_time = os::clocktime();
+
+	_boot_package = create_resource_package(_boot_package_id);
+	_boot_package->load();
+	_boot_package->flush();
 
-	// Execute lua boot file
-	m_lua_environment->load_and_execute(m_boot_file);
-	m_lua_environment->call_global("init", 0);
+	ResourceId bootid;
+	bootid.type = LUA_TYPE;
+	bootid.name = _boot_script_id;
+	_lua_environment->execute((LuaResource*) _resource_manager->get(bootid));
+	_lua_environment->call_global("init", 0);
 }
 
 //-----------------------------------------------------------------------------
 void Device::shutdown()
 {
-	CE_ASSERT(is_init(), "Engine is not initialized");
+	CE_ASSERT(_is_init, "Engine is not initialized");
 
 	// Shutdowns the game
-	m_lua_environment->call_global("shutdown", 0);
+	_lua_environment->call_global("shutdown", 0);
 
-	CE_LOGD("Releasing audio...");
-	audio_system::shutdown();
-
-	CE_LOGD("Releasing physics...");
-	physics_system::shutdown();
+	_boot_package->unload();
+	destroy_resource_package(_boot_package);
 
 	CE_LOGD("Releasing lua system...");
 	lua_system::shutdown();
-	if (m_lua_environment)
-	{
-		CE_DELETE(m_allocator, m_lua_environment);
-	}
-
-	CE_LOGD("Releasing input devices...");
-	CE_DELETE(m_allocator, m_touch);
-	CE_DELETE(m_allocator, m_mouse);
-	CE_DELETE(m_allocator, m_keyboard);
+	CE_DELETE(_allocator, _lua_environment);
 
 	CE_LOGD("Releasing material manager...");
 	material_manager::shutdown();
 
-	CE_LOGD("Releasing renderer...");
-	debug_line::shutdown();
-	graphics_system::shutdown();
-
 	CE_LOGD("Releasing world manager...");
-	CE_DELETE(m_allocator, m_world_manager);
+	CE_DELETE(_allocator, _world_manager);
 
 	CE_LOGD("Releasing resource manager...");
-	if (m_resource_manager)
-	{
-		CE_DELETE(m_allocator, m_resource_manager);
-	}
-
-	if (m_resource_bundle)
-	{
-		Bundle::destroy(m_allocator, m_resource_bundle);
-	}
-
-	CE_LOGD("Releasing filesystem...");
-	if (m_filesystem)
-	{
-		CE_DELETE(m_allocator, m_filesystem);
-	}
-
-	m_allocator.clear();
-	m_is_init = false;
-}
+	CE_DELETE(_allocator, _resource_manager);
 
-//-----------------------------------------------------------------------------
-bool Device::is_init() const
-{
-	return m_is_init;
-}
-
-//-----------------------------------------------------------------------------
-bool Device::is_paused() const
-{
-	return m_is_paused;
-}
+	Bundle::destroy(_allocator, _resource_bundle);
 
-//-----------------------------------------------------------------------------
-Filesystem* Device::filesystem()
-{
-	return m_filesystem;
+	_allocator.clear();
+	_is_init = false;
 }
 
 //-----------------------------------------------------------------------------
 ResourceManager* Device::resource_manager()
 {
-	return m_resource_manager;
+	return _resource_manager;
 }
 
 //-----------------------------------------------------------------------------
 LuaEnvironment* Device::lua_environment()
 {
-	return m_lua_environment;
-}
-
-//-----------------------------------------------------------------------------
-OsWindow* Device::window()
-{
-	return m_window;
+	return _lua_environment;
 }
 
 //-----------------------------------------------------------------------------
-Renderer* Device::renderer()
+void Device::quit()
 {
-	return NULL;
-}
-
-//-----------------------------------------------------------------------------
-Keyboard* Device::keyboard()
-{
-	return m_keyboard;
-}
-
-//-----------------------------------------------------------------------------
-Mouse* Device::mouse()
-{
-	return m_mouse;
-}
-
-//-----------------------------------------------------------------------------
-Touch* Device::touch()
-{
-	return m_touch;
-}
-
-//-----------------------------------------------------------------------------
-Accelerometer* Device::accelerometer()
-{
-	return NULL;
-}
-
-//-----------------------------------------------------------------------------
-void Device::start()
-{
-	CE_ASSERT(m_is_init, "Cannot start uninitialized engine.");
-
-	m_is_running = true;
-	m_last_time = os::clocktime();
-}
-
-//-----------------------------------------------------------------------------
-void Device::stop()
-{
-	CE_ASSERT(m_is_init, "Cannot stop uninitialized engine.");
-
-	m_is_running = false;
+	_is_running = false;
 }
 
 //-----------------------------------------------------------------------------
 void Device::pause()
 {
-	m_is_paused = true;
+	_is_paused = true;
 	CE_LOGI("Engine paused.");
 }
 
 //-----------------------------------------------------------------------------
 void Device::unpause()
 {
-	m_is_paused = false;
+	_is_paused = false;
 	CE_LOGI("Engine unpaused.");
 }
 
 //-----------------------------------------------------------------------------
 bool Device::is_running() const
 {
-	return m_is_running;
+	return _is_running;
 }
 
 //-----------------------------------------------------------------------------
 uint64_t Device::frame_count() const
 {
-	return m_frame_count;
+	return _frame_count;
 }
 
 //-----------------------------------------------------------------------------
 float Device::last_delta_time() const
 {
-	return m_last_delta_time;
+	return _last_delta_time;
 }
 
 //-----------------------------------------------------------------------------
 double Device::time_since_start() const
 {
-	return m_time_since_start;
+	return _time_since_start;
 }
 
 //-----------------------------------------------------------------------------
-void Device::frame()
+void Device::update()
 {
-	m_current_time = os::clocktime();
-	const int64_t time = m_current_time - m_last_time;
-	m_last_time = m_current_time;
+	_current_time = os::clocktime();
+	const int64_t time = _current_time - _last_time;
+	_last_time = _current_time;
 	const double freq = (double) os::clockfrequency();
-	m_last_delta_time = time * (1.0 / freq);
-	m_time_since_start += m_last_delta_time;
+	_last_delta_time = time * (1.0 / freq);
+	_time_since_start += _last_delta_time;
 
-	if (!m_is_paused)
+	if (!_is_paused)
 	{
-		m_resource_manager->complete_requests();
-		m_lua_environment->call_global("frame", 1, ARGUMENT_FLOAT, last_delta_time());
+		_resource_manager->complete_requests();
+		_lua_environment->call_global("update", 1, ARGUMENT_FLOAT, last_delta_time());
 	}
 
 	lua_system::clear_temporaries();
-	m_frame_count++;
+	_frame_count++;
 }
 
 //-----------------------------------------------------------------------------
@@ -399,19 +231,26 @@ void Device::render_world(World* world, Camera* camera)
 //-----------------------------------------------------------------------------
 WorldId Device::create_world()
 {
-	return m_world_manager->create_world();
+	return _world_manager->create_world();
 }
 
 //-----------------------------------------------------------------------------
 void Device::destroy_world(WorldId world)
 {
-	m_world_manager->destroy_world(world);
+	_world_manager->destroy_world(world);
 }
 
 //-----------------------------------------------------------------------------
 ResourcePackage* Device::create_resource_package(const char* name)
 {
-	return CE_NEW(default_allocator(), ResourcePackage)(name, *m_resource_manager);
+	ResourceId resid("package", name);
+	return create_resource_package((StringId64) resid.name);
+}
+
+//-----------------------------------------------------------------------------
+ResourcePackage* Device::create_resource_package(StringId64 id)
+{
+	return CE_NEW(default_allocator(), ResourcePackage)(id, *_resource_manager);
 }
 
 //-----------------------------------------------------------------------------
@@ -423,71 +262,27 @@ void Device::destroy_resource_package(ResourcePackage* package)
 //-----------------------------------------------------------------------------
 void Device::reload(const char* , const char* )
 {
-/*
-	#if defined(LINUX) || defined(WINDOWS)
-		TempAllocator4096 temp;
-		DynamicString filename(temp);
-		filename += name;
-		filename += '.';
-		filename += type;
-
-
-		if (!m_bundle_compiler->compile(m_bundle_dir, m_source_dir, filename.c_str()))
-		{
-			CE_LOGD("Compilation failed.");
-			return;
-		}
-
-		ResourceId old_res_id(type, name);
-		const void* old_res = m_resource_manager->get(old_res_id);
-		m_resource_manager->unload(old_res_id, true);
-
-		ResourceId res_id = m_resource_manager->load(type, name);
-		m_resource_manager->flush();
-		const void* new_res = m_resource_manager->get(res_id);
-
-		uint32_t type_hash = string::murmur2_32(type, string::strlen(type), 0);
-
-		switch (type_hash)
-		{
-			case UNIT_TYPE:
-			{
-				CE_LOGD("Reloading unit: %s", name);
-				/// Reload unit in all worlds
-				for (uint32_t i = 0; i < id_array::size(m_world_manager->worlds()); i++)
-				{
-					m_world_manager->worlds()[i]->reload_units((UnitResource*) old_res, (UnitResource*) new_res);
-				}
-				break;
-			}
-			case SOUND_TYPE:
-			{
-				CE_LOGD("Reloading sound: %s", name);
-				for (uint32_t i = 0; i < id_array::size(m_world_manager->worlds()); i++)
-				{
-					m_world_manager->worlds()[i]->sound_world()->reload_sounds((SoundResource*) old_res, (SoundResource*) new_res);
-				}
-				break;
-			}
-			default:
-			{
-				CE_ASSERT(false, "Oops, unknown resource type: %s", type);
-				break;
-			}
-		}
-	#endif
-*/
 }
 
-static Device* g_device;
-void set_device(Device* device)
+namespace device_globals
 {
-	g_device = device;
-}
+	Device* _device = NULL;
+
+	void init(Filesystem& fs, StringId64 boot_package, StringId64 boot_script)
+	{
+		CE_ASSERT(_device == NULL, "Crown already initialized");
+		_device = CE_NEW(default_allocator(), Device)(fs, boot_package, boot_script);
+	}
+
+	void shutdown()
+	{
+		CE_DELETE(default_allocator(), _device);
+	}
+} // namespace device_globals
 
 Device* device()
 {
-	return g_device;
+	return device_globals::_device;
 }
 
 } // namespace crown

+ 52 - 96
engine/device.h

@@ -36,19 +36,11 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
-struct Accelerometer;
 class Bundle;
-class BundleCompiler;
-class ConsoleServer;
 class Filesystem;
-struct Keyboard;
 class LuaEnvironment;
-struct Mouse;
-class OsWindow;
-class Renderer;
 class ResourceManager;
 struct ResourcePackage;
-struct Touch;
 class World;
 class WorldManager;
 struct Camera;
@@ -69,36 +61,27 @@ struct DisplayMode
 /// the engine subsystems and related stuff.
 ///
 /// @ingroup Device
-class Device
+struct Device
 {
-public:
-
-	Device();
-	~Device();
+	Device(Filesystem& fs, StringId64 boot_package, StringId64 boot_script);
 
 	void init();
 
 	/// Shutdowns the engine freeing all the allocated resources
 	void shutdown();
 
-	/// Returns the number of command line arguments passed to
-	/// the engine executable.
-	int32_t argc() const { return m_argc; }
-
-	/// Returns the string value of the command line arguments passed
-	/// to the engine executable.
-	/// The size of the returned array is given by Device::argc().
-	const char** argv() const { return (const char**) m_argv; }
+	/// Returns a string identifying what platform the engine is running on.
+	const char* platform() const { return CROWN_PLATFORM_NAME; }
 
-	/// Returns wheter the engine is running (i.e. it is actually
-	/// doing work).
-	bool is_running() const;
+	/// Returns a string identifying what architecture the engine is running on.
+	const char* architecture() const { return CROWN_ARCH_NAME; }
 
-	/// Returns whether the engine is correctly initialized
-	bool is_init() const;
+	/// Returns a string identifying the engine version.
+	const char* version() const { return CROWN_VERSION_MAJOR "." CROWN_VERSION_MINOR "." CROWN_VERSION_MICRO; }
 
-	/// Returns wheter the engine is paused
-	bool is_paused() const;
+	/// Returns wheter the engine is running (i.e. it is advancing
+	/// the simulation).
+	bool is_running() const;
 
 	/// Return the number of frames rendered from the first
 	/// call to Device::start()
@@ -110,12 +93,8 @@ public:
 	/// Returns the time in seconds since the first call to start().
 	double time_since_start() const;
 
-	/// Forces the engine to actually start doing work.
-	void start();
-
-	/// Forces the engine to stop all the work it is doing
-	/// and normally terminates the program.
-	void stop();
+	/// Quits the application.
+	void quit();
 
 	/// Pauses the engine
 	void pause();
@@ -123,21 +102,20 @@ public:
 	/// Unpauses the engine
 	void unpause();
 
-	virtual int32_t run(int argc, char** argv) = 0;
+	void update_resolution(uint16_t width, uint16_t height)
+	{
+		_width = width;
+		_height = height;
+	}
 
-	/// Returns an array of video @a modes.
-	/// Each DisplayMode has an id that can be passed to set_video_mode().
-	virtual void display_modes(Array<DisplayMode>& modes) = 0;
-
-	/// Sets the video mode @a id.
-	/// @note See video_modes().
-	virtual void set_display_mode(uint32_t id) = 0;
-
-	/// Sets whether in fullscreen or not.
-	virtual void set_fullscreen(bool full) = 0;
+	void resolution(uint16_t& width, uint16_t& height)
+	{
+		width = _width;
+		height = _height;
+	}
 
 	/// Updates all the subsystems
-	void frame();
+	void update();
 
 	/// Updates the given @a world and renders it from the given @a camera.
 	void update_world(World* world, float dt);
@@ -150,6 +128,7 @@ public:
 
 	/// Returns the resource package with the given @a package_name name.
 	ResourcePackage* create_resource_package(const char* name);
+	ResourcePackage* create_resource_package(StringId64 id);
 
 	/// Destroy a previously created resource @a package.
 	/// @note
@@ -159,64 +138,37 @@ public:
 
 	void reload(const char* type, const char* name);
 
-	Filesystem* filesystem();
 	ResourceManager* resource_manager();
 	LuaEnvironment* lua_environment();
+	WorldManager* world_manager() { return _world_manager; }
 
-	OsWindow* window();
-	Renderer* renderer();
-
-	Keyboard* keyboard();
-	Mouse* mouse();
-	Touch* touch();
-	Accelerometer* accelerometer();
-	ConsoleServer* console() { return m_console; }
-	WorldManager* world_manager() { return m_world_manager; }
-
-protected:
+private:
 
 	// Used to allocate all subsystems
-	LinearAllocator m_allocator;
+	LinearAllocator _allocator;
 
-	int32_t m_argc;
-	char** m_argv;
+	uint16_t _width;
+	uint16_t _height;
 
-	// Preferred settings
-	char m_source_dir[MAX_PATH_LENGTH];
-	char m_bundle_dir[MAX_PATH_LENGTH];
-	char m_boot_file[MAX_PATH_LENGTH];
-	int32_t m_fileserver;
-	uint16_t m_console_port;
+	bool _is_init		: 1;
+	bool _is_running	: 1;
+	bool _is_paused		: 1;
 
-	bool m_is_init		: 1;
-	bool m_is_running	: 1;
-	bool m_is_paused	: 1;
+	uint64_t _frame_count;
+	int64_t _last_time;
+	int64_t _current_time;
+	float _last_delta_time;
+	double _time_since_start;
 
-	uint64_t m_frame_count;
+	Filesystem& _fs;
+	StringId64 _boot_package_id;
+	StringId64 _boot_script_id;
+	ResourcePackage* _boot_package;
 
-	int64_t m_last_time;
-	int64_t m_current_time;
-	float m_last_delta_time;
-	double m_time_since_start;
-
-	// Public subsystems
-	Filesystem* m_filesystem;
-
-	OsWindow* m_window;
-
-	Keyboard* m_keyboard;
-	Mouse* m_mouse;
-	Touch* m_touch;
-
-	LuaEnvironment* m_lua_environment;
-	Renderer* m_renderer;
-
-	// Private subsystems
-	BundleCompiler* m_bundle_compiler;
-	ConsoleServer* m_console;
-	ResourceManager* m_resource_manager;
-	Bundle* m_resource_bundle;
-	WorldManager* m_world_manager;
+	LuaEnvironment* _lua_environment;
+	ResourceManager* _resource_manager;
+	Bundle* _resource_bundle;
+	WorldManager* _world_manager;
 
 private:
 
@@ -225,8 +177,12 @@ private:
 	Device& operator=(const Device&);
 };
 
-Device* device();
+namespace device_globals
+{
+	void init(Filesystem& fs, StringId64 boot_package, StringId64 boot_script);
+	void shutdown();
+} // namespace device_globals
 
-void set_device(Device* device);
+Device* device();
 
 } // namespace crown