Add player name entry, dedicated menu music, and original Bullfrog menu fonts

This commit is contained in:
Luxferre committed 2026-11-30 23:41:37 +02:00
1 parent c017d27b2a
commit 6c24f715a0
13 files changed
+835 -199

No files matched your search

+33 -28
View File
@@ -18,6 +18,7 @@ int map_track_slot(MusicTrackId id) {
case MusicTrackId::Game4: return 2;
case MusicTrackId::TitleIntro: return 3;
case MusicTrackId::Game3: return 4;
case MusicTrackId::Menu: return 3;
}
return 0;
}
@@ -29,6 +30,7 @@ const char* track_stem(MusicTrackId id) {
case MusicTrackId::Game2: return "tgame2";
case MusicTrackId::Game3: return "tgame3";
case MusicTrackId::Game4: return "tgame4";
case MusicTrackId::Menu: return "tmenu";
}
return "tgame1";
}
@@ -77,8 +79,8 @@ bool AudioEngine::init(const Vfs& vfs, const GameConfig& cfg, bool open_device)
}
}
// Optional custom WAV music tracks in <pref_dir>/music/*.wav
for (int i = 0; i < 5; ++i) {
// Optional custom WAV music tracks in <pref_dir>/music/*.wav (including optional tmenu.wav)
for (int i = 0; i < 6; ++i) {
MusicTrackId tid = static_cast<MusicTrackId>(i);
std::filesystem::path wp = vfs.pref_dir() / "music" / (std::string(track_stem(tid)) + ".wav");
std::vector<uint8_t> wdata;
@@ -86,6 +88,11 @@ bool AudioEngine::init(const Vfs& vfs, const GameConfig& cfg, bool open_device)
assets::decode_wav_bytes(wdata.data(), wdata.size(), custom_wav_tracks_[i]);
}
}
if (custom_wav_tracks_[static_cast<int>(MusicTrackId::Menu)].pcm_mono.empty() &&
!custom_wav_tracks_[static_cast<int>(MusicTrackId::TitleIntro)].pcm_mono.empty()) {
custom_wav_tracks_[static_cast<int>(MusicTrackId::Menu)] =
custom_wav_tracks_[static_cast<int>(MusicTrackId::TitleIntro)];
}
opl_synth_.set_timbre_bank(&opl_bank_);
opl_synth_.set_sample_rate(kSampleRate);
@@ -237,6 +244,26 @@ bool AudioEngine::push_command(const AudioCommand& cmd) {
return true;
}
void AudioEngine::start_active_track() {
opl_synth_.reset();
sf_synth_.reset();
custom_music_playing_ = false;
custom_music_pos_ = 0.0;
int idx = std::clamp(static_cast<int>(active_track_), 0, 5);
int slot = map_track_slot(active_track_);
if (active_backend_ == MusicBackend::Custom && !custom_wav_tracks_[idx].pcm_mono.empty()) {
xmi_player_.stop();
custom_music_playing_ = true;
} else {
uint32_t start_tick = (active_track_ == MusicTrackId::Menu) ? 1610u : 0u;
const assets::XmiSequence* seq = (active_backend_ == MusicBackend::SoundFont)
? &wt_seqs_[slot]
: &opl_seqs_[slot];
xmi_player_.set_sequence(seq, active_loop_, start_tick);
}
}
void AudioEngine::drain_commands() {
uint32_t tail = cmd_tail_.load(std::memory_order_relaxed);
uint32_t head = cmd_head_.load(std::memory_order_acquire);
@@ -298,20 +325,7 @@ void AudioEngine::drain_commands() {
case CmdType::PlayMusic: {
active_track_ = cmd.track;
active_loop_ = cmd.loop;
int slot = map_track_slot(cmd.track);
opl_synth_.reset();
sf_synth_.reset();
custom_music_playing_ = false;
custom_music_pos_ = 0.0;
if (active_backend_ == MusicBackend::Custom &&
!custom_wav_tracks_[static_cast<int>(cmd.track)].pcm_mono.empty()) {
xmi_player_.stop();
custom_music_playing_ = true;
} else if (active_backend_ == MusicBackend::SoundFont) {
xmi_player_.set_sequence(&wt_seqs_[slot], cmd.loop);
} else {
xmi_player_.set_sequence(&opl_seqs_[slot], cmd.loop);
}
start_active_track();
break;
}
case CmdType::StopMusic:
@@ -329,17 +343,7 @@ void AudioEngine::drain_commands() {
sf_synth_.reset();
custom_music_playing_ = false;
if (was_playing) {
int slot = map_track_slot(active_track_);
if (active_backend_ == MusicBackend::Custom &&
!custom_wav_tracks_[static_cast<int>(active_track_)].pcm_mono.empty()) {
xmi_player_.stop();
custom_music_playing_ = true;
custom_music_pos_ = 0.0;
} else if (active_backend_ == MusicBackend::SoundFont) {
xmi_player_.set_sequence(&wt_seqs_[slot], active_loop_);
} else {
xmi_player_.set_sequence(&opl_seqs_[slot], active_loop_);
}
start_active_track();
}
}
break;
@@ -368,7 +372,8 @@ void AudioEngine::render_internal(float* stereo_out, int frames) {
// 1. Render Music into output chunk
if (mvol > 0.001f) {
if (custom_music_playing_) {
const auto& tr = custom_wav_tracks_[static_cast<int>(active_track_)];
int tidx = std::clamp(static_cast<int>(active_track_), 0, 5);
const auto& tr = custom_wav_tracks_[tidx];
if (!tr.pcm_mono.empty()) {
double step = static_cast<double>(tr.sample_rate) / kSampleRate;
for (int f = 0; f < cur; ++f) {
+3 -1
View File
@@ -24,6 +24,7 @@ enum class MusicTrackId {
Game2 = 2, // TGAME2
Game3 = 3, // TGAME3
Game4 = 4, // TGAME4
Menu = 5, // Dedicated Menu loop (downbeat of TINTRO2 / optional tmenu.wav)
};
class AudioEngine {
@@ -101,6 +102,7 @@ private:
bool push_command(const AudioCommand& cmd);
void drain_commands();
void start_active_track();
void render_internal(float* stereo_out, int frames);
int find_sfx_index(const std::string& name, bool intro_bank) const;
static void sdl_audio_callback(void* userdata, uint8_t* stream, int len);
@@ -118,7 +120,7 @@ private:
// Pre-parsed sequences for 2 banks (Adlib .XMI and AWE32 .WTB) x 5 songs
std::array<assets::XmiSequence, 5> opl_seqs_{};
std::array<assets::XmiSequence, 5> wt_seqs_{};
std::array<assets::SoundSample, 5> custom_wav_tracks_{};
std::array<assets::SoundSample, 6> custom_wav_tracks_{};
Opl3Synth opl_synth_;
SfSynth sf_synth_;
+26 -2
View File
@@ -9,12 +9,13 @@ void XmidiPlayer::set_sample_rate(int sample_rate) {
samples_per_tick_ = static_cast<double>(sample_rate_) / 120.0;
}
void XmidiPlayer::set_sequence(const assets::XmiSequence* seq, bool loop) {
void XmidiPlayer::set_sequence(const assets::XmiSequence* seq, bool loop, uint32_t start_tick) {
seq_ = seq;
loop_ = loop;
playing_ = (seq != nullptr && !seq->events.empty());
event_cursor_ = 0;
current_tick_ = 0;
pending_start_tick_ = (seq && start_tick <= seq->total_ticks) ? start_tick : 0;
sample_accum_ = 0.0;
samples_per_tick_ = static_cast<double>(sample_rate_) / 120.0;
for (auto& n : note_offs_) n = PendingNoteOff{};
@@ -24,6 +25,7 @@ void XmidiPlayer::set_sequence(const assets::XmiSequence* seq, bool loop) {
void XmidiPlayer::stop() {
playing_ = false;
seq_ = nullptr;
pending_start_tick_ = 0;
for (auto& n : note_offs_) n = PendingNoteOff{};
loop_depth_ = 0;
}
@@ -31,6 +33,29 @@ void XmidiPlayer::stop() {
void XmidiPlayer::process_tick(IMidiSynth& synth) {
if (!seq_ || !playing_) return;
const auto& evs = seq_->events;
// Fast-forward controller/program/pitch-bend setup before requested start_tick
if (pending_start_tick_ > 0) {
while (event_cursor_ < evs.size() && evs[event_cursor_].tick < pending_start_tick_) {
const auto& ev = evs[event_cursor_++];
if (ev.status == 0xFFu) continue;
uint8_t hi = ev.status & 0xF0u;
uint8_t ch = ev.status & 0x0Fu;
if (hi == 0xB0u) {
if (ev.data1 != 116u && ev.data1 != 117u && ev.data1 != 120u && ev.data1 != 123u) {
synth.control_change(ch, ev.data1, ev.data2);
}
} else if (hi == 0xC0u) {
synth.program_change(ch, ev.data1);
} else if (hi == 0xE0u) {
int16_t bend14 = static_cast<int16_t>(((ev.data2 & 0x7Fu) << 7) | (ev.data1 & 0x7Fu)) - 8192;
synth.pitch_bend(ch, bend14);
}
}
current_tick_ = pending_start_tick_;
pending_start_tick_ = 0;
}
// Fire due note-offs
for (auto& n : note_offs_) {
if (n.active && current_tick_ >= n.off_tick) {
@@ -48,7 +73,6 @@ void XmidiPlayer::process_tick(IMidiSynth& synth) {
};
// Fire events at or before current_tick_
const auto& evs = seq_->events;
while (event_cursor_ < evs.size() && evs[event_cursor_].tick <= current_tick_) {
const auto& ev = evs[event_cursor_++];
if (ev.status == 0xFFu) {
+2 -1
View File
@@ -14,7 +14,7 @@ public:
static constexpr int kMaxActiveNotes = 128;
void set_sample_rate(int sample_rate);
void set_sequence(const assets::XmiSequence* seq, bool loop = true);
void set_sequence(const assets::XmiSequence* seq, bool loop = true, uint32_t start_tick = 0);
void stop();
bool is_playing() const { return seq_ != nullptr && playing_; }
@@ -45,6 +45,7 @@ private:
size_t event_cursor_ = 0;
uint32_t current_tick_ = 0;
uint32_t pending_start_tick_ = 0;
double samples_per_tick_ = 400.0; // 48000 / 120 Hz default
double sample_accum_ = 0.0;
+13 -1
View File
@@ -115,7 +115,19 @@ bool GameConfig::load(const std::filesystem::path& path) {
if (auto* v = get("keys.p2_fire_minigun")) keys.p2_fire_minigun = parse_int(*v, keys.p2_fire_minigun);
if (auto* v = get("keys.p2_fire_missile")) keys.p2_fire_missile = parse_int(*v, keys.p2_fire_missile);
if (auto* v = get("game.player_name")) player_name = v->substr(0, 12);
if (auto* v = get("game.player_name")) {
std::string clean;
for (char raw : trim(*v)) {
char c = static_cast<char>(std::toupper(static_cast<unsigned char>(raw)));
if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
c == ' ' || c == '-' || c == '_' || c == '.') {
clean.push_back(c);
if (clean.size() >= 12) break;
}
}
clean = trim(clean);
player_name = clean.empty() ? "PLAYER" : clean;
}
if (auto* v = get("game.language")) language = static_cast<Language>(std::clamp(parse_int(*v, 0), 0, 3));
if (auto* v = get("game.preferred_vehicle")) preferred_vehicle = std::clamp(parse_int(*v, 0), 0, 5);
if (auto* v = get("game.preferred_team")) preferred_team = std::clamp(parse_int(*v, 7), 0, 7);
+330 -134
View File
@@ -167,6 +167,7 @@ bool GameApp::init(const LaunchOptions& opts) {
}
void GameApp::shutdown() {
cancel_name_edit();
save_all_user_data();
audio_.shutdown();
input_.shutdown();
@@ -207,28 +208,105 @@ void GameApp::toggle_fullscreen() {
apply_video_settings();
}
void GameApp::ensure_music(audio::MusicTrackId track, bool loop, bool force_restart) {
int tid = static_cast<int>(track);
if (force_restart || active_music_track_ != tid) {
active_music_track_ = tid;
audio_.play_music(track, loop);
}
}
void GameApp::begin_name_edit() {
editing_player_name_ = true;
name_edit_buf_ = cfg_.player_name;
input_.start_text_input();
audio_.play_sfx("SELECT.WAV", 0.38f);
}
void GameApp::cancel_name_edit() {
if (editing_player_name_) {
editing_player_name_ = false;
input_.stop_text_input();
}
}
void GameApp::update_name_edit() {
for (char ch : input_.text_input_chars()) {
if (name_edit_buf_.size() < 12) {
name_edit_buf_.push_back(ch);
audio_.play_sfx("CHANGE.WAV", 0.22f);
}
}
if (input_.text_backspace_pressed() && !name_edit_buf_.empty()) {
name_edit_buf_.pop_back();
audio_.play_sfx("CHANGE.WAV", 0.22f);
}
if (input_.text_confirm_pressed()) {
size_t a = 0;
while (a < name_edit_buf_.size() && name_edit_buf_[a] == ' ') ++a;
size_t b = name_edit_buf_.size();
while (b > a && name_edit_buf_[b - 1] == ' ') --b;
std::string cleaned = name_edit_buf_.substr(a, b - a);
if (!cleaned.empty()) {
cfg_.player_name = cleaned;
} else if (cfg_.player_name.empty()) {
cfg_.player_name = "PLAYER";
}
if (champ_.active && !champ_.drivers.empty()) {
champ_.player_name = cfg_.player_name;
champ_.drivers[0].name = cfg_.player_name;
}
cfg_.save(vfs_.pref_dir() / "config.ini");
cancel_name_edit();
audio_.play_sfx("SELECT.WAV", 0.38f);
status_toast_ = "PILOT NAME: " + cfg_.player_name;
status_toast_timer_ = 2.0f;
} else if (input_.text_cancel_pressed()) {
cancel_name_edit();
audio_.play_sfx("CHANGE.WAV", 0.32f);
}
}
void GameApp::enter_state(ScreenState s) {
cancel_name_edit();
state_ = s;
menu_cursor_ = 0;
menu_cursor_ = (s == ScreenState::VehicleSelect) ? 1 : 0;
rebind_slot_ = -1;
input_.cancel_rebind();
if (s == ScreenState::Intro) {
in_race_session_ = false;
flic_.reset();
intro_accum_ = 0.0;
intro_turbo_played_ = false;
intro_explode_played_ = false;
audio_.set_engine_state(false, 0.0f, false, 0.0f);
audio_.play_music(audio::MusicTrackId::TitleIntro, false);
} else if (s == ScreenState::Title) {
ensure_music(audio::MusicTrackId::TitleIntro, true, true);
} else if (s == ScreenState::Title || s == ScreenState::MainMenu ||
s == ScreenState::ChampionshipMenu || s == ScreenState::TrackSelect ||
s == ScreenState::VehicleSelect || s == ScreenState::Highscores) {
in_race_session_ = false;
audio_.set_engine_state(false, 0.0f, false, 0.0f);
audio_.play_music(audio::MusicTrackId::TitleIntro, true);
} else if (s == ScreenState::MainMenu) {
if (active_music_track_ != static_cast<int>(audio::MusicTrackId::TitleIntro)) {
ensure_music(audio::MusicTrackId::Menu, true, false);
}
} else if (s == ScreenState::Options) {
if (!in_race_session_) {
audio_.set_engine_state(false, 0.0f, false, 0.0f);
if (active_music_track_ != static_cast<int>(audio::MusicTrackId::TitleIntro)) {
ensure_music(audio::MusicTrackId::Menu, true, false);
}
}
} else if (s == ScreenState::PostRace) {
in_race_session_ = false;
audio_.set_engine_state(false, 0.0f, false, 0.0f);
ensure_music(audio::MusicTrackId::Menu, true, false);
}
}
void GameApp::enter_track_viewer(int track_num) {
in_race_session_ = false;
cancel_name_edit();
selected_track_ = std::clamp(track_num, 1, 6);
if (!viewer_level_.load(vfs_, selected_track_)) return;
world_ren_.load_level(vfs_, viewer_level_, palette_);
@@ -241,10 +319,11 @@ void GameApp::enter_track_viewer(int track_num) {
viewer_yaw_ = kPi;
viewer_pitch_ = -0.25f;
state_ = ScreenState::TrackViewer;
audio_.play_music(static_cast<audio::MusicTrackId>(1 + (selected_track_ - 1) % 4), true);
ensure_music(static_cast<audio::MusicTrackId>(1 + (selected_track_ - 1) % 4), true, true);
}
void GameApp::start_race_session() {
cancel_name_edit();
assets::LevelData lvl;
if (!lvl.load(vfs_, selected_track_)) {
LOG_ERROR("Failed to load level %d", selected_track_);
@@ -278,9 +357,10 @@ void GameApp::start_race_session() {
hud_banner_timer_ = {0.0f, 0.0f};
post_race_page_ = 0;
post_race_record_flags_ = 0;
in_race_session_ = true;
state_ = ScreenState::Racing;
audio_.play_music(static_cast<audio::MusicTrackId>(1 + (selected_track_ - 1) % 4), true);
ensure_music(static_cast<audio::MusicTrackId>(1 + (selected_track_ - 1) % 4), true, true);
}
void GameApp::handle_sim_events() {
@@ -542,22 +622,30 @@ void GameApp::update_menu(float dt) {
}
if (state_ == ScreenState::VehicleSelect) {
int max_rows = (selected_mode_ == sim::GameMode::SplitScreen) ? 5 : 3;
if (editing_player_name_) {
update_name_edit();
return;
}
int max_rows = (selected_mode_ == sim::GameMode::SplitScreen) ? 6 : 4;
if (m.up) { menu_cursor_ = (menu_cursor_ + max_rows - 1) % max_rows; audio_.play_sfx("CHANGE.WAV", 0.32f); }
if (m.down) { menu_cursor_ = (menu_cursor_ + 1) % max_rows; audio_.play_sfx("CHANGE.WAV", 0.32f); }
int dir = m.right ? 1 : (m.left ? -1 : 0);
if (dir != 0) {
audio_.play_sfx("CHANGE.WAV", 0.32f);
if (menu_cursor_ == 0) p1_veh_ = (p1_veh_ + dir + 6) % 6;
else if (menu_cursor_ == 1) p1_team_ = (p1_team_ + dir + 8) % 8;
else if (selected_mode_ == sim::GameMode::SplitScreen && menu_cursor_ == 2) p2_veh_ = (p2_veh_ + dir + 6) % 6;
else if (selected_mode_ == sim::GameMode::SplitScreen && menu_cursor_ == 3) p2_team_ = (p2_team_ + dir + 8) % 8;
if (menu_cursor_ == 1) p1_veh_ = (p1_veh_ + dir + 6) % 6;
else if (menu_cursor_ == 2) p1_team_ = (p1_team_ + dir + 8) % 8;
else if (selected_mode_ == sim::GameMode::SplitScreen && menu_cursor_ == 3) p2_veh_ = (p2_veh_ + dir + 6) % 6;
else if (selected_mode_ == sim::GameMode::SplitScreen && menu_cursor_ == 4) p2_team_ = (p2_team_ + dir + 8) % 8;
}
if (m.confirm && (menu_cursor_ == max_rows - 1 || menu_cursor_ == 0)) {
if (menu_cursor_ == 0 && m.confirm) {
begin_name_edit();
} else if (m.confirm && (menu_cursor_ == max_rows - 1 || menu_cursor_ == 1)) {
audio_.play_sfx("SELECT.WAV", 0.38f);
if (selected_mode_ == sim::GameMode::Championship) {
champ_.player_name = cfg_.player_name;
champ_.player_vehicle = p1_veh_;
champ_.player_team = p1_team_;
champ_.drivers[0].name = cfg_.player_name;
champ_.drivers[0].vehicle_idx = p1_veh_;
champ_.drivers[0].team_idx = p1_team_;
}
@@ -584,6 +672,10 @@ void GameApp::update_menu(float dt) {
}
if (state_ == ScreenState::Options) {
if (editing_player_name_) {
update_name_edit();
return;
}
if (rebind_slot_ >= 0) {
int new_sc = 0;
if (input_.consume_rebound_scancode(new_sc)) {
@@ -602,8 +694,8 @@ void GameApp::update_menu(float dt) {
return;
}
// Tab switching with Q/E or Left/Right on row 0
int row_counts[4] = {7, 5, 13, 4};
// Tab switching with Left/Right on row 0
int row_counts[4] = {7, 5, 13, 5};
int rows = row_counts[static_cast<int>(opt_tab_)];
if (m.up) { menu_cursor_ = (menu_cursor_ + rows - 1) % rows; audio_.play_sfx("CHANGE.WAV", 0.32f); }
if (m.down) { menu_cursor_ = (menu_cursor_ + 1) % rows; audio_.play_sfx("CHANGE.WAV", 0.32f); }
@@ -669,22 +761,27 @@ void GameApp::update_menu(float dt) {
} else if (menu_cursor_ == 12 && (dir != 0 || m.confirm)) {
cfg_.mouse_steering = !cfg_.mouse_steering;
}
} else if (opt_tab_ == OptionsTab::Game && (dir != 0 || m.confirm)) {
audio_.play_sfx("CHANGE.WAV", 0.32f);
if (menu_cursor_ == 1) {
int l = (static_cast<int>(cfg_.language) + (dir != 0 ? dir : 1) + 4) % 4;
cfg_.language = static_cast<Language>(l);
} else if (menu_cursor_ == 2) {
cfg_.skip_intro = !cfg_.skip_intro;
} else if (menu_cursor_ == 3) {
cfg_.classic_scanlines = !cfg_.classic_scanlines;
} else if (opt_tab_ == OptionsTab::Game) {
if (menu_cursor_ == 1 && m.confirm) {
begin_name_edit();
return;
} else if (dir != 0 || m.confirm) {
audio_.play_sfx("CHANGE.WAV", 0.32f);
if (menu_cursor_ == 2) {
int l = (static_cast<int>(cfg_.language) + (dir != 0 ? dir : 1) + 4) % 4;
cfg_.language = static_cast<Language>(l);
} else if (menu_cursor_ == 3) {
cfg_.skip_intro = !cfg_.skip_intro;
} else if (menu_cursor_ == 4) {
cfg_.classic_scanlines = !cfg_.classic_scanlines;
}
}
}
if (m.back) {
cfg_.save(vfs_.pref_dir() / "config.ini");
audio_.play_sfx("SELECT.WAV", 0.38f);
enter_state(race_sim_.crafts().empty() ? ScreenState::MainMenu : ScreenState::Paused);
enter_state(in_race_session_ ? ScreenState::Paused : ScreenState::MainMenu);
}
return;
}
@@ -743,7 +840,8 @@ int GameApp::run() {
window_focused_ = true;
audio_.apply_config(cfg_, true);
}
} else if (ev.type == SDL_KEYDOWN && !ev.key.repeat && !input_.is_rebinding()) {
} else if (ev.type == SDL_KEYDOWN && !ev.key.repeat &&
!input_.is_rebinding() && !input_.is_text_input_active()) {
if (ev.key.keysym.scancode == SDL_SCANCODE_F2) {
cfg_.classic_mode = !cfg_.classic_mode;
} else if (ev.key.keysym.scancode == SDL_SCANCODE_F4) {
@@ -934,8 +1032,8 @@ void GameApp::render_frame(float alpha) {
int pv_h = win_h_ * 46 / 100;
int pv_x = win_w_ * 52 / 100;
int pv_y = win_h_ * 26 / 100;
int veh = (selected_mode_ == sim::GameMode::SplitScreen && menu_cursor_ >= 2) ? p2_veh_ : p1_veh_;
int team = (selected_mode_ == sim::GameMode::SplitScreen && menu_cursor_ >= 2) ? p2_team_ : p1_team_;
int veh = (selected_mode_ == sim::GameMode::SplitScreen && menu_cursor_ >= 3 && menu_cursor_ <= 4) ? p2_veh_ : p1_veh_;
int team = (selected_mode_ == sim::GameMode::SplitScreen && menu_cursor_ >= 3 && menu_cursor_ <= 4) ? p2_team_ : p1_team_;
float pv_scale = sim::kVehicleSpecs[std::clamp(veh, 0, 5)].visual_scale * 1.45f;
world_ren_.render_model_preview(sim::model_key_for(veh, team),
static_cast<float>(ui_time_) * 0.9f,
@@ -948,7 +1046,6 @@ void GameApp::render_hud_viewport(const sim::CraftState& craft, const render::Re
const auto& str = get_strings(cfg_.language);
int top_y = win_h_ - (vy + vh);
float s = std::clamp(static_cast<float>(vh) / 720.0f, 0.55f, 1.5f);
float ts = 2.0f * s;
// 1. Missile lock-on targeting diamond
if (craft.lock_target_id >= 0 && craft.lock_target_id < static_cast<int>(race_sim_.crafts().size())) {
@@ -973,8 +1070,10 @@ void GameApp::render_hud_viewport(const sim::CraftState& craft, const render::Re
// 2. Top-left Race Info (POS, LAP, TIME)
float pad = 16.0f * s;
ui_ren_.draw_rect(pad, top_y + pad, 230.0f * s, 74.0f * s, Vec4(0.02f, 0.04f, 0.1f, 0.68f));
ui_ren_.draw_rect_outline(pad, top_y + pad, 230.0f * s, 74.0f * s, 2.0f, Vec4(0.25f, 0.65f, 1.0f, 0.8f));
float top_box_w = 300.0f * s;
float top_box_h = 78.0f * s;
ui_ren_.draw_rect(pad, top_y + pad, top_box_w, top_box_h, Vec4(0.02f, 0.04f, 0.1f, 0.68f));
ui_ren_.draw_rect_outline(pad, top_y + pad, top_box_w, top_box_h, 2.0f, Vec4(0.25f, 0.65f, 1.0f, 0.8f));
char line1[64], line2[64];
if (selected_mode_ == sim::GameMode::DeathMatch) {
std::snprintf(line1, sizeof(line1), "POS %d/%zu KILLS %d/%d",
@@ -987,23 +1086,26 @@ void GameApp::render_hud_viewport(const sim::CraftState& craft, const render::Re
race_sim_.setup().total_laps);
}
std::snprintf(line2, sizeof(line2), "TIME %s", format_time(craft.race_time).c_str());
ui_ren_.draw_text(pad + 10.0f * s, top_y + pad + 10.0f * s, ts, line1, Vec4(1, 0.9f, 0.25f, 1));
ui_ren_.draw_text(pad + 10.0f * s, top_y + pad + 34.0f * s, ts * 0.9f, line2, Vec4(0.85f, 0.95f, 1, 1));
ui_ren_.draw_text_fit(pad + 10.0f * s, top_y + pad + 10.0f * s, top_box_w - 20.0f * s,
1.75f * s, line1, Vec4(1, 0.9f, 0.25f, 1));
ui_ren_.draw_text_fit(pad + 10.0f * s, top_y + pad + 34.0f * s, top_box_w - 20.0f * s,
1.55f * s, line2, Vec4(0.85f, 0.95f, 1, 1));
if (craft.best_lap_time > 0.0f) {
ui_ren_.draw_text(pad + 10.0f * s, top_y + pad + 54.0f * s, ts * 0.75f,
"BEST " + format_time(craft.best_lap_time), Vec4(0.4f, 1.0f, 0.6f, 1));
ui_ren_.draw_text_fit(pad + 10.0f * s, top_y + pad + 55.0f * s, top_box_w - 20.0f * s,
1.40f * s, "BEST " + format_time(craft.best_lap_time), Vec4(0.4f, 1.0f, 0.6f, 1));
}
// 3. Bottom-left Vehicle Resource Bars + Speedometer
float hud_h = 104.0f * s;
float hud_w = 330.0f * s;
float hud_h = 108.0f * s;
float hud_y = static_cast<float>(top_y + vh) - hud_h - pad;
ui_ren_.draw_rect(pad, hud_y, 290.0f * s, hud_h, Vec4(0.02f, 0.04f, 0.1f, 0.72f));
ui_ren_.draw_rect_outline(pad, hud_y, 290.0f * s, hud_h, 2.0f, Vec4(0.25f, 0.65f, 1.0f, 0.8f));
ui_ren_.draw_rect(pad, hud_y, hud_w, hud_h, Vec4(0.02f, 0.04f, 0.1f, 0.72f));
ui_ren_.draw_rect_outline(pad, hud_y, hud_w, hud_h, 2.0f, Vec4(0.25f, 0.65f, 1.0f, 0.8f));
auto draw_bar = [&](float bx, float by, const char* lbl, float val, float maxv, const Vec4& col) {
ui_ren_.draw_text(bx, by, 1.5f * s, lbl, Vec4(0.85f, 0.9f, 1.0f, 1));
float bar_x = bx + 66.0f * s;
float bar_w = 195.0f * s;
ui_ren_.draw_text_fit(bx, by, 72.0f * s, 1.45f * s, lbl, Vec4(0.85f, 0.9f, 1.0f, 1));
float bar_x = bx + 78.0f * s;
float bar_w = 215.0f * s;
float bar_h = 12.0f * s;
ui_ren_.draw_rect(bar_x, by, bar_w, bar_h, Vec4(0.12f, 0.12f, 0.18f, 0.9f));
float frac = std::clamp(val / std::max(maxv, 1.0f), 0.0f, 1.0f);
@@ -1017,7 +1119,8 @@ void GameApp::render_hud_viewport(const sim::CraftState& craft, const render::Re
char spd_buf[64];
std::snprintf(spd_buf, sizeof(spd_buf), "%3d KPH G%d M%d B%d",
kmh, craft.minigun_level, craft.missile_level, craft.booster_level);
ui_ren_.draw_text(pad + 10.0f * s, hud_y + 74.0f * s, 1.9f * s, spd_buf, Vec4(1, 1, 1, 1));
ui_ren_.draw_text_fit(pad + 10.0f * s, hud_y + 74.0f * s, hud_w - 20.0f * s,
1.65f * s, spd_buf, Vec4(1, 1, 1, 1));
// 4. Top-right Track Minimap
if (show_minimap) {
@@ -1030,13 +1133,16 @@ void GameApp::render_hud_viewport(const sim::CraftState& craft, const render::Re
float cy = static_cast<float>(top_y) + static_cast<float>(vh) * 0.25f;
if (race_sim_.countdown_active()) {
int c = static_cast<int>(std::ceil(race_sim_.countdown_timer()));
ui_ren_.draw_text_centered(cx, cy, 5.5f * s, std::to_string(c), Vec4(1.0f, 0.85f, 0.15f, 1.0f));
ui_ren_.draw_text_centered(cx, cy, 4.8f * s, std::to_string(c),
Vec4(1.0f, 1.0f, 1.0f, 1.0f), true, render::UiFont::Panel);
} else if (craft.wrong_way) {
ui_ren_.draw_text_centered(cx, cy, 3.2f * s, str.wrong_way, Vec4(1.0f, 0.2f, 0.2f, 1.0f));
ui_ren_.draw_text_centered_fit(cx, cy, static_cast<float>(vw) * 0.8f, 2.8f * s,
str.wrong_way, Vec4(1.0f, 0.2f, 0.2f, 1.0f));
} else {
int pi = (craft.player_index == 1) ? 1 : 0;
if (hud_banner_timer_[pi] > 0.0f && !hud_banner_[pi].empty()) {
ui_ren_.draw_text_centered(cx, cy, 2.8f * s, hud_banner_[pi], Vec4(1.0f, 0.92f, 0.25f, 1.0f));
ui_ren_.draw_text_centered_fit(cx, cy, static_cast<float>(vw) * 0.8f, 2.6f * s,
hud_banner_[pi], Vec4(1.0f, 0.92f, 0.25f, 1.0f));
}
}
}
@@ -1115,33 +1221,43 @@ void GameApp::render_menus() {
return;
}
// Header Title
ui_ren_.draw_text_centered(cx, 28, 3.6f, "TOP-OCTANE", Vec4(1.0f, 0.72f, 0.15f, 1));
// Header Title in authentic Bullfrog neon-green bevel font (PFONT0-1)
ui_ren_.draw_text_centered_fit(cx, 20.0f, static_cast<float>(win_w_) - 48.0f, 4.6f,
"TOP-OCTANE", Vec4(1.0f, 1.0f, 1.0f, 1.0f), true,
render::UiFont::Panel);
bool blink_cursor = std::fmod(ui_time_ * 2.5, 1.0) < 0.55;
std::string edit_display = name_edit_buf_ + (blink_cursor ? "_" : " ");
if (state_ == ScreenState::MainMenu) {
ui_ren_.draw_text_centered(cx, 74.0f, 1.8f, "PILOT: " + cfg_.player_name,
Vec4(0.45f, 0.95f, 0.65f, 1.0f));
const char* items[] = {
str.single_race, str.death_match, str.clone_race,
str.championship, str.split_screen, str.track_viewer,
str.highscores, str.options, str.quit
};
float start_y = 140.0f;
float start_y = 122.0f;
for (int i = 0; i < 9; ++i) {
bool sel = (i == menu_cursor_);
if (sel) {
ui_ren_.draw_rect(cx - 230, start_y + i * 48 - 6, 460, 34, Vec4(0.15f, 0.45f, 0.85f, 0.6f));
ui_ren_.draw_rect_outline(cx - 230, start_y + i * 48 - 6, 460, 34, 2.0f, Vec4(1, 0.85f, 0.2f, 1));
ui_ren_.draw_rect(cx - 270, start_y + i * 46 - 7, 540, 34, Vec4(0.15f, 0.45f, 0.85f, 0.6f));
ui_ren_.draw_rect_outline(cx - 270, start_y + i * 46 - 7, 540, 34, 2.0f, Vec4(1, 0.85f, 0.2f, 1));
}
ui_ren_.draw_text_centered(cx, start_y + i * 48, 2.4f, items[i],
sel ? Vec4(1, 0.95f, 0.25f, 1) : Vec4(0.82f, 0.9f, 1, 1));
ui_ren_.draw_text_centered_fit(cx, start_y + i * 46, 510.0f, 2.2f, items[i],
sel ? Vec4(1, 0.95f, 0.25f, 1) : Vec4(0.82f, 0.9f, 1, 1));
}
ui_ren_.draw_text_centered(cx, static_cast<float>(win_h_) - 32.0f, 1.5f,
"ARROWS/DPAD NAVIGATE ENTER/A SELECT F2 CLASSIC MODE F11 FULLSCREEN",
Vec4(0.6f, 0.72f, 0.88f, 1));
ui_ren_.draw_text_centered_fit(cx, static_cast<float>(win_h_) - 32.0f,
static_cast<float>(win_w_) - 40.0f, 1.45f,
"ARROWS/DPAD NAVIGATE ENTER/A SELECT F2 CLASSIC MODE F11 FULLSCREEN",
Vec4(0.6f, 0.72f, 0.88f, 1));
return;
}
if (state_ == ScreenState::ChampionshipMenu) {
ui_ren_.draw_text_centered(cx, 92, 2.6f, str.championship, Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_centered(cx, 78.0f, 2.4f, str.championship, Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_centered(cx, 114.0f, 1.75f, "PILOT: " + cfg_.player_name,
Vec4(0.45f, 0.95f, 0.65f, 1.0f));
std::string items[5] = {
"NEW CHAMPIONSHIP",
champ_.active && !champ_.is_complete()
@@ -1154,23 +1270,24 @@ void GameApp::render_menus() {
};
for (int i = 0; i < 5; ++i) {
bool sel = (i == menu_cursor_);
ui_ren_.draw_text_centered(cx, 175 + i * 48.0f, 2.2f, items[i],
sel ? Vec4(1, 0.9f, 0.2f, 1) : Vec4(0.85f, 0.9f, 1, 1));
ui_ren_.draw_text_centered_fit(cx, 170.0f + i * 48.0f, static_cast<float>(win_w_) - 80.0f,
2.15f, items[i],
sel ? Vec4(1, 0.9f, 0.2f, 1) : Vec4(0.85f, 0.9f, 1, 1));
}
if (status_toast_timer_ > 0.0f) {
ui_ren_.draw_text_centered(cx, 450, 2.2f, status_toast_, Vec4(0.3f, 1.0f, 0.5f, 1));
ui_ren_.draw_text_centered(cx, 450, 2.0f, status_toast_, Vec4(0.3f, 1.0f, 0.5f, 1));
}
return;
}
if (state_ == ScreenState::TrackSelect) {
ui_ren_.draw_text_centered(cx, 92, 2.6f, str.select_track, Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_centered(cx, 82.0f, 2.5f, str.select_track, Vec4(0.4f, 0.85f, 1, 1));
// Track map preview sprite from TRACK0-1 (full track silhouettes sit at indices 1, 3, 6, 7, 9, 11)
static constexpr int kTrackPreviewSprites[6] = {1, 3, 6, 7, 9, 11};
int preview_idx = kTrackPreviewSprites[std::clamp(selected_track_ - 1, 0, 5)];
const float box_x = cx + 120.0f;
const float box_y = 160.0f;
const float box_w = 280.0f;
const float box_x = cx + 115.0f;
const float box_y = 158.0f;
const float box_w = std::max(140.0f, std::min(280.0f, static_cast<float>(win_w_) - box_x - 24.0f));
const float box_h = 250.0f;
ui_ren_.draw_rect(box_x, box_y, box_w, box_h, Vec4(0.03f, 0.06f, 0.14f, 0.78f));
ui_ren_.draw_rect_outline(box_x, box_y, box_w, box_h, 2.0f, Vec4(0.3f, 0.8f, 1.0f, 0.85f));
@@ -1185,6 +1302,8 @@ void GameApp::render_menus() {
float sy = box_y + (box_h - draw_h) * 0.5f;
ui_ren_.draw_bank_sprite("track", preview_idx, sx, sy, sc, Vec4(0.85f, 1.0f, 1.0f, 1.0f));
}
const float left_x = std::max(36.0f, cx - 480.0f);
const float max_w = std::max(180.0f, box_x - left_x - 24.0f);
const char* tnames[6] = {
"1. AMAZON DELTA TURNPIKE", "2. TRANS-ASIA INTERSTATE", "3. SHANGHAI DRAGON",
"4. NEW CHERNOBYL CENTRAL", "5. SLAM CANYON", "6. THRAK CITY"
@@ -1198,69 +1317,105 @@ void GameApp::render_menus() {
};
for (int i = 0; i < 5; ++i) {
bool sel = (i == menu_cursor_);
ui_ren_.draw_text(cx - 380.0f, 180.0f + i * 50.0f, 2.1f, rows[i],
sel ? Vec4(1, 0.92f, 0.2f, 1) : Vec4(0.85f, 0.9f, 1, 1));
ui_ren_.draw_text_fit(left_x, 175.0f + i * 48.0f, max_w, 1.95f, rows[i],
sel ? Vec4(1, 0.92f, 0.2f, 1) : Vec4(0.85f, 0.9f, 1, 1));
}
const auto& rec = hiscores_.tracks[selected_track_ - 1];
ui_ren_.draw_text(cx - 380.0f, 460.0f, 1.8f,
"BEST LAP: " + format_time(rec.best_lap) + " (" + rec.best_lap_driver + ")",
Vec4(0.4f, 1, 0.65f, 1));
ui_ren_.draw_text(cx - 380.0f, 490.0f, 1.8f,
"BEST RACE: " + format_time(rec.best_race) + " (" + rec.best_race_driver + ")",
Vec4(0.4f, 1, 0.65f, 1));
ui_ren_.draw_text_fit(left_x, 450.0f, max_w + 180.0f, 1.75f,
"BEST LAP: " + format_time(rec.best_lap) + " (" + rec.best_lap_driver + ")",
Vec4(0.4f, 1, 0.65f, 1));
ui_ren_.draw_text_fit(left_x, 482.0f, max_w + 180.0f, 1.75f,
"BEST RACE: " + format_time(rec.best_race) + " (" + rec.best_race_driver + ")",
Vec4(0.4f, 1, 0.65f, 1));
return;
}
if (state_ == ScreenState::VehicleSelect) {
ui_ren_.draw_text_centered(cx, 92, 2.6f, str.select_car, Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_centered(cx, 82.0f, 2.5f, str.select_car, Vec4(0.4f, 0.85f, 1, 1));
const float left_x = std::max(36.0f, cx - 480.0f);
const float max_w = std::max(220.0f, static_cast<float>(win_w_) * 0.50f - left_x - 16.0f);
if (editing_player_name_) {
ui_ren_.draw_rect(left_x - 6.0f, 142.0f, max_w + 12.0f, 30.0f, Vec4(0.16f, 0.34f, 0.68f, 0.85f));
ui_ren_.draw_rect_outline(left_x - 6.0f, 142.0f, max_w + 12.0f, 30.0f, 2.0f,
Vec4(1.0f, 0.88f, 0.2f, 1.0f));
ui_ren_.draw_text_fit(left_x, 148.0f, max_w, 1.85f, "PILOT NAME: " + edit_display,
Vec4(1.0f, 0.96f, 0.3f, 1.0f));
} else {
std::string pline = "PILOT NAME: " + cfg_.player_name + (menu_cursor_ == 0 ? " [ENTER EDIT]" : "");
ui_ren_.draw_text_fit(left_x, 148.0f, max_w, 1.85f, pline,
menu_cursor_ == 0 ? Vec4(1, 0.92f, 0.2f, 1) : Vec4(0.45f, 0.95f, 0.65f, 1));
}
const auto& sp = sim::kVehicleSpecs[p1_veh_];
ui_ren_.draw_text(cx - 420.0f, 165.0f, 2.2f,
std::string("P1 VEHICLE: < ") + sp.name + " >",
menu_cursor_ == 0 ? Vec4(1, 0.92f, 0.2f, 1) : Vec4(0.85f, 0.9f, 1, 1));
ui_ren_.draw_text(cx - 420.0f, 205.0f, 2.0f,
std::string("P1 TEAM: < ") + sim::kTeamNames[p1_team_] + " >",
menu_cursor_ == 1 ? Vec4(1, 0.92f, 0.2f, 1) : Vec4(0.85f, 0.9f, 1, 1));
ui_ren_.draw_text_fit(left_x, 190.0f, max_w, 2.0f,
std::string("P1 VEHICLE: < ") + sp.name + " >",
menu_cursor_ == 1 ? Vec4(1, 0.92f, 0.2f, 1) : Vec4(0.85f, 0.9f, 1, 1));
ui_ren_.draw_text_fit(left_x, 228.0f, max_w, 1.9f,
std::string("P1 TEAM: < ") + sim::kTeamNames[p1_team_] + " >",
menu_cursor_ == 2 ? Vec4(1, 0.92f, 0.2f, 1) : Vec4(0.85f, 0.9f, 1, 1));
auto draw_stat = [&](float y, const char* lbl, int val) {
ui_ren_.draw_text(cx - 420.0f, y, 1.8f, lbl, Vec4(0.8f, 0.9f, 1, 1));
ui_ren_.draw_text_fit(left_x, y, 180.0f, 1.7f, lbl, Vec4(0.8f, 0.9f, 1, 1));
for (int b = 0; b < 10; ++b) {
Vec4 col = (b < val) ? Vec4(0.25f, 0.9f, 1.0f, 1.0f) : Vec4(0.15f, 0.2f, 0.3f, 0.8f);
ui_ren_.draw_rect(cx - 260.0f + b * 24.0f, y, 20.0f, 14.0f, col);
ui_ren_.draw_rect(left_x + 190.0f + b * 22.0f, y, 18.0f, 14.0f, col);
}
};
draw_stat(260, str.weight, sp.stat_weight);
draw_stat(290, str.armour, sp.stat_armour);
draw_stat(320, str.speed, sp.stat_speed);
draw_stat(350, str.firepower, sp.stat_firepower);
draw_stat(274, str.weight, sp.stat_weight);
draw_stat(302, str.armour, sp.stat_armour);
draw_stat(330, str.speed, sp.stat_speed);
draw_stat(358, str.firepower, sp.stat_firepower);
int start_row = 2;
int start_row = 3;
if (selected_mode_ == sim::GameMode::SplitScreen) {
const auto& sp2 = sim::kVehicleSpecs[p2_veh_];
ui_ren_.draw_text(cx - 420.0f, 400.0f, 2.0f,
std::string("P2 VEHICLE: < ") + sp2.name + " >",
menu_cursor_ == 2 ? Vec4(1, 0.92f, 0.2f, 1) : Vec4(0.85f, 0.9f, 1, 1));
ui_ren_.draw_text(cx - 420.0f, 435.0f, 2.0f,
std::string("P2 TEAM: < ") + sim::kTeamNames[p2_team_] + " >",
menu_cursor_ == 3 ? Vec4(1, 0.92f, 0.2f, 1) : Vec4(0.85f, 0.9f, 1, 1));
start_row = 4;
ui_ren_.draw_text_fit(left_x, 398.0f, max_w, 1.9f,
std::string("P2 VEHICLE: < ") + sp2.name + " >",
menu_cursor_ == 3 ? Vec4(1, 0.92f, 0.2f, 1) : Vec4(0.85f, 0.9f, 1, 1));
ui_ren_.draw_text_fit(left_x, 434.0f, max_w, 1.9f,
std::string("P2 TEAM: < ") + sim::kTeamNames[p2_team_] + " >",
menu_cursor_ == 4 ? Vec4(1, 0.92f, 0.2f, 1) : Vec4(0.85f, 0.9f, 1, 1));
start_row = 5;
}
ui_ren_.draw_text_fit(left_x, 492.0f, max_w, 2.3f, "START RACE >>",
menu_cursor_ == start_row ? Vec4(0.3f, 1.0f, 0.45f, 1) : Vec4(0.8f, 0.9f, 1, 1));
if (editing_player_name_) {
ui_ren_.draw_text_centered_fit(cx, static_cast<float>(win_h_) - 32.0f,
static_cast<float>(win_w_) - 40.0f, 1.45f,
"TYPE PILOT NAME BACKSPACE DELETE ENTER SAVE ESC CANCEL",
Vec4(1.0f, 0.9f, 0.35f, 1.0f));
} else if (status_toast_timer_ > 0.0f) {
ui_ren_.draw_text_centered(cx, static_cast<float>(win_h_) - 36.0f, 1.8f, status_toast_,
Vec4(0.3f, 1.0f, 0.5f, 1));
}
ui_ren_.draw_text(cx - 420.0f, 495.0f, 2.5f, "START RACE >>",
menu_cursor_ == start_row ? Vec4(0.3f, 1.0f, 0.45f, 1) : Vec4(0.8f, 0.9f, 1, 1));
return;
}
if (state_ == ScreenState::Highscores) {
ui_ren_.draw_text_centered(cx, 92, 2.6f, str.highscores, Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_centered(cx, 82.0f, 2.5f, str.highscores, Vec4(0.4f, 0.85f, 1, 1));
const char* tnames[6] = {"AMAZON DELTA", "TRANS-ASIA", "SHANGHAI DRAGON", "NEW CHERNOBYL", "SLAM CANYON", "THRAK CITY"};
float tbl_w = std::min(900.0f, static_cast<float>(win_w_) - 48.0f);
float c_trk = cx - tbl_w * 0.5f;
float c_lap = c_trk + tbl_w * 0.30f;
float c_rac = c_trk + tbl_w * 0.65f;
float hdr_y = 148.0f;
ui_ren_.draw_text_fit(c_trk, hdr_y, tbl_w * 0.28f, 1.7f, "TRACK", Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_fit(c_lap, hdr_y, tbl_w * 0.33f, 1.7f, "BEST LAP", Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_fit(c_rac, hdr_y, tbl_w * 0.35f, 1.7f, "BEST RACE", Vec4(0.4f, 0.85f, 1, 1));
for (int i = 0; i < 6; ++i) {
const auto& r = hiscores_.tracks[i];
char buf[128];
std::snprintf(buf, sizeof(buf), "%d. %-16s LAP %-9s (%-8s) RACE %-9s (%-8s)",
i + 1, tnames[i], format_time(r.best_lap).c_str(), r.best_lap_driver.c_str(),
format_time(r.best_race).c_str(), r.best_race_driver.c_str());
ui_ren_.draw_text(cx - 460.0f, 165.0f + i * 44.0f, 1.8f, buf, Vec4(0.9f, 0.95f, 1, 1));
float ry = 186.0f + i * 44.0f;
std::string col0 = std::to_string(i + 1) + ". " + tnames[i];
std::string col1 = format_time(r.best_lap) + " (" + r.best_lap_driver + ")";
std::string col2 = format_time(r.best_race) + " (" + r.best_race_driver + ")";
ui_ren_.draw_text_fit(c_trk, ry, tbl_w * 0.28f, 1.65f, col0, Vec4(0.9f, 0.95f, 1, 1));
ui_ren_.draw_text_fit(c_lap, ry, tbl_w * 0.33f, 1.65f, col1, Vec4(0.85f, 0.95f, 0.9f, 1));
ui_ren_.draw_text_fit(c_rac, ry, tbl_w * 0.35f, 1.65f, col2, Vec4(0.85f, 0.95f, 0.9f, 1));
}
ui_ren_.draw_text_centered(cx, 500, 1.9f, "ENTER CLEAR HISCORES ESC BACK", Vec4(1, 0.85f, 0.2f, 1));
ui_ren_.draw_text_centered_fit(cx, 500, tbl_w, 1.85f, "ENTER CLEAR HISCORES ESC BACK", Vec4(1, 0.85f, 0.2f, 1));
if (status_toast_timer_ > 0.0f) {
ui_ren_.draw_text_centered(cx, 540, 2.0f, status_toast_, Vec4(0.3f, 1, 0.5f, 1));
}
@@ -1268,10 +1423,10 @@ void GameApp::render_menus() {
}
if (state_ == ScreenState::Options) {
ui_ren_.draw_text_centered(cx, 92, 2.6f, str.options, Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_centered(cx, 82.0f, 2.5f, str.options, Vec4(0.4f, 0.85f, 1, 1));
const char* tabs[] = {"VIDEO", "AUDIO", "CONTROLS", "GAME"};
std::string tab_str = std::string("CATEGORY: < ") + tabs[static_cast<int>(opt_tab_)] + " >";
ui_ren_.draw_text_centered(cx, 145, 2.2f, tab_str,
ui_ren_.draw_text_centered(cx, 138.0f, 2.1f, tab_str,
menu_cursor_ == 0 ? Vec4(1, 0.92f, 0.2f, 1) : Vec4(0.5f, 0.85f, 1, 1));
std::vector<std::string> rows;
@@ -1302,72 +1457,113 @@ void GameApp::render_menus() {
rows.push_back(std::string("RUMBLE HAPTICS: ") + (cfg_.rumble_enabled ? "ON" : "OFF"));
rows.push_back(std::string("MOUSE STEERING: ") + (cfg_.mouse_steering ? "ON" : "OFF"));
} else {
if (editing_player_name_) {
rows.push_back("PLAYER NAME: " + edit_display);
} else {
rows.push_back("PLAYER NAME: " + cfg_.player_name + " [ENTER EDIT]");
}
rows.push_back(std::string("LANGUAGE: ") + lang_label(cfg_.language));
rows.push_back(std::string("SKIP INTRO: ") + (cfg_.skip_intro ? "ON" : "OFF"));
rows.push_back(std::string("CLASSIC SCANLINES: ") + (cfg_.classic_scanlines ? "ON" : "OFF"));
}
float step_y = (rows.size() > 7) ? 30.0f : 44.0f;
float opt_max_w = static_cast<float>(win_w_) - 64.0f;
for (size_t i = 0; i < rows.size(); ++i) {
float ry = 190.0f + static_cast<float>(i) * step_y;
bool sel = (static_cast<int>(i) + 1 == menu_cursor_);
ui_ren_.draw_text_centered(cx, 195.0f + static_cast<float>(i) * step_y, 1.9f, rows[i],
sel ? Vec4(1, 0.92f, 0.2f, 1) : Vec4(0.85f, 0.9f, 1, 1));
if (editing_player_name_ && opt_tab_ == OptionsTab::Game && i == 0) {
float bw = std::min(500.0f, opt_max_w);
ui_ren_.draw_rect(cx - bw * 0.5f, ry - 6.0f, bw, 28.0f, Vec4(0.16f, 0.34f, 0.68f, 0.85f));
ui_ren_.draw_rect_outline(cx - bw * 0.5f, ry - 6.0f, bw, 28.0f, 2.0f,
Vec4(1.0f, 0.88f, 0.2f, 1.0f));
}
ui_ren_.draw_text_centered_fit(cx, ry, opt_max_w, 1.85f, rows[i],
sel ? Vec4(1, 0.92f, 0.2f, 1) : Vec4(0.85f, 0.9f, 1, 1));
}
ui_ren_.draw_text_centered(cx, static_cast<float>(win_h_) - 32.0f, 1.5f,
"LEFT/RIGHT ADJUST ENTER REBIND/TOGGLE ESC SAVE & BACK",
Vec4(0.6f, 0.72f, 0.88f, 1));
if (status_toast_timer_ > 0.0f && !editing_player_name_) {
ui_ren_.draw_text_centered(cx, static_cast<float>(win_h_) - 64.0f, 1.8f,
status_toast_, Vec4(0.3f, 1.0f, 0.5f, 1.0f));
}
const char* footer = editing_player_name_
? "TYPE PLAYER NAME BACKSPACE DELETE ENTER SAVE ESC CANCEL"
: "LEFT/RIGHT ADJUST ENTER EDIT/TOGGLE ESC SAVE & BACK";
ui_ren_.draw_text_centered_fit(cx, static_cast<float>(win_h_) - 32.0f,
static_cast<float>(win_w_) - 40.0f, 1.45f, footer,
editing_player_name_ ? Vec4(1.0f, 0.9f, 0.35f, 1.0f)
: Vec4(0.6f, 0.72f, 0.88f, 1));
return;
}
if (state_ == ScreenState::PostRace) {
ui_ren_.draw_text_centered(cx, 92, 2.8f, str.race_over, Vec4(0.35f, 1.0f, 0.55f, 1));
ui_ren_.draw_text_centered(cx, 82.0f, 2.6f, str.race_over, Vec4(0.35f, 1.0f, 0.55f, 1));
if (post_race_record_flags_ & 1) {
ui_ren_.draw_text_centered(cx, 128, 1.9f, "NEW FASTEST LAP!", Vec4(1, 0.9f, 0.2f, 1));
ui_ren_.draw_text_centered(cx, 122.0f, 1.85f, "NEW FASTEST LAP!", Vec4(1, 0.9f, 0.2f, 1));
}
if (post_race_page_ == 0) {
auto sorted = race_sim_.crafts();
std::sort(sorted.begin(), sorted.end(), [](const auto& a, const auto& b) {
return a.placement < b.placement;
});
char hdr[96];
std::snprintf(hdr, sizeof(hdr), "%-3s %-11s %-13s %-9s %-9s %-5s",
str.position, str.driver, "TEAM", str.time_hdr, str.best_lap, "KILLS");
ui_ren_.draw_text(cx - 430.0f, 165.0f, 1.8f, hdr, Vec4(0.4f, 0.85f, 1, 1));
float tbl_w = std::min(880.0f, static_cast<float>(win_w_) - 48.0f);
float c0 = cx - tbl_w * 0.5f;
float c1 = c0 + tbl_w * 0.09f;
float c2 = c0 + tbl_w * 0.31f;
float c3 = c0 + tbl_w * 0.55f;
float c4 = c0 + tbl_w * 0.73f;
float c5 = c0 + tbl_w * 0.91f;
ui_ren_.draw_text_fit(c0, 162.0f, tbl_w * 0.08f, 1.65f, str.position, Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_fit(c1, 162.0f, tbl_w * 0.21f, 1.65f, str.driver, Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_fit(c2, 162.0f, tbl_w * 0.23f, 1.65f, "TEAM", Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_fit(c3, 162.0f, tbl_w * 0.17f, 1.65f, str.time_hdr, Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_fit(c4, 162.0f, tbl_w * 0.17f, 1.65f, str.best_lap, Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_fit(c5, 162.0f, tbl_w * 0.09f, 1.65f, "KILLS", Vec4(0.4f, 0.85f, 1, 1));
for (size_t i = 0; i < sorted.size(); ++i) {
const auto& c = sorted[i];
char row[128];
std::snprintf(row, sizeof(row), "%2d. %-11s %-13s %-9s %-9s %3d",
c.placement, c.pilot_name.c_str(), sim::kTeamNames[c.team_idx],
format_time(c.race_time).c_str(), format_time(c.best_lap_time).c_str(),
c.kills);
float ry = 198.0f + static_cast<float>(i) * 32.0f;
Vec4 col = c.is_human ? Vec4(1, 0.95f, 0.25f, 1) : Vec4(0.85f, 0.9f, 1, 1);
ui_ren_.draw_text(cx - 430.0f, 200.0f + i * 32.0f, 1.8f, row, col);
ui_ren_.draw_text_fit(c0, ry, tbl_w * 0.08f, 1.65f, std::to_string(c.placement) + ".", col);
ui_ren_.draw_text_fit(c1, ry, tbl_w * 0.21f, 1.65f, c.pilot_name, col);
ui_ren_.draw_text_fit(c2, ry, tbl_w * 0.23f, 1.65f, sim::kTeamNames[c.team_idx], col);
ui_ren_.draw_text_fit(c3, ry, tbl_w * 0.17f, 1.65f, format_time(c.race_time), col);
ui_ren_.draw_text_fit(c4, ry, tbl_w * 0.17f, 1.65f, format_time(c.best_lap_time), col);
ui_ren_.draw_text_fit(c5, ry, tbl_w * 0.09f, 1.65f, std::to_string(c.kills), col);
}
} else if (post_race_page_ == 1 && !race_sim_.crafts().empty()) {
const auto& p1 = race_sim_.crafts()[0];
ui_ren_.draw_text_centered(cx, 165, 2.2f, str.statistics, Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_centered(cx, 162.0f, 2.2f, str.statistics, Vec4(0.4f, 0.85f, 1, 1));
int acc = (p1.shots_fired > 0) ? (p1.shots_hit * 100 / p1.shots_fired) : 0;
ui_ren_.draw_text(cx - 240, 220, 2.0f, "KILLS / DEATHS: " + std::to_string(p1.kills) + " / " + std::to_string(p1.deaths));
ui_ren_.draw_text(cx - 240, 260, 2.0f, "HIT ACCURACY: " + std::to_string(acc) + "%");
ui_ren_.draw_text(cx - 240, 300, 2.0f, "FUEL USED: " + std::to_string(static_cast<int>(p1.fuel_used)));
ui_ren_.draw_text(cx - 240, 340, 2.0f, "ARMOUR USED: " + std::to_string(static_cast<int>(p1.armour_used)));
ui_ren_.draw_text(cx - 240, 220, 1.95f, "KILLS / DEATHS: " + std::to_string(p1.kills) + " / " + std::to_string(p1.deaths));
ui_ren_.draw_text(cx - 240, 260, 1.95f, "HIT ACCURACY: " + std::to_string(acc) + "%");
ui_ren_.draw_text(cx - 240, 300, 1.95f, "FUEL USED: " + std::to_string(static_cast<int>(p1.fuel_used)));
ui_ren_.draw_text(cx - 240, 340, 1.95f, "ARMOUR USED: " + std::to_string(static_cast<int>(p1.armour_used)));
if (selected_mode_ == sim::GameMode::Championship) {
ui_ren_.draw_text(cx - 240, 380, 2.0f, "RATING: " + champ_.player_rating_title(), Vec4(1, 0.9f, 0.2f, 1));
ui_ren_.draw_text(cx - 240, 380, 1.95f, "RATING: " + champ_.player_rating_title(), Vec4(1, 0.9f, 0.2f, 1));
}
} else if (selected_mode_ == sim::GameMode::Championship) {
ui_ren_.draw_text_centered(cx, 165, 2.2f, "CHAMPIONSHIP TABLE", Vec4(0.4f, 0.85f, 1, 1));
ui_ren_.draw_text_centered(cx, 162.0f, 2.2f, "CHAMPIONSHIP TABLE", Vec4(0.4f, 0.85f, 1, 1));
auto ds = champ_.drivers;
std::sort(ds.begin(), ds.end(), [](const auto& a, const auto& b) { return a.points > b.points; });
float tbl_w = std::min(700.0f, static_cast<float>(win_w_) - 48.0f);
float c0 = cx - tbl_w * 0.5f;
float c1 = c0 + tbl_w * 0.10f;
float c2 = c0 + tbl_w * 0.40f;
float c3 = c0 + tbl_w * 0.70f;
float c4 = c0 + tbl_w * 0.84f;
for (size_t i = 0; i < ds.size(); ++i) {
char row[96];
std::snprintf(row, sizeof(row), "%2zu. %-12s %-14s %3d PTS (%d WINS)",
i + 1, ds[i].name.c_str(), sim::kTeamNames[ds[i].team_idx],
ds[i].points, ds[i].wins);
ui_ren_.draw_text(cx - 320, 210 + i * 32.0f, 1.8f, row, Vec4(0.9f, 0.95f, 1, 1));
float ry = 210.0f + static_cast<float>(i) * 32.0f;
Vec4 col = (i == 0 && ds[i].name == cfg_.player_name)
? Vec4(1, 0.95f, 0.25f, 1)
: Vec4(0.9f, 0.95f, 1, 1);
ui_ren_.draw_text_fit(c0, ry, tbl_w * 0.09f, 1.7f, std::to_string(i + 1) + ".", col);
ui_ren_.draw_text_fit(c1, ry, tbl_w * 0.29f, 1.7f, ds[i].name, col);
ui_ren_.draw_text_fit(c2, ry, tbl_w * 0.29f, 1.7f, sim::kTeamNames[ds[i].team_idx], col);
ui_ren_.draw_text_fit(c3, ry, tbl_w * 0.13f, 1.7f, std::to_string(ds[i].points) + " PTS", col);
ui_ren_.draw_text_fit(c4, ry, tbl_w * 0.16f, 1.7f, std::to_string(ds[i].wins) + " W", col);
}
}
ui_ren_.draw_text_centered(cx, 520, 1.9f,
"LEFT/RIGHT STATS PAGE ENTER CONTINUE", Vec4(1, 0.85f, 0.2f, 1));
ui_ren_.draw_text_centered_fit(cx, 520.0f, static_cast<float>(win_w_) - 48.0f, 1.85f,
"LEFT/RIGHT STATS PAGE ENTER CONTINUE", Vec4(1, 0.85f, 0.2f, 1));
}
}
+10
View File
@@ -78,6 +78,10 @@ private:
void apply_video_settings();
void toggle_fullscreen();
void save_all_user_data();
void ensure_music(audio::MusicTrackId track, bool loop = true, bool force_restart = false);
void begin_name_edit();
void cancel_name_edit();
void update_name_edit();
Vfs vfs_;
GameConfig cfg_;
@@ -135,6 +139,12 @@ private:
std::array<float, 2> hud_banner_timer_{0.0f, 0.0f};
bool intro_turbo_played_ = false;
bool intro_explode_played_ = false;
// Music & player name editing state
int active_music_track_ = -1;
bool in_race_session_ = false;
bool editing_player_name_ = false;
std::string name_edit_buf_;
};
} // namespace top_octane::game
+70 -10
View File
@@ -2,6 +2,7 @@
#include "core/log.hpp"
#include <algorithm>
#include <cctype>
#include <cmath>
namespace top_octane {
@@ -38,6 +39,7 @@ bool InputManager::init() {
}
void InputManager::shutdown() {
stop_text_input();
for (int i = 0; i < kMaxGamepads; ++i) {
if (gamepads_[i]) {
SDL_GameControllerClose(gamepads_[i]);
@@ -86,10 +88,34 @@ SDL_GameController* InputManager::get_player_gamepad(int player_idx, const GameC
return gamepads_[slot];
}
void InputManager::start_text_input() {
text_input_active_ = true;
text_chars_.clear();
text_backspace_ = false;
text_confirm_ = false;
text_cancel_ = false;
SDL_StartTextInput();
}
void InputManager::stop_text_input() {
if (text_input_active_) {
text_input_active_ = false;
text_chars_.clear();
text_backspace_ = false;
text_confirm_ = false;
text_cancel_ = false;
SDL_StopTextInput();
}
}
void InputManager::begin_frame() {
prev_keys_ = curr_keys_;
prev_gp_buttons_ = curr_gp_buttons_;
menu_ = MenuInput{};
text_chars_.clear();
text_backspace_ = false;
text_confirm_ = false;
text_cancel_ = false;
}
void InputManager::handle_event(const SDL_Event& ev, GameConfig& cfg,
@@ -107,7 +133,31 @@ void InputManager::handle_event(const SDL_Event& ev, GameConfig& cfg,
close_gamepad_by_instance_id(ev.cdevice.which);
break;
case SDL_TEXTINPUT:
if (text_input_active_) {
for (const char* p = ev.text.text; *p != '\0'; ++p) {
char c = static_cast<char>(std::toupper(static_cast<unsigned char>(*p)));
if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
c == ' ' || c == '-' || c == '_' || c == '.') {
text_chars_.push_back(c);
}
}
}
break;
case SDL_KEYDOWN:
if (text_input_active_) {
SDL_Scancode sc = ev.key.keysym.scancode;
if (sc == SDL_SCANCODE_BACKSPACE) {
text_backspace_ = true;
} else if (!ev.key.repeat &&
(sc == SDL_SCANCODE_RETURN || sc == SDL_SCANCODE_KP_ENTER)) {
text_confirm_ = true;
} else if (!ev.key.repeat && sc == SDL_SCANCODE_ESCAPE) {
text_cancel_ = true;
}
return;
}
if (!ev.key.repeat) {
menu_.any_pressed = true;
SDL_Scancode sc = ev.key.keysym.scancode;
@@ -130,6 +180,14 @@ void InputManager::handle_event(const SDL_Event& ev, GameConfig& cfg,
break;
case SDL_CONTROLLERBUTTONDOWN:
if (text_input_active_) {
if (ev.cbutton.button == SDL_CONTROLLER_BUTTON_START) {
text_confirm_ = true;
} else if (ev.cbutton.button == SDL_CONTROLLER_BUTTON_B) {
text_cancel_ = true;
}
return;
}
menu_.any_pressed = true;
break;
@@ -177,16 +235,18 @@ void InputManager::end_frame(const GameConfig& cfg) {
return curr_gp_buttons_[idx] && !prev_gp_buttons_[idx];
};
menu_.up = key_just(SDL_SCANCODE_UP) || key_just(SDL_SCANCODE_W) || gp_just(SDL_CONTROLLER_BUTTON_DPAD_UP);
menu_.down = key_just(SDL_SCANCODE_DOWN) || key_just(SDL_SCANCODE_S) || gp_just(SDL_CONTROLLER_BUTTON_DPAD_DOWN);
menu_.left = key_just(SDL_SCANCODE_LEFT) || key_just(SDL_SCANCODE_A) || gp_just(SDL_CONTROLLER_BUTTON_DPAD_LEFT);
menu_.right = key_just(SDL_SCANCODE_RIGHT) || key_just(SDL_SCANCODE_D) || gp_just(SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
menu_.confirm = key_just(SDL_SCANCODE_RETURN) || key_just(SDL_SCANCODE_KP_ENTER) ||
key_just(SDL_SCANCODE_SPACE) || gp_just(SDL_CONTROLLER_BUTTON_A);
menu_.back = key_just(SDL_SCANCODE_ESCAPE) || key_just(SDL_SCANCODE_BACKSPACE) ||
gp_just(SDL_CONTROLLER_BUTTON_B);
menu_.pause = key_just(SDL_SCANCODE_ESCAPE) || key_just(SDL_SCANCODE_P) ||
gp_just(SDL_CONTROLLER_BUTTON_START);
if (!text_input_active_) {
menu_.up = key_just(SDL_SCANCODE_UP) || key_just(SDL_SCANCODE_W) || gp_just(SDL_CONTROLLER_BUTTON_DPAD_UP);
menu_.down = key_just(SDL_SCANCODE_DOWN) || key_just(SDL_SCANCODE_S) || gp_just(SDL_CONTROLLER_BUTTON_DPAD_DOWN);
menu_.left = key_just(SDL_SCANCODE_LEFT) || key_just(SDL_SCANCODE_A) || gp_just(SDL_CONTROLLER_BUTTON_DPAD_LEFT);
menu_.right = key_just(SDL_SCANCODE_RIGHT) || key_just(SDL_SCANCODE_D) || gp_just(SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
menu_.confirm = key_just(SDL_SCANCODE_RETURN) || key_just(SDL_SCANCODE_KP_ENTER) ||
key_just(SDL_SCANCODE_SPACE) || gp_just(SDL_CONTROLLER_BUTTON_A);
menu_.back = key_just(SDL_SCANCODE_ESCAPE) || key_just(SDL_SCANCODE_BACKSPACE) ||
gp_just(SDL_CONTROLLER_BUTTON_B);
menu_.pause = key_just(SDL_SCANCODE_ESCAPE) || key_just(SDL_SCANCODE_P) ||
gp_just(SDL_CONTROLLER_BUTTON_START);
}
if (cfg.mouse_steering) {
// Gentle centering decay for mouse steering
+15
View File
@@ -54,6 +54,15 @@ public:
bool is_rebinding() const { return rebind_active_; }
bool consume_rebound_scancode(int& out_scancode);
// Live text input capture for player name entry
void start_text_input();
void stop_text_input();
bool is_text_input_active() const { return text_input_active_; }
const std::string& text_input_chars() const { return text_chars_; }
bool text_backspace_pressed() const { return text_backspace_; }
bool text_confirm_pressed() const { return text_confirm_; }
bool text_cancel_pressed() const { return text_cancel_; }
// Rumble / Haptics
void trigger_rumble(int player_idx, float low_freq, float high_freq, uint32_t duration_ms,
const GameConfig& cfg);
@@ -78,6 +87,12 @@ private:
MenuInput menu_{};
bool rebind_active_ = false;
int captured_scancode_ = 0;
bool text_input_active_ = false;
std::string text_chars_;
bool text_backspace_ = false;
bool text_confirm_ = false;
bool text_cancel_ = false;
};
} // namespace top_octane
+227 -18
View File
@@ -197,14 +197,116 @@ bool UiRenderer::init(const Vfs& vfs, const assets::Palette& pal) {
load_bank(vfs, pal, "panel", "PANEL0-1");
load_bank(vfs, pal, "track", "TRACK0-1");
load_font_atlas(vfs, pal, "OLFNT0-1", font_large_);
load_font_atlas(vfs, pal, "OSFNT0-1", font_small_);
load_font_atlas(vfs, pal, "PFONT0-1", font_panel_);
initialized_ = true;
return true;
}
bool UiRenderer::load_font_atlas(const Vfs& vfs, const assets::Palette& pal,
const std::string& stem, FontAtlas& out) {
out = FontAtlas{};
assets::SpriteBank sb;
if (!sb.load(vfs, "DATA/" + stem + ".DAT", "DATA/" + stem + ".TAB", pal)) {
LOG_WARN("Bullfrog font bank failed to load: %s", stem.c_str());
return false;
}
int max_cap_h = 0;
for (int ch = '0'; ch <= 'Z'; ++ch) {
if (ch > '9' && ch < 'A') continue;
size_t idx = static_cast<size_t>(ch + 1);
if (idx < sb.sprites.size()) {
max_cap_h = std::max(max_cap_h, sb.sprites[idx].height);
}
}
out.line_h = (max_cap_h > 0) ? max_cap_h : 20;
out.space_advance = static_cast<float>(out.line_h) * 0.58f;
const int pad_px = (out.line_h >= 30) ? 5 : ((out.line_h >= 18) ? 3 : 2);
const int atlas_w = 512;
int cur_x = 2;
int cur_y = 2;
int row_h = 0;
std::array<int, 96> px_x{};
std::array<int, 96> px_y{};
std::array<bool, 96> active{};
for (int ch = 33; ch <= 95; ++ch) {
if (ch == '<' || ch == '>' || ch == '@' || ch == '_' || ch == '^') continue;
size_t idx = static_cast<size_t>(ch + 1);
if (idx >= sb.sprites.size()) continue;
const auto& sp = sb.sprites[idx];
if (sp.width <= 0 || sp.height <= 0 || sp.rgba.empty()) continue;
int maxx = -1;
for (int y = 0; y < sp.height; ++y) {
for (int x = 0; x < sp.width; ++x) {
if (sp.rgba[(static_cast<size_t>(y) * sp.width + x) * 4u + 3u] > 0) {
if (x > maxx) maxx = x;
}
}
}
if (maxx < 0) continue;
if (cur_x + sp.width + 2 > atlas_w) {
cur_x = 2;
cur_y += row_h + 2;
row_h = 0;
}
px_x[ch] = cur_x;
px_y[ch] = cur_y;
active[ch] = true;
out.glyphs[ch].w = sp.width;
out.glyphs[ch].h = sp.height;
out.glyphs[ch].advance = static_cast<float>(maxx + 1 + pad_px);
cur_x += sp.width + 2;
row_h = std::max(row_h, sp.height);
}
int used_h = cur_y + row_h + 2;
int atlas_h = std::max(128, ((used_h + 15) / 16) * 16);
std::vector<uint8_t> pixels(static_cast<size_t>(atlas_w) * static_cast<size_t>(atlas_h) * 4u, 0);
for (int ch = 33; ch <= 95; ++ch) {
if (!active[ch]) continue;
const auto& sp = sb.sprites[static_cast<size_t>(ch + 1)];
const int gx = px_x[ch];
const int gy = px_y[ch];
for (int y = 0; y < sp.height; ++y) {
for (int x = 0; x < sp.width; ++x) {
size_t src = (static_cast<size_t>(y) * sp.width + x) * 4u;
size_t dst = (static_cast<size_t>(gy + y) * atlas_w + (gx + x)) * 4u;
pixels[dst + 0] = sp.rgba[src + 0];
pixels[dst + 1] = sp.rgba[src + 1];
pixels[dst + 2] = sp.rgba[src + 2];
pixels[dst + 3] = sp.rgba[src + 3];
}
}
FontGlyph& fg = out.glyphs[ch];
fg.valid = true;
fg.u0 = static_cast<float>(gx) / static_cast<float>(atlas_w);
fg.v0 = static_cast<float>(gy) / static_cast<float>(atlas_h);
fg.u1 = static_cast<float>(gx + sp.width) / static_cast<float>(atlas_w);
fg.v1 = static_cast<float>(gy + sp.height) / static_cast<float>(atlas_h);
}
upload_rgba(out.tex, atlas_w, atlas_h, pixels.data(), true);
return out.tex != 0;
}
void UiRenderer::shutdown() {
if (!initialized_) return;
if (white_tex_) glDeleteTextures(1, &white_tex_);
if (stream_tex_) glDeleteTextures(1, &stream_tex_);
if (font_large_.tex) glDeleteTextures(1, &font_large_.tex);
if (font_small_.tex) glDeleteTextures(1, &font_small_.tex);
if (font_panel_.tex) glDeleteTextures(1, &font_panel_.tex);
font_large_ = FontAtlas{};
font_small_ = FontAtlas{};
font_panel_ = FontAtlas{};
for (auto& [k, s] : screens_) {
if (s.tex) glDeleteTextures(1, &s.tex);
}
@@ -314,37 +416,144 @@ void UiRenderer::draw_line(float x0, float y0, float x1, float y1, float thickne
}
}
float UiRenderer::measure_text_w(const std::string& text, float scale) const {
const UiRenderer::FontAtlas* UiRenderer::resolve_font(float scale, UiFont font) const {
switch (font) {
case UiFont::Pixel5x7:
return nullptr;
case UiFont::Panel:
if (font_panel_.tex) return &font_panel_;
if (font_large_.tex) return &font_large_;
return font_small_.tex ? &font_small_ : nullptr;
case UiFont::MenuLarge:
if (font_large_.tex) return &font_large_;
return font_small_.tex ? &font_small_ : nullptr;
case UiFont::MenuSmall:
if (font_small_.tex) return &font_small_;
return font_large_.tex ? &font_large_ : nullptr;
case UiFont::Auto:
default:
if (scale >= 2.15f && font_large_.tex) return &font_large_;
if (font_small_.tex) return &font_small_;
if (font_large_.tex) return &font_large_;
return nullptr;
}
}
void UiRenderer::draw_fallback_glyph(char ch, float x, float y, float cell_h, float cell_w,
const Vec4& color) {
const Glyph5x7& g = get_glyph(ch);
float px_w = cell_w / 5.0f;
float px_h = cell_h / 7.0f;
for (int r = 0; r < 7; ++r) {
uint8_t row = g.rows[r];
if (row == 0) continue;
for (int c = 0; c < 5; ++c) {
if (row & (1u << (4 - c))) {
draw_rect(x + c * px_w, y + r * px_h, px_w, px_h, color);
}
}
}
}
float UiRenderer::measure_text_w(const std::string& text, float scale, UiFont font) const {
if (text.empty()) return 0.0f;
return static_cast<float>(text.size()) * 6.0f * scale - scale;
const FontAtlas* atlas = resolve_font(scale, font);
if (!atlas) {
return static_cast<float>(text.size()) * 6.0f * scale - scale;
}
const float target_h = 8.0f * scale;
const float px_scale = target_h / static_cast<float>(std::max(atlas->line_h, 1));
float w = 0.0f;
for (char ch : text) {
uint8_t uc = static_cast<uint8_t>(std::toupper(static_cast<unsigned char>(ch)));
if (uc == ' ') {
w += atlas->space_advance * px_scale;
} else if (uc > 32 && uc < 96 && atlas->glyphs[uc].valid) {
w += atlas->glyphs[uc].advance * px_scale;
} else {
w += target_h * 0.78f;
}
}
return w;
}
float UiRenderer::measure_text_h(float scale, UiFont font) const {
const FontAtlas* atlas = resolve_font(scale, font);
return atlas ? (8.0f * scale) : (7.0f * scale);
}
void UiRenderer::draw_text(float x, float y, float scale, const std::string& text,
const Vec4& color, bool shadow) {
const Vec4& color, bool shadow, UiFont font) {
if (text.empty()) return;
const FontAtlas* atlas = resolve_font(scale, font);
if (shadow) {
Vec4 sh(0.0f, 0.0f, 0.0f, color.w * 0.78f);
draw_text(x + scale, y + scale, scale, text, sh, false);
float off = atlas ? std::max(1.5f, scale * 0.85f) : scale;
Vec4 sh(0.0f, 0.0f, 0.0f, color.w * (atlas ? 0.62f : 0.78f));
draw_text(x + off, y + off, scale, text, sh, false, font);
}
if (!atlas) {
float cx = x;
for (char ch : text) {
draw_fallback_glyph(ch, cx, y, 7.0f * scale, 5.0f * scale, color);
cx += 6.0f * scale;
}
return;
}
const float target_h = 8.0f * scale;
const float px_scale = target_h / static_cast<float>(std::max(atlas->line_h, 1));
float cx = x;
for (char ch : text) {
const Glyph5x7& g = get_glyph(ch);
for (int r = 0; r < 7; ++r) {
uint8_t row = g.rows[r];
if (row == 0) continue;
for (int c = 0; c < 5; ++c) {
if (row & (1u << (4 - c))) {
draw_rect(cx + c * scale, y + r * scale, scale, scale, color);
}
}
uint8_t uc = static_cast<uint8_t>(std::toupper(static_cast<unsigned char>(ch)));
if (uc == ' ') {
cx += atlas->space_advance * px_scale;
} else if (uc > 32 && uc < 96 && atlas->glyphs[uc].valid) {
const FontGlyph& fg = atlas->glyphs[uc];
float gw = static_cast<float>(fg.w) * px_scale;
float gh = static_cast<float>(fg.h) * px_scale;
float gy = y + (static_cast<float>(atlas->line_h - fg.h) * 0.5f) * px_scale;
push_quad(atlas->tex, cx, gy, cx + gw, gy + gh, fg.u0, fg.v0, fg.u1, fg.v1, color);
cx += fg.advance * px_scale;
} else {
float slot_w = target_h * 0.78f;
float cell_h = target_h * 0.72f;
float cell_w = cell_h * (5.0f / 7.0f);
float fx = cx + (slot_w - cell_w) * 0.5f;
float fy = y + (target_h - cell_h) * 0.5f;
draw_fallback_glyph(static_cast<char>(uc), fx, fy, cell_h, cell_w, color);
cx += slot_w;
}
cx += 6.0f * scale;
}
}
void UiRenderer::draw_text_centered(float cx, float y, float scale, const std::string& text,
const Vec4& color, bool shadow) {
float w = measure_text_w(text, scale);
draw_text(cx - w * 0.5f, y, scale, text, color, shadow);
const Vec4& color, bool shadow, UiFont font) {
float w = measure_text_w(text, scale, font);
draw_text(cx - w * 0.5f, y, scale, text, color, shadow, font);
}
void UiRenderer::draw_text_fit(float x, float y, float max_w, float scale, const std::string& text,
const Vec4& color, bool shadow, UiFont font) {
float w = measure_text_w(text, scale, font);
float eff = scale;
if (w > max_w && max_w > 1.0f) {
eff = scale * (max_w / w);
y += (measure_text_h(scale, font) - measure_text_h(eff, font)) * 0.5f;
}
draw_text(x, y, eff, text, color, shadow, font);
}
void UiRenderer::draw_text_centered_fit(float cx, float y, float max_w, float scale,
const std::string& text, const Vec4& color,
bool shadow, UiFont font) {
float w = measure_text_w(text, scale, font);
float eff = scale;
if (w > max_w && max_w > 1.0f) {
eff = scale * (max_w / w);
y += (measure_text_h(scale, font) - measure_text_h(eff, font)) * 0.5f;
}
draw_text_centered(cx, y, eff, text, color, shadow, font);
}
void UiRenderer::draw_screen_cover(const std::string& screen_key, float alpha) {
+46 -4
View File
@@ -7,12 +7,21 @@
#include "render/gl_api.hpp"
#include "render/shader.hpp"
#include <array>
#include <string>
#include <unordered_map>
#include <vector>
namespace top_octane::render {
enum class UiFont {
Auto = 0,
MenuLarge,
MenuSmall,
Panel,
Pixel5x7,
};
struct GpuSpriteTex {
GLuint tex = 0;
int width = 0;
@@ -38,12 +47,22 @@ public:
void draw_rect_outline(float x, float y, float w, float h, float thickness, const Vec4& color);
void draw_line(float x0, float y0, float x1, float y1, float thickness, const Vec4& color);
// Text rendering (crisp built-in 5x7 font covering full ASCII)
// Text rendering (original Bullfrog OLFNT0-1 / OSFNT0-1 / PFONT0-1 with 5x7 fallback)
void draw_text(float x, float y, float scale, const std::string& text,
const Vec4& color = Vec4(1, 1, 1, 1), bool shadow = true);
const Vec4& color = Vec4(1, 1, 1, 1), bool shadow = true,
UiFont font = UiFont::Auto);
void draw_text_centered(float cx, float y, float scale, const std::string& text,
const Vec4& color = Vec4(1, 1, 1, 1), bool shadow = true);
float measure_text_w(const std::string& text, float scale) const;
const Vec4& color = Vec4(1, 1, 1, 1), bool shadow = true,
UiFont font = UiFont::Auto);
void draw_text_fit(float x, float y, float max_w, float scale, const std::string& text,
const Vec4& color = Vec4(1, 1, 1, 1), bool shadow = true,
UiFont font = UiFont::Auto);
void draw_text_centered_fit(float cx, float y, float max_w, float scale, const std::string& text,
const Vec4& color = Vec4(1, 1, 1, 1), bool shadow = true,
UiFont font = UiFont::Auto);
float measure_text_w(const std::string& text, float scale, UiFont font = UiFont::Auto) const;
float measure_text_h(float scale, UiFont font = UiFont::Auto) const;
bool has_bullfrog_fonts() const { return font_large_.tex != 0 && font_small_.tex != 0; }
// Bullfrog sprite & raw screen helpers
void draw_screen_cover(const std::string& screen_key, float alpha = 1.0f);
@@ -69,11 +88,30 @@ private:
uint32_t count = 0;
};
struct FontGlyph {
bool valid = false;
float u0 = 0.0f, v0 = 0.0f, u1 = 0.0f, v1 = 0.0f;
int w = 0, h = 0;
float advance = 0.0f;
};
struct FontAtlas {
GLuint tex = 0;
int line_h = 0;
float space_advance = 0.0f;
std::array<FontGlyph, 96> glyphs{}; // indexed by ASCII code (0..95)
};
void upload_rgba(GLuint& tex, int w, int h, const uint8_t* rgba, bool linear);
void push_quad(GLuint tex, float x0, float y0, float x1, float y1,
float u0, float v0, float u1, float v1, const Vec4& c);
void load_bank(const Vfs& vfs, const assets::Palette& pal,
const std::string& key, const std::string& stem);
bool load_font_atlas(const Vfs& vfs, const assets::Palette& pal,
const std::string& stem, FontAtlas& out);
const FontAtlas* resolve_font(float scale, UiFont font) const;
void draw_fallback_glyph(char ch, float x, float y, float cell_h, float cell_w,
const Vec4& color);
bool initialized_ = false;
ShaderProgram ui_shader_;
@@ -85,6 +123,10 @@ private:
int screen_w_ = 1280;
int screen_h_ = 720;
FontAtlas font_large_{};
FontAtlas font_small_{};
FontAtlas font_panel_{};
std::unordered_map<std::string, GpuSpriteTex> screens_;
std::unordered_map<std::string, std::vector<GpuSpriteTex>> banks_;