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.