implements PLN-0078 Classic2D packet consumer

This commit is contained in:
2026-05-25 14:07:41 +01:00
parent 5e3cb87b57
commit 57439846c5
4 changed files with 79 additions and 4 deletions
+71 -1
View File
@@ -1,11 +1,11 @@
use crate::memory_banks::GlyphBankPoolAccess;
use prometeu_hal::GfxBridge;
use prometeu_hal::color::Color;
use prometeu_hal::glyph::Glyph;
use prometeu_hal::glyph_bank::GlyphBank;
use prometeu_hal::scene_viewport_cache::{CachedTileEntry, SceneViewportCache};
use prometeu_hal::scene_viewport_resolver::{LayerCopyRequest, ResolverUpdate};
use prometeu_hal::sprite::Sprite;
use prometeu_hal::{Game2DFramePacket, Gfx2dCommand, GfxBridge};
use std::sync::Arc;
/// Blending modes inspired by classic 16-bit hardware.
@@ -686,6 +686,58 @@ impl Gfx {
Self::apply_fade_to_buffer(&mut self.back, self.hud_fade_level, self.hud_fade_color);
}
pub fn render_game2d_frame_packet(&mut self, packet: &Game2DFramePacket) {
let sprites: Vec<Sprite> = packet
.composer
.sprites
.iter()
.map(|sprite| Sprite {
glyph: Glyph {
glyph_id: sprite.glyph_id as u16,
palette_id: sprite.palette_id as u8,
},
x: sprite.x,
y: sprite.y,
layer: sprite.layer as u8,
bank_id: sprite.bank_id as u8,
active: true,
flip_x: sprite.flip_x,
flip_y: sprite.flip_y,
priority: sprite.priority as u8,
})
.collect();
self.load_frame_sprites(&sprites);
self.render_no_scene_frame();
for command in &packet.gfx2d {
self.apply_gfx2d_command(command);
}
}
fn apply_gfx2d_command(&mut self, command: &Gfx2dCommand) {
match command {
Gfx2dCommand::Clear { color } => self.clear(*color),
Gfx2dCommand::FillRect { rect, color } => {
self.fill_rect(rect.x, rect.y, rect.w, rect.h, *color);
}
Gfx2dCommand::DrawLine { x0, y0, x1, y1, color } => {
self.draw_line(*x0, *y0, *x1, *y1, *color);
}
Gfx2dCommand::DrawCircle { x, y, radius, color } => {
self.draw_circle(*x, *y, *radius, *color);
}
Gfx2dCommand::DrawDisc { x, y, radius, border_color, fill_color } => {
self.draw_disc(*x, *y, *radius, *border_color, *fill_color);
}
Gfx2dCommand::DrawSquare { rect, border_color, fill_color } => {
self.draw_square(rect.x, rect.y, rect.w, rect.h, *border_color, *fill_color);
}
Gfx2dCommand::DrawText { x, y, text, color } => {
self.draw_text(*x, *y, text, *color);
}
}
}
fn populate_layer_buckets(&mut self) {
for bucket in self.layer_buckets.iter_mut() {
bucket.clear();
@@ -1331,4 +1383,22 @@ mod tests {
assert_eq!(gfx.sprites[0].layer, 1);
assert_eq!(gfx.sprites[1].glyph.glyph_id, 5);
}
#[test]
fn render_game2d_frame_packet_applies_primitive_commands() {
let mut gfx = Gfx::new(8, 8, Arc::new(MemoryBanks::default()));
let packet = Game2DFramePacket::new(
Default::default(),
vec![Gfx2dCommand::FillRect {
rect: prometeu_hal::primitives::Rect { x: 1, y: 1, w: 2, h: 2 },
color: Color::RED,
}],
);
gfx.render_game2d_frame_packet(&packet);
gfx.present();
assert_eq!(gfx.front_buffer()[1 + 8], Color::RED.raw());
assert_eq!(gfx.front_buffer()[2 + 2 * 8], Color::RED.raw());
}
}
@@ -73,7 +73,12 @@ impl HardwareBridge for Hardware {
}
fn render_frame(&mut self) {
self.frame_composer.render_frame(&mut self.gfx);
if self.frame_composer.active_scene_id().is_none() {
let packet = self.frame_composer.close_game2d_packet();
self.gfx.render_game2d_frame_packet(&packet);
} else {
self.frame_composer.render_frame(&mut self.gfx);
}
}
fn has_glyph_bank(&self, bank_id: usize) -> bool {