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

First try for resource reloading

Daniele Bartolini 12 лет назад
Родитель
Сommit
470c9c9519

+ 41 - 45
samples/terrain/terrain.cpp

@@ -31,7 +31,8 @@ public:
 	MainScene() :
 		optShowSkybox(true),
 		optShowCrate(true),
-		optShowTerrain(true)
+		optShowTerrain(true),
+		camera_active(true)
 	{
 		device()->input_manager()->register_keyboard_listener(this);
 		device()->input_manager()->register_mouse_listener(this);
@@ -59,6 +60,16 @@ public:
 		{		
 			terrain.PlotCircle(8, 8, 8, 2);
 		}
+
+		if (event.key == KC_F5)
+		{
+			device()->reload(grass);
+		}
+
+		if (event.key == KC_SPACE)
+		{
+			camera_active = !camera_active;
+		}
 	}
 
 	void button_pressed(const MouseEvent& event)
@@ -115,62 +126,49 @@ public:
 	void on_load()
 	{
 		crown::Renderer* renderer = crown::device()->renderer();
-
-		renderer->set_clear_color(Color4::LIGHTBLUE);
 		
 		Vec3 start = Vec3(0.0f, 10.0f, 0.0f);
 
 		// Add a movable camera
 		cam = new Camera(start, 90.0f, 1.6f);
-
 		system = new FPSSystem(cam, 10.0f, 2.5f);
-
 		// Add a skybox
 		skybox = new Skybox(Vec3::ZERO, true);
 
-		//if (skybox)
-		//{
-		//	skybox->SetFace(SF_NORTH,	GetTextureManager()->Load("res/red_north.tga"));
-		//	skybox->SetFace(SF_SOUTH,	GetTextureManager()->Load("res/red_south.tga"));
-		//	skybox->SetFace(SF_EAST,	GetTextureManager()->Load("res/red_east.tga"));
-		//	skybox->SetFace(SF_WEST,	GetTextureManager()->Load("res/red_west.tga"));
-		//	skybox->SetFace(SF_UP,		GetTextureManager()->Load("res/red_up.tga"));
-		//	skybox->SetFace(SF_DOWN,	GetTextureManager()->Load("res/red_down.tga"));
-		//}
-
 		terrain.CreateTerrain(64, 64, 1, 0.0f);
-
-		red_north = device()->resource_manager()->load("textures/red_north.tga");
-		red_south = device()->resource_manager()->load("textures/red_south.tga");
-		red_east  = device()->resource_manager()->load("textures/red_east.tga");
-		red_west  = device()->resource_manager()->load("textures/red_west.tga");
-		red_up    = device()->resource_manager()->load("textures/red_up.tga");
-		red_down  = device()->resource_manager()->load("textures/red_down.tga");
-
-		grass = device()->resource_manager()->load("textures/grass.tga");
-
 		terrain.PlotCircle(4, 4, 4, 2);
-
-		//terrain.ApplyBrush(32, 32, 1.25f);
 		terrain.UpdateVertexBuffer(true);
+
+		red_north = device()->load("textures/red_north.tga");
+		red_south = device()->load("textures/red_south.tga");
+		red_east  = device()->load("textures/red_east.tga");
+		red_west  = device()->load("textures/red_west.tga");
+		red_up    = device()->load("textures/red_up.tga");
+		red_down  = device()->load("textures/red_down.tga");
+		grass     = device()->load("textures/grass.tga");
 	}
 
 	void on_unload()
 	{
-		device()->resource_manager()->unload(grass);
-		device()->resource_manager()->unload(red_north);
-		device()->resource_manager()->unload(red_south);
-		device()->resource_manager()->unload(red_east);
-		device()->resource_manager()->unload(red_west);
-		device()->resource_manager()->unload(red_up);
-		device()->resource_manager()->unload(red_down);
+		device()->unload(grass);
+		device()->unload(red_north);
+		device()->unload(red_south);
+		device()->unload(red_east);
+		device()->unload(red_west);
+		device()->unload(red_up);
+		device()->unload(red_down);
 	}
 
 	void render(float dt)
 	{
 		Renderer* renderer = device()->renderer();
+
+		renderer->set_clear_color(Color4::LIGHTBLUE);
 		
-		system->set_view_by_cursor();
+		if (camera_active)
+		{
+			system->set_view_by_cursor();
+		}
 		system->update(dt);
 
 		renderer->set_lighting(false);
@@ -197,18 +195,15 @@ public:
 
 		renderer->set_matrix(MT_MODEL, Mat4::IDENTITY);
 
-		if (device()->resource_manager()->is_loaded(grass))
+		if (device()->is_loaded(grass))
 		{
-			TextureResource* grass_tex = (TextureResource*)device()->resource_manager()->data(grass);
-			if (grass_tex)
-			{
-				TextureId grass_id = grass_tex->m_render_texture;
-				renderer->set_texturing(0, true);
-				renderer->set_texture(0, grass_id);
-				renderer->set_lighting(true);
-			}
-		}
+			renderer->set_lighting(true);
+
+			renderer->set_texturing(0, true);
 
+			TextureId grass_id = device()->renderer()->load_texture(grass);
+			renderer->set_texture(0, grass_id);
+		}
 		
 		//glColor3f(1, 1, 1);
 
