Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ target
.vscode
dist
node_modules
examples/resources
tools/wasm-template
10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ members = [ "modules/world", "modules/audio" ]
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
gl = "0.10.0"
glutin = "0.18.0"
websocket = "0.22.3"
tokio = "0.1"
futures ={ version = "0.1.26", features=["use_std"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2.29"
wasm-bindgen = "0.2.38"
js-sys = "0.3.6"
console_error_panic_hook = "0.1.5"

Expand All @@ -35,9 +38,9 @@ features = [
'Node',
'Document',
'Element',
'ErrorEvent',
'DomRect',
'HtmlCanvasElement',
"HtmlElement",
'WebGlBuffer',
'WebGl2RenderingContext',
'WebGlProgram',
Expand All @@ -49,13 +52,16 @@ features = [
'WebGlFramebuffer',
'WebGlVertexArrayObject',
'Window',
'WebSocket',
'Performance',
'PerformanceTiming',
'XmlHttpRequest',
'XmlHttpRequestResponseType',
'Response',
'EventTarget',
'Event',
'MouseEvent',
'MessageEvent',
'KeyboardEvent',
'UiEvent'
]
Expand Down
183 changes: 183 additions & 0 deletions examples/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
extern crate crayon;

use crayon::impl_vertex;
use crayon::prelude::*;
use crayon::network;
impl_vertex! {
Vertex {
position => [Position; Float; 2; false],
}
}

struct Pass {
surface: SurfaceHandle,
shader: ShaderHandle,
mesh: MeshHandle,
}

struct Window {
pass: Pass,
post_effect: Pass,
texture: RenderTextureHandle,
batch: CommandBuffer,
time: f32,
}

impl Window {
pub fn build() -> CrResult<Self> {
let attributes = AttributeLayoutBuilder::new()
.with(Attribute::Position, 2)
.finish();

//
let (pass, rendered_texture) = {
let verts: [Vertex; 3] = [
Vertex::new([0.0, 0.5]),
Vertex::new([0.5, -0.5]),
Vertex::new([-0.5, -0.5]),
];
let idxes: [u16; 3] = [0, 1, 2];

// Create vertex buffer object.
let mut params = MeshParams::default();
params.num_verts = 3;
params.num_idxes = 3;
params.layout = Vertex::layout();

let data = MeshData {
vptr: Vertex::encode(&verts[..]).into(),
iptr: IndexFormat::encode(&idxes).into(),
};

let mesh = video::create_mesh(params, Some(data))?;

// Create render texture for post effect.
let mut params = RenderTextureParams::default();
params.format = RenderTextureFormat::RGBA8;
params.dimensions = (568, 320).into();
let rendered_texture = video::create_render_texture(params)?;

// Create the surface state for pass 1.
let mut params = SurfaceParams::default();
params.set_attachments(&[rendered_texture], None)?;
params.set_clear(Color::gray(), None, None);
let surface = video::create_surface(params)?;

// Create shader state.
let mut params = ShaderParams::default();
params.attributes = attributes;
let vs = include_str!("shaders/render_target_p1.vs").to_owned();
let fs = include_str!("shaders/render_target_p1.fs").to_owned();
let shader = video::create_shader(params, vs, fs)?;

(
Pass {
surface,
shader,
mesh,
},
rendered_texture,
)
};

let post_effect = {
let verts: [Vertex; 4] = [
Vertex::new([-1.0, -1.0]),
Vertex::new([1.0, -1.0]),
Vertex::new([1.0, 1.0]),
Vertex::new([-1.0, 1.0]),
];
let idxes: [u16; 6] = [0, 1, 2, 0, 2, 3];

let mut params = MeshParams::default();
params.num_verts = 4;
params.num_idxes = 6;
params.layout = Vertex::layout();

let data = MeshData {
vptr: Vertex::encode(&verts[..]).into(),
iptr: IndexFormat::encode(&idxes).into(),
};

let mesh = video::create_mesh(params, Some(data))?;

let params = SurfaceParams::default();
let surface = video::create_surface(params)?;

let uniforms = UniformVariableLayout::build()
.with("renderedTexture", UniformVariableType::RenderTexture)
.with("time", UniformVariableType::F32)
.finish();

let mut params = ShaderParams::default();
params.attributes = attributes;
params.uniforms = uniforms;
let vs = include_str!("shaders/render_target_p2.vs").to_owned();
let fs = include_str!("shaders/render_target_p2.fs").to_owned();
let shader = video::create_shader(params, vs, fs)?;

Pass {
surface,
shader,
mesh,
}
};

Ok(Window {
pass,
post_effect,
texture: rendered_texture,

batch: CommandBuffer::new(),
time: 0.0,
})
}
}

impl Drop for Window {
fn drop(&mut self) {
video::delete_render_texture(self.texture);

video::delete_mesh(self.pass.mesh);
video::delete_shader(self.pass.shader);
video::delete_surface(self.pass.surface);

video::delete_mesh(self.post_effect.mesh);
video::delete_shader(self.post_effect.shader);
video::delete_surface(self.post_effect.surface);
}
}

impl LifecycleListener for Window {
fn on_update(&mut self) -> CrResult<()> {
let surface = self.pass.surface;
let dc = Draw::new(self.pass.shader, self.pass.mesh);
self.batch.draw(dc);
self.batch.submit(surface)?;

let surface = self.post_effect.surface;
let mut dc = Draw::new(self.post_effect.shader, self.post_effect.mesh);
dc.set_uniform_variable("renderedTexture", self.texture);
dc.set_uniform_variable("time", self.time);
self.batch.draw(dc);
self.batch.submit(surface)?;
let k = "ws://0.0.0.0:8080".to_owned();
network::create_connection(k.clone()).unwrap();
if self.time == 4.0{
// network::send(k);
}

for e in network::receive(){
print!("e {:?}",e);
}
self.time += 0.05;
Ok(())
}
}

main!({
let mut params = Params::default();
params.window.title = "CR: RenderTexture".into();
params.window.size = (568, 320).into();
crayon::application::setup(params, Window::build).unwrap();
});
4 changes: 2 additions & 2 deletions examples/resources/427EB273E77446B8B7010ACE1B7652EE
Git LFS file not shown
4 changes: 2 additions & 2 deletions examples/resources/4572A4B5C9F146BD84517F6FA535FAE6
Git LFS file not shown
4 changes: 2 additions & 2 deletions examples/resources/E32F80DF6B5946D9B49E4651AAF8A64E
Git LFS file not shown
9 changes: 9 additions & 0 deletions src/input/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,21 @@ impl Keyboard {
pub fn is_key_press(&self, key: Key) -> bool {
self.presses.contains(&key)
}
#[inline]
pub fn key_presses(&self) -> FastHashSet<Key> {
self.presses.clone()
}

#[inline]
pub fn is_key_release(&self, key: Key) -> bool {
self.releases.contains(&key)
}

#[inline]
pub fn key_releases(&self) -> FastHashSet<Key> {
self.releases.clone()
}

pub fn is_key_repeat(&self, key: Key) -> bool {
if let Some(v) = self.downs.get(&key) {
match *v {
Expand Down
21 changes: 20 additions & 1 deletion src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ use self::inside::{ctx, CTX};
use self::keyboard::{Key, KeyboardParams};
use self::mouse::{MouseButton, MouseParams};
use self::touchpad::{GesturePan, GestureTap, TouchPadParams};
use crate::utils::hash::FastHashSet;

/// The setup parameters of all supported input devices.
#[derive(Debug, Clone, Copy, Default)]
Expand Down Expand Up @@ -231,12 +232,20 @@ pub fn is_key_press(key: Key) -> bool {
ctx().is_key_press(key)
}

#[inline]
pub fn key_presses() -> FastHashSet<Key> {
ctx().key_presses()
}

/// Checks if a key has been released during the last frame.
#[inline]
pub fn is_key_release(key: Key) -> bool {
ctx().is_key_release(key)
}

#[inline]
pub fn key_releases() -> FastHashSet<Key>{
ctx().key_releases()
}
/// Checks if a key has been repeated during the last frame.
#[inline]
pub fn is_key_repeat(key: Key) -> bool {
Expand Down Expand Up @@ -267,12 +276,22 @@ pub fn is_mouse_press(button: MouseButton) -> bool {
ctx().is_mouse_press(button)
}

/// Checks if a mouse button has been pressed during last frame.
#[inline]
pub fn mouse_presses() -> FastHashSet<MouseButton> {
ctx().mouse_presses()
}

/// Checks if a mouse button has been released during last frame.
#[inline]
pub fn is_mouse_release(button: MouseButton) -> bool {
ctx().is_mouse_release(button)
}

#[inline]
pub fn mouse_releases() -> FastHashSet<MouseButton> {
ctx().mouse_releases()
}
/// Checks if a mouse button has been clicked during last frame.
#[inline]
pub fn is_mouse_click(button: MouseButton) -> bool {
Expand Down
22 changes: 19 additions & 3 deletions src/input/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Default for MouseParams {
}

/// Describes a button of a mouse controller.
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Serialize)]
pub enum MouseButton {
Left,
Right,
Expand Down Expand Up @@ -109,7 +109,10 @@ impl Mouse {
detector.on_pressed(self.position);
self.click_detectors.insert(button, detector);
}

#[inline]
pub fn mouse_presses(&self) -> FastHashSet<MouseButton> {
self.presses.clone()
}
#[inline]
pub fn on_button_released(&mut self, button: MouseButton) {
self.downs.remove(&button);
Expand All @@ -124,7 +127,10 @@ impl Mouse {
detector.on_released(self.position);
self.click_detectors.insert(button, detector);
}

#[inline]
pub fn mouse_releases(&self) -> FastHashSet<MouseButton> {
self.releases.clone()
}
#[inline]
pub fn on_wheel_scroll(&mut self, delta: (f32, f32)) {
self.scrol = delta.into();
Expand All @@ -140,11 +146,21 @@ impl Mouse {
self.presses.contains(&button)
}

#[inline]
pub fn button_presses(&self) -> FastHashSet<MouseButton> {
self.presses.clone()
}

#[inline]
pub fn is_button_release(&self, button: MouseButton) -> bool {
self.releases.contains(&button)
}

#[inline]
pub fn button_releases(&self) -> FastHashSet<MouseButton> {
self.releases.clone()
}

#[inline]
pub fn is_button_click(&self, button: MouseButton) -> bool {
if let Some(v) = self.click_detectors.get(&button) {
Expand Down
Loading