Created
September 5, 2020 21:27
-
-
Save Abdillah/9c11b927bf23021243f42a1464d3a15c to your computer and use it in GitHub Desktop.
Glutin transparent window
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "transpi" | |
version = "0.1.0" | |
authors = ["Hernawan Faïz Abdillah <[email protected]>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
gl = "0.11.0" | |
glutin = "0.24.1" | |
takeable-option = "0.4" | |
piston-viewport = "1.0.0" | |
piston2d-graphics = "0.37.0" | |
piston2d-opengl_graphics = "0.74.0" | |
[build-dependencies] | |
gl_generator = "0.13" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mod support; | |
use glutin::event::{Event, WindowEvent}; | |
use glutin::event_loop::{ControlFlow, EventLoop}; | |
use glutin::window::WindowBuilder; | |
use glutin::ContextBuilder; | |
use opengl_graphics::{GlGraphics, OpenGL}; | |
use gl as glapi; | |
pub struct App { | |
gl: GlGraphics, | |
} | |
fn main() { | |
let el = EventLoop::new(); | |
let wb = WindowBuilder::new() | |
.with_title("A transparent window!") | |
.with_decorations(false) | |
.with_transparent(true); | |
let windowed_context = | |
ContextBuilder::new() | |
.with_gl(glutin::GlRequest::GlThenGles { | |
opengl_version: (3 as u8, 2 as u8), | |
opengles_version: (3 as u8, 2 as u8), | |
}) | |
.build_windowed(wb, &el).unwrap(); | |
let windowed_context = unsafe { windowed_context.make_current().unwrap() }; | |
glapi::load_with(|s| windowed_context.get_proc_address(s) as *const _); | |
println!( | |
"Pixel format of the window's GL context: {:?}", | |
windowed_context.get_pixel_format() | |
); | |
let opengl = OpenGL::V3_2; | |
let mut gl = GlGraphics::new(opengl); | |
el.run(move |event, _, control_flow| { | |
println!("{:?}", event); | |
*control_flow = ControlFlow::Wait; | |
match event { | |
Event::LoopDestroyed => return, | |
Event::WindowEvent { event, .. } => match event { | |
WindowEvent::Resized(physical_size) => { | |
windowed_context.resize(physical_size) | |
} | |
WindowEvent::CloseRequested => { | |
*control_flow = ControlFlow::Exit | |
} | |
_ => (), | |
}, | |
Event::RedrawRequested(_) => { | |
use graphics::*; | |
const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0]; | |
const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0]; | |
let square = rectangle::square(0.0, 0.0, 50.0); | |
let (x, y) = (20.0 / 2.0, 20.0 / 2.0); | |
gl.draw(viewport::Viewport { | |
rect: [ 20, 20, 40, 40 ], | |
draw_size: [ 20, 20 ], | |
window_size: [ 20.0, 20.0 ], | |
}, |c, gl| { | |
// Clear the screen. | |
// clear(GREEN, gl); | |
let transform = c | |
.transform | |
.trans(x, y) | |
.rot_rad(20.0) | |
.trans(-25.0, -25.0); | |
// Draw a box rotating around the middle of the screen. | |
rectangle(RED, square, transform, gl); | |
}); | |
windowed_context.swap_buffers().unwrap(); | |
} | |
_ => (), | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment