1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2024-11-16 10:52:44 +00:00
doukutsu-rs/src/lib.rs

294 lines
8.8 KiB
Rust
Raw Normal View History

#[macro_use]
extern crate log;
2021-01-01 01:46:01 +00:00
#[cfg_attr(feature = "scripting", macro_use)]
#[cfg(feature = "scripting")]
extern crate lua_ffi;
extern crate strum;
#[macro_use]
extern crate strum_macros;
2021-01-01 01:46:01 +00:00
use std::cell::UnsafeCell;
2021-01-28 22:33:43 +00:00
use std::env;
use std::path::PathBuf;
2021-02-24 08:28:47 +00:00
use std::sync::Mutex;
use std::time::Instant;
2021-01-28 22:33:43 +00:00
use directories::ProjectDirs;
2021-02-24 08:28:47 +00:00
use lazy_static::lazy_static;
2021-01-28 22:33:43 +00:00
use crate::builtin_fs::BuiltinFS;
2021-01-27 18:20:47 +00:00
use crate::framework::context::Context;
use crate::framework::error::{GameError, GameResult};
2021-01-28 22:33:43 +00:00
use crate::framework::filesystem::{mount_user_vfs, mount_vfs};
2021-01-27 18:20:47 +00:00
use crate::framework::graphics;
2021-02-05 22:47:13 +00:00
use crate::framework::ui::UI;
2021-01-28 22:33:43 +00:00
use crate::framework::vfs::PhysicalFS;
use crate::scene::loading_scene::LoadingScene;
use crate::scene::Scene;
use crate::shared_game_state::{SharedGameState, TimingMode};
2021-01-28 22:33:43 +00:00
use crate::texture_set::{G_MAG, I_MAG};
mod bmfont;
mod bmfont_renderer;
mod builtin_fs;
mod caret;
mod common;
mod components;
mod encoding;
mod engine_constants;
mod entity;
mod frame;
2021-01-27 18:20:47 +00:00
mod framework;
2020-11-28 19:25:51 +00:00
mod input;
2021-02-10 20:14:09 +00:00
mod inventory;
#[cfg(feature = "hooks")]
mod hooks;
mod live_debugger;
mod macros;
mod map;
mod menu;
2021-03-12 16:55:20 +00:00
#[cfg(feature = "netplay")]
mod netplay;
mod npc;
mod physics;
mod player;
mod profile;
mod rng;
mod scene;
2021-01-01 01:46:01 +00:00
#[cfg(feature = "scripting")]
mod scripting;
2020-11-28 19:25:51 +00:00
mod settings;
2021-01-27 18:20:47 +00:00
#[cfg(feature = "backend-gfx")]
2020-11-28 19:25:51 +00:00
mod shaders;
mod shared_game_state;
mod sound;
2021-02-10 20:14:09 +00:00
mod stage;
mod text_script;
mod texture_set;
mod weapon;
2021-10-14 04:54:11 +00:00
pub struct LaunchOptions {
pub server_mode: bool,
}
2021-02-24 08:28:47 +00:00
lazy_static! {
pub static ref GAME_SUSPENDED: Mutex<bool> = Mutex::new(false);
}
2021-01-28 22:33:43 +00:00
pub struct Game {
scene: Option<Box<dyn Scene>>,
2021-01-01 01:46:01 +00:00
state: UnsafeCell<SharedGameState>,
ui: UI,
start_time: Instant,
2020-11-07 17:17:01 +00:00
last_tick: u128,
next_tick: u128,
loops: u64,
}
impl Game {
fn new(ctx: &mut Context) -> GameResult<Game> {
let s = Game {
scene: None,
ui: UI::new(ctx)?,
2021-01-01 01:46:01 +00:00
state: UnsafeCell::new(SharedGameState::new(ctx)?),
start_time: Instant::now(),
2020-11-07 17:17:01 +00:00
last_tick: 0,
next_tick: 0,
loops: 0,
};
Ok(s)
}
fn update(&mut self, ctx: &mut Context) -> GameResult {
if let Some(scene) = self.scene.as_mut() {
2021-01-01 01:46:01 +00:00
let state_ref = unsafe { &mut *self.state.get() };
match state_ref.timing_mode {
TimingMode::_50Hz | TimingMode::_60Hz => {
2020-11-07 17:17:01 +00:00
let last_tick = self.next_tick;
2021-02-24 08:28:47 +00:00
while self.start_time.elapsed().as_nanos() >= self.next_tick && self.loops < 10 {
2021-01-01 01:46:01 +00:00
if (state_ref.settings.speed - 1.0).abs() < 0.01 {
self.next_tick += state_ref.timing_mode.get_delta() as u128;
} else {
2021-02-24 08:28:47 +00:00
self.next_tick +=
(state_ref.timing_mode.get_delta() as f64 / state_ref.settings.speed) as u128;
}
self.loops += 1;
}
2020-11-04 15:37:00 +00:00
if self.loops == 10 {
log::warn!("Frame skip is way too high, a long system lag occurred?");
2020-11-07 17:17:01 +00:00
self.last_tick = self.start_time.elapsed().as_nanos();
2021-02-10 20:14:09 +00:00
self.next_tick = self.last_tick
2021-02-24 08:28:47 +00:00
+ (state_ref.timing_mode.get_delta() as f64 / state_ref.settings.speed) as u128;
2020-11-04 15:37:00 +00:00
self.loops = 0;
}
2020-11-07 17:17:01 +00:00
if self.loops != 0 {
2021-01-01 01:46:01 +00:00
scene.draw_tick(state_ref)?;
2020-11-07 17:17:01 +00:00
self.last_tick = last_tick;
}
for _ in 0..self.loops {
2021-01-01 01:46:01 +00:00
scene.tick(state_ref, ctx)?;
}
}
TimingMode::FrameSynchronized => {
2021-01-01 01:46:01 +00:00
scene.tick(state_ref, ctx)?;
}
}
}
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> GameResult {
2021-01-01 01:46:01 +00:00
let state_ref = unsafe { &mut *self.state.get() };
2021-10-14 04:54:11 +00:00
if ctx.headless {
self.loops = 0;
state_ref.frame_time = 1.0;
return Ok(());
}
2021-01-01 01:46:01 +00:00
if state_ref.timing_mode != TimingMode::FrameSynchronized {
2021-02-10 12:12:38 +00:00
let mut elapsed = self.start_time.elapsed().as_nanos();
2021-02-24 08:28:47 +00:00
// Even with the non-monotonic Instant mitigation at the start of the event loop, there's still a chance of it not working.
// This check here should trigger if that happens and makes sure there's no panic from an underflow.
if elapsed < self.last_tick {
elapsed = self.last_tick;
2021-02-10 20:14:09 +00:00
}
2021-02-24 08:28:47 +00:00
let n1 = (elapsed - self.last_tick) as f64;
2020-11-07 17:17:01 +00:00
let n2 = (self.next_tick - self.last_tick) as f64;
2021-02-24 08:28:47 +00:00
state_ref.frame_time = if state_ref.settings.motion_interpolation { n1 / n2 } else { 1.0 };
}
2021-01-28 22:33:43 +00:00
unsafe {
2021-02-24 08:28:47 +00:00
G_MAG = if state_ref.settings.subpixel_coords { state_ref.scale } else { 1.0 };
2021-01-28 22:33:43 +00:00
I_MAG = state_ref.scale;
}
self.loops = 0;
2021-02-24 08:28:47 +00:00
graphics::prepare_draw(ctx)?;
graphics::clear(ctx, [0.0, 0.0, 0.0, 1.0].into());
if let Some(scene) = self.scene.as_mut() {
2021-01-01 01:46:01 +00:00
scene.draw(state_ref, ctx)?;
if state_ref.settings.touch_controls {
2021-02-10 20:14:09 +00:00
state_ref.touch_controls.draw(
state_ref.canvas_size,
2021-02-24 08:28:47 +00:00
state_ref.scale,
2021-02-10 20:14:09 +00:00
&state_ref.constants,
&mut state_ref.texture_set,
ctx,
)?;
2020-10-20 20:45:56 +00:00
}
2021-01-01 01:46:01 +00:00
self.ui.draw(state_ref, ctx, scene)?;
}
graphics::present(ctx)?;
2021-02-24 08:28:47 +00:00
Ok(())
}
}
2021-10-14 04:54:11 +00:00
pub fn init(options: LaunchOptions) -> GameResult {
let _ = simple_logger::init_with_level(log::Level::Info);
2021-02-24 08:28:47 +00:00
#[cfg(not(target_os = "android"))]
2021-01-28 22:33:43 +00:00
let resource_dir = if let Ok(data_dir) = env::var("CAVESTORY_DATA_DIR") {
PathBuf::from(data_dir)
} else {
let mut resource_dir = env::current_exe()?;
if resource_dir.file_name().is_some() {
let _ = resource_dir.pop();
}
2021-08-13 01:46:41 +00:00
#[cfg(target_os = "macos")]
{
let mut bundle_dir = resource_dir.clone();
let _ = bundle_dir.pop();
let mut bundle_exec_dir = bundle_dir.clone();
bundle_exec_dir.push("MacOS");
bundle_dir.push("Resources");
if bundle_exec_dir.is_dir() && bundle_dir.is_dir() {
log::info!("Running in macOS bundle mode");
resource_dir = bundle_dir
}
}
2021-01-28 22:33:43 +00:00
resource_dir.push("data");
resource_dir
};
2021-02-24 08:28:47 +00:00
#[cfg(not(target_os = "android"))]
2021-03-09 14:05:38 +00:00
log::info!("Resource directory: {:?}", resource_dir);
log::info!("Initializing engine...");
2021-01-28 22:33:43 +00:00
let mut context = Context::new();
mount_vfs(&mut context, Box::new(BuiltinFS::new()));
2021-02-24 08:28:47 +00:00
#[cfg(not(target_os = "android"))]
2021-01-28 22:33:43 +00:00
mount_vfs(&mut context, Box::new(PhysicalFS::new(&resource_dir, true)));
#[cfg(not(target_os = "android"))]
2021-02-10 20:14:09 +00:00
let project_dirs = match ProjectDirs::from("", "", "doukutsu-rs") {
2021-01-28 22:33:43 +00:00
Some(dirs) => dirs,
None => {
2021-02-24 08:28:47 +00:00
return Err(GameError::FilesystemError(String::from("No valid home directory path could be retrieved.")));
2021-01-28 22:33:43 +00:00
}
};
#[cfg(target_os = "android")]
2021-02-10 20:14:09 +00:00
{
2021-02-24 08:28:47 +00:00
let mut data_path =
PathBuf::from(ndk_glue::native_activity().internal_data_path().to_string_lossy().to_string());
let mut user_path = data_path.clone();
data_path.push("data");
user_path.push("saves");
let _ = std::fs::create_dir_all(&data_path);
let _ = std::fs::create_dir_all(&user_path);
log::info!("Android data directories: data_path={:?} user_path={:?}", &data_path, &user_path);
mount_vfs(&mut context, Box::new(PhysicalFS::new(&data_path, true)));
mount_user_vfs(&mut context, Box::new(PhysicalFS::new(&user_path, false)));
2021-02-10 20:14:09 +00:00
}
2021-01-28 22:33:43 +00:00
2021-02-24 08:28:47 +00:00
#[cfg(not(target_os = "android"))]
2021-06-21 11:12:58 +00:00
{
if let Ok(_) = crate::framework::filesystem::open(&mut context, "/.drs_localstorage") {
let mut user_dir = resource_dir.clone();
user_dir.push("_drs_profile");
let _ = std::fs::create_dir_all(&user_dir);
mount_user_vfs(&mut context, Box::new(PhysicalFS::new(&user_dir, false)));
} else {
mount_user_vfs(&mut context, Box::new(PhysicalFS::new(project_dirs.data_local_dir(), false)));
}
}
2021-02-24 08:28:47 +00:00
2021-10-14 04:54:11 +00:00
if options.server_mode {
log::info!("Running in server mode...");
context.headless = true;
}
2021-02-24 08:28:47 +00:00
let game = UnsafeCell::new(Game::new(&mut context)?);
let state_ref = unsafe { &mut *((&mut *game.get()).state.get()) };
2021-01-28 22:33:43 +00:00
#[cfg(feature = "scripting")]
2021-02-10 20:14:09 +00:00
{
2021-02-24 08:28:47 +00:00
state_ref.lua.update_refs(unsafe { (&*game.get()).state.get() }, &mut context as *mut Context);
2021-02-10 20:14:09 +00:00
}
2021-01-28 22:33:43 +00:00
state_ref.next_scene = Some(Box::new(LoadingScene::new()));
2021-02-24 08:28:47 +00:00
context.run(unsafe { &mut *game.get() })?;
2021-01-28 22:33:43 +00:00
Ok(())
}