From c551f0be0a7587d42d8409c6e9cbc811a57611db Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Fri, 23 Apr 2021 12:29:30 -0400 Subject: [PATCH] load in the game logo placeholder --- Cargo.toml | 5 ++- assets/img/logos/game-logo.png | Bin 0 -> 3163 bytes src/gamecore.rs | 10 +++-- src/logic/ingame/mod.rs | 31 +++++++++++++++ src/logic/loadingscreen.rs | 69 +++++++-------------------------- src/logic/mainmenu.rs | 7 +++- src/logic/screen.rs | 8 +++- src/main.rs | 25 ++++++++---- src/resources.rs | 14 +++++-- 9 files changed, 95 insertions(+), 74 deletions(-) create mode 100644 assets/img/logos/game-logo.png create mode 100644 src/logic/ingame/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 8d72bb7..5231d76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,7 @@ raylib = { version = "3.5", git = "https://github.com/ewpratten/raylib-rs", bran serialstudio = "0.1.0" serde = "1.0.125" serde_json = "1.0.64" -failure = "0.1.8" \ No newline at end of file +failure = "0.1.8" +parry2d = "0.4.0" +log = "0.4.14" +env_logger = "0.8.3" \ No newline at end of file diff --git a/assets/img/logos/game-logo.png b/assets/img/logos/game-logo.png new file mode 100644 index 0000000000000000000000000000000000000000..8f734b3cabc4bf60ad12ea837b330be38d2df2b4 GIT binary patch literal 3163 zcmV-h45agkP)EX>4Tx04R}tkv&MmKpe$i(~6=M1q+HeWT@g`K~%(1t5Adrp;l;o12rOi_^|?Psx08P93mETO{_FA%bObU6meM9bjlY} z9;=+UIBUf+tKXBqFqqL-mbp%I2r(>T2@*soD5HQPY(!|)NwJWi{b&pSpzD{&rI4!t zMveuPph0&1;D7MDTPr&`;Uxv5K--Jsd<+BayFjhtIN!&PQ#%3t&%l-5@RzE<%qQuU zh88{odbWX!>xQQ60hc?#z>^`HvMc#%3fV01en#Jv2KsJ+&NZ*M`aVt{fFyMlzX1*o zfsq_#ueW%2M{{rgo~ifu16Pc4oIyI?u>b%724YJ`L;(K){{a7>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syh8UEr@#6g2w%KrwPd0nB@?)vFg;EW=*BQ^X~ zVK5ldfPuhRB+P6xa$VDK#5T7E@$BnFEERSjx3av69D=, + pub resources: GlobalResources, } impl GameCore { - pub fn new() -> Self { + pub fn new(raylib: &mut RaylibHandle, thread: &RaylibThread) -> Self { Self { state: GameState::Loading, last_state_change_time: 0.0, - has_rendered_first_frame:false, - resources: None, + has_rendered_first_frame: false, + resources: GlobalResources::load_all(raylib, thread).expect("Failed to load game assets. Can not launch!"), } } } diff --git a/src/logic/ingame/mod.rs b/src/logic/ingame/mod.rs new file mode 100644 index 0000000..81e542c --- /dev/null +++ b/src/logic/ingame/mod.rs @@ -0,0 +1,31 @@ +use raylib::prelude::*; + +use crate::{ + gamecore::{GameCore, GameState}, + lib::wrappers::audio::player::AudioPlayer, +}; + +use super::screen::Screen; + +pub struct InGameScreen {} + +impl InGameScreen { + pub fn new() -> Self { + Self {} + } +} + +impl Screen for InGameScreen { + fn render( + &mut self, + draw_handle: &mut RaylibDrawHandle, + thread: &RaylibThread, + audio_system: &mut AudioPlayer, + game_core: &mut GameCore, + ) -> Option { + // Clear frame + draw_handle.clear_background(Color::WHITE); + + return None; + } +} diff --git a/src/logic/loadingscreen.rs b/src/logic/loadingscreen.rs index 8bb43fa..184322b 100644 --- a/src/logic/loadingscreen.rs +++ b/src/logic/loadingscreen.rs @@ -3,7 +3,6 @@ use raylib::prelude::*; use crate::{ gamecore::{GameCore, GameState}, lib::wrappers::audio::player::AudioPlayer, - resources::GlobalResources, }; use super::screen::Screen; @@ -13,8 +12,6 @@ const RUST_ORANGE: Color = Color::new(222, 165, 132, 255); #[derive(Debug, PartialEq)] enum LoadingScreenState { - Preload, - LoadingResources, GameLogo, RaylibLogo, Finished, @@ -28,50 +25,11 @@ pub struct LoadingScreen { impl LoadingScreen { pub fn new() -> Self { Self { - state: LoadingScreenState::Preload, + state: LoadingScreenState::GameLogo, last_state_switch_time: 0.0, } } - fn load_global_resources( - &mut self, - draw_handle: &mut RaylibDrawHandle, - game_core: &mut GameCore, - win_height: i32, - win_width: i32, - ) { - // Show a loading message (this will stay on screen until all resources are loaded) - draw_handle.draw_text( - "Loading Assets...", - (win_width / 2) - 90, - (win_height / 3) * 2, - 25, - Color::BLACK, - ); - - if self.state == LoadingScreenState::LoadingResources { - // Load the global resources - let resources = GlobalResources::load_all(); - - // Handle a loading error - if resources.is_err() { - println!("ERROR: Failed to load game resources!"); - panic!("{:?}", resources.err()); - } - - // Set the global resources - game_core.resources = Some(resources.unwrap()); - - // Set the loading screen state to move on to the game logo - self.state = LoadingScreenState::GameLogo; - self.last_state_switch_time = draw_handle.get_time(); - return; - } - - // Update internal state - self.state = LoadingScreenState::LoadingResources; - } - fn get_logo_mask(&self, playthrough_percent: f64) -> Color { // Determine the alpha let alpha; @@ -105,10 +63,18 @@ impl LoadingScreen { (draw_handle.get_time() - self.last_state_switch_time) / SECONDS_PER_LOGO; // Build a color mask - let mask = self.get_logo_mask( playthrough_percent); + let mask = self.get_logo_mask(playthrough_percent); + + // Get the logo + let logo = &game_core.resources.game_logo; // Render the logo - // TODO + draw_handle.draw_texture( + logo, + (win_width / 2) - (logo.width / 2), + (win_height / 2) - (logo.height / 2), + mask, + ); // Move on to next logo if needed if playthrough_percent >= 1.0 { @@ -130,14 +96,14 @@ impl LoadingScreen { (draw_handle.get_time() - self.last_state_switch_time) / SECONDS_PER_LOGO; // Build a color mask - let mask = self.get_logo_mask( playthrough_percent); + let mask = self.get_logo_mask(playthrough_percent); // Create modified colors let alpha_orange = Color { r: RUST_ORANGE.r, g: RUST_ORANGE.g, b: RUST_ORANGE.b, - a: mask.a + a: mask.a, }; // Render the raylib logo @@ -182,6 +148,7 @@ impl Screen for LoadingScreen { fn render( &mut self, draw_handle: &mut RaylibDrawHandle, + thread: &RaylibThread, _audio_system: &mut AudioPlayer, game_core: &mut GameCore, ) -> Option { @@ -194,12 +161,6 @@ impl Screen for LoadingScreen { // Call the appropriate internal handler function match self.state { - LoadingScreenState::Preload => { - self.load_global_resources(draw_handle, game_core, win_height, win_width) - } - LoadingScreenState::LoadingResources => { - self.load_global_resources(draw_handle, game_core, win_height, win_width) - } LoadingScreenState::GameLogo => { self.show_game_logo(draw_handle, game_core, win_height, win_width) } @@ -212,7 +173,6 @@ impl Screen for LoadingScreen { // A DEBUG warning and skip button #[cfg(debug_assertions)] { - // Render debug text draw_handle.draw_text("RUNNING IN DEBUG MODE", 0, 0, 20, Color::RED); draw_handle.draw_text("Press ESC to skip this screen", 0, 25, 20, Color::RED); @@ -220,7 +180,6 @@ impl Screen for LoadingScreen { if draw_handle.is_key_pressed(KeyboardKey::KEY_ESCAPE) { return Some(GameState::MainMenu); } - } return None; diff --git a/src/logic/mainmenu.rs b/src/logic/mainmenu.rs index 3cb0732..459f49a 100644 --- a/src/logic/mainmenu.rs +++ b/src/logic/mainmenu.rs @@ -1,6 +1,9 @@ use raylib::prelude::*; -use crate::{gamecore::{GameCore, GameState}, lib::wrappers::audio::player::AudioPlayer}; +use crate::{ + gamecore::{GameCore, GameState}, + lib::wrappers::audio::player::AudioPlayer, +}; use super::screen::Screen; @@ -16,10 +19,10 @@ impl Screen for MainMenuScreen { fn render( &mut self, draw_handle: &mut RaylibDrawHandle, + thread: &RaylibThread, audio_system: &mut AudioPlayer, game_core: &mut GameCore, ) -> Option { - // Clear frame draw_handle.clear_background(Color::RED); diff --git a/src/logic/screen.rs b/src/logic/screen.rs index e89d603..c8d7040 100644 --- a/src/logic/screen.rs +++ b/src/logic/screen.rs @@ -1,12 +1,16 @@ -use raylib::prelude::RaylibDrawHandle; +use raylib::{prelude::RaylibDrawHandle, RaylibThread}; -use crate::{gamecore::{GameCore, GameState}, lib::wrappers::audio::player::AudioPlayer}; +use crate::{ + gamecore::{GameCore, GameState}, + lib::wrappers::audio::player::AudioPlayer, +}; /// A trait describing all game screens pub trait Screen { fn render( &mut self, draw_handle: &mut RaylibDrawHandle, + thread: &RaylibThread, audio_system: &mut AudioPlayer, game_core: &mut GameCore, ) -> Option; diff --git a/src/main.rs b/src/main.rs index 02fc2fc..8038e24 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod resources; use gamecore::{GameCore, GameState}; use lib::{utils::profiler::GameProfiler, wrappers::audio::player::AudioPlayer}; +use log::{debug, info}; use logic::{loadingscreen::LoadingScreen, mainmenu::MainMenuScreen, screen::Screen}; use raylib::prelude::*; @@ -17,6 +18,9 @@ const WINDOW_TITLE: &str = r"Ludum Dare 48"; const MAX_FPS: u32 = 60; fn main() { + // Configure the logger + env_logger::init(); + // Configure a window let (mut raylib, raylib_thread) = raylib::init() .size( @@ -31,7 +35,7 @@ fn main() { raylib.set_exit_key(None); // Set up the game's core state - let mut game_core = GameCore::new(); + let mut game_core = GameCore::new(&mut raylib, &raylib_thread); // Set up the game's profiler let mut profiler = GameProfiler::new(); @@ -50,17 +54,24 @@ fn main() { // Call appropriate render function let new_state: Option = match game_core.state { - GameState::Loading => { - loading_screen.render(&mut draw_handle, &mut audio_system, &mut game_core) - } - GameState::MainMenu => { - main_menu_screen.render(&mut draw_handle, &mut audio_system, &mut game_core) - } + GameState::Loading => loading_screen.render( + &mut draw_handle, + &raylib_thread, + &mut audio_system, + &mut game_core, + ), + GameState::MainMenu => main_menu_screen.render( + &mut draw_handle, + &raylib_thread, + &mut audio_system, + &mut game_core, + ), }; if new_state.is_some() { game_core.state = new_state.unwrap(); game_core.last_state_change_time = draw_handle.get_time(); + debug!("Switching global state to: {}", game_core.state); } // Feed the profiler diff --git a/src/resources.rs b/src/resources.rs index 16e485b..9ca9a7d 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -1,11 +1,19 @@ use failure::Error; +use raylib::{RaylibHandle, RaylibThread, texture::{Image, Texture2D}}; /// This struct contains all textures and sounds that must be loaded into (V)RAM at the start of the game -pub struct GlobalResources {} +pub struct GlobalResources { + + // Branding + pub game_logo: Texture2D + +} impl GlobalResources { /// Load all resources. **THIS WILL HANG!** - pub fn load_all() -> Result { - Ok(GlobalResources {}) + pub fn load_all(raylib: &mut RaylibHandle, thread: &RaylibThread) -> Result { + Ok(GlobalResources { + game_logo: raylib.load_texture_from_image(&thread, &Image::load_image("./assets/img/logos/game-logo.png")?)? + }) } }