// SPDX-License-Identifier: GPL-3.0-only #include "formats/badge_policy.hpp" #include "formats/executable_layout.hpp" #include "ui/season_promotion.hpp" #include #include #include #include #include "formats/binary.hpp" #include "formats/font.hpp" #include "formats/textd.hpp" #include "game/season.hpp" #include "game/state.hpp" #include "ui/manager.hpp" namespace bmh::ui { namespace { constexpr std::array kScanlineOrder{ 12,34,7,16,22,9,31,19,2,37,1,14,21,28,24,4,36,13,5, 30,3,32,23,25,8,11,35,17,26,20,6,27,15,33,18,29,10,0}; void copy(Image& dst,const Image& src,int sx,int sy,int x,int y,int width,int height, bool transparent=true) { // FormatError2 rejects the complete rectangle instead of drawing a clipped // fragment. This also keeps the eight-entry Source list inside the surface. if(width<=0||height<=0||sx<0||sy<0||x<0||y<0|| width>src.width-sx||height>src.height-sy|| width>dst.width-x||height>dst.height-y)return; for(int yy=0;yy=dst.width||dy>=dst.height||source_x<0||source_y<0|| source_x>=src.width||source_y>=src.height)continue; const uint8_t pixel=src.pixels[size_t(source_y)*size_t(src.width)+size_t(source_x)]; if(!transparent||pixel!=0)dst.at(dx,dy)=pixel; } } void source_badge_frame(Image& out,int x,int y) { for(int xx=x-1;xx<=x+38;++xx)if(xx>=0&&xx=0&&y-1=0&&yy=0&&x-1=0&&xx=0&&y+38=0&&yy=0&&x+383||projection_.count>8|| projection_.relegated.size()int(kScanlineOrder.size())|| background_.width!=320||background_.height!=240||texts_.strings.size()<=1251) throw FormatError("season promotion: invalid projection or Original assets"); for(size_t i=0;ioriginal_name.empty())throw FormatError("season promotion: empty Source club name"); if(!team->badge_fallback) { const int atlas=(team->badge_selector&0x1ff)>>3; if(!badge_atlases_.contains(atlas)) badge_atlases_.emplace(atlas,load_vga(asset_path(data_directory/"W",std::to_string(atlas)+".VGA"))); } } } } Image SeasonPromotionScreen::render() const { return render(projection_.scanline_steps); } Image SeasonPromotionScreen::render(int scanline_steps) const { if(scanline_steps<0||scanline_steps>int(kScanlineOrder.size())) throw FormatError("season promotion: scanline step outside Source range"); // SeasonPromotionScreen installs KB/25 as its non-null source background. The // PlainWindowFrameUsedBy non-null branch copies that full image independently of PlainBackground. Image out=background_; const std::string heading=original_to_utf8(texts_[1251]); const int heading_left=172+(282-172)/2-sprite_font_.text_width(heading)/2; sprite_font_.draw(out,heading_left,13,heading,5); sprite_font_.draw(out,5,235,original_to_utf8(projection_.league_footer),5); const bool gradient=projection_.gradient_text&&exe_.u8(::bmh::layout::TextGradientMode)==2; auto draw_name=[&](bool relegated,size_t row,const std::string& bytes) { const int center=name_x(relegated),baseline=201+6*int(row); const int width=font_.text_width(bytes),left=center-width/2,top=baseline-font_.baseline; const int source_baseline=exe_.u8(::bmh::layout::SeasonPromotion_FormatError_RuntimeFieldUnsigned+3u*4u); if(left<0||baseline-source_baseline<0||left+width+2>out.width||baseline+2>out.height)return; // DrawnFirstBy flags 0x40 draws the +2px shadow then Source text. font_.draw(out,left+2,top+2,bytes,0x40); const uint8_t color=name_color(relegated); int ramp=0; while(ramp<32&&exe_.u8(::bmh::layout::Advertising_Text_RuntimeField2Unsigned+uint32_t(ramp))!=color)++ramp; if(!gradient||ramp==32){font_.draw(out,left,top,bytes,color);return;} Image mask(std::max(1,width),font_.height,0); font_.draw(mask,0,0,bytes,1); for(int yy=0;yy>3); copy(out,atlas,(team.badge_selector&7)*38,0,x,y,38,38,false); } draw_name(relegated,row,team.original_name); }; for(size_t i=0;i=out.height||up_y>=out.height|| team_x(true)+38>out.width||team_x(false)+38>out.width)continue; std::array down{},up{}; for(int x=0;x<38;++x) { down[size_t(x)]=out.pixels[size_t(down_y)*out.width+team_x(true)+x]; up[size_t(x)]=out.pixels[size_t(up_y)*out.width+team_x(false)+x]; } for(int x=0;x<38;++x) { out.at(team_x(true)+x,down_y)=up[size_t(x)]; out.at(team_x(false)+x,up_y)=down[size_t(x)]; } } } return out; } SeasonPromotionProjection season_promotion_from_game( const GameState& game,const SeasonPromotionEvent& event, const LeImage& exe,const TextDb& texts) { static constexpr std::array first_league{0,1,2,4}; static constexpr std::array suffix{0,0,'A','B',0}; if(event.group<0||event.group>=int(first_league.size())||event.count>8|| event.relegated.size()=texts.strings.size()||suffix_index>=texts.strings.size()) throw FormatError("season promotion: missing Source league labels"); // Reuse the game's Source LeagueName implementation when the live state is // attached to its TextDb; the fallback uses the supplied owned database. std::string footer; if(game.texts)footer=league_name(game,league); else { footer=texts[footer_index]; if(suffix[size_t(league)]!=0) { footer+=texts[suffix_index]; if(!footer.empty())footer.back()=suffix[size_t(league)]; } } SeasonPromotionProjection out; out.group=event.group;out.count=event.count;out.league_footer=std::move(footer); out.gradient_text=(!game.memory.has(::bmh::layout::TextGradientDisabled)||game.memory.u8(::bmh::layout::TextGradientDisabled)==0)&&exe.u8(::bmh::layout::TextGradientMode)==2; auto project=[&](const std::vector& ids,std::vector& rows) { rows.reserve(event.count); for(size_t i=0;i=game.club_heap_78.size())throw FormatError("season promotion: missing Source badge selector"); badge=uint16_t(game.club_heap_78[at]|uint16_t(game.club_heap_78[at+1])<<8); } rows.push_back({id,badge,fallback,game.club_name(id)}); } }; project(event.relegated,out.relegated);project(event.promoted,out.promoted); return out; } } // namespace bmh::ui