From 9817d78cd0808d2776b89f6ce5cfd019ccce13c3 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Sat, 20 Jul 2019 08:58:27 +0200 Subject: [PATCH] Add Rust OpenGL example --- .gitignore | 5 +++- .vscode/tasks.json | 19 +++++++++++++ ecg-example/Cargo.toml | 11 ++++++++ ecg-example/src/main.rs | 62 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 ecg-example/Cargo.toml create mode 100644 ecg-example/src/main.rs diff --git a/.gitignore b/.gitignore index 10891cc..20478a6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,7 @@ exercise2/build/ exercise3/build/ exercise4/build/ -.vscode/ipch \ No newline at end of file +.vscode/ipch + +Cargo.lock +target/ \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 8f14c13..b55a66d 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -99,5 +99,24 @@ "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", + } + } + } + }, ] } \ No newline at end of file diff --git a/ecg-example/Cargo.toml b/ecg-example/Cargo.toml new file mode 100644 index 0000000..5ce3629 --- /dev/null +++ b/ecg-example/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "ecg-example" +version = "0.1.0" +authors = ["hodasemi "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +sdl2 = "*" +gl = "*" diff --git a/ecg-example/src/main.rs b/ecg-example/src/main.rs new file mode 100644 index 0000000..9d00367 --- /dev/null +++ b/ecg-example/src/main.rs @@ -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(()) +}