Bienvenue, Invité. Merci de vous connecter ou de vous inscrire. Avez-vous oublié d'activer ?

Voir les contributions

Cette section vous permet de consulter les contributions (messages, sujets et fichiers joints) d'un utilisateur. Vous ne pourrez voir que les contributions des zones auxquelles vous avez accès.


Sujets - damjuve

Pages: [1]
1
Réseau / Class sf::SocketTCP, besoin d'aide pour un débutant en réseau
« le: Décembre 10, 2012, 08:51:28 pm »
Bonjours à tous :)

J'avais envi de tester la programmation réseau avec cette merveilleuse lib qu'est la SFML, j'ai donc composé un petit code qui peux je pense faire office de code minimal :

/*
 * main.cpp
 *
 *  Created on: 10 déc. 2012
 *      Author: Damien
 */


#include                <SFML/System.hpp>
#include                <SFML/Graphics.hpp>
#include                <SFML/Window.hpp>
#include                <SFML/Network.hpp>
#include                <iostream>

int                     main(int ac, char **av)
{

    // Create a TCP socket for communicating with server
    sf::SocketTCP Client;

    sf::IPAddress ServerAddress("192.168.0.2");

    // Connect to the specified server
    if (!Client.Connect(8045, ServerAddress, 3.f))
    {
        std::cerr << "Cannot connect to server " << ServerAddress.ToString() << std::endl;
        return (EXIT_FAILURE);
    }
    std::cout << "Connected to server " << ServerAddress.ToString() << std::endl;

    // Receive a message from the client
    char Message[128];
    std::size_t Received;

    int stat;
    if ((stat = Client.Receive(Message, sizeof(Message), Received)) != sf::Socket::Done)
    {
        std::cerr << "Cannot receive message from the server (" << stat << ") " << std::endl;
        return (EXIT_FAILURE);
    }
    // Show it
    std::cout << "Message received from server : \"" << Message << "\"" << std::endl;

    // Define a message to send back to the server
    char ToSend[] = "Hi, I'm a client !";

    // Send the message
    if (Client.Send(ToSend, sizeof(ToSend)) != sf::Socket::Done)
    {
        std::cerr << "Cannot send message to server " << std::endl;
        return (EXIT_FAILURE);
    }
    std::cout << "Message sent to server : \"" << ToSend << "\"" << std::endl;

    // Close the socket when we're done
    Client.Close();
    return (EXIT_SUCCESS);
}
 

(je me base directement sur le code de Laurent).

Et, c'est là que ça se complique, mon serveur est en Java.

Or comme je vous l'ai dit he débutte totalement en réseau, par conséquent je ne sais pas trop où se trouve l'erreur que j'ai commise (dans le code cpp, dans le code Java, dans la configuration de mes machines), et j'ai besoin de votre oeil d'expert pour en trouver l'origine :)

Le plus étrange pour moi c'est que la console client me renvoi :
Cannot connect to server 192.168.0.2

Alors que la console serveur me renvoi :
New client, adress 192.168.0.1 on 52359
Client message : null
 

Merci d'avance de votre temps :)

2
Discussions générales / Besoin de conseils
« le: Juin 03, 2012, 09:21:07 pm »
Bonjours à tous.

Désolé pour le titre pas très claire, mais je savais pas trop comment formuler ça.
Voilà déjà je voudrais vous présenter une classe héritant de sf::Sprite que j'ai écrite :

AnimSprite.h
/*
 * AnimSprite.h
 *
 *  Created on: 3 juin 2012
 *      Author: Damien
 */


#ifndef ANIMSPRITE_H_
#define ANIMSPRITE_H_

#include       <SFML/Graphics.hpp>

class          AnimSprite : public sf::Sprite
{
   public:
      /*
       *  Default Constructor
       */

      AnimSprite();

      /*
       *    Complete Constructor :
       *       nb_anim = nombre d'animation.
       *       nb_move = nombre de frame pour chaque animation.
       *       first_tile_subrect = subrect de la première frame pour chaque animation.
       */

