-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_texture.rs
More file actions
91 lines (82 loc) · 2.78 KB
/
example_texture.rs
File metadata and controls
91 lines (82 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use libforge::{Color, LibContext, Rect, TextureId};
use std::sync::Arc;
use winit::{
application::ApplicationHandler,
dpi::PhysicalSize,
event::WindowEvent,
event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
window::{Window, WindowId},
};
struct App {
window: Option<Arc<Window>>,
ctx: Option<LibContext<Arc<Window>>>,
texture: Option<TextureId>,
}
impl ApplicationHandler for App {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let window_attributes = Window::default_attributes()
.with_title("libforge - hello_texture")
.with_inner_size(PhysicalSize::new(1024, 768));
let window = Arc::new(event_loop.create_window(window_attributes).unwrap());
self.window = Some(window.clone());
let mut ctx = LibContext::new_from_window(window).unwrap();
// Load texture once at startup
let bytes = include_bytes!("tennis-clay-court.png");
let tex = ctx
.load_texture_from_bytes("tennis_court", bytes)
.expect("Failed to load texture");
self.texture = Some(tex);
self.ctx = Some(ctx);
}
fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
_window_id: WindowId,
event: WindowEvent,
) {
match event {
WindowEvent::CloseRequested => {
event_loop.exit();
}
WindowEvent::Resized(size) => {
if let Some(ctx) = &mut self.ctx {
ctx.resize(size.width, size.height);
if let Some(window) = &self.window {
window.request_redraw();
}
}
}
WindowEvent::RedrawRequested => {
if let Some(ctx) = &mut self.ctx
&& let Some(tex) = self.texture
{
ctx.clear_background(Color([0.1, 0.1, 0.15, 1.0]));
// Draw texture filling most of the window
ctx.draw_texture(
tex,
Rect {
x: 50.0,
y: 50.0,
w: 924.0,
h: 668.0,
},
Color([1.0, 1.0, 1.0, 1.0]), // white tint (no modification)
);
ctx.end_frame().expect("end_frame failed");
}
}
_ => {}
}
}
}
fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
let event_loop = EventLoop::new()?;
event_loop.set_control_flow(ControlFlow::Poll);
let mut app = App {
window: None,
ctx: None,
texture: None,
};
event_loop.run_app(&mut app)?;
Ok(())
}