I made a very simple app to test JSFML and learn how to use views, but when I tried to set the viewport, nothing happened; the view still takes up the whole screen when it should take up half. I looked through my code a few times, but I was unable to catch the problem. I've tested similar code in the c++ version and it works, so I'm not sure if there's a difference in how I need to use the viewports in java or if I'm making a silly error...
If anyone could point me to the problem I'd really be grateful.
RenderWindow window
= new RenderWindow
(); ContextSettings ctxSettings
= new ContextSettings
(8); int screenWidth
= 720; int screenHeight
= 480; window.
create(new VideoMode
(screenWidth, screenHeight
),
"JSFML Test",
RenderWindow.
CLOSE | RenderWindow.
TITLEBAR,
ctxSettings
); window.
setVerticalSyncEnabled(true); Texture bgTexture
= new Texture
(); // load the bg picture with an InputStream Vector2i worldSize
= bgTexture.
getSize(); RectangleShape bg
= new RectangleShape
(); bg.
setPosition(0,
0); bg.
setSize(new Vector2f
(bgTexture.
getSize())); bg.
setTexture(bgTexture
); View view
= new View(window.
getDefaultView().
getCenter(),
new Vector2f
(screenWidth, screenHeight
)); view.
setViewport(new FloatRect
(0,
0, 0.5f,
1.
f)); window.
setView(view
); RectangleShape player1
= new RectangleShape
(); player1.
setSize(new Vector2f
(20,
20)); player1.
setPosition(0,
0); player1.
setFillColor(Color.
RED); int player1MoveSpeed
= 200; Clock clock
= new Clock
(); while (window.
isOpen()) { window.
clear(); window.
draw(bg
); window.
draw(player1
); window.
display(); float dt
= clock.
restart().
asSeconds(); // move the player at `player1MoveSpeed` for (Event event
: window.
pollEvents()) { switch (event.
type) { case CLOSED
: window.
close(); break; } } }