Jump to content

  • Log In with Google      Sign In   
  • Create Account

Working with SFML - can't display image?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Meerul264   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 January 2013 - 03:07 AM

The screen is blank, no sprite shows up and I dunno what to do. 

 

Main.cpp:

 

 
 
</p><div>#include <SFML/Graphics.hpp></div>
<div> </div>
<div>#define SCREENWIDTH 800</div>
<div>#define SCREENHEIGHT 600</div>
<div> </div>
<div>#include "Animation.h"</div>
<div>#include "Player.h"</div>
<div> </div>
<div>int main()</div>
<div>{</div>
<div>    sf::RenderWindow Window(sf::VideoMode(SCREENWIDTH,SCREENHEIGHT,32), "SFML ADVANCED ANIMATION TUTORIAL");</div>
<div> </div>
<div>    Player player;</div>
<div> </div>
<div>    player.Initialize();</div>
<div>    player.LoadContent();</div>
<div> </div>
<div>    while(Window.isOpen())</div>
<div>    {</div>
<div>        sf::Event Event;</div>
<div> </div>
<div>        while(Window.pollEvent(Event))</div>
<div>        {</div>
<div>            if(Event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))</div>
<div>                Window.close();</div>
<div>        }</div>
<div>        player.Update(Window);</div>
<div>        player.Draw(Window);</div>
<div> </div>
<div>       Window.clear();</div>
<div>       Window.display();</div>
<div> </div>
<div>    }</div>
<div> </div>
<div>    return 0;</div>
<div>}</div>
<div> </div>
 
 

 

Animation.h:

 

 
 
</p><div>#ifndef ANIMATION_H</div>
<div>#define ANIMATION_H</div>
<div> </div>
<div>#include <SFML/Graphics.hpp></div>
<div> </div>
<div>class Animation</div>
<div>{</div>
<div>public:</div>
<div>Animation();</div>
<div>~Animation();</div>
<div> </div>
<div>void Initialize(float x,float y, int FrameX, int FrameY);</div>
<div>void isOpen();</div>
<div>void Update(sf::RenderWindow &Window);</div>
<div>void Draw(sf::RenderWindow &Window);</div>
<div> </div>
<div>bool getActive();</div>
<div>void setActive(bool value);</div>
<div> </div>
<div>int getCurrentFrame(int axis);</div>
<div>void setCurrentFrame(int axis, int value);</div>
<div> </div>
<div>float getPosition(int axis);</div>
<div>float setPosition(int axis, int value);</div>
<div> </div>
<div>void setTexture(sf::Texture &spriteTexture);</div>
<div> </div>
<div> </div>
<div>protected:</div>
<div>int getFrameWidth();</div>
<div>int getFrameHeight();</div>
<div>private:</div>
<div>sf::Texture spriteTexture;</div>
<div>sf::Sprite spriteImage;</div>
<div>sf::Clock Clock;</div>
<div>int currentFrameX, currentFrameY;</div>
<div>float frameCounter, switchFrame;</div>
<div>float frameTime;</div>
<div>int amountOfFramesX, amountOfFramesY;</div>
<div>float x, y;</div>
<div>bool active;</div>
<div>};</div>
<div> </div>
<div>#endif //ANIMATION_H</div>
 

 

Animation.cpp:

 

 
 
