// SPDX-License-Identifier: GPL-3.0-only #include "formats/executable_layout.hpp" #include "ui/game_end.hpp" #include #include #include #include #include "formats/font.hpp" #include "formats/le.hpp" #include "ui/manager.hpp" #include "ui/setup.hpp" #include "ui/sprite_font.hpp" namespace bmh::ui { namespace { void copy(Image& d,const Image& s,int sx,int sy,int x,int y,int w,int h,bool transparent=false) { for(int yy=0;yy=0&&y+yy>=0&&x+xx=0&&top+f.height<=im.height) f.draw(im,x,top,bytes,c); } void source_text(Image& out,const Font& f,const LeImage& exe,int x,int baseline, std::string_view bytes,uint8_t color,bool gradient) { const int top=baseline-f.baseline; if(top<0||top+f.height>out.height) return; if(!gradient||exe.u8(::bmh::layout::TextGradientMode)!=2||color==0x40){f.draw(out,x,top,bytes,color);return;} unsigned slot=0;while(slot<32&&exe.u8(::bmh::layout::Advertising_Text_RuntimeField2+slot)!=color)++slot; if(slot==32){f.draw(out,x,top,bytes,color);return;} Image mask(std::max(1,f.text_width(bytes)),f.height,0); f.draw(mask,0,0,bytes,1); for(int y=0;y=0&&x+xx out.width || baseline + 2 > out.height) return; raw_text(out,f,x+2,baseline+2,bytes,0x40); source_text(out,f,exe,x,baseline,bytes,color,gradient); } class SourceHyphenator { public: SourceHyphenator(const Font& font,const LeImage& exe):font_(font),exe_(exe),vowels_(exe.cstring(::bmh::layout::GameEnd_SourceHyphenator_ExecutableField)) { for(int i=0;i<30;++i) clusters_.push_back(exe_.cstring(exe_.u32(::bmh::layout::GameEnd_SourceHyphenator_RuntimeField+uint32_t(i)*4))); } std::vector wrap(std::string_view text,int max_width,bool justify=false) const { if(max_width<=4)throw FormatError("Source paragraph: invalid width"); std::vector lines;std::string line;std::vector ends; size_t at=0; while(atmax_width) { // FindBreak measures strlen(pending)+1, including glyph0. const int cut=find_break(word,max_width-4, font_.text_width(line)+font_.glyph(0).advance); if(cut>=0) { if(!line.empty()){line+=' ';ends.push_back(line.size()-1);} std::string head=word.substr(0,size_t(cut+1)); if(head.back()=='c'&&word[size_t(cut+1)]=='k')head.back()='k'; line+=head;line+=exe_.cstring(::bmh::layout::Fltd_Layout_ExecutableField6); word.erase(0,size_t(cut+1)); } else if(line.empty()) { // DOS cannot advance on an oversized indivisible word. // Keep it intact for the caller's clipping, never hang or // invent a syllable. Ordinary Source words take the path above. line=word;word.clear(); } if(!ends.empty())ends.pop_back(); size_t gap=0; while(justify&&!ends.empty()&&font_.text_width(line)=ends.size())gap=0; } lines.push_back(line);line.clear();ends.clear();candidate=word; } if(!line.empty())line+=' '; line+=word;ends.push_back(line.size()); if(at clusters_; bool vowel(char c) const { return vowels_.find(c)!=std::string::npos; } static char fold(char c) { const unsigned char u=static_cast(c);return u>='A'&&u<='Z'?char(u+32):c; } int find_break(const std::string& word,int max_prefix_width,int preceding) const { int limit=int(word.size())-1; for(int cut=limit;cut>=0;--cut) { // Draw2 retains the previous over-width candidate as the // right-section limit; this is not always the end of the word. if(valid_break(word,cut,limit)) { const auto prefix=std::string_view(word).substr(0,size_t(cut+1)); if(preceding+font_.text_width(prefix)=0&&!vowel(word[size_t(start)]);--start) { for(const auto& cluster:clusters_) { if(cluster.empty()||start+int(cluster.size())<=cut+1)continue; if(start+int(cluster.size())>int(word.size()))continue; bool equal=true; for(size_t j=0;j 0; at -= 3) digits.insert(size_t(at), "."); const unsigned separators = (count - 1) / 3; const unsigned reserved = minimum_digits > 3 ? (minimum_digits - 4) / 3 + 1 : 0; std::string out = negative ? "-" : ""; if (reserved > separators) out.append(reserved - separators, ' '); if (minimum_digits > count + unsigned(negative)) out.append(minimum_digits - count - unsigned(negative), '%'); return out + digits; } void button(Image& o,const Image& a,const SpriteFont& sf,const LeImage& exe,int x,int y,int w, std::string_view label,bool selected=false){ if(selected){const auto under=o.at(x,y+10);fill(o,x,y,w+1,2,under);fill(o,x,y,2,11,under);x+=2;y+=2;} else {fill(o,x+2,y+10,w,2,0x40);fill(o,x+w,y+2,2,10,0x40);} const int sy=selected?44:74; copy(o,a,24,sy,x,y,31,10);int used=31; while(w-used>20){int n=std::min(8,w-used-20);copy(o,a,24,sy+10,x+used,y,n,10);used+=n;} copy(o,a,45-(w-used),sy+20,x+used,y,w-used,10); sf.draw(o,x+w/2-sf.text_width(label)/2,y+7,label,exe.u8(::bmh::layout::Advertising_Button_RuntimeField + int(selected))); } std::string_view text_at(const TextDb& texts,uint32_t address) { constexpr uint32_t base=::bmh::layout::TextPointerTable; if(address wrap_source_text(const Font& font,const LeImage& exe, std::string_view text,int max_width,bool justify) { return SourceHyphenator(font,exe).wrap(text,max_width,justify); } std::vector wrap_source_text(const std::filesystem::path& data,std::string_view text,int max_width) { const auto font=load_font(asset_path(data/"F","BMH3.FNT")); const auto exe=load_le(asset_path(data,"BMH.exe")); return wrap_source_text(font,exe,text,max_width); } struct GameEndHighscoreScreen::FontData { Font small; LeImage exe; SpriteFont sprite; explicit FontData(const std::filesystem::path& p):small(load_font(asset_path(p/"F","BMH3.FNT"))),exe(load_le(asset_path(p,"BMH.exe"))),sprite(p){} }; GameEndHighscoreScreen::GameEndHighscoreScreen(const std::filesystem::path& data,HighscoreProjection p,GameEndRenderOptions options) : projection_(std::move(p)),options_(options),background_(load_vga(asset_path(data/"KB","129.VGA"))), logo_(load_vga(asset_path(data/"KB","51.VGA"))),texts_(load_textd(asset_path(data,"TEXTD.BLK"))),font_(std::make_shared(data)) {} Image GameEndHighscoreScreen::render() const { Image out=source_base(background_,options_.plain_background,true); if(logo_.width>=112&&logo_.height>=74){fill(out,102,99,112,2,0x40);fill(out,212,27,2,74,0x40);copy(out,logo_,0,0,100,25,112,74);} if(!projection_.ranked)return out; const auto& heading=texts_[893]; font_->sprite.draw(out,SpriteFont::centered_title_x(font_->sprite.text_width(original_to_utf8(heading))),13,original_to_utf8(heading),5); for(size_t i=0;ismall,font_->exe,33,baseline,left,0x5f,options_.gradient_text); source_text_shadow(out,font_->small,font_->exe,125,baseline,r.club_name,0x5f,options_.gradient_text); source_text_shadow(out,font_->small,font_->exe,230,baseline,right,0x5f,options_.gradient_text); } return out; } struct GameEndFinalScreen::FontData { Font final_font; explicit FontData(const std::filesystem::path& p):final_font(load_font(asset_path(p/"F","BMH2.FNT"))){} }; GameEndFinalScreen::GameEndFinalScreen(const std::filesystem::path& data,GameEndRenderOptions options) : background_(load_vga(asset_path(data/"KB","129.VGA"))),options_(options),texts_(load_textd(asset_path(data,"TEXTD.BLK"))),font_(std::make_shared(data)) {} Image GameEndFinalScreen::render() const { Image out=source_base(background_,options_.plain_background,false); for(const auto& [id,y]:{std::pair{1172,100},std::pair{1173,140}}){ const auto& text=texts_[id];const int w=font_->final_font.text_width(text); raw_text(out,font_->final_font,8+(311-w)/2,y,text,0x5f); } return out; } struct GameEndTaxDialog::FontData { Font small,large;LeImage exe;SpriteFont sprite;explicit FontData(const std::filesystem::path& p):small(load_font(asset_path(p/"F","BMH3.FNT"))),large(load_font(asset_path(p/"F","BMH2.FNT"))),exe(load_le(asset_path(p,"BMH.exe"))),sprite(p){} }; struct ProgramErrorScreen::FontData { Font font,header; LeImage exe; explicit FontData(const std::filesystem::path& p) : font(load_font(asset_path(p/"F","BMH3.FNT"))), header(load_font(asset_path(p/"F","BMH2.FNT"))), exe(load_le(asset_path(p,"BMH.exe"))) {} }; ProgramErrorScreen::ProgramErrorScreen(const std::filesystem::path& data,uint16_t code,GameEndRenderOptions options) : code_(code),options_(options),texts_(load_textd(asset_path(data,"TEXTD.BLK"))),font_(std::make_shared(data)) {} Image ProgramErrorScreen::render() const { Image out(320,240,0); const uint8_t color=code_<150 && (code_<91 || code_>99)?0x75:0xf7; const auto label=[&](std::string_view text,int baseline){ source_text(out,font_->font,font_->exe,159-font_->font.text_width(text)/2, baseline,text,color,options_.gradient_text); }; // TextAt selects font2 for the heading. DrawnFirstBy resets it to font3 // after its first label; param7=-1 gives no shadow, header no gradient. const auto heading=text_at(texts_,::bmh::layout::GameEnd_Out_TextPointer); raw_text(out,font_->header,159-font_->header.text_width(heading)/2,21,heading,color); for(int i=0;i<5;++i){ std::string line(text_at(texts_,::bmh::layout::GameEnd_Line_TextPointer+uint32_t(i)*4)); if(i==0) { const auto number=std::to_string(code_); // Replace(...,0x45) uses Source's '%' padding glyph, whose // blank advance differs from an ordinary space in BMH3.FNT. line.replace(14,5,std::string(5-number.size(),'%')+number); } label(std::move(line),60+i*10); } for(int n=0;n<3;++n){ fill(out,n,n,320-2*n,1,color);fill(out,n,33-n,320-2*n,1,color); fill(out,n,n,1,34-2*n,color);fill(out,319-n,n,1,34-2*n,color); } return out; } GameEndTaxDialog::GameEndTaxDialog(const std::filesystem::path& data,TaxDialogProjection p,GameEndRenderOptions options,std::optional restored_backdrop) : projection_(std::move(p)),options_(options),restored_backdrop_(std::move(restored_backdrop)),background_(load_vga(asset_path(data/"KB","129.VGA"))),widgets_(load_vga(asset_path(data/"KB","0.VGA"))), faces_(load_vga(asset_path(data/"KB","26.VGA"))),texts_(load_textd(asset_path(data,"TEXTD.BLK"))),font_(std::make_shared(data)) { if(!projection_.badge_fallback) badge_=load_vga(asset_path(data/"W",std::to_string(std::max(0,projection_.badge_sprite)>>3)+".VGA")); } Image GameEndTaxDialog::backdrop() const { if(projection_.compact_dialog_frame)return Image(320,240,0); if(restored_backdrop_&&restored_backdrop_->width==320&&restored_backdrop_->height==240)return *restored_backdrop_; return source_base(background_,options_.plain_background,false); } Image GameEndTaxDialog::render(std::optional