|
|
@@ -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
|