Просмотр исходного кода

resource: CROWN_CAN_COMPILE for platforms allowed to compile data

Daniele Bartolini 6 лет назад
Родитель
Сommit
84ecd44b0c

+ 4 - 0
src/config.h

@@ -23,6 +23,10 @@
 	#define CROWN_DEVELOPMENT 0
 #endif // CROWN_DEVELOPMENT
 
+#ifndef CROWN_CAN_COMPILE
+	#define CROWN_CAN_COMPILE (CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS)
+#endif
+
 #ifndef CROWN_TOOLS
 	#define CROWN_TOOLS 0
 #endif // CROWN_TOOLS

+ 5 - 0
src/resource/compile_options.cpp

@@ -4,6 +4,9 @@
  */
 
 #include "config.h"
+
+#if CROWN_CAN_COMPILE
+
 #include "core/containers/array.h"
 #include "core/containers/vector.h"
 #include "core/filesystem/file.h"
@@ -193,3 +196,5 @@ const char* CompileOptions::exe_path(const char* const* paths, u32 num)
 }
 
 } // namespace crown
+
+#endif // CROWN_CAN_COMPILE

+ 1 - 1
src/resource/compile_options.h

@@ -38,7 +38,7 @@
 		, name                                       \
 		)
 
-#if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_ANDROID
+#if CROWN_PLATFORM_LINUX
 	#define EXE_PREFIX "./"
 	#define EXE_SUFFIX ""
 #elif CROWN_PLATFORM_WINDOWS

+ 21 - 14
src/resource/config_resource.cpp

@@ -3,6 +3,7 @@
  * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  */
 
+#include "config.h"
 #include "core/filesystem/file.h"
 #include "core/json/json_object.h"
 #include "core/json/sjson.h"
@@ -16,6 +17,25 @@
 namespace crown
 {
 namespace config_resource_internal
+{
+	void* load(File& file, Allocator& a)
+	{
+		const u32 size = file.size();
+		char* res = (char*)a.allocate(size + 1);
+		file.read(res, size);
+		res[size] = '\0';
+		return res;
+	}
+
+	void unload(Allocator& a, void* resource)
+	{
+		a.deallocate(resource);
+	}
+
+} // namespace config_resource_internal
+
+#if CROWN_CAN_COMPILE
+namespace config_resource_internal
 {
 	s32 compile(CompileOptions& opts)
 	{
@@ -42,20 +62,7 @@ namespace config_resource_internal
 		return 0;
 	}
 
-	void* load(File& file, Allocator& a)
-	{
-		const u32 size = file.size();
-		char* res = (char*)a.allocate(size + 1);
-		file.read(res, size);
-		res[size] = '\0';
-		return res;
-	}
-
-	void unload(Allocator& a, void* resource)
-	{
-		a.deallocate(resource);
-	}
-
 } // namespace config_resource_internal
+#endif // CROWN_CAN_COMPILE
 
 } // namespace crown

+ 5 - 0
src/resource/data_compiler.cpp

@@ -4,6 +4,9 @@
  */
 
 #include "config.h"
+
+#if CROWN_CAN_COMPILE
+
 #include "core/containers/hash_map.h"
 #include "core/containers/hash_set.h"
 #include "core/containers/vector.h"
@@ -1187,3 +1190,5 @@ int main_data_compiler(const DeviceOptions& opts)
 }
 
 } // namespace crown
+
+#endif // CROWN_CAN_COMPILE

+ 31 - 29
src/resource/expression_language.cpp

@@ -105,8 +105,37 @@ namespace expression_language
 
 	static inline float unsigned_to_float(unsigned u)	{FloatAndUnsigned fu; fu.u = u; return fu.f;}
 
-	#ifdef CAN_COMPILE
+	bool run(const unsigned *byte_code, const float *variables, Stack &stack)
+	{
+		const unsigned *p = byte_code;
+		while (true) {
+			unsigned bc = *p++;
+			unsigned op = bc_mask(bc);
+			unsigned id = id_mask(bc);
+			switch (op) {
+				case BC_PUSH_VAR:
+					if (stack.size == stack.capacity) return false;
+					stack.data[stack.size++] = variables[id];
+					break;
+				case BC_FUNCTION:
+					compute_function((OpCode)id, stack);
+					break;
+				case BC_END:
+					return true;
+				default: // BC_PUSH_FLOAT
+					if (stack.size == stack.capacity) return false;
+					stack.data[stack.size++] = unsigned_to_float(bc);
+					break;
 
+			}
+		}
+	}
+
+} // namespace expression_language
+
+#if CROWN_CAN_COMPILE
+namespace expression_language
+{
 	static inline unsigned float_to_unsigned(float f)	{FloatAndUnsigned fu; fu.f = f; return fu.u;}
 
 	#ifdef WIN32
@@ -452,35 +481,8 @@ namespace expression_language
 		return generate_bytecode(rpl, num_rpl, env, byte_code, capacity);
 	}
 
-	#endif // CAN_COMPILE
-
-	bool run(const unsigned *byte_code, const float *variables, Stack &stack)
-	{
-		const unsigned *p = byte_code;
-		while (true) {
-			unsigned bc = *p++;
-			unsigned op = bc_mask(bc);
-			unsigned id = id_mask(bc);
-			switch (op) {
-				case BC_PUSH_VAR:
-					if (stack.size == stack.capacity) return false;
-					stack.data[stack.size++] = variables[id];
-					break;
-				case BC_FUNCTION:
-					compute_function((OpCode)id, stack);
-					break;
-				case BC_END:
-					return true;
-				default: // BC_PUSH_FLOAT
-					if (stack.size == stack.capacity) return false;
-					stack.data[stack.size++] = unsigned_to_float(bc);
-					break;
-
-			}
-		}
-	}
-
 } // namespace expression_language
+#endif // CROWN_CAN_COMPILE
 
 } // namespace skinny
 

+ 28 - 27
src/resource/expression_language.h

@@ -24,9 +24,7 @@
 /// NAN_MARKER (9) BC_END (3)	    zero (20)	Marks the end of the byte code.
 /// float (32)									Pushes the float.
 
