summaryrefslogtreecommitdiffstats
path: root/src/graphics.rs
blob: 18ccfec00d24bfd708bcc7d7ee2c2cda47142ce3 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use std::collections::HashMap;

use sfml::system::Vector2f;
use sfml::window::mouse::Button;

use sfml::window::{
	ContextSettings,
	Event,
	Style,
};

use sfml::graphics::{
	RenderWindow,
	RenderTarget,
	View,
};

use sfml::graphics::{
	Texture,
	Image,
	Color,
	Rect,
};

use sfml::graphics::{
	RectangleShape,
	Transformable,
};


#[derive(RustEmbed)]
#[folder = "res/maps"]
pub struct MapAssets;

#[derive(Debug)]
pub struct Graphics {
	// sfml elements
	window: RenderWindow,
	view: View,
	// status
	running: bool,
	// loaded resources
	tilesets: HashMap<u32, Image>,
	textures: HashMap<u32, Texture>,
}

impl Graphics {
	pub fn new() -> Graphics {
		let default_window_size = (1280, 720);
		let default_framerate = 80;

		let context_settings = ContextSettings {
			antialiasing_level: 0,
			..Default::default()
		};

		// create window
		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);

		let mut graphics = Graphics {
			// sfml elements
			window: window,
			view: View::new(
				Vector2f::new(
					default_window_size.0 as f32 / 2.0,
					default_window_size.1 as f32 / 2.0
				),
				Vector2f::new(
					default_window_size.0 as f32,
					default_window_size.1 as f32,
				),
			),
			// status
			running: true,
			// resources
			tilesets: HashMap::new(),
			textures: HashMap::new(),
		};

		graphics.window.set_view(&graphics.view);

		return graphics;
	}

	pub fn is_running(self: &mut Self) -> bool {
		return self.running;
	}

	pub fn render(self: &mut Self, 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,
		};

		self.window.clear(&bgcolor);

		// 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 {

					let tmx_tile = layer.tiles[y as usize][x as usize];

					// blank tile
					if tmx_tile <= 0 {
						continue;
					}

					let tmx_tileset = match state.map.get_tileset_by_gid(tmx_tile) {
						Some(tileset) => tileset,
						None => {
							panic!("Failed to get tileset with get_tileset_by_gid");
						}
					};

					// load tileset if not loaded yet
					// TODO: load in a separate thread?
					if !self.tilesets.contains_key(&tmx_tileset.first_gid) {
						// TODO: replace with iter or error message
						assert_eq!(tmx_tileset.images.len(), 1);

						// load tileset image
						let tmx_image = &tmx_tileset.images[0];
						let asset = match MapAssets::get(&tmx_image.source) {
							Some(asset) => asset,
							None => {
								panic!("Failed to get asset: {}", &tmx_image.source);
							}
						};

						let image = match Image::from_memory(asset.as_ref()) {
							Some(image) => image,
							None => {
								panic!("Failed load image from asset");
							}
						};

						// load tiles (textures)
						let tileset_width = tmx_image.width as u32 / tmx_tileset.tile_width;
						let tileset_height = tmx_image.height as u32 / tmx_tileset.tile_height;

						for ty in 0 .. tileset_height {
							for tx in 0 .. tileset_width {
								let texture = Texture::from_image_with_rect(
									&image, &Rect::new(
										(tx * tmx_tileset.tile_width) as i32,
										(ty * tmx_tileset.tile_height) as i32,
										tmx_tileset.tile_width as i32,
										tmx_tileset.tile_height as i32
									)
								).unwrap();

								// save tile texture
								self.textures.insert(
									tmx_tileset.first_gid + ty * tileset_width + tx,
									texture
								);
							}
						}

						// save tileset image
						self.tilesets.insert(tmx_tileset.first_gid, image);
					}

					// draw!
					// TODO: check map render order
					let texture = &self.textures.get(&tmx_tile).unwrap();
					let mut tile_rect = RectangleShape::with_texture(texture);

					tile_rect.set_size(Vector2f {
						x: tmx_tileset.tile_width as f32,
						y: tmx_tileset.tile_height as f32
					});

					tile_rect.set_position(Vector2f {
						x: (x * tmx_tileset.tile_width) as f32,
						y: (y * tmx_tileset.tile_height) as f32
					});

					self.window.draw(&tile_rect);
				}
			}
		}

		self.window.display();
	}

	pub fn update(self: &mut Self) {
		while let Some(ev) = self.window.poll_event() {
			match ev {
				Event::Closed => {
					self.window.close();
					self.running = false;
				},
				Event::MouseWheelScrolled { wheel: _, delta, x, y } => {

				},
				Event::MouseButtonPressed { button, x: _, y: _ } => {
					match button {
						Button::Left => {},
						Button::Right => {}
						_ => {},
					}
				},
				Event::MouseButtonReleased { button, x: _, y: _ } => {
					match button {
						Button::Left => {}
						Button::Right => {},
						_ => {},
					}
				},
				_ => {},
			}
		}
	}
}