From fb3c2fe75727d37fafa45b86f171e2b9dee55e18 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 13:07:24 +0100 Subject: [PATCH] ui/draw.hpp: one place for the pixel loops of fill and copy 67 file-local helpers (fill/rect/copy/copy_region/... in 39 screens) now call draw::fill, draw::copy_clipped or draw::copy_unclipped. Only the loops moved: every screen keeps its own wrapper with its bounds check and error text, so the contracts are unchanged. Eight helpers with special behaviour stay as they are (bank, season promotion, toto, tax animation, ...). Verified: 9 headless careers and the scripted UI walk (German and English data, 38 screens, about 5,460 steps each) give the same fingerprints as before. ctest: 209 of 211; other_games_ui_tests and ui_preparation_tests fail the same way without this change. Co-Authored-By: Claude --- src/ui/achievements.cpp | 4 +-- src/ui/advertising.cpp | 9 ++--- src/ui/alltime_table.cpp | 9 +++-- src/ui/balance_plan.cpp | 7 ++-- src/ui/bank.cpp | 4 +-- src/ui/club_info.cpp | 10 ++---- src/ui/club_player_detail.cpp | 9 ++--- src/ui/contracts.cpp | 9 ++--- src/ui/cup_draw.cpp | 7 ++-- src/ui/desk_records.cpp | 7 ++-- src/ui/desktop.cpp | 10 ++---- src/ui/draw.hpp | 59 +++++++++++++++++++++++++++++++++ src/ui/game_end.cpp | 9 ++--- src/ui/incoming_transfer.cpp | 6 ++-- src/ui/league_pairings.cpp | 8 ++--- src/ui/league_table.cpp | 9 ++--- src/ui/lineup.cpp | 9 ++--- src/ui/live_intervention.cpp | 5 ++- src/ui/live_matchday.cpp | 8 ++--- src/ui/mailbox_screen.cpp | 7 ++-- src/ui/manager.cpp | 11 ++---- src/ui/minute_statistics.cpp | 7 ++-- src/ui/opportunities.cpp | 9 ++--- src/ui/other_games.cpp | 11 ++---- src/ui/outgoing_transfer.cpp | 9 ++--- src/ui/pitch.cpp | 7 ++-- src/ui/player_card.cpp | 9 ++--- src/ui/save_load.cpp | 8 ++--- src/ui/season_entrants.cpp | 5 ++- src/ui/setup.cpp | 9 ++--- src/ui/stadium.cpp | 5 ++- src/ui/statistics_rankings.cpp | 10 ++---- src/ui/strengths.cpp | 7 ++-- src/ui/tactics.cpp | 10 ++---- src/ui/team_statistics.cpp | 10 ++---- src/ui/top_scorers.cpp | 9 ++--- src/ui/training.cpp | 10 ++---- src/ui/training_camp.cpp | 10 ++---- src/ui/transfer_market.cpp | 9 ++--- src/ui/world_cup_statistics.cpp | 7 ++-- 40 files changed, 171 insertions(+), 206 deletions(-) create mode 100644 src/ui/draw.hpp diff --git a/src/ui/achievements.cpp b/src/ui/achievements.cpp index ed7cbde..7ea6cd0 100644 --- a/src/ui/achievements.cpp +++ b/src/ui/achievements.cpp @@ -15,6 +15,7 @@ #include "game/schedule.hpp" #include "ui/manager.hpp" #include "ui/setup.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -32,8 +33,7 @@ void fill(Image& out, int x, int y, int w, int h, uint8_t color) { } void copy(Image& out,const Image& source,int sx,int sy,int x,int y,int w,int h,int transparent=-1) { if(sx<0||sy<0||sx+w>source.width||sy+h>source.height)throw FormatError("achievements: Source KB56 bounds"); - for(int yy=0;yy=0&&y+yy>=0&&x+xx& b,size_t at){return uint16_t(b.at(at))|uint16_t(uint16_t(b.at(at+1))<<8);} // FNT files consume the game's single-byte encoding; only JSON/test boundaries use UTF-8. diff --git a/src/ui/advertising.cpp b/src/ui/advertising.cpp index eb70b0a..5cc971a 100644 --- a/src/ui/advertising.cpp +++ b/src/ui/advertising.cpp @@ -27,6 +27,7 @@ #include "game/state.hpp" #include "ui/manager.hpp" #include "ui/setup.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -270,17 +271,13 @@ void copy(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int if (sx < 0 || sy < 0 || sx + w > src.width || sy + h > src.height || x < 0 || y < 0 || x + w > out.width || y + h > out.height) throw FormatError("advertising: original VGA crop outside image"); - for (int yy = 0; yy < h; ++yy) - for (int xx = 0; xx < w; ++xx) - out.at(x + xx, y + yy) = src.pixels[size_t(sy + yy) * src.width + sx + xx]; + draw::copy_unclipped(out, src, sx, sy, x, y, w, h, -1); } void fill_rect(Image& image, int x, int y, int width, int height, uint8_t color) { if (x < 0 || y < 0 || width < 0 || height < 0 || x + width > image.width || y + height > image.height) throw FormatError("advertising: rectangle outside image"); - for (int yy = y; yy < y + height; ++yy) - for (int xx = x; xx < x + width; ++xx) - image.at(xx, yy) = color; + draw::fill(image, x, y, width, height, color); } void rectangle(Image& image, int x, int y, int width, int height, uint8_t color) { diff --git a/src/ui/alltime_table.cpp b/src/ui/alltime_table.cpp index d59ff23..3ade759 100644 --- a/src/ui/alltime_table.cpp +++ b/src/ui/alltime_table.cpp @@ -8,11 +8,16 @@ #include "game/table.hpp" #include "ui/manager.hpp" #include "ui/setup.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { constexpr uint32_t kFirst=::bmh::layout::First, kLeague=::bmh::layout::AlltimeTable_Layout_ExecutableField; std::string original_text(const TextDb& t,uint32_t at){if(at<::bmh::layout::TextPointerTable||((at-::bmh::layout::TextPointerTable)&3U))throw FormatError("alltime: invalid TEXTD address");return original_to_utf8(t[(at-::bmh::layout::TextPointerTable)/4]);} -void fill(Image&o,int x,int y,int w,int h,uint8_t c){for(int j=0;js.width||sy+h>s.height||x+w>o.width||y+h>o.height)throw FormatError("alltime: Original asset bounds");for(int j=0;js.width||sy+h>s.height||x+w>o.width||y+h>o.height)throw FormatError("alltime: Original asset bounds"); + draw::copy_unclipped(o, s, sx, sy, x, y, w, h, tr); +} std::string source_number(uint32_t value,uint8_t flags){ const unsigned width=flags&0x3f;std::string digits=std::to_string(value); const bool grouped=(flags&0x40)==0&&width>3; diff --git a/src/ui/balance_plan.cpp b/src/ui/balance_plan.cpp index 40d4797..16e82bb 100644 --- a/src/ui/balance_plan.cpp +++ b/src/ui/balance_plan.cpp @@ -14,6 +14,7 @@ #include "game/messages.hpp" #include "ui/manager.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -23,9 +24,11 @@ std::string_view at(const TextDb& t,uint32_t a){if(a=x0&&x<=x1&&y>=y0&&y<=y1;} void copy(Image& o,const Image& s,int sx,int sy,int x,int y,int w,int h,int transparent=-1){ if(sx<0||sy<0||x<0||y<0||w<0||h<0||sx+w>s.width||sy+h>s.height||x+w>o.width||y+h>o.height)throw FormatError("balance: sprite bounds "+std::to_string(sx)+","+std::to_string(sy)+" "+std::to_string(w)+"x"+std::to_string(h)+" source "+std::to_string(s.width)+"x"+std::to_string(s.height)+" dest "+std::to_string(o.width)+"x"+std::to_string(o.height)); - for(int yy=0;yy= 0 && y + yy >= 0 && - x + xx < out.width && y + yy < out.height) out.at(x + xx, y + yy) = value; - } + draw::copy_clipped(out, src, sx, sy, x, y, w, h, transparent); } void rect(Image& out, int x, int y, int w, int h, uint8_t color) { - for (int yy = std::max(0, y); yy < std::min(out.height, y + h); ++yy) - for (int xx = std::max(0, x); xx < std::min(out.width, x + w); ++xx) out.at(xx, yy) = color; + draw::fill(out, x, y, w, h, color); } std::string padded(uint64_t value, int width = 2) { std::string s = std::to_string(value); diff --git a/src/ui/club_player_detail.cpp b/src/ui/club_player_detail.cpp index fda8f13..af212c0 100644 --- a/src/ui/club_player_detail.cpp +++ b/src/ui/club_player_detail.cpp @@ -12,21 +12,18 @@ #include #include #include "ui/manager.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { void rect(Image& out,int x,int y,int w,int h,uint8_t c) { - for(int yy=std::max(0,y);yysrc.width||sy+h>src.height) throw FormatError("club player detail: original atlas bounds "+std::to_string(sx)+","+std::to_string(sy)+ ":"+std::to_string(w)+"x"+std::to_string(h)+" in "+std::to_string(src.width)+"x"+std::to_string(src.height)); - for(int yy=0;yy=0&&x+xx=0&&y+yy source.width || sy + height > source.height || x + width > target.width || y + height > target.height) throw FormatError("contracts: invalid original widget bounds"); - for (int yy = 0; yy < height; ++yy) for (int xx = 0; xx < width; ++xx) { - const uint8_t value = source.pixels[size_t(sy + yy) * size_t(source.width) + size_t(sx + xx)]; - if (!transparent || value != 0) target.at(x + xx, y + yy) = value; - } + draw::copy_unclipped(target, source, sx, sy, x, y, width, height, transparent ? 0 : -1); } // Forward declaration; fill() is defined below and is used by frame(). @@ -56,8 +54,7 @@ void frame(Image& out, const LeImage& exe, const Image& widgets, int x, int y, i void fill(Image& target, int x, int y, int width, int height, uint8_t color) { if (x < 0 || y < 0 || width < 0 || height < 0 || x + width > target.width || y + height > target.height) throw FormatError("contracts: invalid fill bounds"); - for (int row = 0; row < height; ++row) - std::fill_n(target.pixels.begin() + size_t(y + row) * size_t(target.width) + size_t(x), size_t(width), color); + draw::fill(target, x, y, width, height, color); } // Formats a number right-aligned in `width` characters; with `grouped`, '.' is inserted as thousands separator diff --git a/src/ui/cup_draw.cpp b/src/ui/cup_draw.cpp index 741d6fe..0a4f0e8 100644 --- a/src/ui/cup_draw.cpp +++ b/src/ui/cup_draw.cpp @@ -20,6 +20,7 @@ #include "game/state.hpp" #include "game/season.hpp" #include "ui/manager.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -34,8 +35,7 @@ void copy(Image& dst,const Image& src,int sx,int sy,int x,int y,int w,int h) { // FormatError2 passes ff as the VGA plane bitmask. The copy is opaque and an // out-of-bounds rectangle is rejected in full by SourceMaskCopy. if(!valid_rect(dst,src,sx,sy,x,y,w,h))return; - for(int yy=0;yy=0&&xx=0&&y-1src.width || sy+h>src.height || x+w>out.width || y+h>out.height) throw FormatError("desk records: Original asset bounds"); - for(int yy=0;yy(g.manager_teams.size(),4); diff --git a/src/ui/desktop.cpp b/src/ui/desktop.cpp index 94daf05..1fd1c2b 100644 --- a/src/ui/desktop.cpp +++ b/src/ui/desktop.cpp @@ -11,23 +11,19 @@ #include #include "ui/manager.hpp" +#include "ui/draw.hpp" #include "game/state.hpp" #include "game/messages.hpp" namespace bmh::ui { namespace { void rect(Image& out, int x, int y, int w, int h, uint8_t color) { - for (int yy = std::max(0, y); yy < std::min(out.height, y + h); ++yy) - for (int xx = std::max(0, x); xx < std::min(out.width, x + w); ++xx) out.at(xx, yy) = color; + draw::fill(out, x, y, w, h, color); } void copy(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int h, bool transparent = false) { if (sx < 0 || sy < 0 || sx + w > src.width || sy + h > src.height) throw FormatError("desktop: sprite outside original atlas"); - for (int yy = 0; yy < h; ++yy) for (int xx = 0; xx < w; ++xx) { - const auto color = src.pixels[size_t(sy + yy) * src.width + sx + xx]; - if (x + xx >= 0 && y + yy >= 0 && x + xx < out.width && y + yy < out.height && (!transparent || color)) - out.at(x + xx, y + yy) = color; - } + draw::copy_clipped(out, src, sx, sy, x, y, w, h, transparent ? 0 : -1); } void border(Image& out, int x, int y) { rect(out, x - 1, y - 1, 40, 1, 0x46); rect(out, x - 1, y - 1, 1, 40, 0x46); diff --git a/src/ui/draw.hpp b/src/ui/draw.hpp new file mode 100644 index 0000000..11778c0 --- /dev/null +++ b/src/ui/draw.hpp @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: GPL-3.0-only +#pragma once +// The pixel loops shared by the screens: filling a rectangle and copying a rectangle of a picture +// (an atlas) into another, with or without a transparent colour. +// +// Nearly every screen had its own copy of these loops in an anonymous namespace (about 75 copies). +// The screens keep their own small wrappers, because what they check before drawing differs and is +// part of their contract: some reject a source rectangle outside the atlas with their own error +// text, some reject a destination outside the screen, some clip silently. Only the loops are here. +// +// Conventions: `key` is the transparent palette index; a value outside 0..255 (the default -1) +// copies every pixel. Rectangles are x, y, width, height; empty or negative sizes draw nothing. + +#include +#include +#include + +#include "formats/vga.hpp" + +namespace bmh::ui::draw { + +// Fills the part of the rectangle that lies inside the image. +inline void fill(Image& out, int x, int y, int w, int h, uint8_t color) { + const int x0 = std::max(0, x), y0 = std::max(0, y); + const int x1 = std::min(out.width, x + w), y1 = std::min(out.height, y + h); + for (int yy = y0; yy < y1; ++yy) + for (int xx = x0; xx < x1; ++xx) out.pixels[size_t(yy) * size_t(out.width) + size_t(xx)] = color; +} + +// True when the rectangle lies completely inside the image (and has no negative size). +inline bool inside(const Image& image, int x, int y, int w, int h) { + return x >= 0 && y >= 0 && w >= 0 && h >= 0 && x + w <= image.width && y + h <= image.height; +} + +// Copies w x h pixels from (sx, sy) of `src` to (x, y) of `out`; pixels that fall outside `out` are +// skipped. The source rectangle must lie inside `src` (the caller checks that and reports its own error). +inline void copy_clipped(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int h, int key = -1) { + for (int yy = 0; yy < h; ++yy) { + const int ty = y + yy; + if (ty < 0 || ty >= out.height) continue; + for (int xx = 0; xx < w; ++xx) { + const int tx = x + xx; + if (tx < 0 || tx >= out.width) continue; + const uint8_t c = src.pixels[size_t(sy + yy) * size_t(src.width) + size_t(sx + xx)]; + if (int(c) != key) out.pixels[size_t(ty) * size_t(out.width) + size_t(tx)] = c; + } + } +} + +// The same without clipping: both rectangles must lie inside their images (checked by the caller). +inline void copy_unclipped(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int h, int key = -1) { + for (int yy = 0; yy < h; ++yy) + for (int xx = 0; xx < w; ++xx) { + const uint8_t c = src.pixels[size_t(sy + yy) * size_t(src.width) + size_t(sx + xx)]; + if (int(c) != key) out.pixels[size_t(y + yy) * size_t(out.width) + size_t(x + xx)] = c; + } +} + +} // namespace bmh::ui::draw diff --git a/src/ui/game_end.cpp b/src/ui/game_end.cpp index d8a3e18..7b133f3 100644 --- a/src/ui/game_end.cpp +++ b/src/ui/game_end.cpp @@ -18,18 +18,15 @@ #include "ui/manager.hpp" #include "ui/setup.hpp" #include "ui/sprite_font.hpp" +#include "ui/draw.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 source.width || sy + height > source.height || x + width > target.width || y + height > target.height) throw FormatError("incoming transfer: invalid original sprite bounds"); - for (int yy = 0; yy < height; ++yy) for (int xx = 0; xx < width; ++xx) { - const auto value = source.pixels[size_t(sy + yy) * size_t(source.width) + size_t(xx + sx)]; - if (transparent_index < 0 || value != transparent_index) target.at(x + xx, y + yy) = value; - } + draw::copy_unclipped(target, source, sx, sy, x, y, width, height, transparent_index); } // Formats a number right-aligned in `width` characters ('%' padding, as the original font expects); diff --git a/src/ui/league_pairings.cpp b/src/ui/league_pairings.cpp index f6f8a61..20f3f25 100644 --- a/src/ui/league_pairings.cpp +++ b/src/ui/league_pairings.cpp @@ -14,6 +14,7 @@ #include "game/season.hpp" #include "ui/desk_state.hpp" #include "ui/manager.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -38,13 +39,10 @@ void copy(Image& out,const Image& src,int sx,int sy,int x,int y,int w,int h,int throw FormatError("pairings: Original atlas bounds (source "+std::to_string(src.width)+"x"+ std::to_string(src.height)+", crop "+std::to_string(sx)+","+std::to_string(sy)+" "+ std::to_string(w)+"x"+std::to_string(h)+", destination "+std::to_string(x)+","+std::to_string(y)+")"); - for(int yy=0;yy source.width || sy + height > source.height || x + width > out.width || y + height > out.height) throw FormatError("league table: original atlas bounds"); - for (int dy = 0; dy < height; ++dy) for (int dx = 0; dx < width; ++dx) { - const auto pixel = source.pixels[size_t(sy + dy) * size_t(source.width) + size_t(sx + dx)]; - if (pixel != transparent) out.at(x + dx, y + dy) = pixel; - } + draw::copy_unclipped(out, source, sx, sy, x, y, width, height, transparent); } void fill_region(Image& out, int x, int y, int width, int height, uint8_t color) { - for (int dy = y; dy < y + height; ++dy) - for (int dx = x; dx < x + width; ++dx) out.at(dx, dy) = color; + draw::fill(out, x, y, width, height, color); } } diff --git a/src/ui/lineup.cpp b/src/ui/lineup.cpp index e2d323c..51acc25 100644 --- a/src/ui/lineup.cpp +++ b/src/ui/lineup.cpp @@ -12,6 +12,7 @@ #include "formats/savestate.hpp" #include "game/state.hpp" #include "ui/manager.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -77,15 +78,11 @@ LineupState project(const StaticMemory& memory, const LeImage& exe, const std::v return out; } void rect(Image& out, int x, int y, int w, int h, uint8_t color) { - for (int yy = std::max(0,y); yy < std::min(out.height,y+h); ++yy) - for (int xx = std::max(0,x); xx < std::min(out.width,x+w); ++xx) out.at(xx,yy) = color; + draw::fill(out, x, y, w, h, color); } void copy(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int h, int transparent = -1) { if (sx < 0 || sy < 0 || sx+w > src.width || sy+h > src.height) throw FormatError("lineup: original atlas bounds " + std::to_string(sx) + "," + std::to_string(sy) + ":" + std::to_string(w) + "x" + std::to_string(h) + " in " + std::to_string(src.width) + "x" + std::to_string(src.height)); - for (int yy=0; yy=0 && x+xx=0 && y+yy rows; }; diff --git a/src/ui/live_matchday.cpp b/src/ui/live_matchday.cpp index 114ae10..dfac325 100644 --- a/src/ui/live_matchday.cpp +++ b/src/ui/live_matchday.cpp @@ -14,6 +14,7 @@ #include "ui/manager.hpp" #include "ui/live_set_piece.hpp" #include "ui/setup.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -24,12 +25,7 @@ void copy_region(Image& target, const Image& source, int sx, int sy, int dx, int int width, int height, int transparent = -1) { if (sx < 0 || sy < 0 || width < 0 || height < 0 || sx + width > source.width || sy + height > source.height) throw FormatError("live matchday: original asset crop out of bounds"); - for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) { - const uint8_t pixel = source.pixels[size_t(sy + y) * size_t(source.width) + size_t(sx + x)]; - const int tx = dx + x, ty = dy + y; - if (pixel != transparent && tx >= 0 && ty >= 0 && tx < target.width && ty < target.height) - target.at(tx, ty) = pixel; - } + draw::copy_clipped(target, source, sx, sy, dx, dy, width, height, transparent); } void pattern_rect(Image& target, const LeImage& exe, int x, int y, int width, int height) { diff --git a/src/ui/mailbox_screen.cpp b/src/ui/mailbox_screen.cpp index 36e0341..9abe7d5 100644 --- a/src/ui/mailbox_screen.cpp +++ b/src/ui/mailbox_screen.cpp @@ -14,6 +14,7 @@ #include "ui/manager.hpp" #include "ui/setup.hpp" #include "ui/game_end.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -23,11 +24,7 @@ void copy(Image& out, const Image& source, int sx, int sy, int dx, int dy, int w bool transparent = false) { if (sx < 0 || sy < 0 || sx + w > source.width || sy + h > source.height) throw FormatError("mailbox: sprite outside original atlas"); - for (int y = 0; y < h; ++y) for (int x = 0; x < w; ++x) { - const auto c = source.pixels[size_t(sy + y) * source.width + sx + x]; - if ((!transparent || c) && dx + x >= 0 && dy + y >= 0 && - dx + x < out.width && dy + y < out.height) out.at(dx + x, dy + y) = c; - } + draw::copy_clipped(out, source, sx, sy, dx, dy, w, h, transparent ? 0 : -1); } } diff --git a/src/ui/manager.cpp b/src/ui/manager.cpp index 627d8c9..11e0c54 100644 --- a/src/ui/manager.cpp +++ b/src/ui/manager.cpp @@ -7,6 +7,7 @@ #include "ui/manager.hpp" #include "ui/dialog_frame.hpp" #include "ui/button_frame.hpp" +#include "ui/draw.hpp" #include "formats/league_visibility.hpp" #include @@ -21,19 +22,13 @@ namespace bmh::ui { namespace { void rect(Image& out, int x, int y, int w, int h, uint8_t color) { - for (int yy = std::max(y, 0); yy < std::min(y + h, out.height); ++yy) - for (int xx = std::max(x, 0); xx < std::min(x + w, out.width); ++xx) out.at(xx, yy) = color; + draw::fill(out, x, y, w, h, color); } void copy(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int h, bool transparent = false) { if (sx < 0 || sy < 0 || sx + w > src.width || sy + h > src.height) throw FormatError("manager: sprite outside original atlas"); - for (int yy = 0; yy < h; ++yy) - for (int xx = 0; xx < w; ++xx) { - const auto c = src.pixels[size_t(sy + yy) * src.width + sx + xx]; - if (x + xx >= 0 && y + yy >= 0 && x + xx < out.width && y + yy < out.height && (!transparent || c)) - out.at(x + xx, y + yy) = c; - } + draw::copy_clipped(out, src, sx, sy, x, y, w, h, transparent ? 0 : -1); } void border(Image& out, int x, int y) { diff --git a/src/ui/minute_statistics.cpp b/src/ui/minute_statistics.cpp index 5e998f1..ef1bd7f 100644 --- a/src/ui/minute_statistics.cpp +++ b/src/ui/minute_statistics.cpp @@ -13,6 +13,7 @@ #include "game/messages.hpp" #include "ui/manager.hpp" #include "ui/setup.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -28,11 +29,7 @@ void copy_transparent(Image& target, const Image& source, int sx, int sy, sx + width > source.width || sy + height > source.height || dx + width > target.width || dy + height > target.height) throw FormatError("minute statistics: Original KB36 crop outside image"); - for (int y = 0; y < height; ++y) - for (int x = 0; x < width; ++x) { - const uint8_t pixel = source.pixels[size_t(sy + y) * size_t(source.width) + size_t(sx + x)]; - if (pixel != transparent) target.at(dx + x, dy + y) = pixel; - } + draw::copy_unclipped(target, source, sx, sy, dx, dy, width, height, transparent); } // SourceBarColor / SourceBarColor2 scan each current 18-byte series. Their diff --git a/src/ui/opportunities.cpp b/src/ui/opportunities.cpp index cf894bd..3ea2296 100644 --- a/src/ui/opportunities.cpp +++ b/src/ui/opportunities.cpp @@ -9,6 +9,7 @@ #include #include "ui/manager.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -17,14 +18,10 @@ void copy(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int if (sx < 0 || sy < 0 || x < 0 || y < 0 || w < 0 || h < 0 || sx + w > src.width || sy + h > src.height || x + w > out.width || y + h > out.height) throw FormatError("opportunities: Original sprite bounds"); - for (int yy = 0; yy < h; ++yy) for (int xx = 0; xx < w; ++xx) { - const auto pixel = src.pixels[size_t(sy + yy) * size_t(src.width) + size_t(sx + xx)]; - if (pixel != transparent) out.at(x + xx, y + yy) = pixel; - } + draw::copy_unclipped(out, src, sx, sy, x, y, w, h, transparent); } void fill(Image& out, int x, int y, int w, int h, uint8_t color) { - for (int yy = y; yy < y + h; ++yy) for (int xx = x; xx < x + w; ++xx) - out.at(xx, yy) = color; + draw::fill(out, x, y, w, h, color); } std::string original_text(const TextDb& texts, uint32_t at) { if (at < ::bmh::layout::TextPointerTable || ((at - ::bmh::layout::TextPointerTable) & 3U)) diff --git a/src/ui/other_games.cpp b/src/ui/other_games.cpp index 39f1d74..f3f9b3e 100644 --- a/src/ui/other_games.cpp +++ b/src/ui/other_games.cpp @@ -11,6 +11,7 @@ #include #include "ui/manager.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -19,18 +20,12 @@ void copy(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int if (sx < 0 || sy < 0 || x < 0 || y < 0 || w < 0 || h < 0 || sx + w > src.width || sy + h > src.height || x + w > out.width || y + h > out.height) throw FormatError("other games: Original atlas bounds"); - for (int yy = 0; yy < h; ++yy) - for (int xx = 0; xx < w; ++xx) { - const uint8_t color = src.pixels[size_t(sy + yy) * size_t(src.width) + size_t(sx + xx)]; - if (color != transparent) out.at(x + xx, y + yy) = color; - } + draw::copy_unclipped(out, src, sx, sy, x, y, w, h, transparent); } void fill(Image& out, int x, int y, int w, int h, uint8_t color) { if (w <= 0 || h <= 0) return; - for (int yy = std::max(0, y); yy < std::min(out.height, y + h); ++yy) - for (int xx = std::max(0, x); xx < std::min(out.width, x + w); ++xx) - out.at(xx, yy) = color; + draw::fill(out, x, y, w, h, color); } std::string source_number(unsigned value, unsigned width = 0) { diff --git a/src/ui/outgoing_transfer.cpp b/src/ui/outgoing_transfer.cpp index a0d1919..b761a93 100644 --- a/src/ui/outgoing_transfer.cpp +++ b/src/ui/outgoing_transfer.cpp @@ -16,6 +16,7 @@ #include "formats/textd.hpp" #include "ui/manager.hpp" #include "ui/setup.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -45,18 +46,14 @@ void copy_region(Image& target, const Image& source, int sx, int sy, int x, int sx + width > source.width || sy + height > source.height || x + width > target.width || y + height > target.height) throw FormatError("outgoing transfer: invalid Source sprite bounds"); - for (int yy = 0; yy < height; ++yy) for (int xx = 0; xx < width; ++xx) { - const auto pixel = source.pixels[size_t(sy + yy) * size_t(source.width) + size_t(sx + xx)]; - if (transparent < 0 || pixel != transparent) target.at(x + xx, y + yy) = pixel; - } + draw::copy_unclipped(target, source, sx, sy, x, y, width, height, transparent); } // Fills a rectangle with one palette index; throws if it leaves the image. void fill_rect(Image& out, int x, int y, int width, int height, uint8_t color) { if (x < 0 || y < 0 || width < 0 || height < 0 || x + width > out.width || y + height > out.height) throw FormatError("outgoing transfer: invalid Source rectangle"); - for (int yy = y; yy < y + height; ++yy) - std::fill_n(out.pixels.begin() + size_t(yy) * size_t(out.width) + size_t(x), width, color); + draw::fill(out, x, y, width, height, color); } // Number with '.' as thousands separator, no padding. diff --git a/src/ui/pitch.cpp b/src/ui/pitch.cpp index eddbcb6..b0fcb42 100644 --- a/src/ui/pitch.cpp +++ b/src/ui/pitch.cpp @@ -14,6 +14,7 @@ #include "game/messages.hpp" #include "game/state.hpp" #include "ui/manager.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -23,8 +24,7 @@ Bytes block(const SaveState& save, uint32_t address) { } void copy(Image& out,const Image& src,int sx,int sy,int x,int y,int w,int h,int transparent=-1) { if(sx<0||sy<0||sx+w>src.width||sy+h>src.height)throw FormatError("pitch: original atlas bounds"); - for(int yy=0;yy=0&&y+yy>=0&&x+xxsrc.width||sy+h>src.height) throw FormatError("player card: original atlas bounds "+std::to_string(sx)+","+std::to_string(sy)+ ":"+std::to_string(w)+"x"+std::to_string(h)+" in "+std::to_string(src.width)+"x"+std::to_string(src.height)); - for(int yy=0;yy=0&&x+xx=0&&y+yy src.width || sy + h > src.height) throw FormatError("save/load: Original widget outside atlas"); - for (int yy = 0; yy < h; ++yy) for (int xx = 0; xx < w; ++xx) - if (x + xx >= 0 && x + xx < out.width && y + yy >= 0 && y + yy < out.height) - out.at(x + xx, y + yy) = src.pixels[size_t(sy + yy) * size_t(src.width) + size_t(sx + xx)]; + draw::copy_clipped(out, src, sx, sy, x, y, w, h, -1); } void pattern(Image& out, const LeImage& exe, int x, int y, int w, int h) { for (int yy = 0; yy < h; ++yy) for (int xx = 0; xx < w; ++xx) diff --git a/src/ui/season_entrants.cpp b/src/ui/season_entrants.cpp index cd3c871..43055ad 100644 --- a/src/ui/season_entrants.cpp +++ b/src/ui/season_entrants.cpp @@ -10,6 +10,7 @@ #include "formats/textd.hpp" #include "game/state.hpp" #include "ui/manager.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -20,9 +21,7 @@ constexpr int kMaxEntrants = 32; constexpr int kPlacementSlots = 8; void fill(Image& out, int x, int y, int w, int h, uint8_t color) { - for (int yy = std::max(0, y); yy < std::min(out.height, y + h); ++yy) - for (int xx = std::max(0, x); xx < std::min(out.width, x + w); ++xx) - out.at(xx, yy) = color; + draw::fill(out, x, y, w, h, color); } void source_badge_frame(Image& out, int x, int y) { diff --git a/src/ui/setup.cpp b/src/ui/setup.cpp index 5fece88..7a4317f 100644 --- a/src/ui/setup.cpp +++ b/src/ui/setup.cpp @@ -2,6 +2,7 @@ // Implementation of SetupScreen (setup.hpp): layout tables read from the executable, shared render and hit-testing, and the limits between managers and league/world options. #include "formats/executable_layout.hpp" #include "ui/setup.hpp" +#include "ui/draw.hpp" #include #include @@ -19,13 +20,7 @@ void copy_rect(Image& dst, const Image& src, int sx, int sy, int dx, int dy, int width, int height, bool transparent = false) { if (sx < 0 || sy < 0 || sx + width > src.width || sy + height > src.height) throw FormatError("setup: original sprite rectangle outside atlas"); - for (int y = 0; y < height; ++y) - for (int x = 0; x < width; ++x) { - auto c = src.pixels[size_t(sy + y) * src.width + sx + x]; - if (dx + x >= 0 && dy + y >= 0 && dx + x < dst.width && dy + y < dst.height && - (!transparent || c != 0)) - dst.at(dx + x, dy + y) = c; - } + draw::copy_clipped(dst, src, sx, sy, dx, dy, width, height, transparent ? 0 : -1); } } // namespace diff --git a/src/ui/stadium.cpp b/src/ui/stadium.cpp index f21c492..b892f42 100644 --- a/src/ui/stadium.cpp +++ b/src/ui/stadium.cpp @@ -20,14 +20,13 @@ #include "game/state.hpp" #include "ui/manager.hpp" #include "ui/setup.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { // Fills a w*h rectangle with one palette index, clipped to the image. Used for panels, shadows and backgrounds. void fill(Image& im, int x, int y, int w, int h, uint8_t color) { - for (int yy = std::max(0, y); yy < std::min(im.height, y + h); ++yy) - for (int xx = std::max(0, x); xx < std::min(im.width, x + w); ++xx) - im.at(xx, yy) = color; + draw::fill(im, x, y, w, h, color); } // Draws a 1-pixel rectangle outline (w*h, clipped to the image). Used for hover and press highlights. void outline(Image& im, int x, int y, int w, int h, uint8_t color) { diff --git a/src/ui/statistics_rankings.cpp b/src/ui/statistics_rankings.cpp index 96cb853..cdbdfaa 100644 --- a/src/ui/statistics_rankings.cpp +++ b/src/ui/statistics_rankings.cpp @@ -9,6 +9,7 @@ #include "ui/setup.hpp" #include "ui/manager.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -25,18 +26,13 @@ void copy_transparent(Image& out, const Image& src, int sx, int sy, int x, int y if (sx < 0 || sy < 0 || x < 0 || y < 0 || w < 0 || h < 0 || sx + w > src.width || sy + h > src.height || x + w > out.width || y + h > out.height) throw FormatError("statistics rankings: Original sprite bounds"); - for (int yy = 0; yy < h; ++yy) - for (int xx = 0; xx < w; ++xx) { - const uint8_t color = src.pixels[size_t(sy + yy) * size_t(src.width) + size_t(sx + xx)]; - if (color != 0xc0) out.at(x + xx, y + yy) = color; - } + draw::copy_unclipped(out, src, sx, sy, x, y, w, h, 0xc0); } void fill(Image& out, int x, int y, int w, int h, uint8_t color) { if (x < 0 || y < 0 || w < 0 || h < 0 || x + w > out.width || y + h > out.height) throw FormatError("statistics rankings: Original button bounds"); - for (int yy = y; yy < y + h; ++yy) - for (int xx = x; xx < x + w; ++xx) out.at(xx, yy) = color; + draw::fill(out, x, y, w, h, color); } uint8_t original_name_color(const GameState& game, int runtime_team) { diff --git a/src/ui/strengths.cpp b/src/ui/strengths.cpp index c2131ae..4f41eb8 100644 --- a/src/ui/strengths.cpp +++ b/src/ui/strengths.cpp @@ -6,18 +6,17 @@ #include #include #include "ui/manager.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { void copy(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int h, int transparent = -1) { if (sx<0||sy<0||x<0||y<0||w<0||h<0||sx+w>src.width||sy+h>src.height||x+w>out.width||y+h>out.height) throw FormatError("strengths: Original atlas bounds"); - for(int yy=0;yy src.width || sy + h > src.height) throw FormatError("tactics: sprite outside original atlas"); - for (int yy = 0; yy < h; ++yy) for (int xx = 0; xx < w; ++xx) { - const auto c = src.pixels.at(size_t(sy + yy) * src.width + sx + xx); - if (x + xx >= 0 && y + yy >= 0 && x + xx < out.width && y + yy < out.height && (!transparent || c)) - out.at(x + xx, y + yy) = c; - } + draw::copy_clipped(out, src, sx, sy, x, y, w, h, transparent ? 0 : -1); } } // namespace diff --git a/src/ui/team_statistics.cpp b/src/ui/team_statistics.cpp index e6efea2..3fbf1b3 100644 --- a/src/ui/team_statistics.cpp +++ b/src/ui/team_statistics.cpp @@ -13,6 +13,7 @@ #include "game/season.hpp" #include "ui/manager.hpp" #include "ui/setup.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -20,15 +21,10 @@ void copy(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int int transparent = -1) { if (sx < 0 || sy < 0 || w < 0 || h < 0 || sx + w > src.width || sy + h > src.height) throw FormatError("team statistics: Source asset bounds"); - for (int yy = 0; yy < h; ++yy) for (int xx = 0; xx < w; ++xx) { - const uint8_t value = src.pixels[size_t(sy + yy) * size_t(src.width) + size_t(sx + xx)]; - if (value != transparent && x + xx >= 0 && y + yy >= 0 && x + xx < out.width && y + yy < out.height) - out.at(x + xx, y + yy) = value; - } + draw::copy_clipped(out, src, sx, sy, x, y, w, h, transparent); } void rect(Image& out, int x, int y, int w, int h, uint8_t value) { - for (int yy = std::max(0, y); yy < std::min(out.height, y + h); ++yy) - for (int xx = std::max(0, x); xx < std::min(out.width, x + w); ++xx) out.at(xx, yy) = value; + draw::fill(out, x, y, w, h, value); } void source_frame(Image& out, const Image& widgets, const LeImage& exe, int x, int y, int w, int h, uint8_t color) { diff --git a/src/ui/top_scorers.cpp b/src/ui/top_scorers.cpp index a01ddb0..3bcb6f8 100644 --- a/src/ui/top_scorers.cpp +++ b/src/ui/top_scorers.cpp @@ -8,6 +8,7 @@ #include #include "ui/manager.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -25,15 +26,11 @@ void copy(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int if (sx < 0 || sy < 0 || x < 0 || y < 0 || w < 0 || h < 0 || sx + w > src.width || sy + h > src.height || x + w > out.width || y + h > out.height) throw FormatError("top scorers: Original sprite bounds"); - for (int yy = 0; yy < h; ++yy) - for (int xx = 0; xx < w; ++xx) - out.at(x + xx, y + yy) = src.pixels[size_t(sy + yy) * size_t(src.width) + size_t(sx + xx)]; + draw::copy_unclipped(out, src, sx, sy, x, y, w, h, -1); } void fill(Image& out, int x, int y, int w, int h, uint8_t color) { - for (int yy = y; yy < y + h; ++yy) - for (int xx = x; xx < x + w; ++xx) - out.at(xx, yy) = color; + draw::fill(out, x, y, w, h, color); } std::string source_number(unsigned value, unsigned width = 0) { diff --git a/src/ui/training.cpp b/src/ui/training.cpp index ba4374e..de35e2f 100644 --- a/src/ui/training.cpp +++ b/src/ui/training.cpp @@ -12,6 +12,7 @@ #include "formats/savestate.hpp" #include "game/state.hpp" #include "ui/manager.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -84,17 +85,12 @@ TrainingState project(const StaticMemory& memory, const Bytes& records, const By validate(s); return s; } void rect(Image& out, int x, int y, int w, int h, uint8_t color) { - for (int yy = std::max(0, y); yy < std::min(out.height, y + h); ++yy) - for (int xx = std::max(0, x); xx < std::min(out.width, x + w); ++xx) out.at(xx, yy) = color; + draw::fill(out, x, y, w, h, color); } void copy(Image& out, const Image& atlas, int sx, int sy, int x, int y, int w, int h, int transparent = -1) { if (sx < 0 || sy < 0 || sx + w > atlas.width || sy + h > atlas.height) throw FormatError("training: sprite outside original atlas"); - for (int yy = 0; yy < h; ++yy) for (int xx = 0; xx < w; ++xx) { - const auto c = atlas.pixels[size_t(sy + yy) * atlas.width + sx + xx]; - if (c != transparent && x + xx >= 0 && y + yy >= 0 && x + xx < out.width && y + yy < out.height) - out.at(x + xx, y + yy) = c; - } + draw::copy_clipped(out, atlas, sx, sy, x, y, w, h, transparent); } const char* request_name(TrainingRequestKind kind) { switch (kind) { diff --git a/src/ui/training_camp.cpp b/src/ui/training_camp.cpp index 664dae3..2772581 100644 --- a/src/ui/training_camp.cpp +++ b/src/ui/training_camp.cpp @@ -15,6 +15,7 @@ #include "ui/manager.hpp" #include "ui/setup.hpp" #include "ui/game_end.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -28,15 +29,10 @@ constexpr size_t kMoney = 0x314; void copy(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int h, int transparent = -1) { if (sx < 0 || sy < 0 || sx + w > src.width || sy + h > src.height) throw FormatError("training camp: sprite outside original atlas"); - for (int yy = 0; yy < h; ++yy) for (int xx = 0; xx < w; ++xx) { - const uint8_t color = src.pixels[size_t(sy + yy) * src.width + sx + xx]; - if (x + xx >= 0 && y + yy >= 0 && x + xx < out.width && y + yy < out.height && color != transparent) - out.at(x + xx, y + yy) = color; - } + draw::copy_clipped(out, src, sx, sy, x, y, w, h, transparent); } void rect(Image& out, int x, int y, int w, int h, uint8_t color) { - for (int yy = std::max(0, y); yy < std::min(out.height, y + h); ++yy) - for (int xx = std::max(0, x); xx < std::min(out.width, x + w); ++xx) out.at(xx, yy) = color; + draw::fill(out, x, y, w, h, color); } int32_t rd32(const std::vector& v, size_t at) { return int32_t(uint32_t(v.at(at)) | uint32_t(v.at(at + 1)) << 8 | uint32_t(v.at(at + 2)) << 16 | diff --git a/src/ui/transfer_market.cpp b/src/ui/transfer_market.cpp index 2803c66..0244874 100644 --- a/src/ui/transfer_market.cpp +++ b/src/ui/transfer_market.cpp @@ -13,6 +13,7 @@ #include "game/state.hpp" #include "game/messages.hpp" #include "ui/setup.hpp" +#include "ui/draw.hpp" namespace bmh::ui { namespace { @@ -27,18 +28,14 @@ constexpr size_t kManagerRecordSize = 0xb0c; // Fills a rectangle with one palette index, clipped to the image. void rect(Image& out,int x,int y,int w,int h,uint8_t color) { - for(int yy=std::max(0,y);yysource.width||sy+h>source.height) throw FormatError("transfer market: Original asset rectangle outside source"); - for(int yy=0;yy=0&&x+xx=0&&y+yy src.width || sy + h > src.height) throw FormatError("World Cup statistics: Source atlas bounds"); - for (int yy = 0; yy < h; ++yy) for (int xx = 0; xx < w; ++xx) { - const uint8_t value = src.pixels[size_t(sy + yy) * size_t(src.width) + size_t(sx + xx)]; - if (value != transparent && x + xx >= 0 && y + yy >= 0 && x + xx < out.width && y + yy < out.height) - out.at(x + xx, y + yy) = value; - } + draw::copy_clipped(out, src, sx, sy, x, y, w, h, transparent); } void draw_text(const Font& font, Image& out, int x, int baseline, const std::string& value, uint8_t color) {