Compare commits
2 Commits
67d5830da5
...
fce1de6303
Author | SHA1 | Date |
---|---|---|
Johannes Hendrik Gerard van der Weide | fce1de6303 | |
Johannes Hendrik Gerard van der Weide | 59560ced66 |
Binary file not shown.
After Width: | Height: | Size: 54 KiB |
|
@ -12,5 +12,11 @@ class Game
|
|||
|
||||
private:
|
||||
sf::RenderWindow mWindow;
|
||||
sf::CircleShape mPlayer;
|
||||
sf::Texture mTexture;
|
||||
sf::Sprite mPlayer;
|
||||
|
||||
bool mIsMovingUp{false};
|
||||
bool mIsMovingDown{false};
|
||||
bool mIsMovingLeft{false};
|
||||
bool mIsMovingRight{false};
|
||||
};
|
||||
|
|
|
@ -6,22 +6,22 @@ const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);
|
|||
|
||||
Game::Game()
|
||||
: mWindow(sf::VideoMode(640, 480), "SFML Application")
|
||||
, mPlayer()
|
||||
{
|
||||
mPlayer.setRadius(40.f);
|
||||
, mTexture()
|
||||
, mPlayer() {
|
||||
if(!mTexture.loadFromFile("Assets/blobcat_hertog.png")) {
|
||||
// Handle loading errors
|
||||
}
|
||||
mPlayer.setTexture(mTexture);
|
||||
mPlayer.setScale(0.25f, 0.25f);
|
||||
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,13 +30,10 @@ 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);
|
||||
|
@ -51,8 +48,7 @@ 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)
|
||||
|
@ -63,30 +59,27 @@ 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 -= 1.f;
|
||||
movement.y -= 100.f;
|
||||
if (mIsMovingDown)
|
||||
movement.y += 1.f;
|
||||
movement.y += 100.f;
|
||||
if (mIsMovingLeft)
|
||||
movement.x -= 1.f;
|
||||
movement.x -= 100.f;
|
||||
if (mIsMovingRight)
|
||||
movement.x += 1.f;
|
||||
movement.x += 100.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();
|
||||
}
|
Loading…
Reference in New Issue