This commit is contained in:
Arkitu 2024-08-24 22:44:13 +02:00
parent 97f4581224
commit 4114003688
3 changed files with 62 additions and 10 deletions

View File

@ -3,8 +3,19 @@ name = "forestiles"
version = "0.1.0"
edition = "2021"
[lib]
crate_type=["cdylib"]
[[bin]]
path = "src/lib.rs"
name = "forestiles"
[package.metadata.android]
package = "com.forestiles.arkitu"
build_targets = ["armv7-linux-androideabi", "aarch64-linux-android", "i686-linux-android", "x86_64-linux-android"]
[dependencies]
winit = { version = "0.29", features = ["rwh_05"] }
winit = { version = "0.29", features = ["rwh_05", "android-native-activity"] }
env_logger = "0.11"
log = "0.4"
wgpu = "22.1"

View File

@ -2,7 +2,6 @@ mod graphics;
mod state;
use state::State;
use graphics::Graphics;
use winit::event_loop::EventLoop;
pub fn main() {
#[cfg(target_arch = "wasm32")]
@ -15,7 +14,7 @@ pub fn main() {
env_logger::init();
}
let event_loop = EventLoop::new().unwrap();
let event_loop = winit::event_loop::EventLoop::new().unwrap();
let window = winit::window::WindowBuilder::new().build(&event_loop).unwrap();
#[cfg(not(target_arch = "wasm32"))]
@ -39,3 +38,28 @@ pub fn main() {
});
}
}
#[cfg(target_os = "android")]
#[no_mangle]
fn android_main(app: winit::platform::android::activity::AndroidApp) {
use std::thread;
use std::time::Duration;
use winit::platform::android::EventLoopBuilderExtAndroid;
use winit::event_loop::EventLoopBuilder;
use winit::window::WindowBuilder;
let event_loop = EventLoopBuilder::new()
.with_android_app(app)
.build()
.expect("Cant build event loop");
let mut window_builder = WindowBuilder::new();
let window = window_builder.build(&event_loop)
.expect("Can't create window!");
thread::sleep(Duration::from_secs(2));
let state = State::new();
pollster::block_on(Graphics::init(&window, state)).run(event_loop);
}

View File

@ -1,5 +1,5 @@
use std::time::Instant;
use noise::{Fbm, NoiseFn, Perlin};
use noise::{Fbm, MultiFractal, NoiseFn, Perlin};
use rand::prelude::*;
use voronoice::{BoundingBox, Point, Voronoi, VoronoiBuilder, VoronoiCell};
use winit::event::{DeviceEvent, ElementState, Event, MouseButton, MouseScrollDelta, WindowEvent};
@ -13,7 +13,9 @@ pub const SQRT_3: f32 = 1.732050807568877293527446341505872367;
enum CellKind {
Void,
Sea,
Grass,
Plain,
Beach,
Forest
}
impl CellKind {
const VARIANTS: u8 = 3;
@ -23,7 +25,9 @@ impl CellKind {
match self {
Self::Void => [0.; 4],
Self::Sea => [0., 0., 1., 1.],
Self::Grass => [0., 1., 0., 1.]
Self::Plain => [0., 1., 0., 1.],
Self::Beach => [1., 1., 0., 1.],
Self::Forest => [0., 0.5, 0., 1.]
}
}
}
@ -32,7 +36,9 @@ impl From<u8> for CellKind {
match value {
0 => Self::Void,
1 => Self::Sea,
2 => Self::Grass,
2 => Self::Plain,
3 => Self::Beach,
4 => Self::Forest,
_ => panic!("Invalid cell kind")
}
}
@ -61,7 +67,7 @@ struct Map {
impl Map {
const HEIGHT: f32 = 2.;
const WIDTH: f32 = 2.;
const SIZE: usize = 1_00;
const SIZE: usize = 10_000;
fn new(seed: u32) -> Self {
let mut rng = rand::rngs::SmallRng::seed_from_u64(seed as u64);
let mut sites = Vec::with_capacity(Self::SIZE);
@ -71,11 +77,13 @@ impl Map {
let voronoi = VoronoiBuilder::default()
.set_sites(sites)
.set_bounding_box(BoundingBox::new_centered(Self::WIDTH as f64, Self::HEIGHT as f64))
.set_lloyd_relaxation_iterations(5)
.set_lloyd_relaxation_iterations(3)
.build()
.unwrap();
let mut cells_data = Vec::with_capacity(Self::SIZE);
let z_noise = Fbm::<Perlin>::new(seed);
let moisture_noise = Fbm::<Perlin>::new(seed+1)
.set_frequency(2.);
for i in 0..Self::SIZE {
let c = voronoi.cell(i);
let site = c.site_position();
@ -86,8 +94,17 @@ impl Map {
).clamp(0., 1.);
let k = if z <= 0.5 {
CellKind::Sea
} else if z <= 0.52 {
CellKind::Beach
} else {
CellKind::Grass
let m = (
(moisture_noise.get([site.x, site.y])+1.)/2. // Noise + [0; 1]
).clamp(0., 1.);
if m > 0.5 {
CellKind::Forest
} else {
CellKind::Plain
}
};
cells_data.push(CellData::new(k, i, z as f32));
}