1#![allow(unused)]
2
3pub use crate::camera::camera2d::Camera2D;
6pub use crate::camera::render_target::RenderTarget;
7use crate::draw_state::shapes::Shape;
8pub use crate::draw_state::shapes::color::*;
9pub use crate::end_frame;
10use crate::global::{PENDING_CONFIG, STATE, ctx};
11pub use crate::misc::flake_image::Image;
12use crate::misc::font::Font;
13pub use crate::misc::math::*;
14pub use crate::misc::rect::Rect;
15use crate::misc::span::RichtextSection;
16pub use crate::misc::span::Span;
17pub use crate::misc::timer::Timer;
18pub use crate::misc::*;
19use crate::model::instance::Instance;
20pub use crate::model::texture::DrawTextureParams;
21pub use crate::model::texture::FilterType;
22use crate::model::texture::{GpuTexture, Texture};
23pub use crate::runtime::next_frame::next_frame;
24pub use crate::{FlakeResult, flake};
25use image::{DynamicImage, GenericImageView, ImageBuffer, Rgb, Rgba};
26use std::borrow::Borrow;
27use std::sync::{Arc, Mutex};
28use wgpu::{CommandEncoder, Device, Queue, SurfaceConfiguration, TextureView};
29pub use winit::event::MouseButton;
30pub use winit::event::WindowEvent;
31pub use winit::keyboard::KeyCode;
32#[cfg(target_os = "windows")]
33use winit::platform::windows::WindowExtWindows;
34use winit::window::Window;
35pub use winit::window::{CursorIcon, WindowButtons, WindowLevel};
36pub fn flake_quit() {
46 ctx(|s| s.exit())
47}
48
49pub fn flake_sleep(seconds: f32) {
60 std::thread::sleep(std::time::Duration::from_secs_f32(seconds));
61}
62
63pub fn add_timer(timer: Timer) -> Timer {
75 if STATE.get().is_some() {
76 return ctx(|s| {
77 if !s.timers.contains(&timer) {
78 s.add_timer(timer.clone());
79 }
80 timer
81 });
82 }
83
84 PENDING_CONFIG.with_borrow_mut(|s| {
85 if !s.timers.contains(&timer) {
86 s.timers.push(timer.clone())
87 }
88 });
89
90 timer
91}
92
93pub fn get_window() -> Arc<Window> {
102 ctx(|s| s.get_window())
103}
104
105pub fn get_device() -> Arc<Device> {
113 ctx(|s| s.device.clone())
114}
115
116pub fn get_queue() -> Queue {
124 ctx(|s| s.queue.clone())
125}
126
127pub fn add_post_render_hook(
136 hook: impl FnMut(&Device, &Queue, &mut CommandEncoder, &TextureView, (u32, u32)) + 'static,
137) {
138 ctx(|s| s.post_render_hooks.push(Box::new(hook)));
139}
140
141pub fn get_surface_config() -> Arc<Mutex<SurfaceConfiguration>> {
149 ctx(|s| s.config.clone())
150}
151
152pub fn get_window_events() -> Vec<WindowEvent> {
160 ctx(|s| s.event_list.clone())
161}
162
163pub fn set_window_passthrough(passthrough: bool) {
165 if STATE.get().is_some() {
166 return ctx(|s| s.set_window_passthrough(passthrough));
167 }
168
169 PENDING_CONFIG.with_borrow_mut(|s| s.window_passthrough = Some(passthrough));
170}
171
172pub fn set_window_level(level: WindowLevel) {
174 if STATE.get().is_some() {
175 return ctx(|s| s.set_window_level(level));
176 }
177
178 PENDING_CONFIG.with_borrow_mut(|s| s.window_level = Some(level));
179}
180
181pub fn set_window_enabled_buttons(window_buttons: WindowButtons) {
184 ctx(|s| s.get_window().set_enabled_buttons(window_buttons));
185}
186
187pub fn set_skip_taskbar(skip: bool) {
189 if STATE.get().is_some() {
190 #[cfg(target_os = "windows")]
191 return ctx(|s| s.window.set_skip_taskbar(skip));
192 }
193
194 PENDING_CONFIG.with_borrow_mut(|s| s.skip_taskbar = Some(skip));
195}
196
197pub fn set_window_transparent(transparent: bool) {
200 if STATE.get().is_some() {
201 return get_window().set_transparent(transparent);
202 }
203
204 PENDING_CONFIG.with_borrow_mut(|s| s.window_transparent = Some(transparent));
205}
206
207pub fn set_window_decorations(enabled: bool) {
209 if STATE.get().is_some() {
210 return get_window().set_decorations(enabled);
211 }
212
213 PENDING_CONFIG.with_borrow_mut(|s| s.window_decorations = Some(enabled));
214}
215
216pub fn set_window_title(title: &str) {
226 if STATE.get().is_some() {
227 return ctx(|s| s.set_window_title(title));
228 }
229
230 PENDING_CONFIG.with_borrow_mut(|s| s.window_title = Some(title.into()));
231}
232
233pub fn set_window_visible(visible: bool) {
242 if STATE.get().is_some() {
243 return ctx(|s| s.set_window_visible(visible));
244 }
245
246 PENDING_CONFIG.with_borrow_mut(|s| s.window_visible = Some(visible));
247}
248
249pub fn set_window_icon(path: &str) {
257 let img = image::open(path)
258 .expect("image icon does not exist at path")
259 .into_rgba8();
260 let (width, height) = img.dimensions();
261 let rgba = img.into_raw();
262
263 if STATE.get().is_some() {
264 return ctx(|s| s.set_window_icon(rgba, width, height));
265 }
266
267 PENDING_CONFIG.with_borrow_mut(|s| s.window_icon = Some((rgba, width, height)));
268}
269
270pub fn set_window_icon_bytes(bytes: &[u8]) {
272 let img = image::load_from_memory(bytes)
273 .expect("image icon does not exist at path")
274 .into_rgba8();
275 let (width, height) = img.dimensions();
276 let rgba = img.into_raw();
277
278 if STATE.get().is_some() {
279 return ctx(|s| s.set_window_icon(rgba, width, height));
280 }
281
282 PENDING_CONFIG.with_borrow_mut(|s| s.window_icon = Some((rgba, width, height)));
283}
284
285pub fn set_window_x(x: i32) {
293 if STATE.get().is_some() {
294 return ctx(|s| s.set_window_x(x));
295 }
296
297 PENDING_CONFIG.with_borrow_mut(|s| s.window_x = Some(x));
298}
299
300pub fn set_window_y(y: i32) {
308 if STATE.get().is_some() {
309 return ctx(|s| s.set_window_y(y));
310 }
311
312 PENDING_CONFIG.with_borrow_mut(|s| s.window_y = Some(y));
313}
314
315pub fn set_window_position(x: i32, y: i32) {
323 set_window_x(x);
324 set_window_y(y);
325}
326
327pub fn window_width() -> f32 {
329 ctx(|s| s.window_width())
330}
331
332pub fn window_height() -> f32 {
334 ctx(|s| s.window_height())
335}
336
337pub fn window_size() -> Vector2 {
339 ctx(|s| Vector2::new(s.window_width(), s.window_height()))
340}
341
342pub fn window_position() -> (f32, f32) {
344 ctx(|s| s.window_position())
345}
346
347pub fn window_position_vec2() -> Vector2 {
349 ctx(|s| s.window_position().into())
350}
351
352pub fn window_x() -> f32 {
354 ctx(|s| s.window_x())
355}
356
357pub fn window_y() -> f32 {
359 ctx(|s| s.window_y())
360}
361
362pub fn set_window_width(w: u32) {
370 if STATE.get().is_some() {
371 return ctx(|s| s.set_window_w(w));
372 }
373
374 PENDING_CONFIG.with_borrow_mut(|s| s.window_width = Some(w));
375}
376
377pub fn set_window_height(h: u32) {
385 if STATE.get().is_some() {
386 return ctx(|s| s.set_window_h(h));
387 }
388
389 PENDING_CONFIG.with_borrow_mut(|s| s.window_height = Some(h));
390}
391
392pub fn set_window_size(w: u32, h: u32) {
400 if STATE.get().is_some() {
401 return ctx(|s| s.set_window_size(w, h));
402 }
403
404 PENDING_CONFIG.with_borrow_mut(|s| {
405 s.window_width = Some(w);
406 s.window_height = Some(h);
407 });
408}
409
410pub fn set_window_resizable(resizeable: bool) {
412 ctx(|s| s.window.set_resizable(resizeable));
413}
414
415pub fn get_fps() -> f32 {
426 ctx(|s| s.get_fps())
427}
428
429pub fn set_target_fps(fps: usize) {
437 if STATE.get().is_some() {
438 return ctx(|s| s.set_target_fps(fps));
439 }
440
441 PENDING_CONFIG.with_borrow_mut(|s| s.target_fps = Some(fps));
442}
443
444pub fn set_fps_capped(capped: bool) {
453 if STATE.get().is_some() {
454 return ctx(|s| s.set_fps_capped(capped));
455 }
456
457 PENDING_CONFIG.with_borrow_mut(|s| s.fps_capped = Some(capped));
458}
459
460pub fn get_frame_time() -> f32 {
469 ctx(|s| s.get_frame_time())
470}
471
472pub fn get_frames() -> usize {
481 ctx(|s| s.get_frames())
482}
483
484pub fn mouse_position() -> (f32, f32) {
494 ctx(|s| s.mouse_state.get_position().into())
495}
496
497pub fn mouse_position_vec2() -> Vector2 {
506 ctx(|s| s.mouse_state.get_position())
507}
508
509pub fn mouse_screen_position_vec2() -> Vector2 {
511 ctx(|s| s.mouse_state.get_screen_position())
512}
513
514pub fn mouse_screen_position() -> (f32, f32) {
516 ctx(|s| s.mouse_state.get_screen_position().into())
517}
518
519pub fn mouse_wheel() -> Vector2 {
528 ctx(|s| s.mouse_state.get_mouse_wheel())
529}
530
531pub fn set_mouse_icon(cursor_icon: CursorIcon) {
539 ctx(|s| s.window.set_cursor(cursor_icon))
540}
541pub fn focus_window() {
543 get_window().focus_window();
544}
545
546pub fn is_mouse_button_down(btn: MouseButton) -> bool {
557 ctx(|s| s.mouse_state.is_button_down(btn))
558}
559
560pub fn is_mouse_button_pressed(btn: MouseButton) -> bool {
570 ctx(|s| s.mouse_state.is_button_pressed(btn))
571}
572
573pub fn is_mouse_button_released(btn: MouseButton) -> bool {
584 ctx(|s| s.mouse_state.is_button_released(btn))
585}
586
587pub fn is_key_down(key: KeyCode) -> bool {
596 ctx(|s| s.keyboard_state.is_key_down(key))
597}
598
599pub fn is_key_pressed(key: KeyCode) -> bool {
609 ctx(|s| s.keyboard_state.is_key_pressed(key))
610}
611
612pub fn is_key_released(key: KeyCode) -> bool {
622 ctx(|s| s.keyboard_state.is_key_released(key))
623}
624
625pub fn get_axis(left: KeyCode, right: KeyCode) -> f32 {
627 ctx(|s| match (is_key_down(left), is_key_down(right)) {
628 (true, false) => -1.0,
629 (false, true) => 1.0,
630 _ => 0.0,
631 })
632}
633
634pub fn get_vector(left: KeyCode, right: KeyCode, up: KeyCode, down: KeyCode) -> Vector2 {
637 ctx(|s| {
638 Vector2::new(
639 match (is_key_down(left), is_key_down(right)) {
640 (true, false) => -1.0,
641 (false, true) => 1.0,
642 _ => 0.0,
643 },
644 match (is_key_down(up), is_key_down(down)) {
645 (true, false) => -1.0,
646 (false, true) => 1.0,
647 _ => 0.0,
648 },
649 )
650 })
651}
652
653pub fn clear_background(color: Color) {
664 if STATE.get().is_some() {
665 return ctx(|s| {
666 s.clear_color = Some(color);
667 s.draw_state.clear();
668 });
669 }
670
671 PENDING_CONFIG.with_borrow_mut(|c| c.window_clear_color = Some(color));
672}
673
674pub fn draw_rectangle(x: f32, y: f32, w: f32, h: f32, color: Color) {
683 draw_rectangle_rounded(x, y, w, h, 0.0, color);
684}
685
686pub fn draw_rectangle_rounded(x: f32, y: f32, w: f32, h: f32, radius: f32, color: Color) {
695 draw_rectangle_rounded_ex(x, y, w, h, radius, radius, radius, radius, color)
696}
697
698pub fn draw_rectangle_rounded_ex(
713 x: f32,
714 y: f32,
715 w: f32,
716 h: f32,
717 top_left: f32,
718 top_right: f32,
719 bottom_left: f32,
720 bottom_right: f32,
721 color: Color,
722) {
723 ctx(|s| {
724 s.draw_state.draw_rectangle(
725 x,
726 y,
727 w,
728 h,
729 top_left,
730 top_right,
731 bottom_left,
732 bottom_right,
733 color,
734 )
735 });
736}
737
738pub fn draw_rectangle_from_rect(rect: Rect, color: Color) {
746 draw_rectangle(rect.x, rect.y, rect.w, rect.h, color);
747}
748
749pub fn draw_rectangle_rounded_from_rect(rect: Rect, radius: f32, color: Color) {
758 draw_rectangle_rounded(rect.x, rect.y, rect.w, rect.h, radius, color);
759}
760
761pub fn draw_rectangle_lines_rounded_from_rect(
770 rect: Rect,
771 radius: f32,
772 thickness: f32,
773 color: Color,
774) {
775 draw_rectangle_lines_rounded(rect.x, rect.y, rect.w, rect.h, radius, thickness, color);
776}
777
778pub fn draw_rectangle_lines_rounded_from_rect_ex(
787 rect: Rect,
788 top_left: f32,
789 top_right: f32,
790 bottom_left: f32,
791 bottom_right: f32,
792 thickness: f32,
793 color: Color,
794) {
795 draw_rectangle_lines_rounded_ex(
796 rect.x,
797 rect.y,
798 rect.w,
799 rect.h,
800 top_left,
801 top_right,
802 bottom_left,
803 bottom_right,
804 thickness,
805 color,
806 );
807}
808
809pub fn draw_rectangle_rounded_from_rect_ex(
823 rect: Rect,
824 top_left: f32,
825 top_right: f32,
826 bottom_left: f32,
827 bottom_right: f32,
828 color: Color,
829) {
830 draw_rectangle_rounded_ex(
831 rect.x,
832 rect.y,
833 rect.w,
834 rect.h,
835 top_left,
836 top_right,
837 bottom_left,
838 bottom_right,
839 color,
840 );
841}
842
843pub fn draw_rectangle_lines(x: f32, y: f32, w: f32, h: f32, thickness: f32, color: Color) {
851 draw_rectangle_lines_rounded(x, y, w, h, 0.0, thickness, color)
852}
853
854pub fn draw_rectangle_lines_from_rect(rect: Rect, thickness: f32, color: Color) {
862 draw_rectangle_lines(rect.x, rect.y, rect.w, rect.h, thickness, color);
863}
864
865pub fn draw_rectangle_lines_rounded(
876 x: f32,
877 y: f32,
878 w: f32,
879 h: f32,
880 radius: f32,
881 thickness: f32,
882 color: Color,
883) {
884 draw_rectangle_lines_rounded_ex(x, y, w, h, radius, radius, radius, radius, thickness, color);
885}
886
887pub fn draw_rectangle_lines_rounded_ex(
902 x: f32,
903 y: f32,
904 w: f32,
905 h: f32,
906 top_left: f32,
907 top_right: f32,
908 bottom_left: f32,
909 bottom_right: f32,
910 thickness: f32,
911 color: Color,
912) {
913 ctx(|s| {
914 s.draw_state.draw_rectangle_lines(
915 x,
916 y,
917 w,
918 h,
919 top_left,
920 top_right,
921 bottom_left,
922 bottom_right,
923 thickness,
924 color,
925 )
926 });
927}
928
929pub fn draw_rectangle_rotated(x: f32, y: f32, w: f32, h: f32, rotation: f32, color: Color) {
938 ctx(|s| {
939 s.draw_state
940 .draw_rectangle_rotated(x, y, w, h, rotation, 0.0, 0.0, 0.0, 0.0, color)
941 });
942}
943
944pub fn draw_rectangle_lines_rotated(
947 x: f32,
948 y: f32,
949 w: f32,
950 h: f32,
951 rotation: f32,
952 thickness: f32,
953 color: Color,
954) {
955 ctx(|s| {
956 s.draw_state.draw_rectangle_lines_rotated(
957 x, y, w, h, rotation, 0.0, 0.0, 0.0, 0.0, thickness, color,
958 )
959 });
960}
961
962pub fn draw_rectangle_rounded_rotated(
966 x: f32,
967 y: f32,
968 w: f32,
969 h: f32,
970 rotation: f32,
971 radius: f32,
972 color: Color,
973) {
974 draw_rectangle_rounded_rotated_ex(x, y, w, h, rotation, radius, radius, radius, radius, color);
975}
976
977pub fn draw_rectangle_rounded_rotated_ex(
978 x: f32,
979 y: f32,
980 w: f32,
981 h: f32,
982 rotation: f32,
983 top_left: f32,
984 top_right: f32,
985 bottom_left: f32,
986 bottom_right: f32,
987 color: Color,
988) {
989 ctx(|s| {
990 s.draw_state.draw_rectangle_rotated(
991 x,
992 y,
993 w,
994 h,
995 rotation,
996 top_left,
997 top_right,
998 bottom_left,
999 bottom_right,
1000 color,
1001 )
1002 });
1003}
1004
1005pub fn draw_rectangle_lines_rounded_rotated(
1009 x: f32,
1010 y: f32,
1011 w: f32,
1012 h: f32,
1013 rotation: f32,
1014 radius: f32,
1015 thickness: f32,
1016 color: Color,
1017) {
1018 draw_rectangle_lines_rounded_rotated_ex(
1019 x, y, w, h, rotation, radius, radius, radius, radius, thickness, color,
1020 );
1021}
1022
1023pub fn draw_rectangle_lines_rounded_rotated_ex(
1027 x: f32,
1028 y: f32,
1029 w: f32,
1030 h: f32,
1031 rotation: f32,
1032 top_left: f32,
1033 top_right: f32,
1034 bottom_left: f32,
1035 bottom_right: f32,
1036 thickness: f32,
1037 color: Color,
1038) {
1039 ctx(|s| {
1040 s.draw_state.draw_rectangle_lines_rotated(
1041 x,
1042 y,
1043 w,
1044 h,
1045 rotation,
1046 top_left,
1047 top_right,
1048 bottom_left,
1049 bottom_right,
1050 thickness,
1051 color,
1052 )
1053 });
1054}
1055
1056pub fn draw_circle(x: f32, y: f32, r: f32, color: Color) {
1064 ctx(|s| s.draw_state.draw_circle(x, y, r, color));
1065}
1066
1067pub fn draw_circle_lines(x: f32, y: f32, r: f32, thickness: f32, color: Color) {
1076 ctx(|s| s.draw_state.draw_circle_lines(x, y, r, thickness, color));
1077}
1078
1079pub fn draw_triangle(x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, color: Color) {
1088 ctx(|s| s.draw_state.draw_triangle(x1, y1, x2, y2, x3, y3, color));
1089}
1090
1091pub fn draw_triangle_lines(
1100 x1: f32,
1101 y1: f32,
1102 x2: f32,
1103 y2: f32,
1104 x3: f32,
1105 y3: f32,
1106 thickness: f32,
1107 color: Color,
1108) {
1109 ctx(|s| {
1110 s.draw_state
1111 .draw_triangle_lines(x1, y1, x2, y2, x3, y3, thickness, color)
1112 });
1113}
1114
1115pub fn draw_line(x1: f32, y1: f32, x2: f32, y2: f32, thickness: f32, color: Color) {
1124 ctx(|s| s.draw_state.draw_line(x1, y1, x2, y2, thickness, color))
1125}
1126
1127pub fn draw_poly_line(points: &[Vector2], thickness: f32, color: Color, closed: bool) {
1138 ctx(|s| {
1139 s.draw_state
1140 .draw_poly_line_miter(points, thickness, color, closed)
1141 })
1142}
1143
1144pub fn draw_poly_line_rounded(points: &[Vector2], thickness: f32, color: Color, closed: bool) {
1155 ctx(|s| {
1156 s.draw_state
1157 .draw_poly_line(points, thickness, color, closed, true)
1158 })
1159}
1160
1161pub fn draw_polygon(points: &[Vector2], color: Color) {
1174 ctx(|s| s.draw_state.draw_polygon(points, color))
1175}
1176
1177pub fn draw_polygon_concave(points: &[Vector2], color: Color) {
1191 ctx(|s| s.draw_state.draw_polygon_concave(points, color))
1192}
1193
1194pub fn draw_text(t: &str, x: f32, y: f32, size: f32, font: Option<&Font>, color: Color) {
1203 ctx(|s| s.draw_state.draw_text(&s.fonts, t, x, y, size, font, color));
1204}
1205
1206pub fn draw_rich_text<'a, I>(
1225 position: Vector2,
1226 size: f32,
1227 font: Option<&Font>,
1228 color: Color,
1229 spans: I,
1230) where
1231 I: IntoIterator,
1232 I::Item: Borrow<Span<'a>>,
1233{
1234 ctx(|s| {
1235 s.draw_state
1236 .draw_richtext(position, size, font, color, spans)
1237 })
1238}
1239
1240pub fn load_font_from_path(path: &str) -> Font {
1254 if STATE.get().is_some() {
1255 return ctx(|s| {
1256 s.load_font_from_path(path);
1257 s.fonts[s.fonts.len() - 1].clone()
1258 });
1259 }
1260
1261 PENDING_CONFIG.with_borrow_mut(|s| {
1262 let font = Font::from_path(s.fonts.len() + 1, path);
1263 s.fonts.push(font.clone());
1264 font
1265 })
1266}
1267
1268pub fn load_font_from_bytes(bytes: &[u8]) -> Font {
1282 if STATE.get().is_some() {
1283 return ctx(|s| {
1284 s.load_font_from_bytes(bytes);
1285 s.fonts[s.fonts.len() - 1].clone()
1286 });
1287 }
1288
1289 PENDING_CONFIG.with_borrow_mut(|s| {
1290 let font = Font::from_bytes(s.fonts.len() + 1, bytes);
1291 s.fonts.push(font.clone());
1292 font
1293 })
1294}
1295
1296pub fn measure_text(t: &str, size: f32, font: Option<&Font>) -> Vector2 {
1298 let (w, h) = ctx(|s| s.measure_text(t, size, font));
1299 Vector2::new(w, h)
1300}
1301
1302pub fn draw_arrow(
1305 x1: f32,
1306 y1: f32,
1307 x2: f32,
1308 y2: f32,
1309 thickness: f32,
1310 head_size: f32,
1311 color: Color,
1312) {
1313 ctx(|s| {
1314 s.draw_state
1315 .draw_arrow(x1, y1, x2, y2, thickness, head_size, color)
1316 });
1317}
1318
1319pub fn screen_size() -> Vector2 {
1321 let win = get_window();
1322 match win.current_monitor() {
1323 Some(m) => {
1324 let s = m.size();
1325 Vector2::new(s.width as f32, s.height as f32)
1326 }
1327 None => window_size(),
1328 }
1329}
1330
1331pub fn screen_width() -> f32 {
1332 screen_size().x
1333}
1334pub fn screen_height() -> f32 {
1335 screen_size().y
1336}
1337
1338pub fn create_empty_texture(width: u32, height: u32, filter_type: FilterType) -> Texture {
1342 ctx(|s| {
1343 let gpu = GpuTexture::empty(
1344 &s.device,
1345 width,
1346 height,
1347 filter_type,
1348 wgpu::TextureFormat::Rgba8UnormSrgb,
1349 )
1350 .expect("Couldn't create empty GpuTexture");
1351
1352 Texture {
1353 id: s.add_gpu_texture(gpu, false),
1354 width,
1355 height,
1356 }
1357 })
1358}
1359
1360pub fn load_texture(path: &str) -> Texture {
1362 ctx(|s| {
1363 let image = image::load_from_memory(&std::fs::read(path).unwrap()).unwrap();
1364 let width = image.width();
1365 let height = image.height();
1366
1367 Texture {
1368 id: s.add_texture(image, FilterType::Linear),
1369 width,
1370 height,
1371 }
1372 })
1373}
1374
1375pub fn load_texture_with_filter_type(path: &str, filter_type: FilterType) -> Texture {
1377 ctx(|s| {
1378 let image = image::load_from_memory(&std::fs::read(path).unwrap()).unwrap();
1379 let width = image.width();
1380 let height = image.height();
1381
1382 Texture {
1383 id: s.add_texture(image, filter_type),
1384 width,
1385 height,
1386 }
1387 })
1388}
1389
1390pub fn load_texture_from_bytes(bytes: &[u8]) -> Texture {
1392 ctx(|s| {
1393 let image = image::load_from_memory(bytes).unwrap();
1394 let (width, height) = image.dimensions();
1395
1396 Texture {
1397 id: s.add_texture(image, FilterType::Linear),
1398 width,
1399 height,
1400 }
1401 })
1402}
1403
1404pub fn load_texture_from_bytes_with_filter_type(bytes: &[u8], filter_type: FilterType) -> Texture {
1406 ctx(|s| {
1407 let image = image::load_from_memory(bytes).unwrap();
1408 let (width, height) = image.dimensions();
1409
1410 Texture {
1411 id: s.add_texture(image, filter_type),
1412 width,
1413 height,
1414 }
1415 })
1416}
1417
1418pub fn load_texture_from_data(width: u32, height: u32, data: &[f32]) -> Texture {
1420 ctx(|s| {
1421 let img_buf = ImageBuffer::<Rgba<f32>, Vec<f32>>::from_raw(width, height, data.to_vec())
1422 .expect("Buffer size mismatch");
1423
1424 let dynamic_img = DynamicImage::ImageRgba32F(img_buf);
1425
1426 Texture {
1427 id: s.add_texture(dynamic_img, FilterType::Linear),
1428 width,
1429 height,
1430 }
1431 })
1432}
1433
1434pub fn load_texture_from_data_with_filter_type(
1438 width: u32,
1439 height: u32,
1440 data: &[f32],
1441 filter_type: FilterType,
1442) -> Texture {
1443 ctx(|s| {
1444 let img_buf = ImageBuffer::<Rgba<f32>, Vec<f32>>::from_raw(width, height, data.to_vec())
1445 .expect("Buffer size mismatch");
1446
1447 let dynamic_img = DynamicImage::ImageRgba32F(img_buf);
1448
1449 Texture {
1450 id: s.add_texture(dynamic_img, filter_type),
1451 width,
1452 height,
1453 }
1454 })
1455}
1456
1457pub fn load_texture_from_image(image: &Image) -> Texture {
1459 let tex = create_empty_texture(image.width, image.height, image.filter_type);
1460 update_texture(tex, image);
1461
1462 tex
1463}
1464
1465pub fn draw_texture(texture: Texture, x: f32, y: f32, w: f32, h: f32, tint: Color) {
1468 ctx(|s| s.draw_state.draw_texture(texture, x, y, w, h, tint))
1469}
1470
1471pub fn draw_texture_ex(texture: Texture, x: f32, y: f32, p: DrawTextureParams) {
1474 ctx(|s| s.draw_state.draw_texture_ex(texture, x, y, p))
1475}
1476
1477pub fn update_texture(texture: Texture, image: &Image) {
1479 ctx(|s| s.update_texture(texture.id, image))
1480}
1481
1482pub fn render_target(width: u32, height: u32, filter_type: FilterType) -> RenderTarget {
1484 ctx(|s| s.create_render_target(width, height, filter_type))
1485}
1486
1487pub fn set_camera(cam: &Camera2D) {
1489 ctx(|s| s.set_camera(cam))
1490}
1491
1492pub fn set_default_camera() {
1494 ctx(|s| s.set_default_camera())
1495}