Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(duration_as_u128)]
- #[macro_use]
- extern crate image;
- extern crate gl;
- extern crate winapi;
- extern crate glfw;
- use std::ffi::CString;
- use std::ptr;
- use std::time::{SystemTime};
- use std::mem;
- use std::str;
- use std::sync::mpsc::Receiver;
- use self::glfw::{Context, Key, Action};
- use gl::types::{GLenum, GLuint, GLint, GLchar, GLboolean, GLfloat, GLsizeiptr};
- //use ash::vk;
- //use ash::version::DeviceV1_0;
- //bitbucket, sourcetree, trello
- //rustup default nightly
- //gl_generator, khronos_api maybe not used
- //expand more things into several lines for readability
- //make some stuff inline
- //see if variables in fn are duplicated when returning or what and if there is a way to chane it if it is duplicating
- //TODO make license
- fn main() {
- println!("Ensuring safe installation:");
- unsafe { ensure_safety(2 * 1024 * 1024, 4 * 1024 * 1024); }
- let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
- glfw.window_hint(glfw::WindowHint::ContextVersion(3, 3));
- glfw.window_hint(glfw::WindowHint::OpenGlProfile(glfw::OpenGlProfileHint::Core));
- #[cfg(target_os = "macos")]
- glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true));
- // glfw window creation
- // --------------------
- let (mut window, events) = glfw.create_window(800, 600, "Survival Game", glfw::WindowMode::Windowed)
- .expect("Failed to create GLFW window");
- window.make_current();
- window.set_key_polling(true);
- window.set_framebuffer_size_polling(true);
- gl::load_with(|symbol| window.get_proc_address(symbol) as *const _);
- let vs = compile_shader(
- "
- #version 150
- in vec2 position;
- void main() {
- gl_Position = vec4(position, 0.0, 1.0);
- }
- ",
- gl::VERTEX_SHADER
- );
- let fs = compile_shader(
- "
- #version 150
- out vec4 out_color;
- void main() {
- out_color = vec4(1.0, 1.0, 1.0, 1.0);
- }
- ",
- gl::FRAGMENT_SHADER
- );
- let program = link_program(vs, fs);
- let mut vao = 0;
- let mut vbo = 0;
- unsafe {
- // Create Vertex Array Object
- gl::GenVertexArrays(1, &mut vao);
- gl::BindVertexArray(vao);
- // Create a Vertex Buffer Object and copy the vertex data to it
- gl::GenBuffers(1, &mut vbo);
- gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
- let vertex_data: [GLfloat; 6] = [0.0, 0.5, 0.5, -0.5, -0.5, -0.5];
- gl::BufferData(
- gl::ARRAY_BUFFER,
- (vertex_data.len() * mem::size_of::<GLfloat>()) as GLsizeiptr,
- mem::transmute(&vertex_data[0]),
- gl::STATIC_DRAW,
- );
- // Use shader program
- gl::UseProgram(program);
- gl::BindFragDataLocation(program, 0, CString::new("out_color").unwrap().as_ptr());
- // Specify the layout of the vertex data
- let pos_attr = gl::GetAttribLocation(program, CString::new("position").unwrap().as_ptr());
- gl::EnableVertexAttribArray(pos_attr as GLuint);
- gl::VertexAttribPointer(
- pos_attr as GLuint,
- 2,
- gl::FLOAT,
- gl::FALSE as GLboolean,
- 0,
- ptr::null(),
- );
- }
- let mut frames = 0;
- let mut start_time = SystemTime::now();
- while !window.should_close() {
- frames += 1;
- if start_time.elapsed().unwrap().as_millis() >= 1000 {
- println!("{}", frames);
- frames = 0;
- start_time = SystemTime::now();
- }
- //let start_time = SystemTime::now();
- process_events(&mut window, &events);
- unsafe {
- gl::ClearColor(0.3, 0.3, 0.3, 1.0);
- gl::Clear(gl::COLOR_BUFFER_BIT);
- for x in 0..10000 {
- gl::DrawArrays(gl::TRIANGLES, 0, 3);
- }
- }
- //println!("time: {}", start_time.elapsed().unwrap().as_nanos());
- window.swap_buffers();
- glfw.poll_events();
- }
- }
- fn process_events(window: &mut glfw::Window, events: &Receiver<(f64, glfw::WindowEvent)>) {
- for (_, event) in glfw::flush_messages(events) {
- match event {
- glfw::WindowEvent::FramebufferSize(width, height) => {
- // make sure the viewport matches the new window dimensions; note that width and
- // height will be significantly larger than specified on retina displays.
- unsafe { gl::Viewport(0, 0, width, height) }
- }
- glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => window.set_should_close(true),
- _ => {}
- }
- }
- }
- fn compile_shader(
- src: &str,
- ty: GLenum,
- ) -> GLuint {
- let shader;
- unsafe {
- shader = gl::CreateShader(ty);
- // Attempt to compile the shader
- let c_str = CString::new(src.as_bytes()).unwrap();
- gl::ShaderSource(shader, 1, &c_str.as_ptr(), ptr::null());
- gl::CompileShader(shader);
- // Get the compile status
- let mut status = gl::FALSE as GLint;
- gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut status);
- // Fail on error
- if status != (gl::TRUE as GLint) {
- let mut len = 0;
- gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut len);
- let mut buf = Vec::with_capacity(len as usize);
- buf.set_len((len as usize) - 1); // subtract 1 to skip the trailing null character
- gl::GetShaderInfoLog(
- shader,
- len,
- ptr::null_mut(),
- buf.as_mut_ptr() as *mut GLchar,
- );
- panic!(
- "{}",
- str::from_utf8(&buf)
- .ok()
- .expect("ShaderInfoLog not valid utf8")
- );
- }
- }
- shader
- }
- fn link_program(
- vs: GLuint,
- fs: GLuint,
- ) -> GLuint {
- unsafe {
- let program = gl::CreateProgram();
- gl::AttachShader(program, vs);
- gl::AttachShader(program, fs);
- gl::LinkProgram(program);
- // Get the link status
- let mut status = gl::FALSE as GLint;
- gl::GetProgramiv(program, gl::LINK_STATUS, &mut status);
- // Fail on error
- if status != (gl::TRUE as GLint) {
- let mut len: GLint = 0;
- gl::GetProgramiv(program, gl::INFO_LOG_LENGTH, &mut len);
- let mut buf = Vec::with_capacity(len as usize);
- buf.set_len((len as usize) - 1); // subtract 1 to skip the trailing null character
- gl::GetProgramInfoLog(
- program,
- len,
- ptr::null_mut(),
- buf.as_mut_ptr() as *mut GLchar,
- );
- panic!(
- "{}",
- str::from_utf8(&buf)
- .ok()
- .expect("ProgramInfoLog not valid utf8")
- );
- }
- program
- }
- }
- unsafe fn ensure_safety(min_ram: u64,
- rec_ram: u64,
- ) {
- //DEPENDENCY CHECKS
- //Check for OpenGL?
- //SPECIFICATION CHECKS7
- let mut memory_kb: u64 = 0;
- winapi::um::sysinfoapi::GetPhysicallyInstalledSystemMemory(&mut memory_kb as *mut u64);
- if memory_kb < min_ram {
- display_warning_with_box("Memory Warning", &format!("Your system RAM is well below the minimum requirements. It is recommended that you have at least {}GB of RAM. Your system only has {}GB. The game will either fail to run or you will experience severely degraded performance.", min_ram / (1024 * 1024), memory_kb / (1024 * 1024)));
- } else if memory_kb < rec_ram {
- display_warning_with_box("Memory Warning", &format!("Your system RAM meets the minimum requirements but is below the recommended specifications. {}GB of RAM is recommended but your system has {}GB. The game may not run at optimal performance.", rec_ram / (1024 * 1024), memory_kb / (1024 * 1024)));
- } else {
- println!("{}gb of RAM detected. Recommended requirements are met!", memory_kb / (1024 * 1024))
- }
- }
- unsafe fn display_error_with_box(title: &str,
- message: &str,
- ) -> ! {
- //might be a problem with the warning thing
- winapi::um::winuser::MessageBoxA(ptr::null_mut(), CString::new(message).unwrap().as_ptr(), CString::new(title).unwrap().as_ptr(), winapi::um::winuser::MB_OK | winapi::um::winuser::MB_ICONERROR);
- panic!("Error: {}", message);
- }
- unsafe fn display_warning_with_box(title: &str,
- message: &str,
- ) {
- winapi::um::winuser::MessageBoxA(ptr::null_mut(), CString::new(message).unwrap().as_ptr(), CString::new(title).unwrap().as_ptr(), winapi::um::winuser::MB_OK | winapi::um::winuser::MB_ICONWARNING);
- println!("Warning: {}", message);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement