aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authortcmal <me@aria.rip>2024-08-25 17:44:19 +0100
committertcmal <me@aria.rip>2024-08-25 17:44:19 +0100
commita963fc3b65eff0f20afff71d0388f1d472467733 (patch)
treef3521822186a17711f52d62080d0a5992e2b7046 /examples
parentcd94fb83fb51d37a84cbb80c8135430e26c74566 (diff)
feat(render): Resize vertex buffers and rainbow triangles example
Diffstat (limited to 'examples')
-rw-r--r--examples/render-triangles/Cargo.toml (renamed from examples/render/Cargo.toml)3
-rw-r--r--examples/render-triangles/README.md3
-rw-r--r--examples/render-triangles/src/main.rs85
-rw-r--r--examples/render/13power.bspbin4568172 -> 0 bytes
-rw-r--r--examples/render/src/main.rs54
5 files changed, 90 insertions, 55 deletions
diff --git a/examples/render/Cargo.toml b/examples/render-triangles/Cargo.toml
index 3144943..dd2c189 100644
--- a/examples/render/Cargo.toml
+++ b/examples/render-triangles/Cargo.toml
@@ -1,5 +1,5 @@
[package]
-name = "render"
+name = "render-triangles"
version = "0.1.0"
authors = ["Oscar <oscar.shrimpton.personal@gmail.com>"]
edition = "2018"
@@ -11,6 +11,7 @@ stockton-bsp = "2.0.0"
winit = "0.19.1"
log = "0.4.0"
simple_logger = "1.0"
+rand = "0.7"
[target.'cfg(feature = "gl")'.dependencies.stockton-render]
path = "../../stockton-render"
diff --git a/examples/render-triangles/README.md b/examples/render-triangles/README.md
new file mode 100644
index 0000000..a048662
--- /dev/null
+++ b/examples/render-triangles/README.md
@@ -0,0 +1,3 @@
+# render-triangles
+
+Renders rainbow triangles. Space adds a new random triangle, Escape exits. \ No newline at end of file
diff --git a/examples/render-triangles/src/main.rs b/examples/render-triangles/src/main.rs
new file mode 100644
index 0000000..08eff03
--- /dev/null
+++ b/examples/render-triangles/src/main.rs
@@ -0,0 +1,85 @@
+// Copyright (C) Oscar Shrimpton 2019
+
+// This program is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation, either version 3 of the License, or (at your option)
+// any later version.
+
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>.
+
+//! Renders ./example.bsp
+
+extern crate stockton_types;
+extern crate stockton_bsp;
+extern crate stockton_render;
+extern crate winit;
+extern crate simple_logger;
+extern crate rand;
+
+use stockton_render::draw::{RenderingContext, Tri2};
+use stockton_types::Vector2;
+
+use winit::{Event, WindowEvent, VirtualKeyCode, ElementState};
+use rand::prelude::*;
+
+fn main() {
+
+ simple_logger::init().unwrap();
+
+ // Create the renderer.
+ let mut ctx = RenderingContext::new().unwrap();
+ let mut rng = thread_rng();
+ let mut vertices: Vec<Tri2> = Vec::new();
+ let mut running = true;
+ let mut vertices_dirty = false;
+
+ while running {
+ ctx.events_loop.poll_events(|event| {
+ match event {
+ // TODO: Handle resize
+ Event::WindowEvent {
+ event: WindowEvent::KeyboardInput { input, .. },
+ ..
+ } => match input.state {
+ ElementState::Released => match input.virtual_keycode {
+ Some(VirtualKeyCode::Escape) => running = false,
+ Some(VirtualKeyCode::Space) => {
+ vertices.push(Tri2 ([
+ Vector2::new(
+ rng.gen_range(-1.0, 1.0),
+ rng.gen_range(-1.0, 1.0),
+ ),
+ Vector2::new(
+ rng.gen_range(-1.0, 1.0),
+ rng.gen_range(-1.0, 1.0),
+ ),
+ Vector2::new(
+ rng.gen_range(-1.0, 1.0),
+ rng.gen_range(-1.0, 1.0),
+ )
+ ]));
+
+ vertices_dirty = true;
+ },
+ _ => ()
+ },
+ _ => ()
+ }
+ _ => ()
+ }
+ });
+
+ if vertices_dirty {
+ ctx.populate_vertices(vertices.as_slice()).unwrap();
+ vertices_dirty = false;
+ }
+
+ ctx.draw_vertices().unwrap();
+ }
+}
diff --git a/examples/render/13power.bsp b/examples/render/13power.bsp
deleted file mode 100644
index 0885199..0000000
--- a/examples/render/13power.bsp
+++ /dev/null
Binary files differ
diff --git a/examples/render/src/main.rs b/examples/render/src/main.rs
deleted file mode 100644
index 308a018..0000000
--- a/examples/render/src/main.rs
+++ /dev/null
@@ -1,54 +0,0 @@
-// Copyright (C) Oscar Shrimpton 2019
-
-// This program is free software: you can redistribute it and/or modify it
-// under the terms of the GNU General Public License as published by the Free
-// Software Foundation, either version 3 of the License, or (at your option)
-// any later version.
-
-// This program is distributed in the hope that it will be useful, but WITHOUT
-// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
-// more details.
-
-// You should have received a copy of the GNU General Public License along
-// with this program. If not, see <http://www.gnu.org/licenses/>.
-
-//! Renders ./example.bsp
-
-extern crate stockton_types;
-extern crate stockton_bsp;
-extern crate stockton_render;
-extern crate winit;
-extern crate simple_logger;
-
-use stockton_bsp::BSPFile;
-use stockton_render::Renderer;
-use stockton_types::World;
-
-use std::sync::{Arc,RwLock};
-
-fn main() {
-
- simple_logger::init().unwrap();
-
- // Parse the BSP file.
- let data = include_bytes!("../13power.bsp");
- let bsp = BSPFile::from_buffer(data).unwrap();
-
- // Load it into a world.
- // None of the entities are mapped for simplicity.
- let world = Arc::new(RwLock::new(World::new(bsp, |_| {
- None
- }).unwrap()));
-
- // Create the renderer.
- let mut renderer = Renderer::new(world).unwrap();
-
- loop {
- // TODO: Poll Window events
- // TODO: Handle resize
- // TODO: Simulate world
-
- renderer.render_frame().unwrap();
- }
-}