Hi everybody,
This is driving me up the wall, I cannot get this thing to run and I've tried everything, I'm trying to follow the book example from
http://www.amazon.com/SFML-Game-Development-Jan-Haller/dp/1849696845 Chapter 1 and I'm getting an error at compile
**EDIT**
If I remove the call to loadFromFile it seems fine, although it does nothing....
**ENDEDIT**
**EDIT**
Oh and I'm using VS2013 community ed, but I've also tried this on VC++E2010 too and I get the same issue
**ENDEDIT**
Error:
First-chance exception at 0x6D96F20C (msvcr120.dll) in SFML-tuts.exe: 0xC0000005: Access violation reading location 0x00ACF000.Stack:
6D96F20C rep movs byte ptr es:[edi],byte ptr [esi] All the code:
Game.h:
#include <SFML/Graphics.hpp>
#include <SFML/Window/Event.hpp>
class Game
{
public:
Game();
void run();
private:
void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
void processEvents();
void update();
void update(sf::Time deltaTime);
void render();
bool mIsMovingUp;
bool mIsMovingDown;
bool mIsMovingLeft;
bool mIsMovingRight;
static const float PlayerSpeed;
sf::RenderWindow mWindow;
sf::Texture mTexture;
sf::Sprite mPlayer;
};
int main()
{
Game game;
game.run();
}
Game.cpp
#include "Game.h"
#include <iostream>
const float Game::PlayerSpeed = 100.f;
Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application")
, mTexture()
, mPlayer()
{
if (!mTexture.loadFromFile("/Media/Textures/Eagle.png"))
{
std::cout << "Error canny load file";
}
mPlayer.setTexture(mTexture);
mPlayer.setPosition(100.f, 100.f);
}
void Game::run()
{
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
sf::Time TimePerFrame = sf::seconds(1.f / 60.f);
while (mWindow.isOpen())
{
processEvents();
timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > TimePerFrame)
{
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
}
render();
}
}
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;
}
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 deltaTime)
{
sf::Vector2f movement(0.f, 0.f);
if (mIsMovingUp)
movement.y -= PlayerSpeed;
if (mIsMovingDown)
movement.y += PlayerSpeed;
if (mIsMovingLeft)
movement.x -= PlayerSpeed;
if (mIsMovingRight)
movement.x += PlayerSpeed;
mPlayer.move(movement * deltaTime.asSeconds());
}
void Game::render()
{
mWindow.clear();
mWindow.draw(mPlayer);
mWindow.display();
}