initial commit? <_< forgor'd

This commit is contained in:
dani 2023-07-01 10:44:21 +00:00
commit 45962a19cd
16 changed files with 2299 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,10 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
<option name="processCode" value="true" />
<option name="processLiterals" value="true" />
<option name="processComments" value="true" />
</inspection_tool>
</profile>
</component>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/skunkrts.iml" filepath="$PROJECT_DIR$/.idea/skunkrts.iml" />
</modules>
</component>
</project>

12
.idea/skunkrts.iml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

1900
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

13
Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "skunk2d"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
pixels = "0.13.0"
winit = "0.28.6"
gif = "0.12.0"
rand = "0.8.5"
num = "0.4.0"

BIN
examples/assets/hex.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 925 B

BIN
examples/assets/test.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

45
examples/test.rs Normal file
View File

@ -0,0 +1,45 @@
use skunk2d::*;
const WIDTH: i32 = 1280;
const HEIGHT: i32 = 720;
struct World {
img: Image,
pos: Vec2<i32>
}
fn main() {
run::<World>(WIDTH, HEIGHT);
}
impl Game for World {
fn new(window_state: &mut WindowState) -> Self {
//window_state.set_palette(0, 0xc0, 0xf0, 0xd0);
window_state.scramble_palette();
Self {
img: Image::load("examples/assets/test.gif"),
pos: Vec2::zero()
}
}
fn update(&mut self, window_state: &mut WindowState) {
self.pos = window_state.mouse_pos();
}
fn on_event(&mut self, window_state: &mut WindowState, event: Event) {
match event {
Event::MouseClick(MouseButton::Left, _) => {
window_state.scramble_palette();
}
_ => {}
}
}
fn draw(&self, target: &mut Image) {
target.clear();
target.draw_image(Vec2{x: 200, y: 100}, &self.img);
target.draw_image(self.pos, &self.img);
target.draw_line(Vec2{x: WIDTH/2, y: HEIGHT/2}, self.pos, 10);
}
}

96
src/image.rs Normal file
View File

@ -0,0 +1,96 @@
use std::fs::File;
use std::path::Path;
use crate::vec2::Vec2;
//todo: make dynamically different bitdepths
pub struct Image {
data: Vec<u8>,
size: Vec2<i32>
}
impl Image {
pub fn new(size: Vec2<i32>) -> Self {
Image {
data: vec![0u8; (size.x * size.y) as usize],
size
}
}
pub fn load(path: &str) -> Self {
let input = File::open(path).unwrap();
let mut options = gif::DecodeOptions::new();
options.set_color_output(gif::ColorOutput::Indexed);
let mut decoder = options.read_info(input).unwrap();
let x = decoder.width();
let y = decoder.height();
let mut data = vec![0u8; (x * y) as usize];
decoder.next_frame_info().unwrap();
decoder.read_into_buffer(&mut data).unwrap();
Image {
data,
size: Vec2 {x: x as i32, y: y as i32}
}
}
pub fn clear(&mut self) {
self.fill(0);
}
pub fn data_mut(&mut self) -> &mut [u8] {
self.data.as_mut_slice()
}
pub fn data(&mut self) -> &[u8] {
self.data.as_slice()
}
pub fn draw_image(&mut self, pos: Vec2<i32>, image: &Image) {
//todo: write proper implementation later
for y in 0..image.size.y as i32 {
for x in 0..image.size.x as i32 {
//todo: implement better(very stupid to do per pixel)
if x + pos.x >= 0 && y + pos.y >= 0 && x + pos.x < self.size.x as i32 && y + pos.y < self.size.y as i32 {
let p = image.get_pixel(Vec2 { x, y: y });
if p > 0 {
self.set_pixel(Vec2 { x: x + pos.x, y: y + pos.y }, p);
}
}
}
}
}
pub fn draw_line(&mut self, pos1: Vec2<i32>, pos2: Vec2<i32>, color: u8) {
let (x1, y1, x2, y2) = (pos1.x, pos1.y, pos2.x, pos2.y);
let mut x = x1 as f32;
let mut y = y1 as f32;
let xdiff = (x2 - x1) as f32;
let ydiff = (y2 - y1) as f32;
let step = if xdiff.abs() > ydiff.abs() {
xdiff.abs()
} else {
ydiff.abs()
};
let xs = xdiff / step;
let ys = ydiff / step;
for i in 1..=(step as i32) {
self.set_pixel(Vec2 { x: x as i32, y: y as i32 }, color);
x += xs;
y += ys;
}
}
pub fn fill(&mut self, color: u8) {
self.data.fill(color);
}
pub fn get_pixel(&self, pos: Vec2<i32>) -> u8 {
self.data[(pos.x + self.size.x * pos.y) as usize]
}
pub fn set_pixel(&mut self, pos: Vec2<i32>, color: u8) {
self.data[(pos.x + self.size.x * pos.y) as usize] = color;
}
}

9
src/lib.rs Normal file
View File

@ -0,0 +1,9 @@
mod window;
mod image;
mod vec2;
mod rect;
pub use window::*;
pub use image::*;
pub use vec2::*;
pub use rect::*;

17
src/rect.rs Normal file
View File

@ -0,0 +1,17 @@
use num::Num;
use crate::vec2::Vec2;
#[derive(Copy, Clone)]
pub struct Rect<T: Num + Copy> {
pub pos: Vec2<T>,
pub size: Vec2<T>,
}
impl<T: Num + Copy> Rect<T> {
pub fn new(x: T, y: T, w: T, h: T) -> Rect<T> {
return Rect {
pos: Vec2{x, y},
size: Vec2{x: w, y: h},
};
}
}

22
src/vec2.rs Normal file
View File

