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

Author Topic: Scrolling Background  (Read 7778 times)

0 Members and 1 Guest are viewing this topic.

haxortiz

  • Newbie
  • *
  • Posts: 14
    • View Profile
Scrolling Background
« on: March 06, 2010, 12:12:16 am »
Hello,

I have been working on this project for few days and I really have no idea how to make a scrolling background.

If anyone can help me it would be so greatful. Also example of coding on how to produce this would be great.

Thanks,

Haxortiz

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Scrolling Background
« Reply #1 on: March 06, 2010, 04:19:39 am »
There are several ways to achieve this. One is to set up a view and move it around. That is probably the easiest way.

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Scrolling Background
« Reply #2 on: March 06, 2010, 04:44:25 am »
Depends on what you mean by "scrolling background". For a shmup I would just have the background tiled past the end of the window. For example, a window 1024x768 tile1.x would start at 0, tile2.x (same image as tile1) would start at 1024, both tiles will move by whatever increment towards the left,

simplified:
while gameisopen

- increment tiles

if tile1.x == -1024 (offscreen) then tile1.x = 1024

if tile2.x == -1024, then tile2.x = 1024

end while

It can probably be even simpler.

For an adventure type game, you could do it as model76 explained.

What is "this project"? Information is good.

haxortiz

  • Newbie
  • *
  • Posts: 14
    • View Profile
Scrolling Background
« Reply #3 on: March 06, 2010, 01:28:26 pm »
The user is a submarine that goes up and down but only increases it's velocity left to right either faster or slower. While the background scrolls from right to left as the game goes on.

Just thinking how to get the background scrolling using one background image but only showing so much at a time.

Thanks,

Haxortiz

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Scrolling Background
« Reply #4 on: March 07, 2010, 05:26:22 am »
Well if the background is one long picture, just start it a 0 and move it left whenever you need to. Or you could use a view and have it "attached" to your submarine.

Have you tried implementing anything yet?

haxortiz

  • Newbie
  • *
  • Posts: 14
    • View Profile
Scrolling Background
« Reply #5 on: March 07, 2010, 01:40:56 pm »
No not yet, i tried using a view but not sure how to attach it to the background image.

Also do u think u can give me some example coding to produce this action please, first time using SFML its for my assignment lol.

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Scrolling Background
« Reply #6 on: March 08, 2010, 04:07:28 am »
I'm not going to code it for you, sorry.

I meant attach the view to the submarine/player.

Simply move the view each frame to atleast the X coordinate of your sprite/player/sub, you could leave the Y at the center of the window so that your player can go up and down without the view looking past the background sprite.

Just try and do it. You will probably get it working.

haxortiz

  • Newbie
  • *
  • Posts: 14
    • View Profile
Views
« Reply #7 on: March 08, 2010, 11:54:36 am »
Ok, so i set a new view up at beginning, set the centre of the view and do i move the view inside the game loop? also do i have to draw the view at the end.

Sorry for some of these noobish questions lol. Still learning this multimedia library.

Thanks,

Haxortiz

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Scrolling Background
« Reply #8 on: March 08, 2010, 11:57:09 am »
There is a tutorial about views, have you read it first?
Laurent Gomila - SFML developer

haxortiz

  • Newbie
  • *
  • Posts: 14
    • View Profile
Scrolling Background
« Reply #9 on: March 08, 2010, 03:11:46 pm »
Nope :S, didn't think there was one.

On this website?

Can i have a link please

Thanks,

Haxortiz

[EDITED] Doesn't matter I have found the View tutorial. Thanks anyways

haxortiz

  • Newbie
  • *
  • Posts: 14
    • View Profile
Scrolling Background
« Reply #10 on: March 08, 2010, 09:47:19 pm »
Thanks for the help I have got the view moving along but how can I move string text with the view?

Haxortiz

RetroX

  • Newbie
  • *
  • Posts: 13
    • View Profile
Scrolling Background
« Reply #11 on: March 08, 2010, 10:27:10 pm »
Another possibility would be creating four sprites and an image that's the size of the window.  Draw the four sprites in a square-like pattern, like:

12
34

And then whenever Sprite #4 moves to the width/height of the window, set it back to 0 for that axis.  Just make 1, 2, and 3 all keep the same positions relative to the original background.  I use this method, and it works very well.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Scrolling Background
« Reply #12 on: March 08, 2010, 10:53:35 pm »
Quote
how can I move string text with the view?

Draw it with another view that doesn't move. window.GetDefaultView() should be ok for that.
Laurent Gomila - SFML developer

haxortiz

  • Newbie
  • *
  • Posts: 14
    • View Profile
Scrolling Background
« Reply #13 on: March 09, 2010, 09:27:07 pm »
Quote from: "Laurent"
Quote
how can I move string text with the view?

Draw it with another view that doesn't move. window.GetDefaultView() should be ok for that.


Thanks.

Got it working how can i found out when the view goes past the background sprite.

Haxortiz

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
Scrolling Background
« Reply #14 on: March 09, 2010, 11:06:47 pm »
Well, how about comparing the view's position to the position and size of the background sprite? Remember to account for the view's size, as well, if you need that.