      AnimSprite(   const sf::Image& img, const unsigned int nb_anim, const unsigned int* nb_move, const sf::IntRect* first_tile_subrect, const sf::Vector2f& pos = sf::Vector2f(0, 0), const sf::Vector2f& scale = sf::Vector2f(1, 1), float rotation = 0.f, const sf::Color& col = sf::Color(255, 255, 255, 255));

      /*
       *    Default destructor
       */

      virtual ~AnimSprite();

      /*
       *    Setteur for anim
       */

      void setAnim(const unsigned int nb_anim, const unsigned int* nb_move, const sf::IntRect* first_tile_subrect);

      /*
       *    Setteur for static frame
       *    (frame sélectionné lorsqu'aucune animation n'est lancé)
       */

      void setStaticFrame(const unsigned int stat_frame);
      void setStaticAnim(const unsigned int stat_anim);
      void setStaticStuff(const unsigned int stat_anim, const unsigned int stat_frame);

      /*
       *    Is funcs
       */

      const bool is_anim() const;

      /*
       *  Configure Anim funcs
       *        nb_repeat : nombre de fois où l'annimation doit être effectué (0 pour boucler tant qu'aucun appel à stopAnim n'est fait).
       *         end_turn : si true, l'annimation s'arretera une fois terminée.
       */

      void startAnim(const unsigned int id_anim, const unsigned int nb_repeat = 0);
      void stopAnim(const bool end_turn = false);

      /*
       *  Call whence frame has to be updated
       */

      void updateAnim();

   private:
         /* Data */
         unsigned int   nb_anim;            /* nombre d'animation disponible. */
         unsigned int   *nb_move;            /* nombre de tiles pour chaque animation. */
         sf::IntRect      *first_tile_subrect;   /* subrect de la première tile de chaque anim. (les suivantes doivent être à la suite à droite et faire la même taille. */

         /* Anim Stuff */
         unsigned int   current_anim;
         unsigned int   current_frame;
         bool         anim;
         unsigned int   static_anim;
         unsigned int   static_frame;
         unsigned int   nb_repeat;

         /* Funcs */
         void         init_animStuff();
         void         setNewSubRectByAnimAndFrame(unsigned int anim, unsigned int frame);


};

#endif /* ANIMSPRITE_H_ */


AnimSprite.cpp
/*
 * AnimSprite.cpp
 *
 *  Created on: 3 juin 2012
 *      Author: Damien
 */


#include "AnimSprite.h"

/*
**  ************************** CONSTRUCT & SETTEUR **************************
*/

void   AnimSprite::init_animStuff()
{
   this->current_anim = 0;
   this->current_frame = 0;
   this->anim = false;
   this->static_anim = 0;
   this->static_frame = 0;
   this->nb_repeat = 0;
}

      AnimSprite::AnimSprite()
{
   this->init_animStuff();
   this->nb_anim = 0;
   this->nb_move = NULL;
   this->first_tile_subrect = NULL;
}

      AnimSprite::AnimSprite(   const sf::Image& img, const unsigned int nb_anim, const unsigned int* nb_move, const sf::IntRect* first_tile_subrect, const sf::Vector2f& pos, const sf::Vector2f& scale, float rotation, const sf::Color& col)
      : sf::Sprite(img, pos, scale, rotation, col)
{
   this->init_animStuff();
   this->setAnim(nb_anim, nb_move, first_tile_subrect);
}

void   AnimSprite::setAnim(const unsigned int nb_anim, const unsigned int* nb_move, const sf::IntRect* first_tile_subrect)
{
   if (this->nb_anim != 0)
   {
      delete this->nb_move;
      delete this->first_tile_subrect;
   }
   this->nb_anim = nb_anim;
   this->nb_move = new unsigned int[nb_anim];
   this->first_tile_subrect = new sf::IntRect[nb_anim];
   for (unsigned int i = 0; i < nb_anim; i++)
   {
      this->first_tile_subrect[i] = first_tile_subrect[i];
      this->nb_move[i] = nb_move[i];
   }
}

   AnimSprite::~AnimSprite()
{
   if (this->nb_anim != 0)
   {
      delete this->nb_move;
      delete this->first_tile_subrect;
   }
}

