debugging and more screens
This commit is contained in:
parent
c551f0be0a
commit
64bfd6fbe6
@ -2,15 +2,20 @@
|
|||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use raylib::{RaylibHandle, RaylibThread};
|
use raylib::{
|
||||||
|
camera::Camera2D, math::Vector2, prelude::RaylibDrawHandle, RaylibHandle, RaylibThread,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::resources::GlobalResources;
|
use crate::resources::GlobalResources;
|
||||||
|
|
||||||
|
use log::debug;
|
||||||
|
|
||||||
/// Overall states for the game
|
/// Overall states for the game
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
||||||
pub enum GameState {
|
pub enum GameState {
|
||||||
Loading,
|
Loading,
|
||||||
MainMenu,
|
MainMenu,
|
||||||
|
PauseMenu,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for GameState {
|
impl fmt::Display for GameState {
|
||||||
@ -23,20 +28,47 @@ impl fmt::Display for GameState {
|
|||||||
pub struct GameCore {
|
pub struct GameCore {
|
||||||
/// The game's overall state
|
/// The game's overall state
|
||||||
pub state: GameState,
|
pub state: GameState,
|
||||||
|
pub last_state: GameState,
|
||||||
pub last_state_change_time: f64,
|
pub last_state_change_time: f64,
|
||||||
pub has_rendered_first_frame: bool,
|
pub has_rendered_first_frame: bool,
|
||||||
|
|
||||||
/// Resources
|
/// Resources
|
||||||
pub resources: GlobalResources,
|
pub resources: GlobalResources,
|
||||||
|
|
||||||
|
/// Camera (more than one maybe?)
|
||||||
|
pub master_camera: Camera2D,
|
||||||
|
|
||||||
|
/// Debug features
|
||||||
|
pub show_simple_debug_info: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GameCore {
|
impl GameCore {
|
||||||
pub fn new(raylib: &mut RaylibHandle, thread: &RaylibThread) -> Self {
|
pub fn new(raylib: &mut RaylibHandle, thread: &RaylibThread) -> Self {
|
||||||
Self {
|
Self {
|
||||||
state: GameState::Loading,
|
state: GameState::Loading,
|
||||||
|
last_state: GameState::Loading,
|
||||||
last_state_change_time: 0.0,
|
last_state_change_time: 0.0,
|
||||||
has_rendered_first_frame: false,
|
has_rendered_first_frame: false,
|
||||||
resources: GlobalResources::load_all(raylib, thread).expect("Failed to load game assets. Can not launch!"),
|
resources: GlobalResources::load_all(raylib, thread)
|
||||||
|
.expect("Failed to load game assets. Can not launch!"),
|
||||||
|
master_camera: Camera2D {
|
||||||
|
offset: Vector2::zero(),
|
||||||
|
target: Vector2::zero(),
|
||||||
|
rotation: 0.0,
|
||||||
|
zoom: 1.0,
|
||||||
|
},
|
||||||
|
show_simple_debug_info: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn switch_state(&mut self, new_state: GameState, draw_handle: Option<&RaylibDrawHandle>) {
|
||||||
|
debug!("Switching global state to: {}", new_state);
|
||||||
|
|
||||||
|
self.last_state = self.state;
|
||||||
|
self.state = new_state;
|
||||||
|
|
||||||
|
if draw_handle.is_some() {
|
||||||
|
self.last_state_change_time = draw_handle.as_ref().unwrap().get_time();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,11 @@ impl Screen for MainMenuScreen {
|
|||||||
audio_system: &mut AudioPlayer,
|
audio_system: &mut AudioPlayer,
|
||||||
game_core: &mut GameCore,
|
game_core: &mut GameCore,
|
||||||
) -> Option<GameState> {
|
) -> Option<GameState> {
|
||||||
|
|
||||||
// Clear frame
|
// Clear frame
|
||||||
draw_handle.clear_background(Color::RED);
|
draw_handle.clear_background(Color::WHITE);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
pub mod screen;
|
pub mod screen;
|
||||||
pub mod loadingscreen;
|
pub mod loadingscreen;
|
||||||
pub mod mainmenu;
|
pub mod mainmenu;
|
||||||
|
pub mod pausemenu;
|
||||||
|
pub mod ingame;
|
34
src/logic/pausemenu.rs
Normal file
34
src/logic/pausemenu.rs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
use raylib::prelude::*;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
gamecore::{GameCore, GameState},
|
||||||
|
lib::wrappers::audio::player::AudioPlayer,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::screen::Screen;
|
||||||
|
|
||||||
|
pub struct PauseMenuScreen {}
|
||||||
|
|
||||||
|
impl PauseMenuScreen {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Screen for PauseMenuScreen {
|
||||||
|
fn render(
|
||||||
|
&mut self,
|
||||||
|
draw_handle: &mut RaylibDrawHandle,
|
||||||
|
thread: &RaylibThread,
|
||||||
|
audio_system: &mut AudioPlayer,
|
||||||
|
game_core: &mut GameCore,
|
||||||
|
) -> Option<GameState> {
|
||||||
|
|
||||||
|
// Clear frame
|
||||||
|
draw_handle.clear_background(Color::WHITE);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
36
src/main.rs
36
src/main.rs
@ -5,8 +5,10 @@ mod resources;
|
|||||||
|
|
||||||
use gamecore::{GameCore, GameState};
|
use gamecore::{GameCore, GameState};
|
||||||
use lib::{utils::profiler::GameProfiler, wrappers::audio::player::AudioPlayer};
|
use lib::{utils::profiler::GameProfiler, wrappers::audio::player::AudioPlayer};
|
||||||
use log::{debug, info};
|
use logic::{
|
||||||
use logic::{loadingscreen::LoadingScreen, mainmenu::MainMenuScreen, screen::Screen};
|
loadingscreen::LoadingScreen, mainmenu::MainMenuScreen, pausemenu::PauseMenuScreen,
|
||||||
|
screen::Screen,
|
||||||
|
};
|
||||||
use raylib::prelude::*;
|
use raylib::prelude::*;
|
||||||
|
|
||||||
// Game Launch Configuration
|
// Game Launch Configuration
|
||||||
@ -47,6 +49,7 @@ fn main() {
|
|||||||
// Create all the game screens
|
// Create all the game screens
|
||||||
let mut loading_screen = LoadingScreen::new();
|
let mut loading_screen = LoadingScreen::new();
|
||||||
let mut main_menu_screen = MainMenuScreen::new();
|
let mut main_menu_screen = MainMenuScreen::new();
|
||||||
|
let mut pause_menu_screen = PauseMenuScreen::new();
|
||||||
|
|
||||||
// Main rendering loop
|
// Main rendering loop
|
||||||
while !raylib.window_should_close() {
|
while !raylib.window_should_close() {
|
||||||
@ -66,12 +69,17 @@ fn main() {
|
|||||||
&mut audio_system,
|
&mut audio_system,
|
||||||
&mut game_core,
|
&mut game_core,
|
||||||
),
|
),
|
||||||
|
GameState::PauseMenu => pause_menu_screen.render(
|
||||||
|
&mut draw_handle,
|
||||||
|
&raylib_thread,
|
||||||
|
&mut audio_system,
|
||||||
|
&mut game_core,
|
||||||
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// If needed, update the global state
|
||||||
if new_state.is_some() {
|
if new_state.is_some() {
|
||||||
game_core.state = new_state.unwrap();
|
game_core.switch_state(new_state.unwrap(), Some(&draw_handle));
|
||||||
game_core.last_state_change_time = draw_handle.get_time();
|
|
||||||
debug!("Switching global state to: {}", game_core.state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Feed the profiler
|
// Feed the profiler
|
||||||
@ -90,6 +98,24 @@ fn main() {
|
|||||||
profiler.update();
|
profiler.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Debug key
|
||||||
|
if draw_handle.is_key_pressed(KeyboardKey::KEY_F3) {
|
||||||
|
game_core.show_simple_debug_info = !game_core.show_simple_debug_info;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle showing some simple debug info if needed
|
||||||
|
if game_core.show_simple_debug_info {
|
||||||
|
draw_handle.draw_text(
|
||||||
|
&format!("FPS: {}", draw_handle.get_fps()),
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
20,
|
||||||
|
Color::RED,
|
||||||
|
);
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
draw_handle.draw_text("DEBUG BUILD", 0, 20, 20, Color::RED);
|
||||||
|
}
|
||||||
|
|
||||||
// Set the first frame flag
|
// Set the first frame flag
|
||||||
game_core.has_rendered_first_frame = true;
|
game_core.has_rendered_first_frame = true;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user