So, I'm fairly new to SFML and I've run into some unusual behaviour. I'm wondering if I'm using the zoom function incorrectly.
I've adapted my camera code to mostly work with sf::View. It scrolls across the map to my satisfaction. Until I use zoom. Then the map coasts off into oblivion on load.
I'm going to paraphrase my code, to copy it would clutter up the post
Camera.cpp:
void camApplyTranslation()
{
Camera.wView.setCenter(sf::Vector2f(Camera.x, Camera.y));
// Problem here
// Camera.wView.zoom(Camera.z);
}
void camInitCamera()
{
// ... other camera initializations
Camera.wView.setCenter(sf::Vector2f(0,0));
Camera.wView.setSize(1024, 768);
Camera.wView.zoom(1.0);
Window.setView(Camera.wView);
camLookAt(200.0,200.0,1.0); // Move to view these coordinates on load
}
Game.cpp
void GameLoop()
{
camInitCamera();
while (Window.isOpen())
{
// Controls
Window.clear();
camPollCamera();
camApplyTranslation();
Window.setView(Camera.wView);
//Draw stuff
Window.display();
}
}
The code in it's current state works, minus the zoom functionality. When I uncomment that zoom line, as soon as the map loads I can see it zoom out until the map is invisible. I had a look at the variables per-frame and there's nothing that would tell me something is wrong. Camera.z starts out at 5.0f and slowly decreases to 1.0f every frame. Instead each frame is zooming the map away from the camera.
So, to establish a baseline, I changed that line to Camera.wView.zoom(1.0);
Which worked, of course minus the zoom, being the same value every frame. So that tells me it's not zooming out by x every frame. I've played around enough with the static value on wView.zoom() to understand what range I'm looking at. The documentation I've come across seems to agree with me. zoom(0.5) would effectively zoom in half way into the map. zoom(2.0) would zoom out away from the map.
The camPollCamera() is a bit long, but in short what it does is manage a few floats representing the camera position and target position. As I mentioned, I checked that the values are in acceptable ranges using my debugger. It seems with static values, the zoom behaves like a static value. With a variable in place, it behaves like the value is cumulative.
I tested that, by rigging a test float to it. Started it out at 0.0 and each frame would increase it by 0.05, capping it at 1.1. In theory that should start the camera right up to the map, and slowly zoom out to just outside of 'normal' view. Instead, the view just hovered just in front of the map right up close, yet the variable was behaving as expected: ramping from 0..1.1 each frame. Stranger yet, awhile after the test float had plateaued to 1.1, the map just zoomed off into oblivion anyway.
Comment that zoom line again, and everything works fine. So I'm obviously using it wrong.
Could someone enlighten me, or point me to some reading materials? (Yes, I've read most of the documentation on this site - though might not have completely exhausted the forums yet.)
Using SFML 2.1, Thanks