SFML community forums
Help => Graphics => Topic started by: Lacrymology on February 26, 2012, 08:27:30 pm
-
I'm starting up with this, and I'm having a little 2D issue: how to draw a sprite on a
sf::RenderWindow
so it has its texture's original size regardless of window resizing?
What about having it render over the whole screen, stretching as necessary?
here's my code:
void handleEvents(sf::Window &App) {
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
// Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
// Resize event : adjust viewport
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
}
}
void glPerFrame() {
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
int main() {
// Create the main window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML OpenGL");
sf::Image depthBuffer;
// Start game loop
while (App.IsOpened())
{
handleEvents(App);
// Set the active window before using OpenGL commands
// It's useless here because active window is always the same,
// but don't forget it if you use multiple windows or controls
App.SetActive();
glPerFrame();
// Use the depth buffer as texture
updateDepthBuffer(depthBuffer); // This takes a Kinect depth image stream and unpacks it into the provided sf::Image
sf::Sprite dbSprite(depthBuffer);
float appW = App.GetWidth(), appH = App.GetHeight();
dbSprite.Resize(appW, appH);
App.Draw(dbSprite);
dbSprite.SetScale(1,1);
App.Draw(dbSprite);
// Finally, display rendered frame on screen
App.Display();
}
return EXIT_SUCCESS;
}
This only looks right (this is, the first call to Draw draws over the whole window, and the second draws on the depthBuffer original size) when the window has the original size it was created with: if I make it bigger, both images get stretched, so the bigger one escapes the window, and the smaller one becomes bigger, and if I make it smaller I end up with black borders.
-
so you want to keep the aspect ratio? scale the image by the smallest (width or height of the window), then position it by the largest minus the smallest divided by two (assuming you want to maintain the centre position), hope this helps! :D
-
no, see.. my original texture is 320x240.
I want the sprite that is scaled 1x1 to be 320x240, and the sprite scaled widthXheight to be fullscreen.
This SHOULD have to do with the viewport. My best guess is that the RenderWindow doesn't update it's internally used viewport matrix when it's resized
-
I think you are correct on your last assumption. You should try and process the window resize event to update its viewport if you want the resolution to change on resizing.
-
@ Lacrymology:
How did you convert the Kinect DepthData into a sf::image? Can you post the source code of the updateDepthBuffer() function?