mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-12-25 05:08:21 -06:00
setup basic window and asset loading
This commit is contained in:
parent
63b734540a
commit
c27b226d4d
|
@ -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"] }
|
||||
|
|
BIN
assets/spritesheet.png
Normal file
BIN
assets/spritesheet.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 222 KiB |
33
src/game.rs
Normal file
33
src/game.rs
Normal file
|
@ -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();
|
||||
});
|
||||
}
|
15
src/game/assets.rs
Normal file
15
src/game/assets.rs
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
36
src/game/assets/textures.rs
Normal file
36
src/game/assets/textures.rs
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
8
src/game/display.rs
Normal file
8
src/game/display.rs
Normal file
|
@ -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()
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
mod game;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
game::run();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue