From 3c6adc8301bff7e405340c27068399e545d570e4 Mon Sep 17 00:00:00 2001 From: Nicolas THIERRY Date: Mon, 6 May 2024 01:00:53 +0100 Subject: [PATCH] feat: new colors + background --- assets/background_tile.png | Bin 0 -> 2774 bytes src/bezier.rs | 16 ++++---- src/colors.rs | 9 +++++ src/main.rs | 80 ++++++++++++++++++++++++++++--------- 4 files changed, 79 insertions(+), 26 deletions(-) create mode 100644 assets/background_tile.png create mode 100644 src/colors.rs diff --git a/assets/background_tile.png b/assets/background_tile.png new file mode 100644 index 0000000000000000000000000000000000000000..69be2111ccdf84505de4f29ccf49925419687a21 GIT binary patch literal 2774 zcmeAS@N?(olHy`uVBq!ia0y~yU;#2&7+9ErRIjnw1`sdZ(btiIVPik{pF~z5UnsyQ z#5FQ9Iy*ZzIy%?SW>G0y07%hb7@#gx81K zSnK+f_xa2gjAWQGss$JV8@L;GA1&IpmGj*GP2aaO&KV69GDF}7>wz~Lr#$$*tL)#2 zd-vH2n8-9|gljWn`rz@3VS;Rf@B{u49u(wze>TGm+i5~I@z>AE-~4@>!H;~CM!3#5 zOb_xl&dJSf`n>+l-&_Xu5gr`md%uj)pjt=u$G&xQ?MJH_vV*aqm%(O|RlR}lA(4rz Rz3za_@pScbS?83{1OOF$a6JG3 literal 0 HcmV?d00001 diff --git a/src/bezier.rs b/src/bezier.rs index efe9779..706e945 100644 --- a/src/bezier.rs +++ b/src/bezier.rs @@ -2,6 +2,8 @@ use ::core::cell::RefCell; use raylib::prelude::*; use std::rc::Rc; +use crate::colors::*; + pub fn binomial(n: u64, k: u64) -> u64 { if n >= 63 { panic!("N is too great, will overflow!"); @@ -131,7 +133,7 @@ impl PointGui for JoinPoint { self.radius = radius; } fn get_color(&self) -> Color { - Color::BLUE + COLOR_BLUE } fn is_hovered(&self) -> bool { self.is_hovered @@ -245,7 +247,7 @@ impl PointGui for ControlPoint { self.radius = radius; } fn get_color(&self) -> Color { - Color::WHITE + COLOR_LIGHT } fn is_hovered(&self) -> bool { self.is_hovered @@ -376,7 +378,7 @@ pub fn draw_bezier(points: &[impl PointGui], d: &mut RaylibDrawHandle, t: Option line_points[0].get_position(), line_points[1].get_position(), 3.0, - Color::RED, + COLOR_RED, ); } @@ -392,11 +394,11 @@ pub fn draw_bezier(points: &[impl PointGui], d: &mut RaylibDrawHandle, t: Option .collect::>(); // Drawing lines before points so that points will override them for p in next_points.windows(2) { - d.draw_line_ex(p[0], p[1], 2.0, Color::RED); + d.draw_line_ex(p[0], p[1], 2.0, COLOR_RED); } // Draw lerp points for this run for p in next_points.iter() { - d.draw_rectangle_v(*p - rec_size * 0.5, rec_size, Color::GREEN); + d.draw_rectangle_v(*p - rec_size * 0.5, rec_size, COLOR_GREEN); } debug_points = next_points; } @@ -409,11 +411,11 @@ pub fn draw_bezier(points: &[impl PointGui], d: &mut RaylibDrawHandle, t: Option .collect::>(); for line_points in step_points.windows(2) { - d.draw_line_ex(line_points[0], line_points[1], 3.0, Color::GREEN); + d.draw_line_ex(line_points[0], line_points[1], 3.0, COLOR_GREEN); } if let Some(final_point) = final_point { - d.draw_circle_v(final_point, POINTS_RADIUS / 2.0, Color::YELLOW); + d.draw_circle_v(final_point, POINTS_RADIUS / 2.0, COLOR_YELLOW); } for point in points.iter() { diff --git a/src/colors.rs b/src/colors.rs new file mode 100644 index 0000000..8aed496 --- /dev/null +++ b/src/colors.rs @@ -0,0 +1,9 @@ +use raylib::prelude::*; + +pub static COLOR_RED: Color = Color::new(232, 57, 53, 255); +pub static COLOR_GREEN: Color = Color::new(54, 184, 62, 255); +pub static COLOR_YELLOW: Color = Color::new(227, 210, 70, 255); +pub static COLOR_BLUE: Color = Color::new(67, 130, 232, 255); +pub static COLOR_DARK: Color = Color::new(0, 0, 0, 255); +pub static COLOR_BLACK: Color = Color::new(0, 0, 0, 255); +pub static COLOR_LIGHT: Color = Color::new(247, 251, 252, 255); diff --git a/src/main.rs b/src/main.rs index 649e458..6b41f94 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use ::core::cell::RefCell; +use raylib::core::texture::Image; use raylib::prelude::*; use std::ffi::CStr; use std::rc::Rc; @@ -7,6 +8,9 @@ use std::time::SystemTime; mod bezier; use bezier::*; +mod colors; +use colors::*; + enum Scenes { BezierCurve, BezierSpline, @@ -22,6 +26,13 @@ fn main() { rl_handle.set_exit_key(None); rl_handle.set_window_state(rl_handle.get_window_state().set_window_maximized(true)); + let image_bytes = include_bytes!("../assets/background_tile.png"); + let mut background_tile_image = Image::load_image_from_mem(".png", image_bytes).unwrap(); + background_tile_image.resize(256, 256); + let background_tile_texture = rl_handle + .load_texture_from_image(&rl_thread, &background_tile_image) + .unwrap(); + const TITLE_FONT_SIZE: i32 = 60; let mut scene_to_load = None; while !rl_handle.window_should_close() { @@ -29,14 +40,14 @@ fn main() { let mut rl_draw_handle = rl_handle.begin_drawing(&rl_thread); let screen_width = rl_draw_handle.get_screen_width(); let screen_height = rl_draw_handle.get_screen_height(); - draw_background(&mut rl_draw_handle); + draw_background(&mut rl_draw_handle, &background_tile_texture); rl_draw_handle.draw_text( "Spline Drawer", (screen_width - rl_draw_handle.measure_text("Spline Drawer", TITLE_FONT_SIZE)) / 2, (screen_height - TITLE_FONT_SIZE) / 2, TITLE_FONT_SIZE, - Color::WHITE, + COLOR_LIGHT, ); if rl_draw_handle.gui_button( @@ -65,20 +76,47 @@ fn main() { } match scene_to_load { - Some(Scenes::BezierCurve) => bezier_curve_scene(&mut rl_handle, &rl_thread), - Some(Scenes::BezierSpline) => bezier_spline_scene(&mut rl_handle, &rl_thread), + Some(Scenes::BezierCurve) => { + bezier_curve_scene(&mut rl_handle, &rl_thread, &background_tile_texture) + } + Some(Scenes::BezierSpline) => { + bezier_spline_scene(&mut rl_handle, &rl_thread, &background_tile_texture) + } None => {} } scene_to_load = None; } } -fn draw_background(rl_draw_handle: &mut RaylibDrawHandle) { - rl_draw_handle.clear_background(Color::BLACK); +fn draw_background(rl_draw_handle: &mut RaylibDrawHandle, tile_texture: &Texture2D) { + rl_draw_handle.clear_background(COLOR_DARK); + let screen_width = rl_draw_handle.get_screen_width(); + let screen_height = rl_draw_handle.get_screen_height(); + for i in 0..=screen_width / tile_texture.width() { + for j in 0..=screen_height / tile_texture.height() { + rl_draw_handle.draw_texture( + tile_texture, + i * tile_texture.width(), + j * tile_texture.height(), + COLOR_LIGHT, + ); + } + } + rl_draw_handle.draw_text( + "CREDITS: Captainfl4me", + screen_width - rl_draw_handle.measure_text("CREDITS: Captainfl4me", 32) - 20, + screen_height - 40, + 32, + COLOR_BLACK, + ); } const T_ANIMATION_SPEED: f32 = 0.005; -fn bezier_curve_scene(rl_handle: &mut RaylibHandle, rl_thread: &RaylibThread) { +fn bezier_curve_scene( + rl_handle: &mut RaylibHandle, + rl_thread: &RaylibThread, + tile_texture: &Texture2D, +) { // Initialize let mut points = [ Vector2::new(300.0, 600.0), @@ -90,9 +128,9 @@ fn bezier_curve_scene(rl_handle: &mut RaylibHandle, rl_thread: &RaylibThread) { .enumerate() .map(|(i, pos)| { let color = if i == 0 || i == 4 - 1 { - Color::BLUE + COLOR_BLUE } else { - Color::WHITE + COLOR_LIGHT }; BasicPoint::new(*pos, color) }) @@ -148,15 +186,15 @@ fn bezier_curve_scene(rl_handle: &mut RaylibHandle, rl_thread: &RaylibThread) { KeyboardKey::KEY_SPACE => { if points.len() < 62 { // Prevent binomial overflow - points.last_mut().unwrap().color = Color::WHITE; - let new_point = BasicPoint::new(mouse_position, Color::BLUE); + points.last_mut().unwrap().color = COLOR_LIGHT; + let new_point = BasicPoint::new(mouse_position, COLOR_BLUE); points.push(new_point); } } KeyboardKey::KEY_BACKSPACE => { if points.len() > 2 { points.pop(); - points.last_mut().unwrap().color = Color::BLUE; + points.last_mut().unwrap().color = COLOR_BLUE; } } KeyboardKey::KEY_ESCAPE => { @@ -170,7 +208,7 @@ fn bezier_curve_scene(rl_handle: &mut RaylibHandle, rl_thread: &RaylibThread) { // Update Frame let mut rl_draw_handle = rl_handle.begin_drawing(rl_thread); let screen_width = rl_draw_handle.get_screen_width(); - draw_background(&mut rl_draw_handle); + draw_background(&mut rl_draw_handle, tile_texture); let current_fps_text = format!("{} FPS", rl_draw_handle.get_fps()); rl_draw_handle.draw_text( @@ -178,7 +216,7 @@ fn bezier_curve_scene(rl_handle: &mut RaylibHandle, rl_thread: &RaylibThread) { screen_width - rl_draw_handle.measure_text(current_fps_text.as_str(), 14) - 30, 20, 14, - Color::GREEN, + COLOR_GREEN, ); // Draw GUI Controls @@ -217,7 +255,7 @@ fn bezier_curve_scene(rl_handle: &mut RaylibHandle, rl_thread: &RaylibThread) { screen_width - rl_draw_handle.measure_text(current_draw_time_text.as_str(), 14) - 30, 50, 14, - Color::WHITE, + COLOR_LIGHT, ); // Update Animation @@ -243,7 +281,11 @@ fn bezier_curve_scene(rl_handle: &mut RaylibHandle, rl_thread: &RaylibThread) { } } -fn bezier_spline_scene(rl_handle: &mut RaylibHandle, rl_thread: &RaylibThread) { +fn bezier_spline_scene( + rl_handle: &mut RaylibHandle, + rl_thread: &RaylibThread, + tile_texture: &Texture2D, +) { // Initialize let mut points: Vec>>> = vec![Rc::new(RefCell::new( Box::new(JoinPoint::new(Vector2::new(300.0, 600.0), None, None)), @@ -423,7 +465,7 @@ fn bezier_spline_scene(rl_handle: &mut RaylibHandle, rl_thread: &RaylibThread) { // Update Frame let mut rl_draw_handle = rl_handle.begin_drawing(rl_thread); let screen_width = rl_draw_handle.get_screen_width(); - draw_background(&mut rl_draw_handle); + draw_background(&mut rl_draw_handle, tile_texture); let current_fps_text = format!("{} FPS", rl_draw_handle.get_fps()); rl_draw_handle.draw_text( @@ -431,7 +473,7 @@ fn bezier_spline_scene(rl_handle: &mut RaylibHandle, rl_thread: &RaylibThread) { screen_width - rl_draw_handle.measure_text(current_fps_text.as_str(), 14) - 30, 20, 14, - Color::GREEN, + COLOR_GREEN, ); // Draw GUI Controls @@ -498,7 +540,7 @@ fn bezier_spline_scene(rl_handle: &mut RaylibHandle, rl_thread: &RaylibThread) { screen_width - rl_draw_handle.measure_text(current_draw_time_text.as_str(), 14) - 30, 50, 14, - Color::WHITE, + COLOR_LIGHT, ); // Update Animation