Struct nze_game_sdl::Render

source ·
pub struct Render<'sdl> {
    pub texture_manager: TextureManager<'sdl, WindowContext>,
    pub font_manager: FontManager<'sdl, WindowContext>,
    /* private fields */
}
Expand description

Holds ownership of a DrawingArea and texture and font managers, created using a ContextSdl

Fields§

§texture_manager: TextureManager<'sdl, WindowContext>§font_manager: FontManager<'sdl, WindowContext>

Implementations§

source§

impl<'sdl> Render<'sdl>

source

pub fn new( drawing_area: DrawingArea, context: &ContextSdl ) -> Result<Render<'_>, Error>

Examples found in repository?
examples/tiledmap.rs (line 20)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
pub fn main() -> Result<(), Error> {
    let (mut cam, drawing_area, context) = DrawingArea::new(
        "Tiled Map Example",
        Rect::new(0.0, 0.0, 240.0, 180.0),
        Vec2::new(480.0, 360.0)
    )?;
    let mut render = Render::new(drawing_area, &context)?;
    let mut controls = Controls::new(&context)?;
    let mut game = Game::new(&mut render)?;

    while !controls.should_close {
        controls.update(&cam);
        game.update(&mut controls);
        render.start_draw();
        game.draw(&mut cam);
        render.end_draw(&mut cam)?;
    }
    
    Ok(())
}
More examples
Hide additional examples
examples/simple.rs (line 17)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
pub fn main() -> Result<(), Error> {
    let (mut cam, drawing_area, context) = DrawingArea::new(
        "Name of Game",                              // window name
        Rect::new(0.0, 0.0, 400.0, 400.0), // window camera
        Vec2::new(400.0, 400.0)            // window size
    )?;
    let mut render = Render::new(drawing_area, &context)?;
    let mut controls = Controls::new(&context)?;
    
    let mut obj = GameObject::new_from_tex(
        render.texture_manager.load(Path::new("resources/textures/gaia.png"))?
    );
    
    obj.rect.x = cam.get_view_size().x / 2.0 - obj.rect.w / 2.0;
    obj.rect.y = cam.get_view_size().y / 2.0 - obj.rect.h / 2.0;

    const SPEED: f64 = 125.0;
    
    while !controls.should_close {
        controls.update(&cam);
        
        if controls.kb.down(Key::D) {
            obj.rect.x += SPEED * controls.frame_elapsed;
        }

        if controls.kb.down(Key::A) {
            obj.rect.x -= SPEED * controls.frame_elapsed;
        }

        if controls.kb.down(Key::W) {
            obj.rect.y -= SPEED * controls.frame_elapsed;
        }
        
        if controls.kb.down(Key::S) {
            obj.rect.y += SPEED * controls.frame_elapsed;
        }

        if controls.kb.down(Key::Escape) {
            controls.should_close = true;
        }
        
        render.start_draw();
        cam.draw(&obj);
        render.end_draw(&mut cam)?;
    }
    Ok(())
}
examples/main.rs (line 18)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
pub fn main() -> Result<(), Error> {
    
    let (mut cam, drawing_area, context) = DrawingArea::new(
        "Game Template", //window name
        Rect::new(0.0, 0.0, 240.0, 160.0),
        Vec2::new(240.0 * 4.0, 160.0 * 4.0)
    )?;
    let mut render = Render::new(drawing_area, &context)?;
    let mut controls = Controls::new(&context)?;
   
    let mono_font = render.font_manager.load_font(
        Path::new("resources/textures/fonts/FiraCode-Light.ttf"))?;
    
    let map = Map::new(
        Path::new("resources/map/test.tmx"), &mut render.texture_manager,
        Path::new("resources/textures/fonts"), &mut render.font_manager)?;

    let mut audio = AudioManager::new()?;

    let music = audio.music.load(Path::new("resources/audio/test.wav"))?;
    audio.music.play(music, -1)?;

    let sfx = audio.sfx.load(Path::new("resources/audio/test.mp3"))?;
    audio.sfx.set_volume(sfx, 0.4)?;
    
    //checking resource loading/unloading
    let mut is_gaia = true;
    let mut ephemeral_obj = GameObject::new_from_tex(
        render.texture_manager.load(
            Path::new("resources/textures/gaia.png"))?);

    let mut text = render
        .font_manager
        .load_text_obj(
            &mono_font,
            "The Planet Earth",
            Colour::new(100, 200, 70, 255),
            Vec2::new(0.0, 0.0), 10.0,
            Vec2::new(0.0, 0.0)
        )?;

    while !controls.should_close {
        cam_controls(&mut render, &mut controls, &mut cam)?;
        controls.update(&cam);
        
        // load/unload resources
        if controls.kb.press(Key::L) ||
            controls.c.press(0, controller::Button::A)
        {
            render.texture_manager.unload_from_gameobject(ephemeral_obj);
            render.font_manager.unload_text_obj(text);
            if is_gaia {
                ephemeral_obj = GameObject::new_from_tex(
                    render.texture_manager.load(
                        Path::new("resources/textures/error.png"))?);
                text = render.font_manager.load_text_obj(
                    &mono_font,
                    "Error Text",
                    Colour::new(200, 100, 70, 255),
                    Vec2::new(100.0, 0.0),
                    10.0,
                    Vec2::new(0.0, 0.0))?;
                text.parallax = Vec2::new(1.0, 1.0);
            } else {
                ephemeral_obj = GameObject::new_from_tex(
                    render.texture_manager.load(
                        Path::new("resources/textures/gaia.png"))?);
                text = render.font_manager.load_text_obj(
                    &mono_font,
                    "The Planet Earth",
                    Colour::new(100, 200, 70, 255),
                    Vec2::new(0.0, 0.0),
                    10.0,
                    Vec2::new(0.0, 0.0))?;
            }
            is_gaia = !is_gaia;
        }
        
        ephemeral_obj.rotate += controls.frame_elapsed * 30.0;
        
        if controls.kb.press(Key::F) {
            ephemeral_obj.flip_horizontal = !ephemeral_obj.flip_horizontal;
        }

        if controls.kb.press(Key::P) {
            audio.sfx.play(sfx)?;
        }
        
        if controls.c.press(0, controller::Button::DPadUp) {
            controls.c.rumble(0, 10000, 20000, 1000);
        }
        
        render.start_draw();
        
        map.draw(&mut cam);
        cam.draw(&ephemeral_obj);
        cam.draw_text(&text);
        cam.draw_disposable_text(
            &mono_font,
            format!("Wheel: {}", controls.m.wheel()),
            40,
            controls.m.pos(),
            Colour::white(),
            Vec2::new(1.0, 1.0));
        render.end_draw(&mut cam)?;
    }

    Ok(())
}
source

pub fn start_draw(&mut self)

Clears the DrawingArea for it to be filled with this frame’s drawing instructions

Examples found in repository?
examples/tiledmap.rs (line 27)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
pub fn main() -> Result<(), Error> {
    let (mut cam, drawing_area, context) = DrawingArea::new(
        "Tiled Map Example",
        Rect::new(0.0, 0.0, 240.0, 180.0),
        Vec2::new(480.0, 360.0)
    )?;
    let mut render = Render::new(drawing_area, &context)?;
    let mut controls = Controls::new(&context)?;
    let mut game = Game::new(&mut render)?;

    while !controls.should_close {
        controls.update(&cam);
        game.update(&mut controls);
        render.start_draw();
        game.draw(&mut cam);
        render.end_draw(&mut cam)?;
    }
    
    Ok(())
}
More examples
Hide additional examples
examples/simple.rs (line 52)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
pub fn main() -> Result<(), Error> {
    let (mut cam, drawing_area, context) = DrawingArea::new(
        "Name of Game",                              // window name
        Rect::new(0.0, 0.0, 400.0, 400.0), // window camera
        Vec2::new(400.0, 400.0)            // window size
    )?;
    let mut render = Render::new(drawing_area, &context)?;
    let mut controls = Controls::new(&context)?;
    
    let mut obj = GameObject::new_from_tex(
        render.texture_manager.load(Path::new("resources/textures/gaia.png"))?
    );
    
    obj.rect.x = cam.get_view_size().x / 2.0 - obj.rect.w / 2.0;
    obj.rect.y = cam.get_view_size().y / 2.0 - obj.rect.h / 2.0;

    const SPEED: f64 = 125.0;
    
    while !controls.should_close {
        controls.update(&cam);
        
        if controls.kb.down(Key::D) {
            obj.rect.x += SPEED * controls.frame_elapsed;
        }

        if controls.kb.down(Key::A) {
            obj.rect.x -= SPEED * controls.frame_elapsed;
        }

        if controls.kb.down(Key::W) {
            obj.rect.y -= SPEED * controls.frame_elapsed;
        }
        
        if controls.kb.down(Key::S) {
            obj.rect.y += SPEED * controls.frame_elapsed;
        }

        if controls.kb.down(Key::Escape) {
            controls.should_close = true;
        }
        
        render.start_draw();
        cam.draw(&obj);
        render.end_draw(&mut cam)?;
    }
    Ok(())
}
examples/main.rs (line 103)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
pub fn main() -> Result<(), Error> {
    
    let (mut cam, drawing_area, context) = DrawingArea::new(
        "Game Template", //window name
        Rect::new(0.0, 0.0, 240.0, 160.0),
        Vec2::new(240.0 * 4.0, 160.0 * 4.0)
    )?;
    let mut render = Render::new(drawing_area, &context)?;
    let mut controls = Controls::new(&context)?;
   
    let mono_font = render.font_manager.load_font(
        Path::new("resources/textures/fonts/FiraCode-Light.ttf"))?;
    
    let map = Map::new(
        Path::new("resources/map/test.tmx"), &mut render.texture_manager,
        Path::new("resources/textures/fonts"), &mut render.font_manager)?;

    let mut audio = AudioManager::new()?;

    let music = audio.music.load(Path::new("resources/audio/test.wav"))?;
    audio.music.play(music, -1)?;

    let sfx = audio.sfx.load(Path::new("resources/audio/test.mp3"))?;
    audio.sfx.set_volume(sfx, 0.4)?;
    
    //checking resource loading/unloading
    let mut is_gaia = true;
    let mut ephemeral_obj = GameObject::new_from_tex(
        render.texture_manager.load(
            Path::new("resources/textures/gaia.png"))?);

    let mut text = render
        .font_manager
        .load_text_obj(
            &mono_font,
            "The Planet Earth",
            Colour::new(100, 200, 70, 255),
            Vec2::new(0.0, 0.0), 10.0,
            Vec2::new(0.0, 0.0)
        )?;

    while !controls.should_close {
        cam_controls(&mut render, &mut controls, &mut cam)?;
        controls.update(&cam);
        
        // load/unload resources
        if controls.kb.press(Key::L) ||
            controls.c.press(0, controller::Button::A)
        {
            render.texture_manager.unload_from_gameobject(ephemeral_obj);
            render.font_manager.unload_text_obj(text);
            if is_gaia {
                ephemeral_obj = GameObject::new_from_tex(
                    render.texture_manager.load(
                        Path::new("resources/textures/error.png"))?);
                text = render.font_manager.load_text_obj(
                    &mono_font,
                    "Error Text",
                    Colour::new(200, 100, 70, 255),
                    Vec2::new(100.0, 0.0),
                    10.0,
                    Vec2::new(0.0, 0.0))?;
                text.parallax = Vec2::new(1.0, 1.0);
            } else {
                ephemeral_obj = GameObject::new_from_tex(
                    render.texture_manager.load(
                        Path::new("resources/textures/gaia.png"))?);
                text = render.font_manager.load_text_obj(
                    &mono_font,
                    "The Planet Earth",
                    Colour::new(100, 200, 70, 255),
                    Vec2::new(0.0, 0.0),
                    10.0,
                    Vec2::new(0.0, 0.0))?;
            }
            is_gaia = !is_gaia;
        }
        
        ephemeral_obj.rotate += controls.frame_elapsed * 30.0;
        
        if controls.kb.press(Key::F) {
            ephemeral_obj.flip_horizontal = !ephemeral_obj.flip_horizontal;
        }

        if controls.kb.press(Key::P) {
            audio.sfx.play(sfx)?;
        }
        
        if controls.c.press(0, controller::Button::DPadUp) {
            controls.c.rumble(0, 10000, 20000, 1000);
        }
        
        render.start_draw();
        
        map.draw(&mut cam);
        cam.draw(&ephemeral_obj);
        cam.draw_text(&text);
        cam.draw_disposable_text(
            &mono_font,
            format!("Wheel: {}", controls.m.wheel()),
            40,
            controls.m.pos(),
            Colour::white(),
            Vec2::new(1.0, 1.0));
        render.end_draw(&mut cam)?;
    }

    Ok(())
}
source

pub fn end_draw(&mut self, cam: &mut Camera) -> Result<(), Error>

Drain the draws from Camera and draws to the canvas held by DrawingArea

This is when the sdl drawing commands actually occur

Examples found in repository?
examples/tiledmap.rs (line 29)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
pub fn main() -> Result<(), Error> {
    let (mut cam, drawing_area, context) = DrawingArea::new(
        "Tiled Map Example",
        Rect::new(0.0, 0.0, 240.0, 180.0),
        Vec2::new(480.0, 360.0)
    )?;
    let mut render = Render::new(drawing_area, &context)?;
    let mut controls = Controls::new(&context)?;
    let mut game = Game::new(&mut render)?;

    while !controls.should_close {
        controls.update(&cam);
        game.update(&mut controls);
        render.start_draw();
        game.draw(&mut cam);
        render.end_draw(&mut cam)?;
    }
    
    Ok(())
}
More examples
Hide additional examples
examples/simple.rs (line 54)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
pub fn main() -> Result<(), Error> {
    let (mut cam, drawing_area, context) = DrawingArea::new(
        "Name of Game",                              // window name
        Rect::new(0.0, 0.0, 400.0, 400.0), // window camera
        Vec2::new(400.0, 400.0)            // window size
    )?;
    let mut render = Render::new(drawing_area, &context)?;
    let mut controls = Controls::new(&context)?;
    
    let mut obj = GameObject::new_from_tex(
        render.texture_manager.load(Path::new("resources/textures/gaia.png"))?
    );
    
    obj.rect.x = cam.get_view_size().x / 2.0 - obj.rect.w / 2.0;
    obj.rect.y = cam.get_view_size().y / 2.0 - obj.rect.h / 2.0;

    const SPEED: f64 = 125.0;
    
    while !controls.should_close {
        controls.update(&cam);
        
        if controls.kb.down(Key::D) {
            obj.rect.x += SPEED * controls.frame_elapsed;
        }

        if controls.kb.down(Key::A) {
            obj.rect.x -= SPEED * controls.frame_elapsed;
        }

        if controls.kb.down(Key::W) {
            obj.rect.y -= SPEED * controls.frame_elapsed;
        }
        
        if controls.kb.down(Key::S) {
            obj.rect.y += SPEED * controls.frame_elapsed;
        }

        if controls.kb.down(Key::Escape) {
            controls.should_close = true;
        }
        
        render.start_draw();
        cam.draw(&obj);
        render.end_draw(&mut cam)?;
    }
    Ok(())
}
examples/main.rs (line 115)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
pub fn main() -> Result<(), Error> {
    
    let (mut cam, drawing_area, context) = DrawingArea::new(
        "Game Template", //window name
        Rect::new(0.0, 0.0, 240.0, 160.0),
        Vec2::new(240.0 * 4.0, 160.0 * 4.0)
    )?;
    let mut render = Render::new(drawing_area, &context)?;
    let mut controls = Controls::new(&context)?;
   
    let mono_font = render.font_manager.load_font(
        Path::new("resources/textures/fonts/FiraCode-Light.ttf"))?;
    
    let map = Map::new(
        Path::new("resources/map/test.tmx"), &mut render.texture_manager,
        Path::new("resources/textures/fonts"), &mut render.font_manager)?;

    let mut audio = AudioManager::new()?;

    let music = audio.music.load(Path::new("resources/audio/test.wav"))?;
    audio.music.play(music, -1)?;

    let sfx = audio.sfx.load(Path::new("resources/audio/test.mp3"))?;
    audio.sfx.set_volume(sfx, 0.4)?;
    
    //checking resource loading/unloading
    let mut is_gaia = true;
    let mut ephemeral_obj = GameObject::new_from_tex(
        render.texture_manager.load(
            Path::new("resources/textures/gaia.png"))?);

    let mut text = render
        .font_manager
        .load_text_obj(
            &mono_font,
            "The Planet Earth",
            Colour::new(100, 200, 70, 255),
            Vec2::new(0.0, 0.0), 10.0,
            Vec2::new(0.0, 0.0)
        )?;

    while !controls.should_close {
        cam_controls(&mut render, &mut controls, &mut cam)?;
        controls.update(&cam);
        
        // load/unload resources
        if controls.kb.press(Key::L) ||
            controls.c.press(0, controller::Button::A)
        {
            render.texture_manager.unload_from_gameobject(ephemeral_obj);
            render.font_manager.unload_text_obj(text);
            if is_gaia {
                ephemeral_obj = GameObject::new_from_tex(
                    render.texture_manager.load(
                        Path::new("resources/textures/error.png"))?);
                text = render.font_manager.load_text_obj(
                    &mono_font,
                    "Error Text",
                    Colour::new(200, 100, 70, 255),
                    Vec2::new(100.0, 0.0),
                    10.0,
                    Vec2::new(0.0, 0.0))?;
                text.parallax = Vec2::new(1.0, 1.0);
            } else {
                ephemeral_obj = GameObject::new_from_tex(
                    render.texture_manager.load(
                        Path::new("resources/textures/gaia.png"))?);
                text = render.font_manager.load_text_obj(
                    &mono_font,
                    "The Planet Earth",
                    Colour::new(100, 200, 70, 255),
                    Vec2::new(0.0, 0.0),
                    10.0,
                    Vec2::new(0.0, 0.0))?;
            }
            is_gaia = !is_gaia;
        }
        
        ephemeral_obj.rotate += controls.frame_elapsed * 30.0;
        
        if controls.kb.press(Key::F) {
            ephemeral_obj.flip_horizontal = !ephemeral_obj.flip_horizontal;
        }

        if controls.kb.press(Key::P) {
            audio.sfx.play(sfx)?;
        }
        
        if controls.c.press(0, controller::Button::DPadUp) {
            controls.c.rumble(0, 10000, 20000, 1000);
        }
        
        render.start_draw();
        
        map.draw(&mut cam);
        cam.draw(&ephemeral_obj);
        cam.draw_text(&text);
        cam.draw_disposable_text(
            &mono_font,
            format!("Wheel: {}", controls.m.wheel()),
            40,
            controls.m.pos(),
            Colour::white(),
            Vec2::new(1.0, 1.0));
        render.end_draw(&mut cam)?;
    }

    Ok(())
}
source

pub fn set_win_size(&mut self, cam: &mut Camera, cs: Vec2) -> Result<(), Error>

Update the game window to the new size, and change the Camera to the new resolution

Changes the resolution of the Sdl Canvas and centeres the window

Examples found in repository?
examples/main.rs (line 195)
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
fn cam_controls(render: &mut Render, controls: &mut Controls, cam: &mut Camera) -> Result<(), Error> {
    let prev_frame = controls.frame_elapsed;
    let mut pos = cam.get_offset();
    const SPEED : f64 = 500.0;
    if controls.kb.down(Key::Left) {
        pos.x -= SPEED * prev_frame;
    }
    if controls.kb.down(Key::Right) {
        pos.x += SPEED * prev_frame;
    }
    if controls.kb.down(Key::Up) {
        pos.y -= SPEED * prev_frame;
    }
    if controls.kb.down(Key::Down) {
        pos.y += SPEED * prev_frame;
    }

    pos.x += SPEED * controls.m.wheel() as f64 * prev_frame;

    let v =  controls.c.joy(0, controller::Side::Left) * SPEED * prev_frame;
    pos = pos + v;
    
    cam.set_offset(pos);
    let mut win_size_update = false;
    let mut cs = cam.get_window_size();
    
    if controls.kb.down(Key::Equals) {
        if cs.x < cam.get_view_size().x {
            cs.x *= 2.0;
            cs.y *= 2.0;
        } else {
            cs.x += cam.get_view_size().x/2.0;
            cs.y += cam.get_view_size().y/2.0;
        }
        win_size_update = true;
    }
    if controls.kb.down(Key::Minus) {
        if cs.x <= cam.get_view_size().x {
            cs.x /= 2.0;
            cs.y /= 2.0;
        } else {
            cs.x -= cam.get_view_size().x/2.0;
            cs.y -= cam.get_view_size().y/2.0;
        }
        win_size_update = true;
    }

    if controls.kb.down(Key::D) {
        cs.x += SPEED * prev_frame;
        win_size_update = true;
    }
    if controls.kb.down(Key::A) {
        
        cs.x -= SPEED * prev_frame;
        win_size_update = true;
    }
    if controls.kb.down(Key::W) {
        
        cs.y += SPEED * prev_frame;
        win_size_update = true;
    }

    if controls.kb.down(Key::S) {
        
        cs.y -= SPEED * prev_frame;
        win_size_update = true;
    }

    if controls.kb.press(Key::Num1) {
        render.toggle_fullscreen(cam)?;
    }
    
    if win_size_update {
        render.set_win_size(cam, cs)?;
    }

    if controls.kb.down(Key::Escape) {
        controls.should_close = true;
    }
    
    Ok(())
}
source

pub fn set_fullscreen( &mut self, cam: &mut Camera, fullscreen: bool ) -> Result<(), Error>

Set whether the window shoudl take up the full screen or not

Fullscreen is really windowed borderless, taking up the whole monitor

source

pub fn get_fullscreen(&self) -> bool

source

pub fn toggle_fullscreen(&mut self, cam: &mut Camera) -> Result<bool, Error>

Toggle from the current fullscreen state, to the opposite, and returns the new value.

Examples found in repository?
examples/main.rs (line 191)
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
fn cam_controls(render: &mut Render, controls: &mut Controls, cam: &mut Camera) -> Result<(), Error> {
    let prev_frame = controls.frame_elapsed;
    let mut pos = cam.get_offset();
    const SPEED : f64 = 500.0;
    if controls.kb.down(Key::Left) {
        pos.x -= SPEED * prev_frame;
    }
    if controls.kb.down(Key::Right) {
        pos.x += SPEED * prev_frame;
    }
    if controls.kb.down(Key::Up) {
        pos.y -= SPEED * prev_frame;
    }
    if controls.kb.down(Key::Down) {
        pos.y += SPEED * prev_frame;
    }

    pos.x += SPEED * controls.m.wheel() as f64 * prev_frame;

    let v =  controls.c.joy(0, controller::Side::Left) * SPEED * prev_frame;
    pos = pos + v;
    
    cam.set_offset(pos);
    let mut win_size_update = false;
    let mut cs = cam.get_window_size();
    
    if controls.kb.down(Key::Equals) {
        if cs.x < cam.get_view_size().x {
            cs.x *= 2.0;
            cs.y *= 2.0;
        } else {
            cs.x += cam.get_view_size().x/2.0;
            cs.y += cam.get_view_size().y/2.0;
        }
        win_size_update = true;
    }
    if controls.kb.down(Key::Minus) {
        if cs.x <= cam.get_view_size().x {
            cs.x /= 2.0;
            cs.y /= 2.0;
        } else {
            cs.x -= cam.get_view_size().x/2.0;
            cs.y -= cam.get_view_size().y/2.0;
        }
        win_size_update = true;
    }

    if controls.kb.down(Key::D) {
        cs.x += SPEED * prev_frame;
        win_size_update = true;
    }
    if controls.kb.down(Key::A) {
        
        cs.x -= SPEED * prev_frame;
        win_size_update = true;
    }
    if controls.kb.down(Key::W) {
        
        cs.y += SPEED * prev_frame;
        win_size_update = true;
    }

    if controls.kb.down(Key::S) {
        
        cs.y -= SPEED * prev_frame;
        win_size_update = true;
    }

    if controls.kb.press(Key::Num1) {
        render.toggle_fullscreen(cam)?;
    }
    
    if win_size_update {
        render.set_win_size(cam, cs)?;
    }

    if controls.kb.down(Key::Escape) {
        controls.should_close = true;
    }
    
    Ok(())
}

Auto Trait Implementations§

§

impl<'sdl> RefUnwindSafe for Render<'sdl>

§

impl<'sdl> !Send for Render<'sdl>

§

impl<'sdl> !Sync for Render<'sdl>

§

impl<'sdl> Unpin for Render<'sdl>

§

impl<'sdl> UnwindSafe for Render<'sdl>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.