Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Basic SDL2 setup, call to Canvas.clear() does nothing
I'm trying to use the Rust bindings for SDL2, but have run into an issue. I have a window opening and closing, and have added a set of calls to set the draw color to red, clear the canvas, and present to the screen, but the window is still black. My code is below. Is there anything I've missed?
init
Module:
use sdl2::{ Sdl, EventPump };
use sdl2::render::Canvas;
use sdl2::video::Window;
pub struct SdlSetup {
pub context: Sdl,
pub renderer: Canvas<Window>,
pub events: EventPump
}
impl SdlSetup {
pub fn new(w: u32, h: u32) -> Result<Self, String> {
let context = sdl2::init()?;
let window = context.video()?.window("SDL2 Window", w, h)
.position_centered()
.borderless()
.build()
.unwrap();
let renderer = window.into_canvas().build().unwrap();
let events = context.event_pump().unwrap();
Ok(SdlSetup {
context,
renderer,
events
})
}
}
main
:
mod init;
use sdl2::keyboard::Keycode;
use sdl2::event::Event;
use sdl2::pixels::Color;
use crate::init::SdlSetup;
fn main() {
let mut sdl = SdlSetup::new(800, 600).unwrap();
let mut running = true;
while running {
// Process Events
for event in sdl.events.poll_iter() {
match event {
Event::KeyDown { keycode: Some(Keycode::ESCAPE), .. } |
Event::Quit { .. } => running = false,
_ => ()
}
}
// Render
sdl.renderer.set_draw_color(Color::RGB(255, 0, 0));
sdl.renderer.clear();
sdl.renderer.present();
}
}
1 post - 1 participant
🏷️ Rust_feed