engine: materialize live ADV glyphs

This commit is contained in:
gamer147
2026-07-30 19:04:22 -04:00
parent c2b72cd4b9
commit 1367563485
15 changed files with 758 additions and 27 deletions

View File

@@ -64,6 +64,15 @@ public interface IHost
void ShowText(int offset, string text);
void ShowText(AdvLiveTextRun run, int glyphDelayMilliseconds)
=> ShowText(run.SourceOffset, run.Text);
AdvRetainedTextRunResult? ShowText(
GfxState gfx,
AdvTextLayoutPresentationBinding binding,
AdvLiveTextRun run,
int glyphDelayMilliseconds)
{
ShowText(run, glyphDelayMilliseconds);
return null;
}
int MessageGlyphDelayMilliseconds => 50;
void SetMessageGlyphDelayMilliseconds(int milliseconds) { }
// Native ADV text subsystem: op 0x7a updates the selected layout's last 20-byte cursor record;
@@ -73,6 +82,9 @@ public interface IHost
void DrawStringToSurface(int surfaceSlot, int x, int y, string text, AdvTextStyle style)
=> DrawStringToSurface(surfaceSlot, x, y, text);
void ClearRenderedAdvTextLayout(int layoutSlot) { }
void ResetRenderedAdvTextLayout(
GfxState gfx, AdvTextLayoutPresentationBinding binding)
=> ClearRenderedAdvTextLayout(binding.LayoutSlot);
void RenderTextHistory(AdvTextHistoryRenderBatch batch) { }
// History render batches are transient bindings, unlike the retained backlog itself. HISTORY.BIN's
// recording re-enable at exit ends that presentation and drops every bound target layout.
@@ -87,10 +99,15 @@ public interface IHost
void SetAdvWaitIndicatorEnabled(bool enabled) { }
// Op 0x20a republishes one retained ADV text layout and includes the current marker frame when active.
void PublishAdvTextLayout(int layoutSlot) { }
void PublishAdvTextLayout(
GfxState gfx, AdvTextLayoutPresentationBinding binding)
=> PublishAdvTextLayout(binding.LayoutSlot);
// Op 0x199 temporarily yields the active ADV page into its registered hide-window coroutine.
// The retained scene continues to render, but the text layout and its wait marker are suspended
// until op 0x7c restores the saved page PC.
void SetAdvPagePresentationSuspended(bool suspended) { }
void SetAdvPagePresentationSuspended(GfxState gfx, bool suspended)
=> SetAdvPagePresentationSuspended(suspended);
void WaitForInput();
void WaitForInput(int layoutSlot) => WaitForInput();
// Interactive hosts service script callbacks on the VM thread while the enclosing ADV page remains

View File

@@ -30,7 +30,21 @@ public readonly record struct AdvTextLayoutPresentationBinding(
int SourceSurfaceSlot,
long FirstObjectHandle,
long ObjectCapacity,
long WaitIndicatorObjectHandle);
long WaitIndicatorObjectHandle,
int ResetCursorX,
int ResetCursorY);
public readonly record struct AdvRetainedGlyphPlacement(
AdvRetainedGlyphRecord Record,
int LayoutOriginX,
int LayoutOriginY);
public readonly record struct AdvRetainedTextRunResult(
int LayoutSlot,
int FirstGlyphIndex,
int GlyphCount,
int CursorX,
int CursorY);
/// <summary>Platform-neutral rules established from AGE's native retained-glyph workers.</summary>
public static class AdvRetainedTextContract

View File

@@ -201,7 +201,8 @@ public sealed class AdvTextHistory
var layout = GetOrCreateLayout(slot);
return new AdvTextLayoutPresentationBinding(
slot, checked(slot + 0x14), layout.TextObjectRangeFirst,
layout.TextObjectRangeCount, layout.WaitIndicatorObjectHandle);
layout.TextObjectRangeCount, layout.WaitIndicatorObjectHandle,
layout.ResetCursorX, layout.ResetCursorY);
}
/// <summary>

View File

@@ -0,0 +1,86 @@
using Age.Engine.Model;
namespace Age.Engine.Text;
/// <summary>
/// Transient live-layout presentation state. History persistence owns neither this list nor its Gfx
/// bindings; saved-frame script reconstruction rebuilds both.
/// </summary>
public sealed class RetainedAdvTextLayoutPresentation
{
private readonly List<AdvRetainedGlyphPlacement> _glyphs = new();
public RetainedAdvTextLayoutPresentation(AdvTextLayoutPresentationBinding binding)
{
if (binding.LayoutSlot <= 0)
throw new ArgumentOutOfRangeException(nameof(binding));
if (binding.SourceSurfaceSlot < 0)
throw new ArgumentOutOfRangeException(nameof(binding));
if (binding.FirstObjectHandle < 0 || binding.ObjectCapacity <= 0)
throw new ArgumentException(
"A retained ADV layout requires a nonnegative first handle and positive capacity.",
nameof(binding));
Binding = binding;
}
public AdvTextLayoutPresentationBinding Binding { get; }
public IReadOnlyList<AdvRetainedGlyphPlacement> Glyphs => _glyphs;
public int PublishedGlyphCount { get; private set; }
public int PublishableGlyphCount
=> (int)Math.Min(_glyphs.Count, Binding.ObjectCapacity);
public int Append(
IReadOnlyList<AdvRetainedGlyphRecord> records,
int layoutOriginX,
int layoutOriginY)
{
ArgumentNullException.ThrowIfNull(records);
int first = _glyphs.Count;
foreach (AdvRetainedGlyphRecord record in records)
_glyphs.Add(new AdvRetainedGlyphPlacement(
record, layoutOriginX, layoutOriginY));
return first;
}
/// <summary>Publish newly revealed glyphs through a layout-global target count.</summary>
public int PublishThrough(GfxState gfx, int targetGlyphCount)
{
ArgumentNullException.ThrowIfNull(gfx);
int target = Math.Clamp(targetGlyphCount, 0, PublishableGlyphCount);
for (int index = PublishedGlyphCount; index < target; index++)
Bind(gfx, index);
PublishedGlyphCount = Math.Max(PublishedGlyphCount, target);
return PublishedGlyphCount;
}
/// <summary>Recreate every currently revealed binding after op 0x20a or temporary suspension.</summary>
public int Republish(GfxState gfx)
{
ArgumentNullException.ThrowIfNull(gfx);
for (int index = 0; index < PublishedGlyphCount; index++)
Bind(gfx, index);
return PublishedGlyphCount;
}
public void ErasePublished(GfxState gfx)
{
ArgumentNullException.ThrowIfNull(gfx);
gfx.EraseRange(Binding.FirstObjectHandle, Binding.ObjectCapacity);
}
private void Bind(GfxState gfx, int index)
{
AdvRetainedGlyphPlacement placement = _glyphs[index];
AdvRetainedGlyphRecord record = placement.Record;
if (record.Width <= 0 || record.Height <= 0) return;
gfx.BindDraw(
checked(Binding.FirstObjectHandle + index),
Binding.SourceSurfaceSlot,
record.Left,
record.Top,
record.Width,
record.Height,
checked(placement.LayoutOriginX + record.Left),
checked(placement.LayoutOriginY + record.Top));
}
}

View File

@@ -1809,7 +1809,7 @@ public sealed class VirtualMachine
{
_cur.CoroutineResumePc = pc + 1;
_cur.CoroutineYieldActive = true;
_host.SetAdvPagePresentationSuspended(true);
_host.SetAdvPagePresentationSuspended(Gfx, true);
targetOffset = _cur.CoroutineYieldHandlerA;
}
else targetOffset = _cur.CoroutineYieldHandlerB;
@@ -1824,7 +1824,7 @@ public sealed class VirtualMachine
{
_cur.CoroutineResumePc = null;
_cur.CoroutineYieldActive = false;
_host.SetAdvPagePresentationSuspended(false);
_host.SetAdvPagePresentationSuspended(Gfx, false);
return resumePc;
}
return pc + 1; // cold bounded scene-entry path
@@ -1991,7 +1991,13 @@ public sealed class VirtualMachine
var liveRun = new AdvLiveTextRun(
off, TextHistory.GetLayoutSnapshot(layoutSlot), _advTextStyle, text, scriptStack);
TextHistory.AppendText(layoutSlot, off, text, _advTextStyle);
_host.ShowText(liveRun, _messageGlyphDelayMilliseconds);
AdvTextLayoutPresentationBinding binding =
TextHistory.GetPresentationBinding(liveRun.Layout.Slot);
AdvRetainedTextRunResult? retained = _host.ShowText(
Gfx, binding, liveRun, _messageGlyphDelayMilliseconds);
if (retained is { } result)
TextHistory.SetCursor(
result.LayoutSlot, result.CursorX, result.CursorY);
}
return pc + 1;
case "define-adv-text-layout": // 0x70: configure layout and begin a logical retained group
@@ -2003,8 +2009,12 @@ public sealed class VirtualMachine
int requestedSlot = (int)Read(a[0]);
TextHistory.ResetLayout(requestedSlot);
var layout = TextHistory.GetLayoutSnapshot(requestedSlot);
AdvTextLayoutPresentationBinding binding =
TextHistory.GetPresentationBinding(layout.Slot);
if (binding.FirstObjectHandle >= 0 && binding.ObjectCapacity > 0)
Gfx.EraseRange(binding.FirstObjectHandle, binding.ObjectCapacity);
_host.SetAdvTextCursor(layout.Slot, layout.CursorX, layout.CursorY);
_host.ClearRenderedAdvTextLayout(layout.Slot);
_host.ResetRenderedAdvTextLayout(Gfx, binding);
_cur.ReadMessageOffset = ins.Offset;
_sharedProfile.ReadText.CommitPending();
RefreshAdvReadSkipState();
@@ -2031,8 +2041,13 @@ public sealed class VirtualMachine
return pc + 1;
case "u00420CE0":
case "publish-adv-text-layout": // 0x20a: publish layout and current marker frame if active
_host.PublishAdvTextLayout((int)Read(a[0]));
{
int requestedSlot = (int)Read(a[0]);
AdvTextLayoutPresentationBinding binding =
TextHistory.GetPresentationBinding(requestedSlot);
_host.PublishAdvTextLayout(Gfx, binding);
return pc + 1;
}
case "draw-string": // 0x204 (surface slot, x, y, string)
_host.DrawStringToSurface((int)Read(a[0]), (int)Read(a[1]), (int)Read(a[2]), ReadStr(a[3]),
_advTextStyle);