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

Author Topic: sfml view issue  (Read 5771 times)

0 Members and 1 Guest are viewing this topic.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sfml view issue
« Reply #15 on: September 11, 2012, 02:16:12 pm »
Why don't you try the render-texture solution? It's straight-forward to implement and should work perfectly.

Forget solutions that involve manual processing of pixels, it will be too slow.

Quote
Just another question btw, i wanted to do a wheel, and i wanted to make it spin, but when i use setOrigin, it still isn't precicely in the center... Should i make the wheel size 21 * 21 or 20 * 20?  i thought of 21*21 so that the one pixel in the center would be an axle for spinning so i setOrigin(10, 10) but that doesn't work... Any clue?
It should rather be 20x20 if the origin is set to (10, 10). Doesn't it work as expected?
Laurent Gomila - SFML developer

Omyk

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: sfml view issue
« Reply #16 on: September 11, 2012, 03:50:39 pm »
Thanks for the advice for setOrigin, that worked !

but for the main issue:
I can't try the renderTexture solution, because i don't know what it is, would you be so kind and explain it to me please ?
« Last Edit: September 11, 2012, 04:02:59 pm by Omyk »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sfml view issue
« Reply #17 on: September 11, 2012, 04:00:37 pm »
Instead of drawing directly to a window, you draw to a smaller sf::RenderTexture (the drawing API is the same as sf::RenderWindow), and then you draw the resulting texture with a scaled sprite on your window.

This way you really scale the final pixels, instead of the entities.
Laurent Gomila - SFML developer

Omyk

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: sfml view issue
« Reply #18 on: September 11, 2012, 04:23:15 pm »
Awesome, it works !

Here's a code, for those who haven't seen what i wanted to do in the first place!

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

#define REAL_WIDTH 20       //size of the original texture
#define REAL_HEIGHT 20
#define VIRTUAL_HEIGHT 400  //desired size for the window (multiple of the real size)
#define VIRTUAL_WIDTH 400

int main()
{
    sf::RenderWindow window(sf::VideoMode(VIRTUAL_WIDTH, VIRTUAL_HEIGHT), "Test");

    sf::Texture real_texture;
    real_texture.loadFromFile("spin.png");  //just make a random small sprite like 10*10
    sf::Sprite real_sprite(real_texture);
    real_sprite.setOrigin(real_sprite.getGlobalBounds().width/2, real_sprite.getGlobalBounds().height/2);
    real_sprite.setPosition(REAL_WIDTH/2, REAL_HEIGHT/2);

    sf::RenderTexture real_target;
    real_target.create(REAL_WIDTH, REAL_HEIGHT);

    sf::Sprite virtual_sprite;
    virtual_sprite.scale(VIRTUAL_WIDTH/REAL_WIDTH, VIRTUAL_HEIGHT/REAL_HEIGHT);
    virtual_sprite.setTexture(real_target.getTexture());

    window.setFramerateLimit(30);
    sf::Event event;

    while(window.isOpen())
    {
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
            window.close();

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

        real_target.clear();
        real_target.draw(real_sprite);
        real_target.display();

        //drawing
        window.clear();
        window.draw(virtual_sprite);
        window.display();

        //rotation afterwards
        real_sprite.rotate(5);

    }
    return 0;
}
 

you just need to create a png image for it to work (it's a basic example of a spinning pinwheel) ;)

Once again: thank you very much for your contribution ! :P
« Last Edit: September 11, 2012, 04:43:00 pm by Omyk »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sfml view issue
« Reply #19 on: September 11, 2012, 04:27:53 pm »
There should be a call to real_target.display() after drawing to the render-texture.

And you don't need to call setTexture again and again, the texture object inside the render-texture is still the same, even if its contents have changed.
Laurent Gomila - SFML developer

Omyk

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: sfml view issue
« Reply #20 on: September 11, 2012, 04:37:45 pm »
do i need to call real_target.display since i will display it just after with the window ?

and yeah i just remember now, the setTexture is just giving the adress of the texture to display, and the adress will stay the same since the texture is automatically updated with the renderTexture...

Edit: I just changed the code :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sfml view issue
« Reply #21 on: September 11, 2012, 04:40:12 pm »
real_target.display is needed to correctly display what you've just drawn in the texture, the same way window.display is required to see something on screen.

Without this call, the contents of the render-texture should be either missing or upside down.
Laurent Gomila - SFML developer

Omyk

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: sfml view issue
« Reply #22 on: September 11, 2012, 04:42:45 pm »
Oh yeah i see, that's why the pinwheel was spinning on counterclockwise !