-/// Flag used to include the parts of the code needed to compile to bytecode.
-/// If you compile offline you can exclude this code in the runtime version.
-#define CAN_COMPILE
+#include "config.h"
 
 namespace crown
 {
@@ -34,30 +32,6 @@ namespace skinny
 {
 namespace expression_language
 {
-#ifdef CAN_COMPILE
-		/// Compiles the @a source and stores the result in the @a byte_code.
-		/// @a variables is a list of variable names. The position of the variable in
-		/// the list should match the position when @a variables is sent to the run
-		/// function.
-		///
-		/// @a constants and @a constant_values specifies a list of runtime constants
-		/// and corresponding values. Constants are expanded to numbers at compile
-		/// time.
-		///
-		/// Returns the number of compiled unsigned words. If the returned number is
-		/// greater than @a byte_code_capacity, only the first @a byte_code_capacity
-		/// words of the byte code are written to @a byte_code.
-		unsigned compile(const char *source
-			, unsigned num_variables
-			, const char **variables
-			, unsigned num_constants
-			, const char **constants
-			, const float *constant_values
-			, unsigned *byte_code
-			, unsigned byte_code_capacity
-			);
-#endif
-
 	/// Represents the working stack.
 	struct Stack
 	{
@@ -80,6 +54,33 @@ namespace expression_language
 
 } // namespace expression_language
 
+#if CROWN_CAN_COMPILE
+namespace expression_language
+{
+	/// Compiles the @a source and stores the result in the @a byte_code.
+	/// @a variables is a list of variable names. The position of the variable in
+	/// the list should match the position when @a variables is sent to the run
+	/// function.
+	///
+	/// @a constants and @a constant_values specifies a list of runtime constants
+	/// and corresponding values. Constants are expanded to numbers at compile
+	/// time.
+	///
+	/// Returns the number of compiled unsigned words. If the returned number is
+	/// greater than @a byte_code_capacity, only the first @a byte_code_capacity
+	/// words of the byte code are written to @a byte_code.
+	unsigned compile(const char *source
+		, unsigned num_variables
+		, const char **variables
+		, unsigned num_constants
+		, const char **constants
+		, const float *constant_values
+		, unsigned *byte_code
+		, unsigned byte_code_capacity
+		);
+} // namespace expression_language
+#endif
+
 } // namespace skinny
 
 } // namespace crown

+ 25 - 22
src/resource/font_resource.cpp

@@ -3,6 +3,7 @@
  * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  */
 
+#include "config.h"
 #include "core/containers/array.h"
 #include "core/filesystem/file.h"
 #include "core/filesystem/filesystem.h"
@@ -18,6 +19,29 @@
 
 namespace crown
 {
+namespace font_resource
+{
+	const GlyphData* glyph(const FontResource* fr, CodePoint cp)
+	{
+		CE_ASSERT(cp < fr->num_glyphs, "Index out of bounds");
+
+		const CodePoint* pts  = (CodePoint*)&fr[1];
+		const GlyphData* data = (GlyphData*)(pts + fr->num_glyphs);
+
+		// FIXME: Can do binary search
+		for (u32 i = 0; i < fr->num_glyphs; ++i)
+		{
+			if (pts[i] == cp)
+				return &data[i];
+		}
+
+		CE_FATAL("Glyph not found");
+		return NULL;
+	}
+
+} // namespace font_resource
+
+#if CROWN_CAN_COMPILE
 namespace font_resource_internal
 {
 	struct GlyphInfo
@@ -93,27 +117,6 @@ namespace font_resource_internal
 	}
 
 } // namespace font_resource_internal
-
-namespace font_resource
-{
-	const GlyphData* glyph(const FontResource* fr, CodePoint cp)
-	{
-		CE_ASSERT(cp < fr->num_glyphs, "Index out of bounds");
-
-		const CodePoint* pts  = (CodePoint*)&fr[1];
-		const GlyphData* data = (GlyphData*)(pts + fr->num_glyphs);
-
-		// FIXME: Can do binary search
-		for (u32 i = 0; i < fr->num_glyphs; ++i)
-		{
-			if (pts[i] == cp)
-				return &data[i];
-		}
-
-		CE_FATAL("Glyph not found");
-		return NULL;
-	}
-
-} // namespace font_resource
+#endif // CROWN_CAN_COMPILE
 
 } // namespace crown

+ 28 - 26
src/resource/level_resource.cpp

@@ -17,6 +17,33 @@
 
 namespace crown
 {
+namespace level_resource
+{
+	const StringId32* unit_names(const LevelResource* lr)
+	{
+		return (const StringId32*)((char*)lr + lr->unit_names_offset);
+	}
+
+	const UnitResource* unit_resource(const LevelResource* lr)
+	{
+		return (const UnitResource*)((char*)lr + lr->units_offset);
+	}
+
+	u32 num_sounds(const LevelResource* lr)
+	{
+		return lr->num_sounds;
+	}
+
+	const LevelSound* get_sound(const LevelResource* lr, u32 i)
+	{
+		CE_ASSERT(i < num_sounds(lr), "Index out of bounds");
+		const LevelSound* begin = (LevelSound*)((char*)lr + lr->sounds_offset);
+		return &begin[i];
+	}
+
+} // namespace level_resource
+
+#if CROWN_CAN_COMPILE
 namespace level_resource_internal
 {
 	s32 compile(CompileOptions& opts)
@@ -97,31 +124,6 @@ namespace level_resource_internal
 	}
 
 } // namespace level_resource_internal
-
-namespace level_resource
-{
-	const StringId32* unit_names(const LevelResource* lr)
-	{
-		return (const StringId32*)((char*)lr + lr->unit_names_offset);
-	}
-
-	const UnitResource* unit_resource(const LevelResource* lr)
-	{
-		return (const UnitResource*)((char*)lr + lr->units_offset);
-	}
-
-	u32 num_sounds(const LevelResource* lr)
-	{
-		return lr->num_sounds;
-	}
-
-	const LevelSound* get_sound(const LevelResource* lr, u32 i)
-	{
-		CE_ASSERT(i < num_sounds(lr), "Index out of bounds");
-		const LevelSound* begin = (LevelSound*)((char*)lr + lr->sounds_offset);
-		return &begin[i];
-	}
-
-} // namespace level_resource
+#endif // CROWN_CAN_COMPILE
 
 } // namespace crown

