diff --git a/Cargo.toml b/Cargo.toml index d29ecb5..55895b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ name = "kubi" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] +glium = "0.32" +glutin = "*" +image = { version = "0.24", default_features = false, features = ["png"] } diff --git a/assets/spritesheet.png b/assets/spritesheet.png new file mode 100644 index 0000000..aef1fb9 Binary files /dev/null and b/assets/spritesheet.png differ diff --git a/src/game.rs b/src/game.rs new file mode 100644 index 0000000..5afd196 --- /dev/null +++ b/src/game.rs @@ -0,0 +1,33 @@ +use glium::Surface; +use glutin::{ + event::{Event, WindowEvent}, + event_loop::{EventLoop, ControlFlow}, +}; + +mod assets; +mod display; + +use assets::Assets; +use display::init_display; + +pub fn run() { + let event_loop = EventLoop::new(); + let display = init_display(&event_loop); + let assets = Assets::load_all_sync(&display); + + event_loop.run(move |ev, _, control_flow| { + match ev { + Event::WindowEvent { event, .. } => match event { + WindowEvent::CloseRequested => { + *control_flow = ControlFlow::Exit; + return + }, + _ => () + }, + _ => () + } + let mut target = display.draw(); + target.clear_color_and_depth((0.5, 0.5, 1., 1.), 1.); + target.finish().unwrap(); + }); +} diff --git a/src/game/assets.rs b/src/game/assets.rs new file mode 100644 index 0000000..db34f2e --- /dev/null +++ b/src/game/assets.rs @@ -0,0 +1,15 @@ +pub mod textures; + +use textures::Textures; + +pub struct Assets { + pub textures: Textures +} +impl Assets { + /// Load all assets synchronously + pub fn load_all_sync(display: &glium::Display) -> Self { + Self { + textures: Textures::load_sync(display) + } + } +} diff --git a/src/game/assets/textures.rs b/src/game/assets/textures.rs new file mode 100644 index 0000000..b1e846c --- /dev/null +++ b/src/game/assets/textures.rs @@ -0,0 +1,36 @@ +use std::{fs, io}; +use glium::texture::{RawImage2d, SrgbTexture2d}; + +fn load_png(file_path: &str, display: &glium::Display) -> SrgbTexture2d { + //Load file + let data = fs::read(file_path) + .expect(&format!("Failed to load texture: {}", file_path)); + + //decode image data + let image_data = image::load( + io::Cursor::new(&data), + image::ImageFormat::Png + ).unwrap().to_rgba8(); + + //Create raw glium image + let image_dimensions = image_data.dimensions(); + let raw_image = RawImage2d::from_raw_rgba_reversed( + &image_data.into_raw(), + image_dimensions + ); + + //Create texture + SrgbTexture2d::new(display, raw_image).unwrap() +} + +pub struct Textures { + block_atlas: SrgbTexture2d +} +impl Textures { + /// Load textures synchronously, one by one and upload them to the GPU + pub fn load_sync(display: &glium::Display) -> Self { + Self { + block_atlas: load_png("assets/spritesheet.png", display) + } + } +} diff --git a/src/game/display.rs b/src/game/display.rs new file mode 100644 index 0000000..bd4b7a2 --- /dev/null +++ b/src/game/display.rs @@ -0,0 +1,8 @@ +use glium::Display; +use glutin::event_loop::EventLoop; + +pub fn init_display(event_loop: &EventLoop<()>) -> Display { + let wb = glutin::window::WindowBuilder::new(); + let cb = glutin::ContextBuilder::new().with_depth_buffer(24); + Display::new(wb, cb, &event_loop).unwrap() +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..0680567 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +mod game; + fn main() { - println!("Hello, world!"); + game::run(); }