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

Author Topic: how to load sprite sheet? new to sfml.  (Read 7844 times)

0 Members and 1 Guest are viewing this topic.

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
how to load sprite sheet? new to sfml.
« on: December 09, 2011, 07:53:25 am »
Im new to sfml and I choose sfml 2.0 but it lacks of tutorial.

I have a problem about loading sprite sheet. Here is my code.
Code: [Select]
#include "SFML/Graphics.hpp"

int main()
{
    sf::RenderWindow Window(sf::VideoMode(640,480,32),"Pickin Sticks!");

    sf::Texture image;
    image.LoadFromFile("untitled.png");
    sf::Sprite Sprite;
    if (!image.LoadFromFile("untitled.png"))
        return 1;
    Sprite.SetTexture(image);
    Sprite.SetSubRect(sf::IntRect(10,8,65,65));

    while (Window.IsOpened())
    {
        sf::Event Event;

        while (Window.PollEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                Window.Close();
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::LAlt))
           {
               if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::F4))
                Window.Close();
           }
           if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Down))
                Sprite.SetSubRect(sf::IntRect(70,8,65,65));
           if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Left))
                Sprite.SetSubRect(sf::IntRect(10,8,65,65));
           if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Right))
                Sprite.SetSubRect(sf::IntRect(135,8,65,65));
           if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Up))
                Sprite.SetSubRect(sf::IntRect(195,8,65,65));
        }
        Window.Clear();
        Window.Draw(Sprite);
        Window.Display();
    }
}


Here is my Sprite Sheet.


im able to load it. is there a good way to load it without using Sprite.SetSubRect?
im going to make a simple game..
thank you..

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
how to load sprite sheet? new to sfml.
« Reply #1 on: December 09, 2011, 08:02:12 am »
Quote
is there a good way to load it without using Sprite.SetSubRect?

What's your problem with SetSubRect?

There are a few animation classes on the wiki, you can have a look at them (or use them directly).
Laurent Gomila - SFML developer

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
how to load sprite sheet? new to sfml.
« Reply #2 on: December 09, 2011, 09:59:42 am »
ughh.. my bad sir Laurent..
I mean.. what if i had this image.


and i want to use this as a movement of my character in game..
Like if i pressed LEFT the sprite will display the character looking in the left side..
My problem is, when i use to load it with this code.
Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
sf::RenderWindow Window(sf::VideoMode(800,600,32),"Window");

sf::Texture Image;
if (!Image.LoadFromFile("characters.bmp")
    return 1;
sf::Sprite Sprite;
Sprite.SetTexture(Image);

while(Window.IsOpened())
{
    Window.Clear();
    Window.Draw(Sprite);
    Window.Display();
}
    return 0;
}


The whole image loads on the window..

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
how to load sprite sheet? new to sfml.
« Reply #3 on: December 09, 2011, 10:12:33 am »
You must use SetSubRect to see only a part of the whole image.
Laurent Gomila - SFML developer

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
how to load sprite sheet? new to sfml.
« Reply #4 on: December 09, 2011, 10:15:01 am »
Quote from: "mnajrg"
The whole image loads on the window..


If you don't set the SubRect of the sprite you are printing, the whole image should load on the window. You must set it to the portion of the image you want to show.

By the way, your code contains a call to Draw() without parameters. I don't think that would compile.

Edit: Laurent was faster xD

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
how to load sprite sheet? new to sfml.
« Reply #5 on: December 09, 2011, 10:15:45 am »
I see. So I have to use setsubrect..
I have another problem..
how to  erase the black part of the image?
because if i use red background or some background with other color,
the black part is still there..

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
how to load sprite sheet? new to sfml.
« Reply #6 on: December 09, 2011, 10:17:35 am »
Quote from: "Tex Killer"
Quote from: "mnajrg"
The whole image loads on the window..


If you don't set the SubRect of the sprite you are printing, the whole image should load on the window. You must set it to the portion of the image you want to show.

By the way, your code contains a call to Draw() without parameters. I don't think that would compile.

Edit: Laurent was faster xD

My bad sir, I just edited it by now after i read your reply

Thankyou!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Laurent Gomila - SFML developer

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
how to load sprite sheet? new to sfml.
« Reply #8 on: December 09, 2011, 10:20:12 am »
sir Laurent, Im using the sf::Texture because when i use sf::Image
and error occurs when i try to pass it in sf::Sprite

Here is my code:
Code: [Select]
#include "SFML/Graphics.hpp"

int main()
{
    sf::RenderWindow Window(sf::VideoMode(640,480,32),"Graphics!");

    sf::Image image;
    image.LoadFromFile("untitled.png");
    sf::Sprite Sprite;
    if (!image.LoadFromFile("untitled.png"))
        return 1;
    Sprite.SetTexture(image);
    Sprite.SetSubRect(sf::IntRect(0,0,55,66));

    while (Window.IsOpened())
    {
        sf::Event Event;

        while (Window.PollEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                Window.Close();
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::LAlt))
           {
               if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::F4))
                Window.Close();
           }
           if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Down))
                Sprite.SetSubRect(sf::IntRect(70,8,65,65));
           if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Left))
                Sprite.SetSubRect(sf::IntRect(8,8,65,65));
           if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Right))
                Sprite.SetSubRect(sf::IntRect(135,8,65,65));
           if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Up))
                Sprite.SetSubRect(sf::IntRect(195,8,65,65));
        }
        Window.Clear();
        Window.Draw(Sprite);
        Window.Display();
    }
    return 0;
}


And this is the error.

C:\Documents and Settings\JONG\My Documents\CodeBlocks\SFML TEST\main.cpp||In function 'int main()':|
C:\Documents and Settings\JONG\My Documents\CodeBlocks\SFML TEST\main.cpp|12|error: no matching function for call to 'sf::Sprite::SetTexture(sf::Image&)'|
C:\sfml\sfml2\include\SFML\Graphics\Sprite.hpp|83|note: candidates are: void sf::Sprite::SetTexture(const sf::Texture&, bool)|

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
how to load sprite sheet? new to sfml.
« Reply #9 on: December 09, 2011, 10:27:27 am »
You should also use another type of image, as JPEG will produce some garbage on the image and the mask will not work very well.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
how to load sprite sheet? new to sfml.
« Reply #10 on: December 09, 2011, 10:34:18 am »
There's no tutorial for SFML 2.0, but the online API documentation is complete enough so that it can answer most of your questions.

Quote from: "sf::Texture doc"
if you want to perform some modifications on the pixels before creating the final texture, you can load your file to a sf::Image, do whatever you need with the pixels, and then call Texture::LoadFromImage


But the simplest solution would be to create the alpha channel directly in the source image, with an image editor software.
Laurent Gomila - SFML developer

 

anything