0

(premise: the (pseudo) code is in Rust, however, answers in any language are accepted, since the Rust SDL library mimics closely the standard one).

I have a very minimal SDL2 template I use when I need to draw an image, display it (in a maximized window), and wait for a keypress.

The current logic works, however, it has a niggle: after the window is maximized (and the canvas updated), if I resize the window, redrawing doesn't occur.

This is my current pseudo-code:

let sdl_context = sdl2::init().unwrap();

let window = sdl_context
    .video()
    .unwrap()
    .window(""window_title"", width as u32, height as u32)
    .maximized()
    .position_centered()
    .resizable()
    .build()
    .unwrap();

let mut event_pump = sdl_context.event_pump().unwrap();

let mut canvas = window.into_canvas().present_vsync().build().unwrap();

canvas.set_logical_size(640, 480).unwrap();

// Needed to redraw, as the window has been maximized above.
//
event_pump.pump_events();

////////////////////////////////////////
// Here some pixels are drawn.
////////////////////////////////////////

canvas.present();

// Wait for a keypress or quit event.
//
for event in event_pump.wait_iter() {
    match event {
        Event::KeyDown { .. } => std::process::exit(0),
        Event::KeyUp { .. } => std::process::exit(0),
        Event::Quit { .. } => std::process::exit(0),
        _ => {}
    }
}

If I run this code, and resize the window without pressing any key (or quitting), the canvas doesn't redraw.

How should I change the code to make it redraw?

(note that I'm not using textures, because they were making the code more complex, and in the cases I use this template for, either speed is not a concern or there isn't a measurable speed improvement)

1 Answer 1

2

There is a WindowEvent::Resized event, so the following should work (untested):

for event in event_pump.wait_iter() {
    match event {
        Event::KeyDown { .. } => std::process::exit(0),
        Event::KeyUp { .. } => std::process::exit(0),
        Event::Quit { .. } => std::process::exit(0),
        Event::WindowEvent (WindowEvent::Resized (w, h)) => unimplemented!(), // Redraw the window
        _ => {}
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Does the redraw imply that I need to keep an internal copy of the image to be drawn? Or there is an SDL function to refresh?
You need to keep a copy of the image or to redo the drawing operations taking into account the new window size.
I see; that's something I wanted to avoid, but it turns out it's necessary. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.