Bienvenue, Invité. Merci de vous connecter ou de vous inscrire.
Avez-vous perdu votre e-mail d'activation ?

Auteur Sujet: [Résolu][SFML 1.6] Affichage aléatoire  (Lu 2704 fois)

0 Membres et 1 Invité sur ce sujet

Hindi

  • Newbie
  • *
  • Messages: 20
    • Voir le profil
[Résolu][SFML 1.6] Affichage aléatoire
« le: Avril 30, 2012, 09:31:21 am »
A l'ai de d'un tuto vidéo, j'ai créé une classe animation qui permet de découper des images et afficher des animations. J'ai appliqué cette méthodes à une de mes classes déjà crée (Player) sans qu'il n'y ai aucun soucis. J'ai voulu ensuite "monter d'un cran" en l'appliquant à la classe mère (mère de Player et Enemy). Toujours aucun soucis pour l'affichage du joueur, mais j'ai bug plutôt étrange sur l'affichage des ennemis qui se font de manière aléatoire. En fait, tous les ennemis sont présent (les collisions sont conservées) mais l'affichage des ennemis est aléatoire (dans le nombre d'ennemis affiché et dans les ennemis affichés).

Je met le minimum de code, avec Player et Enemy pour pouvoir comparer, dites moi s'il vous en faut plus.

animation.h :
#ifndef ANIMATION_H_INCLUDED
#define ANIMATION_H_INCLUDED
#include <SFML/Graphics.hpp>
#include <iostream>

class Animation
{
    public:
        Animation();
        ~Animation();
        void initialize(float x, float y, int frameX, int frameY);
        void update(sf::RenderWindow &app);
        void draw(sf::RenderWindow &app);
        bool getActive();
        void setActive(bool value);
        int getCurrentFrame(int axis);
        void setCurrentFrame(int axis, int value);
        int getPosition(int axis);
        void setPosition(int axis, int value);
        void setImage(sf::Image &tempImage);
        int getFrameWidth();
        int getFrameHeight();

    protected:
        sf::Sprite sprite;
        float frameCounter, switchFrame, amountOfFrameX, amountOfFrameY;
        int currentFrameX, currentFrameY;
        float x,y;
        bool active;
        sf::Clock clock;

};
#endif // ANIMATION_H_INCLUDED

animation.cpp :
int Animation::getFrameWidth()
Animation::Animation()
{

}

Animation::~Animation()
{

}

int Animation::getFrameWidth()
{
    //Calcule la largeur des images à afficher
    return sprite.GetImage()->GetWidth() / amountOfFrameX;
}

int Animation::getFrameHeight()
{
    //Calcule la hauteur des images à afficher
    return sprite.GetImage()->GetHeight() / amountOfFrameY;
}

void Animation::initialize(float x, float y, int frameX, int frameY)
{
    //Fait office de constructeur
    frameCounter = 0;
    switchFrame = 3;
    this->x = x;
    this->y = y;
    amountOfFrameY = frameY;
    amountOfFrameX = frameX;
    active = false;
}

void Animation::update(RenderWindow &app)
{
    //Permet de faire défiler les images
    if(active)
        frameCounter += 10*(float)app.GetFrameTime();
    else
        frameCounter += clock.GetElapsedTime();

    if(frameCounter >= switchFrame)
    {
        frameCounter = 0;
        clock.Reset();
        currentFrameX += getFrameWidth();
    }
    if(currentFrameX >= sprite.GetImage()->GetWidth())
        currentFrameX = 0;
    sprite.SetSubRect(IntRect(currentFrameX, currentFrameY*getFrameHeight(), currentFrameX + getFrameWidth(),currentFrameY*getFrameHeight() + getFrameHeight()));
    sprite.SetPosition(x,y);
}

void Animation::draw(RenderWindow &app)
{
    app.Draw(sprite);
}

bool Animation::getActive()
{
    return active;
}

void Animation::setActive(bool value)
{
    active = value;
}

