Images
The images area loads pictures and draws them in your windows. Wraith holds them on the GPU for you, in the overlay and anywhere else they get drawn.
Load one
Section titled “Load one”images.load(path) reads and decodes a file on a worker, so the game doesn’t stall. A path that isn’t full is in your plugin’s own folder, like images/logo.png. Anywhere outside your plugin’s folders needs "files": true in your permissions. It reads PNG, JPEG, BMP, TGA and the first frame of a GIF. Sides over 16384 pixels get refused, and a file that’s missing fails with NotFound.
import { Wraith } from "wraith";
const images = Wraith.images();
const logo = await images.load("images/logo.png");local Wraith = require("wraith").Wraith
local images = Wraith.images()
local logotask.spawn(function() logo = images:load("images/logo.png"):await()end)logo = await Images.Instance.LoadAsync("images/logo.png");static Wraith::Image logo;
static sal::Future<void> load_logo() { auto loaded = co_await wraith::api().images().load("images/logo.png"); if (loaded) logo = *loaded; co_return {};}use std::sync::Mutex;
use wraith::{Image, ImageError};
static LOGO: Mutex<Option<Image>> = Mutex::new(None);
fn load_logo() { let _ = salamander::spawn(async { let image = wraith::api().images().load("images/logo.png").await?; *LOGO.lock().unwrap() = Some(image); Ok::<(), ImageError>(()) });}From a task, sdk.tasks.wait waits on it.
fn loadLogo(self: *Plugin) !void { self.logo = try sdk.tasks.wait(self.ctx.wraith.images().load("images/logo.png"));}
// in starttry ctx.spawn(loadLogo, .{self});The SDK’s sdk.images.load(io, allocator, path) is a Zig helper that reads any file on a worker. Its path is a full one, so join it to ctx.wraith.config().pluginDirectory() for your own.
static sal_Promise loading;static Wraith_Image logo;
static void loaded(uintptr_t self) { (void)self; sal_Promise_take(loading, &logo); /* no table if it failed */ sal_Promise_release(loading);}
static const sal_Task_table loaded_table = { SAL_TABLE(sal_Task), .call = loaded };
/* in start */loading = Wraith_Images_load(Wraith_Api_images(w), SAL_STR("images/logo.png"));sal_Promise_then(loading, (sal_Task){ 0, &loaded_table }, (sal_Queue){ 0, NULL });Already have the bytes? images.decode(bytes) decodes them right there, so keep it off the render thread. Raw RGBA pixels go to images.create(width, height, rgba). An image’s dimensions() gives its width and height in pixels.
Draw it
Section titled “Draw it”images.draw(image, size) draws it in the current window, inside a window’s draw or a draw callback. A zero size draws it at its own size.
import { Wraith } from "wraith";
Wraith.ui().addWindow({ title: "Logo", icon: "image" }, () => { if (!logo) return; images.draw(logo, { x: 0, y: 0 }); // its own size images.draw(logo, { x: 64, y: 64 }); // 64 by 64});local Wraith = require("wraith").Wraith
Wraith.ui():addWindow({ title = "Logo", icon = "image" }, function() if logo then images:draw(logo, { x = 0, y = 0 }) -- its own size images:draw(logo, { x = 64, y = 64 }) -- 64 by 64 endend)var images = Images.Instance;
window = Ui.Instance.AddWindow(new Window.Options { Title = "Logo", Icon = "image" }, () =>{ if (logo == null) return; images.Draw(logo, new ImVec2()); // its own size images.Draw(logo, new ImVec2 { X = 64, Y = 64 }); // 64 by 64});static Wraith::Image logo;
// in startauto window = wraith::api().ui().addWindow({.title = sal::String::of("Logo"), .icon = sal::String::of("image")}, sal::keep<Wraith::Window::Draw>([] { if (!logo) return; auto images = wraith::api().images(); images.draw(logo, {0, 0}); // its own size images.draw(logo, {64, 64}); // 64 by 64}));use std::sync::Mutex;
use salamander::{Str, TraitObject};use wraith::{ImVec2, Image, WindowDraw, WindowOptions};
static LOGO: Mutex<Option<Image>> = Mutex::new(None);
fn add_window() { let options = WindowOptions { title: Str::new("Logo"), icon: Str::new("image"), ..Default::default() }; wraith::api().ui().add_window(options, WindowDraw::keep(|| { let Some(logo) = *LOGO.lock().unwrap() else { return }; let images = wraith::api().images(); images.draw(logo, ImVec2 { x: 0.0, y: 0.0 }); // its own size images.draw(logo, ImVec2 { x: 64.0, y: 64.0 }); // 64 by 64 }));}ctx.wraith.images().draw(image, size) works as in other languages. The SDK’s sdk.images.draw is a Zig helper that takes options instead of a size. With one side of .size 0 it keeps its shape, .fit = .contain fits it inside .size, .fit = .width fills the window across, and .tint multiplies its colors.
fn drawLogo(self: *Plugin) void { const logo = self.logo orelse return; sdk.images.draw(logo, .{}); // its own size sdk.images.draw(logo, .{ .fit = .width }); // as wide as the window, same shape sdk.images.draw(logo, .{ .size = .{ 64, 64 }, .fit = .contain }); // inside 64 by 64}static Wraith_Image logo;
static void draw(uintptr_t self) { (void)self; if (!logo.table) return; Wraith_Images images = Wraith_Api_images(w); Wraith_Images_draw(images, logo, (Wraith_ImVec2){ 0, 0 }); /* its own size */ Wraith_Images_draw(images, logo, (Wraith_ImVec2){ 64, 64 }); /* 64 by 64 */}
static const Wraith_Window_Draw_table draw_table = { SAL_TABLE(Wraith_Window_Draw), .call = draw };
/* in start */Wraith_Window_Options options = { .title = SAL_STR("Logo"), .icon = SAL_STR("image") };Wraith_Window window = Wraith_Ui_addWindow(Wraith_Api_ui(w), options, (Wraith_Window_Draw){ 0, &draw_table });For anything else, an image’s texture() gives ImGui’s ImTextureRef, for calls like imgui.ImageButton or a draw list’s AddImage. It’s only good for that frame.
Let it go
Section titled “Let it go”destroy() frees it now, and it leaves the GPU over the next frames. Calling it twice is fine. Whatever you don’t destroy goes when your plugin stops. In a tweak, images go when the tweak turns off.
logo.destroy();logo:destroy()logo.Destroy();static Wraith::Image logo;
// in startlogo.destroy();use wraith::Image;
fn done(logo: Image) { logo.destroy();}logo.destroy();static Wraith_Image logo;
/* in start */Wraith_Image_destroy(logo);Pixels and captures
Section titled “Pixels and captures”display.capture(point) copies the next frame, before_overlay or after_overlay, as a Capture with its width, height, pitch, format and pixels. It needs "capture": true in your permissions. images.create makes an image from pixels you’ve made. Encoding PNGs and tone-mapping HDR are Zig only so far.
import { Wraith } from "wraith";
const shot = await Wraith.display().capture("after_overlay");// shot.width, shot.height, shot.pitch, shot.format and shot.pixelslocal Wraith = require("wraith").Wraith
task.spawn(function() local shot = Wraith.display():capture("after_overlay"):await() -- shot.width, shot.height, shot.pitch, shot.format and shot.pixelsend)var shot = await Display.Instance.CaptureAsync(Capture.Point.AfterOverlay);// shot.Width, shot.Height, shot.Pitch, shot.Format and shot.Pixelsstatic sal::Future<void> screenshot() { auto shot = co_await wraith::api().display().capture(Wraith::Capture::Point::after_overlay); // shot->width, shot->height, shot->pitch, shot->format and shot->pixels co_return {};}use wraith::{CaptureError, CapturePoint};
fn screenshot() { let _ = salamander::spawn(async { let shot = wraith::api().display().capture(CapturePoint::AFTER_OVERLAY).await?; // shot.width, shot.height, shot.pitch, shot.format and shot.pixels Ok::<(), CaptureError>(()) });}sdk.images has decodePixels, encodePng, and fromCapture, which turns a frame from ctx.wraith.display().capture into SDR pixels, tone-mapping HDR with an exposure in stops. linearFromCapture stops before that, with linear light you can write out as HDR with encodeExr. Wraith’s own screenshots use these for their PNGs and EXRs. All of it’s slow, so keep it on a worker.
sal_Promise shot = Wraith_Display_capture(Wraith_Api_display(w), Wraith_Capture_Point_after_overlay);/* Once it's settled, sal_Promise_take gives a Wraith_Capture: its width, height, pitch, format and pixels */wraith.io · Mods on the Hub · Get Wraith
Made with ♥ by Asgard
