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

Author Topic: Sprite colors bleeding when using subrect  (Read 5686 times)

0 Members and 1 Guest are viewing this topic.

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Sprite colors bleeding when using subrect
« on: January 08, 2011, 11:48:05 pm »
I'm trying to draw 32x32 tiles from a tileset using sprite subrects, but the very edge of the tiles shows some of the tile next to it. Smoothing is disabled. Even with subrect set to 0,0,32,32 I see bleeding into the next tile at the border.
This is the result I get drawing a tilemap of pure color tiles: (notice the apparent 'gridlines'


This is the tileset:


I have confirmed the rectangle my code generates it correct and the location the tiles draw to is correct so I' m unsure why this happens.

Drawing the same map with actual tiles gives me this:

I don't understand why the 'grid' of darkened tile appears.

bkaxrf

  • Newbie
  • *
  • Posts: 10
    • View Profile
Sprite colors bleeding when using subrect
« Reply #1 on: January 09, 2011, 08:08:07 am »
I haven't used subrect, but 0,0,32,32 seems like 33x33 pixels.  Try 0,0,31,31?

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Sprite colors bleeding when using subrect
« Reply #2 on: January 09, 2011, 06:09:18 pm »
Quote from: "bkaxrf"
I haven't used subrect, but 0,0,32,32 seems like 33x33 pixels.  Try 0,0,31,31?

I forgot to mention it but that's what I had in the second picture. The colored tiles were with 0,0,32,32 and the 'real' tiles were 0,0,31,31.

This is the code that creates the rectangle:
Code: [Select]

spr.SetSubRect(
sf::IntRect(
tilePosInTileSet.x * tileWidth,
tilePosInTileSet.y * tileHeight,
(tilePosInTileSet.x + 1) * tileWidth,
(tilePosInTileSet.y + 1) * tileHeight)
);

I've tried every combination of adding and subtracting 1 to some or all of the 4 values and the problem just won't go away.

bkaxrf

  • Newbie
  • *
  • Posts: 10
    • View Profile
Sprite colors bleeding when using subrect
« Reply #3 on: January 09, 2011, 09:31:00 pm »
Did you try..
Code: [Select]

spr.SetSubRect(
   sf::IntRect(
      tileWidth * tilePosInTileSet.x,
      tileHeight * tilePosInTileSet.y,
      tileWidth * (tilePosInTileSet.x + 1) - 1,
      tileHeight * (tilePosInTileSet.y + 1) - 1
);

For 0,0 you get 0,0,31,31
For 1,1 you get 32, 32, 63, 63
etc

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Sprite colors bleeding when using subrect
« Reply #4 on: January 09, 2011, 09:31:56 pm »
Quote from: "bkaxrf"
Did you try..
Code: [Select]

spr.SetSubRect(
   sf::IntRect(
      tileWidth * tilePosInTileSet.x,
      tileHeight * tilePosInTileSet.y,
      tileWidth * (tilePosInTileSet.x + 1) - 1,
      tileHeight * (tilePosInTileSet.y + 1) - 1
);

For 0,0 you get 0,0,31,31
For 1,1 you get 32, 32, 63, 63
etc

Yeah I've tried that, doesn't fix it.

bkaxrf

  • Newbie
  • *
  • Posts: 10
    • View Profile
Sprite colors bleeding when using subrect
« Reply #5 on: January 09, 2011, 09:37:04 pm »
Do you have the tileset used in the second image?  

Maybe the subrect is okay, but the position that you're drawing the tile to is off by 1?

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Sprite colors bleeding when using subrect
« Reply #6 on: January 09, 2011, 09:39:23 pm »
Quote from: "bkaxrf"
Do you have the tileset used in the second image?  

Maybe the subrect is okay, but the position that you're drawing the tile to is off by 1?




And the position isn't the problem, even drawing a single tile exhibits this.

Could this function from SFML's Image class be the culprit?
Code: [Select]
FloatRect Image::GetTexCoords(const IntRect& Rect) const
 {
     float Width  = static_cast<float>(myTextureWidth);
     float Height = static_cast<float>(myTextureHeight);
 
     return FloatRect(Rect.Left   / Width,
                      Rect.Top    / Height,
                      Rect.Right  / Width,
                      Rect.Bottom / Height);
 }

It strikes me that it would end up with odd values when it divides 31 by the width etc.

This is the (zoomed) output of drawing a solid blue square: (border and black dots should not be there)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sprite colors bleeding when using subrect
« Reply #7 on: January 09, 2011, 10:50:46 pm »
Are you sure that smoothing is disabled here? Looks like bilinear filtering.
Laurent Gomila - SFML developer

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Sprite colors bleeding when using subrect
« Reply #8 on: January 09, 2011, 10:53:54 pm »
Quote from: "Laurent"
Are you sure that smoothing is disabled here? Looks like bilinear filtering.

Yeah I have smoothing disabled.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sprite colors bleeding when using subrect
« Reply #9 on: January 09, 2011, 11:00:34 pm »
Have you tried SFML 1.5 or 2.0?
Laurent Gomila - SFML developer

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Sprite colors bleeding when using subrect
« Reply #10 on: January 09, 2011, 11:01:22 pm »
I'll download 2.0 and see what happens...
edit: Looks like 2.0 changed how a ton of the stuff I'm using works... May take me a bit before I can try this

bkaxrf

  • Newbie
  • *
  • Posts: 10
    • View Profile
Sprite colors bleeding when using subrect
« Reply #11 on: January 09, 2011, 11:20:12 pm »
You're right about it being 0,0,32,32 since its texture coordinates...

But that shouldn't affect the smoothing, it would just be 31x31 instead.

So yeah, your image needs SetSmooth(false),  but you already did that so I'm stumped.  Hrmmm

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Sprite colors bleeding when using subrect
« Reply #12 on: January 11, 2011, 12:32:02 am »
I just tried with SFML 2 and it appears to be fixed. Is it safe to upgrade to SFML 2 in my project or is there any way to fix this for SFML 1.6?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sprite colors bleeding when using subrect
« Reply #13 on: January 11, 2011, 08:35:44 am »
Quote
Is it safe to upgrade to SFML 2 in my project

A few things will change until the final release, but it's already very stable.

Quote
is there any way to fix this for SFML 1.6?

No.
Laurent Gomila - SFML developer

 

anything