Add Rust OpenGL example
This commit is contained in:
parent
f89a16075a
commit
9817d78cd0
4 changed files with 96 additions and 1 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -3,4 +3,7 @@ exercise2/build/
|
||||||
exercise3/build/
|
exercise3/build/
|
||||||
exercise4/build/
|
exercise4/build/
|
||||||
|
|
||||||
.vscode/ipch
|
.vscode/ipch
|
||||||
|
|
||||||
|
Cargo.lock
|
||||||
|
target/
|
19
.vscode/tasks.json
vendored
19
.vscode/tasks.json
vendored
|
@ -99,5 +99,24 @@
|
||||||
"clear": true
|
"clear": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "shell",
|
||||||
|
"label": "Run Example",
|
||||||
|
"command": "cd ecg-example && cargo run",
|
||||||
|
"args": [],
|
||||||
|
"problemMatcher": [
|
||||||
|
"$rustc"
|
||||||
|
],
|
||||||
|
"presentation": {
|
||||||
|
"clear": true
|
||||||
|
},
|
||||||
|
"linux": {
|
||||||
|
"options": {
|
||||||
|
"env": {
|
||||||
|
"RUST_BACKTRACE": "1",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
11
ecg-example/Cargo.toml
Normal file
11
ecg-example/Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
[package]
|
||||||
|
name = "ecg-example"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["hodasemi <michaelh.95@t-online.de>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
sdl2 = "*"
|
||||||
|
gl = "*"
|
62
ecg-example/src/main.rs
Normal file
62
ecg-example/src/main.rs
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
use gl::load_with;
|
||||||
|
|
||||||
|
use sdl2;
|
||||||
|
use sdl2::{event::Event, video::GLProfile};
|
||||||
|
|
||||||
|
fn main() -> Result<(), String> {
|
||||||
|
// init sdl2
|
||||||
|
let sdl_context = sdl2::init()?;
|
||||||
|
|
||||||
|
// init video_subsystem
|
||||||
|
let video_subsystem = sdl_context.video()?;
|
||||||
|
|
||||||
|
// gl context configuration
|
||||||
|
let gl_attr = video_subsystem.gl_attr();
|
||||||
|
gl_attr.set_context_profile(GLProfile::Core);
|
||||||
|
gl_attr.set_context_version(3, 3);
|
||||||
|
|
||||||
|
// create window
|
||||||
|
let window = video_subsystem
|
||||||
|
.window("ECG Example", 1280, 720)
|
||||||
|
.position_centered()
|
||||||
|
.opengl()
|
||||||
|
.build()
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
|
// create opengl context handle
|
||||||
|
let opengl_context = window.gl_create_context()?;
|
||||||
|
|
||||||
|
// load opengl functions from system library
|
||||||
|
load_with(|s| video_subsystem.gl_get_proc_address(s) as *const _);
|
||||||
|
|
||||||
|
// enable vsync
|
||||||
|
video_subsystem.gl_set_swap_interval(1);
|
||||||
|
|
||||||
|
// init sdl2 event system
|
||||||
|
let mut event_pump = sdl_context.event_pump()?;
|
||||||
|
|
||||||
|
// main loop
|
||||||
|
'running: loop {
|
||||||
|
// check for events
|
||||||
|
for event in event_pump.poll_iter() {
|
||||||
|
match event {
|
||||||
|
Event::Quit { .. } => break 'running,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// activate opengl context
|
||||||
|
window.gl_make_current(&opengl_context)?;
|
||||||
|
|
||||||
|
// OpenGL stuff ...
|
||||||
|
unsafe {
|
||||||
|
gl::ClearColor(1.0, 0.0, 0.0, 1.0);
|
||||||
|
gl::Clear(gl::COLOR_BUFFER_BIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// swap back buffer
|
||||||
|
window.gl_swap_window();
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in a new issue