int Animation::getCurrentFrame(int axis)
{
    //Axe 1 : x, les autre : y
    //Permet de savoir à quelle image on en est
    if(axis == 1)
        return currentFrameX;
    else
        return currentFrameY;
}

void Animation::setCurrentFrame(int axis, int value)
{
    //Axe 1 : x, les autre : y
    //value : Les valeurs commencent à zéro et sont croissante en descendant l'image.
    if(axis == 1)
        currentFrameX = value;
    else
        currentFrameY = value;
}

int Animation::getPosition(int axis)
{
    if(axis == 1)
        return x;
    else
        return y;
}

void Animation::setPosition(int axis, int value)
{
    //Axe 1 : x, les autre : y
    if(axis == 1)
        x = value;
    else
        y = value;
}

void Animation::setImage(Image &tempImage)
{
    sprite.SetImage(tempImage);
}

enemy.h
#ifndef ENEMY_H_INCLUDED
#define ENEMY_H_INCLUDED
#include <list>
#include <SFML/Graphics.hpp>
#include "timer.h"
#include "unit.h"
#include "projectile.h"
#include "animation.h"

//Permet de créer le type enemy : ils sont ensuite créé et stocké dans population.

class Enemy : public Unit
{
    public:
        Enemy(int life, int score, int xSpeed, int ySpeed, const std::string &filepath, sf::Vector2f position, sf::RenderWindow &app);
        ~Enemy();
        sf::Sprite* getSprite();
        sf::IntRect getBoundingBox();
        std::list<Projectile*> fire();
        std::list<Projectile*> getProjectiles();
        void recieveDamages(int dmg);
        int getScore();
        bool isDead();
        void moveLeft();
        void moveUp();
        void moveDown();
        void moveRight();
        void dontMove();
        void draw();
        int getPosition(int axis);
        void setPosition(int axis, int value);

    protected:
        std::list<Projectile*> m_projectiles;
        static sf::Image image;
        float lastShot;
        Timer timer;
        int const m_score;
};

#endif // ENEMY_H_INCLUDED

enemy.cpp
Enemy::Enemy(int life, int score, int xSpeed, int ySpeed, const string &filepath, Vector2f position, RenderWindow &app):Unit(1, xSpeed, ySpeed, filepath, position, app), lastShot(0), m_score(score)
{
    timer.start();
    currentFrameX = currentFrameY = 0;
    animation->initialize(m_position.x, m_position.y, 1, 1);

}

void Enemy::moveUp()
{
    animation->setActive(true);
    m_position.y -= m_ySpeed * m_app.GetFrameTime() * coefSpeed;
    currentFrameY = 0;
    animation->setPosition(2, m_position.y);
    animation->setCurrentFrame(2, currentFrameY);
    animation->update(m_app);
}

void Enemy::moveDown()
{
    animation->setActive(true);
    m_position.y += m_ySpeed * m_app.GetFrameTime() * coefSpeed;
    currentFrameY = 0;
    animation->setPosition(2, m_position.y);
    animation->setCurrentFrame(2, currentFrameY);
    animation->update(m_app);
}

void Enemy::moveLeft()
{
    animation->setActive(true);
    m_position.x -= m_xSpeed * m_app.GetFrameTime() * coefSpeed;
    currentFrameY = 1;
    animation->setPosition(1, m_position.x);
    animation->setCurrentFrame(2, currentFrameY);
    animation->update(m_app);
}

void Enemy::moveRight()
{
    animation->setActive(true);
    m_position.x += m_xSpeed * m_app.GetFrameTime() * coefSpeed;
    currentFrameY = 2;
    animation->setPosition(1, m_position.x);
    animation->setCurrentFrame(2, currentFrameY);
    animation->update(m_app);
}

void Enemy::dontMove()
{
    animation->setActive(true);
    currentFrameY = 0;
    animation->setCurrentFrame(0, currentFrameY);
    animation->update(m_app);
}

