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

Question: Texture lifetimes #1361

Closed
mileswijeyeratne opened this issue Jan 7, 2024 · 1 comment
Closed

Question: Texture lifetimes #1361

mileswijeyeratne opened this issue Jan 7, 2024 · 1 comment

Comments

@mileswijeyeratne
Copy link

mileswijeyeratne commented Jan 7, 2024

EDIT: I switched to raylib as this wasn't working. Still wondering if there is a way to do this though?

I am having some trouble with the Texture struct's lifetimes in my game of chess. Sorry in advance if there is anything I have overlooked - I am very new to rust.

My code has a moudle to handle all IO elements which currently consists of a IO trait and a implementation for a terminal but I want to implement a SDL2 window to do the same thing.

The issue is that I want to contain all the SDL2 inside 1 file that consists of a Window struct to implement IO. This means that I run into some trouble when trying to create textures as the texture creator goes out of scope after the struct is initalised. I have tried lots of different approaches such as using Rc or storing the texure_creator in the struct and having an init() function to create the textures but nothing seems to work. Here is my current code:

pub struct WindowRenderer<'a> {
    canvas: Canvas<Window>,
    events: EventPump,
    texture_creator: TextureCreator<WindowContext>, 
    board_texture: Texture<'a>,
}

impl<'a> WindowRenderer<'a> {
    pub fn new() -> Self {
        let sdl = sdl2::init().unwrap();
        let _sdl_img = sdl2::image::init(InitFlag::PNG);
        let video = sdl.video().unwrap();
        let window = video
            .window("Chess", 800, 800)
            .position_centered()
            .build()
            .unwrap();
        let canvas = window.into_canvas().build().unwrap();

        let events = sdl.event_pump().unwrap();

        let texture_creator = canvas.texture_creator();
        let board_texture = texture_creator.load_texture("../assets/board.png").unwrap();

        // Gives the error:
        // cannot return value referencing local variable `texture_creator`
        Self {
            canvas,
            events,
            texture_creator,
            board_texture,
        }
    }
}

impl IO for WindowRenderer<'_> {
    // IO impl
}

I am wondering if there is even a solution. I did some looking and found this issue which seems to say that what I am trying to do is not going to work due to the SDL2 crate structure and I would have to create the texture_creator in my main.rs. I would like to keep the SDL2 in ints own file to keep the codebase clean in a way that would mean that I can switch between rendering through a cli and SDL2 window easily. Is there any way to do this?

@Cobrand
Copy link
Member

Cobrand commented Jan 12, 2024

There is a feature called unsafe-textures which removes the lifetime on textures, but the tradeoff is that by default are only "freed" when the whole TextureCreator is dropped, unless you use unsafe.

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

No branches or pull requests

2 participants