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

Author Topic: Repeating texture question  (Read 4792 times)

0 Members and 1 Guest are viewing this topic.

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Repeating texture question
« on: July 20, 2014, 11:42:06 am »
For the purpose of explaining, I will use the simple example of a very small starfield.

I've tested rendering it into a RenderTexture set to Repeat. It would look something like this:

http://imgur.com/E9QtGaK

...which is then drawn with a sprite in tiled fashion multiple times to create a larger, seamless effect.

http://imgur.com/CRoR7Lx

Given that the stars that say, go off the top, are repositioned at the bottom, there is a moment where they are sliced in half which destroys the illusion.

My question : Is there a way of making things drawn to the texture 'wrap-around' (Pretty sure this isn't possible with my knowledge of the library).

Sure I could re-draw the ones that are partially off-screen to the opposite side in order to maintain the illusion, but my intention is to do this with considerably larger "stars" and there would be a lot of re-drawing going on!

Any good ideas would be appreciated.
{much better code}

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Repeating texture question
« Reply #1 on: July 20, 2014, 11:58:46 am »
I can't quite figure out if you're asking about a repeating texture or about drawing a single sprite on a wraparound map, since the two are completely different so one doesn't really count as a simple version of the other.


If you mean a repeating texture:

Nope.  It's on you to find/make a sprite that looks good when repeated.  That might mean drawing parts of a star on multiple sides of the tile, and it might mean simply not putting any stars on the edge in the first place.

Even if there was a programmatic way to accurately guess how you wanted to fill it in (and there isn't; computers cannot read your mind), it would still be far simpler and more efficient to do that filling in when you make the image in the first place, and not every time you draw the tile.


If you mean drawing sprites on a "wraparound" game world:

Nope.  The exact way the wraparound is going to work is something you as the application developer need to decide.

But with decent code design it's not a very hard thing to do anyway.  You shouldn't need any help from SFML for this.
One idea: if all your drawables have a common base class like Entity, you should be able to give their common draw() method some logic that checks the entity's size and position, plus the map size (which I guess could be a static member of this base class), and if necessary draws it in two places instead of one.
Another idea: design your code so you draw everything at a position relative to the player.  Everything has an absolute position, then you ask it for its relative position when drawing, and pass it the player's current position.  Thus it can take into account the need for wrap around and return that it's 10 pixels to the left instead of 990 to the right.
« Last Edit: July 20, 2014, 12:02:34 pm by Ixrec »

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Re: Repeating texture question
« Reply #2 on: July 20, 2014, 05:28:01 pm »
No, not map related as such.

The idea is to draw a moving starfield into a large RenderTexture, say 1024x1024, then use that texture to draw for example a 4x4 tiling of the texture using a sprite. When the view is zoomed out it looks like you're drawing thousands of them, but it's just a few hundred in reality.

I wonder if that would be a nice addition to SFML, a setting that makes anything drawn at the edge of a texture is auto-wrapped. So, if you did a 'setWrapped(true)' and drew a circle at 0,0 the 4 quarters of the circle would appear in each corner of the texture, thereby allowing the creation of seamless tiling textures.
{much better code}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Repeating texture question
« Reply #3 on: July 20, 2014, 05:36:02 pm »
Quote
I wonder if that would be a nice addition to SFML, a setting that makes anything drawn at the edge of a texture is auto-wrapped. So, if you did a 'setWrapped(true)' and drew a circle at 0,0 the 4 quarters of the circle would appear in each corner of the texture, thereby allowing the creation of seamless tiling textures.
This is higher-level logic that needs to be handled by user code.
Laurent Gomila - SFML developer

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Repeating texture question
« Reply #4 on: July 21, 2014, 12:21:13 am »
The second picture makes me think its just you doing it wrong by drawing 4 sprites of a size equal to the texture and then moving the texture rectangle such that they dont match.

I would instead use a single sprite that is twice as wide and long and convers the whole area, use setRepeated(true) on the texture, and then use setTextureRect to set a large texture rectangle such that you see the texture repeated more then once inside that single sprite (though it may look better if you instead use a rendertexture that is larger than the area you want to cover with the sprite and only show part of the texture).
If you are not half-drawing stars onto the edge of the rendertexture it should be looking right then.

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Re: Repeating texture question
« Reply #5 on: July 21, 2014, 01:36:22 pm »
That's a different way of looking at it and just might work!

Thanks for the tip.
{much better code}

 

anything