void   AnimSprite::setStaticFrame(const unsigned int stat_frame)
{
   this->static_frame = (stat_frame % this->nb_move[this->current_anim]);
}

void   AnimSprite::setStaticAnim(const unsigned int stat_anim)
{
   this->static_anim = (stat_anim % this->nb_anim);
}

void   AnimSprite::setStaticStuff(const unsigned int stat_anim, const unsigned int stat_frame)
{
   this->setStaticFrame(stat_frame);
   this->setStaticAnim(stat_anim);
}

const bool   AnimSprite::is_anim() const
{
   return (this->anim);
}


/*
** ************************** FUNCTION ANIM **************************
*/

void   AnimSprite::startAnim(const unsigned int id_anim, const unsigned int nb_repeat)
{
   if (id_anim < this->nb_anim)
   {
      this->nb_repeat = nb_repeat;
      this->current_anim = id_anim;
      this->current_frame = 0;
      this->anim = true;
      this->updateAnim();
   }
}

void   AnimSprite::stopAnim(const bool end_turn)
{
   if (end_turn == true)
      this->nb_anim = 1;
   else
   {
      this->anim = false;
      this->current_anim = this->static_anim;
      this->current_frame = this->static_frame;
      this->updateAnim();
   }
}

void   AnimSprite::setNewSubRectByAnimAndFrame(unsigned int anim, unsigned int frame)
{
   sf::IntRect         new_sub;

   anim = anim % this->nb_anim;
   frame = (frame % this->nb_move[anim]);
   new_sub.Top = this->first_tile_subrect[anim].Top;
   new_sub.Bottom = this->first_tile_subrect[anim].Bottom;
   new_sub.Left = this->first_tile_subrect[anim].Left + (frame * this->first_tile_subrect[anim].GetWidth());
   new_sub.Right = this->first_tile_subrect[anim].Right + (frame * this->first_tile_subrect[anim].GetWidth());
   this->SetSubRect(new_sub);
}

void   AnimSprite::updateAnim()
{
   if (this->anim && (this->current_frame = (this->current_frame+ 1) % this->nb_move[this->current_anim]) == 0 && this->nb_repeat != 0)
   {
      this->nb_repeat --;
      if (this->nb_repeat == 0)
         this->stopAnim();
   }

   if (this->anim)
   {
      this->setNewSubRectByAnimAndFrame(this->current_anim, this->current_frame);
   }
   else
      this->setNewSubRectByAnimAndFrame(this->static_anim, this->static_frame);

}

L'idée était donc de rajouter des fonctionalitées à la classe sf::Sprite pour pouvoir gérer une animation (nottament dans le cadre d'un personnage pour un jeu 2D).

Les animations fonctionnent sur une image, vous pouvez définir autant d'animation que vous voulez, il n'y a que deux contraintes : chaque frame d'une même animation doivent faire la même taille et être placé à la suite dans l'image.

Ma première question sera donc simple, que pensez vous de mon code ? Je suis à la recherche de toute amélioration possible. En faite je prépare le terrain pour ensuite facilement créer des jeux 2D, et j'ai besoin de votre avis afin de ne pas partir sur une structure mal conçue.

Ensuite, j'ai choisi d'hérité de Sprite pour pouvoir utiliser les fonctions tels que Rotate(), FlipX() ou Scale(). Et celà m'amène à ma seconde question concernant ces trois fonctions :
ralentissent t'elles beaucoup mon affichage ? Dans cette optique ne serait-il pas mieux de les blitter dans une image créée en mémoire ?

Merci beaucoup de votre lecture, et d'avance pour vos réponses.

damjuve

Pages: [1]
anything