Skip to content

Commit

Permalink
Make sdl displays with tv
Browse files Browse the repository at this point in the history
  • Loading branch information
fazibear committed Oct 22, 2023
1 parent 550d254 commit cc858ff
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 26 deletions.
189 changes: 173 additions & 16 deletions fazic_sdl/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion fazic_sdl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ rand = "*"
simple_logger = "*"
log = "*"
fazic = { path = "../fazic_lib" }
sdl2 = "*"
sdl2 = { version = "0.32.0", features = ["image"] }
2 changes: 1 addition & 1 deletion fazic_sdl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn main() {
let mut window = window::Window::new(&ctx);
let mut events = events::Events::new(&ctx);

let timer = ctx.timer().unwrap();
let mut timer = ctx.timer().unwrap();

let mut fps_last_time = 0;

Expand Down
39 changes: 31 additions & 8 deletions fazic_sdl/src/window.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use fazic::config::*;
use sdl2::pixels::PixelFormatEnum;
use sdl2::image::LoadTexture;

const SCALE: f32 = 2.0;
const WIDTH: u32 = SCREEN_WIDTH as u32 * SCALE as u32;
const HEIGHT: u32 = SCREEN_HEIGHT as u32 * SCALE as u32;
const RGB_WIDTH: usize = SCREEN_WIDTH as usize * 3;

const WIDTH: u32 = 640;
const HEIGHT: u32 = 480;

const WINDOW_WIDTH: u32 = 1140;
const WINDOW_HEIGHT: u32 = 698;

pub struct Window {
canvas: sdl2::render::Canvas<sdl2::video::Window>,
}
Expand All @@ -15,19 +19,30 @@ impl Window {
let video_ctx = ctx.video().unwrap();

let window = video_ctx
.window("fazic", WIDTH, HEIGHT)
.window("fazic", WINDOW_WIDTH, WINDOW_HEIGHT)
.position_centered()
.opengl()
.build()
.unwrap();

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

Self {
canvas: window.into_canvas().build().unwrap(),
}
// let _image_context = sdl2::image::init(InitFlag::PNG | InitFlag::JPG).unwrap();

Self { canvas }
}

pub fn update(&mut self, pixels: &[u8]) {
let texture_creator = self.canvas.texture_creator();

let tv_path = std::path::Path::new("tv.jpg");
let tv_texture = texture_creator.load_texture(&tv_path).unwrap();

self.canvas.copy(&tv_texture, None, None).unwrap();

let mut texture = texture_creator
.create_texture_streaming(
PixelFormatEnum::RGB24,
Expand All @@ -46,7 +61,15 @@ impl Window {
}

texture.update(None, rgb.as_slice(), RGB_WIDTH).unwrap();
self.canvas.copy(&texture, None, None).unwrap();
self.canvas.copy_ex(
&texture,
None,
Some(sdl2::rect::Rect::new(165, 110, WIDTH, HEIGHT)),
0.0,
None,
false,
false,
).unwrap();
self.canvas.present();
}
}
Binary file added fazic_sdl/tv.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cc858ff

Please sign in to comment.