Example #1
0
Sprite* SpriteManager::CreateSprite(const std::string& filename, int x, int y, int w, int h)
{
	// first we search for if the texture is already loaded
	auto it = m_textures.find(filename);
	if (it == m_textures.end())
	{
		// if not, we create a new one
		SDL_Surface* surface = IMG_Load(filename.c_str());
		SDL_Surface* optimizedSurface;

		if (surface != nullptr)
		{
			optimizedSurface = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ABGR4444, 0);
			SDL_FreeSurface(surface);

			SDL_Texture* texture = SDL_CreateTextureFromSurface(m_renderer, optimizedSurface);

			// we save the texture in the map for later reuse
			m_textures.insert(std::pair<std::string, SDL_Texture*>(filename, texture));

			// we can destroy the surface
			// since we now have a texture
			// that we use instead
			if (optimizedSurface != nullptr)
			{
				SDL_FreeSurface(optimizedSurface);
			}
			else
			{
				const char* error = SDL_GetError();
			}

			// now we get the texture so we can return it with a sprite
			it = m_textures.find(filename);
		}
	}

	// then we create a new sprite 
	// with the texture associated
	Sprite* sprite = new Sprite(it->second);
	sprite->GetRegion()->x = x;
	sprite->GetRegion()->y = y;
	sprite->GetRegion()->w = w;
	sprite->GetRegion()->h = h;
	m_sprites.push_back(sprite);

	// return the newly newed sprite
	return sprite;
}
Example #2
0
void GameState::Enter()
{
	////////////////////
	Sprite* xSprite = m_xSystem.m_pxSpriteManager->CreateSprite("../assets/main.bmp", 0, 0, 21, 8);
	SDL_Rect* xRect = xSprite->GetRegion();
	int iHeight = xRect->h;

	//Playership
	m_pxPlayerShip = new PlayerShip(m_xSystem.m_pxKeyboard, xSprite, m_xSystem.m_iScreenWidth / 2, 
		m_xSystem.m_iScreenHeight - 20 - iHeight, m_xSystem.m_iScreenWidth);

	//Player projectile
	m_pxProjectile = new Projectile(m_xSystem.m_pxKeyboard,
		m_xSystem.m_pxSpriteManager->CreateSprite("../assets/main.bmp", 0, 10, 1, 4),
		m_xSystem.m_iScreenWidth,
		m_xSystem.m_iScreenHeight);

	
	//Invaders
	SDL_Rect blockCoords[] = 
	{
		{23, 0, 21, 8},
		{46, 0, 21, 8},
		{69, 0, 21, 8}
	};
	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			SDL_Rect& rect = blockCoords[j % 3];
			Alien* pxAlien = new Alien(
				m_xSystem.m_pxSpriteManager->CreateSprite("../assets/main.bmp", rect.x, rect.y, rect.w, rect.h),
				10 + i * 50.0f,
				10 + j * 20);
			m_apxAlien.push_back(pxAlien);
		}
	}

	//Alien rektangel hitbox.
	auto rectit = m_apxAlien.begin();
	auto endrect = m_apxAlien.rbegin();
	int x = (*rectit)->GetX();
	int y = (*rectit)->GetY();
	int w = (*endrect)->GetX() + (*endrect)->GetSprite()->GetRegion()->w;
	int h = (*endrect)->GetY() + (*endrect)->GetSprite()->GetRegion()->h;
	m_pxAlienRect = new AlienRect(m_xSystem.m_pxSpriteManager->CreateSprite("../assets/main.bmp", 
		0, 40, w, h), x, y, w, h);
	
	//Alien skott
	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			m_pxAlienProj = new AlienProj(m_xSystem.m_pxSpriteManager->CreateSprite("../assets/main.bmp",
				3, 9, 2, 6), 
				10 + i * 50.0f,
				10 + j *20);
			m_apxAlienP.push_back(m_pxAlienProj);
		}
	}

	//Skydd
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			for (int x = 0; x < 7; x++)
			{
				m_pxCover = new Cover(m_xSystem.m_pxSpriteManager->CreateSprite("../assets/main.bmp",
					0, 27, 5, 9),
					100 + (i*125.0f) + x*6,
                    400 + j * 9.0f);
				m_apxCover.push_back(m_pxCover);
			}
			
		}
	}
	Sprite* xSpriteText = m_xSystem.m_pxSpriteManager->CreateSprite("../assets/main.bmp", 92, 0, 76, 21);

	m_pxLoseText = new LoseText(xSpriteText, m_xSystem.m_iScreenWidth / 2,
		m_xSystem.m_iScreenHeight / 2);
}