we can now load files!

This commit is contained in:
Evan Pratten 2022-03-19 13:19:10 -04:00
parent 12f084fff8
commit f25c5b54e0
9 changed files with 103 additions and 12 deletions

View File

@ -3,5 +3,12 @@
"cSpell.words": [
"msaa",
"vsync"
]
],
"files.watcherExclude": {
"**/.git/objects/**": true,
"**/.git/subtree-cache/**": true,
"**/node_modules/*/**": true,
"**/.hg/store/**": true,
"**/target/**": true,
}
}

8
.vscode/tasks.json vendored
View File

@ -9,6 +9,14 @@
],
"group": "build",
"label": "rust: cargo build"
},
{
"type": "cargo",
"command": "run",
"problemMatcher": [
"$rustc"
],
"label": "Launch Game"
}
]
}

View File

@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
raylib = { version = "3.7", path = "../../third_party/raylib-rs/raylib" }
sad_machine = { version = "1.0", path = "../../third_party/sm" }
tokio = { version = "1.17.0", featurs = ["fs"] }
log = "0.4.14"
profiling = "1.0.5"
serde = { version = "1.0.136", features = ["derive"] }
@ -15,3 +16,7 @@ serde_json = "1.0.79"
directories = "4.0.1"
chrono = { verison = "0.4.19", features = ["serde"] }
discord-sdk = "0.3.0"
rust-embed = { version = "6.2.0", features = ["compression"] }
thiserror = "1.0.30"
# nalgebra = { version = "0.30.1", features = ["serde"] }
approx = "0.5.1"

View File

@ -0,0 +1,8 @@
/// This structure is dynamically packed with the contents of `dist` at compile time
///
/// This process allows us to only distribute a single binary, and have all the game assets stored in memory automatically.
/// The downside of this process is that the game will require a decent amount of RAM on the client's machine (and x64).
#[derive(rust_embed::RustEmbed)]
#[folder = "../dist"]
pub struct InternalData;

View File

@ -0,0 +1,29 @@
use serde::de::DeserializeOwned;
use super::datastore::InternalData;
/// Possible errors generated when deserializing JSON data from memory
#[derive(Debug, thiserror::Error)]
pub enum InternalJsonLoadError {
/// An error occurred with the JSON data itself
#[error(transparent)]
JsonError(#[from] serde_json::Error),
/// The JSON data was not found in the internal data store
#[error("Could not load embedded asset: {0}")]
AssetNotFound(String),
}
/// Load an embedded JSON file
pub fn load_json_structure<'a, T: DeserializeOwned>(
dist_path: &str,
) -> Result<T, InternalJsonLoadError> {
// Load the json file from the embedded data as a string
let data = InternalData::get(dist_path)
.ok_or(InternalJsonLoadError::AssetNotFound(dist_path.to_string()))?
.data;
// Deserialize the json string into a rust structure
let json_structure: T = serde_json::from_slice(&data)?;
Ok(json_structure)
}

View File

@ -0,0 +1,2 @@
pub mod datastore;
pub mod json;

View File

@ -1,11 +1,15 @@
//! This file is the main entry point for the game logic.
use std::borrow::Borrow;
use crate::{asset_manager::json::load_json_structure, project_constants::ProjectConstants};
#[macro_use]
extern crate approx; // For the macro `relative_eq!`
pub mod persistent;
pub mod rendering;
pub mod discord;
pub mod asset_manager;
pub mod discord;
pub mod persistent;
pub mod project_constants;
pub mod rendering;
/// This is the game logic entrypoint. Despite being async,
/// this is expected to block the main thread for rendering and stuff.
@ -14,18 +18,29 @@ pub mod asset_manager;
pub async fn entrypoint(force_recreate_savefiles: bool) {
log::info!("Game main thread handed off to logic crate.");
// Load the project constants
let project_constants: ProjectConstants =
load_json_structure("project-constants.json").expect("Could not load project constants");
// Load the game settings
let mut settings = persistent::settings::PersistentGameSettings::load_or_create(force_recreate_savefiles)
let mut settings =
persistent::settings::PersistentGameSettings::load_or_create(force_recreate_savefiles)
.expect("Failed to parse game settings from disk. Possibly corrupt file?");
// Load the game save state
let mut save_state = persistent::save_state::GameSaveState::load_or_create(force_recreate_savefiles)
let mut save_state =
persistent::save_state::GameSaveState::load_or_create(force_recreate_savefiles)
.expect("Failed to parse game save state from disk. Possibly corrupt file?");
// Blocking call to the graphics rendering loop.
rendering::event_loop::handle_graphics_blocking(
|builder| {
builder.msaa_4x().vsync();
builder
.msaa_4x()
.vsync()
.title(project_constants.game_name.as_str())
.height(project_constants.base_window_size.1 as i32)
.width(project_constants.base_window_size.0 as i32);
},
settings.target_fps,
);

View File

@ -0,0 +1,17 @@
use serde::Deserialize;
/// This structure is filled with the contents of `dist/project-constants.json` at runtime
#[derive(Debug, Deserialize)]
pub struct ProjectConstants {
/// The name of the game
pub game_name: String,
/// The window size to use on launch
pub base_window_size: (u32, u32),
/// The Discord application ID
pub discord_app_id: u64,
/// The target framerate of the game
pub target_fps: u32,
}