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.


Topics - cire

Pages: [1]
1
Graphics / sf::View, Viewports and scaling - Default behavior
« on: November 22, 2012, 06:51:05 am »
I have been doing a little experimenting with sf::View.  I'd wanted to use 2 sf::Views in combination with their viewports to map some content to different areas of the screen.  One area of the screen would be for the 'action' and one area would be for information related to what was occurring on the 'action' portion in a 75% - 25% split.  Because I don't need nearly the same size to render the action area and the information area, I defined the view for the information area to be much smaller than the view for the action area.

The following code/picture should illustrate the result I achieved.  The only difference in the code here and the code I used is that the background tiles I used to differentiate the two areas has been removed for brevity.  The text and the rectangle shape should be faithfully reproduced, although you'll have to supply your own font!

#include <SFML/Graphics.hpp>

const sf::Vector2f leftSize(1440.0f, 1080.0f) ;
const sf::Vector2f rightSize(480.0f, 1080.0f) ;
const sf::Vector2f origin(0.0f, 0.0f) ;

int main()
{
    sf::RenderWindow window(sf::VideoMode(1280, 720), "Text & View" ) ;

    sf::View leftView( sf::FloatRect(origin, leftSize) ) ;
    leftView.setViewport(sf::FloatRect(0.0f, 0.0f, 0.75f, 1.0f)) ;

    sf::View rightView( sf::FloatRect(origin, rightSize) ) ;
    rightView.setViewport(sf::FloatRect(0.75f, 0.0f, 1.0f, 1.0f)) ;

    sf::RectangleShape rect(sf::Vector2f(120.0f, 50.0f)) ;
    rect.setPosition(10, 15) ;
    rect.setFillColor(sf::Color(0,0,127)) ;

    sf::Font font ;
    font.loadFromFile("SketchFlow.ttf") ;

    sf::Text text("Word!", font);
    text.setColor(sf::Color(127, 255, 63, 255)) ;
    text.setPosition(20.0, 20.0) ;

    while ( window.isOpen() )
    {
        sf::Event event ;
        while ( window.pollEvent(event) )
        {
            if ( event.type == sf::Event::Closed )
                window.close() ;
        }

        window.clear() ;
       
        window.setView(leftView) ;
        window.draw(rect) ;
        window.draw(text) ;

        window.setView(rightView) ;
        window.draw(rect) ;
        window.draw(text) ;

        window.display() ;
    }
}

The result:


Someone correct me if I'm wrong:  the content in the sf::views that represent the right and left seems to be scaled from the view's respective sizes to the view's initial viewport size (in this case, 1280x720.)  This seems a little counter-intuitive to me.  I would've expected the content to be scaled from the size of the view to the size of the current viewport size.  I haven't yet been mucking about in the internals of SFML, so I don't know if this expectation is reasonable given the implementation.

I did try to implement a few solutions.  Unfortunately, the zoom can't be used to adjust the scaling factor, because the height to width ratio isn't the same, in other words I can't just zoom out from the x-axis without also zooming out from the y-axis.  Adjusting the size of the right view so the height/width ratio is correct and then using zoom to adjust the scaling factor does seem to work, although it feels very hackish.  The easiest solution I found was just to give the right view the same size as the left view, so that they both scaled the same.  Again, that still feels a little hackish.

So, I guess my question is whether or not this behavior is right?  The documentation states:

Quote
If the source rectangle has not the same size as the viewport, its contents will be stretched to fit in.

which seems to fit what my expectations were.

Any insights?  Is there something I'm missing?

2
Graphics / Unexpected result using sf::Lines primitive.
« on: October 14, 2012, 02:41:23 am »
I'm not sure why the 'gaps' appear when I'm using the code below.  At first I thought perhaps it might be some float rounding thing, but that doesn't seem to be the case.


You can see the gaps (black lines) here:



Here is the (minimal, compilable) code that produces it on my system.  You'll have to pardon the naming style mismatch.

#include <SFML/Graphics.hpp>

const unsigned win_height = 600 ;
const unsigned win_width  = 800 ;
const unsigned line_height = 300 ;

void draw_vertical_line(sf::RenderTexture& t, unsigned x) ;
void update_texture(sf::RenderTexture& t) ;
void wait_for_close(sf::RenderWindow& window) ;

int main()
{
    sf::RenderWindow window(sf::VideoMode(win_width, win_height), "Vertical Lines") ;

    sf::RenderTexture texture ;
    if (texture.create(win_width, win_height))
    {
        update_texture(texture) ;
        window.clear() ;
        window.draw(sf::Sprite(texture.getTexture())) ;
        window.display() ;
    }
    wait_for_close(window) ;
}

void draw_vertical_line(sf::RenderTexture& t, unsigned x)
{
     sf::VertexArray line(sf::Lines, 2) ;

    line[0].position = sf::Vector2f(x, win_height) ;
    line[1].position = sf::Vector2f(x, win_height - line_height) ;
    line[0].color = line[1].color = sf::Color(0, 255, 256 * x / win_width) ;

    t.draw(line) ;
}

void wait_for_close(sf::RenderWindow& window)
{
    sf::Event event ;

    while (window.isOpen())
        while ( window.pollEvent(event) )
            if ( event.type == sf::Event::Closed )
                window.close() ;
}

void update_texture(sf::RenderTexture& t)
{
    t.clear() ;
    for ( unsigned i=0; i<win_width; ++i)
        draw_vertical_line(t, i) ;
    t.display() ;
}

Can anyone see what I'm doing wrong here?




Pages: [1]