Compare commits

..

No commits in common. "fce1de630345e376ea050b3e0175b9ff74e2a2c6" and "67d5830da52d6afd808f6555b0cfe97662a3e47b" have entirely different histories.

3 changed files with 29 additions and 28 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

View File

@ -12,11 +12,5 @@ class Game
private:
sf::RenderWindow mWindow;
sf::Texture mTexture;
sf::Sprite mPlayer;
bool mIsMovingUp{false};
bool mIsMovingDown{false};
bool mIsMovingLeft{false};
bool mIsMovingRight{false};
sf::CircleShape mPlayer;
};

View File

@ -6,22 +6,22 @@ const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);
Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application")
, mTexture()
, mPlayer() {
if(!mTexture.loadFromFile("Assets/blobcat_hertog.png")) {
// Handle loading errors
}
mPlayer.setTexture(mTexture);
mPlayer.setScale(0.25f, 0.25f);
, mPlayer()
{
mPlayer.setRadius(40.f);
mPlayer.setPosition(100.f, 100.f);
mPlayer.setFillColor(sf::Color::Cyan);
}
void Game::run() {
void Game::run()
{
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (mWindow.isOpen()) {
while (mWindow.isOpen())
{
processEvents();
timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > TimePerFrame) {
while (timeSinceLastUpdate > TimePerFrame)
{
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
@ -30,10 +30,13 @@ void Game::run() {
}
}
void Game::processEvents() {
void Game::processEvents()
{
sf::Event event;
while (mWindow.pollEvent(event)) {
switch (event.type) {
while (mWindow.pollEvent(event))
{
switch (event.type)
{
//For each time the while loop iterates, it means a new event that was registered by the window is being handled. While there can be many different events, we will only check for some types of events, which are of our interest right now.
case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true);
@ -48,7 +51,8 @@ void Game::processEvents() {
}
}
void Game::handlePlayerInput(sf::Keyboard::Key key,bool isPressed) {
void Game::handlePlayerInput(sf::Keyboard::Key key,bool isPressed)
{
if (key == sf::Keyboard::W)
mIsMovingUp = isPressed;
else if (key == sf::Keyboard::S)
@ -59,27 +63,30 @@ void Game::handlePlayerInput(sf::Keyboard::Key key,bool isPressed) {
mIsMovingRight = isPressed;
}
void Game::update(sf::Time deltaTime) {
void Game::update(sf::Time deltaTime)
{
sf::Vector2f movement(0.f, 0.f);
if (mIsMovingUp)
movement.y -= 100.f;
movement.y -= 1.f;
if (mIsMovingDown)
movement.y += 100.f;
movement.y += 1.f;
if (mIsMovingLeft)
movement.x -= 100.f;
movement.x -= 1.f;
if (mIsMovingRight)
movement.x += 100.f;
movement.x += 1.f;
mPlayer.move(movement * deltaTime.asSeconds());
}
void Game::render() {
void Game::render()
{
mWindow.clear();
mWindow.draw(mPlayer);
mWindow.display();
}
int main() {
int main()
{
Game game;
game.run();
}