GameWorld.hpp
#pragma once
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/System/Time.hpp>
class GameWorld
{
public:
explicit GameWorld( sf::RenderWindow& window );
void update( sf::Time dt );
void draw();
private:
void loadTextures();
void buildScene();
sf::Sprite _playerSprite;
sf::Sprite _floorSprite;
sf::Texture _playerTexture;
sf::Texture _floorTexture;
sf::RenderWindow& _window;
sf::Vector2f _spawnPosition;
sf::FloatRect _worldBounds;
sf::View _worldView;
};
GameWorld.cpp
#include <app/GameWorld.hpp>
#include <iostream>
GameWorld::GameWorld( sf::RenderWindow& window)
: _window( window )
, _playerSprite()
, _playerTexture()
, _floorSprite()
, _floorTexture()
, _worldView( window.getDefaultView() )
, _worldBounds( 0.f, 0.f, 1600.f, 1200.f )
//, _spawnPosition( 800.f, 600.f )
, _spawnPosition( _worldBounds.width / 2.f, _worldBounds.height / 2.f )
{
loadTextures();
buildScene();
std::cout << "_worldView.x = " << std::to_string( _worldView.getSize().x ) << std::endl;
std::cout << "_worldView.y = " << std::to_string( _worldView.getSize().y ) << std::endl;
std::cout << "_worldView.center.x = " << std::to_string( _worldView.getCenter().x ) << std::endl;
std::cout << "_worldView.center.y = " << std::to_string( _worldView.getCenter().y ) << std::endl;
std::cout << "_playerSprite.x = " << std::to_string( _playerSprite.getPosition().x ) << std::endl;
std::cout << "_playerSprite.y = " << std::to_string( _playerSprite.getPosition().y ) << std::endl;
std::cout << "_spawnPosition.x = " << std::to_string( _spawnPosition.x ) << std::endl;
std::cout << "_spawnPosition.y = " << std::to_string( _spawnPosition.y ) << std::endl;
std::cout << "_worldBounds.x = " << std::to_string( _worldBounds.width ) << std::endl;
std::cout << "_worldBounds.y = " << std::to_string( _worldBounds.height ) << std::endl;
}
void GameWorld::update( const sf::Time dt )
{
_worldView.move( 25.f * dt.asSeconds(), 25.f * dt.asSeconds() );
}
void GameWorld::draw()
{
_window.setView( _worldView );
_window.draw( _floorSprite );
_window.draw( _playerSprite );
}
void GameWorld::loadTextures()
{
if( !_playerTexture.loadFromFile( "../media/male.png" ) ) {
throw std::runtime_error( "Could not load image file 'male.png'" );
}
if( !_floorTexture.loadFromFile( "../media/grass.png" ) ) {
throw std::runtime_error( "Could not load image file 'grass.png'" );
}
}
void GameWorld::buildScene()
{
_playerSprite.setTexture( _playerTexture );
auto bounds = _playerSprite.getLocalBounds();
_playerSprite.setOrigin( bounds.width / 2.f, bounds.height / 2.f );
_playerSprite.setPosition( _spawnPosition );
_floorTexture.setRepeated( true );
_floorSprite.setTexture( _floorTexture );
_floorSprite.setTextureRect( sf::IntRect( _worldBounds ) );
_floorSprite.setPosition( _worldBounds.left, _worldBounds.top );
_worldView.setCenter( _spawnPosition );
}
Application.cpp
#include <app/Application.hpp>
#include <SFML/Window/Event.hpp>
Application::Application()
: _window( { 800, 600 }, "Game Demo", sf::Style::Close )
, _timeStep( sf::seconds( 1.f / 60.f ) )
, _gameWorld( _window )
{
}
void Application::run()
{
sf::Clock appClock;
sf::Time timeSinceLastFrame{ sf::Time::Zero };
while( _window.isOpen() ) {
sf::Time dt = appClock.restart();
timeSinceLastFrame += dt;
while( timeSinceLastFrame > _timeStep ) {
timeSinceLastFrame -= _timeStep;
handleEvents();
update( _timeStep );
}
render();
}
}
void Application::handleEvents()
{
sf::Event evt;
while( _window.pollEvent( evt ) ) {
if( evt.type == sf::Event::Closed ) {
_window.close();
}
}
}
void Application::update( const sf::Time dt)
{
_gameWorld.update( dt );
}
void Application::render()
{
_window.clear();
_gameWorld.draw();
_window.display();
}