+ 11 - 9
src/resource/lua_resource.cpp

@@ -21,6 +21,16 @@
 
 namespace crown
 {
+namespace lua_resource
+{
+	const char* program(const LuaResource* lr)
+	{
+		return (char*)&lr[1];
+	}
+
+} // namespace lua_resource
+
+#if CROWN_CAN_COMPILE
 namespace lua_resource_internal
 {
 	s32 compile(CompileOptions& opts)
@@ -75,14 +85,6 @@ namespace lua_resource_internal
 	}
 
 } // namespace lua_resource_internal
-
-namespace lua_resource
-{
-	const char* program(const LuaResource* lr)
-	{
-		return (char*)&lr[1];
-	}
-
-} // namespace lua_resource
+#endif // CROWN_CAN_COMPILE
 
 } // namespace crown

+ 83 - 77
src/resource/material_resource.cpp

@@ -19,6 +19,88 @@
 
 namespace crown
 {
+namespace material_resource
+{
+	UniformData* uniform_data(const MaterialResource* mr, u32 i)
+	{
+		UniformData* base = (UniformData*)((char*)mr + mr->uniform_data_offset);
+		return &base[i];
+	}
+
+	UniformData* uniform_data_by_name(const MaterialResource* mr, StringId32 name)
+	{
+		for (u32 i = 0, n = mr->num_uniforms; i < n; ++i)
+		{
+			UniformData* data = uniform_data(mr, i);
+			if (data->name == name)
+				return data;
+		}
+
+		CE_FATAL("Unknown uniform");
+		return NULL;
+	}
+
+	const char* uniform_name(const MaterialResource* mr, const UniformData* ud)
+	{
+		return (const char*)mr + mr->dynamic_data_offset + mr->dynamic_data_size + ud->name_offset;
+	}
+
+	TextureData* texture_data(const MaterialResource* mr, u32 i)
+	{
+		TextureData* base = (TextureData*)((char*)mr + mr->texture_data_offset);
+		return &base[i];
+	}
+
+	const char* texture_name(const MaterialResource* mr, const TextureData* td)
+	{
+		return (const char*)mr + mr->dynamic_data_offset + mr->dynamic_data_size + td->sampler_name_offset;
+	}
+
+	UniformHandle* uniform_handle(const MaterialResource* mr, u32 i, char* dynamic)
+	{
+		UniformData* ud = uniform_data(mr, i);
+		return (UniformHandle*)(dynamic + ud->data_offset);
+	}
+
+	UniformHandle* uniform_handle_by_name(const MaterialResource* mr, StringId32 name, char* dynamic)
+	{
+		UniformData* ud = uniform_data_by_name(mr, name);
+		return (UniformHandle*)(dynamic + ud->data_offset);
+	}
+
+	TextureHandle* texture_handle(const MaterialResource* mr, u32 i, char* dynamic)
+	{
+		TextureData* td = texture_data(mr, i);
+		return (TextureHandle*)(dynamic + td->data_offset);
+	}
+
+} // namespace material_resource
+
+namespace material_resource_internal
+{
+	void* load(File& file, Allocator& a)
+	{
+		return device()->_material_manager->load(file, a);
+	}
+
+	void online(StringId64 id, ResourceManager& rm)
+	{
+		device()->_material_manager->online(id, rm);
+	}
+
+	void offline(StringId64 id, ResourceManager& rm)
+	{
+		device()->_material_manager->offline(id, rm);
+	}
+
+	void unload(Allocator& a, void* res)
+	{
+		device()->_material_manager->unload(a, res);
+	}
+
+} // namespace material_resource_internal
+
+#if CROWN_CAN_COMPILE
 namespace material_resource_internal
 {
 	struct UniformTypeInfo
@@ -256,83 +338,7 @@ namespace material_resource_internal
 		return 0;
 	}
 
-	void* load(File& file, Allocator& a)
-	{
-		return device()->_material_manager->load(file, a);
-	}
-
-	void online(StringId64 id, ResourceManager& rm)
-	{
-		device()->_material_manager->online(id, rm);
-	}
-
-	void offline(StringId64 id, ResourceManager& rm)
-	{
-		device()->_material_manager->offline(id, rm);
-	}
-
-	void unload(Allocator& a, void* res)
-	{
-		device()->_material_manager->unload(a, res);
-	}
-
 } // namespace material_resource_internal
-
-namespace material_resource
-{
-	UniformData* uniform_data(const MaterialResource* mr, u32 i)
-	{
-		UniformData* base = (UniformData*)((char*)mr + mr->uniform_data_offset);
-		return &base[i];
-	}
-
-	UniformData* uniform_data_by_name(const MaterialResource* mr, StringId32 name)
-	{
-		for (u32 i = 0, n = mr->num_uniforms; i < n; ++i)
-		{
-			UniformData* data = uniform_data(mr, i);
-			if (data->name == name)
-				return data;
-		}
-
-		CE_FATAL("Unknown uniform");
-		return NULL;
-	}
-
-	const char* uniform_name(const MaterialResource* mr, const UniformData* ud)
-	{
-		return (const char*)mr + mr->dynamic_data_offset + mr->dynamic_data_size + ud->name_offset;
-	}
-
-	TextureData* texture_data(const MaterialResource* mr, u32 i)
-	{
-		TextureData* base = (TextureData*)((char*)mr + mr->texture_data_offset);
-		return &base[i];
-	}
-
-	const char* texture_name(const MaterialResource* mr, const TextureData* td)
-	{
-		return (const char*)mr + mr->dynamic_data_offset + mr->dynamic_data_size + td->sampler_name_offset;
-	}
-
-	UniformHandle* uniform_handle(const MaterialResource* mr, u32 i, char* dynamic)
-	{
-		UniformData* ud = uniform_data(mr, i);
-		return (UniformHandle*)(dynamic + ud->data_offset);
-	}
-
-	UniformHandle* uniform_handle_by_name(const MaterialResource* mr, StringId32 name, char* dynamic)
-	{
-		UniformData* ud = uniform_data_by_name(mr, name);
-		return (UniformHandle*)(dynamic + ud->data_offset);
-	}
-
-	TextureHandle* texture_handle(const MaterialResource* mr, u32 i, char* dynamic)
-	{
-		TextureData* td = texture_data(mr, i);
-		return (TextureHandle*)(dynamic + td->data_offset);
-	}
-
-} // namespace material_resource
+#endif // CROWN_CAN_COMPILE
 
 } // namespace crown