@ -0,0 +1,22 @@
use num::Num;
#[derive(Copy, Clone)]
pub struct Vec2<T : Num + Copy> {
pub x: T,
pub y: T,
}
impl<T : Num + Copy> Vec2<T> {
pub fn zero() -> Self {
Vec2 {x: T::zero(), y: T::zero()}
}
}
impl<T : Num + Copy + Default> Default for Vec2<T> {
fn default() -> Self {
Vec2 {
x: T::default(),
y: T::default()
}
}
}

152
src/window.rs Normal file
View File

@ -0,0 +1,152 @@
use pixels::{Pixels, SurfaceTexture};
use winit::dpi::LogicalSize;
use winit::event::{ElementState, Event as WinitEvent, VirtualKeyCode, WindowEvent, MouseButton as WinitMouseButton};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::{WindowBuilder};
use crate::image::Image;
use crate::vec2::Vec2;
use rand::Rng;
use winit::event::VirtualKeyCode::Escape;
pub enum MouseButton {
Left, Right, Middle, WheelUp, WheelDown, Forward, Back
}
pub enum Event {
MouseClick(MouseButton, Vec2<i32>)
}
pub trait Game {
fn new(window_state: &mut WindowState) -> Self;
fn update(&mut self, window_state: &mut WindowState);
fn on_event(&mut self, window_state: &mut WindowState, event: Event);
fn draw(&self, target: &mut Image);
}
pub struct WindowState {
palette: [[u8; 4]; 256],
mouse_pos: Vec2<i32>
}
impl WindowState {
fn new() -> Self {
WindowState {
palette: [[0u8; 4]; 256],
mouse_pos: Vec2::zero(),
}
}
pub fn set_palette(&mut self, index: u8, r: u8, g: u8, b: u8) {
self.palette[index as usize] = [r, g, b, 0xffu8];
}
pub fn scramble_palette(&mut self) {
let mut rng = rand::thread_rng();
for i in 0..=255 {
self.set_palette(i, rng.gen(), rng.gen(), rng.gen());
}
}
pub fn mouse_pos(&self) -> Vec2<i32> {
self.mouse_pos
}
}
pub fn run<T : Game + 'static>(width: i32, height: i32) {
let event_loop = EventLoop::new();
let window = {
let size = LogicalSize::new(width as f64, height as f64);
WindowBuilder::new()
.with_title("Skunk 2D")
.with_inner_size(size)
.with_min_inner_size(size)
//.with_fullscreen(Option::Some(Fullscreen::Borderless(None)))
.build(&event_loop)
.unwrap()
};
let mut pixels = {
let window_size = window.inner_size();
let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window);
Pixels::new(width as u32, height as u32, surface_texture).unwrap()
};
let mut screen = Image::new(Vec2{x: width, y: height});
let mut window_state = WindowState::new();
let mut game = T::new(&mut window_state);
event_loop.run(move |event, _, control_flow| {
match event {
WinitEvent::NewEvents(_) => {}
WinitEvent::WindowEvent { window_id, event: window_event } => {
match window_event {
WindowEvent::Resized(_) => {}
WindowEvent::Moved(_) => {}
WindowEvent::CloseRequested => {}
WindowEvent::Destroyed => {}
WindowEvent::DroppedFile(_) => {}
WindowEvent::HoveredFile(_) => {}
WindowEvent::HoveredFileCancelled => {}
WindowEvent::ReceivedCharacter(_) => {}
WindowEvent::Focused(_) => {}
WindowEvent::KeyboardInput { input, .. } => {
//todo: actually do input handling
if input.virtual_keycode.unwrap() == Escape {
*control_flow = ControlFlow::Exit;
return;
}
}
WindowEvent::ModifiersChanged(_) => {}
WindowEvent::Ime(_) => {}
WindowEvent::CursorMoved { position, .. } => {
window_state.mouse_pos = Vec2{x: position.x as i32, y: position.y as i32}
}
WindowEvent::CursorEntered { .. } => {}
WindowEvent::CursorLeft { .. } => {}
WindowEvent::MouseWheel { .. } => {}
WindowEvent::MouseInput { button, state, .. } => {
let b = match button {
WinitMouseButton::Left => MouseButton::Left,
WinitMouseButton::Right => MouseButton::Right,
WinitMouseButton::Middle => MouseButton::Middle,
//todo: handle other mousebuttons
WinitMouseButton::Other(b) => match b { _ => MouseButton::Back }
};
match state {
ElementState::Pressed => {
let e = Event::MouseClick(b, window_state.mouse_pos);
game.on_event(&mut window_state, e);
}
ElementState::Released => {}
}
}
WindowEvent::TouchpadMagnify { .. } => {}
WindowEvent::SmartMagnify { .. } => {}
WindowEvent::TouchpadRotate { .. } => {}
WindowEvent::TouchpadPressure { .. } => {}
WindowEvent::AxisMotion { .. } => {}
WindowEvent::Touch(_) => {}
WindowEvent::ScaleFactorChanged { .. } => {}
WindowEvent::ThemeChanged(_) => {}
WindowEvent::Occluded(_) => {}
}
}
WinitEvent::DeviceEvent { .. } => {}
WinitEvent::UserEvent(_) => {}
WinitEvent::Suspended => {}
WinitEvent::Resumed => {}
WinitEvent::MainEventsCleared => {}
WinitEvent::RedrawRequested(_) => {
game.draw(&mut screen);
for (i, pixel) in pixels.frame_mut().chunks_exact_mut(4).enumerate() {
pixel.copy_from_slice(&window_state.palette[screen.data()[i] as usize]);
}
pixels.render().unwrap();
}
WinitEvent::RedrawEventsCleared => {}
WinitEvent::LoopDestroyed => {}
}
game.update(&mut window_state);
window.request_redraw();
});
}