Skip to content

Commit

Permalink
feat: new colors + background
Browse files Browse the repository at this point in the history
  • Loading branch information
Captainfl4me committed May 6, 2024
1 parent 8b732e4 commit 3c6adc8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 26 deletions.
Binary file added assets/background_tile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 9 additions & 7 deletions src/bezier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
);
}

Expand All @@ -392,11 +394,11 @@ pub fn draw_bezier(points: &[impl PointGui], d: &mut RaylibDrawHandle, t: Option
.collect::<Vec<_>>();
// 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;
}
Expand All @@ -409,11 +411,11 @@ pub fn draw_bezier(points: &[impl PointGui], d: &mut RaylibDrawHandle, t: Option
.collect::<Vec<_>>();

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() {
Expand Down
9 changes: 9 additions & 0 deletions src/colors.rs
Original file line number Diff line number Diff line change
@@ -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);
80 changes: 61 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ::core::cell::RefCell;
use raylib::core::texture::Image;
use raylib::prelude::*;
use std::ffi::CStr;
use std::rc::Rc;
Expand All @@ -7,6 +8,9 @@ use std::time::SystemTime;
mod bezier;
use bezier::*;

mod colors;
use colors::*;

enum Scenes {
BezierCurve,
BezierSpline,
Expand All @@ -22,21 +26,28 @@ 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() {
{
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(
Expand Down Expand Up @@ -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),
Expand All @@ -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)
})
Expand Down Expand Up @@ -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 => {
Expand All @@ -170,15 +208,15 @@ 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(
current_fps_text.as_str(),
screen_width - rl_draw_handle.measure_text(current_fps_text.as_str(), 14) - 30,
20,
14,
Color::GREEN,
COLOR_GREEN,
);

// Draw GUI Controls
Expand Down Expand Up @@ -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
Expand All @@ -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<Rc<RefCell<Box<dyn MovableGuiPoint>>>> = vec![Rc::new(RefCell::new(
Box::new(JoinPoint::new(Vector2::new(300.0, 600.0), None, None)),
Expand Down Expand Up @@ -423,15 +465,15 @@ 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(
current_fps_text.as_str(),
screen_width - rl_draw_handle.measure_text(current_fps_text.as_str(), 14) - 30,
20,
14,
Color::GREEN,
COLOR_GREEN,
);

// Draw GUI Controls
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3c6adc8

Please sign in to comment.