Procházet zdrojové kódy

Merge branch 'master' of github.com:taylor001/crown

Daniele Bartolini před 10 roky
rodič
revize
cd1bae1d96

+ 5 - 3
src/compilers/bundle_compiler.cpp

@@ -91,10 +91,12 @@ bool BundleCompiler::compile_all(const char* platform)
 		continue;
 		continue;
 
 
 		const char* filename = files[i].c_str();
 		const char* filename = files[i].c_str();
-		char type[256];
+		const char* type = path::extension(filename);
+
 		char name[256];
 		char name[256];
-		path::extension(filename, type, 256);
-		path::filename_without_extension(filename, name, 256);
+		const uint32_t size = type - filename - 1;
+		strncpy(name, filename, size);
+		name[size] = '\0';
 
 
 		compile(type, name, platform);
 		compile(type, name, platform);
 	}
 	}

+ 0 - 2
src/core/containers/array.h

@@ -280,7 +280,6 @@ template <typename T>
 inline T& Array<T>::operator[](uint32_t index)
 inline T& Array<T>::operator[](uint32_t index)
 {
 {
 	CE_ASSERT(index < _size, "Index out of bounds");
 	CE_ASSERT(index < _size, "Index out of bounds");
-
 	return _data[index];
 	return _data[index];
 }
 }
 
 
@@ -288,7 +287,6 @@ template <typename T>
 inline const T& Array<T>::operator[](uint32_t index) const
 inline const T& Array<T>::operator[](uint32_t index) const
 {
 {
 	CE_ASSERT(index < _size, "Index out of bounds");
 	CE_ASSERT(index < _size, "Index out of bounds");
-
 	return _data[index];
 	return _data[index];
 }
 }
 
 

+ 0 - 2
src/core/containers/vector.h

@@ -297,7 +297,6 @@ template <typename T>
 inline T& Vector<T>::operator[](uint32_t index)
 inline T& Vector<T>::operator[](uint32_t index)
 {
 {
 	CE_ASSERT(index < _size, "Index out of bounds");
 	CE_ASSERT(index < _size, "Index out of bounds");
-
 	return _data[index];
 	return _data[index];
 }
 }
 
 
@@ -305,7 +304,6 @@ template <typename T>
 inline const T& Vector<T>::operator[](uint32_t index) const
 inline const T& Vector<T>::operator[](uint32_t index) const
 {
 {
 	CE_ASSERT(index < _size, "Index out of bounds");
 	CE_ASSERT(index < _size, "Index out of bounds");
-
 	return _data[index];
 	return _data[index];
 }
 }
 
 

+ 20 - 20
src/core/math/math_utils.h

@@ -51,12 +51,12 @@ inline T clamp(const T& min, const T& max, const T& val)
 
 
 inline float to_rad(float deg)
 inline float to_rad(float deg)
 {
 {
-	return deg * float(PI / 180.0);
+	return deg * PI / 180.0f;
 }
 }
 
 
 inline float to_deg(float rad)
 inline float to_deg(float rad)
 {
 {
-	return rad * float(180.0 / PI);
+	return rad * 180.0f / PI;
 }
 }
 
 
 inline uint32_t next_pow_2(uint32_t x)
 inline uint32_t next_pow_2(uint32_t x)
@@ -148,8 +148,8 @@ inline T linear(const T& p0, const T& p1, float t)
 template <typename T>
 template <typename T>
 inline T cosine(const T& p0, const T& p1, float t)
 inline T cosine(const T& p0, const T& p1, float t)
 {
 {
-	float f = t * PI;
-	float g = (1.0 - cos(f)) * 0.5;
+	const float f = t * PI;
+	const float g = (1.0f - cos(f)) * 0.5f;
 
 
 	return p0 + (g * (p1 - p0));
 	return p0 + (g * (p1 - p0));
 }
 }