@@ -258,6 +253,7 @@ private:
 	bool mouseLeftPressed;
 	bool mouseRightPressed;
 	float wheel;
+	bool camera_active;
 	Ray ray;
 };
 

+ 32 - 0
src/Device.cpp

@@ -39,6 +39,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "ArchiveResourceArchive.h"
 #include "FileResourceArchive.h"
 #include "ResourceManager.h"
+#include "TextureResource.h"
 
 #ifdef CROWN_BUILD_OPENGL
 	#include "renderers/gl/GLRenderer.h"
@@ -306,6 +307,37 @@ void Device::frame()
 	m_frame_count++;
 }
 
+//-----------------------------------------------------------------------------
+ResourceId Device::load(const char* name)
+{
+	return m_resource_manager->load(name);
+}
+
+//-----------------------------------------------------------------------------
+void Device::unload(ResourceId name)
+{
+	m_resource_manager->unload(name);
+}
+
+//-----------------------------------------------------------------------------
+void Device::reload(ResourceId name)
+{
+	m_resource_manager->reload(name);
+	m_renderer->reload_texture(name);
+}
+
+//-----------------------------------------------------------------------------
+bool Device::is_loaded(ResourceId name)
+{
+	return m_resource_manager->is_loaded(name);
+}
+
+//-----------------------------------------------------------------------------
+const void* Device::data(ResourceId name)
+{
+	return m_resource_manager->data(name);
+}
+
 //-----------------------------------------------------------------------------
 void Device::create_filesystem()
 {

+ 9 - 1
src/Device.h

@@ -29,6 +29,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Config.h"
 #include "OS.h"
 #include "MallocAllocator.h"
+#include "Resource.h"
 
 namespace crown
 {
@@ -83,6 +84,13 @@ public:
 	/// Updates all the subsystems
 	void					frame();
 
+	/// Loads a resource and returns its unique identifier.
+	ResourceId				load(const char* name);
+	void					unload(ResourceId name);
+	void					reload(ResourceId name);
+	bool					is_loaded(ResourceId name);
+	const void*				data(ResourceId name);
+
 	Filesystem*				filesystem();
 	ResourceManager*		resource_manager();
 	InputManager*			input_manager();
@@ -123,12 +131,12 @@ private:
 
 	// Public subsystems
 	Filesystem*				m_filesystem;
-	ResourceManager*		m_resource_manager;
 	InputManager*			m_input_manager;
 	Renderer*				m_renderer;
 	DebugRenderer*			m_debug_renderer;
 
 	// Private subsystems
+	ResourceManager*		m_resource_manager;
 	ResourceArchive*		m_resource_archive;
 	MallocAllocator			m_resource_allocator;
 

+ 5 - 0
src/Resource.h

@@ -46,6 +46,11 @@ struct ResourceId
 	uint32_t		name;		// Hashed resource name
 	uint32_t		type;		// Hashed resource type
 	uint32_t		index;		// Index into the ResourceManager internal list
+
+	bool			operator==(const ResourceId& b)
+	{
+		return name == b.name && type == b.type && index == b.index;
+	}
 };
 
 } // namespace crown

+ 13 - 1
src/ResourceManager.cpp

@@ -92,6 +92,8 @@ void ResourceManager::unload(ResourceId name)
 
 		entry.state = RS_UNLOADED;
 		entry.resource = NULL;
+
+
 	}
 
 	m_resources_mutex.unlock();
@@ -102,12 +104,22 @@ void ResourceManager::reload(ResourceId name)
 {
 	assert(has(name));
 	
+	m_resources_mutex.lock();
+
 	ResourceEntry& entry = m_resources[name.index];
 	
 	if (entry.state == RS_LOADED)
 	{
-		// FIXME
+		unload_by_type(name, entry.resource);
+
+		entry.state = RS_UNLOADED;
+		entry.resource = NULL;
+
+		entry.resource = load_by_type(name);
+		entry.state = RS_LOADED;
 	}
+
+	m_resources_mutex.unlock();
 }
 
 //-----------------------------------------------------------------------------

+ 0 - 2
src/TextureResource.cpp

@@ -39,8 +39,6 @@ void* TextureResource::load(Allocator& allocator, ResourceArchive& archive, Reso
 void TextureResource::online(void* resource)
 {
 	assert(resource != NULL);
-
-	((TextureResource*)resource)->m_render_texture = device()->renderer()->load_texture((TextureResource*)resource);
 }
 
 //-----------------------------------------------------------------------------

+ 0 - 4
src/TextureResource.h

@@ -58,10 +58,6 @@ private:
 	uint16_t			m_width;
 	uint16_t			m_height;
 	uint8_t*			m_data;
-
-public:
-
-	TextureId			m_render_texture;
 };
 
 } // namespace crown

+ 7 - 10
src/renderers/Renderer.h

@@ -31,6 +31,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Point2.h"
 #include "Material.h"
 #include "Texture.h"
+#include "Resource.h"
 
 namespace crown
 {
@@ -94,6 +95,9 @@ public:
 	/// Set whether the given texture @unit is enabled.
 	virtual void set_texturing(uint32_t unit, bool texturing) = 0;
 
+	//! Sets the texture to use in the specified layer
+	virtual void set_texture(uint32_t layer, TextureId texture) = 0;
+
 	/// Sets the texture @mode for the given texture @unit.
 	virtual void set_texture_mode(uint32_t unit, TextureMode mode, const Color4& blendColor) = 0;
 
@@ -169,13 +173,7 @@ public:
 	virtual void set_point_size(float size) = 0;
 	virtual void set_point_params(float min, float max) = 0;
 
-	//! Sets the texture to use in the specified layer
-	virtual void set_texture(uint32_t layer, TextureId texture) = 0;
-
-	//! Returns the current matrix
 	virtual Mat4 get_matrix(MatrixType type) const = 0;
-
-	//! Loads the current matrix
 	virtual void set_matrix(MatrixType type, const Mat4& matrix) = 0;
 
 	virtual void draw_vertex_index_buffer(const VertexBuffer* vertices, const IndexBuffer* indices) = 0;
@@ -184,10 +182,9 @@ public:
 	virtual void draw_lines(const float* vertices, const float* colors, uint32_t count) = 0;
 	virtual void draw_triangles(const float* vertices, const float* normals, const float* uvs, const uint16_t* indices, uint32_t count) = 0;
 
-	// FIXME
-	virtual TextureId	load_texture(TextureResource* texture) = 0;
-	virtual void		unload_texture(TextureResource* texture) = 0;
-	virtual TextureId	reload_texture(TextureResource* old_texture, TextureResource* new_texture) = 0;
+	virtual TextureId	load_texture(ResourceId texture) = 0;
+	virtual void		unload_texture(ResourceId texture) = 0;
+	virtual TextureId	reload_texture(ResourceId texture) = 0;
 };
 
 } // namespace crown

+ 22 - 15
src/renderers/gl/GLRenderer.cpp

@@ -37,8 +37,9 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Log.h"
 #include "Material.h"
 #include "Allocator.h"
-
 #include "TextureResource.h"