+ 115 - 108
src/resource/mesh_resource.cpp

@@ -3,6 +3,7 @@
  * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  */
 
+#include "config.h"
 #include "core/containers/vector.h"
 #include "core/filesystem/filesystem.h"
 #include "core/filesystem/reader_writer.h"
@@ -23,6 +24,119 @@
 namespace crown
 {
 namespace mesh_resource_internal
+{
+	void* load(File& file, Allocator& a)
+	{
+		BinaryReader br(file);
+
+		u32 version;
+		br.read(version);
+		CE_ASSERT(version == RESOURCE_HEADER(RESOURCE_VERSION_MESH), "Wrong version");
+
+		u32 num_geoms;
+		br.read(num_geoms);
+
+		MeshResource* mr = CE_NEW(a, MeshResource)(a);
+		array::resize(mr->geometry_names, num_geoms);
+		array::resize(mr->geometries, num_geoms);
+
+		for (u32 i = 0; i < num_geoms; ++i)
+		{
+			StringId32 name;
+			br.read(name);
+
+			bgfx::VertexLayout layout;
+			br.read(layout);
+
+			OBB obb;
+			br.read(obb);
+
+			u32 num_verts;
+			br.read(num_verts);
+
+			u32 stride;
+			br.read(stride);
+
+			u32 num_inds;
+			br.read(num_inds);
+
+			const u32 vsize = num_verts*stride;
+			const u32 isize = num_inds*sizeof(u16);
+
+			const u32 size = sizeof(MeshGeometry) + vsize + isize;
+
+			MeshGeometry* mg = (MeshGeometry*)a.allocate(size);
+			mg->obb             = obb;
+			mg->layout          = layout;
+			mg->vertex_buffer   = BGFX_INVALID_HANDLE;
+			mg->index_buffer    = BGFX_INVALID_HANDLE;
+			mg->vertices.num    = num_verts;
+			mg->vertices.stride = stride;
+			mg->vertices.data   = (char*)&mg[1];
+			mg->indices.num     = num_inds;
+			mg->indices.data    = mg->vertices.data + vsize;
+
+			br.read(mg->vertices.data, vsize);
+			br.read(mg->indices.data, isize);
+
+			mr->geometry_names[i] = name;
+			mr->geometries[i] = mg;
+		}
+
+		return mr;
+	}
+
+	void online(StringId64 id, ResourceManager& rm)
+	{
+		MeshResource* mr = (MeshResource*)rm.get(RESOURCE_TYPE_MESH, id);
+
+		for (u32 i = 0; i < array::size(mr->geometries); ++i)
+		{
+			MeshGeometry& mg = *mr->geometries[i];
+
+			const u32 vsize = mg.vertices.num * mg.vertices.stride;
+			const u32 isize = mg.indices.num * sizeof(u16);
+
+			const bgfx::Memory* vmem = bgfx::makeRef(mg.vertices.data, vsize);
+			const bgfx::Memory* imem = bgfx::makeRef(mg.indices.data, isize);
+
+			bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(vmem, mg.layout);
+			bgfx::IndexBufferHandle ibh  = bgfx::createIndexBuffer(imem);
+			CE_ASSERT(bgfx::isValid(vbh), "Invalid vertex buffer");
+			CE_ASSERT(bgfx::isValid(ibh), "Invalid index buffer");
+
+			mg.vertex_buffer = vbh;
+			mg.index_buffer  = ibh;
+		}
+	}
+
+	void offline(StringId64 id, ResourceManager& rm)
+	{
+		MeshResource* mr = (MeshResource*)rm.get(RESOURCE_TYPE_MESH, id);
+
+		for (u32 i = 0; i < array::size(mr->geometries); ++i)
+		{
+			MeshGeometry& mg = *mr->geometries[i];
+			bgfx::destroy(mg.vertex_buffer);
+			bgfx::destroy(mg.index_buffer);
+		}
+	}
+
+	void unload(Allocator& a, void* res)
+	{
+		MeshResource* mr = (MeshResource*)res;
+
+		for (u32 i = 0; i < array::size(mr->geometries); ++i)
+		{
+			a.deallocate(mr->geometries[i]);
+		}
+		CE_DELETE(a, (MeshResource*)res);
+	}
+
+} // namespace mesh_resource_internal
+
+#if CROWN_CAN_COMPILE
+namespace mesh_resource_internal
 {
 	struct MeshCompiler
 	{
@@ -293,114 +407,7 @@ namespace mesh_resource_internal
 		return 0;
 	}
 
-	void* load(File& file, Allocator& a)
-	{
-		BinaryReader br(file);
-
-		u32 version;
-		br.read(version);
-		CE_ASSERT(version == RESOURCE_HEADER(RESOURCE_VERSION_MESH), "Wrong version");
-
-		u32 num_geoms;
-		br.read(num_geoms);
-
-		MeshResource* mr = CE_NEW(a, MeshResource)(a);
-		array::resize(mr->geometry_names, num_geoms);
-		array::resize(mr->geometries, num_geoms);
-
-		for (u32 i = 0; i < num_geoms; ++i)
-		{
-			StringId32 name;
-			br.read(name);
-
-			bgfx::VertexLayout layout;
-			br.read(layout);
-
-			OBB obb;
-			br.read(obb);
-
-			u32 num_verts;
-			br.read(num_verts);
-
-			u32 stride;
-			br.read(stride);
-
-			u32 num_inds;
-			br.read(num_inds);
-
-			const u32 vsize = num_verts*stride;
-			const u32 isize = num_inds*sizeof(u16);
-
-			const u32 size = sizeof(MeshGeometry) + vsize + isize;
-
-			MeshGeometry* mg = (MeshGeometry*)a.allocate(size);
-			mg->obb             = obb;
-			mg->layout          = layout;
-			mg->vertex_buffer   = BGFX_INVALID_HANDLE;
-			mg->index_buffer    = BGFX_INVALID_HANDLE;
-			mg->vertices.num    = num_verts;
-			mg->vertices.stride = stride;
-			mg->vertices.data   = (char*)&mg[1];
-			mg->indices.num     = num_inds;
-			mg->indices.data    = mg->vertices.data + vsize;
-
-			br.read(mg->vertices.data, vsize);
-			br.read(mg->indices.data, isize);
-
-			mr->geometry_names[i] = name;
-			mr->geometries[i] = mg;
-		}
-
-		return mr;
-	}
-
-	void online(StringId64 id, ResourceManager& rm)
-	{
-		MeshResource* mr = (MeshResource*)rm.get(RESOURCE_TYPE_MESH, id);
-
-		for (u32 i = 0; i < array::size(mr->geometries); ++i)
-		{
-			MeshGeometry& mg = *mr->geometries[i];
-
-			const u32 vsize = mg.vertices.num * mg.vertices.stride;
-			const u32 isize = mg.indices.num * sizeof(u16);
-
-			const bgfx::Memory* vmem = bgfx::makeRef(mg.vertices.data, vsize);
-			const bgfx::Memory* imem = bgfx::makeRef(mg.indices.data, isize);
-
-			bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(vmem, mg.layout);
-			bgfx::IndexBufferHandle ibh  = bgfx::createIndexBuffer(imem);
-			CE_ASSERT(bgfx::isValid(vbh), "Invalid vertex buffer");
-			CE_ASSERT(bgfx::isValid(ibh), "Invalid index buffer");
-
-			mg.vertex_buffer = vbh;
-			mg.index_buffer  = ibh;
-		}
-	}
-
-	void offline(StringId64 id, ResourceManager& rm)
-	{
-		MeshResource* mr = (MeshResource*)rm.get(RESOURCE_TYPE_MESH, id);
-
-		for (u32 i = 0; i < array::size(mr->geometries); ++i)
-		{
-			MeshGeometry& mg = *mr->geometries[i];
-			bgfx::destroy(mg.vertex_buffer);
-			bgfx::destroy(mg.index_buffer);
-		}
-	}
-
-	void unload(Allocator& a, void* res)
-	{
-		MeshResource* mr = (MeshResource*)res;
-
-		for (u32 i = 0; i < array::size(mr->geometries); ++i)
-		{
-			a.deallocate(mr->geometries[i]);
-		}
-		CE_DELETE(a, (MeshResource*)res);
-	}
-
 } // namespace mesh_resource_internal
+#endif // CROWN_CAN_COMPILE
 
 } // namespace crown

