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

Author Topic: [1.6] SFML how to have stuff appear only in a certain part of the window?  (Read 4254 times)

0 Members and 1 Guest are viewing this topic.

Link

  • Newbie
  • *
  • Posts: 11
    • View Profile
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?



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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Anyways, I've once written a tutorial 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... ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Link

  • Newbie
  • *
  • Posts: 11
    • View Profile
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

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
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.
« Last Edit: April 01, 2013, 05:04:09 am by FRex »
Back to C++ gamedev with SFML in May 2023

Link

  • Newbie
  • *
  • Posts: 11
    • View Profile
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...

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
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. :-\
Back to C++ gamedev with SFML in May 2023

Link

  • Newbie
  • *
  • Posts: 11
    • View Profile
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?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
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.
Back to C++ gamedev with SFML in May 2023

Link

  • Newbie
  • *
  • Posts: 11
    • View Profile
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);
}
« Last Edit: April 02, 2013, 12:02:24 am by Link »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
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
« Last Edit: April 02, 2013, 12:37:54 am by FRex »
Back to C++ gamedev with SFML in May 2023

 

anything