I've been working with a 2D platformer, and have been trying to implement a view in the screen. I was able to do it in SDL rather easily, though in SFML I've been trying to use sf::View in order to create a view and then set the view in the window. However, whenever I try to set a view other than the default the window has, the screen is black ( when there should be a rendered character centered on the view ). I'm not sure if it's a problem with keeping the view alive, or that it's not allowing me to blit to the screen, but I've tried hacking at it for hours to no avail.
My code:
// First level of the game.
int Game::Level1()
{
// The center view of the camera.
sf::Vector2f CenterView( ( Jim_Character.GetXPosition()
+ (float)11.5 ),
( Jim_Character.GetYPosition()
+ (float)20 ) );
// Half the total size of the view.
sf::Vector2f HalfSizeOfView( 400, 300 );
//The camera view for the game.
sf::View Camera( CenterView, HalfSizeOfView );
// Sets the view to center Jim.
MainWindow.SetCameraView( Camera );
// Loop while MainWindow is still open.
while( MainWindow.IsOpen() )
{
// Loop and process events ( if any ) in queue.
while( MainWindow.GetEvents() )
{
// Perform if the user clicks the red 'X'.
if( MainWindow.WindowEventsType() == "Closed" )
{
// Close the main window.
MainWindow.CloseWindow();
}
// Perform if the user pressed the "Esc" key.
if( MainWindow.KeyboardKeyPressed() == "Escape" )
{
// Sets the default view on the window.
MainWindow.SetDefaultCameraView();
// Return to the main menu.
MainMenu();
}
// Perform if the user pressed the "A" key.
if( MainWindow.KeyboardKeyPressed() == "A" )
{
// If Jim is not trying to go outside the level.
if( Jim_Character.GetXPosition() > 0 ||
Jim_Character.GetXPosition() == 0 )
{
// Start moving Jim left.
Jim_Move_Left = true;
}
}
// Perform if the user pressed the "D" key.
if( MainWindow.KeyboardKeyPressed() == "D" )
{
// If Jim is not trying to go outside the level.
if( ( Jim_Character.GetXPosition() + 33 ) < Level_1_Width )
{
// Start moving Jim right.
Jim_Move_Right = true;
}
}
// Perform if the user released the "A" key.
if( MainWindow.KeyboardKeyReleased() == "A" )
{
// Start moving Jim left.
Jim_Move_Left = false;
}
// Perform if the user released the "D" key.
if( MainWindow.KeyboardKeyReleased() == "D" )
{
// Start moving Jim right.
Jim_Move_Right = false;
}
}
// If Jim is to be moved to the left.
if( Jim_Move_Left == true )
{
// If Jim is not trying to go outside the level.
if( Jim_Character.GetXPosition() > 0 ||
Jim_Character.GetXPosition() == 0 )
{
// Move him to the left 4.
Jim_Character.MovePositionX( -.50f );
}
}
// If Jim is to be moved to the right.
if( Jim_Move_Right == true )
{
// If Jim is not trying to go outside the level.
if( ( Jim_Character.GetXPosition() + 33 ) < Level_1_Width )
{
// Move him to the left 4.
Jim_Character.MovePositionX( .50f );
}
}
// Renders the load game graphic.
MainWindow.Render( Jim_Character.GetSprite() );
// Clears and updates window with new data.
MainWindow.Update();
}
// The program exits successfully, and the main window is closed.
return 0;
}
Note that I use a wrapper library I made myself that incorporates SFML 1.6 ( as well as several other smaller libraries + obvious heavy additions of my own ). Therefore, some of the code above will do things that SFML does, as well as other things too. The comments should be enough to clear up any misunderstanding though, but if not I can certainly clarify. If any more code needs to be added, just ask. I would cut some irrelevant code out of the state function, but I thought it would be better to see what it's supposed to do as a whole.