+ 30 - 23
src/resource/package_resource.cpp

@@ -3,6 +3,7 @@
  * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  */
 
+#include "config.h"
 #include "core/containers/array.h"
 #include "core/containers/hash_map.h"
 #include "core/containers/hash_set.h"
@@ -31,6 +32,34 @@ struct hash<PackageResource::Resource>
 	}
 };
 
+namespace package_resource_internal
+{
+	void* load(File& file, Allocator& a)
+	{
+		BinaryReader br(file);
+
+		u32 version;
+		br.read(version);
+		CE_ASSERT(version == RESOURCE_HEADER(RESOURCE_VERSION_PACKAGE), "Wrong version");
+
+		u32 num_resources;
+		br.read(num_resources);
+
+		PackageResource* pr = CE_NEW(a, PackageResource)(a);
+		array::resize(pr->resources, num_resources);
+		br.read(array::begin(pr->resources), sizeof(PackageResource::Resource)*num_resources);
+
+		return pr;
+	}
+
+	void unload(Allocator& a, void* resource)
+	{
+		CE_DELETE(a, (PackageResource*)resource);
+	}
+
+} // namespace package_resource_internal
+
+#if CROWN_CAN_COMPILE
 namespace package_resource_internal
 {
 	s32 bring_in_requirements(HashSet<PackageResource::Resource>& output, CompileOptions& opts, ResourceId res_id)
@@ -154,29 +183,7 @@ namespace package_resource_internal
 		return 0;
 	}
 
-	void* load(File& file, Allocator& a)
-	{
-		BinaryReader br(file);
-
-		u32 version;
-		br.read(version);
-		CE_ASSERT(version == RESOURCE_HEADER(RESOURCE_VERSION_PACKAGE), "Wrong version");
-
-		u32 num_resources;
-		br.read(num_resources);
-
-		PackageResource* pr = CE_NEW(a, PackageResource)(a);
-		array::resize(pr->resources, num_resources);
-		br.read(array::begin(pr->resources), sizeof(PackageResource::Resource)*num_resources);
-
-		return pr;
-	}
-
-	void unload(Allocator& a, void* resource)
-	{
-		CE_DELETE(a, (PackageResource*)resource);
-	}
-
 } // namespace package_resource_internal
+#endif // CROWN_CAN_COMPILE
 
 } // namespace crown

+ 46 - 43
src/resource/physics_resource.cpp

@@ -3,6 +3,7 @@
  * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  */
 
+#include "config.h"
 #include "core/containers/hash_map.h"
 #include "core/filesystem/file.h"
 #include "core/filesystem/filesystem.h"
@@ -21,6 +22,50 @@
 
 namespace crown
 {
+namespace physics_config_resource
+{
+	const PhysicsMaterial* material(const PhysicsConfigResource* pcr, StringId32 name)
+	{
+		const PhysicsMaterial* begin = (PhysicsMaterial*)((const char*)pcr + pcr->materials_offset);
+		for (u32 i = 0; i < pcr->num_materials; ++i)
+		{
+			if (begin[i].name == name)
+				return &begin[i];
+		}
+
+		CE_FATAL("Material not found");
+		return NULL;
+	}
+
+	const PhysicsActor* actor(const PhysicsConfigResource* pcr, StringId32 name)
+	{
+		const PhysicsActor* begin = (PhysicsActor*)((const char*)pcr + pcr->actors_offset);
+		for (u32 i = 0; i < pcr->num_actors; ++i)
+		{
+			if (begin[i].name == name)
+				return &begin[i];
+		}
+
+		CE_FATAL("Actor not found");
+		return NULL;
+	}
+
+	const PhysicsCollisionFilter* filter(const PhysicsConfigResource* pcr, StringId32 name)
+	{
+		const PhysicsCollisionFilter* begin = (PhysicsCollisionFilter*)((const char*)pcr + pcr->filters_offset);
+		for (u32 i = 0; i < pcr->num_filters; ++i)
+		{
+			if (begin[i].name == name)
+				return &begin[i];
+		}
+
+		CE_FATAL("Filter not found");
+		return NULL;
+	}
+
+} // namespace physics_config_resource
+
+#if CROWN_CAN_COMPILE
 namespace physics_resource_internal
 {
 	struct ColliderInfo
@@ -554,48 +599,6 @@ namespace physics_config_resource_internal
 	}
 
 } // namespace physics_config_resource_internal
