• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Meerul264

Working with SFML - can't display image?

10 posts in this topic

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

 

Main.cpp:

 

[CODE]   

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

 

Animation.h:

 

[CODE]   

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

 

Animation.cpp:

 

[CODE]   

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

 

Player.h:

 

[CODE]   

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

 

Player.cpp:

 

[CODE]   

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

 

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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).

0

Share this post


Link to post
Share on other sites

Problems

one:

[code]

  • player.Update(Window);
  •         player.Draw(Window);
  •  
  •        Window.clear();
  •        Window.display();
[/code]

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

[code]

playerAnimation.setActive(false); //if player not pressing any button
[/code]

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

 

Two

This in "Animation.cpp" At line 99

[code]

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

You set the crop here as it seems.

[code]spriteImage.setTextureRect(sf::IntRect(X, Y, Width, Height));[/code]

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

1

Share this post


Link to post
Share on other sites

Getting closer: it seems that the

 

[CODE] 

  1. int Animation::getFrameWidth()
  2. {
  3. return spriteTexture.getSize().x / amountOfFramesX;
  4. }
 [/CODE]

 

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

0

Share this post


Link to post
Share on other sites

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

 

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

 

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

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

Well, the code for changing frames is slightly convoluted.

 

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

[code]unsigned int currentFrame = 0;[/code]

 

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

[code]currentFrame++;   if(currentFrame >= (numFramesWide * numFramesHigh)) {      currentFrame = 0; }[/code]

 

To calculate the x,y image in the imagesheet:

[code]imageX = (currentFrame % numFramesWide);  imageY = (currentFrame / numFramesWide);[/code]

 

To calculate the pixel location of the image:

[code]pixelX = (imageX * ImageWidth); pixelY = (imageY * imageHeight);[/code]

 

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:

[code]float setPosition(int axis, int value);[/code]

And this:

[code]playerAnimation.setPosition(1,x); playerAnimation.setPosition(2,y);[/code]

 

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.

[code]setPositionX(int x); setPositionY(int y);   getPositionX(); getPositionY();[/code]

 

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).

1

Share this post


Link to post
Share on other sites

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

[CODE] 

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

Animation.cpp

 

[CODE] 

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

 

Player.cpp

[CODE] 

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0