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

Author Topic: Parallax Scrolling: Views or no views  (Read 5967 times)

0 Members and 1 Guest are viewing this topic.

DxE

  • Newbie
  • *
  • Posts: 22
    • View Profile
Parallax Scrolling: Views or no views
« on: January 27, 2014, 11:23:04 am »
So I've been playing around with SFML and have been using views for a lot of things (mainly because I didn't have them back when I was using SDL, so they're like magical boxes from Christmas to me).

The 2 things that I will probably keep using them for are text boxes and parallax scrolling. For certain text boxes effects this is necessary (or might not be, but there's definitely less code involved so it's the obvious choice), but for parallax scrolling is it an arguably bad decision?

In SDL I would just draw the background images at {x*n, y*n} where n is less than one (getting smaller as things got further away), which I would imagine is less intensive than using views, but I find using views more practical than doing this. If they put more of a strain than just a little bit on the system I would rather be using my old method.

Any help is appreciated ^^

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Parallax Scrolling: Views or no views
« Reply #1 on: January 27, 2014, 11:26:52 am »
You'll most likely see no difference, so use what is the simplest for you.
Laurent Gomila - SFML developer

DxE

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Parallax Scrolling: Views or no views
« Reply #2 on: January 27, 2014, 11:33:59 am »
Ok cool, thanks for the quick answer!

cob59

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Parallax Scrolling: Views or no views
« Reply #3 on: January 27, 2014, 11:40:05 am »
I don't see why a sf::View would help you to display a text box...
Could you explain that? I'm curious.

DxE

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Parallax Scrolling: Views or no views
« Reply #4 on: January 27, 2014, 11:49:07 am »
I don't see why a sf::View would help you to display a text box...
Could you explain that? I'm curious.
Well for text boxes in SDL (without views) when a player hits Z or whatever to show the next slide I would just have it erase what's in the text box currently and write the next line. With a view I can set it so that it's viewport on the screen is the same as the size of the box itself and have text scroll up out of it, which is very useful for cutscenes, and even outside of them the speed can be increased so that it's just a nice bonus visual effect.

When I was using SDL I considered making it so when out of a certain boundary it would clip the text surface to give the same effect, but the amount of code required for that is harder to work with and just less worth the effort than something as simple as a view.

EDIT:
I don't know how coherent that actually was but the general gist of it is that you can have text scroll out of a box and disappear which is something you can't (to my knowledge) do easily without views.
« Last Edit: January 27, 2014, 11:53:58 am by DxE »

cob59

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Parallax Scrolling: Views or no views
« Reply #5 on: January 27, 2014, 04:37:26 pm »
I see. But does that mean you have to draw the entire text somewhere in an out-of-reach area beside your actual game scene?
And what if you want some transparency for your textbox? Is it possible with a viewport?

I haven't used sf::Text yet, so I thought I'd have to use a sf::TextureRenderer to make my textbox into a texture/sprite and then display it above my game scene.

DxE

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Parallax Scrolling: Views or no views
« Reply #6 on: January 27, 2014, 06:54:43 pm »
I see. But does that mean you have to draw the entire text somewhere in an out-of-reach area beside your actual game scene?
And what if you want some transparency for your textbox? Is it possible with a viewport?

I haven't used sf::Text yet, so I thought I'd have to use a sf::TextureRenderer to make my textbox into a texture/sprite and then display it above my game scene.
I think you may have misunderstood how views work with SFML. It sounds like you're confused with views in Game Maker. In SFML views aren't displays of different camera locations, they are like separate screens altogether.

Let's say you had a view which is your main screen (the one with all the main stuff going on in it) and you wanted a HUD overlay.

In something like SDL where views don't exist you would draw the main screen relative to a camera object (something like draw(mySprite, x-camX, y-camY)) and the HUD would be drawn in constant locations (draw(myHUD, x, y)).

With Game Maker views you would draw the HUD in a position relative to the current view's location (draw(myHUD, view_x+x, view_y+y)) so that it's always following the camera.

With SFML it's better to think of views as layers of your window, you'd say, ok here's one view for my screen, now I'll make another view for the HUD. We'll call them vMain and vHUD for convenience. When your player moves vMain would follow them, but vHUD would stay in the same place, so you could draw something at location {0, 0} on vHUD and it would never move, but if you drew it on vMain then it would appear to move left as the player moves right (taking vMain with them).

Views are super fun because you can do all sorts of cool stuff with them (check out this thread here to see a cool little game that takes advantage of some of their features). They are also transparent, so my text box is actually invisible and if I don't draw anything else then there would just be text overlaying my scene.

DxE

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Parallax Scrolling: Views or no views
« Reply #7 on: January 27, 2014, 07:33:19 pm »
Here's an example to simplify (?) things:

2 views, one for a background with a big face, one for a foreground with a small face. The small face will be drawn so part of it is outside of the viewport, showing where the view cuts off. Here's the result:


#include <SFML/Graphics.hpp>

int main(){
    sf::RenderWindow window;                                    //Declare window
    window.create(sf::VideoMode(640, 480), "Viewport Example"); //Make window

    sf::View vMain;                                             //Declare main viewport
    vMain.setViewport(sf::FloatRect(0, 0, 1, 1));               //Set main viewport
    vMain.setCenter(320, 240);

    sf::View vFace;                                             //Declare other viewport
    vFace.setViewport(sf::FloatRect(0, 0, 0.5, 0.5));           //Set face viewport, different place on screen
    vFace.setSize(640*0.5, 480*0.5);
    vFace.setCenter(320*0.5, 240*0.5);


    sf::Texture texFace;                                        //Declare texture
    texFace.loadFromFile("face.png");                           //Load texture

    sf::Sprite spFaceBig;                                       //Declare big face
    spFaceBig.setTexture(texFace);                              //Set big face's texture
    spFaceBig.setScale(10.f, 10.f);                             //Make big face live up to name
    spFaceBig.setPosition(-320, -320);                          //Set big face to a nice position
    spFaceBig.setColor(sf::Color::Cyan);                        //Change big face's colour a little

    sf::Sprite spFace;                                          //Declare face
    spFace.setTexture(texFace);                                 //Set face's texture
    spFace.setPosition(240, 170);                               //Set face so some of it leaves the viewport

    bool running=true;

    while(running){
        sf::Event event;                                        //Event junk for ending loop
        while(window.pollEvent(event)){
            if(event.type == sf::Event::Closed){
                running=false;
            }
        }
        window.clear();                                         //Clear window

        window.setView(vMain);                                  //Switch active viewport
        window.draw(spFaceBig);                                 //Draw big face on active viewport

        window.setView(vFace);                                  //Switch active viewport
        window.draw(spFace);                                    //Draw face on active viewport

        window.display();                                       //Show the screen
    }

    window.close();

    return 0;
}

Aaand here's the face if you wanna play around with that at all

cob59

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Parallax Scrolling: Views or no views
« Reply #8 on: January 27, 2014, 09:33:17 pm »
So sf::View objects can be used to define different layers within one renderer, and from independant Drawable objects...

Interesting! I'll check your thread link, thanks.