-
-namespace physics_config_resource
-{
-	const PhysicsMaterial* material(const PhysicsConfigResource* pcr, StringId32 name)
-	{
-		const PhysicsMaterial* begin = (PhysicsMaterial*)((const char*)pcr + pcr->materials_offset);
-		for (u32 i = 0; i < pcr->num_materials; ++i)
-		{
-			if (begin[i].name == name)
-				return &begin[i];
-		}
-
-		CE_FATAL("Material not found");
-		return NULL;
-	}
-
-	const PhysicsActor* actor(const PhysicsConfigResource* pcr, StringId32 name)
-	{
-		const PhysicsActor* begin = (PhysicsActor*)((const char*)pcr + pcr->actors_offset);
-		for (u32 i = 0; i < pcr->num_actors; ++i)
-		{
-			if (begin[i].name == name)
-				return &begin[i];
-		}
-
-		CE_FATAL("Actor not found");
-		return NULL;
-	}
-
-	const PhysicsCollisionFilter* filter(const PhysicsConfigResource* pcr, StringId32 name)
-	{
-		const PhysicsCollisionFilter* begin = (PhysicsCollisionFilter*)((const char*)pcr + pcr->filters_offset);
-		for (u32 i = 0; i < pcr->num_filters; ++i)
-		{
-			if (begin[i].name == name)
-				return &begin[i];
-		}
-
-		CE_FATAL("Filter not found");
-		return NULL;
-	}
-
-} // namespace physics_config_resource
+#endif // CROWN_CAN_COMPILE
 
 } // namespace crown

+ 36 - 30
src/resource/shader_resource.cpp

@@ -20,20 +20,45 @@
 
 namespace crown
 {
-static const char* shaderc_paths[] =
+namespace shader_resource_internal
 {
-	EXE_PATH("shaderc"),
-#if CROWN_DEBUG
-	EXE_PATH("shaderc-debug")
-#elif CROWN_DEVELOPMENT
-	EXE_PATH("shaderc-development")
-#else
-	EXE_PATH("shaderc-release")
-#endif
-};
+	void* load(File& file, Allocator& a)
+	{
+		return device()->_shader_manager->load(file, a);
+	}
+
+	void online(StringId64 id, ResourceManager& rm)
+	{
+		device()->_shader_manager->online(id, rm);
+	}
 
+	void offline(StringId64 id, ResourceManager& rm)
+	{
+		device()->_shader_manager->offline(id, rm);
+	}
+
+	void unload(Allocator& a, void* res)
+	{
+		device()->_shader_manager->unload(a, res);
+	}
+
+} // namespace shader_resource_internal
+
+#if CROWN_CAN_COMPILE
 namespace shader_resource_internal
 {
+	static const char* shaderc_paths[] =
+	{
+		EXE_PATH("shaderc"),
+	#if CROWN_DEBUG
+		EXE_PATH("shaderc-debug")
+	#elif CROWN_DEVELOPMENT
+		EXE_PATH("shaderc-development")
+	#else
+		EXE_PATH("shaderc-release")
+	#endif
+	};
+
 	struct DepthFunction
 	{
 		enum Enum
@@ -1322,26 +1347,7 @@ namespace shader_resource_internal
 		return sc.compile();
 	}
 
-	void* load(File& file, Allocator& a)
-	{
-		return device()->_shader_manager->load(file, a);
-	}
-
-	void online(StringId64 id, ResourceManager& rm)
-	{
-		device()->_shader_manager->online(id, rm);
-	}
-
-	void offline(StringId64 id, ResourceManager& rm)
-	{
-		device()->_shader_manager->offline(id, rm);
-	}
-
-	void unload(Allocator& a, void* res)
-	{
-		device()->_shader_manager->unload(a, res);
-	}
-
 } // namespace shader_resource_internal
+#endif // CROWN_CAN_COMPILE
 
 } // namespace crown

+ 12 - 9
src/resource/sound_resource.cpp

@@ -3,6 +3,7 @@
  * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  */
 
+#include "config.h"
 #include "core/filesystem/file.h"
 #include "core/json/json_object.h"
 #include "core/json/sjson.h"
@@ -14,6 +15,16 @@
 
 namespace crown
 {
+namespace sound_resource
+{
+	const char* data(const SoundResource* sr)
+	{
+		return (char*)&sr[1];
+	}
+
+} // namespace sound_resource
+
+#if CROWN_CAN_COMPILE
 namespace sound_resource_internal
 {
 	struct WAVHeader
@@ -74,14 +85,6 @@ namespace sound_resource_internal
 	}
 
 } // namespace sound_resource_internal
-
-namespace sound_resource
-{
-	const char* data(const SoundResource* sr)
-	{
-		return (char*)&sr[1];
-	}
-
-} // namespace sound_resource
+#endif // CROWN_CAN_COMPILE
 
 } // namespace crown

+ 18 - 14
src/resource/sprite_resource.cpp

@@ -20,6 +20,17 @@
 
 namespace crown
 {
+namespace sprite_resource
+{
+	const f32* frame_data(const SpriteResource* sr, u32 i)
+	{
+		CE_ENSURE(i < sr->num_frames);
+		return ((f32*)&sr[1]) + 20*i;
+	}
+
+} // namespace sprite_resource
+
+#if CROWN_CAN_COMPILE
 namespace sprite_resource_internal
 {
 	struct SpriteFrame
@@ -144,17 +155,18 @@ namespace sprite_resource_internal
 	}
 
 } // namespace sprite_resource_internal
