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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Sauce

Pages: [1]
1
Graphics / Scrolling backgrounds / wrapping sprites at screen edge
« on: April 26, 2010, 03:39:30 pm »
It has to be done at render time because it relies on the current position of the View. Doing it at any other point in time would be pointless.

2
Graphics / Scrolling backgrounds / wrapping sprites at screen edge
« on: April 25, 2010, 04:32:35 pm »
Quote from: "Nexus"
Code: [Select]
sf::Vector2f pos;
pos.x = screenRect.Left + fmod(pos.x, width);
pos.y = screenRect.Top + fmod(pos.y, height);
You access pos.x and pos.y, which are both zero (because the vector has been default-constructed). fmod(0, anything) should return 0, so this has not much effect.


Code: [Select]
sf::Sprite temp(*this);
temp.SetPosition(pos);
target.Draw(temp);
Why create a temporary sprite? Is your class now deriving from sf::Sprite? If so, just invoke the base method:
Code: [Select]
sf::Sprite::Render(target);

Code: [Select]
if(GetPosition().x < screenRect.Left)You might replace if with while. And don't forget to draw after each translation. You should also make sure that the sprite isn't moved back in -x direction after it has been moved forward (use else if instead of the second if).


Code: [Select]
temp.Move(GetSize().x * 1.0f, 0);Is GetSize().x not enough? Or why * 1.0f? :P
Also, instead of GetSize().x * -1.0f, use -GetSize().x. ;)


lol woops about the zero thing with the vector. It was meant to be initialised to the value of GetPos(). Serves me right for watching tv while coding. As with the * 1.0 and -1.0 thing, I just left it there in case i wanted to quickly set it back to 2 and -2 :P

Unfortunately the problem still exists. It just refuses to wrap more than twice. For reference:

Code: [Select]
void WrappingSprite::Render(sf::RenderTarget &target) const
{
FloatRect screenRect = target.GetView().GetRect();
float width = screenRect.Right - screenRect.Left;
float height = screenRect.Bottom - screenRect.Top;

sf::Vector2f pos = GetPosition();
pos.x = screenRect.Left + fmod(pos.x, width);
pos.y = screenRect.Top + fmod(pos.y, height);

sf::Sprite temp(*this);
temp.SetPosition(pos);
target.Draw(temp);

if(GetPosition().x < screenRect.Left)
{
temp.Move(GetSize().x, 0);
}

if(GetPosition().x + GetSize().x > screenRect.Right)
{
temp.Move(-GetSize().x, 0);
}

target.Draw(temp);
}

3
Graphics / Scrolling backgrounds / wrapping sprites at screen edge
« on: April 25, 2010, 03:40:13 pm »
Okay, sorry about the communication error :P

I was already aware I'd need to use more than one sprite (or reuse the same one) to achieve it. Here's what I've got so far;

Code: [Select]
void WrappingSprite::Render(sf::RenderTarget &target) const
{
FloatRect screenRect = target.GetView().GetRect();
float width = screenRect.Right - screenRect.Left;
float height = screenRect.Bottom - screenRect.Top;

sf::Vector2f pos;
pos.x = screenRect.Left + fmod(pos.x, width);
pos.y = screenRect.Top + fmod(pos.y, height);

sf::Sprite temp(*this);
temp.SetPosition(pos);
target.Draw(temp);

if(GetPosition().x < screenRect.Left)
{
temp.Move(GetSize().x * 1.0f, 0);
}

if(GetPosition().x + GetSize().x > screenRect.Right)
{
temp.Move(GetSize().x * -1.0f, 0);
}

target.Draw(temp);
}


The problem (as I was having before) is that I can only get it to wrap twice... No matter what I do! I would have thought fmod() would have done the trick, but it's still no good.

4
Graphics / Scrolling backgrounds / wrapping sprites at screen edge
« on: April 25, 2010, 02:50:46 pm »
My current (failed) implementation is the method using sf::View (which doesnt work, at all) I don't have the old code which worked so long as the view didnt change as I overwrote it >_<

You seem to be under the impression that I'm trying to draw more than one sprite (with more than one image) within the same sprite. This is not the case. What I'm trying to do is wrap the sprite when it gets to the screen edge, so for example the sprite goes over the right edge of the screen, and loops back to the other side of the screen. That's all I want to do.

edit: thanks gsaurus. I thought getSize() returned the scaling factor, not the size in pixels. That will make things easier.

5
Graphics / Scrolling backgrounds / wrapping sprites at screen edge
« on: April 25, 2010, 12:49:16 pm »
This isn't my "tile manager". This is just a specialisation of a sprite. Thus I've chosen public inheritance. I want to use it in the same context as a sprite. The idea is that the sprite simply wraps at the screen edge. is that so hard to understand?

Also, I'm perfectly aware of how to get the rect of the current view. If it was as simple as that then I wouldn't be asking how to do it. It's the combination of all those factors that's making the math difficult.

6
Graphics / Scrolling backgrounds / wrapping sprites at screen edge
« on: April 25, 2010, 09:24:20 am »
I'm not scaling the background, though I may need to in future. That's why its important that I also take that into account. I've already read that thread multiple times when researching this problem and it wasn't of any help to me.

7
Graphics / Scrolling backgrounds / wrapping sprites at screen edge
« on: April 25, 2010, 08:13:05 am »
the array method wont really help me much, i need the texture to tile indefinitely as we are working on multiple levels, each of a different lengths. We should just be able to determine the coordinates to offset the sprites by on a per-frame basis but the math for accomplishing this escapes me.

It's quite sad because the art our team has produced is brilliant and isn't being put to good use at the moment.

8
Graphics / Scrolling backgrounds / wrapping sprites at screen edge
« on: April 25, 2010, 07:51:08 am »
I'm working on a simple platformer game with a few people for an assessment and I've hit a brick wall. We have a number of large textures (1080p) that we are using for the backgrounds, which we would like to tile throughout the entire level. Rather than manually place instances of the texture at every 1080 units horizontally, i've opted to write a class deriving from sf::Sprite that will automatically handle wrapping the texture when it reaches the edge of the screen.

I've tried numerous methods of achieiving this, including manipulating sf::View's before drawing the sprite, and manually calculating the appropriate coordinates, however I just haven't had any success. There's just too many factors to take into account, including sprite position in the world, View position, sprite's scale, etc (rotation will be ignored).

Does anyone have a suggestion (or better yet, working implementation) that could help me out? I've already wasted far too much time on this feature alone.

Pages: [1]