diff options
-rw-r--r-- | CHANGELOG.txt | 12 | ||||
-rw-r--r-- | Cargo.toml | 15 | ||||
-rw-r--r-- | shader/main.frag.glsl | 7 | ||||
-rw-r--r-- | shader/main.vert.glsl | 7 | ||||
-rw-r--r-- | source/dw.rs | 10 | ||||
-rw-r--r-- | source/dw/app.rs | 14 | ||||
-rw-r--r-- | source/dw/app/getshdprg.rs | 71 | ||||
-rw-r--r-- | source/dw/app/ini.rs | 6 | ||||
-rw-r--r-- | source/dw/app/inigfx.rs | 12 | ||||
-rw-r--r-- | source/dw/app/inisig.rs | 28 | ||||
-rw-r--r-- | source/dw/app/lop.rs | 11 | ||||
-rw-r--r-- | source/dw/app/new.rs | 10 | ||||
-rw-r--r-- | source/main.rs (renamed from source/ini.rs) | 2 | ||||
-rwxr-xr-x | validateShaders.py | 33 |
14 files changed, 217 insertions, 21 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt index c767f78..81de880 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,15 @@ +# 0.1.0 + +* Bump minor version; +* Add basic vertex and fragment shaders; +* Update dependencies; +* Pad values in Cargo configuration file; +* Depend on libc crate; +* Set signal handlers; +* Specify versions of dependencies; +* Add shader program compiler routine; +* Add Python script for validating shaders; + # 0.0.0 * Add gitignore; @@ -1,17 +1,16 @@ [package] -name = "deltaworld" -version = "0.0.0" +name = "deltaworld" +version = "0.1.0" edition = "2021" [[bin]] name = "dw" -path = "source/ini.rs" +path = "source/main.rs" [profile.release] lto = true -[dependencies.gl] -version = "*" - -[dependencies.glfw] -version = "*" +[dependencies] +gl = "0.14.0" +glfw = "0.51.0" +libc = "0.2.144" diff --git a/shader/main.frag.glsl b/shader/main.frag.glsl new file mode 100644 index 0000000..e81d967 --- /dev/null +++ b/shader/main.frag.glsl @@ -0,0 +1,7 @@ +#version 150 core + +out vec4 col; + +void main() { + col = vec4(0.7137f,0.0941f,0.2000f,1.0f); +} diff --git a/shader/main.vert.glsl b/shader/main.vert.glsl new file mode 100644 index 0000000..d6c02c9 --- /dev/null +++ b/shader/main.vert.glsl @@ -0,0 +1,7 @@ +#version 150 core + +in vec3 pos; + +void main() { + gl_Position = vec4(pos.x,pos.y,pos.z,1.0f); +} diff --git a/source/dw.rs b/source/dw.rs index 976f049..409368e 100644 --- a/source/dw.rs +++ b/source/dw.rs @@ -8,8 +8,16 @@ pub struct VerTyp { pub const VER: VerTyp = VerTyp { maj: 0x0, - min: 0x0, + min: 0x1, pat: 0x0, }; +pub const fn datpth() -> &'static str { + if cfg!(unix) { + //return "/usr/share/local/deltaworld"; + return "/home/delta/Repositories/deltaworld" + } + return ""; +} + pub mod app; diff --git a/source/dw/app.rs b/source/dw/app.rs index caba0c1..14b2292 100644 --- a/source/dw/app.rs +++ b/source/dw/app.rs @@ -2,18 +2,26 @@ extern crate glfw; +use gl::types::GLuint; use glfw::{Glfw,Window,WindowEvent}; +use std::sync::atomic::AtomicBool; use std::sync::mpsc::Receiver; pub struct Gfx { - evt: Receiver<(f64,WindowEvent)>, - glfw: Glfw, - win: Window, + evt: Receiver<(f64,WindowEvent)>, + glfw: Glfw, + shdprg: GLuint, + win: Window, } pub struct App { } +static mut GOTINT: AtomicBool = AtomicBool::new(false); + +pub mod getshdprg; pub mod ini; pub mod inigfx; +pub mod inisig; pub mod lop; +pub mod new; diff --git a/source/dw/app/getshdprg.rs b/source/dw/app/getshdprg.rs new file mode 100644 index 0000000..2e02ff8 --- /dev/null +++ b/source/dw/app/getshdprg.rs @@ -0,0 +1,71 @@ +// Copyright 2023 Gabriel Jensen. + +use crate::dw::{app::App,datpth}; + +extern crate gl; + +use gl::{AttachShader,COMPILE_STATUS,CompileShader,CreateProgram,CreateShader,DeleteShader,FALSE,FRAGMENT_SHADER,GetShaderiv,LinkProgram,ShaderSource,VERTEX_SHADER}; +use gl::types::{GLchar,GLenum,GLint,GLuint}; +use std::fs::read; +use std::ptr::{addr_of,null}; + +impl App { + pub fn getshdprg(&mut self) -> GLuint { + eprintln!("compiling shaders"); + + let cmpshd = |nam: & str,typ:GLenum| -> GLuint { + let typstr = match typ { + FRAGMENT_SHADER => "fragment", + VERTEX_SHADER => "vertex", + _ => panic!("invalid shader type {}",typ), + }; + + let filext = match typ { + FRAGMENT_SHADER => "frag", + VERTEX_SHADER => "vert", + _ => panic!("invalid shader type {}",typ), + }; + + let mut pth = String::new(); + pth.push_str(datpth()); + pth.push_str("/shader/"); + pth.push_str(nam); + pth.push( '.'); + pth.push_str(filext); + pth.push_str(".glsl"); + + eprintln!("compiling {} shader at \"{}\"",typstr,pth); + + let src = read(pth).expect("unable to read shader at"); + + unsafe { + let shd = CreateShader(typ); + let srcptr = src.as_ptr(); + ShaderSource(shd,0x1,addr_of!(srcptr) as *const *const GLchar,null::<GLint>()); + + CompileShader(shd); + + let mut sts:GLint = 0x0; + GetShaderiv(shd,COMPILE_STATUS,&mut sts); + if sts == FALSE as GLint {panic!("unable to compile shader");} + + return shd; + } + }; + + let frgshd = cmpshd("main",FRAGMENT_SHADER); + let vtxshd = cmpshd("main",VERTEX_SHADER); + + unsafe { + let prg = CreateProgram(); + AttachShader(prg,frgshd); + AttachShader(prg,vtxshd); + LinkProgram(prg); + + DeleteShader(frgshd); + DeleteShader(vtxshd); + + return prg; + } + } +} diff --git a/source/dw/app/ini.rs b/source/dw/app/ini.rs index 5d168eb..5e39556 100644 --- a/source/dw/app/ini.rs +++ b/source/dw/app/ini.rs @@ -1,7 +1,7 @@ // Copyright 2023 Gabriel Jensen. use crate::dw::app::App; -use crate::dw::VER; +use crate::dw::{datpth,VER}; extern crate glfw; @@ -9,6 +9,10 @@ impl App { pub fn ini(&mut self) -> i8 { println!("\x1B[0m\x1B[1mDeltaWorld\x1B[0m {}.{}.{} \u{2014} Copyright 2023 \x1B[0m\x1B[1mGabriel Jensen\x1B[0m.\n",VER.maj,VER.min,VER.pat); + eprintln!("data directory at \"{}\"",datpth()); + + self.inisig(); + let mut gfx = self.inigfx(); let cod = self.lop(&mut gfx); diff --git a/source/dw/app/inigfx.rs b/source/dw/app/inigfx.rs index b037757..9760ded 100644 --- a/source/dw/app/inigfx.rs +++ b/source/dw/app/inigfx.rs @@ -1,7 +1,6 @@ // Copyright 2023 Gabriel Jensen. -use crate::dw::app::App; -use crate::dw::app::Gfx; +use crate::dw::app::{App,Gfx}; extern crate gl; extern crate glfw; @@ -27,10 +26,13 @@ impl App { eprintln!("initialising opengl"); gl::load_with(|nam| glfw.get_proc_address_raw(nam) as *const c_void); + let shdprg = self.getshdprg(); + return Gfx { - glfw: glfw, - win: win, - evt: evt, + evt: evt, + glfw: glfw, + shdprg:shdprg, + win: win, }; } } diff --git a/source/dw/app/inisig.rs b/source/dw/app/inisig.rs new file mode 100644 index 0000000..12e8061 --- /dev/null +++ b/source/dw/app/inisig.rs @@ -0,0 +1,28 @@ +// Copyright 2023 Gabriel Jensen. + +use crate::dw::app::{App,GOTINT}; + +extern crate libc; + +use libc::{c_int,sighandler_t,signal,SIGINT,SIGTERM}; +use std::mem::transmute; +use std::sync::atomic::Ordering; + +fn hnd(sig: c_int) { + unsafe { + signal(sig,transmute::<fn(c_int),sighandler_t>(hnd)); + + GOTINT.store(true,Ordering::Relaxed); + } +} + +impl App { + pub fn inisig(&mut self) { + eprintln!("initialising signal handlers"); + + unsafe { + signal(SIGINT, transmute::<fn(c_int),sighandler_t>(hnd)); + signal(SIGTERM,transmute::<fn(c_int),sighandler_t>(hnd)); + } + } +} diff --git a/source/dw/app/lop.rs b/source/dw/app/lop.rs index af3e81c..928ddf1 100644 --- a/source/dw/app/lop.rs +++ b/source/dw/app/lop.rs @@ -1,18 +1,25 @@ // Copyright 2023 Gabriel Jensen. -use crate::dw::app::App; +use crate::dw::app::{App,GOTINT}; use crate::dw::app::Gfx; //extern crate gl; extern crate glfw; -use glfw::{Context}; +use std::sync::atomic::Ordering; impl App { pub fn lop(&mut self,gfx: &mut Gfx) -> i8 { eprintln!("entering main loop"); while !gfx.win.should_close() { + unsafe { + if GOTINT.load(Ordering::Relaxed) { + eprintln!("got interrupt"); + gfx.win.set_should_close(true); + } + } + gfx.glfw.poll_events(); } diff --git a/source/dw/app/new.rs b/source/dw/app/new.rs new file mode 100644 index 0000000..7c99f65 --- /dev/null +++ b/source/dw/app/new.rs @@ -0,0 +1,10 @@ +// Copyright 2023 Gabriel Jensen. + +use crate::dw::app::App; + +impl App { + pub fn new() -> App { + return App { + }; + } +} diff --git a/source/ini.rs b/source/main.rs index aa0802c..a698921 100644 --- a/source/ini.rs +++ b/source/main.rs @@ -7,7 +7,7 @@ use crate::dw::app::App; use std::process::exit; fn main() { - let mut app = App {}; + let mut app = App::new(); exit(app.ini() as i32); } diff --git a/validateShaders.py b/validateShaders.py new file mode 100755 index 0000000..64d91bf --- /dev/null +++ b/validateShaders.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +from subprocess import PIPE,run + +def validate(path:str): + print("validating \"",path,"\"... ",end='',sep='') + + path = "shader/" + path + ".glsl" + prog = "glslangValidator" + + status = run([prog,path],stdout=PIPE) + + result = status.returncode + if result != 0x0: + print("\x1B[38;5;161merror\x1B[0m") + print() + print(status.stdout.decode("utf-8")) + quit(0x1) + + print("\x1B[38;5;77mokay\x1B[0m") + +if __name__ == "__main__": + print("validating shaders...") + + shaders = [ + "main.frag", + "main.vert", + ] + + for shader in shaders: + validate(shader) + + print("success") |