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

Made single get texture function

Sanjay Madhav 8 лет назад
Родитель
Сommit
15e683160d
2 измененных файлов с 24 добавлено и 35 удалено
  1. 23 33
      Chapter02/Game.cpp
  2. 1 2
      Chapter02/Game.h

+ 23 - 33
Chapter02/Game.cpp

@@ -153,16 +153,6 @@ void Game::GenerateOutput()
 
 void Game::LoadData()
 {
-	// Load textures
-	LoadTexture("Assets/Laser.png");
-	LoadTexture("Assets/Ship01.png");
-	LoadTexture("Assets/Ship02.png");
-	LoadTexture("Assets/Ship03.png");
-	LoadTexture("Assets/Ship04.png");
-	LoadTexture("Assets/Farback01.png");
-	LoadTexture("Assets/Farback02.png");
-	LoadTexture("Assets/Stars.png");
-
 	// Create player's ship
 	mShip = new Ship(this);
 	mShip->SetPosition(Vector2(100.0f, 384.0f));
@@ -208,36 +198,36 @@ void Game::UnloadData()
 	mTextures.clear();
 }
 
-void Game::LoadTexture(const char* fileName)
-{
-	// Load from file
-	SDL_Surface* surf = IMG_Load(fileName);
-	if (!surf)
-	{
-		SDL_Log("Failed to load texture file %s", fileName);
-		return;
-	}
-
-	// Create texture from surface
-	SDL_Texture* text = SDL_CreateTextureFromSurface(mRenderer, surf);
-	SDL_FreeSurface(surf);
-	if (!text)
-	{
-		SDL_Log("Failed to convert surface to texture for %s", fileName);
-		return;
-	}
-	
-	mTextures.emplace(fileName, text);
-}
-
-SDL_Texture * Game::GetTexture(const char * fileName)
+SDL_Texture* Game::GetTexture(const std::string& fileName)
 {
 	SDL_Texture* tex = nullptr;
+	// Is the texture already in the map?
 	auto iter = mTextures.find(fileName);
 	if (iter != mTextures.end())
 	{
 		tex = iter->second;
 	}
+	else
+	{
+		// Load from file
+		SDL_Surface* surf = IMG_Load(fileName.c_str());
+		if (!surf)
+		{
+			SDL_Log("Failed to load texture file %s", fileName.c_str());
+			return nullptr;
+		}
+
+		// Create texture from surface
+		tex = SDL_CreateTextureFromSurface(mRenderer, surf);
+		SDL_FreeSurface(surf);
+		if (!tex)
+		{
+			SDL_Log("Failed to convert surface to texture for %s", fileName.c_str());
+			return nullptr;
+		}
+
+		mTextures.emplace(fileName.c_str(), tex);
+	}
 	return tex;
 }
 

+ 1 - 2
Chapter02/Game.h

@@ -26,8 +26,7 @@ public:
 	void AddSprite(class SpriteComponent* sprite);
 	void RemoveSprite(class SpriteComponent* sprite);
 	
-	void LoadTexture(const char* fileName);
-	SDL_Texture* GetTexture(const char* fileName);
+	SDL_Texture* GetTexture(const std::string& fileName);
 private:
 	void ProcessInput();
 	void UpdateGame();