Initializes a new SDL console. The console is sized to `resolution` pixels and the font is set to `font`. There can only be one active `SdlConsole` at any given time given that this initializes and owns the SDL context.
(resolution: Resolution, font: &'static Font)
| 261 | /// There can only be one active `SdlConsole` at any given time given that this initializes and |
| 262 | /// owns the SDL context. |
| 263 | fn new(resolution: Resolution, font: &'static Font) -> io::Result<Self> { |
| 264 | let sdl = sdl2::init().map_err(string_error_to_io_error)?; |
| 265 | let event_pump = sdl.event_pump().map_err(string_error_to_io_error)?; |
| 266 | let video = sdl.video().map_err(string_error_to_io_error)?; |
| 267 | |
| 268 | video.text_input().start(); |
| 269 | |
| 270 | let mut title = format!("EndBASIC {}", env!("CARGO_PKG_VERSION")); |
| 271 | let mut window = match resolution { |
| 272 | Resolution::FullScreenDesktop => { |
| 273 | let mut window = video.window(&title, 0, 0); |
| 274 | window.fullscreen_desktop(); |
| 275 | window |
| 276 | } |
| 277 | Resolution::FullScreen(size) => { |
| 278 | let mut window = video.window(&title, size.0.get(), size.1.get()); |
| 279 | window.fullscreen(); |
| 280 | window |
| 281 | } |
| 282 | Resolution::Windowed(size) => { |
| 283 | let mut window = video.window(&title, size.0.get(), size.1.get()); |
| 284 | window.position_centered(); |
| 285 | window |
| 286 | } |
| 287 | } |
| 288 | .build() |
| 289 | .map_err(window_build_error_to_io_error)?; |
| 290 | |
| 291 | // macOS causes graphical apps started from the terminal to go into the background |
| 292 | // (the end of the Command+Tab switcher), which is confusing and unexpected. We have |
| 293 | // to force the app to raise to the front. |
| 294 | #[cfg(target_os = "macos")] |
| 295 | activate_app(); |
| 296 | window.raise(); |
| 297 | |
| 298 | let size_pixels = requested_size_pixels(resolution).unwrap_or_else(|| { |
| 299 | let (width, height) = window.size(); |
| 300 | SizeInPixels::new(width.clamped_into(), height.clamped_into()) |
| 301 | }); |
| 302 | let size_chars = font.chars_in_area(size_pixels); |
| 303 | |
| 304 | write!( |
| 305 | &mut title, |
| 306 | " - {}x{} pixels, {}x{} chars", |
| 307 | size_pixels.width, size_pixels.height, size_chars.x, size_chars.y |
| 308 | ) |
| 309 | .map_err(fmt_error_to_io_error)?; |
| 310 | window.set_title(&title).expect("There should have been no NULLs in the formatted title"); |
| 311 | |
| 312 | let pixel_format = window.window_pixel_format(); |
| 313 | let surface = |
| 314 | Surface::new(u32::from(size_pixels.width), u32::from(size_pixels.height), pixel_format) |
| 315 | .map_err(string_error_to_io_error)?; |
| 316 | let mut canvas = surface.into_canvas().map_err(string_error_to_io_error)?; |
| 317 | let texture_creator = canvas.texture_creator(); |
| 318 | |
| 319 | let draw_color = RGB::default(); |
| 320 | canvas.set_draw_color(rgb_to_color(draw_color)); |
nothing calls this directly
no test coverage detected