瀏覽代碼

Invader sample: Fix score blinking for one frame on update

Moves the score change to the Update call so that the context is updated after the score RML is changed.
Michael Ragazzon 3 年之前
父節點
當前提交
9acde9afa1

+ 37 - 34
Samples/invaders/src/Defender.cpp

@@ -79,14 +79,7 @@ void Defender::Update(double t)
 	else if (position.x > (game->GetWindowDimensions().x - SPRITE_WIDTH - 5))
 		position.x = game->GetWindowDimensions().x - SPRITE_WIDTH - 5;
 
-	// Update the bullet
-	if (bullet_in_flight)
-	{
-		// Move it up and mark it dead if it flies off the top of the screen
-		bullet_position.y -= BULLET_SPEED;
-		if (bullet_position.y < 0)
-			bullet_in_flight = false;
-	}
+	UpdateBullet(t);
 
 	if (state == RESPAWN)
 	{	
@@ -102,7 +95,7 @@ void Defender::Update(double t)
 	}
 }
 
-void Defender::Render(double t, float dp_ratio, Rml::TextureHandle texture)
+void Defender::Render(float dp_ratio, Rml::TextureHandle texture)
 {
 	Rml::Colourb color = GameDetails::GetDefenderColour();
 
@@ -110,33 +103,9 @@ void Defender::Render(double t, float dp_ratio, Rml::TextureHandle texture)
 	if (render)
 		defender_sprite.Render(position, dp_ratio, color, texture);
 
-	// Update the bullet, doing collision detection
+	// Render the bullet
 	if (bullet_in_flight)
-	{
 		bullet_sprite.Render(bullet_position, dp_ratio, color, texture);
-
-		// Check if we hit the shields
-		for (int i = 0; i < game->GetNumShields(); i++)
-		{
-			if (game->GetShield(i)->CheckHit(bullet_position))
-			{
-				bullet_in_flight = false;
-				break;
-			}
-		}
-
-		if (bullet_in_flight)
-		{
-			for (int i = 0; i < game->GetNumInvaders(); i++)
-			{
-				if (game->GetInvader(i)->CheckHit(t, bullet_position))
-				{
-					bullet_in_flight = false;
-					break;
-				}
-			}
-		}
-	}
 }
 
 void Defender::StartMove(float direction)
@@ -182,3 +151,37 @@ bool Defender::CheckHit(double t, const Rml::Vector2f& check_position)
 
 	return false;
 }
+
+void Defender::UpdateBullet(double t)
+{
+	if (bullet_in_flight)
+	{
+		// Move the bullet up and mark it dead if it flies off the top of the screen
+		bullet_position.y -= BULLET_SPEED;
+		if (bullet_position.y < 0)
+		{
+			bullet_in_flight = false;
+			return;
+		}
+
+		// Check if we hit the shields
+		for (int i = 0; i < game->GetNumShields(); i++)
+		{
+			if (game->GetShield(i)->CheckHit(bullet_position))
+			{
+				bullet_in_flight = false;
+				return;
+			}
+		}
+
+		// Check if we hit any invaders
+		for (int i = 0; i < game->GetNumInvaders(); i++)
+		{
+			if (game->GetInvader(i)->CheckHit(t, bullet_position))
+			{
+				bullet_in_flight = false;
+				return;
+			}
+		}
+	}
+}

+ 5 - 2
Samples/invaders/src/Defender.h

@@ -48,19 +48,22 @@ public:
 	/// Update the defender state.
 	void Update(double t);
 	/// Render the defender.
-	void Render(double t, float dp_ratio, Rml::TextureHandle texture);
+	void Render(float dp_ratio, Rml::TextureHandle texture);
 
 	/// Move the defender left.
 	void StartMove(float direction);	
 	/// Stop the movement.
 	void StopMove(float direction);
 	/// Fire a bullet (if one isn't already in flight).
-	void Fire();	
+	void Fire();
 
 	/// Check if an object at the given position would hit the defender.
 	bool CheckHit(double t, const Rml::Vector2f& position);
 
 private:
+	/// Update the bullet, doing collision detection.
+	void UpdateBullet(double t);
+
 	Game* game;
 	Rml::Vector2f position;	
 	

+ 1 - 1
Samples/invaders/src/ElementGame.cpp

@@ -95,7 +95,7 @@ void ElementGame::OnUpdate()
 // Renders the game.
 void ElementGame::OnRender()
 {
-	game->Render(Rml::GetSystemInterface()->GetElapsedTime(), GetContext()->GetDensityIndependentPixelRatio());
+	game->Render(GetContext()->GetDensityIndependentPixelRatio());
 }
 
 void ElementGame::OnChildAdd(Rml::Element* element)

+ 2 - 2
Samples/invaders/src/Game.cpp

@@ -139,7 +139,7 @@ void Game::Update(double t)
 	}
 }
 
-void Game::Render(double t, float dp_ratio)
+void Game::Render(float dp_ratio)
 {	
 	if (defender_lives <= 0)
 		return;
@@ -154,7 +154,7 @@ void Game::Render(double t, float dp_ratio)
 	for (int i = 0; i < NUM_INVADERS + 1; i++)
 		invaders[i]->Render(dp_ratio, texture_handle);
 
-	defender->Render(t, dp_ratio, texture_handle);
+	defender->Render(dp_ratio, texture_handle);
 }
 
 Defender* Game::GetDefender()

+ 1 - 1
Samples/invaders/src/Game.h

@@ -58,7 +58,7 @@ public:
 	void Update(double t);
 
 	/// Render the game
