Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to put MTLTexture into ImGuiImage from dictionary? #25

Open
tanelxen opened this issue May 27, 2024 · 1 comment
Open

How to put MTLTexture into ImGuiImage from dictionary? #25

tanelxen opened this issue May 27, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@tanelxen
Copy link

tanelxen commented May 27, 2024

That question mainly about pointers management in Swift than SwiftImGui as is.

I'm trying to add thumbnail in asset browser, but get bad access because pointer to texture isn't valid at the moment when ImGuiImage have to be rendered.

I hold textures for thumbnails in dictionary and render it like this:

var textures: [String: MTLTexture]
....
let ptr = withUnsafePointer(to: &textures[name]) { ptr in
    return UnsafeMutableRawPointer(mutating: ptr)
}

ImGuiImage(
    ptr,
    ImVec2(24, 24),
    ImVec2(0, 0),
    ImVec2(1, 1),
    ImVec4(1, 1, 1, 1),
    ImVec4(1, 1, 1, 1)
)

It would be nice to use ids instead of pointers.

@tanelxen tanelxen added the enhancement New feature or request label May 27, 2024
@tanelxen
Copy link
Author

Well, I've managed it with proxy-class:

final class TextureProxy {
    let name: String
    var texture: MTLTexture?
    
    var ptr: UnsafeMutableRawPointer {
        withUnsafePointer(to: &texture) { ptr in
            return UnsafeMutableRawPointer(mutating: ptr)
        }
    }

    init(name: String) {
        self.name = name
    }
}

It makes us to be sure that the pointer will always be same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant