Skip to content

Instantly share code, notes, and snippets.

@sztomi
Created August 15, 2020 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sztomi/b9f82e48823e7e334bda2254e93f5089 to your computer and use it in GitHub Desktop.
Save sztomi/b9f82e48823e7e334bda2254e93f5089 to your computer and use it in GitHub Desktop.
use std::env;
use std::mem;
use std::os::raw::c_int;
use std::path::PathBuf;
use std::ptr;
use anyhow::Result;
use sciter::dom;
use sciter::dom::event::{EventHandler, DRAW_EVENTS, EVENT_GROUPS};
use sciter::dom::{Element, HELEMENT};
use sciter::graphics::{self, rgb, Graphics, HGFX};
use sciter::types::HWINDOW;
use sciter::types::RECT;
use x11::glx::{
glXChooseVisual, glXCreateContext, glXGetCurrentContext, glXGetCurrentDisplay, glXMakeCurrent,
GLXContext, GLX_BLUE_SIZE, GLX_DOUBLEBUFFER, GLX_GREEN_SIZE, GLX_RED_SIZE, GLX_RGBA,
};
use x11::xlib::{Display, XDefaultScreen};
struct Molehill {
hwnd: HWINDOW,
mainGLRC: GLXContext,
thisGLRC: GLXContext,
hdc: *mut Display,
}
impl Molehill {
fn new() -> Self {
Self {
hwnd: ptr::null_mut(),
mainGLRC: ptr::null_mut(),
thisGLRC: ptr::null_mut(),
hdc: ptr::null_mut(),
}
}
fn init_drawing(&mut self) {
if self.mainGLRC != ptr::null_mut() {
return;
}
println!("init drawing");
unsafe {
self.mainGLRC = glXGetCurrentContext();
self.hdc = glXGetCurrentDisplay();
let mut attrs: [c_int; 9] = [
GLX_RGBA,
GLX_DOUBLEBUFFER,
GLX_RED_SIZE,
1,
GLX_GREEN_SIZE,
1,
GLX_BLUE_SIZE,
1,
0,
];
let vis = glXChooseVisual(self.hdc, XDefaultScreen(self.hdc), attrs.as_mut_ptr());
self.thisGLRC = glXCreateContext(self.hdc, vis, self.mainGLRC, 1);
}
}
}
impl EventHandler for Molehill {
fn get_subscription(&mut self) -> Option<EVENT_GROUPS> {
Some(EVENT_GROUPS::HANDLE_DRAW)
}
fn attached(&mut self, root: HELEMENT) {
println!("attached");
self.hwnd = dom::Element::from(root).get_hwnd(true);
}
fn on_draw(&mut self, he: HELEMENT, gfx: HGFX, area: &RECT, layer: DRAW_EVENTS) -> bool {
if layer != DRAW_EVENTS::DRAW_CONTENT {
return false;
}
println!("DRAW {:?}", layer);
unsafe {
let currentGLRC = glXGetCurrentContext();
if currentGLRC == ptr::null_mut() {
return false;
}
println!("hello?");
let mut gfx = Graphics::from(gfx);
gfx.flush();
self.init_drawing();
glXMakeCurrent(self.hdc, self.hwnd as u64, self.thisGLRC);
gl::ClearColor(0.9, 0.3, 0.5, 1.0);
gl::Flush();
glXMakeCurrent(self.hdc, self.hwnd as u64, currentGLRC);
println!("draw");
}
true
}
}
fn main() -> Result<()> {
let cwd = env::current_dir()?;
let cwd = PathBuf::from(cwd.into_os_string());
let path = env::var("PATH")?;
let mut paths: Vec<_> = env::split_paths(&path).collect();
paths.push(PathBuf::from(format!(
"{}/sdk/bin.lnx/x64",
cwd.to_str().unwrap()
)));
let paths = env::join_paths(paths)?.to_os_string();
env::set_var("PATH", &paths);
sciter::set_options(sciter::RuntimeOptions::DebugMode(true)).unwrap();
let mut frame = sciter::Window::new();
frame.register_behavior("molehill", || Box::new(Molehill::new()));
let html = include_bytes!("../resources/main.htm");
frame.load_html(html, None);
frame.run_app();
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment