Created
August 3, 2021 14:12
-
-
Save newvertex/fc3c8f7ce4bc6cca305b87d1821ac941 to your computer and use it in GitHub Desktop.
Basic setup OpenGL with SDL2 in Rust lang
This file contains 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
// Cargo.toml | |
// ----------------------- | |
// [dependencies] | |
// gl = "0.14.0" | |
// [dependencies.sdl2] | |
// version = "0.34.5" | |
// features = ["bundled", "static-link"] | |
use std::thread; | |
use std::time; | |
use sdl2; | |
const WIDTH: u32 = 480; | |
const HEIGHT: u32 = 320; | |
const TITLE: &str = "Hello From OpenGL and SDL"; | |
fn main() { | |
create_sdl_window(WIDTH, HEIGHT, TITLE); | |
} | |
// just a helper function to get the original string value from gl::GetString function call | |
fn gl_get_string<'a>(name: gl::types::GLenum) -> &'a str { | |
let v = unsafe { gl::GetString(name) }; | |
let v: &std::ffi::CStr = unsafe { std::ffi::CStr::from_ptr(v as *const i8) }; | |
v.to_str().unwrap() | |
} | |
fn create_sdl_window(width: u32, height: u32, title: &str) { | |
let sdl_context = sdl2::init().expect("SDL: Failed on init."); | |
let video_subsystem = sdl_context.video().unwrap(); | |
// set the OpenGL version and profile | |
let gl_attr = video_subsystem.gl_attr(); | |
gl_attr.set_context_profile(sdl2::video::GLProfile::Core); | |
gl_attr.set_context_version(3, 3); | |
gl_attr.set_stencil_size(8); | |
// create main window | |
let window = video_subsystem.window(title, width, height) | |
.opengl() | |
.build() | |
.expect("SDL: Failed to create window."); | |
// create the opengl context and load the function pointers into their respective function pointer | |
let ctx = window.gl_create_context().unwrap(); | |
gl::load_with(|s| video_subsystem.gl_get_proc_address(s) as *const _); | |
// print OpenGL version from SDL | |
println!("SDL: OpenGL Context Created."); | |
println!("SDL: Context profile: {:?}, OpenGL version: {:?}", gl_attr.context_profile(), gl_attr.context_version()); | |
unsafe { | |
gl::Viewport(0, 0, width as i32, height as i32); | |
gl::ClearColor(0.2, 0.3, 0.4, 1.0); | |
} | |
// print OpenGL version from gl-rs | |
println!("OpenGL version: {}", gl_get_string(gl::VERSION)); | |
println!("GLSL version: {}", gl_get_string(gl::SHADING_LANGUAGE_VERSION)); | |
let mut event_pump = sdl_context.event_pump().unwrap(); | |
let mut is_running = true; | |
loop { | |
// handle Events | |
for event in event_pump.poll_iter() { | |
sdl_handle_event(&window, event, &mut is_running); | |
} | |
if !is_running { | |
break; | |
} | |
// Render | |
unsafe { | |
gl::Clear(gl::COLOR_BUFFER_BIT); | |
} | |
// Draw on Screen | |
window.gl_swap_window(); | |
thread::sleep(time::Duration::new(0, 1_000_000_000u32 / 60)); | |
} | |
} | |
fn sdl_handle_event(window: &sdl2::video::Window, event: sdl2::event::Event, is_running: &mut bool) { | |
use sdl2::event::Event; | |
use sdl2::keyboard::Keycode; | |
match event { | |
Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), ..}=> { | |
*is_running = false; | |
}, | |
// Handle other events here | |
_ => {}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment