ソースを参照

Add SpriteAnimation and SpriteAnimationPlayer

Daniele Bartolini 11 年 前
コミット
ef76c6daba

+ 86 - 0
engine/world/sprite_animation.cpp

@@ -0,0 +1,86 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "sprite_animation.h"
+#include "log.h"
+
+namespace crown
+{
+
+SpriteAnimation::SpriteAnimation(const SpriteAnimationResource* sar)
+	: m_resource(sar)
+	, m_animation(NULL)
+	, m_frames(sprite_animation_resource::get_animation_frames(sar))
+	, m_cur_time(0)
+	, m_cur_frame(0)
+	, m_loop(false)
+{
+}
+
+void SpriteAnimation::play(StringId32 name, bool loop)
+{
+	if (m_animation)
+		return;
+
+	m_animation = sprite_animation_resource::get_animation(m_resource, name);
+	m_cur_time = 0;
+	m_cur_frame = 0;
+	m_loop = loop;
+}
+
+void SpriteAnimation::stop()
+{
+	m_animation = NULL;
+	m_cur_time = 0;
+	m_cur_frame = 0;
+}
+
+void SpriteAnimation::update(float dt)
+{
+	if (!m_animation)
+		return;
+
+	const uint32_t frame = m_animation->first_frame + uint32_t(m_animation->num_frames * (m_cur_time / m_animation->time));
+
+	m_cur_frame = m_frames[frame];
+	m_cur_time += dt;
+
+	if (m_cur_time >= m_animation->time)
+	{
+		if (m_loop)
+		{
+			m_cur_time = 0;
+			return;
+		}
+		else
+		{
+			stop();
+			return;
+		}
+	}
+}
+
+} // namespace crown

+ 53 - 0
engine/world/sprite_animation.h

@@ -0,0 +1,53 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "sprite_resource.h"
+
+namespace crown
+{
+
+struct SpriteAnimation
+{
+	SpriteAnimation(const SpriteAnimationResource* sar);
+
+	void play(StringId32 name, bool loop);
+	void stop();
+
+	void update(float dt);
+
+public:
+
+	const SpriteAnimationResource* m_resource;
+	const SpriteAnimationData* m_animation;
+	const uint32_t* m_frames;
+	float m_cur_time;
+	uint32_t m_cur_frame;
+	bool m_loop;
+};
+
+} // namespace crown

+ 72 - 0
engine/world/sprite_animation_player.cpp

@@ -0,0 +1,72 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "sprite_animation_player.h"
+#include "array.h"
+#include "memory.h"
+
+namespace crown
+{
+
+SpriteAnimationPlayer::SpriteAnimationPlayer()
+	: m_animations(default_allocator())
+{
+}
+
+SpriteAnimation* SpriteAnimationPlayer::create_sprite_animation(const SpriteAnimationResource* sar)
+{
+	SpriteAnimation* anim = CE_NEW(default_allocator(), SpriteAnimation)(sar);
+	array::push_back(m_animations, anim);
+	return anim;
+}
+
+void SpriteAnimationPlayer::destroy_sprite_animation(SpriteAnimation* anim)
+{
+	const uint32_t num = array::size(m_animations);
+
+	for (uint32_t i = 0; i < num; i++)
+	{
+		if (anim == m_animations[i])
+		{
+			CE_DELETE(default_allocator(), anim);
+			m_animations[i] = m_animations[num - 1];
+			array::pop_back(m_animations);
+			return;
+		}
+	}
+}
+
+void SpriteAnimationPlayer::update(float dt)
+{
+	const uint32_t num = array::size(m_animations);
+
+	for (uint32_t i = 0; i < num; i++)
+	{
+		m_animations[i]->update(dt);
+	}
+}
+
+} // namespace crown

+ 50 - 0
engine/world/sprite_animation_player.h