</p><div>#include <SFML/Graphics.hpp></div>
<div>#include <iostream></div>
<div>#include "Animation.h"</div>
<div> </div>
<div>Animation::Animation()</div>
<div>{</div>
<div> </div>
<div>}</div>
<div> </div>
<div>Animation::~Animation()</div>
<div>{</div>
<div> </div>
<div>}</div>
<div> </div>
<div>int Animation::getFrameWidth()</div>
<div>{</div>
<div>return spriteTexture.getSize().x / amountOfFramesX;</div>
<div>}</div>
<div> </div>
<div>int Animation::getFrameHeight()</div>
<div>{</div>
<div>return spriteTexture.getSize().y / amountOfFramesY;</div>
<div>}</div>
<div> </div>
<div>bool Animation::getActive()</div>
<div>{</div>
<div>return active;</div>
<div>}</div>
<div> </div>
<div>void Animation::setActive(bool value)</div>
<div>{</div>
<div>active = value;</div>
<div>}</div>
<div> </div>
<div>int Animation::getCurrentFrame(int axis)</div>
<div>{</div>
<div>if(axis == 1)</div>
<div>return currentFrameX;</div>
<div>else</div>
<div>return currentFrameY;</div>
<div>}</div>
<div> </div>
<div>void Animation::setCurrentFrame(int axis, int value)</div>
<div>{</div>
<div>if(axis == 1)</div>
<div>currentFrameX = value;</div>
<div>else</div>
<div>currentFrameY = value;</div>
<div> </div>
<div>}</div>
<div> </div>
<div>float Animation::getPosition(int axis)</div>
<div>{</div>
<div>if(axis == 1)</div>
<div>return x;</div>
<div>else</div>
<div>return y;</div>
<div>}</div>
<div> </div>
<div>float Animation::setPosition(int axis, int value)</div>
<div>{</div>
<div>if(axis == 1)</div>
<div>x = value;</div>
<div>else</div>
<div>y = value;</div>
<div>}</div>
<div> </div>
<div>void Animation::setTexture(sf::Texture &spriteTexture)</div>
<div>{</div>
<div>    spriteImage.setTexture(spriteTexture);</div>
<div>    std::cout << "loaded 2\n";</div>
<div>}</div>
<div> </div>
<div>void Animation::Initialize(float x,float y, int FrameX, int FrameY)</div>
<div>{</div>
<div>frameCounter = 0;</div>
<div>switchFrame = 1;</div>
<div>active = false;</div>
<div>this->x = x;</div>
<div>this->y = y;</div>
<div>this->amountOfFramesX = FrameX;</div>
<div>this->amountOfFramesY = FrameY;</div>
<div>std::cout << "init2\n";</div>
<div> </div>
<div>}</div>
<div> </div>
<div>void Animation::isOpen()</div>
<div>{</div>
<div>Clock.restart();</div>
<div>}</div>
<div> </div>
<div>void Animation::Update(sf::RenderWindow &Window)</div>
<div>{</div>
<div>frameTime = Clock.getElapsedTime().asSeconds();</div>
<div> </div>
<div> </div>
<div>if(active)</div>
<div>frameCounter += frameTime;</div>
<div>else</div>
<div>frameCounter = 0;</div>
<div> </div>
<div>if(frameCounter >= switchFrame)</div>
<div>{</div>
<div>frameCounter = 0;</div>
<div>currentFrameX += getFrameWidth();</div>
<div>if(currentFrameX >= spriteTexture.getSize().x)</div>
<div>currentFrameX = 0;</div>
<div>}</div>
<div>spriteImage.setTextureRect(sf::IntRect(currentFrameX, currentFrameY * getFrameHeight(), currentFrameX + getFrameWidth(), currentFrameY * getFrameHeight() + getFrameHeight()));</div>
<div>spriteImage.setPosition(x, y);</div>
<div>}</div>
<div> </div>
<div>void Animation::Draw(sf::RenderWindow &Window)</div>
<div>{</div>
<div>Window.draw(spriteImage);</div>
<div> </div>
<div>}</div>
<div> </div>
 

 

Player.h:

 

 
 
</p><div>#ifndef PLAYER_H</div>
<div>#define PLAYER_H</div>
<div> </div>
<div>#include <SFML/Graphics.hpp></div>
<div>#include "Animation.h"</div>
<div> </div>
<div>class Player</div>
<div>{</div>
<div>public:</div>
<div>Player();</div>
<div>~Player();</div>
<div> </div>
<div>void Initialize();</div>
<div> </div>
<div>void LoadContent();</div>
<div>void Update(sf::RenderWindow &Window);</div>
<div>void Draw(sf::RenderWindow &Window);</div>
<div> </div>
<div>protected:</div>
<div>    Animation playerAnimation;</div>
<div> </div>
<div>private:</div>
<div>sf::Texture playerTexture;</div>
<div>sf::Clock clock;</div>
<div>float x, y;</div>
<div>int currentFrameX, currentFrameY;</div>
<div>float moveSpeed;</div>
<div>};</div>
<div> </div>
<div>#endif //PLAYER_H</div>
 

 

Player.cpp:

 

 
 
</p><div>#include <SFML/Graphics.hpp></div>
<div>#include <iostream></div>
<div>#include "Player.h"</div>
<div> </div>
<div>Player::Player()</div>
<div>{</div>
<div> </div>
<div>}</div>
<div> </div>
<div> </div>
<div>Player::~Player()</div>
<div>{</div>
<div> </div>
<div>}</div>
<div> </div>
<div>void Player::Initialize()</div>
<div>{</div>
<div>    x = 0;</div>
<div>    y = 0;</div>
<div>    moveSpeed = 200;</div>
<div>    currentFrameX = 0;</div>
<div>    currentFrameY = 0;</div>
<div>    playerAnimation.Initialize(x,y,3,4);</div>
<div>    std::cout << "init\n";</div>
<div>}</div>
<div> </div>
<div> </div>
<div>void Player::LoadContent()</div>
<div>{</div>
<div>    if(playerTexture.loadFromFile("C:/Program Files (x86)/CodeBlocks/Projects/SFML Animation Advanced tutorial/resource/Animation/Cloaked dude.png"))</div>
<div>        playerAnimation.setTexture(playerTexture);</div>
<div>        std::cout << "loaded" << std::endl;</div>
<div>}</div>
<div> </div>
<div>void Player::Update(sf::RenderWindow &Window)</div>
<div>{</div>
<div>    if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))  //UP</div>
<div>    {</div>
<div>        y -= moveSpeed * clock.getElapsedTime().asSeconds();</div>
<div>        currentFrameY = 3;</div>
<div>        std::cout << "moved up\n";</div>
<div>    }</div>
<div>    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) //DOWN</div>
<div>    {</div>
<div>        y += moveSpeed * clock.getElapsedTime().asSeconds();</div>
<div>        currentFrameY = 0;</div>
<div>        std::cout << "moved down\n";</div>
<div>    }</div>
<div>    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) //LEFT</div>
<div>    {</div>
<div>        x -= moveSpeed * clock.getElapsedTime().asSeconds();</div>
<div>        currentFrameY = 1;</div>
<div>        std::cout << "moved left\n";</div>
<div>    }</div>
<div>    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) //RIGHT</div>
<div>    {</div>
<div>        x += moveSpeed * clock.getElapsedTime().asSeconds();</div>
<div>        currentFrameY = 2;</div>
<div>        std::cout << "moved right\n";</div>
<div>    }</div>
<div>    else</div>
<div>    {</div>
<div>        playerAnimation.setActive(false); //if player not pressing any button</div>
<div>    }</div>
<div> </div>
<div>    playerAnimation.setPosition(1,x);</div>
<div>    playerAnimation.setPosition(2,y);</div>
<div>    playerAnimation.setCurrentFrame(2,currentFrameY);</div>
<div>    playerAnimation.Update(Window);</div>
<div>}</div>
<div> </div>
<div>void Player::Draw(sf::RenderWindow &Window)</div>
<div>{</div>
<div>    playerAnimation.Draw(Window);</div>
<div>}</div>
<div> </div>
 

 



Sponsor:

#2 slicer4ever   Crossbones+   -  Reputation: 3988

Like
0Likes
Like

Posted 12 January 2013 - 03:47 AM

read over your rendering logic:

 

 

player.draw(); //draw the player
window.clear(); //clear what we've drawn
window.display(); //output what we drew, but since we've cleared it, their's nothing to display.

 

 

i'm not familiar with sfml's internal workings, but if it's like any other sane rendering api, then your problem is that you clear w/e you draw, before you display it.


Edited by slicer4ever, 12 January 2013 - 03:47 AM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#3 Meerul264   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 January 2013 - 03:53 AM

Thanks, but the sprite still isn't showing up :(



#4 Meerul264   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 January 2013 - 03:58 AM

I think something's wrong with the updating part, since the image of Coaked dude.png shows up (and obviously its the full pic not the cropped ones) when I commented out the player.Update(Window).



#5 BaneTrapper   Members   -  Reputation: 1246

Like
1Likes
Like

Posted 12 January 2013 - 04:43 AM

Problems

one:

</p><ul><li>player.Update(Window);</li>
<li>        player.Draw(Window);</li>
<li> </li>
<li>       Window.clear();</li>
<li>       Window.display();</li>
</ul>

Window.clear();

Clears whole buffer with black color

move the Window.clear() before all drawing or do all drawings after the function

 

Rest seams fine, your image is loaded and sfml has its own system to display to console if the image location is invalid

 

Also from glimpse of your code at Player::Update() when you don't move you set

</p><pre>
playerAnimation.setActive(false); //if player not pressing any button</pre>

but when you move you don't set it to true

 

Two

This in "Animation.cpp" At line 99

</p><ul><li> </li>
<li>if(active)</li>
<li>frameCounter += frameTime;</li>
<li>else</li>
<li>frameCounter = 0;</li>
<li> </li>
<li>if(frameCounter >= switchFrame)</li>
<li>{</li>
<li>frameCounter = 0;</li>
<li>currentFrameX += getFrameWidth();</li>
<li>if(currentFrameX >= spriteTexture.getSize().x)</li>
<li>currentFrameX = 0;</li>
<li>}</li>
<li>spriteImage.setTextureRect(sf::IntRect(currentFrameX, currentFrameY * getFrameHeight(), currentFrameX + getFrameWidth(), currentFrameY * getFrameHeight() + getFrameHeight()));</li>
<li>spriteImage.setPosition(x, y);</li>
<li>}</li>
</ul>

You set the crop here as it seems.

spriteImage.setTextureRect(sf::IntRect(X, Y, Width, Height));

If there is problem with crop, check values your passing here.


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#6 Meerul264   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 January 2013 - 04:45 AM

Getting closer: it seems that the

 

 
</p><ol><li>int Animation::getFrameWidth()</li>
	<li>{</li>
	<li>return spriteTexture.getSize().x / amountOfFramesX;</li>
	<li>}</li>
</ol>
 

 

from Animation.cpp, the spriteTexture.getSize().x returns value 0. So does the Y part. Anyone have any clue why it gives 0, instead of the actual size of the image, which is 96 by 128?

 

I even tried cast it to float but still



#7 Meerul264   Members   -  Reputation: 130

Like
0Likes
Like

Posted 12 January 2013 - 08:58 AM

it seems that I have trouble with currentFrameX. All the varaibles in 

 

 
spriteImage.setTextureRect(sf::IntRect(currentFrameX, currentFrameY * getFrameHeight(), currentFrameX + getFrameWidth(), currentFrameY * getFrameHeight() + getFrameHeight()));
 

 

Have the correct values, except for currentFrameX, with values of 1900000yadayada.



#8 BaneTrapper   Members   -  Reputation: 1246

Like
0Likes
Like

Posted 12 January 2013 - 10:18 AM

I highly suggest finding the mistake you self, you will learn to fix code / find errors and dozen other essential skills needed for programing.


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#9 Servant of the Lord   Crossbones+   -  Reputation: 21195

Like
1Likes
Like

Posted 12 January 2013 - 10:28 AM

Well, the code for changing frames is slightly convoluted.

 

How about using a single variable to mark the current frame:

unsigned int currentFrame = 0;

 

To change a frame you increment it, then check if we've reached the end.

currentFrame++;
 
if(currentFrame >= (numFramesWide * numFramesHigh))
{
     currentFrame = 0;
}

 

To calculate the x,y image in the imagesheet:

imageX = (currentFrame % numFramesWide); 
imageY = (currentFrame / numFramesWide);

 

To calculate the pixel location of the image:

pixelX = (imageX * ImageWidth);
pixelY = (imageY * imageHeight);

 

It's easier to just keep track of one variable (currentFrame), then to juggle multiple variables, especially when you can calculate the multiple variables from currentFrame and the constants.

 

Also the whole 'axis' thing with your functions, where the functions return a different variable with different meaning depending on what magic numbers you pass in is not a good programming idea unless there is a very good reason to do so.

I'm talking about this:

float setPosition(int axis, int value);

And this:

playerAnimation.setPosition(1,x);
playerAnimation.setPosition(2,y);

 

And why does setPosition() say it returns a float, but never does? It'll return gibberish, which you might accidentally be assigning to your currentFrameX somewhere.

 

setPosition() should take the x AND y positions, or else should have a separate function for each one.

setPositionX(int x);
setPositionY(int y);
 
getPositionX();
getPositionY();

 

Personally, I prefer using some kind of "Point" struct myself. SFML provides one for you if you don't want to use your own: sf::Vector2i (for ints) and sf::Vector2f (for floats).


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#10 Meerul264   Members   -  Reputation: 130

Like
0Likes
Like

Posted 13 January 2013 - 03:28 AM

Thanks for all the replies. Now I redo the codes and make a little change to the previous codes. The problem now is that there are more than one frame being drawn on the screen. I checked the x1 y1 and x2 y2 of the cropped image using sf::setTextureRect(..), and it shows the correct frames with corresponding input. But it also shows the frame next to the right of  'correct frames'.

 

Please help I've spent hours checking but failed to see the cause of this problem.

 

main.cpp

 
</p><div>#include <SFML/Graphics.hpp></div>
<div>#include <iostream></div>
<div> </div>
<div>#define ScreenWidth 800</div>
<div>#define ScreenHeight 600</div>
<div> </div>
<div>#include "Player.h"</div>
<div> </div>
<div>int main()</div>
<div>{</div>
<div>    sf::RenderWindow Window(sf::VideoMode(ScreenWidth,ScreenHeight,32),"SFML ADVANCED SPRITE TUTORIAL 2");</div>
<div> </div>
<div>    Player player;</div>
<div> </div>
<div>    player.LoadTexture();</div>
<div>    player.Initialize();</div>
<div> </div>
<div>    while(Window.isOpen())</div>
<div>    {</div>
<div>        sf::Event Event;</div>
<div> </div>
<div>        while(Window.pollEvent(Event))</div>
<div>        {</div>
<div>            if(Event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))</div>
<div>            {</div>
<div>                Window.close();</div>
<div>            }</div>
<div>        }//END EVENT LOOP</div>
<div> </div>
<div>        Window.clear();</div>
<div>        player.Update(Window);</div>
<div>        player.Draw(Window);</div>
<div>        Window.display();</div>
<div> </div>
<div>        system("cls");</div>
<div> </div>
<div>    }</div>
<div> </div>
<div>    return 0;</div>
<div>}</div>
<div>
 
Animation.h
</div>
<div>#ifndef ANIMATION_H</div>
<div>#define ANIMATION_H</div>
<div> </div>
<div>class Animation</div>
<div>{</div>
<div>    public:</div>
<div>        Animation();</div>
<div>        virtual ~Animation();</div>
<div> </div>
<div>        void setTexture(sf::Texture &tempTexture);</div>
<div> </div>
<div>        void Draw(sf::RenderWindow &Window);</div>
<div>        void Update(sf::RenderWindow &Window);</div>
<div>        void Initialize(float &x, float &y, int amountOfFramesX, int amountOfFramesY, int textureWidth, int textureHeight);</div>
<div> </div>
<div>        float getPositionX(float &x);</div>
<div>        float getPositionY(float &y);</div>
<div> </div>
<div>        float getFrameHeight();</div>
<div>        float getFrameWidth();</div>
<div> </div>
<div>        bool getMoveActive();</div>
<div>        void setMoveActive(bool MoveActive);</div>
<div> </div>
<div>        void setCurrentFrameY(int FrameY);</div>
<div> </div>
<div>    protected:</div>
<div> </div>
<div>    private:</div>
<div>        sf::Texture tempTexture;</div>
<div>        sf::Sprite tempSprite;</div>
<div> </div>
<div>        float x, y;</div>
<div>        float move_x;</div>
<div>        float move_y;</div>
<div> </div>
<div>        float frameHeight, frameWidth;</div>
<div>        int amountOfFramesX, amountOfFramesY;</div>
<div>        int textureWidth, textureHeight;</div>
<div>        bool MoveActive_activated;</div>
<div> </div>
<div>        int currentFrameX, currentFrameY;</div>
<div> </div>
<div>};</div>
<div> </div>
<div>#endif // ANIMATION_H</div>
<div>
 

