Ver Fonte

core: move time functions into dedicated files

Daniele Bartolini há 7 anos atrás
pai
commit
f11d8f4f7d
7 ficheiros alterados com 93 adições e 51 exclusões
  1. 0 30
      src/core/os.cpp
  2. 0 6
      src/core/os.h
  3. 55 0
      src/core/time.cpp
  4. 22 0
      src/core/time.h
  5. 8 8
      src/device/device.cpp
  6. 3 3
      src/device/profiler.cpp
  7. 5 4
      src/resource/data_compiler.cpp

+ 0 - 30
src/core/os.cpp

@@ -36,36 +36,6 @@ namespace crown
 {
 namespace os
 {
-	s64 clocktime()
-	{
-#if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_ANDROID
-		timespec now;
-		clock_gettime(CLOCK_MONOTONIC, &now);
-		return now.tv_sec * s64(1000000000) + now.tv_nsec;
-#elif CROWN_PLATFORM_OSX
-		struct timeval now;
-		gettimeofday(&now, NULL);
-		return now.tv_sec * s64(1000000) + now.tv_usec;
-#elif CROWN_PLATFORM_WINDOWS
-		LARGE_INTEGER ttime;
-		QueryPerformanceCounter(&ttime);
-		return (s64)ttime.QuadPart;
-#endif
-	}
-
-	s64 clockfrequency()
-	{
-#if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_ANDROID
-		return s64(1000000000);
-#elif CROWN_PLATFORM_OSX
-		return s64(1000000);
-#elif CROWN_PLATFORM_WINDOWS
-		LARGE_INTEGER freq;
-		QueryPerformanceFrequency(&freq);
-		return (s64)freq.QuadPart;
-#endif
-	}
-
 	void sleep(u32 ms)
 	{
 #if CROWN_PLATFORM_POSIX

+ 0 - 6
src/core/os.h

@@ -33,12 +33,6 @@ struct Stat
 /// @ingroup OS
 namespace os
 {
-	/// Returns the clock time.
-	s64 clocktime();
-
-	/// Returns che clock frequency.
-	s64 clockfrequency();
-
 	/// Suspends execution for @a ms milliseconds.
 	void sleep(u32 ms);
 

+ 55 - 0
src/core/time.cpp

@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.
+ * License: https://github.com/dbartolini/crown/blob/master/LICENSE
+ */
+
+#include "core/time.h"
+
+#if CROWN_PLATFORM_POSIX
+	#include <time.h> // clock_gettime
+#elif CROWN_PLATFORM_WINDOWS
+	#include <windows.h>
+#endif
+
+namespace crown
+{
+namespace time
+{
+	s64 now()
+	{
+#if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_ANDROID
+		timespec now;
+		clock_gettime(CLOCK_MONOTONIC, &now);
+		return now.tv_sec * s64(1000000000) + now.tv_nsec;
+#elif CROWN_PLATFORM_OSX
+		struct timeval now;
+		gettimeofday(&now, NULL);
+		return now.tv_sec * s64(1000000) + now.tv_usec;
+#elif CROWN_PLATFORM_WINDOWS
+		LARGE_INTEGER ttime;
+		QueryPerformanceCounter(&ttime);
+		return (s64)ttime.QuadPart;
+#endif
+	}
+
+	inline s64 frequency()
+	{
+#if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_ANDROID
+		return s64(1000000000);
+#elif CROWN_PLATFORM_OSX
+		return s64(1000000);
+#elif CROWN_PLATFORM_WINDOWS
+		LARGE_INTEGER freq;
+		QueryPerformanceFrequency(&freq);
+		return (s64)freq.QuadPart;
+#endif
+	}
+
+	f64 seconds(s64 ticks)
+	{
+		return f64(ticks) / f64(frequency());
+	}
+
+} // namespace time
+
+} // namespace crown

+ 22 - 0
src/core/time.h

@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.
+ * License: https://github.com/dbartolini/crown/blob/master/LICENSE
+ */
+
+#pragma once
+
+#include "core/types.h"
+
+namespace crown
+{
+namespace time
+{
+	/// Returns the current time in ticks.
+	s64 now();
+
+	/// Returns the ticks in seconds.
+	f64 seconds(s64 ticks);
+
+} // namespace time
+
+} // namespace crown

+ 8 - 8
src/device/device.cpp

@@ -21,6 +21,7 @@
 #include "core/os.h"
 #include "core/strings/string.h"
 #include "core/strings/string_stream.h"
+#include "core/time.h"
 #include "core/types.h"
 #include "device/console_server.h"
 #include "device/device.h"
@@ -431,15 +432,14 @@ void Device::run()
 
 	_lua_environment->call_global("init", 0);
 
-	s64 time_last = os::clocktime();
 	u16 old_width = _width;
 	u16 old_height = _height;
+	s64 time_last = time::now();
 
 	while (!process_events(_boot_config.vsync) && !_quit)
 	{
-		const s64 time = os::clocktime();
-		const f64 freq = (f64)os::clockfrequency();
-		const f32 dt   = f32(f64(time - time_last) / freq);
+		const s64 time = time::now();
+		const f32 dt   = time::seconds(time - time_last);
 		time_last = time;
 
 		profiler_globals::clear();
@@ -460,14 +460,14 @@ void Device::run()
 			_resource_manager->complete_requests();
 
 			{
-				const s64 t0 = os::clocktime();
+				const s64 t0 = time::now();
 				_lua_environment->call_global("update", 1, ARGUMENT_FLOAT, dt);
-				RECORD_FLOAT("lua.update", f32(f64(os::clocktime() - t0) / freq));
+				RECORD_FLOAT("lua.update", f32(time::seconds(time::now() - t0)));
 			}
 			{
-				const s64 t0 = os::clocktime();
+				const s64 t0 = time::now();
 				_lua_environment->call_global("render", 1, ARGUMENT_FLOAT, dt);
-				RECORD_FLOAT("lua.render", f32(f64(os::clocktime() - t0) / freq));
+				RECORD_FLOAT("lua.render", f32(time::seconds(time::now() - t0)));
 			}
 		}
 

+ 3 - 3
src/device/profiler.cpp

@@ -6,8 +6,8 @@
 #include "core/containers/array.h"
 #include "core/math/vector3.h"
 #include "core/memory/memory.h"
-#include "core/os.h"
 #include "core/thread/mutex.h"
+#include "core/time.h"
 #include "device/profiler.h"
 
 namespace crown
@@ -69,7 +69,7 @@ namespace profiler
 	{
 		EnterProfileScope ev;
 		ev.name = name;
-		ev.time = os::clocktime();
+		ev.time = time::now();
 
 		push(ProfilerEventType::ENTER_PROFILE_SCOPE, ev);
 	}
@@ -77,7 +77,7 @@ namespace profiler
 	void leave_profile_scope()
 	{
 		LeaveProfileScope ev;
-		ev.time = os::clocktime();
+		ev.time = time::now();
 
 		push(ProfilerEventType::LEAVE_PROFILE_SCOPE, ev);
 	}

+ 5 - 4
src/resource/data_compiler.cpp

@@ -17,6 +17,7 @@
 #include "core/os.h"
 #include "core/strings/dynamic_string.h"
 #include "core/strings/string_stream.h"
+#include "core/time.h"
 #include "device/console_server.h"
 #include "device/device_options.h"
 #include "device/log.h"
@@ -267,7 +268,7 @@ void DataCompiler::add_ignore_glob(const char* glob)
 
 void DataCompiler::scan()
 {
-	const s64 time_start = os::clocktime();
+	const s64 time_start = time::now();
 
 	// Scan all source directories
 	auto cur = map::begin(_source_dirs);
@@ -310,13 +311,13 @@ void DataCompiler::scan()
 		scan_source_dir(cur->pair.first.c_str(), "");
 	}
 
-	logi(DATA_COMPILER, "Scanned data in %.2fs", f64(os::clocktime() - time_start)/f64(os::clockfrequency()));
+	logi(DATA_COMPILER, "Scanned data in %.2fs", time::seconds(time::now() - time_start));
 	_file_monitor.start(map::begin(_source_dirs)->pair.second.c_str(), true, filemonitor_callback, this);
 }
 
 bool DataCompiler::compile(const char* data_dir, const char* platform)
 {
-	const s64 time_start = os::clocktime();
+	const s64 time_start = time::now();
 
 	FilesystemDisk data_filesystem(default_allocator());
 	data_filesystem.set_prefix(data_dir);
@@ -429,7 +430,7 @@ bool DataCompiler::compile(const char* data_dir, const char* platform)
 	}
 
 	if (success)
-		logi(DATA_COMPILER, "Compiled data in %.2fs", f64(os::clocktime() - time_start)/f64(os::clockfrequency()));
+		logi(DATA_COMPILER, "Compiled data in %.2fs", time::seconds(time::now() - time_start));
 
 	return success;
 }