@@ -0,0 +1,50 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "sprite_resource.h"
+#include "container_types.h"
+#include "sprite_animation.h"
+
+namespace crown
+{
+
+struct SpriteAnimationPlayer
+{
+	SpriteAnimationPlayer();
+
+	SpriteAnimation* create_sprite_animation(const SpriteAnimationResource* sar);
+	void destroy_sprite_animation(SpriteAnimation* anim);
+
+	void update(float dt);
+
+private:
+
+	Array<SpriteAnimation*> m_animations;
+};
+
+} // namespace crown

+ 15 - 2
engine/world/world.cpp

@@ -166,10 +166,17 @@ Camera* World::get_camera(CameraId id)
 //-----------------------------------------------------------------------------
 void World::update(float dt)
 {
+	m_sprite_animation_player.update(dt);
+
 	m_physics_world.update(dt);
 
 	m_scenegraph_manager.update();
 
+	for (uint32_t i = 0; i < id_array::size(m_units); i++)
+	{
+		m_units[i]->update();
+	}
+
 	m_sound_world->update();
 
 	process_physics_events();
@@ -255,9 +262,9 @@ void World::set_sound_volume(SoundInstanceId id, float vol)
 }
 
 //-----------------------------------------------------------------------------
-GuiId World::create_window_gui(uint16_t width, uint16_t height)
+GuiId World::create_window_gui(uint16_t width, uint16_t height, const char* material)
 {
-	return m_render_world.create_gui(width, height);
+	return m_render_world.create_gui(width, height, material);
 }
 
 //-----------------------------------------------------------------------------
@@ -307,6 +314,12 @@ SceneGraphManager* World::scene_graph_manager()
 {
 	return &m_scenegraph_manager;
 }
+
+SpriteAnimationPlayer* World::sprite_animation_player()
+{
+	return &m_sprite_animation_player;
+}
+
 //-----------------------------------------------------------------------------
 RenderWorld* World::render_world()
 {

+ 14 - 11
engine/world/world.h

@@ -41,6 +41,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "world_types.h"
 #include "sound_world.h"
 #include "event_stream.h"
+#include "sprite_animation_player.h"
 
 namespace crown
 {
@@ -127,7 +128,7 @@ public:
 	void set_sound_volume(SoundInstanceId id, float volume);
 
 	/// Creates a new window-space Gui of size @width and @a height.
-	GuiId create_window_gui(uint16_t width, uint16_t height);
+	GuiId create_window_gui(uint16_t width, uint16_t height, const char* material);
 
 	/// Destroys the gui with the given @a id.
 	void destroy_gui(GuiId id);
@@ -142,6 +143,7 @@ public:
 	void load_level(const char* name);
 
 	SceneGraphManager* scene_graph_manager();
+	SpriteAnimationPlayer* sprite_animation_player();
 
 	/// Returns the rendering sub-world.
 	RenderWorld* render_world();
@@ -158,20 +160,21 @@ private:
 
 private:
 
-	PoolAllocator						m_unit_pool;
-	PoolAllocator						m_camera_pool;
+	PoolAllocator m_unit_pool;
+	PoolAllocator m_camera_pool;
 
-	IdArray<CE_MAX_UNITS, Unit*>		m_units;
-	IdArray<CE_MAX_CAMERAS, Camera*>	m_cameras;
+	IdArray<CE_MAX_UNITS, Unit*> m_units;
+	IdArray<CE_MAX_CAMERAS, Camera*> m_cameras;
 
-	SceneGraphManager					m_scenegraph_manager;
-	RenderWorld							m_render_world;
-	PhysicsWorld						m_physics_world;
-	SoundWorld*							m_sound_world;
+	SceneGraphManager m_scenegraph_manager;
+	SpriteAnimationPlayer m_sprite_animation_player;
+	RenderWorld m_render_world;
+	PhysicsWorld m_physics_world;
+	SoundWorld* m_sound_world;
 
-	WorldId								m_id;
+	WorldId m_id;
 
-	EventStream							m_events;
+	EventStream m_events;
 };
 
 } // namespace crown