Bonjour! Je voudrais premièrement remercier les asuteurs pour cet excellent ouvrage.
Dans la fonction:
Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application", sf::Style::Close)
, mWorld(mWindow)
, mTexture()
, mPlayer()
{
...
}
J'obtiens un segmentation fault dû fort problablement a la ligne mWorld(mWindow) . J'ai pourtant suivi le livre et je vois pas ce que j'ai pû faire de mal...
L'érreur est celle ci: At c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\tuple:140
voici la ligne en question dans tuple: constexpr _Head_base(_UHead&& __h)
: _M_head_impl(std::forward<_UHead>(__h)) { }
Je vais montrer mes codes pour une aide plus précise
main.cpp
#include <cstdlib>
#include <SFML/Graphics.hpp>
#include "Game.h"
#include "ResourceHolder.h"
#include <iostream>
using namespace sf;
int main()
{
try
{
Game game;
game.run();
}
catch(std::exception& e)
{
std::cout << "\nEXCEPTION: " << e.what() << std::endl;
}
}
Game.cpp
#include "Game.h"#include "StringHelpers.hpp"#include <SFML/Window/Event.hpp>Game
::Game(): mWindow
(sf
::VideoMode(640, 480), "SFML Application", sf
::Style::Close) , mWorld
(mWindow
) , mTexture
() , mPlayer
(){ if (!mTexture.
loadFromFile("Media/Textures/Eagle.png")) { } mPlayer.
setTexture(mTexture
); mPlayer.
setPosition(100.
f,100.
f);}void Game
::run(){ sf
::Clock clock; sf
::Time timeSinceLastUpdate
= sf
::Time::Zero; while(mWindow.
isOpen()) { timeSinceLastUpdate
+= clock.
restart(); while(timeSinceLastUpdate
> TimePerFrame
) { timeSinceLastUpdate
-= TimePerFrame
; processEvents
(); update
(TimePerFrame
); } render
(); }}void Game
::render(){mWindow.
clear();mWorld.
draw();mWindow.
setView(mWindow.
getDefaultView());mWindow.
draw(mStatisticsText
);mWindow.
display();}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
::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
;} Game.h
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include "World.h"
#include <SFML/System/Time.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
class Game
{
public:
Game();
void run();
void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
private:
void processEvents();
void update(sf::Time deltaTime);
void render();
bool mIsMovingUp, mIsMovingRight, mIsMovingLeft, mIsMovingDown;
private:
sf::RenderWindow mWindow;
World mWorld;
sf::Texture mTexture;
sf::Sprite mPlayer;
sf::Time TimePerFrame;
sf::Text mStatisticsText;
float PlayerSpeed;
};
#endif // GAME_H_INCLUDED
World.cpp
#include <SFML/Graphics/RenderWindow.hpp>
World::World(sf::RenderWindow& window)
: mWindow(window)
, mWorldView(window.getDefaultView())
, mWorldBounds(0.f, 0.f, mWorldView.getSize().x, 2000.f)
, mSpawnPosition(mWorldView.getSize().x / 2.f, mWorldBounds.height - mWorldView.getSize().y / 2.f)
, mScrollSpeed(-50.f)
, mPlayerAircraft(nullptr)
{
loadTextures();
buildScene();
mWorldView.setCenter(mSpawnPosition);
}
void World::loadTextures()
{
mTextures.load(Textures::Eagle, "Media/Textures/Eagle.png");
mTextures.load(Textures::Raptor, "Media/Textures/Raptor.png");
mTextures.load(Textures::Desert, "Media/Textures/Desert.png");
}
void World::buildScene()
{
for(std::size_t i = 0; i < LayerCount; i++)
{
SceneNode::Ptr layer(new SceneNode());
mSceneLayers[i] = layer.get();
mSceneGraph.attachChild(std::move(layer));
sf::Texture& texture = mTextures.get(Textures::Desert);
sf::IntRect textureRect(mWorldBounds);
texture.setRepeated(true);
std::unique_ptr<SpriteNode> backgroundSprite(new SpriteNode(texture, textureRect));
backgroundSprite->setPosition(mWorldBounds.left, mWorldBounds.top);
mSceneLayers[Background]->attachChild(std::move(backgroundSprite));
std::unique_ptr<Aircraft> leader(new Aircraft(Aircraft::Eagle, mTextures));
mPlayerAircraft = leader.get();
mPlayerAircraft->setPosition(mSpawnPosition);
mPlayerAircraft->setVelocity(40.f, mScrollSpeed);
mSceneLayers[Air]->attachChild(std::move(leader));
std::unique_ptr<Aircraft> leftEscort(new Aircraft(Aircraft::Raptor, mTextures));
leftEscort->setPosition(-80.f, 50.f);
mPlayerAircraft->attachChild(std::move(leftEscort));
std::unique_ptr<Aircraft> rightEscort(new Aircraft(Aircraft::Raptor, mTextures));
leftEscort->setPosition(80.f, 50.f);
mPlayerAircraft->attachChild(std::move(rightEscort));
}
}
void World::draw()
{
mWindow.setView(mWorldView);
mWindow.draw(mSceneGraph);
}
void World::update(sf::Time dt)
{
mWorldView.move(0.f, mScrollSpeed * dt.asSeconds());
sf::Vector2f position = mPlayerAircraft->getPosition();
sf::Vector2f velocity = mPlayerAircraft->getVelocity();
if(position.x <= mWorldBounds.left + 150 || position.x >= mWorldBounds.left + mWorldBounds.width - 150)
{
velocity.x = -velocity.x;
mPlayerAircraft->setVelocity(velocity);
}
mSceneGraph.update(dt);
}
World.h
#ifndef WORLD_H_INCLUDED
#define WORLD_H_INCLUDED
#include "ResourceHolder.h"
#include "ResourceIdentifiers.hpp"
#include "SceneNode.h"
#include "SpriteNode.h"
#include "Aircraft.h"
#include <SFML/System/NonCopyable.hpp>
#include <SFML/Graphics/View.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <array>
class World : private sf::NonCopyable
{
public:
explicit World(sf::RenderWindow& window);
void update(sf::Time dt);
void draw();
private:
void loadTextures();
void buildScene();
private:
enum Layer
{
Background,
Air,
LayerCount
};
private:
sf::RenderWindow& mWindow;
sf::View mWorldView;
TextureHolder mTextures;
SceneNode mSceneGraph;
std::array<SceneNode*, LayerCount> mSceneLayers;
sf::FloatRect mWorldBounds;
sf::Vector2f mSpawnPosition;
float mScrollSpeed;
Aircraft* mPlayerAircraft;
};
#endif // WORLD_H_INCLUDED
Entitity.cpp
#include <SFML/Graphics.hpp>
#include "Entity.h"
#include "SceneNode.h"
void Entity::setVelocity(sf::Vector2f velocity)
{
mVelocity = velocity;
}
void Entity::setVelocity(float vx, float vy)
{
mVelocity.x = vx;
mVelocity.y = vy;
}
sf::Vector2f Entity::getVelocity() const
{
return mVelocity;
}
void Entity::updateCurrent(sf::Time dt)
{
move(mVelocity * dt.asSeconds());
}
Entity.h
#ifndef ENTITY_H_INCLUDED
#define ENTITY_H_INCLUDED
#include "SceneNode.h"
class Entity : public SceneNode
{
public:
void setVelocity(sf::Vector2f velocity);
void setVelocity(float vx, float vy);
sf::Vector2f getVelocity() const;
private:
sf::Vector2f mVelocity;
private:
virtual void updateCurrent(sf::Time dt);
};
#endif // ENTITY_H_INCLUDED;
Aircraft.cpp
#include "Aircraft.h"
#include "ResourceHolder.h"
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/RenderStates.hpp>
Textures::ID toTextureID(Aircraft::Type type)
{
switch(type)
{
case Aircraft::Eagle:
return Textures::Eagle;
case Aircraft::Raptor:
return Textures::Raptor;
}
}
Aircraft::Aircraft(Type type, const TextureHolder& textures)
: mType(type)
, mSprite(textures.get(toTextureID(type)))
{
sf::FloatRect bounds = mSprite.getLocalBounds();
mSprite.setOrigin(bounds.width / 2.f, bounds.height /2.f);
}
void Aircraft::drawCurrent(sf::RenderTarget& target,
sf::RenderStates states) const
{
target.draw(mSprite, states);
}
Aircraft.h
#ifndef AIRCRAFT_H_INCLUDED
#define AIRCRAFT_H_INCLUDED
#include "Entity.h"
#include "ResourceIdentifiers.hpp"
#include <SFML/Graphics/Sprite.hpp>
class Aircraft : public Entity
{
public:
enum Type
{
Eagle,
Raptor,
};
public:
Aircraft(Type type, const TextureHolder& textures);
virtual void drawCurrent(sf::RenderTarget& target,
sf::RenderStates states) const;
private:
Type mType;
sf::Sprite mSprite;
};
#endif // AIRCRAFT_H_INCLUDED
Merci