2019-12-04 23:15:49 +00:00
|
|
|
use sdl2::pixels::{Color, PixelFormatEnum};
|
2019-12-20 15:48:24 +00:00
|
|
|
use sdl2::rect::Rect;
|
2020-01-02 22:48:41 +00:00
|
|
|
use sdl2::render::{Texture, TextureCreator, WindowCanvas};
|
2019-12-29 21:38:51 +00:00
|
|
|
use sdl2::video::{Window, WindowContext};
|
2019-12-04 23:15:49 +00:00
|
|
|
|
|
|
|
use rustboyadvance_ng::core::gpu::{DISPLAY_HEIGHT, DISPLAY_WIDTH};
|
|
|
|
use rustboyadvance_ng::VideoInterface;
|
|
|
|
|
2019-12-29 21:38:51 +00:00
|
|
|
pub const SCREEN_WIDTH: u32 = DISPLAY_WIDTH as u32;
|
|
|
|
pub const SCREEN_HEIGHT: u32 = DISPLAY_HEIGHT as u32;
|
|
|
|
pub const SCALE: u32 = 3; // TODO control via CLI & support window resize
|
2019-12-04 23:15:49 +00:00
|
|
|
|
2020-01-02 22:48:41 +00:00
|
|
|
pub struct Sdl2Video<'a> {
|
|
|
|
_tc: TextureCreator<WindowContext>, // only kept alive because of the texture
|
|
|
|
texture: Texture<'a>, // TODO - what happens if _tc is destroyed first ?
|
2019-12-04 23:15:49 +00:00
|
|
|
canvas: WindowCanvas,
|
|
|
|
}
|
|
|
|
|
2020-01-02 22:48:41 +00:00
|
|
|
impl<'a> Sdl2Video<'a> {
|
2019-12-04 23:15:49 +00:00
|
|
|
pub fn set_window_title(&mut self, title: &str) {
|
2019-12-20 15:48:24 +00:00
|
|
|
self.canvas.window_mut().set_title(&title).unwrap();
|
2019-12-04 23:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 22:48:41 +00:00
|
|
|
impl<'a> VideoInterface for Sdl2Video<'a> {
|
2019-12-04 23:15:49 +00:00
|
|
|
fn render(&mut self, buffer: &[u32]) {
|
2020-01-02 22:48:41 +00:00
|
|
|
self.texture
|
2019-12-20 15:48:24 +00:00
|
|
|
.update(
|
|
|
|
None,
|
|
|
|
unsafe { std::mem::transmute::<&[u32], &[u8]>(buffer) },
|
|
|
|
(SCREEN_WIDTH as usize) * 4,
|
|
|
|
)
|
2019-12-04 23:15:49 +00:00
|
|
|
.unwrap();
|
|
|
|
self.canvas
|
|
|
|
.copy(
|
2020-01-02 22:48:41 +00:00
|
|
|
&self.texture,
|
2019-12-04 23:15:49 +00:00
|
|
|
None,
|
|
|
|
Some(Rect::new(0, 0, SCREEN_WIDTH * SCALE, SCREEN_HEIGHT * SCALE)),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
self.canvas.present();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 22:48:41 +00:00
|
|
|
pub fn create_video_interface<'a>(window: Window) -> Sdl2Video<'a> {
|
2019-12-04 23:15:49 +00:00
|
|
|
let mut canvas = window.into_canvas().accelerated().build().unwrap();
|
|
|
|
canvas.set_draw_color(Color::RGB(0, 0, 0));
|
|
|
|
canvas.clear();
|
2020-01-02 22:48:41 +00:00
|
|
|
let mut tc = canvas.texture_creator();
|
|
|
|
let texture = unsafe {
|
|
|
|
let tc_ptr = &mut tc as *mut TextureCreator<WindowContext>;
|
|
|
|
(*tc_ptr)
|
|
|
|
.create_texture_streaming(PixelFormatEnum::BGRA32, SCREEN_WIDTH, SCREEN_HEIGHT)
|
|
|
|
.unwrap()
|
|
|
|
};
|
2019-12-09 21:37:46 +00:00
|
|
|
Sdl2Video {
|
2020-01-02 22:48:41 +00:00
|
|
|
_tc: tc,
|
|
|
|
texture: texture,
|
2019-12-04 23:15:49 +00:00
|
|
|
canvas: canvas,
|
|
|
|
}
|
|
|
|
}
|