← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~qcumber-some/widelands/animations into lp:widelands

 

Jens Beyer (Qcumber-some) has proposed merging lp:~qcumber-some/widelands/animations into lp:widelands.

Requested reviews:
  Widelands Developers (widelands-dev)

For more details, see:
https://code.launchpad.net/~qcumber-some/widelands/animations/+merge/104138

I'm feeling I should add a review request to further discuss this branch, instead of discussing it in the bug.
This is about bug #535806 and is an attempt to use on-demand loading of the animations, instead of preloading them upon game start.

Please note that this is not a 'merge request' per se, but a review request on the technology used and the possible outcomes. The branch in its current state is not up for merge yet.
-- 
https://code.launchpad.net/~qcumber-some/widelands/animations/+merge/104138
Your team Widelands Developers is requested to review the proposed merge of lp:~qcumber-some/widelands/animations into lp:widelands.
=== modified file 'src/graphic/graphic.cc'
--- src/graphic/graphic.cc	2012-03-09 16:57:20 +0000
+++ src/graphic/graphic.cc	2012-05-09 21:40:27 +0000
@@ -1066,32 +1066,25 @@
 void Graphic::load_animations(UI::ProgressWindow & loader_ui) {
 	assert(m_animations.empty());
 
-	clock_t start = clock();
-
-	const std::string step_description = _("Loading animations: %d%% complete");
-	uint32_t last_shown = 100;
 	const uint32_t nr_animations = g_anim.get_nranimations();
-	for (uint32_t id = 0; id < nr_animations;) {
-		const uint32_t percent = 100 * id / nr_animations;
-		if (percent != last_shown) {
-			last_shown = percent;
-			loader_ui.stepf(step_description.c_str(), percent);
-		}
-		++id;
-		m_animations.push_back(new AnimationGfx(g_anim.get_animation(id)));
-	}
-	loader_ui.step(std::string());
+	m_animations.reserve(nr_animations);
+}
 
-	clock_t end = clock();
-	printf
-		("load_animations took %f seconds\n",
-		 (float(end - start) / CLOCKS_PER_SEC));
+void Graphic::ensure_animation_loaded(uint32_t const anim) {
+	if (anim >= m_animations.size()) {
+		m_animations.resize(anim + 1);
+	}
+	if (!m_animations.at(anim - 1))
+	{
+	  log("Loading animation %i\n", anim);
+	  m_animations.at(anim - 1) = new AnimationGfx(g_anim.get_animation(anim));
+	}
 }
 
 /**
  * Return the number of frames in this animation
  */
-AnimationGfx::Index Graphic::nr_frames(const uint32_t anim) const
+AnimationGfx::Index Graphic::nr_frames(const uint32_t anim)
 {
 	return get_animation(anim)->nr_frames();
 }
@@ -1100,7 +1093,7 @@
  * writes the size of an animation frame to w and h
 */
 void Graphic::get_animation_size
-	(uint32_t const anim, uint32_t const time, uint32_t & w, uint32_t & h) const
+	(uint32_t const anim, uint32_t const time, uint32_t & w, uint32_t & h)
 {
 	AnimationData const * const data = g_anim.get_animation(anim);
 	AnimationGfx  const * const gfx  =        get_animation(anim);
@@ -1156,11 +1149,12 @@
  * @param anim the number of the animation
  * @return the AnimationGfs object of the given number
  */
-AnimationGfx * Graphic::get_animation(uint32_t const anim) const
+AnimationGfx * Graphic::get_animation(uint32_t const anim)
 {
-	if (!anim || anim > m_animations.size())
+	if (!anim)
 		return 0;
 
+	ensure_animation_loaded(anim);
 	return m_animations[anim - 1];
 }
 

=== modified file 'src/graphic/graphic.h'
--- src/graphic/graphic.h	2012-02-21 13:42:13 +0000
+++ src/graphic/graphic.h	2012-05-09 21:40:27 +0000
@@ -178,18 +178,19 @@
 	void reset_texture_animation_reminder();
 
 	void load_animations(UI::ProgressWindow & loader_ui);
-	AnimationGfx::Index nr_frames(uint32_t const anim = 0) const;
+	void ensure_animation_loaded(uint32_t anim);
+	AnimationGfx::Index nr_frames(uint32_t const anim = 0);
 	uint32_t get_animation_frametime(uint32_t anim) const;
 	void get_animation_size
 		(const uint32_t anim,
 		 const uint32_t time,
 		 uint32_t & w,
 		 uint32_t & h)
-		const;
+		;
 
 	void screenshot(const char & fname) const;
 	Texture * get_maptexture_data(uint32_t id);
-	AnimationGfx * get_animation(uint32_t) const;
+	AnimationGfx * get_animation(uint32_t);
 
 	void set_world(std::string);
 	PictureID get_road_texture(int32_t roadtex);


Follow ups