Daniele Bartolini 10 lat temu
rodzic
commit
e99fd24d17

+ 0 - 1
src/compilers/bundle_compiler.h

@@ -7,7 +7,6 @@
 
 #include "disk_filesystem.h"
 #include "container_types.h"
-#include "crown.h"
 
 namespace crown
 {

+ 0 - 1
src/compilers/compile_options.h

@@ -7,7 +7,6 @@
 
 #include "filesystem.h"
 #include "reader_writer.h"
-#include "crown.h"
 
 namespace crown
 {

+ 0 - 89
src/crown.cpp

@@ -1,89 +0,0 @@
-/*
- * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#include "crown.h"
-#include "memory.h"
-#include "console_server.h"
-#include "bundle_compiler.h"
-#include "device.h"
-#include "command_line.h"
-#include "json_parser.h"
-#include "main.h"
-#include "audio.h"
-#include "physics.h"
-#include "disk_filesystem.h"
-#include "config.h"
-#include "math_utils.h"
-#include "profiler.h"
-#include "temp_allocator.h"
-#include <bgfx.h>
-
-namespace crown
-{
-
-static void help(const char* msg = NULL)
-{
-	if (msg)
-	{
-		printf("Error: %s\n", msg);
-	}
-
-	printf(
-		"Usage: crown [options]\n"
-		"Options:\n\n"
-
-		"  -h --help                  Show this help.\n"
-		"  -v --version               Show version informations.\n"
-		"  --bundle-dir <path>        Use <path> as the source directory for compiled resources.\n"
-		"  --console-port <port>      Set port of the console.\n"
-		"  --parent-window <handle>   Set the parent window <handle> of the main window.\n"
-		"                             Used only by tools.\n"
-
-		"\nAvailable only in debug and development builds:\n\n"
-
-		"  --source-dir <path>        Use <path> as the source directory for resource compilation.\n"
-		"  --project <name>           Start the project <name>.\n"
-		"  --compile                  Do a full compile of the resources.\n"
-		"  --platform <platform>      Compile resources for the given <platform>.\n"
-		"      Possible values for <platform> are:\n"
-		"          linux\n"
-		"          windows\n"
-		"          android\n"
-		"  --continue                 Continue the execution after the resource compilation step.\n"
-		"  --wait-console             Wait for a console connection before starting up.\n"
-	);
-}
-
-bool init(DeviceOptions& opts)
-{
-	profiler_globals::init();
-	audio_globals::init();
-	physics_globals::init();
-	bgfx::init();
-	device_globals::init(opts);
-	return true;
-}
-
-void update()
-{
-	while (!process_events() && device()->is_running())
-	{
-		profiler_globals::clear();
-		console_server_globals::update();
-		device()->update();
-		bgfx::frame();
-		profiler_globals::flush();
-	}
-}
-
-void shutdown()
-{
-	device_globals::shutdown();
-	bgfx::shutdown();
-	physics_globals::shutdown();
-	audio_globals::shutdown();
-	profiler_globals::shutdown();
-}
-} // namespace crown

+ 0 - 22
src/crown.h

@@ -1,22 +0,0 @@
-/*
- * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#pragma once
-
-#include "types.h"
-#include "filesystem_types.h"
-#include "device_options.h"
-
-namespace crown
-{
-	/// Initializes the engine.
-	bool init(DeviceOptions& opts);
-
-	/// Updates all the subsystems.
-	void update();
-
-	/// Shutdowns the engine.
-	void shutdown();
-} // namespace crown

+ 180 - 32
src/device.cpp

@@ -21,6 +21,11 @@
 #include "filesystem.h"
 #include "path.h"
 #include "disk_filesystem.h"
+#include "physics.h"
+#include "audio.h"
+#include "profiler.h"
+#include "console_server.h"
+#include "input_device.h"
 
 #if CROWN_PLATFORM_ANDROID
 	#include "apk_filesystem.h"
@@ -68,6 +73,11 @@ void Device::init()
 
 	read_config();
 
+	profiler_globals::init();
+	audio_globals::init();
+	physics_globals::init();
+	bgfx::init();
+
 	// Create resource manager
 	CE_LOGD("Creating resource manager...");
 	_resource_manager = CE_NEW(_allocator, ResourceManager)(*_bundle_filesystem);
@@ -117,6 +127,11 @@ void Device::shutdown()
 	CE_LOGD("Releasing resource manager...");
 	CE_DELETE(_allocator, _resource_manager);
 
+	bgfx::shutdown();
+	physics_globals::shutdown();
+	audio_globals::shutdown();
+	profiler_globals::shutdown();
+
 	CE_DELETE(_allocator, _bundle_filesystem);
 
 	_allocator.clear();
@@ -174,24 +189,34 @@ double Device::time_since_start() const
 
 void Device::update()
 {
-	_current_time = os::clocktime();
-	const int64_t time = _current_time - _last_time;
-	_last_time = _current_time;
-	const double freq = (double) os::clockfrequency();
-	_last_delta_time = time * (1.0 / freq);
-	_time_since_start += _last_delta_time;
-
-	if (!_is_paused)
+	while (!process_events() && _is_running)
 	{
-		_resource_manager->complete_requests();
-		_lua_environment->call_global("update", 1, ARGUMENT_FLOAT, last_delta_time());
-		_lua_environment->call_global("render", 1, ARGUMENT_FLOAT, last_delta_time());
-	}
+		_current_time = os::clocktime();
+		const int64_t time = _current_time - _last_time;
+		_last_time = _current_time;
+		const double freq = (double) os::clockfrequency();
+		_last_delta_time = time * (1.0 / freq);
+		_time_since_start += _last_delta_time;
+
+		profiler_globals::clear();
+		console_server_globals::update();
+
+		if (!_is_paused)
+		{
+			_resource_manager->complete_requests();
+			_lua_environment->call_global("update", 1, ARGUMENT_FLOAT, last_delta_time());
+			_lua_environment->call_global("render", 1, ARGUMENT_FLOAT, last_delta_time());
+		}
+
+		_input_manager->update();
 
-	_input_manager->update();
+		bgfx::frame();
+		profiler_globals::flush();
 
-	_frame_count++;
-	_lua_environment->clear_temporaries();
+		_lua_environment->clear_temporaries();
+
+		_frame_count++;
+	}
 }
 
 void Device::render_world(World& world, Camera* camera)
@@ -259,6 +284,127 @@ InputManager* Device::input_manager()
 	return _input_manager;
 }
 
+bool Device::process_events()
+{
+	OsEvent event;
+	bool exit = false;
+	InputManager* im = _input_manager;
+
+	static int16_t mouse_curr_x = 0;
+	static int16_t mouse_curr_y = 0;
+	static int16_t mouse_last_x = 0;
+	static int16_t mouse_last_y = 0;
+
+	const int16_t dt_x = mouse_curr_x - mouse_last_x;
+	const int16_t dt_y = mouse_curr_y - mouse_last_y;
+	im->mouse()->set_axis(MouseAxis::CURSOR_DELTA, vector3(dt_x, dt_y, 0.0f));
+	mouse_last_x = mouse_curr_x;
+	mouse_last_y = mouse_curr_y;
+
+	while(next_event(event))
+	{
+		if (event.type == OsEvent::NONE) continue;
+
+		switch (event.type)
+		{
+			case OsEvent::TOUCH:
+			{
+				const OsTouchEvent& ev = event.touch;
+				switch (ev.type)
+				{
+					case OsTouchEvent::POINTER:
+						im->touch()->set_button_state(ev.pointer_id, ev.pressed);
+						break;
+					case OsTouchEvent::MOVE:
+						im->touch()->set_axis(ev.pointer_id, vector3(ev.x, ev.y, 0.0f));
+						break;
+					default:
+						CE_FATAL("Unknown touch event type");
+						break;
+				}
+				break;
+			}
+			case OsEvent::MOUSE:
+			{
+				const OsMouseEvent& ev = event.mouse;
+				switch (ev.type)
+				{
+					case OsMouseEvent::BUTTON:
+						im->mouse()->set_button_state(ev.button, ev.pressed);
+						break;
+					case OsMouseEvent::MOVE:
+						mouse_curr_x = ev.x;
+						mouse_curr_y = ev.y;
+						im->mouse()->set_axis(MouseAxis::CURSOR, vector3(ev.x, ev.y, 0.0f));
+						break;
+					case OsMouseEvent::WHEEL:
+						im->mouse()->set_axis(MouseAxis::WHEEL, vector3(0.0f, ev.wheel, 0.0f));
+						break;
+					default:
+						CE_FATAL("Unknown mouse event type");
+						break;
+				}
+				break;
+			}
+			case OsEvent::KEYBOARD:
+			{
+				const OsKeyboardEvent& ev = event.keyboard;
+				im->keyboard()->set_button_state(ev.button, ev.pressed);
+				break;
+			}
+			case OsEvent::JOYPAD:
+			{
+				const OsJoypadEvent& ev = event.joypad;
+				switch (ev.type)
+				{
+					case OsJoypadEvent::CONNECTED:
+						im->joypad(ev.index)->set_connected(ev.connected);
+						break;
+					case OsJoypadEvent::BUTTON:
+						im->joypad(ev.index)->set_button_state(ev.button, ev.pressed);
+						break;
+					case OsJoypadEvent::AXIS:
+						im->joypad(ev.index)->set_axis(ev.button, vector3(ev.x, ev.y, ev.z));
+						break;
+					default:
+						CE_FATAL("Unknown joypad event");
+						break;
+				}
+				break;
+			}
+			case OsEvent::METRICS:
+			{
+				const OsMetricsEvent& ev = event.metrics;
+				update_resolution(ev.width, ev.height);
+				bgfx::reset(ev.width, ev.height, BGFX_RESET_VSYNC);
+				break;
+			}
+			case OsEvent::EXIT:
+			{
+				exit = true;
+				break;
+			}
+			case OsEvent::PAUSE:
+			{
+				pause();
+				break;
+			}
+			case OsEvent::RESUME:
+			{
+				unpause();
+				break;
+			}
+			default:
+			{
+				CE_FATAL("Unknown Os Event");
+				break;
+			}
+		}
+	}
+
+	return exit;
+}
+
 void Device::read_config()
 {
 	TempAllocator1024 ta;
@@ -281,29 +427,31 @@ void Device::read_config()
 	_boot_package_id = root.key("boot_package").to_resource_id();
 }
 
-namespace device_globals
+char _buffer[sizeof(Device)];
+Device* _device = NULL;
+
+void init(DeviceOptions& opts)
 {
-	char _buffer[sizeof(Device)];
-	Device* _device = NULL;
+	CE_ASSERT(_device == NULL, "Crown already initialized");
+	_device = new (_buffer) Device(opts);
+	_device->init();
+}
 
-	void init(DeviceOptions& opts)
-	{
-		CE_ASSERT(_device == NULL, "Crown already initialized");
-		_device = new (_buffer) Device(opts);
-		_device->init();
-	}
+void update()
+{
+	_device->update();
+}
 
-	void shutdown()
-	{
-		_device->shutdown();
-		_device->~Device();
-		_device = NULL;
-	}
-} // namespace device_globals
+void shutdown()
+{
+	_device->shutdown();
+	_device->~Device();
+	_device = NULL;
+}
 
 Device* device()
 {
-	return device_globals::_device;
+	return crown::_device;
 }
 
 } // namespace crown

+ 6 - 6
src/device.h

@@ -15,6 +15,7 @@
 #include "container_types.h"
 #include "input_types.h"
 #include "device_options.h"
+#include "os_event_queue.h"
 
 namespace crown
 {
@@ -105,6 +106,7 @@ struct Device
 
 private:
 
+	bool process_events();
 	void read_config();
 
 private:
@@ -144,12 +146,10 @@ private:
 	Device& operator=(const Device&);
 };
 
-namespace device_globals
-{
-	void init(DeviceOptions& opts);
-	void shutdown();
-} // namespace device_globals
-
+bool next_event(OsEvent& ev);
+void init(DeviceOptions& opts);
+void update();
+void shutdown();
 Device* device();
 
 } // namespace crown

+ 33 - 0
src/device_options.cpp

@@ -9,6 +9,39 @@
 namespace crown
 {
 
+static void help(const char* msg = NULL)
+{
+	if (msg)
+	{
+		printf("Error: %s\n", msg);
+	}
+
+	printf(
+		"Usage: crown [options]\n"
+		"Options:\n\n"
+
+		"  -h --help                  Show this help.\n"
+		"  -v --version               Show version informations.\n"
+		"  --bundle-dir <path>        Use <path> as the source directory for compiled resources.\n"
+		"  --console-port <port>      Set port of the console.\n"
+		"  --parent-window <handle>   Set the parent window <handle> of the main window.\n"
+		"                             Used only by tools.\n"
+
+		"\nAvailable only in debug and development builds:\n\n"
+
+		"  --source-dir <path>        Use <path> as the source directory for resource compilation.\n"
+		"  --project <name>           Start the project <name>.\n"
+		"  --compile                  Do a full compile of the resources.\n"
+		"  --platform <platform>      Compile resources for the given <platform>.\n"
+		"      Possible values for <platform> are:\n"
+		"          linux\n"
+		"          windows\n"
+		"          android\n"
+		"  --continue                 Continue the execution after the resource compilation step.\n"
+		"  --wait-console             Wait for a console connection before starting up.\n"
+	);
+}
+
 DeviceOptions::DeviceOptions(int argc, char** argv)
 	: _source_dir(NULL)
 	, _bundle_dir(NULL)

+ 0 - 1
src/lua/lua_window.cpp

@@ -16,7 +16,6 @@
 #include "device.h"
 #include "lua_stack.h"
 #include "lua_environment.h"
-#include "main.h"
 
 namespace crown
 {

+ 0 - 139
src/main/main.cpp

@@ -1,139 +0,0 @@
-/*
- * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#include "main.h"
-#include "device.h"
-#include "os_event_queue.h"
-#include "input_manager.h"
-#include "input_device.h"
-#include "error.h"
-#include "vector3.h"
-#include <bgfx.h>
-
-namespace crown
-{
-
-bool process_events()
-{
-	OsEvent event;
-	bool exit = false;
-	InputManager* im = device()->input_manager();
-
-	static int16_t mouse_curr_x = 0;
-	static int16_t mouse_curr_y = 0;
-	static int16_t mouse_last_x = 0;
-	static int16_t mouse_last_y = 0;
-
-	const int16_t dt_x = mouse_curr_x - mouse_last_x;
-	const int16_t dt_y = mouse_curr_y - mouse_last_y;
-	im->mouse()->set_axis(MouseAxis::CURSOR_DELTA, vector3(dt_x, dt_y, 0.0f));
-	mouse_last_x = mouse_curr_x;
-	mouse_last_y = mouse_curr_y;
-
-	while(next_event(event))
-	{
-		if (event.type == OsEvent::NONE) continue;
-
-		switch (event.type)
-		{
-			case OsEvent::TOUCH:
-			{
-				const OsTouchEvent& ev = event.touch;
-				switch (ev.type)
-				{
-					case OsTouchEvent::POINTER:
-						im->touch()->set_button_state(ev.pointer_id, ev.pressed);
-						break;
-					case OsTouchEvent::MOVE:
-						im->touch()->set_axis(ev.pointer_id, vector3(ev.x, ev.y, 0.0f));
-						break;
-					default:
-						CE_FATAL("Unknown touch event type");
-						break;
-				}
-				break;
-			}
-			case OsEvent::MOUSE:
-			{
-				const OsMouseEvent& ev = event.mouse;
-				switch (ev.type)
-				{
-					case OsMouseEvent::BUTTON:
-						im->mouse()->set_button_state(ev.button, ev.pressed);
-						break;
-					case OsMouseEvent::MOVE:
-						mouse_curr_x = ev.x;
-						mouse_curr_y = ev.y;
-						im->mouse()->set_axis(MouseAxis::CURSOR, vector3(ev.x, ev.y, 0.0f));
-						break;
-					case OsMouseEvent::WHEEL:
-						im->mouse()->set_axis(MouseAxis::WHEEL, vector3(0.0f, ev.wheel, 0.0f));
-						break;
-					default:
-						CE_FATAL("Unknown mouse event type");
-						break;
-				}
-				break;
-			}
-			case OsEvent::KEYBOARD:
-			{
-				const OsKeyboardEvent& ev = event.keyboard;
-				im->keyboard()->set_button_state(ev.button, ev.pressed);
-				break;
-			}
-			case OsEvent::JOYPAD:
-			{
-				const OsJoypadEvent& ev = event.joypad;
-				switch (ev.type)
-				{
-					case OsJoypadEvent::CONNECTED:
-						im->joypad(ev.index)->set_connected(ev.connected);
-						break;
-					case OsJoypadEvent::BUTTON:
-						im->joypad(ev.index)->set_button_state(ev.button, ev.pressed);
-						break;
-					case OsJoypadEvent::AXIS:
-						im->joypad(ev.index)->set_axis(ev.button, vector3(ev.x, ev.y, ev.z));
-						break;
-					default:
-						CE_FATAL("Unknown joypad event");
-						break;
-				}
-				break;
-			}
-			case OsEvent::METRICS:
-			{
-				const OsMetricsEvent& ev = event.metrics;
-				device()->update_resolution(ev.width, ev.height);
-				bgfx::reset(ev.width, ev.height, BGFX_RESET_VSYNC);
-				break;
-			}
-			case OsEvent::EXIT:
-			{
-				exit = true;
-				break;
-			}
-			case OsEvent::PAUSE:
-			{
-				device()->pause();
-				break;
-			}
-			case OsEvent::RESUME:
-			{
-				device()->unpause();
-				break;
-			}
-			default:
-			{
-				CE_FATAL("Unknown Os Event");
-				break;
-			}
-		}
-	}
-
-	return exit;
-}
-
-} // namespace crown

+ 0 - 16
src/main/main.h

@@ -1,16 +0,0 @@
-/*
- * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#include "os_event_queue.h"
-
-#pragma once
-
-namespace crown
-{
-
-bool next_event(OsEvent& ev);
-bool process_events();
-
-} // namespace crown

+ 1 - 2
src/main/main_android.cpp

@@ -9,11 +9,10 @@
 
 #include "os_event_queue.h"
 #include "thread.h"
-#include "main.h"
 #include "device_options.h"
 #include "console_server.h"
-#include "crown.h"
 #include "memory.h"
+#include "device.h"
 #include <stdlib.h>
 #include <jni.h>
 #include <android/sensor.h>

+ 1 - 2
src/main/main_linux.cpp

@@ -11,11 +11,10 @@
 #include "memory.h"
 #include "os_event_queue.h"
 #include "thread.h"
-#include "main.h"
 #include "command_line.h"
-#include "crown.h"
 #include "bundle_compiler.h"
 #include "console_server.h"
+#include "device.h"
 #include <X11/Xutil.h>
 #include <X11/Xatom.h>
 #include <X11/Xlib.h>

+ 1 - 1
src/main/main_windows.cpp

@@ -9,10 +9,10 @@
 
 #include "os_event_queue.h"
 #include "thread.h"
-#include "crown.h"
 #include "command_line.h"
 #include "console_server.h"
 #include "bundle_compiler.h"
+#include "device.h"
 #include <bgfxplatform.h>
 #include <winsock2.h>
 #ifndef WIN32_LEAN_AND_MEAN