Open the Engine.cpp
file and add the highlighted code to load the sprite sheet texture at the end of the Engine
constructor:
Engine::Engine()
{
// Get the screen resolution and create an SFML window and View
Vector2f resolution;
resolution.x = VideoMode::getDesktopMode().width;
resolution.y = VideoMode::getDesktopMode().height;
m_Window.create(VideoMode(resolution.x, resolution.y),
"Thomas was late",
Style::Fullscreen);
// Initialize the full screen view
m_MainView.setSize(resolution);
m_HudView.reset(
FloatRect(0, 0, resolution.x, resolution.y));
// Inititialize the split-screen Views
m_LeftView.setViewport(
FloatRect(0.001f, 0.001f, 0.498f, 0.998f));
m_RightView.setViewport(
FloatRect(0.5f, 0.001f, 0.499f, 0.998f));
m_BGLeftView.setViewport(
FloatRect(0.001f, 0.001f, 0.498f, 0.998f));
m_BGRightView.setViewport(
FloatRect(0.5f, 0.001f, 0.499f, 0.998f));
// Can this graphics card use shaders?
if (!sf::Shader::isAvailable())
{
// Time to get a new PC
m_Window.close();
}
m_BackgroundTexture = TextureHolder::GetTexture(
"graphics/background.png");
// Associate the sprite with the texture
m_BackgroundSprite.setTexture(m_BackgroundTexture);
// Load the texture for the background vertex array
m_TextureTiles = TextureHolder::GetTexture("graphics/tiles_sheet.png");
}
All we do in the previous code is load the sprite sheet into m_TextureTiles
.
Open the Update.cpp
file and make the following highlighted changes and additions:
void Engine::update(float dtAsSeconds) { if (m_NewLevelRequired) { // These calls to spawn will be moved to a new // LoadLevel function soon // Spawn Thomas and Bob //m_Thomas.spawn(Vector2f(0,0), GRAVITY); //m_Bob.spawn(Vector2f(100, 0), GRAVITY); // Make sure spawn is called only once //m_TimeRemaining = 10; //m_NewLevelRequired = false; // Load a level loadLevel(); }
Actually, you should delete, rather than comment out, the lines we are no longer using. I have just shown it to you this way so that the changes are clear. All there should be in the previous if
statement is the call to loadLevel
.
Finally, before we can see the results of the work so far this chapter, open the Draw.cpp
file and make the following highlighted additions to draw the vertex array that represents a level:
void Engine::draw() { // Rub out the last frame m_Window.clear(Color::White); if (!m_SplitScreen) { // Switch to background view m_Window.setView(m_BGMainView); // Draw the background m_Window.draw(m_BackgroundSprite); // Switch to m_MainView m_Window.setView(m_MainView); // Draw the Level m_Window.draw(m_VALevel, &m_TextureTiles); // Draw thomas m_Window.draw(m_Thomas.getSprite()); // Draw thomas m_Window.draw(m_Bob.getSprite()); } else { // Split-screen view is active // First draw Thomas' side of the screen // Switch to background view m_Window.setView(m_BGLeftView); // Draw the background m_Window.draw(m_BackgroundSprite); // Switch to m_LeftView m_Window.setView(m_LeftView); // Draw the Level m_Window.draw(m_VALevel, &m_TextureTiles); // Draw thomas m_Window.draw(m_Bob.getSprite()); // Draw thomas m_Window.draw(m_Thomas.getSprite()); // Now draw Bob's side of the screen // Switch to background view m_Window.setView(m_BGRightView); // Draw the background m_Window.draw(m_BackgroundSprite); // Switch to m_RightView m_Window.setView(m_RightView); // Draw the Level m_Window.draw(m_VALevel, &m_TextureTiles); // Draw thomas m_Window.draw(m_Thomas.getSprite()); // Draw bob m_Window.draw(m_Bob.getSprite()); } // Draw the HUD // Switch to m_HudView m_Window.setView(m_HudView); // Show everything we have just drawn m_Window.display(); }
Notice we need to draw the VertexArray
for all screen options (full, left, and right.)
Now you can run the game. Unfortunately, however, Thomas and Bob fall straight through all our lovingly-designed platforms. For this reason, we can't try and progress through the levels and beat the clock.
3.144.12.249