commit 8990ca3acfd1b5f0f127664fb2abe6979d3384bf Author: Melik Houij Date: Wed Feb 22 10:23:40 2023 +0100 initial Commit diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..84eaee4 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "UnnamedCarGame" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +gl = "0.14.0" +glfw = "0.51.0" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..6fa09d2 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,65 @@ +use std::convert::TryInto; + +use glfw; +use glfw::Context; +use gl; + +const ScreenWidth: u32 = 480; +const ScreenHeight: u32 = 320; +const TITLE: &str = "GLFWtest"; + +mod shader; + +fn main() { + + // initialize GLFW + let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); + // set window hints + glfw.window_hint(glfw::WindowHint::ContextVersion(3, 3)); + glfw.window_hint(glfw::WindowHint::OpenGlProfile(glfw::OpenGlProfileHint::Core)); + glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true)); + glfw.window_hint(glfw::WindowHint::Resizable(false)); + + + //create window + let (mut window, events) = glfw.create_window(ScreenWidth, ScreenHeight, TITLE, glfw::WindowMode::Windowed).unwrap(); + let (screen_width, screen_height) = window.get_framebuffer_size(); + + // setup window + window.make_current(); + window.set_key_polling(true); + + gl::load_with(|ptr| window.get_proc_address(ptr) as *const _); + + unsafe { + gl::Viewport(0, 0, screen_width, screen_height); + gl::ClearColor(0.4, 0.4, 0.4, 1.0); + } + + while !window.should_close() { + glfw.poll_events(); + for(_, event) in glfw::flush_messages(&events) { + glfw_handle_event(&mut window, event); + }} + + window.swap_buffers(); + + +} + +pub fn clear_color(R: f32, G: f32, B: f32, A: f32) { + unsafe { gl::ClearColor(R, G, B, A) } +} + +fn glfw_handle_event(window: &mut glfw::Window, event: glfw::WindowEvent) { + use glfw::WindowEvent as Event; + use glfw::Key; + use glfw::Action; + + match event { + Event::Key(Key::Escape, _, Action::Press, _) => { + window.set_should_close(true); + }, + _ => {}, + } +}