Animation.cpp

 

 
</p><div>#include <SFML/Graphics.hpp></div>
<div>#include <iostream></div>
<div> </div>
<div>#include "Animation.h"</div>
<div> </div>
<div>Animation::Animation()</div>
<div>{</div>
<div>    //ctor</div>
<div>}</div>
<div> </div>
<div>Animation::~Animation()</div>
<div>{</div>
<div>    //dtor</div>
<div>}</div>
<div> </div>
<div>bool Animation::getMoveActive()</div>
<div>{</div>
<div>    return MoveActive_activated;</div>
<div>}</div>
<div> </div>
<div>void Animation::setMoveActive(bool MoveActive)</div>
<div>{</div>
<div>    MoveActive_activated = MoveActive;</div>
<div>}</div>
<div> </div>
<div>void Animation::setCurrentFrameY(int FrameY)</div>
<div>{</div>
<div>    currentFrameY = FrameY;</div>
<div>}</div>
<div> </div>
<div> </div>
<div>void Animation::Initialize(float &x, float &y, int amountOfFramesX , int amountOfFramesY, int textureWidth, int textureHeight)</div>
<div>{</div>
<div>    this-> x = x;</div>
<div>    this-> y = y;</div>
<div>    this-> amountOfFramesX = amountOfFramesX;</div>
<div>    this-> amountOfFramesY = amountOfFramesY;</div>
<div>    this-> textureWidth = textureWidth;</div>
<div>    this-> textureHeight = textureHeight;</div>
<div> </div>
<div>    currentFrameY = 0;</div>
<div> </div>
<div>}</div>
<div> </div>
<div>float Animation::getFrameWidth()</div>
<div>{</div>
<div>    frameWidth = (float) textureWidth / amountOfFramesX;</div>
<div>    return frameWidth;</div>
<div>}</div>
<div> </div>
<div>float Animation::getFrameHeight()</div>
<div>{</div>
<div>    frameHeight = (float) textureHeight / amountOfFramesY;</div>
<div>    return frameHeight;</div>
<div>}</div>
<div> </div>
<div>float Animation::getPositionX(float &x)</div>
<div>{</div>
<div>    move_x = x;</div>
<div>    return move_x;</div>
<div>}</div>
<div> </div>
<div>float Animation::getPositionY(float &y)</div>
<div>{</div>
<div>    move_y = y;</div>
<div>    return move_y;</div>
<div>}</div>
<div> </div>
<div>void Animation::Update(sf::RenderWindow &Window)</div>
<div>{</div>
<div>    std::cout << "setPosition move_x: " << move_x << std::endl;</div>
<div>    std::cout << "setPosition move_y: " << move_y << std::endl;</div>
<div>    std::cout << "frameWidth: " << getFrameWidth() << std::endl;</div>
<div>    std::cout << "frameHeight: " << getFrameHeight() << std::endl;</div>
<div>    std::cout << "Texture width: " << textureWidth << std::endl;</div>
<div>    std::cout << "Texture height: " << textureHeight << std::endl;</div>
<div>    std::cout << "Amount of frames X: " << amountOfFramesX << std::endl;</div>
<div>    std::cout << "Amount of frames Y: " << amountOfFramesY << std::endl;</div>
<div>    std::cout << "-----------" << std::endl;</div>
<div>    std::cout << "currentFrameX: " << currentFrameX << std::endl;</div>
<div>    std::cout << "currentFrameY: " << currentFrameY << std::endl << std::endl;</div>
<div>    std::cout << "In tempSprite.setTextureRect(sf::IntRect(x1,y1,x2,y2)) : " << std::endl << std::endl;</div>
<div>    std::cout << "x1: " << currentFrameX * frameWidth << std::endl;</div>
<div>    std::cout << "y1: " << currentFrameY * frameHeight << std::endl;</div>
<div>    std::cout << "x2: " << currentFrameX * frameWidth + frameWidth << std::endl;</div>
<div>    std::cout << "y2: " << currentFrameY * frameWidth + frameHeight << std::endl;</div>
<div> </div>
<div>    if(MoveActive_activated)</div>
<div>    {</div>
<div>        std::cout <<"[MoveActive_activated is now TRUE] " << std::endl;</div>
<div>        currentFrameX++;</div>
<div> </div>
<div>            if((frameWidth * currentFrameX) >= (frameWidth*amountOfFramesX))</div>
<div>               {</div>
<div>                   currentFrameX = 0;</div>
<div>               }</div>
<div>    }</div>
<div>    else</div>
<div>    {</div>
<div>        currentFrameX = 1;</div>
<div>    }</div>
<div> </div>
<div>    tempSprite.setTextureRect(sf::IntRect( currentFrameX * frameWidth , currentFrameY * frameHeight , (currentFrameX * frameWidth) + frameWidth  , (currentFrameY * frameHeight) + frameHeight ));</div>
<div>    tempSprite.setPosition(move_x,move_y);</div>
<div> </div>
<div>}</div>
<div> </div>
<div>void Animation::setTexture(sf::Texture &tempTexture)</div>
<div>{</div>
<div>    tempSprite.setTexture(tempTexture);</div>
<div>}</div>
<div> </div>
<div>void Animation::Draw(sf::RenderWindow &Window)</div>
<div>{</div>
<div>    Window.draw(tempSprite);</div>
<div>}</div>
<div>
 
Player.h
</div>
<div>
<div>#ifndef PLAYER_H</div>
<div>#define PLAYER_H</div>
<div> </div>
<div>#include "Animation.h"</div>
<div> </div>
<div>class Player</div>
<div>{</div>
<div>    public:</div>
<div>        Player();</div>
<div>        virtual ~Player();</div>
<div> </div>
<div>         Animation playerAnimation;</div>
<div> </div>
<div>        void LoadTexture();</div>
<div>        void Initialize();</div>
<div>        void Draw(sf::RenderWindow &Window);</div>
<div>        void Update(sf::RenderWindow &Window);</div>
<div> </div>
<div>    protected:</div>
<div>    private:</div>
<div> </div>
<div>        sf::Texture playerTexture;</div>
<div> </div>
<div>        int textureWidth, textureHeight;</div>
<div> </div>
<div>        float x , y;</div>
<div>        float moveSpeed;</div>
<div>};</div>
<div> </div>
<div>#endif // PLAYER_H</div>
<div>
 
 

 

