Sfoglia il codice sorgente

Add time_since_start() and temporary callback in frame()

Daniele Bartolini 12 anni fa
parent
commit
f5fcf7e2ee
2 ha cambiato i file con 27 aggiunte e 9 eliminazioni
  1. 19 6
      engine/Device.cpp
  2. 8 3
      engine/Device.h

+ 19 - 6
engine/Device.cpp

@@ -88,6 +88,7 @@ Device::Device() :
 	m_last_time(0),
 	m_last_time(0),
 	m_current_time(0),
 	m_current_time(0),
 	m_last_delta_time(0.0f),
 	m_last_delta_time(0.0f),
+	m_time_since_start(0.0),
 
 
 	m_filesystem(NULL),
 	m_filesystem(NULL),
 	m_input_manager(NULL),
 	m_input_manager(NULL),
@@ -186,7 +187,8 @@ bool Device::init(int argc, char** argv)
 	Log::d("Window created.");
 	Log::d("Window created.");
 
 
 	// Create renderer
 	// Create renderer
-	m_renderer = Renderer::create(m_allocator);
+	m_renderer = CE_NEW(default_allocator(), Renderer)(m_allocator);
+	CE_ASSERT_NOT_NULL(m_renderer);
 	m_renderer->init();
 	m_renderer->init();
 	Log::d("Renderer created.");
 	Log::d("Renderer created.");
 
 
@@ -217,6 +219,8 @@ bool Device::init(int argc, char** argv)
 		pause();
 		pause();
 	}
 	}
 
 
+	Log::d("Total allocated size: %llu", m_allocator.allocated_size());
+
 	// Show main window
 	// Show main window
 	m_window->show();
 	m_window->show();
 
 
@@ -269,8 +273,7 @@ void Device::shutdown()
 	if (m_renderer)
 	if (m_renderer)
 	{
 	{
 		m_renderer->shutdown();
 		m_renderer->shutdown();
-
-		Renderer::destroy(m_allocator, m_renderer);
+		CE_DELETE(default_allocator(), m_renderer);
 	}
 	}
 
 
 	Log::i("Releasing Window...");
 	Log::i("Releasing Window...");
@@ -435,11 +438,18 @@ float Device::last_delta_time() const
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void Device::frame()
+double Device::time_since_start() const
+{
+	return m_time_since_start;
+}
+
+//-----------------------------------------------------------------------------
+void Device::frame(cb callback)
 {
 {
 	m_current_time = os::microseconds();
 	m_current_time = os::microseconds();
 	m_last_delta_time = (m_current_time - m_last_time) / 1000000.0f;
 	m_last_delta_time = (m_current_time - m_last_time) / 1000000.0f;
 	m_last_time = m_current_time;
 	m_last_time = m_current_time;
+	m_time_since_start += m_last_delta_time;
 
 
 	m_resource_manager->poll_resource_loader();
 	m_resource_manager->poll_resource_loader();
 
 
@@ -455,6 +465,9 @@ void Device::frame()
 	}
 	}
 
 
 	m_debug_renderer->draw_all();
 	m_debug_renderer->draw_all();
+
+	callback(m_last_delta_time);
+
 	m_renderer->frame();
 	m_renderer->frame();
 
 
 	m_frame_count++;
 	m_frame_count++;
@@ -469,7 +482,7 @@ ResourcePackage* Device::create_resource_package(const char* name)
 	m_resource_manager->flush();
 	m_resource_manager->flush();
 
 
 	PackageResource* package_res = (PackageResource*) m_resource_manager->data(package_id);
 	PackageResource* package_res = (PackageResource*) m_resource_manager->data(package_id);
-	ResourcePackage* package = CE_NEW(m_allocator, ResourcePackage)(*m_resource_manager, package_id, package_res);
+	ResourcePackage* package = CE_NEW(default_allocator(), ResourcePackage)(*m_resource_manager, package_id, package_res);
 
 
 	return package;
 	return package;
 }
 }
@@ -480,7 +493,7 @@ void Device::destroy_resource_package(ResourcePackage* package)
 	CE_ASSERT_NOT_NULL(package);
 	CE_ASSERT_NOT_NULL(package);
 
 
 	m_resource_manager->unload(package->resource_id());
 	m_resource_manager->unload(package->resource_id());
-	CE_DELETE(m_allocator, package);
+	CE_DELETE(default_allocator(), package);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------

+ 8 - 3
engine/Device.h

@@ -53,6 +53,8 @@ class ConsoleServer;
 class BundleCompiler;
 class BundleCompiler;
 class ResourcePackage;
 class ResourcePackage;
 
 
+typedef void (*cb)(float);
+
 /// The Engine.
 /// The Engine.
 /// It is the place where to look for accessing all of
 /// It is the place where to look for accessing all of
 /// the engine subsystems and related stuff.
 /// the engine subsystems and related stuff.
@@ -81,10 +83,12 @@ public:
 	/// call to Device::start()
 	/// call to Device::start()
 	uint64_t				frame_count() const;
 	uint64_t				frame_count() const;
 
 
-	/// Returns the time in milliseconds needed to render
-	/// the last frame
+	/// Returns the time in seconds needed to render the last frame
 	float					last_delta_time() const;
 	float					last_delta_time() const;
 
 
+	/// Returns the time in seconds since the first call to start().
+	double					time_since_start() const;
+
 	/// Forces the engine to actually start doing work.
 	/// Forces the engine to actually start doing work.
 	void					start();
 	void					start();
 
 
@@ -99,7 +103,7 @@ public:
 	void					unpause();
 	void					unpause();
 
 
 	/// Updates all the subsystems
 	/// Updates all the subsystems
-	void					frame();
+	void					frame(cb callback);
 
 
 	/// Returns the resource package with the given @a package_name name.
 	/// Returns the resource package with the given @a package_name name.
 	ResourcePackage*		create_resource_package(const char* name);
 	ResourcePackage*		create_resource_package(const char* name);
@@ -165,6 +169,7 @@ private:
 	uint64_t				m_last_time;
 	uint64_t				m_last_time;
 	uint64_t				m_current_time;
 	uint64_t				m_current_time;
 	float					m_last_delta_time;
 	float					m_last_delta_time;
+	double					m_time_since_start;
 
 
 	// Public subsystems
 	// Public subsystems
 	Filesystem*				m_filesystem;
 	Filesystem*				m_filesystem;