feat: Setup SDL - 0002

This commit is contained in:
Gaëtan Brochard 2021-12-02 02:10:16 +01:00
parent 4b433b6d8f
commit 575ac0525c
3 changed files with 92 additions and 0 deletions

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "metagraphs_poc"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
sdl2 = "0.35.1"

49
src/main.rs Normal file
View File

@ -0,0 +1,49 @@
extern crate sdl2;
mod render;
use render::render;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::pixels::PixelFormatEnum;
use sdl2::surface::Surface;
fn main() {
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem
.window("metagraphs proof of concept", 800, 800)
.position_centered()
.opengl()
.build()
.unwrap();
let mut canvas = window.into_canvas().build().unwrap();
let texture_creator = canvas.texture_creator();
let mut event_pump = sdl_context.event_pump().unwrap();
let mut data = Box::new([255u8; 1_920_000]);
let mut frame_count: u32 = 0;
'running: loop {
for event in event_pump.poll_iter() {
match event {
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
..
} => break 'running,
_ => {}
}
}
render(&mut *data, frame_count);
let surface =
Surface::from_data(&mut *data, 800, 800, 2400, PixelFormatEnum::RGB24).unwrap();
let texture = texture_creator
.create_texture_from_surface(surface)
.unwrap();
canvas.copy(&texture, None, None).unwrap();
canvas.present();
frame_count += 1;
}
}

34
src/render.rs Normal file
View File

@ -0,0 +1,34 @@
use std::f64;
use sdl2::pixels::Color;
fn put_pixel(data: &mut [u8], x: usize, y: usize, color: Color) {
data[y * 3 * 800 + x * 3] = color.r;
data[y * 3 * 800 + x * 3 + 1] = color.g;
data[y * 3 * 800 + x * 3 + 2] = color.b;
}
pub fn render(data: &mut [u8], frame_count: u32) {
let time = frame_count as f64 / 60. * 6.28;
let ct = f64::cos(time);
let st = f64::sin(time);
for x in 0..800 {
for y in 0..800 {
let cx: f64 = f64::cos(st * 1.0 + (x as f64 - 400.) / 39.123 * ct) + 1.;
let cy: f64 = f64::cos(ct * 1.1 + (y as f64 - 400.) / 40.123 * st) + 1.;
let sx: f64 = f64::sin(ct * 1.2 + (x as f64 - 400.) / 41.123 * ct) + 1.;
let sy: f64 = f64::sin(st * 1.3 + (y as f64 - 400.) / 42.123 * st) + 1.;
put_pixel(
&mut *data,
x,
y,
Color {
r: (cx * 128.) as u8,
g: (cy * sx * 64.) as u8,
b: (sy * 128.) as u8,
a: 0,
},
);
}
}
}