1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#include "Game.h"
#include "StringHelpers.h"
const float Game::PlayerSpeed = 100.f;
const float Game::BulletSpeed = 1.0f;
const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);
Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application", sf::Style::Close)
, mTexture()
, mTexture_bullet()
, mBullet()
, mPlayer()
, mFont()
, mStatisticsText()
, mStatisticsUpdateTime()
, mStatisticsNumFrames(0)
, mIsMovingUp(false)
, mIsMovingDown(false)
, mIsMovingRight(false)
, mIsMovingLeft(false)
, mIsfiring(false)
{
if (!mTexture.loadFromFile("Images/Eagle.png"))
{
// Handle loading error
}
mPlayer.setTexture(mTexture);
mPlayer.setPosition(100.f, 100.f);
if (!mTexture_bullet.loadFromFile("Images/raptor.png"))
{
// Handle loading error
}
mBullet.setTexture(mTexture_bullet);
mBullet.scale(.5,.5);
mBullet.setPosition(124.f, 124.f);
mFont.loadFromFile("Images/Sansation.ttf");
mStatisticsText.setFont(mFont);
mStatisticsText.setPosition(5.f, 5.f);
mStatisticsText.setCharacterSize(10);
}
void Game::run()
{
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (mWindow.isOpen())
{
sf::Time elapsedTime = clock.restart();
timeSinceLastUpdate += elapsedTime;
while (timeSinceLastUpdate > TimePerFrame)
{
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
}
updateStatistics(elapsedTime);
render();
}
}
void Game::processEvents()
{
sf::Event event;
while (mWindow.pollEvent(event))
{
switch (event.type)
{
case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false);
break;
case sf::Event::Closed:
mWindow.close();
break;
}
}
}
void Game::update(sf::Time elapsedTime)
{
sf::Vector2f movement(0.f, 0.f);
sf::Vector2f bulletmove(0.f, 0.f);
if (mIsMovingUp)
movement.y -= PlayerSpeed;
if (mIsMovingDown)
movement.y += PlayerSpeed;
if (mIsMovingLeft)
movement.x -= PlayerSpeed;
if (mIsMovingRight)
movement.x += PlayerSpeed;
mBullet.move(bulletmove.x + (.000001 * elapsedTime.asSeconds()) ,mPlayer.getPosition().y);
mPlayer.move(movement * elapsedTime.asSeconds());
}
void Game::render()
{
mWindow.clear();
mWindow.draw(mBullet);
mWindow.draw(mPlayer);
mWindow.draw(mStatisticsText);
mWindow.display();
}
void Game::updateStatistics(sf::Time elapsedTime)
{
mStatisticsUpdateTime += elapsedTime;
mStatisticsNumFrames += 1;
if (mStatisticsUpdateTime >= sf::seconds(1.0f))
{
mStatisticsText.setString(
"Frames / Second = " + toString(mStatisticsNumFrames) + "\n" +
"Time / Update = " + toString(mStatisticsUpdateTime.asMicroseconds() / mStatisticsNumFrames) + "us");
mStatisticsUpdateTime -= sf::seconds(1.0f);
mStatisticsNumFrames = 0;
}
}
void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
{
if (key == sf::Keyboard::W)
mIsMovingUp = isPressed;
else if (key == sf::Keyboard::S)
mIsMovingDown = isPressed;
else if (key == sf::Keyboard::A)
mIsMovingLeft = isPressed;
else if (key == sf::Keyboard::D)
mIsMovingRight = isPressed;
else if (key == sf::Keyboard::Space)
mIsfiring = isPressed;
if(mIsfiring)
{
mBullet.setPosition(mPlayer.getPosition().x ,mPlayer.getPosition().y -25);
mIsfiring = false;
}
}
|