Compare commits
No commits in common. "fce1de630345e376ea050b3e0175b9ff74e2a2c6" and "67d5830da52d6afd808f6555b0cfe97662a3e47b" have entirely different histories.
fce1de6303
...
67d5830da5
Binary file not shown.
Before Width: | Height: | Size: 54 KiB |
|
@ -12,11 +12,5 @@ class Game
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sf::RenderWindow mWindow;
|
sf::RenderWindow mWindow;
|
||||||
sf::Texture mTexture;
|
sf::CircleShape mPlayer;
|
||||||
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()
|
Game::Game()
|
||||||
: mWindow(sf::VideoMode(640, 480), "SFML Application")
|
: mWindow(sf::VideoMode(640, 480), "SFML Application")
|
||||||
, mTexture()
|
, mPlayer()
|
||||||
, mPlayer() {
|
{
|
||||||
if(!mTexture.loadFromFile("Assets/blobcat_hertog.png")) {
|
mPlayer.setRadius(40.f);
|
||||||
// Handle loading errors
|
|
||||||
}
|
|
||||||
mPlayer.setTexture(mTexture);
|
|
||||||
mPlayer.setScale(0.25f, 0.25f);
|
|
||||||
mPlayer.setPosition(100.f, 100.f);
|
mPlayer.setPosition(100.f, 100.f);
|
||||||
|
mPlayer.setFillColor(sf::Color::Cyan);
|
||||||
}
|
}
|
||||||
void Game::run() {
|
void Game::run()
|
||||||
|
{
|
||||||
sf::Clock clock;
|
sf::Clock clock;
|
||||||
sf::Time timeSinceLastUpdate = sf::Time::Zero;
|
sf::Time timeSinceLastUpdate = sf::Time::Zero;
|
||||||
while (mWindow.isOpen()) {
|
while (mWindow.isOpen())
|
||||||
|
{
|
||||||
processEvents();
|
processEvents();
|
||||||
timeSinceLastUpdate += clock.restart();
|
timeSinceLastUpdate += clock.restart();
|
||||||
while (timeSinceLastUpdate > TimePerFrame) {
|
while (timeSinceLastUpdate > TimePerFrame)
|
||||||
|
{
|
||||||
timeSinceLastUpdate -= TimePerFrame;
|
timeSinceLastUpdate -= TimePerFrame;
|
||||||
processEvents();
|
processEvents();
|
||||||
update(TimePerFrame);
|
update(TimePerFrame);
|
||||||
|
@ -30,10 +30,13 @@ void Game::run() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::processEvents() {
|
void Game::processEvents()
|
||||||
|
{
|
||||||
sf::Event event;
|
sf::Event event;
|
||||||
while (mWindow.pollEvent(event)) {
|
while (mWindow.pollEvent(event))
|
||||||
switch (event.type) {
|
{
|
||||||
|
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.
|
//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:
|
case sf::Event::KeyPressed:
|
||||||
handlePlayerInput(event.key.code, true);
|
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)
|
if (key == sf::Keyboard::W)
|
||||||
mIsMovingUp = isPressed;
|
mIsMovingUp = isPressed;
|
||||||
else if (key == sf::Keyboard::S)
|
else if (key == sf::Keyboard::S)
|
||||||
|
@ -59,27 +63,30 @@ void Game::handlePlayerInput(sf::Keyboard::Key key,bool isPressed) {
|
||||||
mIsMovingRight = isPressed;
|
mIsMovingRight = isPressed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::update(sf::Time deltaTime) {
|
void Game::update(sf::Time deltaTime)
|
||||||
|
{
|
||||||
sf::Vector2f movement(0.f, 0.f);
|
sf::Vector2f movement(0.f, 0.f);
|
||||||
if (mIsMovingUp)
|
if (mIsMovingUp)
|
||||||
movement.y -= 100.f;
|
movement.y -= 1.f;
|
||||||
if (mIsMovingDown)
|
if (mIsMovingDown)
|
||||||
movement.y += 100.f;
|
movement.y += 1.f;
|
||||||
if (mIsMovingLeft)
|
if (mIsMovingLeft)
|
||||||
movement.x -= 100.f;
|
movement.x -= 1.f;
|
||||||
if (mIsMovingRight)
|
if (mIsMovingRight)
|
||||||
movement.x += 100.f;
|
movement.x += 1.f;
|
||||||
|
|
||||||
mPlayer.move(movement * deltaTime.asSeconds());
|
mPlayer.move(movement * deltaTime.asSeconds());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::render() {
|
void Game::render()
|
||||||
|
{
|
||||||
mWindow.clear();
|
mWindow.clear();
|
||||||
mWindow.draw(mPlayer);
|
mWindow.draw(mPlayer);
|
||||||
mWindow.display();
|
mWindow.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main()
|
||||||
|
{
|
||||||
Game game;
|
Game game;
|
||||||
game.run();
|
game.run();
|
||||||
}
|
}
|
Loading…
Reference in New Issue