Thread: any good way to write sprite/animation classes for a game?

  1. #1
    Registered User compjinx's Avatar
    Join Date
    Aug 2001
    Posts
    214

    any good way to write sprite/animation classes for a game?

    I need a better way of controlling my sprites in my game.
    My typical character will have movment animations along still sprites for direction.

    I wrote my sprite/animation classes like this:
    SpriteControl, you only need one of these per bitmap file
    - this is meant for a whole bunch of sprites to be in one bitmap file
    -feed it a filename for the bitmap.
    - you must feed it a Sprite array, an array of X,Y,Width,Height postions for all the sprites in the file
    - you tell it to draw sprite(x) where X is one of the positions on the array.

    AnimationControl, feed it a pointer of the above
    -feed it a pointer to an initialized SpriteControl class, along with an array of numbers, numbers that point to positions on the Sprite array (see above)
    - tell it to draw the animation (with loop, backwards, and pause capabilities)


    and thats all I have, is there a better way to do this?
    I am using allegro.
    "The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
    Eric Porterfield.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    How about making the sprites have a common base?

    Code:
    class DrawingControl {
    public:
      virtual void draw( /* whatever allegro specific args here, like surface and postion */)=0;
      virtual ~DrawingControl;
    };
    
    class SpriteControl : public DrawingControl {
    public:
      SpriteControl(std::string filename);
      void draw(/* args */);
      ~SpriteControl();
    private:
      // whatever ya need
    };
      
    class AnimationControl : public DrawingControl {
    public:
      AnimationControl(std::string filename);
      void draw(/* args */);
      ~AnimationConrol();
    private:
     // whatever
    };
    The filenames in each constructor should be the names of files that contain the appriopriate data. For example, SpriteControl perhaps has some stuff like transparency, clipping information, and a single bitmap file name. It only is capable of drawing one bitmap however. The animation file could contain things like rate of animation, number of frames, followed by number of SpriteControl filenames, and could internally have an array( or preferablly, a std::vector) of SpriteControls.

    Each type of drawing file should then have a specific extension, say .sc for SpriteControl, and .ac for Animation control, then you could write a function

    Code:
    // leaves caller responsible for deletion
    DrawingControl* allocateControl(std::string filename) {
      string extension = filename.substr(filename.rfind('.'));
      if (extension == ".ac") {
        return new AnimationControl(filename);
      } else if (extension == ".sc") {
        return new SpriteControl(filename);
      }
      return NULL;
    }
    Then, you could have say, some player class

    class Player {
    public:
    Player();
    ~Player();
    void draw() {
    switch (movementDirection) {
    case MOVE_LEFT: leftImage->draw();break;
    case MOVE_RIGHT: rightImage->draw(); break;
    // handle rest
    }
    }

    private:
    Direction movementDirection;
    SomeLocationClass a;
    DrawingControl* leftImage;
    DrawingControl* rightImage;
    DrawingControl* downImage;
    DrawingControl* upImage;
    };

    The player could be loaded from a file that contained 4 DrawingControls, each would be allocated with allocateControl, and you could easily change how the player behaves by just changing the data file... adding another DrawingControl subclass wouldn't break your code either, as you would just have to add the DrawingControl subclass, and then add another extension to allocateDrawingControl.

    With this method, you do lose some things, however. You can't tell a DrawingControl to pause. But you could simply use two DrawingControls to do that. For example, say you have an image for the player moving to the right, and another for the player sitting idle. The idle picture could be an animationless SpriteControl, while the movement can be animated with a AnimationControl. When you draw the player moving, you call the moveRight->draw(), but when he is not moving, you call the idle->draw().

    Least, that's how I am doing my drawing system in BomberLAN .

    Good luck.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User compjinx's Avatar
    Join Date
    Aug 2001
    Posts
    214

    Thanks

    WOW! Thank you very much!
    "The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
    Eric Porterfield.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-26-2006, 12:14 AM
  2. Good Game Programming Books
    By aresashura in forum Game Programming
    Replies: 2
    Last Post: 12-19-2001, 09:28 PM
  3. Whats a very simple but good game i can try to make?
    By bluehead in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2001, 09:24 PM
  4. I'm looking for a good book on 3D game programming
    By Music_Man in forum Game Programming
    Replies: 2
    Last Post: 09-09-2001, 09:29 PM
  5. Looking for a good book on 3D game programming
    By Music_Man in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2001, 09:28 PM