+#endif // CROWN_CAN_COMPILE
 
-namespace sprite_resource
+namespace sprite_animation_resource
 {
-	const f32* frame_data(const SpriteResource* sr, u32 i)
+	const u32* frames(const SpriteAnimationResource* sar)
 	{
-		CE_ENSURE(i < sr->num_frames);
-		return ((f32*)&sr[1]) + 20*i;
+		return (u32*)&sar[1];
 	}
 
-} // namespace sprite_resource
+} // namespace sprite_animation_resource
 
+#if CROWN_CAN_COMPILE
 namespace sprite_animation_resource_internal
 {
 	s32 compile(CompileOptions& opts)
@@ -194,14 +206,6 @@ namespace sprite_animation_resource_internal
 	}
 
 } // namespace sprite_animation_resource_internal
-
-namespace sprite_animation_resource
-{
-	const u32* frames(const SpriteAnimationResource* sar)
-	{
-		return (u32*)&sar[1];
-	}
-
-} // namespace sprite_animation_resource
+#endif // CROWN_CAN_COMPILE
 
 } // namespace crown

+ 90 - 87
src/resource/state_machine_resource.cpp

@@ -3,6 +3,7 @@
  * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  */
 
+#include "config.h"
 #include "core/containers/array.h"
 #include "core/containers/hash_map.h"
 #include "core/containers/vector.h"
@@ -21,6 +22,94 @@
 
 namespace crown
 {
+namespace state_machine
+{
+	const State* initial_state(const StateMachineResource* smr)
+	{
+		return (State*)((char*)smr + smr->initial_state_offset);
+	}
+
+	const State* state(const StateMachineResource* smr, const Transition* t)
+	{
+		return (State*)((char*)smr + t->state_offset);
+	}
+
+	const State* trigger(const StateMachineResource* smr, const State* s, StringId32 event, const Transition** transition_out)
+	{
+		const TransitionArray* ta = state_transitions(s);
+
+		for (u32 i = 0; i < ta->num; ++i)
+		{
+			const Transition* transition_i = transition(ta, i);
+
+			if (transition_i->event == event)
+			{
+				*transition_out = transition_i;
+				return state(smr, transition_i);
+			}
+		}
+
+		*transition_out = NULL;
+		return s;
+	}
+
+	const TransitionArray* state_transitions(const State* s)
+	{
+		return &s->ta;
+	}
+
+	const Transition* transition(const TransitionArray* ta, u32 index)
+	{
+		CE_ASSERT(index < ta->num, "Index out of bounds");
+		const Transition* first = (Transition*)(&ta[1]);
+		return &first[index];
+	}
+
+	const AnimationArray* state_animations(const State* s)
+	{
+		const TransitionArray* ta = state_transitions(s);
+		const Transition* first = (Transition*)(&ta[1]);
+		return (AnimationArray*)(first + ta->num);
+	}
+
+	const Animation* animation(const AnimationArray* aa, u32 index)
+	{
+		CE_ASSERT(index < aa->num, "Index out of bounds");
+		Animation* first = (Animation*)(&aa[1]);
+		return &first[index];
+	}
+
+	static inline const StringId32* variables_names(const StateMachineResource* smr)
+	{
+		return (StringId32*)((char*)smr + smr->variables_offset);
+	}
+
+	const f32* variables(const StateMachineResource* smr)
+	{
+		const StringId32* names = variables_names(smr);
+		return (f32*)(names + smr->num_variables);
+	}
+
+	u32 variable_index(const StateMachineResource* smr, StringId32 name)
+	{
+		const StringId32* names = variables_names(smr);
+		for (u32 i = 0; i < smr->num_variables; ++i)
+		{
+			if (names[i] == name)
+				return i;
+		}
+
+		return UINT32_MAX;
+	}
+
+	const u32* byte_code(const StateMachineResource* smr)
+	{
+		return (u32*)((char*)smr + smr->bytecode_offset);
+	}
+
+} // namespace state_machine
+
+#if CROWN_CAN_COMPILE
 template <>
 struct hash<Guid>
 {
@@ -443,92 +532,6 @@ namespace state_machine_internal
 	}
 
 } // namespace state_machine_internal
-
-namespace state_machine
-{
-	const State* initial_state(const StateMachineResource* smr)
-	{
-		return (State*)((char*)smr + smr->initial_state_offset);
-	}
-
-	const State* state(const StateMachineResource* smr, const Transition* t)
-	{
-		return (State*)((char*)smr + t->state_offset);
-	}
-
-	const State* trigger(const StateMachineResource* smr, const State* s, StringId32 event, const Transition** transition_out)
-	{
-		const TransitionArray* ta = state_transitions(s);
-
-		for (u32 i = 0; i < ta->num; ++i)
-		{
-			const Transition* transition_i = transition(ta, i);
-
-			if (transition_i->event == event)
-			{
-				*transition_out = transition_i;
-				return state(smr, transition_i);
-			}
-		}
-
-		*transition_out = NULL;
-		return s;
-	}
-
-	const TransitionArray* state_transitions(const State* s)
-	{
-		return &s->ta;
-	}
-
-	const Transition* transition(const TransitionArray* ta, u32 index)
-	{
-		CE_ASSERT(index < ta->num, "Index out of bounds");
-		const Transition* first = (Transition*)(&ta[1]);
-		return &first[index];
-	}
-
-	const AnimationArray* state_animations(const State* s)
-	{
-		const TransitionArray* ta = state_transitions(s);
-		const Transition* first = (Transition*)(&ta[1]);
-		return (AnimationArray*)(first + ta->num);
-	}
-
-	const Animation* animation(const AnimationArray* aa, u32 index)
-	{
-		CE_ASSERT(index < aa->num, "Index out of bounds");
-		Animation* first = (Animation*)(&aa[1]);
-		return &first[index];
-	}
-
-	static inline const StringId32* variables_names(const StateMachineResource* smr)
-	{
-		return (StringId32*)((char*)smr + smr->variables_offset);
-	}
-
-	const f32* variables(const StateMachineResource* smr)
-	{
-		const StringId32* names = variables_names(smr);
-		return (f32*)(names + smr->num_variables);
-	}
-
-	u32 variable_index(const StateMachineResource* smr, StringId32 name)
-	{
-		const StringId32* names = variables_names(smr);
-		for (u32 i = 0; i < smr->num_variables; ++i)
-		{
-			if (names[i] == name)
-				return i;
-		}
-
-		return UINT32_MAX;
-	}
-
-	const u32* byte_code(const StateMachineResource* smr)
-	{
-		return (u32*)((char*)smr + smr->bytecode_offset);
-	}
-
-} // namespace state_machine
+#endif // CROWN_CAN_COMPILE
 
 } // namespace crown