@@ -158,25 +158,25 @@ inline T cosine(const T& p0, const T& p1, float t)
 template <typename T>
 template <typename T>
 inline T cubic(const T& p0, const T& p1, float t)
 inline T cubic(const T& p0, const T& p1, float t)
 {
 {
-	float tt = t * t;
-	float ttt = tt * t;
+	const float tt = t * t;
+	const float ttt = tt * t;
 
 
-	return p0 * (2.0 * ttt - 3.0 * tt + 1.0) + p1 * (3.0 * tt  - 2.0 * ttt);
+	return p0 * (2.0f * ttt - 3.0f * tt + 1.0f) + p1 * (3.0f * tt  - 2.0f * ttt);
 }
 }
 
 
 /// Bezier interpolation
 /// Bezier interpolation
 template <typename T>
 template <typename T>
 inline T bezier(const T& p0, const T& p1, const T& p2, const T& p3, float t)
 inline T bezier(const T& p0, const T& p1, const T& p2, const T& p3, float t)
 {
 {
-	float u = 1.0 - t;
-	float tt = t * t ;
-	float uu = u * u;
-	float uuu = uu * u;
-	float ttt = tt * t;
+	const float u = 1.0f - t;
+	const float tt = t * t ;
+	const float uu = u * u;
+	const float uuu = uu * u;
+	const float ttt = tt * t;
 
 
 	T tmp = (uuu * p0) +
 	T tmp = (uuu * p0) +
-			(3 * uu * t * p1) +
-			(3 * u * tt * p2) +
+			(3.0f * uu * t * p1) +
+			(3.0f * u * tt * p2) +
 			(ttt * p3);
 			(ttt * p3);
 
 
 	return tmp;
 	return tmp;
@@ -186,15 +186,15 @@ inline T bezier(const T& p0, const T& p1, const T& p2, const T& p3, float t)
 template <typename T>
 template <typename T>
 inline T catmull_rom(const T& p0, const T& p1, const T& p2, const T& p3, float t)
 inline T catmull_rom(const T& p0, const T& p1, const T& p2, const T& p3, float t)
 {
 {
-	float tt = t * t;
-	float ttt = tt * t;
+	const float tt = t * t;
+	const float ttt = tt * t;
 
 
-	T tmp = (2.0 * p1) +
+	T tmp = (2.0f * p1) +
 			((-p0 + p2) * t) +
 			((-p0 + p2) * t) +
-			(((2.0 * p0) - (5.0 * p1) + (4.0 * p2) - p3) * tt) +
-			((-p0 + (3.0 * p1) + (-3.0 * p2) + p3) * ttt);
+			(((2.0f * p0) - (5.0f * p1) + (4.0f * p2) - p3) * tt) +
+			((-p0 + (3.0f * p1) + (-3.0f * p2) + p3) * ttt);
 
 
-	return tmp * 0.5;
+	return tmp * 0.5f;
 }
 }
 
 
 /// @}
 /// @}

+ 7 - 186
src/core/strings/path.cpp

@@ -5,8 +5,8 @@
 
 
 #include "path.h"
 #include "path.h"
 #include "platform.h"
 #include "platform.h"
-#include "string_utils.h"
 #include <ctype.h> // isalpha
 #include <ctype.h> // isalpha
+#include <string.h> // strlen, strrchr
 
 
 namespace crown
 namespace crown
 {
 {
@@ -39,195 +39,16 @@ namespace path
 		path += p2;
 		path += p2;
 	}
 	}
 
 
-	const char* normalize_path(const char* path)
+	const char* basename(const char* path)
 	{
 	{
-#if CROWN_PLATFORM_POSIX
-		static char norm[1024];
-		char* cur = norm;
-
-		while ((*path) != '\0')
-		{
-			if ((*path) == '\\')
-			{
-				(*cur) = SEPARATOR;
-			}
-			else
-			{
-				(*cur) = (*path);
-			}
-
-			path++;
-			cur++;
-		}
-
-		return norm;
-#elif CROWN_PLATFORM_WINDOWS
-		static char norm[1024];
-
-		for (uint32_t i = 0; i < strlen(path)+1; i++)
-		{
-			if (path[i] == '/')
-			{
-				norm[i] = SEPARATOR;
-			}
-			else
-			{
-				norm[i] = path[i];
-			}
-		}
-
-		return norm;
-#endif
+		const char* ls = strrchr(path, '/');
+		return ls == NULL ? path : ls + 1;
 	}
 	}
 
 
-	/// Returns the pathname of the path.
-	/// @note
-	/// e.g. "/home/project/texture.tga" -> "/home/project"
-	/// e.g. "/home/project" -> "/home"
-	/// e.g. "/home" -> "/"
-	/// e.g. "home" -> ""
-	///
-	/// The @a path must be valid.
-	void pathname(const char* path, char* str, uint32_t len)
+	const char* extension(const char* path)
 	{
 	{
-		CE_ASSERT(path != NULL, "Path must be != NULL");
-		CE_ASSERT(str != NULL, "Str must be != NULL");
-
-		const char* last_separator = strrchr(path, '/');
-		const char* end = path + strlen(path) + 1;
-
-		if (last_separator == end)
-		{
-			strncpy(str, "", len);
-		}
-		else
-		{
-			substring(path, last_separator, str, len);
-		}
-	}
-
-	/// Returns the filename of the path.
-	/// @note
-	/// e.g. "/home/project/texture.tga" -> "texture.tga"
-	/// e.g. "/home/project/texture" -> "texture"
-	/// e.g. "/home -> "home"
-	/// e.g. "/" -> ""
-	///
-	/// The @a path must be valid.
-	void filename(const char* path, char* str, uint32_t len)
-	{
-		CE_ASSERT(path != NULL, "Path must be != NULL");
-		CE_ASSERT(str != NULL, "Str must be != NULL");
-
-		const char* last_separator = strrchr(path, '/');
-		const char* end = str + strlen(path) + 1;
-
-		if (last_separator == end)
-		{
-			strncpy(str, "", len);
-		}
-		else
-		{
-			substring(last_separator + 1, end, str, len);
-		}
-	}
-
-	/// Returns the basename of the path.
-	/// @note
-	/// e.g. "/home/project/texture.tga" -> "texture"
-	/// e.g. "/home/project" -> "project"
-	/// e.g. "/" -> ""
-	///
-	/// The @a path must be valid.
-	void basename(const char* path, char* str, uint32_t len)
-	{
-		CE_ASSERT(path != NULL, "Path must be != NULL");
-		CE_ASSERT(str != NULL, "Str must be != NULL");
-
-		const char* last_separator = strrchr(path, '/');
-		const char* last_dot = strrchr(path, '.');
-		const char* end = path + strlen(path) + 1;
-
-		if (last_separator == end && last_dot != end)
-		{
-			substring(path, last_dot, str, len);
-		}
-		else if (last_separator != end && last_dot == end)
-		{
-			substring(last_separator + 1, end, str, len);
-		}
-		else if (last_separator == end && last_dot == end)
-		{
-			strncpy(str, path, len);
-		}
-		else
-		{
-			substring(last_separator + 1, last_dot, str, len);
-		}
-	}
-
-	/// Returns the extension of the path.
-	/// @note
-	/// e.g. "/home/project/texture.tga" -> "tga"
-	/// e.g. "/home/project.x/texture" -> ""
-	///
-	/// The @a path must be valid.
-	void extension(const char* path, char* str, uint32_t len)
-	{
-		CE_ASSERT(path != NULL, "Path must be != NULL");
-		CE_ASSERT(str != NULL, "Str must be != NULL");
-
-		const char* last_dot = strrchr(path, '.');
-		const char* end = path + strlen(path) + 1;
-
-		if (last_dot == end)
-		{
-			strncpy(str, "", len);
-		}
-		else
-		{
-			substring(last_dot + 1, end, str, len);
-		}
-	}
-
-	/// Returns the filename without the extension.
-	/// @note
-	/// e.g. "/home/project/texture.tga" -> "/home/project/texture"
-	/// e.g. "/home/project/texture" -> "/home/project/texture"
-	///
-	/// The @a path must be valid.
-	void filename_without_extension(const char* path, char* str, uint32_t len)
-	{
-		CE_ASSERT(path != NULL, "Path must be != NULL");
-		CE_ASSERT(str != NULL, "Str must be != NULL");
-
-		const char* last_dot = strrchr(path, '.');
-
-		substring(path, last_dot, str, len);
-	}
-
-	/// Fills 'ret' with the same path but without the trailing directory separator.
-	/// @note
-	/// e.g. "/home/project/texture.tga/" -> "/home/project/texture.tga"
-	/// e.g. "/home/project/texture.tga" -> "/home/project/texture.tga"
-	///
-	/// The @a path must be valid.
-	void strip_trailing_separator(const char* path, char* str, uint32_t len)
-	{
-		CE_ASSERT(path != NULL, "Path must be != NULL");
-		CE_ASSERT(str != NULL, "Str must be != NULL");
-
-		size_t path_len = strlen(path);
-		const char* end = path + strlen(path) + 1;
-
-		if (path[path_len - 1] == '/')
-		{
-			substring(path, end - 2, str, len);
-		}
-		else
-		{
-			substring(path, end, str, len);
-		}
+		const char* ld = strrchr(path, '.');
+		return ld == NULL ? NULL : ld + 1;
 	}
 	}
 } // namespace path
 } // namespace path
 } // namespace crown
 } // namespace crown