+#include "Device.h"
+#include "ResourceManager.h"
 
 #if defined(WINDOWS)
 	//Define the missing constants in vs' gl.h
@@ -760,7 +761,7 @@ void GLRenderer::draw_triangles(const float* vertices, const float* normals, con
 }
 
 //-----------------------------------------------------------------------------
-TextureId GLRenderer::load_texture(TextureResource* texture)
+TextureId GLRenderer::load_texture(ResourceId texture)
 {
 	// Search for an already existent texture
 	for (uint32_t i = 0; i < MAX_TEXTURES; i++)
@@ -775,16 +776,13 @@ TextureId GLRenderer::load_texture(TextureResource* texture)
 	GLuint gl_texture_object;
 
 	glGenTextures(1, &gl_texture_object);
-
 	glBindTexture(GL_TEXTURE_2D, gl_texture_object);
-
-	GLint gl_texture_format = GL::GetPixelFormat(texture->format());
-
-	//FIXME FIXME FIXME
 	glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
 
-	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->width(), texture->height(), 0,
-				 gl_texture_format, GL_UNSIGNED_BYTE, texture->data());
+	TextureResource* texture_ptr = (TextureResource*)device()->resource_manager()->data(texture);
+
+	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_ptr->width(), texture_ptr->height(), 0,
+				 GL::GetPixelFormat(texture_ptr->format()), GL_UNSIGNED_BYTE, texture_ptr->data());
 
 	TextureId id;
 	id.index = m_texture_count;
@@ -800,19 +798,28 @@ TextureId GLRenderer::load_texture(TextureResource* texture)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::unload_texture(TextureResource* texture)
+void GLRenderer::unload_texture(ResourceId texture)
 {
 	// FIXME
 	(void)texture;
 }
 
 //-----------------------------------------------------------------------------
-TextureId GLRenderer::reload_texture(TextureResource* old_texture, TextureResource* new_texture)
+TextureId GLRenderer::reload_texture(ResourceId texture)
 {
-	// FIXME
-	(void)old_texture;
-	(void)new_texture;
-	return TextureId();
+	for (uint32_t i = 0; i < MAX_TEXTURES; i++)
+	{
+		if (m_textures[i].texture_resource == texture)
+		{
+			// Reload texture
+			glBindTexture(GL_TEXTURE_2D, m_textures[i].texture_object);
+
+			TextureResource* texture_ptr = (TextureResource*)device()->resource_manager()->data(texture);
+
+			glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture_ptr->width(), texture_ptr->height(),
+				GL::GetPixelFormat(texture_ptr->format()), GL_UNSIGNED_BYTE, texture_ptr->data());
+		}
+	}
 }
 
 //-----------------------------------------------------------------------------

+ 8 - 5
src/renderers/gl/GLRenderer.h

@@ -27,6 +27,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include <GL/glew.h>
 #include "Renderer.h"
+#include "Resource.h"
 
 #define MAX_TEXTURES 1024
 #define MAX_TEXTURE_UNITS 8
@@ -41,8 +42,9 @@ class TextureResource;
 struct GLTexture
 {
 	TextureId			id;
+	ResourceId			texture_resource;
+
 	GLuint				texture_object;
-	TextureResource*	texture_resource;
 };
 
 /// OpenGL renderer
@@ -117,13 +119,14 @@ public:
 	void				draw_lines(const float* vertices, const float* colors, uint32_t count);
 	void				draw_triangles(const float* vertices, const float* normals, const float* uvs, const uint16_t* indices, uint32_t count);
 
-	TextureId			load_texture(TextureResource* texture);
-	void				unload_texture(TextureResource* texture);
-	TextureId			reload_texture(TextureResource* old_texture, TextureResource* new_texture);
+	TextureId			load_texture(ResourceId texture);
+	void				unload_texture(ResourceId texture);
+	TextureId			reload_texture(ResourceId texture);
 
 private:
 
-	bool				activate_texture_unit(uint32_t unit);		//!< Activates a texture unit and returns true if succes
+	/// Activates a texture unit and returns true if succes
+	bool				activate_texture_unit(uint32_t unit);
 	bool				activate_light(uint32_t light);
 
 	void				check_gl_errors();