Skip to content

Question multiple textures and alpha channel #750

@Schr3da

Description

@Schr3da

Hi,

Im currently workin on a game in web assembly by using rust and sdl.
The game consists of several textures/layers and I would like to draw one layer over the other using
alpha value. Its kind of a strategy game so I try to implement kind of a darker layer on top of my map with some "light radius". However Im facing some sisues which I dont know how to resolve properly.

Thats my current main render loop and Im exclusivly using features of the crates.io documentation, with no features at all.

Quesiton 1: Is this a proper way how to use the api to archieve multiple layers?
Question 2: How can I set a transparent area for the clear color (currently my single render_light method is just drawing over the map without alpha values even when I use BlendMode::Blend before I start clearing/drawing the texture

pub fn run() {
    let sdl_context = sdl2::init().unwrap();
    let font_context = sdl2::ttf::init().unwrap();

    let mut keyboard_controlls = KeyboardControls::new(&sdl_context);
    let screen_rect = window::get_canvas_frame();

    let mut font_manager = FontManager::new(&font_context);
    load_all_fonts(&mut font_manager);

    let mut canvas = get_canvas(sdl_context);
    let texture_creator = canvas.texture_creator();

    let mut game = Game::new();
    game.init();

    let (width, height) = game.get_map_size();
    let mut map_rect = get_map_frame(width, height);
    let mut map_texture = create_map_texture(&texture_creator, width, height).unwrap();
    canvas.with_texture_canvas(&mut map_texture, |map_texture| render_map(&game, map_texture)).unwrap();

    let mut light_texture = create_light_texture(&texture_creator).unwrap();
    canvas.with_texture_canvas(&mut light_texture, |light_texture| render_light(&game, light_texture)).unwrap();

    let layers = vec![
        (&mut map_texture, layers::MAP),
        (&mut light_texture, layers::LIGHT)
    ];

    renderer(|is_init_done| {

        if is_init_done && !update(&mut keyboard_controlls) {
            thread::sleep(window::get_fps());
            return true;
        }

        canvas.clear();
        
        canvas.with_multiple_texture_canvas(layers.iter(), |texture, name| {
            match *name {
                layers::LIGHT => render_light(&game, texture),
                _ => {}
            }
        }).unwrap();

        for &(ref layer, name) in layers.iter(){
            match name {
                layers::LIGHT => canvas.copy(layer, None, Rect::new(0, 0, 100, 100)
                ).unwrap(),
                layers::MAP => canvas.copy(layer,
                    input::utils::get_render_rect_for_keyboard(&keyboard_controlls, &mut map_rect,  &screen_rect),
                    window::get_canvas_frame(),
                ).unwrap(),
                _ => {}
            }
        }

        canvas.present();
        thread::sleep(window::get_fps());
        return true;

Thanks,

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions