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;
}