2020-10-07 14:08:12 +00:00
|
|
|
#[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;
|
2020-10-07 14:08:12 +00:00
|
|
|
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;
|
2020-11-04 23:25:18 +00:00
|
|
|
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;
|
2020-10-07 14:08:12 +00:00
|
|
|
use pretty_env_logger::env_logger::Env;
|
|
|
|
|
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;
|
2020-10-07 14:08:12 +00:00
|
|
|
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};
|
2020-10-07 14:08:12 +00:00
|
|
|
|
|
|
|
mod bmfont;
|
|
|
|
mod bmfont_renderer;
|
|
|
|
mod builtin_fs;
|
|
|
|
mod caret;
|
|
|
|
mod common;
|
2020-11-02 14:01:30 +00:00
|
|
|
mod components;
|
2020-11-24 23:08:27 +00:00
|
|
|
mod difficulty_modifier;
|
2020-10-07 14:08:12 +00:00
|
|
|
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;
|
2021-03-29 21:19:07 +00:00
|
|
|
#[cfg(feature = "hooks")]
|
|
|
|
mod hooks;
|
2020-10-07 14:08:12 +00:00
|
|
|
mod live_debugger;
|
|
|
|
mod macros;
|
|
|
|
mod map;
|
|
|
|
mod menu;
|
2021-03-12 16:55:20 +00:00
|
|
|
#[cfg(feature = "netplay")]
|
|
|
|
mod netplay;
|
2020-10-07 14:08:12 +00:00
|
|
|
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;
|
2020-10-07 14:08:12 +00:00
|
|
|
mod shared_game_state;
|
|
|
|
mod sound;
|
2021-02-10 20:14:09 +00:00
|
|
|
mod stage;
|
2020-10-07 14:08:12 +00:00
|
|
|
mod text_script;
|
|
|
|
mod texture_set;
|
|
|
|
mod weapon;
|
|
|
|
|
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 {
|
2020-10-07 14:08:12 +00:00
|
|
|
scene: Option<Box<dyn Scene>>,
|
2021-01-01 01:46:01 +00:00
|
|
|
state: UnsafeCell<SharedGameState>,
|
2020-10-07 14:08:12 +00:00
|
|
|
ui: UI,
|
|
|
|
start_time: Instant,
|
2020-11-07 17:17:01 +00:00
|
|
|
last_tick: u128,
|
2020-10-29 12:22:56 +00:00
|
|
|
next_tick: u128,
|
2020-10-07 14:08:12 +00:00
|
|
|
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)?),
|
2020-10-07 14:08:12 +00:00
|
|
|
start_time: Instant::now(),
|
2020-11-07 17:17:01 +00:00
|
|
|
last_tick: 0,
|
2020-10-07 14:08:12 +00:00
|
|
|
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 {
|
2020-10-07 14:08:12 +00:00
|
|
|
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;
|
2020-10-29 12:22:56 +00:00
|
|
|
} 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;
|
2020-10-29 12:22:56 +00:00
|
|
|
}
|
2020-10-07 14:08:12 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:08:12 +00:00
|
|
|
for _ in 0..self.loops {
|
2021-01-01 01:46:01 +00:00
|
|
|
scene.tick(state_ref, ctx)?;
|
2020-10-07 14:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TimingMode::FrameSynchronized => {
|
2021-01-01 01:46:01 +00:00
|
|
|
scene.tick(state_ref, ctx)?;
|
2020-10-07 14:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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() };
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-01-04 00:18:38 +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 };
|
2020-10-30 22:47:29 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
2020-10-30 22:47:29 +00:00
|
|
|
self.loops = 0;
|
|
|
|
|
2021-02-24 08:28:47 +00:00
|
|
|
graphics::prepare_draw(ctx)?;
|
2020-10-07 14:08:12 +00:00
|
|
|
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
|
|
|
}
|
2020-10-07 14:08:12 +00:00
|
|
|
|
2021-01-01 01:46:01 +00:00
|
|
|
self.ui.draw(state_ref, ctx, scene)?;
|
2020-10-07 14:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
graphics::present(ctx)?;
|
|
|
|
|
2021-02-24 08:28:47 +00:00
|
|
|
Ok(())
|
2020-10-07 14:08:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init() -> GameResult {
|
2021-02-24 08:28:47 +00:00
|
|
|
pretty_env_logger::env_logger::from_env(Env::default().default_filter_or("info"))
|
|
|
|
//.filter(Some("ndk_glue"), LevelFilter::Trace)
|
|
|
|
.init();
|
2020-10-07 14:08:12 +00:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
resource_dir.push("data");
|
|
|
|
resource_dir
|
|
|
|
};
|
2020-10-07 14:08:12 +00:00
|
|
|
|
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...");
|
2020-10-07 14:08:12 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
2020-10-07 14:08:12 +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
|
|
|
|
|
|
|
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(())
|
2020-10-07 14:08:12 +00:00
|
|
|
}
|