Skip to content

Display combo while game playing #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions game/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use const_format::concatcp;
use sdl2::pixels::Color;

// file path
// asset path
Expand Down Expand Up @@ -44,3 +45,19 @@ pub const ACCURACY_DISPLAY_DURATION: u32 = 800;

// default bpm
pub const DEFAULT_BPM: u32 = 4;

// font size
pub const DEFAULT_FONT_SIZE: u16 = 20;
pub const CREDIT_FONT_SIZE: u16 = 30;
pub const GAME_RESULT_FONT_SIZE: u16 = 35;
pub const COMBO_FONT_SIZE: u16 = 40;

// font outline size
pub const DEFAULT_FONT_OUTLINE_SIZE: u16 = 4;

// font color
pub const DEFAULT_FONT_COLOR: Color = Color::WHITE;
pub const SELECT_SONG_FONT_COLOR: Color = Color::BLACK;

// font outline color
pub const DEFAULT_FONT_OUTLINE_COLOR: Color = Color::BLACK;
13 changes: 8 additions & 5 deletions game/src/game/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use sdl2::{event::Event, keyboard::Keycode, pixels::Color, rect::Rect, render::T
use super::{
game_common_context::GameCommonContext, util::create_outlined_font_texture::create_font_texture,
};
use crate::constants::DEFAULT_FONT_PATH as FONT_PATH;
use crate::constants::{CREDIT_FONT_SIZE, DEFAULT_FONT_OUTLINE_SIZE};
use crate::constants::{
DEFAULT_FONT_COLOR, DEFAULT_FONT_OUTLINE_COLOR, DEFAULT_FONT_PATH as FONT_PATH,
};

pub(crate) fn event_loop_common(event: &Event, coins: &mut u32) -> bool {
match event {
Expand Down Expand Up @@ -51,10 +54,10 @@ pub(crate) fn render_common(context: &mut GameCommonContext) {
&texture_creator,
&mut font,
text.as_str(),
2,
0,
Color::WHITE,
Some(Color::GRAY),
CREDIT_FONT_SIZE,
DEFAULT_FONT_OUTLINE_SIZE,
DEFAULT_FONT_COLOR,
Some(DEFAULT_FONT_OUTLINE_COLOR),
)
.unwrap();

Expand Down
13 changes: 8 additions & 5 deletions game/src/game/display_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use sdl2::{
video::Window,
};

use crate::constants::DEFAULT_FONT_PATH as FONT_PATH;
use crate::constants::{
DEFAULT_FONT_COLOR, DEFAULT_FONT_OUTLINE_COLOR, DEFAULT_FONT_PATH as FONT_PATH,
};
use crate::constants::{DEFAULT_FONT_OUTLINE_SIZE, GAME_RESULT_FONT_SIZE};

use super::{
common::{event_loop_common, render_common},
Expand Down Expand Up @@ -47,10 +50,10 @@ fn render_game_result(
&texture_creator,
&font,
text,
35,
2,
Color::WHITE,
Some(Color::GRAY),
GAME_RESULT_FONT_SIZE,
DEFAULT_FONT_OUTLINE_SIZE,
DEFAULT_FONT_COLOR,
Some(DEFAULT_FONT_OUTLINE_COLOR),
)
.unwrap();

Expand Down
11 changes: 8 additions & 3 deletions game/src/game/game_player/chart_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct ChartPlayer<'a> {
ui: ChartPlayerUI<'a>,
processed_notes: Vec<ProcessedNote>,
accuracy: Option<(NoteAccuracy, i128)>,
combo: Option<u64>,
}

impl ChartPlayer<'_> {
Expand All @@ -42,6 +43,7 @@ impl ChartPlayer<'_> {
ui: ChartPlayerUI::new(texture_creator),
processed_notes: vec![],
accuracy: None,
combo: None,
}
}

Expand Down Expand Up @@ -157,15 +159,18 @@ impl ChartPlayer<'_> {
overall_tick: u128,
janggu_state_with_tick: &JangguStateWithTick,
) {
// set ui accuracy effect
// set ui accuracy and combo effect
self.ui.accuracy = None;
self.ui.accuracy_time_progress = None;
self.ui.combo = None;
self.ui.accuracy_and_combo_time_progress = None;
if let Some(accuracy) = self.accuracy {
if accuracy.1.abs_diff(tick) > ACCURACY_DISPLAY_DURATION.into() {
self.accuracy = None;
self.combo = None;
} else {
self.ui.accuracy = Some(accuracy.0);
self.ui.accuracy_time_progress =
self.ui.combo = Some(self.timing_judge.get_game_result().combo);
self.ui.accuracy_and_combo_time_progress =
Some(accuracy.1.abs_diff(tick) as f32 / ACCURACY_DISPLAY_DURATION as f32)
}
}
Expand Down
57 changes: 50 additions & 7 deletions game/src/game/game_player/chart_player_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ use num_rational::Rational32;
use sdl2::{
pixels::Color,
rect::Rect,
render::{Canvas, TextureCreator},
render::{Canvas, TextureCreator, TextureQuery},
video::{Window, WindowContext},
};

use crate::constants::{NOTE_ACCURACY_WIDTH, NOTE_HEIGHT};
use crate::{
constants::{
COMBO_FONT_SIZE, DEFAULT_FONT_COLOR, DEFAULT_FONT_OUTLINE_COLOR, DEFAULT_FONT_OUTLINE_SIZE,
NOTE_ACCURACY_WIDTH, NOTE_HEIGHT,
},
game::util::create_outlined_font_texture::create_font_texture,
};

use bidrum_data_struct_lib::janggu::{JangguFace, JangguStick};

Expand All @@ -26,7 +32,8 @@ use super::timing_judge::NoteAccuracy;
pub struct ChartPlayerUI<'a> {
pub notes: Vec<DisplayedSongNote>,
pub accuracy: Option<NoteAccuracy>,
pub accuracy_time_progress: Option<f32>,
pub combo: Option<u64>,
pub accuracy_and_combo_time_progress: Option<f32>,
pub input_effect: InputEffect,
pub overall_effect_tick: u128,
pub disappearing_note_effects: DisapearingNoteEffect,
Expand All @@ -44,7 +51,8 @@ impl ChartPlayerUI<'_> {
return ChartPlayerUI {
notes: vec![],
accuracy: None,
accuracy_time_progress: None,
combo: None,
accuracy_and_combo_time_progress: None,
input_effect: InputEffect::new(),
overall_effect_tick: 0,
disappearing_note_effects: DisapearingNoteEffect::new(),
Expand Down Expand Up @@ -372,11 +380,13 @@ impl ChartPlayerUI<'_> {
(viewport.height() - background_height_with_border) as i32 / 2 - (height / 2);
let y_end = y_start - height as i32 - 10;
let y = y_start
+ ((y_end - y_start) as f32 * expo_out(self.accuracy_time_progress.unwrap()))
+ ((y_end - y_start) as f32
* expo_out(self.accuracy_and_combo_time_progress.unwrap()))
as i32;

accuracy_texture
.set_alpha_mod((expo_out(self.accuracy_time_progress.unwrap()) * 255.0) as u8);
accuracy_texture.set_alpha_mod(
(expo_out(self.accuracy_and_combo_time_progress.unwrap()) * 255.0) as u8,
);

canvas
.copy(
Expand All @@ -386,5 +396,38 @@ impl ChartPlayerUI<'_> {
)
.unwrap();
}

// draw combo
if let Some(combo) = self.combo {
let texture_creator = &canvas.texture_creator();
let mut combo_texture = create_font_texture(
texture_creator,
&self.resources.combo_font,
&format!("COMBO: {}", combo),
COMBO_FONT_SIZE,
DEFAULT_FONT_OUTLINE_SIZE,
DEFAULT_FONT_COLOR,
Some(DEFAULT_FONT_OUTLINE_COLOR),
)
.unwrap();

let TextureQuery { width, height, .. } = combo_texture.query();
let x = (viewport.width() - width) as i32 / 2;
let y_start = (viewport.height() - background_height_with_border) as i32 / 2
- (height as i32 / 2);
let y_end = y_start - height as i32 + 300;
let y = y_start
+ ((y_end - y_start) as f32
* expo_out(self.accuracy_and_combo_time_progress.unwrap()))
as i32;

combo_texture.set_alpha_mod(
(expo_out(self.accuracy_and_combo_time_progress.unwrap()) * 255.0) as u8,
);

canvas
.copy(&combo_texture, None, Rect::new(x, y, width, height))
.unwrap();
}
}
}
11 changes: 10 additions & 1 deletion game/src/game/game_player/chart_player_ui/resources.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use cairo::freetype::Face;
use sdl2::{
image::LoadTexture,
render::{Texture, TextureCreator},
video::WindowContext,
};

use crate::constants::DEFAULT_FONT_PATH as FONT_PATH;
use crate::constants::DEFAULT_IMG_PATH as IMG_PATH;

pub struct NoteTextures<'a> {
Expand All @@ -21,8 +23,9 @@ pub struct AccuracyTextures<'a> {
pub struct ChartPlayerUIResources<'a> {
pub judgement_line_texture: Texture<'a>,
pub note_textures: NoteTextures<'a>,
pub accuray_textures: AccuracyTextures<'a>,
pub janggu_texture: Texture<'a>,
pub accuray_textures: AccuracyTextures<'a>,
pub combo_font: Face,
}

fn load_note_textures(
Expand Down Expand Up @@ -57,12 +60,18 @@ impl ChartPlayerUIResources<'_> {
.load_texture(IMG_PATH.to_owned() + "/play_ui/janggu.png")
.expect("Failed to load janggu image");
let accuray_textures = load_accuracy_textures(texture_creator).unwrap();
let library =
cairo::freetype::Library::init().expect("Failed to initialize FreeType library");
let combo_font = library
.new_face(FONT_PATH.to_owned() + "/coin.ttf", 0)
.expect("Failed to load combo font");

return ChartPlayerUIResources {
judgement_line_texture: judgement_line_texture,
note_textures: note_textures,
janggu_texture: janggu_texture,
accuray_textures: accuray_textures,
combo_font: combo_font,
};
}
}
5 changes: 3 additions & 2 deletions game/src/game/select_song.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use sdl2::{

use crate::constants::DEFAULT_FONT_PATH as FONT_PATH;
use crate::constants::DEFAULT_IMG_PATH as IMG_PATH;
use crate::constants::SELECT_SONG_FONT_COLOR;

use super::util::create_outlined_font_texture::create_font_texture;
use super::{
Expand Down Expand Up @@ -292,7 +293,7 @@ pub(crate) fn select_song(
&song_selection_items[real_song_selection_idx as usize].title,
title_font_size,
0,
Color::BLACK,
SELECT_SONG_FONT_COLOR,
None,
)
.unwrap();
Expand All @@ -317,7 +318,7 @@ pub(crate) fn select_song(
&song_selection_items[real_song_selection_idx as usize].artist,
artist_font_size,
0,
Color::BLACK,
SELECT_SONG_FONT_COLOR,
None,
)
.unwrap();
Expand Down
3 changes: 2 additions & 1 deletion game/src/game/title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sdl2::{
video::Window,
};

use crate::constants::DEFAULT_FONT_COLOR;
use crate::constants::DEFAULT_FONT_PATH as FONT_PATH;
use crate::constants::DEFAULT_IMG_PATH as IMG_PATH;
use crate::constants::DEFAULT_VIDEO_PATH as VIDEO_PATH;
Expand Down Expand Up @@ -77,7 +78,7 @@ fn render_text(common_context: &mut GameCommonContext) {
},
35,
2,
Color::WHITE,
DEFAULT_FONT_COLOR,
Some(Color::RGB(160, 160, 160)),
)
.expect("Font rendering failure");
Expand Down
17 changes: 13 additions & 4 deletions game/src/game/util/confirm_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use sdl2::{
render::Texture,
};

use crate::constants::DEFAULT_FONT_COLOR;
use crate::constants::DEFAULT_FONT_PATH as FONT_PATH;

use crate::game::util::create_outlined_font_texture::measure_text_size;
use crate::{
constants::DEFAULT_DIALOG_PATH as DIALOG_PATH,
Expand Down Expand Up @@ -69,7 +71,7 @@ macro_rules! create_button_texture {
$button_text,
$button_font_size,
0,
Color::WHITE,
DEFAULT_FONT_COLOR,
None,
)
.expect("Failed to render font");
Expand Down Expand Up @@ -109,7 +111,6 @@ pub fn render_confirm_dialog(
// Set font-size, line-height and load font
let font_size = 38;
let button_font_size = 32;
let font_color = Color::WHITE;
let line_height = 40;
let font = common_context
.freetype_library
Expand All @@ -125,8 +126,16 @@ pub fn render_confirm_dialog(
.split('\n')
.into_iter()
.map(|x| {
create_font_texture(&texture_creator, &font, x, font_size, 0, font_color, None)
.expect("Failed to render text")
create_font_texture(
&texture_creator,
&font,
x,
font_size,
0,
DEFAULT_FONT_COLOR,
None,
)
.expect("Failed to render text")
})
.collect();

Expand Down
Loading