You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
pubstructWindowRenderer<'a>{canvas:Canvas<Window>,events:EventPump,texture_creator:TextureCreator<WindowContext>,board_texture:Texture<'a>,}impl<'a>WindowRenderer<'a>{pubfnnew() -> 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,}}}implIOforWindowRenderer<'_>{// 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?
The text was updated successfully, but these errors were encountered:
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.
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:
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?
The text was updated successfully, but these errors were encountered: