diff --git a/docs/plugins.md b/docs/plugins.md index ac3931e2..272038a2 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -205,6 +205,15 @@ gives the Binary Ninja address. gives them back for its length and takes them up again from wherever the cutscene leaves the hunter (Quake, Hunter Cam and Isometric do). `on_view` is not called during one; `on_camera` still is. +- `screen_shapes(name, tris, n)` (v30): a plugin's 2D over the picture - + triangles (`BbScreenTri`: corners 0 to 1 across and down the game's picture, + a colour with alpha) that the host draws with its own overlay, the FPS + counter's, over every frame it presents: after the game's HUD, under the + host's boxes, menus and pointer. Never in the 3D, so frame generation, the + upscalers and the TAA take them for UI, as they take the HUD (Quake's + crosshair). A call replaces the plugin's last ones, `n = 0` takes them away, + and they go by themselves a quarter of a second after the last call: call it + each frame to keep them up. At most 2048; main thread. **The world held still, the controller, who else is in the world, the HUD, the look and a panel** (v29) diff --git a/include/bbhost_plugin.h b/include/bbhost_plugin.h index 090869a2..464f53bb 100644 --- a/include/bbhost_plugin.h +++ b/include/bbhost_plugin.h @@ -38,7 +38,7 @@ extern "C" { #endif -#define BB_PLUGIN_API_VERSION 29 +#define BB_PLUGIN_API_VERSION 30 #if defined(_WIN32) #define BB_PLUGIN_EXPORT __declspec(dllexport) @@ -486,6 +486,15 @@ typedef struct BbPanel { menu that holds `page` (BB_OPT_MENU), or all its pages without one */ uint32_t page_count; } BbPanel; +/* Version 30: a triangle of a plugin's 2D over the picture (screen_shapes): + * its corners in the game's picture, 0 to 1 across (x, from the left) and + * down (y, from the top), whatever the window's or the picture's size; its + * colour 0xRRGGBBAA, blended over what is beneath by its alpha. */ +typedef struct BbScreenTri { + float x[3], y[3]; + uint32_t rgba; +} BbScreenTri; + typedef struct BbPanelState { uint32_t size; /* set to sizeof(BbPanelState) before the call: the host fills as much as both know */ int32_t shown; /* 1 while the panel is up */ @@ -1196,6 +1205,19 @@ typedef struct BbHostApi { * on_view is not called while it is 1 (on_camera still is, so a plugin * can see it begin and end). Any thread. */ int (*cutscene)(void); + + /* Version 30: a plugin's 2D over the picture. The triangles (BbScreenTri) + * the plugin named `name` shows, in place of the ones it gave before; + * n = 0 takes them away. The host draws them with its own overlay (the + * FPS counter's, the pointer's) over every frame it presents, after the + * game's HUD and under the host's text boxes, menus and pointer - never + * into the 3D: frame generation, the upscalers, the motion blur and the + * TAA leave them alone, as they leave the HUD. They stay up a quarter of a + * second after the last call, so a plugin keeps them up by calling each + * frame (on_camera, on_frame) and they go by themselves once it stops; + * also while the plugin is off. At most 2048 triangles. Main thread. 0 on + * success; 1 for a bad argument, too many, or a plugin that is off. */ + int (*screen_shapes)(const char* name, const BbScreenTri* tris, size_t n); } BbHostApi; #define BB_SNAP_PIXELS 0xffffffffu /* version 18: scene_snap's grid the 3D's own pixels */ diff --git a/plugins/quake/quake.cpp b/plugins/quake/quake.cpp index c89e2417..21994c15 100644 --- a/plugins/quake/quake.cpp +++ b/plugins/quake/quake.cpp @@ -89,7 +89,7 @@ extern "C" BB_PLUGIN_EXPORT const BbPluginInfo bb_plugin_info = { BB_PLUGIN_OPT_IN | BB_PLUGIN_GAMEPLAY, "quake", "Quake", - "0.3.24", + "0.3.26", "Kyo", "First person with Quake 3's movement and view: strafe jumping, bunny hopping, crouching, and the bob, roll and " "landing dip of Quake 3's camera.", @@ -2901,7 +2901,10 @@ void take_input(bool on, bool moving = true) { g.api->input_take(keys, n, kMouseLeft | kMouseRight, 1, 1); } +void crosshair_off(); // the crosshair's 2D taken away (below) + void release() { + crosshair_off(); g_on_ladder = false; g_ladder_exit_ms = -1.0; // The held character's switches and parts go back only while it is still @@ -3177,10 +3180,73 @@ void debug_disc(std::uint8_t* lists, V3 c, V3 right, V3 up, V3 forward, float r) g.api->call_guest(kDrawSphere, ®s); } +// Plugin API 30's screen_shapes: the host's own 2D over the picture, after the +// HUD - frame generation, the upscalers and the TAA take it for UI, where the +// debug shapes are drawn into the 3D and generated frames smeared the ring. +bool screen_shapes_ok() { + return g.api->version >= 30 && g.api->size >= offsetof(BbHostApi, screen_shapes) + sizeof(void*) && g.api->screen_shapes; +} + +bool g_screen_shown = false; + +void crosshair_off() { + if (!g_screen_shown) return; + g_screen_shown = false; + if (screen_shapes_ok()) g.api->screen_shapes("quake", nullptr, 0); +} + +// A band from radius r0 to r1 round (cx, cy), in the picture's 0..1: `rx` and +// `ry` turn one of Quake 3's 480-line pixels into a share across and down. +void ring_band(std::vector& out, float cx, float cy, float rx, float ry, float r0, float r1, std::uint32_t rgba) { + constexpr int kSegments = 48; + for (int i = 0; i < kSegments; ++i) { + const float a0 = 2.0f * kPi * static_cast(i) / kSegments, a1 = 2.0f * kPi * static_cast(i + 1) / kSegments; + const float c0 = std::cos(a0), s0 = std::sin(a0), c1 = std::cos(a1), s1 = std::sin(a1); + const float ix0 = cx + c0 * r0 * rx, iy0 = cy + s0 * r0 * ry, ox0 = cx + c0 * r1 * rx, oy0 = cy + s0 * r1 * ry; + const float ix1 = cx + c1 * r0 * rx, iy1 = cy + s1 * r0 * ry, ox1 = cx + c1 * r1 * rx, oy1 = cy + s1 * r1 * ry; + out.push_back({{ix0, ox0, ox1}, {iy0, oy0, oy1}, rgba}); + out.push_back({{ix0, ox1, ix1}, {iy0, oy1, iy1}, rgba}); + } +} + +// The same crosshair as the host's 2D: the ring a solid band one 480-line +// pixel wide with fainter edges - what the debug lines' halo of circles drew - +// and the dot a filled disc, at the middle of the picture. +void crosshair_2d(const BbCamera& cam) { + const float aspect = cam.aspect > 0.1f && cam.aspect < 10.0f ? cam.aspect : 16.0f / 9.0f; + const float ry = 1.0f / 480.0f, rx = ry / aspect; + const float ring = s.crosshair_size * 0.375f, dot_r = std::max(s.crosshair_size * 0.03f, 0.5f); + const auto colour = [](float a) { + return 0xffffff00u | static_cast(std::lround(std::clamp(a, 0.0f, 1.0f) * 255.0f)); + }; + const float a = s.crosshair_alpha; + std::vector tris; + tris.reserve(48 * 6 + 24); + ring_band(tris, 0.5f, 0.5f, rx, ry, ring - 0.5f, ring - 0.17f, colour(a * 0.45f)); + ring_band(tris, 0.5f, 0.5f, rx, ry, ring - 0.17f, ring + 0.17f, colour(a)); + ring_band(tris, 0.5f, 0.5f, rx, ry, ring + 0.17f, ring + 0.5f, colour(a * 0.45f)); + constexpr int kDot = 24; + for (int i = 0; i < kDot; ++i) { + const float a0 = 2.0f * kPi * static_cast(i) / kDot, a1 = 2.0f * kPi * static_cast(i + 1) / kDot; + tris.push_back({{0.5f, 0.5f + std::cos(a0) * dot_r * rx, 0.5f + std::cos(a1) * dot_r * rx}, + {0.5f, 0.5f + std::sin(a0) * dot_r * ry, 0.5f + std::sin(a1) * dot_r * ry}, + colour(a)}); + } + g_screen_shown = g.api->screen_shapes("quake", tris.data(), tris.size()) == 0; +} + // Quake 3's crosshair at the middle of the view: a dot in a ring, its size in -// the 640x480 screen's pixels (cg_crosshairSize), white. +// the 640x480 screen's pixels (cg_crosshairSize), white. As the host's 2D where +// it has screen_shapes; else as the game's debug shapes, in the 3D. void draw_crosshair(const BbCamera& cam) { - if (!s.crosshair || s.crosshair_alpha <= 0.0f) return; + if (!s.crosshair || s.crosshair_alpha <= 0.0f) { + crosshair_off(); + return; + } + if (screen_shapes_ok()) { + crosshair_2d(cam); + return; + } std::uint8_t* lists = debug_lists(); if (!lists) return; const V3 eye{cam.position[0], cam.position[1], cam.position[2]}; @@ -3462,6 +3528,7 @@ void camera(BbCamera& cam) { vm_frame(chr, !dead); if (vm.out) vm_place(cam, yaw, pitch, roll); if (!dead) draw_crosshair(cam); + else crosshair_off(); if (g_log && g_now >= g_next_log) { g_next_log = g_now + 0.25; diff --git a/src/host/plugins.cpp b/src/host/plugins.cpp index 7f0459ac..76fa6317 100644 --- a/src/host/plugins.cpp +++ b/src/host/plugins.cpp @@ -49,6 +49,7 @@ #include "host/scene_look.h" #include "host/updater.h" #include "host/window.h" +#include "host/overlay.h" #include "log.h" #include @@ -1235,6 +1236,36 @@ int api_on_view(void (*fn)(BbCamera*, void*), void* user) { // Version 29: a cutscene asked for or playing (engine/loading.h). int api_cutscene() { return loading_cutscene() ? 1 : 0; } +// Version 30: the plugins' 2D over the picture (screen_shapes), drawn by the +// window's pump into the host overlay (plugins_draw_screen_shapes), which the +// presenter puts over the frame after the HUD: frame generation takes it for +// UI. Each plugin's list goes a quarter of a second after it was last given. +struct ScreenShapes { + std::vector tris; + std::chrono::steady_clock::time_point at; +}; +std::mutex g_screen_mu; +std::map g_screen; +constexpr std::size_t kMaxScreenTris = 2048; +constexpr auto kScreenHold = std::chrono::milliseconds(250); + +int api_screen_shapes(const char* name, const BbScreenTri* tris, std::size_t n) { + if (!name || !*name || (n && !tris) || n > kMaxScreenTris) return 1; + if (n) { + const auto it = std::find_if(g_plugins.begin(), g_plugins.end(), [&](const Plugin& p) { return p.name == name; }); + if (it == g_plugins.end() || it->dormant) return 1; + } + std::lock_guard lk(g_screen_mu); + if (!n) { + g_screen.erase(name); + return 0; + } + ScreenShapes& s = g_screen[name]; + s.tris.assign(tris, tris + n); + s.at = std::chrono::steady_clock::now(); + return 0; +} + int api_key_bound(std::uint16_t scancode) { if (scancode == 0) return 0; // A plugin's own keys work in play, where a menu's actions press nothing @@ -1846,6 +1877,7 @@ const BbHostApi g_api = { &api_panel_show, &api_panel_state, &api_option_set, &api_input_take_as, &api_input_as, &api_panel_rows, &api_panel_value, &api_panel_open, &api_cutscene, + &api_screen_shapes, }; std::string read_file(const std::string& path) { @@ -2619,3 +2651,18 @@ void plugins_enter_world(const std::string& host_rules) { c.fn(entry.c_str(), c.user); // "" when the host plays without this plugin } } + +void plugins_draw_screen_shapes(float display_w, float display_h) { + std::lock_guard lk(g_screen_mu); + const auto now = std::chrono::steady_clock::now(); + for (auto it = g_screen.begin(); it != g_screen.end();) { + if (now - it->second.at > kScreenHold) { + it = g_screen.erase(it); + continue; + } + for (const BbScreenTri& t : it->second.tris) + host_overlay_tri(t.x[0] * display_w, t.y[0] * display_h, t.x[1] * display_w, t.y[1] * display_h, + t.x[2] * display_w, t.y[2] * display_h, t.rgba); + ++it; + } +} diff --git a/src/host/plugins.h b/src/host/plugins.h index 930c03a6..5ba80d0f 100644 --- a/src/host/plugins.h +++ b/src/host/plugins.h @@ -77,6 +77,9 @@ std::string plugins_ruleset(); // The plugin of that name is loaded and on for this run (not a visitor): // what host features a plugin turns on ask (engine/debug_menu.h). bool plugins_active(const std::string& name); +// The plugins' 2D over the picture (plugin API 30's screen_shapes) into the +// host overlay being built, at the display buffer's size: the window's pump. +void plugins_draw_screen_shapes(float display_w, float display_h); // Plugins that do not work together (Quake, Isometric and Hunter Cam: each owns the // camera): the warnings `name`'s page shows - one a line - while it and a plugin it clashes // with are both turned on with their main switch on, among `list` (the diff --git a/src/host/window.cpp b/src/host/window.cpp index 338e90c9..d2dd3875 100755 --- a/src/host/window.cpp +++ b/src/host/window.cpp @@ -15,6 +15,7 @@ #include "host/options.h" #include "host/settings.h" #include "host/overlay.h" +#include "host/plugins.h" #include "host/perf_meter.h" #include "host/present_pass.h" #include "host/ingame_menu.h" @@ -2737,6 +2738,19 @@ bool host_window_pump() { // The host overlay's contents for this frame. Rebuilt from scratch each // pump; when nothing draws, the presenter's path is exactly as it was. host_overlay_reset(); + // The plugins' 2D (plugin API 30's screen_shapes: Quake's crosshair) under + // everything else the overlay draws. + { + float dw = 1920.0f, dh = 1080.0f; + { + std::lock_guard ml(g_mouse_mu); + if (g_display_w && g_display_h) { + dw = static_cast(g_display_w); + dh = static_cast(g_display_h); + } + } + plugins_draw_screen_shapes(dw, dh); + } // The FPS counter, top right, under everything else the overlay draws: the // game's frame rate (host_fps_frame_start's average), refreshed three // times a second - every frame, it flickers between neighbours and cannot diff --git a/tests/plugin_abi_test.c b/tests/plugin_abi_test.c index b6d3f4fa..6b7cbd90 100755 --- a/tests/plugin_abi_test.c +++ b/tests/plugin_abi_test.c @@ -1,10 +1,10 @@ -/* The plugin API's version 29 structures as a C compiler lays them out - the +/* The plugin API's version 30 structures as a C compiler lays them out - the * layout plugins are built against, which a host may append to and never * move: their sizes, the places of the fields read most, and the API table's * version 27 calls right after version 26's camera_zoom - the frame's three, * then the shaders' five, followed by version 28's camera target hook and * version 29's world pause, controller, world state and menus, ending with - * its cutscene(). + * its cutscene(), and version 30's screen_shapes(). * Built as C99, so the header stays C. */ #include "bbhost_plugin.h" @@ -60,7 +60,9 @@ _Static_assert(offsetof(BbHostApi, panel_rows) == offsetof(BbHostApi, input_as) _Static_assert(offsetof(BbHostApi, panel_value) == offsetof(BbHostApi, panel_rows) + sizeof(void*), "panel_value after panel_rows"); _Static_assert(offsetof(BbHostApi, panel_open) == offsetof(BbHostApi, panel_value) + sizeof(void*), "panel_open after panel_value"); _Static_assert(offsetof(BbHostApi, cutscene) == offsetof(BbHostApi, panel_open) + sizeof(void*), "cutscene after panel_open"); -_Static_assert(sizeof(BbHostApi) == offsetof(BbHostApi, cutscene) + sizeof(void*), "cutscene ends the API table"); +_Static_assert(offsetof(BbHostApi, screen_shapes) == offsetof(BbHostApi, cutscene) + sizeof(void*), "screen_shapes after cutscene"); +_Static_assert(sizeof(BbHostApi) == offsetof(BbHostApi, screen_shapes) + sizeof(void*), "screen_shapes ends the API table"); +_Static_assert(sizeof(BbScreenTri) == 28 && offsetof(BbScreenTri, rgba) == 24, "BbScreenTri"); _Static_assert(BB_OPT_PANEL == 0x400u && BB_PANEL_DEFAULTS == 1u && BB_PANEL_CORNER == 2u, "the panel's flags"); _Static_assert(BB_OPT_MENU == 0x800u && BB_OPT_SUBPAGE == 0x1000u && BB_OPT_OPEN == 0x2000u && BB_OPT_CONFIRM == 0x4000u && BB_OPT_INFO == 8,