+ 12 - 11
src/core/strings/path.h

@@ -32,16 +32,17 @@ namespace path
 	/// Appends path @a p2 to @a p1 and fills @a path with the result.
 	/// Appends path @a p2 to @a p1 and fills @a path with the result.
 	void join(const char* p1, const char* p2, DynamicString& path);
 	void join(const char* p1, const char* p2, DynamicString& path);
 
 
-	/// Returns os-dependent path from os-indipendent @a path
-	const char* normalize(const char* path);
-
-	void pathname(const char* path, char* str, uint32_t len);
-	void filename(const char* path, char* str, uint32_t len);
-	void basename(const char* path, char* str, uint32_t len);
-	void extension(const char* path, char* str, uint32_t len);
-	void filename_without_extension(const char* path, char* str, uint32_t len);
-
-	//bool segments(const char* path, Array<Str>& ret);
-	void strip_trailing_separator(const char* path, char* ret, uint32_t len);
+	/// Returns the basename of the @a path.
+	/// @note
+	/// "/home/project/texture.tga" -> "texture.tga"
+	/// "/home/project" -> "project"
+	/// "/" -> ""
+	const char* basename(const char* path);
+
+	/// Returns the extension of the @a path or NULL.
+	/// @note
+	/// "/home/texture.tga" -> "tga"
+	/// "/home/texture" -> NULL
+	const char* extension(const char* path);
 } // namespace path
 } // namespace path
 } // namespace crown
 } // namespace crown

+ 0 - 22
src/core/strings/string_utils.h

@@ -61,28 +61,6 @@ inline const char* skip_block(const char* str, char a, char b)
 	return NULL;
 	return NULL;
 }
 }
 
 
-inline void substring(const char* begin, const char* end, char* out, size_t len)
-{
-	CE_ASSERT(begin != NULL, "Begin must be != NULL");
-	CE_ASSERT(end != NULL, "End must be != NULL");
-	CE_ASSERT(out != NULL, "Out must be != NULL");
-
-	size_t i = 0;
-
-	char* out_iterator = out;
-
-	while (begin != end && i < len)
-	{
-		(*out_iterator) = (*begin);
-
-		begin++;
-		out_iterator++;
-		i++;
-	}
-
-	out[i] = '\0';
-}
-
 inline int32_t parse_int(const char* str)
 inline int32_t parse_int(const char* str)
 {
 {
 	int val;
 	int val;