Player.cpp

 
</p><div>#include <SFML/Graphics.hpp></div>
<div>#include <iostream></div>
<div> </div>
<div>#include "Player.h"</div>
<div>#include "Animation.h"</div>
<div> </div>
<div>Player::Player()</div>
<div>{</div>
<div>    //ctor</div>
<div>}</div>
<div> </div>
<div>Player::~Player()</div>
<div>{</div>
<div>    //dtor</div>
<div>}</div>
<div> </div>
<div> </div>
<div>void Player::LoadTexture()</div>
<div>{</div>
<div>    if(playerTexture.loadFromFile("C:/Program Files (x86)/CodeBlocks/Projects/SFML ANIMATION ADVANCED TUTORIAL 2/resources/Cloaked dude.png"))</div>
<div>         {</div>
<div>            playerAnimation.setTexture(playerTexture);</div>
<div>            std::cout << "Loaded the image Cloaked dude.png" << std::endl;</div>
<div>         }</div>
<div> </div>
<div>}</div>
<div> </div>
<div>void Player::Initialize()</div>
<div>{</div>
<div>    std::cout << "Initializing from Player::Initialize()..." << std::endl;</div>
<div>    x = 0;</div>
<div>    y = 0;</div>
<div>    moveSpeed = 10;</div>
<div>    textureWidth = playerTexture.getSize().x;</div>
<div>    textureHeight = playerTexture.getSize().y;</div>
<div> </div>
<div>    playerAnimation.Initialize(x,y,3,4,textureWidth,textureHeight);</div>
<div>}</div>
<div> </div>
<div>void Player::Update(sf::RenderWindow &Window)</div>
<div>{</div>
<div> </div>
<div>    if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) //UP</div>
<div>    {</div>
<div>        playerAnimation.setMoveActive(true);</div>
<div>        y -= moveSpeed;</div>
<div>        playerAnimation.setCurrentFrameY(3);</div>
<div>        std::cout << "Moved up" << std::endl;</div>
<div>    }</div>
<div>    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) //DOWN</div>
<div>    {</div>
<div>        playerAnimation.setMoveActive(true);</div>
<div>        y += moveSpeed;</div>
<div>        playerAnimation.setCurrentFrameY(0);</div>
<div>        std::cout << "Moved down" << std::endl;</div>
<div>    }</div>
<div>    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) //LEFT</div>
<div>    {</div>
<div>        playerAnimation.setMoveActive(true);</div>
<div>        x -= moveSpeed;</div>
<div>        playerAnimation.setCurrentFrameY(1);</div>
<div>        std::cout << "Moved left" << std::endl;</div>
<div>    }</div>
<div>    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) //RIGHT</div>
<div>    {</div>
<div>        playerAnimation.setMoveActive(true);</div>
<div>        x += moveSpeed;</div>
<div>        playerAnimation.setCurrentFrameY(2);</div>
<div>        std::cout << "Moved right" << std::endl;</div>
<div>    }</div>
<div>    else</div>
<div>    {</div>
<div>        playerAnimation.setMoveActive(false);</div>
<div>    }</div>
<div> </div>
<div>    std::cout << "x-position: " << x << std::endl;</div>
<div>    std::cout << "y-position: " << y << std::endl;</div>
<div>    std::cout << "moveSpeed : " << moveSpeed << std::endl;</div>
<div> </div>
<div>    playerAnimation.getPositionX(x);</div>
<div>    playerAnimation.getPositionY(y);</div>
<div>    playerAnimation.Update(Window);</div>
<div> </div>
<div>}</div>
<div> </div>
<div>void Player::Draw(sf::RenderWindow &Window)</div>
<div>{</div>
<div>    playerAnimation.Draw(Window);</div>
<div>}</div>
<div>


#11 Meerul264   Members   -  Reputation: 130

Like
0Likes
Like

Posted 13 January 2013 - 10:55 AM

Ahh nvm problem solved. I feel stupid now.





Back to Game Programming

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS