SFML community forums

Help => Graphics => Topic started by: Link on April 01, 2013, 01:50:24 am

Title: [1.6] SFML how to have stuff appear only in a certain part of the window?
Post by: Link on April 01, 2013, 01:50:24 am
So, initially I thought that sf::View just made a region in the app where stuff was rendered, leaving everything else outside it unaffected. But turns out it just seems to zoom in and focus on the region. How can I only display stuff in a certain region like so?

(http://img163.imageshack.us/img163/4412/fileup.jpg)

Essentially I want the scale for everything to be the same, but say I blit a map to the screen, the portion in the green should be visible, everything else should be hidden, and then the light blue area will/can be used for other stuff.

How can I do this?
Title: Re: [1.6] SFML how to have stuff appear only in a certain part of the window?
Post by: eXpl0it3r on April 01, 2013, 03:14:13 am
Anyways, I've once written a tutorial (https://github.com/SFML/SFML/wiki/Tutorial%3A-Using-View) about the sf::View which might give you some intuition. Keep in mind though, that it was written for SFML 2.0 RC and thus some parts my look different in SFML 1.6.

But you should really be using SFML 2... ;)
Title: Re: [1.6] SFML how to have stuff appear only in a certain part of the window?
Post by: Link on April 01, 2013, 03:48:11 am
I should, but I already had 1.6 done and didn't want to set up 2.0. Besides 1.6 is much easier to install :p
Title: Re: [1.6] SFML how to have stuff appear only in a certain part of the window?
Post by: FRex on April 01, 2013, 04:54:38 am
But you don't spend all the time installing but coding instead, and 1.6 is harder, buggier, older, misses features(the feature you ask for is actually directly provided in 2.0), has different naming convention so NO code in it will ever be directly portable to 2.0. The only reason to use 1.6 at this point is if you are starting a project using old engine that uses 1.6 and can't be easily ported to 2.0 or finishing up a 1.6 project or something completely bizarre and stupid(which might be very likely if you're a college student, if that's the case I'm sure we can find a workaround for this thing here).
I installed SFML,Thor, Box2D and SFGUI from sources and Irrlicht(source on windows, yum'd on fedora) on my Fedora and my Windows 7 under 45 minutes to 1 hour each(each system, not each lib, that'd suck..)
There are precompiled libs for Windows, download, unzip, done, I prefer to clone, cmake and compile myself but that's the easy way.
On Fedora(and only the first step is Fedora exclusive, other distros just use different naming for packages and different managers..) it's just:
0. su yourself as admin(or use sudo)
1. yum install git, cmake and -devel versions of all SFML needs(there's a list in the tutorial)
2. git clone git://github.com/LaurentGomila/SFML.git
3. cmake -i and answer the wizard when it prompts you for something
4. make
5. make install
Either way it's easy but intimidating at first, I'm a complete newbie(kind of skilled according to some.. :P) and I managed on both systems using Google, man command, --help and tutorials.
Title: Re: [1.6] SFML how to have stuff appear only in a certain part of the window?
Post by: Link on April 01, 2013, 08:45:41 pm
Wow, thank's, that's much better, I got rather confused before, I'll do that now, and just port my maybe 10~15 lines of code in SFML over...
Title: Re: [1.6] SFML how to have stuff appear only in a certain part of the window?
Post by: FRex on April 01, 2013, 09:15:27 pm
Really, if you're on Windows then just download precompiled ones if you have or expect to have any trouble or want to save time, I had a packet full of .cpp and .h files written and ran only on Fedora that I needed compiled on someone's pc with vc++ 11 and I just grabbed the precompiled ones and got a project up, compiling and working in 5 minutes there. A programmer on Linux sadly have to know how to run some commands. :-\
Title: Re: [1.6] SFML how to have stuff appear only in a certain part of the window?
Post by: Link on April 01, 2013, 10:04:26 pm
I was on linux and I got it compiled, installed static stuff to rule out future installing on windows and shared libs as well. I also managed to port my code in under 15 minutes, really easy, although can you @FRex, can you tell me what specific functionality how to use it for what I need with the window?
Title: Re: [1.6] SFML how to have stuff appear only in a certain part of the window?
Post by: FRex on April 01, 2013, 10:22:08 pm
Exploiter gave you his tutorial on that and docs are excessive but what you're looking for is setting your view's viewport(which is float rect of normalized coordinates). Basically draw the entire screen stuff with one view that has default 0,0,1,1 viewport, then draw just the green rectangle stuff using the other view with different viewport.
Title: Re: [1.6] SFML how to have stuff appear only in a certain part of the window?
Post by: Link on April 01, 2013, 11:59:15 pm
Thanks, I get the part about drawing onto the default viewport, but when I try to draw it into the the small view, the mapview if you will I do this:

#include "app.h"

void App::Loop(){
        // Clear previous view first.
        gameScreen.clear(sf::Color(24, 24, 24));
        // Set map view
        gameScreen.setView(mapView);
        playerChar.setPosition(x, y);
        gameScreen.draw(playerChar);
        // Set interface view
        gameScreen.setView(fullView);
}

Nothing seems to print at all?

And even if I switch the order of view's, it doesn't work?
#include "app.h"

void App::Loop(){
        // Clear previous view first.
        gameScreen.clear(sf::Color(24, 24, 24));
        // Set interface view
        gameScreen.setView(fullView);
        // Set map view
        gameScreen.setView(mapView);
        playerChar.setPosition(x, y);
        gameScreen.draw(playerChar);
}
Title: Re: [1.6] SFML how to have stuff appear only in a certain part of the window?
Post by: FRex on April 02, 2013, 12:35:33 am
I don't know what you have in app.h but here's simple viewport example:
#include <SFML/Graphics.hpp>
int main()
{
    const float a=0.f;
    sf::RenderWindow app(sf::VideoMode(600,480),"Viewports");
    app.setFramerateLimit(60);
    sf::View first=app.getDefaultView();
    sf::View second=app.getDefaultView();
    second.setViewport(sf::FloatRect(a,a,a+0.1f,a+0.1f));
    sf::RectangleShape shape(sf::Vector2f(200.f,200.f));
    shape.setPosition(sf::Vector2f(200.f,200.f));
    sf::Event eve;
    while(app.isOpen())
    {
        while(app.pollEvent(eve))if(eve.type==sf::Event::Closed) app.close();
        app.clear();
        app.setView(first);
        shape.setFillColor(sf::Color::Red);
        app.draw(shape);//now we draw 0,0,600,480 over entire window(0,0,1,1)
        app.setView(second);
        shape.setFillColor(sf::Color::Blue);
        app.draw(shape);//now we treat entire 0,0,600,480 area as a place which will get
        //drawn over the 0,0,0.1,0.1 portion of the window
        app.display();
    }
}
View basically has it's source rectangle and it's viewport normalized rectangle and the viewport says to which portion of the window to draw and source says where to look for things to draw.
Basically what has to be done is : clear, set view, draw, set view, draw, etc. , display, repeat everything.
Read the wiki tutorial, it's very extensive, has example code and pictures.. ;D