-	void Render(double t, float dp_ratio);
+	void Render(float dp_ratio);
 
 	/// Access the defender
 	Defender* GetDefender();

+ 37 - 34
Samples/luainvaders/src/Defender.cpp

@@ -79,14 +79,7 @@ void Defender::Update(double t)
 	else if (position.x > (game->GetWindowDimensions().x - SPRITE_WIDTH - 5))
 		position.x = game->GetWindowDimensions().x - SPRITE_WIDTH - 5;
 
-	// Update the bullet
-	if (bullet_in_flight)
-	{
-		// Move it up and mark it dead if it flies off the top of the screen
-		bullet_position.y -= BULLET_SPEED;
-		if (bullet_position.y < 0)
-			bullet_in_flight = false;
-	}
+	UpdateBullet(t);
 
 	if (state == RESPAWN)
 	{	
@@ -102,7 +95,7 @@ void Defender::Update(double t)
 	}
 }
 
-void Defender::Render(double t, float dp_ratio, Rml::TextureHandle texture)
+void Defender::Render(float dp_ratio, Rml::TextureHandle texture)
 {
 	Rml::Colourb color = GameDetails::GetDefenderColour();
 
@@ -110,33 +103,9 @@ void Defender::Render(double t, float dp_ratio, Rml::TextureHandle texture)
 	if (render)
 		defender_sprite.Render(position, dp_ratio, color, texture);
 
-	// Update the bullet, doing collision detection
+	// Render the bullet
 	if (bullet_in_flight)
-	{
 		bullet_sprite.Render(bullet_position, dp_ratio, color, texture);
-
-		// Check if we hit the shields
-		for (int i = 0; i < game->GetNumShields(); i++)
-		{
-			if (game->GetShield(i)->CheckHit(bullet_position))
-			{
-				bullet_in_flight = false;
-				break;
-			}
-		}
-
-		if (bullet_in_flight)
-		{
-			for (int i = 0; i < game->GetNumInvaders(); i++)
-			{
-				if (game->GetInvader(i)->CheckHit(t, bullet_position))
-				{
-					bullet_in_flight = false;
-					break;
-				}
-			}
-		}
-	}
 }
 
 void Defender::StartMove(float direction)
@@ -182,3 +151,37 @@ bool Defender::CheckHit(double t, const Rml::Vector2f& check_position)
 
 	return false;
 }
+
+void Defender::UpdateBullet(double t)
+{
+	if (bullet_in_flight)
+	{
+		// Move the bullet up and mark it dead if it flies off the top of the screen
+		bullet_position.y -= BULLET_SPEED;
+		if (bullet_position.y < 0)
+		{
+			bullet_in_flight = false;
+			return;
+		}
+
+		// Check if we hit the shields
+		for (int i = 0; i < game->GetNumShields(); i++)
+		{
+			if (game->GetShield(i)->CheckHit(bullet_position))
+			{
+				bullet_in_flight = false;
+				return;
+			}
+		}
+
+		// Check if we hit any invaders
+		for (int i = 0; i < game->GetNumInvaders(); i++)
+		{
+			if (game->GetInvader(i)->CheckHit(t, bullet_position))
+			{
+				bullet_in_flight = false;
+				return;
+			}
+		}
+	}
+}

+ 5 - 2
Samples/luainvaders/src/Defender.h

@@ -48,19 +48,22 @@ public:
 	/// Update the defender state.
 	void Update(double t);
 	/// Render the defender.
-	void Render(double t, float dp_ratio, Rml::TextureHandle texture);
+	void Render(float dp_ratio, Rml::TextureHandle texture);
 
 	/// Move the defender left.
 	void StartMove(float direction);	
 	/// Stop the movement.
 	void StopMove(float direction);
 	/// Fire a bullet (if one isn't already in flight).
-	void Fire();	
+	void Fire();
 
 	/// Check if an object at the given position would hit the defender.
 	bool CheckHit(double t, const Rml::Vector2f& position);
 
 private:
+	/// Update the bullet, doing collision detection.
+	void UpdateBullet(double t);
+
 	Game* game;
 	Rml::Vector2f position;	
 	

+ 1 - 1
Samples/luainvaders/src/ElementGame.cpp

@@ -92,7 +92,7 @@ void ElementGame::OnUpdate()
 // Renders the game.
 void ElementGame::OnRender()
 {
-	game->Render(Rml::GetSystemInterface()->GetElapsedTime(), GetContext()->GetDensityIndependentPixelRatio());
+	game->Render(GetContext()->GetDensityIndependentPixelRatio());
 }
 
 void ElementGame::OnChildAdd(Rml::Element* element)

+ 3 - 3
Samples/luainvaders/src/Game.cpp

@@ -137,7 +137,7 @@ void Game::Update(double t)
 	}
 }
 
-void Game::Render(double t, float dp_ratio)
+void Game::Render(float dp_ratio)
 {	
 	if (defender_lives <= 0)
 		return;
@@ -151,8 +151,8 @@ void Game::Render(double t, float dp_ratio)
 	// Render all available invaders
 	for (int i = 0; i < NUM_INVADERS + 1; i++)
 		invaders[i]->Render(dp_ratio, texture_handle);
-	
-	defender->Render(t, dp_ratio, texture_handle);
+
+	defender->Render(dp_ratio, texture_handle);
 }
 
 Defender* Game::GetDefender()

+ 1 - 1
Samples/luainvaders/src/Game.h

@@ -58,7 +58,7 @@ public:
 	void Update(double t);
 
 	/// Render the game
-	void Render(double t, float dp_ratio);
+	void Render(float dp_ratio);
 
 	/// Access the defender
 	Defender* GetDefender();