diff options
Diffstat (limited to 'source/dw')
-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 |
7 files changed, 141 insertions, 11 deletions
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 { + }; + } +} |