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

Author Topic: [Solved] Updating Texture from Window with Views  (Read 1414 times)

0 Members and 1 Guest are viewing this topic.

IncredibleSulk

  • Guest
[Solved] Updating Texture from Window with Views
« on: December 31, 2015, 11:01:23 pm »
I apologise if this has been asked before but after a few pages of the forum, and poring over the documentation for both Textures and Views, neither covers as far as I can see, how to update a texture with the contents of a window that has a view set.

I'm trying to implement a pause screen overlay into my game, that when called, draws everything that's already on screen, and draws the pause screen over that.

I have this working in a test project when views are not active, using Texture.update(RenderWindow),
but in the project I am going to implement it in, I am using views to scale resolutions and the problem I have encountered is that when updating a Texture to a window using views, the contents of the texture are not what I expected.

The size of the Window I have created is (1600, 900), and the View is drawing at (1920, 1080).

I am unsure as to whether I should set the size of the texture I am updating, to be the size of the window, or the size of the view (1920, 1080), because both produce completely wrong results.

The code that is currently giving me trouble is as follows, I'd appreciate any help anyone could give me, in updating the contents of the window so that when it displays it spans the screen with all its content intact

#include <iostream>
#include <SFML/Graphics.hpp>

using namespace std;

sf::RenderWindow App;
sf::View View;
sf::Event Event;

int pauseScreen () {
        sf::Texture tex;
        tex.create(App.getSize().x, App.getSize().y);
        tex.update(App);

        sf::Sprite background(tex);

        while (App.isOpen()) {
                while (App.pollEvent(Event)) {

                        if (Event.type == Event.Closed || (Event.type == Event.KeyPressed && Event.key.code == sf::Keyboard::Space))
                                return 0;
                        }
                App.clear();
                App.draw(background);
                App.display();
                }
        return 0;
        }


int main() {
        App.create(sf::VideoMode(1600, 900), "Test");
        View.setSize(1920, 1080);
        View.setCenter(1920 / 2, 1080 / 2);
        App.setView(View);
        sf::RectangleShape box (sf::Vector2f(100, 100));
        box.setPosition(100, 100);

        while (App.isOpen()) {
                while (App.pollEvent(Event)) {
                        if (Event.type == Event.KeyPressed && Event.key.code == sf::Keyboard::Space)
                                pauseScreen();

                        if (Event.type == Event.Closed || (Event.type == Event.KeyPressed && Event.key.code == sf::Keyboard::Escape))
                                App.close();
                        }
                App.clear(sf::Color::Blue);
                App.draw(box);
                App.display();
                }

        return 0;
        }

 
« Last Edit: January 01, 2016, 12:34:27 pm by IncredibleSulk »

IncredibleSulk

  • Guest
Re: Updating Texture from Window with Views
« Reply #1 on: January 01, 2016, 12:33:16 pm »
Answered my own question eventually. For anyone who's interested you need to create a Texture the size of the window you are updating your texture from, and scale the sprite you create  from it, to the resolution of the view you are drawing to. Works a charm

        sf::Texture tex;
        tex.create(App.getSize().x, App.getSize().y);
        tex.update(App);

        sf::Sprite background(tex);

        float xScale = (double)((double)(View.getSize().x - App.getSize().x) / App.getSize().x);
        float yScale = (double)((double)(View.getSize().y - App.getSize().y) / App.getSize().y);

        background.setScale(xScale > 0 ? 1 + xScale : abs(xScale), yScale > 0 ? 1 + yScale : abs(yScale));
 

 

anything