diff options
-rw-r--r-- | Cargo.toml | 6 | ||||
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/sdl.rs | 36 | ||||
-rw-r--r-- | src/win.rs | 8 |
4 files changed, 48 insertions, 4 deletions
@@ -6,11 +6,11 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -gl = "0.14" -nalgebra = "0.31" gen_random_proc_macro = { path = "./gen_random_proc_macro" } gen_random = { path = "./gen_random" } -rand = { version = "0.8", features = ["small_rng"] } +rand = { version = "0.8.0", features = ["small_rng"] } serde_cbor = "0.11.2" serde = "1.0.150" serde_derive = "1.0.150" +gl = "0.14.0" +nalgebra = "0.31.4" diff --git a/src/main.rs b/src/main.rs index 346331c..f010914 100644 --- a/src/main.rs +++ b/src/main.rs @@ -295,7 +295,7 @@ fn try_main() -> Result<(), String> { let mut window = win::Window::new("AutoSDF", 1280, 720, true) .map_err(|e| format!("Error creating window: {e}"))?; let mut program = window.new_program(); - gen_program(&mut window, &mut program)?; + gen_program_with_seed(&mut window, &mut program, 1)?; //gen_program_from_string(&mut window, &mut program, "a263736466a167436f6d706f736583a1695472616e736c61746583a163463332fa3ea4c00ca163463332fa3e85dc00a163463332fa3f2bbdaea167436f6d706f736583a166526f7461746583a163463332fa3f750dc2a163463332fa3f5a7f0ea163463332fa3f2df98ca1634d696e82a167436f6d706f736583a167436f6d706f736582a16353696ea163463332fa3f7cc2a0a167436f6d706f736582684964656e74697479684964656e74697479a166537068657265a163463332fa3f26f8f6684964656e74697479a167436f6d706f736583a166526f7461746583a163463332fa3f1bfed8a163463332fa3f1e1e30a163463332fa3eddc6b0a1634d697883a167436f6d706f736583684964656e74697479a166537068657265a163463332fa3ea149ec684964656e74697479a167436f6d706f736583684964656e74697479a166537068657265a163463332fa3f6b0018684964656e74697479a163463332fa3e60a8d8684964656e74697479684964656e74697479684964656e746974796e636f6c6f725f66756e6374696f6ea165537153696ea163463332fa3ebaa7ec")?; let mut buffer = window.create_buffer(); @@ -1,6 +1,8 @@ #![allow(non_camel_case_types)] #![allow(dead_code)] #![allow(non_snake_case)] +/// this module provides SDL type definitions, and more rust-y wrappers around +/// SDL functions. use std::ffi::{c_char, c_float, c_int, c_void, CStr, CString}; use std::mem; @@ -699,6 +701,9 @@ extern "C" { ) -> SDL_AudioDeviceID; fn SDL_PauseAudioDevice(dev: SDL_AudioDeviceID, pause_on: c_int); fn SDL_CloseAudioDevice(dev: SDL_AudioDeviceID); + fn SDL_GetClipboardText() -> *mut c_char; + fn SDL_SetClipboardText(text: *const c_char) -> c_int; + fn SDL_free(mem: *mut c_void); } pub mod scancode { @@ -1120,3 +1125,34 @@ pub unsafe fn pause_audio_device(dev: SDL_AudioDeviceID, pause_on: bool) { pub unsafe fn close_audio_device(dev: SDL_AudioDeviceID) { SDL_CloseAudioDevice(dev); } + +pub unsafe fn get_clipboard_text() -> Result<String, String> { + let ptr = SDL_GetClipboardText(); + if ptr.is_null() { + // according to some sources, SDL_GetClipboardText returns NULL on failure. + // according to the wiki, it returns an empty string. + return Err(get_err()); + } + + let cstr = CStr::from_ptr(ptr); + let Ok(r#str) = cstr.to_str() else { + // this should never happen since SDL should always give back valid UTF-8. + return Err("clipboard doesn't contain UTF-8".to_string()); + }; + let string = r#str.to_string(); + SDL_free(ptr as *mut c_void); + Ok(string) +} + +pub unsafe fn set_clipboard_text(s: &str) -> Result<(), String> { + let Ok(cstring) = CString::new(s) else { + return Err("can't put null bytes in clipboard text.".to_string()); + }; + + let result = SDL_SetClipboardText(cstring.as_ptr()); + if result == 0 { + Ok(()) + } else { + Err(get_err()) + } +} @@ -1325,6 +1325,14 @@ impl Window { pub fn swap(&mut self) { unsafe { sdl::gl_swap_window(self.sdlwin) }; } + + pub fn get_clipboard_text(&mut self) -> Result<String, String> { + unsafe { sdl::get_clipboard_text() } + } + + pub fn set_clipboard_text(&mut self, s: &str) -> Result<(), String> { + unsafe { sdl::set_clipboard_text(s) } + } } impl Drop for Window { |