Initial commit
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
/target
|
||||
**/*.rs.bk
|
||||
.idea/
|
||||
*.iml
|
195
Cargo.lock
generated
Normal file
|
@ -0,0 +1,195 @@
|
|||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "citysim"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"perlin_noise 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sdl2 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cloudabi"
|
||||
version = "0.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fuchsia-zircon"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fuchsia-zircon-sys"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "0.2.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "num"
|
||||
version = "0.1.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-iter 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.39"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-iter"
|
||||
version = "0.1.37"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "perlin_noise"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"rand 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.3.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "sdl2"
|
||||
version = "0.31.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"sdl2-sys 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sdl2-sys"
|
||||
version = "0.31.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"cfg-if 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[metadata]
|
||||
"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"
|
||||
"checksum bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d0c54bb8f454c567f21197eefcdbf5679d0bd99f2ddbe52e84c77061952e6789"
|
||||
"checksum cfg-if 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "efe5c877e17a9c717a0bf3613b2709f723202c4e4675cc8f12926ded29bcb17e"
|
||||
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
|
||||
"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
|
||||
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
|
||||
"checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73"
|
||||
"checksum libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)" = "b685088df2b950fccadf07a7187c8ef846a959c142338a48f9dc0b94517eb5f1"
|
||||
"checksum num 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e"
|
||||
"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea"
|
||||
"checksum num-iter 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "af3fdbbc3291a5464dc57b03860ec37ca6bf915ed6ee385e7c6c052c422b2124"
|
||||
"checksum num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "630de1ef5cc79d0cdd78b7e33b81f083cbfe90de0f4b2b2f07f905867c70e9fe"
|
||||
"checksum perlin_noise 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "23ca62da55725ddc421972a53b11cc855a168de84307aea534bc94ef6dbd61fa"
|
||||
"checksum rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "15a732abf9d20f0ad8eeb6f909bf6868722d9a06e1e50802b6a70351f40b4eb1"
|
||||
"checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5"
|
||||
"checksum rand 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "12397506224b2f93e6664ffc4f664b29be8208e5157d3d90b44f09b5fae470ea"
|
||||
"checksum rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "edecf0f94da5551fc9b492093e30b041a891657db7940ee221f9d2f66e82eef2"
|
||||
"checksum sdl2 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a74c2a98a354b20713b90cce70aef9e927e46110d1bc4ef728fd74e0d53eba60"
|
||||
"checksum sdl2-sys 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5c543ce8a6e33a30cb909612eeeb22e693848211a84558d5a00bb11e791b7ab7"
|
||||
"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd"
|
||||
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
8
Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "citysim"
|
||||
version = "0.1.0"
|
||||
authors = ["Sijmen Schoon <me@sijmenschoon.nl>"]
|
||||
|
||||
[dependencies]
|
||||
sdl2 = { version = "0.31.0", default-features = false, features = ["image"] }
|
||||
perlin_noise = "1.0.1"
|
BIN
data/png/bridgeEast.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
data/png/bridgeNorth.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
data/png/crossroad.png
Normal file
After Width: | Height: | Size: 1.5 KiB |
BIN
data/png/dirt.png
Normal file
After Width: | Height: | Size: 664 B |
BIN
data/png/dirtHigh.png
Normal file
After Width: | Height: | Size: 918 B |
BIN
data/png/grass.png
Normal file
After Width: | Height: | Size: 841 B |
BIN
data/png/hillCornerEast.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
data/png/hillCornerNW.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
data/png/hillCornerSE.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
data/png/hillCornerWest.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
data/png/hillEast.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
data/png/hillNorth.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
data/png/hillRoadEast.png
Normal file
After Width: | Height: | Size: 1.6 KiB |
BIN
data/png/hillRoadNorth.png
Normal file
After Width: | Height: | Size: 1.7 KiB |
BIN
data/png/hillRoadSouth.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
data/png/hillRoadWest.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
data/png/hillSouth.png
Normal file
After Width: | Height: | Size: 877 B |
BIN
data/png/hillWest.png
Normal file
After Width: | Height: | Size: 836 B |
BIN
data/png/lot.png
Normal file
After Width: | Height: | Size: 930 B |
BIN
data/png/lotCornerEast.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
data/png/lotCornerNorth.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
data/png/lotCornerSouth.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
data/png/lotCornerWest.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
data/png/lotEast.png
Normal file
After Width: | Height: | Size: 936 B |
BIN
data/png/lotExitEast.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
data/png/lotExitNorth.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
data/png/lotExitSouth.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
data/png/lotExitWest.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
data/png/lotNorth.png
Normal file
After Width: | Height: | Size: 1,007 B |
BIN
data/png/lotPark.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
data/png/lotSouth.png
Normal file
After Width: | Height: | Size: 981 B |
BIN
data/png/lotWest.png
Normal file
After Width: | Height: | Size: 1,018 B |
BIN
data/png/roadCornerES.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
BIN
data/png/roadCornerNE.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
BIN
data/png/roadCornerNW.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
BIN
data/png/roadCornerWS.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
data/png/roadEast.png
Normal file
After Width: | Height: | Size: 1 KiB |
BIN
data/png/roadEndEast.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
data/png/roadEndNorth.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
data/png/roadEndSouth.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
data/png/roadEndWest.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
data/png/roadNorth.png
Normal file
After Width: | Height: | Size: 1 KiB |
BIN
data/png/roadTEast.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
data/png/roadTNorth.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
data/png/roadTSouth.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
data/png/roadTWest.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
BIN
data/png/treeConiferShort.png
Normal file
After Width: | Height: | Size: 408 B |
BIN
data/png/treeConiferTall.png
Normal file
After Width: | Height: | Size: 512 B |
BIN
data/png/treeShort.png
Normal file
After Width: | Height: | Size: 267 B |
BIN
data/png/treeShort_autumn.png
Normal file
After Width: | Height: | Size: 263 B |
BIN
data/png/treeTall.png
Normal file
After Width: | Height: | Size: 280 B |
BIN
data/png/treeTall_autumn.png
Normal file
After Width: | Height: | Size: 279 B |
BIN
data/png/water.png
Normal file
After Width: | Height: | Size: 854 B |
BIN
data/png/waterBeachCornerEast.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
data/png/waterBeachCornerNorth.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
BIN
data/png/waterBeachCornerSouth.png
Normal file
After Width: | Height: | Size: 1.7 KiB |
BIN
data/png/waterBeachCornerWest.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
data/png/waterBeachEast.png
Normal file
After Width: | Height: | Size: 1,008 B |
BIN
data/png/waterBeachNorth.png
Normal file
After Width: | Height: | Size: 989 B |
BIN
data/png/waterBeachSouth.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
data/png/waterBeachWest.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
data/png/waterCornerEast.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
data/png/waterCornerNorth.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
data/png/waterCornerSouth.png
Normal file
After Width: | Height: | Size: 1.3 KiB |
BIN
data/png/waterCornerWest.png
Normal file
After Width: | Height: | Size: 1.2 KiB |
BIN
data/png/waterEast.png
Normal file
After Width: | Height: | Size: 1 KiB |
BIN
data/png/waterNorth.png
Normal file
After Width: | Height: | Size: 962 B |
BIN
data/png/waterSouth.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
data/png/waterWest.png
Normal file
After Width: | Height: | Size: 1 KiB |
15
src/canvas_copy_at.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use sdl2::rect::Rect;
|
||||
use sdl2::render::{Canvas, Texture};
|
||||
use sdl2::video::Window;
|
||||
|
||||
pub trait CopyAt {
|
||||
fn copy_at(&mut self, texture: &Texture, x: i32, y: i32) -> Result<(), String>;
|
||||
}
|
||||
|
||||
impl CopyAt for Canvas<Window> {
|
||||
fn copy_at(&mut self, texture: &Texture, x: i32, y: i32) -> Result<(), String> {
|
||||
let query = texture.query();
|
||||
let destination = Rect::new(x, y, query.width, query.height);
|
||||
self.copy(&texture, None, destination)
|
||||
}
|
||||
}
|
131
src/main.rs
Normal file
|
@ -0,0 +1,131 @@
|
|||
extern crate sdl2;
|
||||
extern crate perlin_noise;
|
||||
|
||||
mod texture_manager;
|
||||
mod canvas_copy_at;
|
||||
mod tile;
|
||||
|
||||
use sdl2::EventPump;
|
||||
use sdl2::event::Event;
|
||||
use sdl2::keyboard::Keycode;
|
||||
use sdl2::pixels::Color;
|
||||
use sdl2::render::WindowCanvas;
|
||||
use sdl2::image::INIT_PNG;
|
||||
|
||||
use perlin_noise::PerlinNoise;
|
||||
|
||||
use texture_manager::TextureManager;
|
||||
use canvas_copy_at::CopyAt;
|
||||
use tile::Tile;
|
||||
|
||||
struct GameState {
|
||||
camera_x: i32,
|
||||
camera_y: i32,
|
||||
}
|
||||
|
||||
fn world_to_screen(x: i32, y: i32, z: i32) -> (i32, i32) {
|
||||
let tile_height = 50;
|
||||
let tile_width = 100;
|
||||
|
||||
let screen_x = (x * tile_width / 2) + (y * tile_width / 2) + 500;
|
||||
let screen_y = (y * tile_height / 2) - (x * tile_height / 2) - z + 500;
|
||||
|
||||
(screen_x, screen_y)
|
||||
}
|
||||
|
||||
fn update(event_pump: &mut EventPump, game_state: &mut GameState) -> bool {
|
||||
for event in event_pump.poll_iter() {
|
||||
match event {
|
||||
Event::Quit { .. } => return true,
|
||||
Event::KeyDown { keycode: Some(keycode), .. } => {
|
||||
match keycode {
|
||||
Keycode::Escape => return true,
|
||||
|
||||
Keycode::Up => game_state.camera_y -= 8,
|
||||
Keycode::Down => game_state.camera_y += 8,
|
||||
Keycode::Left => game_state.camera_x -= 8,
|
||||
Keycode::Right => game_state.camera_x += 8,
|
||||
|
||||
_ => {}
|
||||
};
|
||||
},
|
||||
|
||||
_ => {},
|
||||
};
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
fn render(canvas: &mut WindowCanvas, texture_manager: &TextureManager, map: &Vec<Vec<Tile>>,
|
||||
game_state: &mut GameState) {
|
||||
canvas.clear();
|
||||
|
||||
for (y, row) in map.iter().enumerate() {
|
||||
for (x, tile) in row.iter().enumerate().rev() {
|
||||
let texture = &texture_manager[tile.type_];
|
||||
let (screen_x, screen_y) = world_to_screen(x as i32, y as i32, tile.height);
|
||||
|
||||
canvas.copy_at(
|
||||
texture,
|
||||
screen_x - game_state.camera_x,
|
||||
screen_y - game_state.camera_y
|
||||
).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
canvas.present();
|
||||
}
|
||||
|
||||
fn generate_map() -> Vec<Vec<Tile>> {
|
||||
let perlin = PerlinNoise::new();
|
||||
let mut map = vec![vec![Tile::new(6, 0); 128]; 128];
|
||||
|
||||
for y in 0..128 {
|
||||
for x in 0..128 {
|
||||
let height = perlin.get2d([x as f64 / 16.0, y as f64 / 16.0]) * 320.0;
|
||||
println!("({}, {}) = {}", x, y, height);
|
||||
map[y][x].height = height as i32;
|
||||
}
|
||||
}
|
||||
|
||||
map
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let sdl_context = sdl2::init().unwrap();
|
||||
let video_subsystem = sdl_context.video().unwrap();
|
||||
sdl2::image::init(INIT_PNG).unwrap();
|
||||
|
||||
let window = video_subsystem
|
||||
.window("", 1680, 1050)
|
||||
.position_centered()
|
||||
.allow_highdpi()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let mut canvas = window
|
||||
.into_canvas()
|
||||
.present_vsync()
|
||||
.accelerated()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let texture_creator = canvas.texture_creator();
|
||||
let mut event_pump = sdl_context.event_pump().unwrap();
|
||||
|
||||
let texture_manager = TextureManager::new(&texture_creator);
|
||||
let map = generate_map();
|
||||
let mut game_state = GameState { camera_x: 0, camera_y: 0 };
|
||||
|
||||
canvas.set_draw_color(Color::RGB(48, 48, 48));
|
||||
|
||||
loop {
|
||||
let should_quit = update(&mut event_pump, &mut game_state);
|
||||
if should_quit {
|
||||
return;
|
||||
}
|
||||
|
||||
render(&mut canvas, &texture_manager, &map, &mut game_state);
|
||||
}
|
||||
}
|
42
src/texture_manager.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use sdl2::image::LoadTexture;
|
||||
use sdl2::render::{Texture, TextureCreator};
|
||||
use sdl2::video::WindowContext;
|
||||
use std::ops::Index;
|
||||
|
||||
pub struct TextureManager<'a> {
|
||||
bridge_east: Texture<'a>,
|
||||
bridge_north: Texture<'a>,
|
||||
crossroad: Texture<'a>,
|
||||
dirt: Texture<'a>,
|
||||
dirt_high: Texture<'a>,
|
||||
grass: Texture<'a>,
|
||||
}
|
||||
|
||||
impl<'a> TextureManager<'a> {
|
||||
pub fn new(texture_creator: &'a TextureCreator<WindowContext>) -> TextureManager<'a> {
|
||||
TextureManager {
|
||||
bridge_east: texture_creator.load_texture("data/png/bridgeEast.png").unwrap(),
|
||||
bridge_north: texture_creator.load_texture("data/png/bridgeNorth.png").unwrap(),
|
||||
crossroad:texture_creator.load_texture("data/png/crossroad.png").unwrap(),
|
||||
dirt: texture_creator.load_texture("data/png/dirt.png").unwrap(),
|
||||
dirt_high: texture_creator.load_texture("data/png/dirtHigh.png").unwrap(),
|
||||
grass: texture_creator.load_texture("data/png/grass.png").unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Index<usize> for TextureManager<'a> {
|
||||
type Output = Texture<'a>;
|
||||
|
||||
fn index(&self, index: usize) -> &Texture<'a> {
|
||||
match index {
|
||||
1 => &self.bridge_east,
|
||||
2 => &self.bridge_north,
|
||||
3 => &self.crossroad,
|
||||
4 => &self.dirt,
|
||||
5 => &self.dirt_high,
|
||||
6 => &self.grass,
|
||||
_ => panic!("invalid texture ID"),
|
||||
}
|
||||
}
|
||||
}
|
11
src/tile.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
#[derive(Clone)]
|
||||
pub struct Tile {
|
||||
pub type_: usize,
|
||||
pub height: i32,
|
||||
}
|
||||
|
||||
impl Tile {
|
||||
pub fn new(type_: usize, height: i32) -> Tile {
|
||||
Tile { type_, height }
|
||||
}
|
||||
}
|