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

Author Topic: SFML 2RC sf::View Not Updating  (Read 2726 times)

0 Members and 1 Guest are viewing this topic.

vidjogamer

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
SFML 2RC sf::View Not Updating
« on: April 30, 2012, 02:46:42 am »
I am trying to zoom and pan my view with the keyboard. I can manipulate the view when I first create my window... just not during the loop.  Am I doing this the wrong way?

#include <stdexcept>
#include <iostream>
#include <vector>
#include <stdlib.h>

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

using namespace std;


int main(int argc, char** argv) {
       
        //set up a clock to keep track of time
        sf::Clock clock;

        sf::RenderWindow *window;       //sfml window object

        //create a new SFML rendering window
        window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "SFML Test");
        sf::View *view;
        view = const_cast<sf::View*>( &window->getView() );
        view->zoom(2);

        //prevent crazy frame rates (and make the framerate pseudo-sync with the physics stepping, though this is a crappy method of doing so)
        window->setVerticalSyncEnabled(true);
       
               
        sf::Texture texture;
        if (!texture.loadFromFile("./images/blah.jpg")) throw runtime_error("could not load blah.png");
        sf::Sprite sprite;
        sprite.setPosition(300,300);
        sprite.setTexture(texture);
        sf::Vector2f spriteSize = sf::Vector2f(sprite.getLocalBounds().width, sprite.getLocalBounds().height);
        sprite.setOrigin(spriteSize.x/2, spriteSize.y/2);
        sprite.setScale(0.1,0.1);
               

        sf::Event sfmlEvent;
        //start the game loop
        while ( window->isOpen() )
        {
                //check for window closing events
                while (window->pollEvent(sfmlEvent)) {
                        if (sfmlEvent.type == sf::Event::Closed) window->close();
                        if (sfmlEvent.type == sf::Event::KeyPressed) {
                                if (sfmlEvent.key.code == sf::Keyboard::Escape) window->close();
                        }
                }

                //get the current "frame time" (seconds elapsed since the previous frame, hopefully close to 1/60 since vsync is enabled)
                float frameTime = clock.restart().asSeconds();
                       
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Comma)) ( view->zoom(1.1) );
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Period)) ( view->zoom(0.9) );

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) ( view->move(-1,0) );
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) ( view->move(1,0) );
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) ( view->move(0,1) );
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) ( view->move(0,-1) );
       
               
                //clear the window's rendering area before drawing
                window->clear();

                window->draw(sprite);

                //update the rendering window with all the latest drawn items
                window->display();
        }

        //clean up before exiting
        delete window;
       
        return EXIT_SUCCESS;
}
 

« Last Edit: April 30, 2012, 08:04:13 am by Laurent »

vidjogamer

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
Re: SFML 2RC sf::View Not Updating
« Reply #1 on: April 30, 2012, 07:22:50 am »
Oh, looks like I just had to setView() and it works.

Does anyone know why I dont have to call setView for it to work the first time I manipulate the view?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10818
    • View Profile
    • development blog
    • Email
Re: SFML 2RC sf::View Not Updating
« Reply #2 on: April 30, 2012, 07:39:40 am »
Oh, looks like I just had to setView() and it works.

Does anyone know why I dont have to call setView for it to work the first time I manipulate the view?

I guess it's because your using const_cast<>, which will remove the const property and thus you're able to change the window view directly which is a very bad practice! :-\
Just getView first into a sperate sf::View, zoom or do other stuff and then setView again.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: SFML 2RC sf::View Not Updating
« Reply #3 on: April 30, 2012, 08:05:07 am »
Quote
view = const_cast<sf::View*>( &window->getView() );
Don't you feel like you're fighting against the intended usage of the API? :P

Read the doc and learn how to manipulate views correctly (yes I know, ther's no tutorial yet but the doc is detailed and there are many examples on this forum).
Laurent Gomila - SFML developer