Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Custom Animation Class  (Read 2006 times)

0 Members and 1 Guest are viewing this topic.

SEnergy

  • Newbie
  • *
  • Posts: 20
    • View Profile
Custom Animation Class
« on: June 09, 2012, 01:05:05 pm »
can someone help me fix my animation class? I've created default animation class that's swapping textures of sprite, it works so far, however Ican't move with sprite

Animation.h
#include <string>
#include <SFML\Graphics.hpp>
using namespace sf;

class Animation
{
public:
        Animation();
        bool Load(std::string, int);
        void Swap();
        Sprite Draw();
private:
        int CurrentImage;
        Texture tPlayer[2];
        Sprite sPlayer;
};

Animation.cpp
#include "Animation.h"

Animation::Animation()
{
}

void Animation::Swap()
{
        if(CurrentImage == 1)
                CurrentImage = 0;
        else
                CurrentImage = 1;
        sPlayer.setTexture(tPlayer[CurrentImage]);
}

bool Animation::Load(std::string name, int position)
{
        if(tPlayer[position].loadFromFile(name))
        {
                return true;
        }
        return false;
}

Sprite Animation::Draw()
{
        return sPlayer;
}

Animation.cpp
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <Windows.h>
#include "Animation.h"
using namespace sf;

#pragma comment (lib, "sfml-window.lib")
#pragma comment (lib, "sfml-graphics.lib")
#pragma comment (lib, "sfml-system.lib")

int wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR lpCmdLine, INT nShowCmd)
{

        RenderWindow MainWindow(VideoMode(800, 600), "Lucid Dreaming");

        bool left = false, right = false, up = false, down = false;

        Texture tBackground;
        tBackground.loadFromFile("Background.png");
        Sprite sBackground(tBackground);

        Animation anim;
        anim.Load("Untitled.png", 0);
        anim.Load("Untitled2.png", 1);

        Sprite spr;
        spr = anim.Draw();

        while(MainWindow.isOpen())
        {
                spr = anim.Draw();
                Event Event;
                while(MainWindow.pollEvent(Event))
                {
                        if(Event.type == Event::Closed)
                                MainWindow.close();
                }

                if(Keyboard::isKeyPressed(Keyboard::Left))
                {
                        left = true;
                }
                if(Keyboard::isKeyPressed(Keyboard::Right))
                {
                        right = true;
                }
                if(Keyboard::isKeyPressed(Keyboard::Up))
                {
                        up = true;
                }
                if(Keyboard::isKeyPressed(Keyboard::Down))
                {
                        down = true;
                }
                if(left)
                        spr.move(-0.1f, 0);
                if(right)
                        spr.move(0.1f, 0);
                if(up)
                        spr.move(0, -0.1f);
                if(down)
                        spr.move(0, 0.1f);

                left = false;
                right = false;
                up = false;
                down = false;

                MainWindow.clear();

                MainWindow.draw(sBackground);

                anim.Swap();

                MainWindow.draw(spr);

                MainWindow.display();
        }

        return 0;
}

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Re: Custom Animation Class
« Reply #1 on: June 09, 2012, 01:55:07 pm »
Just create a move() method that passes the data along to the internal sprite class(or expose the sprite publicly)

Like this:
#include <string>
#include <SFML\Graphics.hpp>
using namespace sf;

class Animation
{
public:
        Animation();
        bool Load(std::string, int);
        void Swap();
        Sprite Draw(sf::RenderTarget &target)
       {
                target.draw(sPlayer);
       }

       void move (const Vector2f &offset) //inlined for brevity
       {
                sPlayer.Move(offset);
       }
private:
        int CurrentImage;
        Texture tPlayer[2];
        Sprite sPlayer;
};
 

EDIT: just noticed, your draw()method returns a COPY of the sprite, not a reference, so any changes you make to it won't affect the sPlayer object in your animation class. It'd be better to pass your renderwindow into the animation's draw() method than pass the sprite out. I've made the change in my code snippet to reflect this.
« Last Edit: June 09, 2012, 01:59:08 pm by thePyro_13 »

SEnergy

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Custom Animation Class
« Reply #2 on: June 09, 2012, 02:16:50 pm »
yeah I just wanted to say that It's returning sprite since when I create sprite inside the class (public) I can't use it anyway and I didn't know how to do it so it will draw animation within class, THANKS A LOT! it's working great now!

 

anything