void Enemy::draw()
{
    animation->setActive(true);
    animation->update(m_app);
    animation->draw(m_app);
}

player.h
#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED
#include <iostream>
#include <SFML/Graphics.hpp>
#include <list>
#include "projectile.h"
#include "unit.h"
#include "timer.h"
#include "animation.h"


//Tout ce qui concerne le joueur

class Player : public Unit
{
    public:
        Player(int life, int xSpeed, int ySpeed, const std::string &filepath, sf::Vector2f position, sf::RenderWindow &app);
        ~Player();
        sf::Sprite* getSprite();
        int getLives();
        sf::IntRect GetBoundingBox();
        void fire();
        bool HaveProjectilesInProgress();
        std::list<Projectile*>* getProjectiles();
        void moveProjectile();
        void drawProjectile();
        void addScore(int score);
        double getScore();
        void loseLive();
        bool getLostlife();
        void resetLostLife();
        void loadContent();
        void moveUp();
        void moveDown();
        void moveLeft();
        void moveRight();
        void dontMove();
        void draw();
        int getPosition(int axis);
        void setPosition(int axis, int value);

    protected:
        std::list<Projectile*> m_projectiles;
        Projectile *projectile;
        int m_lives; //Vies restantes
        int const max_lives; //Nombre de vie non dépassable
        float const fireRate; //delta en secondes entre chaque tir
        float lastShot;
        Timer timer;
        double m_score;
        bool lostLife;
};

bool canFire(float lastShot, Timer &timer, float const fireRate);

#endif // PLAYER_H_INCLUDED

player.cpp
Player::Player(int life, int xSpeed, int ySpeed, const string &filepath, Vector2f position, RenderWindow &app):Unit(1, xSpeed, ySpeed, filepath, position, app), max_lives(3), lastShot(0), fireRate(0.1), m_lives(4), m_score(0), lostLife(false)
{
    timer.start();
    currentFrameX = currentFrameY = 0;
    animation->initialize(m_position.x, m_position.y, 5, 3);
}

void Player::moveUp()
{
    animation->setActive(true);
    m_position.y -= m_ySpeed * m_app.GetFrameTime() * coefSpeed;
    currentFrameY = 0;
    animation->setPosition(2, m_position.y);
    animation->setCurrentFrame(2, currentFrameY);
    animation->update(m_app);
}

void Player::moveDown()
{
    animation->setActive(true);
    m_position.y += m_ySpeed * m_app.GetFrameTime() * coefSpeed;
    currentFrameY = 0;
    animation->setPosition(2, m_position.y);
    animation->setCurrentFrame(2, currentFrameY);
    animation->update(m_app);
}

void Player::moveLeft()
{
    animation->setActive(true);
    m_position.x -= m_xSpeed * m_app.GetFrameTime() * coefSpeed;
    currentFrameY = 1;
    animation->setPosition(1, m_position.x);
    animation->setCurrentFrame(2, currentFrameY);
    animation->update(m_app);
}

void Player::moveRight()
{
    animation->setActive(true);
    m_position.x += m_xSpeed * m_app.GetFrameTime() * coefSpeed;
    currentFrameY = 2;
    animation->setPosition(1, m_position.x);
    animation->setCurrentFrame(2, currentFrameY);
    animation->update(m_app);
}

void Player::dontMove()
{
    animation->setActive(true);
    currentFrameY = 0;
    animation->setCurrentFrame(2, currentFrameY);
    animation->update(m_app);
}

void Player::draw()
{
    animation->draw(m_app);
}
« Modifié: Mai 02, 2012, 08:44:02 am par Hindi »

Hindi

  • Newbie
  • *
  • Messages: 20
    • Voir le profil
Re : [SFML 1.6] Affichage aléatoire
« Réponse #1 le: Mai 02, 2012, 08:43:39 am »
Après beaucoup (trop) d'heures de debug et l'apparition de toujours plus de bugs étranges, je décide de changer de méthode et d'abandonner celle-ci.

 

anything