Skip to content

Add oam_begin() and OamFrame ref struct to neslib #429

@jonathanpeppers

Description

@jonathanpeppers

Context

This is Phase 2 of the using var pattern support (#427). Depends on #428 (try/finally support).

Proposed API

Add to NESLib.cs:

/// Returns an OamFrame that auto-hides remaining sprites on Dispose
public static OamFrame oam_begin() => throw null!;

Add new ref struct (can live in NESLib.cs or a separate file):

/// RAII scope for OAM sprite drawing.
/// Dispose() calls oam_hide_rest(oam_off).
public ref struct OamFrame
{
    public void Dispose() => throw null!;
}

Why ref struct

Using ref struct instead of a regular struct is critical:

  • The compiler emits a direct call to Dispose() instead of callvirt through IDisposable
  • No boxing, no interface dispatch, no heap allocation
  • The IL is simpler for the transpiler to recognize

Usage

using var frame = oam_begin();
oam_meta_spr_pal(x, y, pal, sprite);
oam_spr(x2, y2, tile, attr, oam_off);
// Dispose() auto-calls oam_hide_rest(oam_off) at end of scope

Part of #427

Metadata

Metadata

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions