Skip to content

Commit

Permalink
Merge branch 'master' into dxr
Browse files Browse the repository at this point in the history
  • Loading branch information
polymonster committed Jan 27, 2025
2 parents e86cae0 + 044bca1 commit 943abb6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
15 changes: 5 additions & 10 deletions src/imgui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ use std::ffi::CString;

use maths_rs::Vec4f;

use std::ptr::addr_of;
use std::ptr::addr_of_mut;

macro_rules! static_ref_mut{
($place:expr) => {
&mut *addr_of_mut!($place)
}
}
use crate::static_ref;
use crate::static_ref_mut;
use crate::static_ref_array_mut;

fn to_im_vec4(v: Vec4f) -> ImVec4 {
unsafe {
Expand Down Expand Up @@ -641,7 +636,7 @@ impl<D, A> ImGui<D, A> where D: Device, A: App, D::RenderPipeline: gfx::Pipeline
let ini_file = parent.join("imgui.ini");
static mut NULL_INI_FILE : Option<CString> = None;
NULL_INI_FILE = Some(CString::new(ini_file.to_str().unwrap().to_string()).unwrap());
if let Some(i) = &*addr_of!(NULL_INI_FILE) {
if let Some(i) = static_ref!(NULL_INI_FILE) {
io.IniFilename = i.as_ptr() as _;
}
};
Expand Down Expand Up @@ -1158,7 +1153,7 @@ impl<D, A> ImGui<D, A> where D: Device, A: App, D::RenderPipeline: gfx::Pipeline
"This is some useful text.\0".as_ptr() as *const i8,
);

igColorEdit3("clear color\0".as_ptr() as _, CLEAR_COLOUR.as_mut_ptr(), 0); // Edit 3 floats representing a color
igColorEdit3("color\0".as_ptr() as _, static_ref_array_mut!(CLEAR_COLOUR), 0); // Edit 3 floats representing a color

igText(
"Application average %.3f ms/frame (%.1f FPS)\0".as_ptr() as _,
Expand Down
30 changes: 29 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,35 @@ fn get_files_recursive(dir: &str, mut files: Vec<String>) -> Vec<String> {
files
}

// macros for static addr of convenience
#[macro_export]
macro_rules! static_ref{
($place:expr) => {
&*std::ptr::addr_of!($place)
}
}

#[macro_export]
macro_rules! static_ref_mut{
($place:expr) => {
&mut *std::ptr::addr_of_mut!($place)
}
}

#[macro_export]
macro_rules! static_ref_array{
($place:expr) => {
&*std::ptr::addr_of!($place[0])
}
}

#[macro_export]
macro_rules! static_ref_array_mut{
($place:expr) => {
&mut *std::ptr::addr_of_mut!($place[0])
}
}

/// This is a hardcoded compile time selection of os backend for windows as win32
#[cfg(target_os = "windows")]
pub use os::win32 as os_platform;
Expand All @@ -153,7 +182,6 @@ pub use gfx::d3d12 as gfx_platform;
#[cfg(target_os = "windows")]
pub use av::wmf as av_platform;


/// Most commonly used re-exported types.
#[cfg(target_os = "windows")]
pub mod prelude {
Expand Down
9 changes: 5 additions & 4 deletions src/os/win32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use std::result;
use std::collections::HashMap;
use std::ffi::CString;

use std::ptr::addr_of;
use crate::static_ref;
use crate::static_ref_mut;

#[derive(Clone)]
pub struct App {
Expand Down Expand Up @@ -727,10 +728,10 @@ impl super::App for App {

fn enumerate_display_monitors() -> Vec<super::MonitorInfo> {
unsafe {
MONITOR_ENUM.clear();
static_ref_mut!(MONITOR_ENUM).clear();
let _ = EnumDisplayMonitors(HDC::default(), None, Some(enum_func), LPARAM(0));
let mut monitors: Vec<super::MonitorInfo> = Vec::new();
for m in &*addr_of!(MONITOR_ENUM) {
for m in static_ref!(MONITOR_ENUM) {
monitors.push(m.clone());
}
monitors
Expand Down Expand Up @@ -1244,7 +1245,7 @@ extern "system" fn enum_func(
1.0
};

MONITOR_ENUM.push(super::MonitorInfo {
static_ref_mut!(MONITOR_ENUM).push(super::MonitorInfo {
rect: super::Rect {
x: info.rcMonitor.left,
y: info.rcMonitor.top,
Expand Down

0 comments on commit 943abb6

Please sign in to comment.