1
2
3
4
5
6
7
8
9
10
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
|
extern crate sfml;
use std::collections::HashMap;
use sfml::window::{
ContextSettings,
Event,
Style,
};
use sfml::graphics::{
RenderWindow,
RenderTarget,
Texture,
Color,
Rect,
};
#[derive(RustEmbed)]
#[folder = "res"]
pub struct Assets;
pub fn start() -> RenderWindow {
let default_window_size = (1280, 720);
let default_framerate = 80;
let context_settings = ContextSettings {
antialiasing_level: 0,
..Default::default()
};
let mut window = RenderWindow::new(
default_window_size,
"Subconscious",
Style::CLOSE,
&context_settings
);
window.set_framerate_limit(default_framerate);
window.set_vertical_sync_enabled(true);
return window;
}
pub fn render(window: &mut RenderWindow, state: &crate::game::State) {
// background
let bgcolor = match state.map.background_colour {
Some(color) => Color::rgb(color.red, color.green, color.blue),
None => Color::BLACK,
};
window.clear(&bgcolor);
// load tilesets
let mut textures: HashMap<u32, Texture> = HashMap::new();
for tileset in &state.map.tilesets {
let image = &tileset.images[0];
let asset = Assets::get(&image.source);
if let Some(asset) = asset {
let texture = Texture::from_memory(
asset.as_ref(),
&Rect {
left: 0,
top: 0,
width: image.width,
height: image.height
}
);
if let Some(texture) = texture {
textures.insert(tileset.first_gid, texture);
}
}
}
// render map
// for each tile
for y in 0 .. state.map.height {
for x in 0 .. state.map.width {
// for each layer
for layer in &state.map.layers {
state.map.get_tileset_by_gid(layer.tiles[y as usize][x as usize]);
}
}
}
window.display();
}
pub fn update(window: &mut RenderWindow) {
while let Some(ev) = window.poll_event() {
match ev {
Event::Closed => window.close(),
_ => {},
}
}
}
|