SFML community forums

Help => Graphics => Topic started by: mimipim on February 13, 2011, 12:27:18 am

Title: Draw seems to be not correct.(sprites)
Post by: mimipim on February 13, 2011, 12:27:18 am
Hi guys.

I have a little problem with drawing a sprites.

See the picture:
(http://store.picbg.net/pubpic/1A/49/06186911233a1a49.png)

Every image size is 32x32px... and every one sprite is drawed on 32x32 block but there still have a little black line between them. (maybe 1px).

Here is the grass image if you want to test.
(http://store.picbg.net/pubpic/BD/F6/d86cd1eb00f1bdf6.png)
Title: Draw seems to be not correct.(sprites)
Post by: bastien on February 13, 2011, 12:36:06 am
Try with Image.SetSmooth(false)? Otherwise it's probably a bug in your code in my opinion.
Title: Draw seems to be not correct.(sprites)
Post by: Groogy on February 13, 2011, 12:57:43 am
Your probably doing a common mistake. You start at 0,0 and the width and height of a tile is 32 and 32. Thus the first pixel is at position 0,0 and then we have 31 in each direction left, giving us that the next tile should be placed at 0,32 or 32,0 and not 0,33 or 33,0.
Title: Draw seems to be not correct.(sprites)
Post by: mimipim on February 13, 2011, 11:16:41 am
Look how I'm generating the place of the sprites:
Code: [Select]
   for(unsigned int a=0; a<15; a++)
    {
        objs.push_back(new Object("images/grass.png", 0, 32*a, "Grass"));
        objs.push_back(new Object("images/grass.png", 32, 32*a, "Grass"));
        if(a<=6)
        {
            objs.push_back(new Object("images/sand.png", 64, 32*a, "Sand"));
            objs.push_back(new Object("images/sand.png", 96, 32*a, "Sand"));
        }
    }


So I think theres no problem with my source.

About SetSmooth function - I already try it, but there no change.

 :?
Title: Draw seems to be not correct.(sprites)
Post by: devlin on February 13, 2011, 12:36:40 pm
There's no black "in between" them.

In the screenshot you posted, the tiles (without the black border) are 31x31 pixels - even though the grass you linked is a 32x32 tile.

Edit: In fact, looking at the pixeldata of the grass tile and the tiles in the original screenshot - the sprites are looking scaled down to 31x31 pixels.

There's an off-by-one error somewhere.
Title: Draw seems to be not correct.(sprites)
Post by: mimipim on February 13, 2011, 12:38:38 pm
Quote from: "devlin"
There's no black "in between" them.

In the screenshot you posted, the tiles (without the black border) are 31x31 pixels - even though the grass you linked is a 32x32 tile.


Sorry, but I don't understand you very good.
Can you explain with code please.
Title: Draw seems to be not correct.(sprites)
Post by: devlin on February 13, 2011, 12:39:29 pm
Probably missed my little edit:

Looking at the pixeldata of the grass tile and the tiles in the original screenshot - the sprites are looking scaled down to 31x31 pixels. (easily checked in for instance photoshop)

There's an off-by-one error somewhere.

Edit: We'll have to see your loading and rendering code to help you any further.
Title: Draw seems to be not correct.(sprites)
Post by: devlin on February 13, 2011, 12:41:48 pm
To rephrase it a bit more clearly:

The sprites in the screenshot are indeed spaced 32 pixels apart. However, the actual sprite image is scaled down to a 31x31 pixel image before rendering.

We'll have to see you actual loading and drawing code to help you find where the error is.
Title: Draw seems to be not correct.(sprites)
Post by: mimipim on February 13, 2011, 12:47:12 pm
I'm not resizing(scale) the image - I'm not so stuped. :D
Title: Draw seems to be not correct.(sprites)
Post by: devlin on February 13, 2011, 12:50:10 pm
I never once called you stupid.

My statement still stands, without the actual code - there's little we can do to help. It's up to you whether you want that help or not. :)
Title: Draw seems to be not correct.(sprites)
Post by: mimipim on February 13, 2011, 12:57:48 pm
This is all the code that create objects. You can see second and third argument is X and Y position on the sprite.

Theres no more code to edit sprite or image.
Title: Draw seems to be not correct.(sprites)
Post by: Groogy on February 13, 2011, 01:07:05 pm
I think devlin want to see the sprite being created.
Title: Draw seems to be not correct.(sprites)
Post by: mimipim on February 13, 2011, 01:14:57 pm
Okay I do just a simple creating without my class.

Take a look at this:

Code: [Select]

int main()
{
    // Create the main window
    sf::RenderWindow Game(sf::VideoMode(640, 480), "Tower Defence");

    sf::Image grass;
    if(!grass.LoadFromFile("images/grass.png"))
        return EXIT_FAILURE;
    sf::Sprite spr_grass(grass);
    sf::Sprite spr_grass2(grass);
    sf::Sprite spr_grass3(grass);
    sf::Sprite spr_grass4(grass);
    sf::Sprite spr_grass5(grass);
    sf::Sprite spr_grass6(grass);

    spr_grass.SetPosition(0,0);
    spr_grass2.SetPosition(0,32);
    spr_grass3.SetPosition(0,64);
    spr_grass4.SetPosition(32,0);
    spr_grass5.SetPosition(64,0);
    spr_grass6.SetPosition(32,32);

    Game.SetFramerateLimit(60);

    // Start the game loop
    while (Game.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (Game.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                Game.Close();
        }

        if(Game.GetInput().IsMouseButtonDown(sf::Mouse::Left))
        {
            std::cout<<"X: "<<Game.GetInput().GetMouseX()<<std::endl;
            std::cout<<"Y: "<<Game.GetInput().GetMouseY()<<std::endl;
        }

        // Clear screen
        Game.Clear(sf::Color(0,0,0));

        Game.Draw(spr_grass);
        Game.Draw(spr_grass2);
        Game.Draw(spr_grass3);
        Game.Draw(spr_grass4);
        Game.Draw(spr_grass5);
        Game.Draw(spr_grass6);

        // Update the window
        Game.Display();
    }

    return EXIT_SUCCESS;
}


Result:
(http://store.picbg.net/pubpic/D9/55/87b31d44527ed955.png)


Can anyone help with this please.
Title: Draw seems to be not correct.(sprites)
Post by: Breakman79 on February 13, 2011, 05:49:55 pm
In the example code you posted, you're not disabling the bilinear filtering on the image which adds the 1 pixel black line around it.  This is definitely something you have to set on every image loaded for use as a tile map.

In your example just change your image loading code to this and try it again:

Code: [Select]
sf::Image grass;
if(!grass.LoadFromFile("images/grass.png"))
    return EXIT_FAILURE;
grass.SetSmooth(false);
sf::Sprite spr_grass(grass);
sf::Sprite spr_grass2(grass);
sf::Sprite spr_grass3(grass);
sf::Sprite spr_grass4(grass);
sf::Sprite spr_grass5(grass);
sf::Sprite spr_grass6(grass);
Title: Draw seems to be not correct.(sprites)
Post by: mimipim on February 13, 2011, 05:59:27 pm
Okay.

The solution is found. SetSmooth helps this time.
But when I SetSmooth on objects in my class theres no change and no errors.

I will try some 'hooks' and write if I can't beat it.

Thanks.

Edit: Thanks to all. I make a litle edition on my class and now it's work perfect. Really thanks.
Title: Draw seems to be not correct.(sprites)
Post by: devlin on February 13, 2011, 06:53:14 pm
I just tried the example mimipim posted, all looks fine. Looked over the drawing code for Sprite - it's correct.

I'd guess you have a driver problem at your end, glad you got it working anyway.

(http://www.trollcraft.com/towerdefence.jpg)