+ 55 - 49
src/resource/texture_resource.cpp

@@ -17,20 +17,64 @@
 
 namespace crown
 {
-static const char* texturec_paths[] =
+namespace texture_resource_internal
 {
-	EXE_PATH("texturec"),
-#if CROWN_DEBUG
-	EXE_PATH("texturec-debug")
-#elif CROWN_DEVELOPMENT
-	EXE_PATH("texturec-development")
-#else
-	EXE_PATH("texturec-release")
-#endif
-};
+	void* load(File& file, Allocator& a)
+	{
+		BinaryReader br(file);
+
+		u32 version;
+		br.read(version);
+		CE_ASSERT(version == RESOURCE_HEADER(RESOURCE_VERSION_TEXTURE), "Wrong version");
+
+		u32 size;
+		br.read(size);
+
+		TextureResource* tr = (TextureResource*)a.allocate(sizeof(TextureResource) + size);
+
+		void* data = &tr[1];
+		br.read(data, size);
+
+		tr->mem        = bgfx::makeRef(data, size);
+		tr->handle.idx = BGFX_INVALID_HANDLE;
+
+		return tr;
+	}
+
+	void online(StringId64 id, ResourceManager& rm)
+	{
+		TextureResource* tr = (TextureResource*)rm.get(RESOURCE_TYPE_TEXTURE, id);
+		tr->handle = bgfx::createTexture(tr->mem);
+	}
+
+	void offline(StringId64 id, ResourceManager& rm)
+	{
+		TextureResource* tr = (TextureResource*)rm.get(RESOURCE_TYPE_TEXTURE, id);
+		bgfx::destroy(tr->handle);
+	}
+
+	void unload(Allocator& a, void* resource)
+	{
+		a.deallocate(resource);
+	}
+
+} // namespace texture_resource_internal
 
+#if CROWN_CAN_COMPILE
 namespace texture_resource_internal
 {
+	static const char* texturec_paths[] =
+	{
+		EXE_PATH("texturec"),
+	#if CROWN_DEBUG
+		EXE_PATH("texturec-debug")
+	#elif CROWN_DEVELOPMENT
+		EXE_PATH("texturec-development")
+	#else
+		EXE_PATH("texturec-release")
+	#endif
+	};
+
 	s32 compile(CompileOptions& opts)
 	{
 		Buffer buf = opts.read();
@@ -101,45 +145,7 @@ namespace texture_resource_internal
 		return 0;
 	}
 
-	void* load(File& file, Allocator& a)
-	{
-		BinaryReader br(file);
-
-		u32 version;
-		br.read(version);
-		CE_ASSERT(version == RESOURCE_HEADER(RESOURCE_VERSION_TEXTURE), "Wrong version");
-
-		u32 size;
-		br.read(size);
-
-		TextureResource* tr = (TextureResource*)a.allocate(sizeof(TextureResource) + size);
-
-		void* data = &tr[1];
-		br.read(data, size);
-
-		tr->mem        = bgfx::makeRef(data, size);
-		tr->handle.idx = BGFX_INVALID_HANDLE;
-
-		return tr;
-	}
-
-	void online(StringId64 id, ResourceManager& rm)
-	{
-		TextureResource* tr = (TextureResource*)rm.get(RESOURCE_TYPE_TEXTURE, id);
-		tr->handle = bgfx::createTexture(tr->mem);
-	}
-
-	void offline(StringId64 id, ResourceManager& rm)
-	{
-		TextureResource* tr = (TextureResource*)rm.get(RESOURCE_TYPE_TEXTURE, id);
-		bgfx::destroy(tr->handle);
-	}
-
-	void unload(Allocator& a, void* resource)
-	{
-		a.deallocate(resource);
-	}
-
 } // namespace texture_resource_internal
+#endif // CROWN_CAN_COMPILE
 
 } // namespace crown

+ 6 - 0
src/resource/unit_compiler.cpp

@@ -3,6 +3,10 @@
  * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  */
 
+#include "config.h"
+
+#if CROWN_CAN_COMPILE
+
 #include "core/containers/array.h"
 #include "core/containers/hash_map.h"
 #include "core/json/json_object.h"
@@ -525,3 +529,5 @@ s32 UnitCompiler::compile_component(Buffer& output, StringId32 type, const char*
 }
 
 } // namespace crown
+
+#endif // CROWN_CAN_COMPILE

+ 7 - 1
src/resource/unit_compiler.h

@@ -5,7 +5,11 @@
 
 #pragma once
 
-#include "core/containers/types.h"
+#include "config.h"
+
+#if CROWN_CAN_COMPILE
+
+#include "core/containers/hash_map.h"
 #include "core/json/types.h"
 #include "core/strings/string_id.h"
 #include "resource/compile_options.h"
@@ -71,3 +75,5 @@ struct UnitCompiler
 };
 
 } // namespace crown
+
+#endif // CROWN_CAN_COMPILE

+ 3 - 6
src/resource/unit_resource.cpp

@@ -3,16 +3,12 @@
  * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  */
 
-#include "core/containers/array.h"
-#include "core/containers/hash_map.h"
-#include "core/filesystem/file.h"
-#include "core/filesystem/filesystem.h"
-#include "core/memory/allocator.h"
-#include "resource/types.h"
+#include "config.h"
 #include "resource/unit_compiler.h"
 
 namespace crown
 {
+#if CROWN_CAN_COMPILE
 namespace unit_resource_internal
 {
 	s32 compile(CompileOptions& opts)
@@ -28,5 +24,6 @@ namespace unit_resource_internal
 	}
 
 } // namespace unit_resource_internal
+#endif // CROWN_CAN_COMPILE
 
 } // namespace crown