Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172// UI framework smoke test: the extracted bitmap font loads with sane metrics and proportional widths,// and a dialog renders its Win95 chrome (face colour + text) into a framebuffer. Skips if the font// assets aren't extracted yet (run extract_assets.py / extract_font.py).#include "sc2k/render/asset_loader.hpp"#include "sc2k/ui/dialog.hpp"#include "sc2k/ui/font.hpp"#include "sc2k/ui/menu.hpp"#include "sc2k/ui/toolbar.hpp"#include "sc2k/ui/resources.hpp"#include <cstdio>#include <string>
#include "test_harness.hpp"#include "test_paths.hpp"
int main() { using namespace sc2k; const std::string adir = render::assets_dir(); if (!exists(adir + "/fonts/system.bmp") || !exists(adir + "/fonts/system.txt")) { std::printf("ui: SKIP (no font under %s/fonts — run extract_assets.py)\n", adir.c_str()); return 0; }
ui::Font font(adir + "/fonts/system.bmp", adir + "/fonts/system.txt"); CHECK(font.height() > 4 && font.height() < 64); CHECK(font.avg_width() > 2); CHECK(font.text_width("OK") > 0); CHECK(font.text_width("WWW") > font.text_width("iii")); // proportional font
// Render a small dialog into a framebuffer and confirm the chrome + text drew. render::Framebuffer fb(400, 300); fb.clear(render::Rgba{0, 100, 0, 0xFF}); // green desktop, distinct from UI greys ui::Dialog d; d.w_dlu = 120; d.h_dlu = 70; d.title = "Test"; d.controls = {{1, "BUTTON", 0, 70, 50, 40, 14, "OK"}, {2, "STATIC", 0, 6, 6, 100, 10, "Hello"}}; d.draw(fb, font, 80, 70);
long face = 0, black = 0; for (int y = 0; y < fb.height(); ++y) for (int x = 0; x < fb.width(); ++x) { const render::Rgba p = fb.at(x, y); if (p.r == ui::theme::kFace.r && p.g == ui::theme::kFace.g && p.b == ui::theme::kFace.b) ++face; if (p.r == 0 && p.g == 0 && p.b == 0) ++black; // text/edge ink } std::printf("ui: font h=%d avg=%d; dialog face px=%ld ink px=%ld\n", font.height(), font.avg_width(), face, black); CHECK(face > 2000); // the dialog face panel painted CHECK(black > 50); // title/button/label text rendered
// Hit-testing: a click on the OK button finds it; a click in an empty corner finds nothing. { const int bx = font.avg_width(), by = font.height(); const int cx = 80 + 70 * bx / 4, cy = 70 + 50 * by / 8; const ui::Control* hb = d.hit_test(font, 80, 70, cx + 3, cy + 3); CHECK(hb != nullptr && hb->id == 1); CHECK(d.hit_test(font, 80, 70, 2, 2) == nullptr); }
// The dialogs are defined in code now (ui/dialog_defs.hpp), not loaded from a resource file. { ui::DialogSet set; set.build_builtin(); CHECK(set.size() == 19); // the live dialog set const ui::Dialog* d125 = set.get(125); // Education budget CHECK(d125 != nullptr); if (d125) { CHECK(d125->controls.size() > 10); CHECK(d125->w_dlu == 228); CHECK(d125->title == "Education Budget"); // caption baked into the definition } CHECK(set.get(157)->w_dlu == 150 && set.get(157)->h_dlu == 104); // SimNation client size baked in CHECK(set.get(246) == nullptr); // the retired City Map window is not defined // The About caption is our own, trademark-free. CHECK(set.get(100)->title == "About Libreopolis"); std::printf("ui: built %zu code-defined dialogs\n", set.size()); }
{ // The main menu is now defined in code (ui/menu_defs.hpp), not loaded from a resource file. ui::MenuBar menu; menu.build_builtin(3); menu.layout(font); CHECK(menu.height(font) > 0); const ui::MenuBar::Click r = menu.click(font, 1000, 4, 4); // open the first top CHECK(r.consumed);
// Seven top-level popups: File / Speed / Options / Disasters / Windows / Newspaper / Help. CHECK(menu.tops().size() == 7); // The retired City Map command (Windows->Map, 32783) is NOT present anywhere. for (const auto& t : menu.tops()) for (const auto& it : t.items) CHECK(it.cmd != 32783); // Checkable defaults are seeded from the MF_CHECKED flag (Auto-Goto / Sound / Music on). CHECK(menu.is_checked(32773) && menu.is_checked(32774) && menu.is_checked(32775));
// Rewards menu: the earned reward buildings are appended to the Newspaper menu (top index 5). // set_reward_items keeps the 2 base items, then a divider + the reward items (or nothing when empty). menu.set_reward_items(5, 2, {{"Mayor's House", "", 32845, false, 0}, {"City Hall", "", 32846, false, 0}}); const auto& np = menu.tops()[5].items; CHECK(np.size() == 5); // 2 base + divider + 2 rewards CHECK(np[3].cmd == 32845 && np[4].cmd == 32846); // the two gifts, in order CHECK(np[2].separator); // divider between base and gifts menu.set_reward_items(5, 2, {}); // no rewards → just the 2 base items CHECK(menu.tops()[5].items.size() == 2);
// The tool-group sub-tool menu (toolbar flyouts) and the debug menu also build from code. ui::MenuBar toolMenu; toolMenu.build_builtin(136); CHECK(toolMenu.tops().size() == 15); // 15 placement groups ui::MenuBar dbg; dbg.build_builtin(223); CHECK(dbg.tops().size() == 1 && dbg.tops()[0].items.size() > 5); }
if (exists(adir + "/ui/bmp_2.bmp")) { ui::ToolPalette palette; palette.build(render::load_ui_bitmap(2)); CHECK(palette.ready()); CHECK(palette.width() > 100 && palette.height() > 200); // tall, narrow floating panel // The first tool button (Bulldoze, index 0) sits at the grid's top-left cell. int x, y, w, h; CHECK(palette.button_rect(0, x, y, w, h)); CHECK(palette.button_at(x + w / 2, y + h / 2) == 0); // Verified button->action map (against the binary). tb_action is the faithful strip-icon reference // table; note the layer toggles (28-32) and the Map window (22) are retired features — the View City // Layers dialog and the map panel replace them, so those icons are not placed on the sidebar. CHECK(ui::tb_action(0).kind == ui::TbKind::Tool && ui::tb_action(0).arg == 0); CHECK(ui::tb_action(15).kind == ui::TbKind::Sign); // Sign tool CHECK(ui::tb_action(16).kind == ui::TbKind::Query); // Query tool (mode 23) CHECK(ui::tb_action(17).kind == ui::TbKind::RotateCCW); CHECK(ui::tb_action(18).kind == ui::TbKind::RotateCW); CHECK(ui::tb_action(19).kind == ui::TbKind::ZoomOut && ui::tb_action(20).kind == ui::TbKind::ZoomIn); CHECK(ui::tb_action(21).kind == ui::TbKind::Center); // Centering TOOL (mode 11) CHECK(ui::tb_action(27).kind == ui::TbKind::Window && ui::tb_action(27).arg == 102); // Budget CHECK(ui::tb_action(23).kind == ui::TbKind::Window && ui::tb_action(23).arg == 128); // Population CHECK(ui::tb_action(33).kind == ui::TbKind::Help); // The retired toggles/Map are NOT placed on the sidebar. for (int i = 28; i <= 32; ++i) { int tx, ty, tw, th; CHECK(!palette.button_rect(i, tx, ty, tw, th)); } int mx, my, mw, mh; CHECK(!palette.button_rect(22, mx, my, mw, mh));
// A selected button must VISIBLY press in: drawing it sunken (bit set in the mask) differs from // drawing it raised, within that button's own rect. (The bevel flips and the icon nudges in.) render::Framebuffer raised(palette.width() + 16, palette.height() + 16); render::Framebuffer sunk(palette.width() + 16, palette.height() + 16); palette.draw(raised, 8, 8, /*sunkenMask*/ 0ull); palette.draw(sunk, 8, 8, /*sunkenMask*/ (1ull << 0)); // Bulldoze group selected int bx2, by2, bw2, bh2; CHECK(palette.button_rect(0, bx2, by2, bw2, bh2)); long btnDiff = 0; for (int yy = by2; yy < by2 + bh2; ++yy) for (int xx = bx2; xx < bx2 + bw2; ++xx) { const render::Rgba p = raised.at(8 + xx, 8 + yy), q = sunk.at(8 + xx, 8 + yy); if (p.r != q.r || p.g != q.g || p.b != q.b) ++btnDiff; } CHECK(btnDiff > 8); // the pressed-in state is visibly different std::printf("ui: tool palette %dx%d, actions verified, select-draw diff=%ld px\n", palette.width(), palette.height(), btnDiff); }
if (failures == 0) { std::printf("ui: all checks passed\n"); return 0; } std::printf("ui: %d failure(s)\n", failures); return 1;}