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

Author Topic: Problem with displaying multiple sprites (1.6)  (Read 1548 times)

0 Members and 1 Guest are viewing this topic.

dodo

  • Newbie
  • *
  • Posts: 2
    • View Profile
Problem with displaying multiple sprites (1.6)
« on: February 15, 2013, 02:37:06 pm »
I tried to display 3 sprites while loading the image only once, using this tutorial: http://www.sfml-dev.org/tutorials/1.6/graphics-sprite.php (bottom).

Instead of 3 200*200 Sprites, 3 white pixels are displayed: http://i.imgur.com/qEmUDSo.png

Code:
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <iostream>

class Player : public sf::Sprite
{
public:

        static bool Init(const std::string &ImageFile)
        {
                return Image.LoadFromFile(ImageFile);
        }

        Player()
        {
                Sprite.SetImage(Image);
        }

private:

        static sf::Image Image;

        sf::Sprite Sprite;
};
sf::Image Player::Image;

int main()
{      
        sf::RenderWindow App(sf::VideoMode(800, 600, 32), "pewpew");
        App.UseVerticalSync(true);

        if(!Player::Init("player.png"))
                return EXIT_FAILURE;

        Player Player1;
        Player Player2;
        Player Player3;

        Player1.SetPosition(50.0f, 50.0f);
        Player2.SetPosition(300.0f, 350.0f);
        Player3.SetPosition(550.0f, 200.0f);

        while (App.IsOpened())
        {
                sf::Event Event;
                while(App.GetEvent(Event))
                {
                        if(Event.Type == sf::Event::Closed)
                                App.Close();

                        if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                                App.Close();

                        if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F1))
                        {
                                sf::Image Screenshot = App.Capture();
                                Screenshot.SaveToFile("screenshot.png");
                                std::cout << "Screenshot saved!" << std::endl;
                        }
                }

                App.Clear(sf::Color(0, 150, 0));

                App.Draw(Player1);
                App.Draw(Player2);
                App.Draw(Player3);

                App.Display();
        }

        return EXIT_SUCCESS;
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Problem with displaying multiple sprites (1.6)
« Reply #1 on: February 15, 2013, 02:56:44 pm »
You should probably take a look at the FAQ on the wiki, e.g. here.
Since it's written for the current version (SFML 2.0) it talks about sf::Texture, which is kind of equal to sf::Image in SFML 1.6.

You also might want to read this section of the FAQ.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

dodo

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Problem with displaying multiple sprites (1.6)
« Reply #2 on: February 15, 2013, 04:08:03 pm »
thanks for the quick reply, i will be switching to sfml 2.

the problem is at the constructor:
Player()
        {
                Sprite.SetImage(Image);
        }
 

if i manually call "Player1.SetImage(Image);", and set Image to public, it works. "Player1.Sprite.SetImage(Image);" doesnt work, could someone explain this to me?

EDIT: God I am dumb, i drew the class instead of the Sprite <.< sorry for wasting your time
« Last Edit: February 15, 2013, 04:18:09 pm by dodo »

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Re: Problem with displaying multiple sprites (1.6)
« Reply #3 on: February 17, 2013, 01:06:50 am »
why are you inheriting a sf::Sprite, while having a member variable Sprite, I would recommend dropping the inheriting, it can only lead to problems, and its not clear (a player is not just a sprite), stick with sprites as member variables :)
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion