Draft 01/27/2024

This is a draft of the codebase on the date reflected above.
This commit is contained in:
2024-01-27 15:58:41 -05:00
parent b582bdd1d8
commit 44db7751e9
7 changed files with 172 additions and 0 deletions

11
vash_caster/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "vash_caster"
version = "0.1.0"
edition = "2021"
default-run = "vash_caster"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
sdl2 = { version = "0.36", features = ["gfx"] }
rand = "0.8"

99
vash_caster/src/caster.rs Normal file
View File

@@ -0,0 +1,99 @@
use std::error::Error;
use sdl2::gfx::primitives::DrawRenderer;
use sdl2::pixels::Color;
use sdl2::{render::Canvas, video::Window, VideoSubsystem};
use rand::prelude::*;
pub struct Caster {
projection_plane: Canvas<Window>,
title: &'static str,
width: u32,
height: u32,
}
impl Caster {
pub fn init(
video_subsystem: &VideoSubsystem,
win_title: &'static str,
win_width: u32,
win_height: u32,
) -> Result<Self, Box<dyn Error>> {
let window = video_subsystem
.window(win_title, win_width, win_height)
.position_centered()
.opengl()
.build()
.map_err(|e| e.to_string())?;
let canvas = window.into_canvas().build().map_err(|e| e.to_string())?;
Ok(Self {
projection_plane: canvas,
title: win_title,
width: win_width,
height: win_height,
})
}
pub fn update(&mut self) -> Result<(), Box<dyn Error>> {
let mut rng = rand::thread_rng();
let mut d_x = rng.gen_range(1..=5);
let mut d_y = rng.gen_range(1..=5);
let mut cur_x = rng.gen_range(0..=800);
let mut cur_y = rng.gen_range(0..=600);
let mut ball_color = Color::RGB(
rng.gen_range(0..=255),
rng.gen_range(0..=255),
rng.gen_range(0..=255),
);
self.projection_plane.set_draw_color(Color::RGB(0, 0, 0));
self.projection_plane.clear();
self.projection_plane
.filled_circle(cur_x, cur_y, 10, ball_color)?;
self.projection_plane.present();
cur_x += d_x;
if cur_x < 0 {
cur_x = 0;
ball_color = Color::RGB(
rng.gen_range(0..=255),
rng.gen_range(0..=255),
rng.gen_range(0..=255),
);
d_x = rng.gen_range(1..=5);
} else if cur_x > 800 {
cur_x = 800;
ball_color = Color::RGB(
rng.gen_range(0..=255),
rng.gen_range(0..=255),
rng.gen_range(0..=255),
);
d_x = rng.gen_range(-5..=-1);
}
cur_y += d_y;
if cur_y < 0 {
cur_y = 0;
ball_color = Color::RGB(
rng.gen_range(0..=255),
rng.gen_range(0..=255),
rng.gen_range(0..=255),
);
d_y = rng.gen_range(1..=5);
} else if cur_y > 600 {
cur_y = 600;
ball_color = Color::RGB(
rng.gen_range(0..=255),
rng.gen_range(0..=255),
rng.gen_range(0..=255),
);
d_y = rng.gen_range(-5..=-1);
}
Ok(())
}
}

39
vash_caster/src/lib.rs Normal file
View File

@@ -0,0 +1,39 @@
extern crate rand;
extern crate sdl2;
mod caster;
use caster::Caster;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use std::error::Error;
use std::time::Duration;
pub fn run() -> Result<(), Box<dyn Error>> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;
let mut caster = Caster::init(&video_subsystem, "Vash Caster", 800, 600)?;
let mut event_pump = sdl_context.event_pump()?;
'running: loop {
for event in event_pump.poll_iter() {
match event {
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
..
} => break 'running,
_ => {}
}
}
caster.update()?;
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 144));
}
Ok(())
}

9
vash_caster/src/main.rs Normal file
View File

@@ -0,0 +1,9 @@
use vash_caster::run;
use std::process;
fn main() {
if let Err(e) = run() {
println!("An error has occured: {e}");
process::exit(1);
}
}