Skip to content

Commit

Permalink
Render sprite atlas test
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed Jul 29, 2023
1 parent b23744c commit 7ac7aaf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
35 changes: 31 additions & 4 deletions src/platform/qt/src/widget/sprite_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ SpriteViewer::SpriteViewer(nba::CoreBase* core, QWidget* parent) : QWidget{paren
layout->addWidget(box);
}

atlas_canvas = new QWidget{};
atlas_canvas->setFixedSize(1024, 512);
layout->addWidget(atlas_canvas);
atlas_canvas->installEventFilter(this);

setLayout(layout);

// @todo: move this out of here
Expand All @@ -108,7 +113,9 @@ SpriteViewer::SpriteViewer(nba::CoreBase* core, QWidget* parent) : QWidget{paren
void SpriteViewer::Update() {
const int index = sprite_index_input->value();

RenderSprite(index, (u32*)image_rgb32.bits());
RenderSpriteAtlas();

RenderSprite(index, (u32*)image_rgb32.bits(), 64);

const int offset = index << 3;

Expand Down Expand Up @@ -207,7 +214,7 @@ void SpriteViewer::Update() {
canvas->update();
}

void SpriteViewer::RenderSprite(int index, u32* buffer) {
void SpriteViewer::RenderSprite(int index, u32* buffer, int stride) {
static constexpr int k_sprite_size[4][4][2] = {
{ { 8 , 8 }, { 16, 16 }, { 32, 32 }, { 64, 64 } }, // Square
{ { 16, 8 }, { 32, 8 }, { 32, 16 }, { 64, 32 } }, // Horizontal
Expand Down Expand Up @@ -254,7 +261,7 @@ void SpriteViewer::RenderSprite(int index, u32* buffer) {
for(int y = 0; y < 8; y++) {
u64 tile_data = nba::read<u64>(vram, tile_address);

u32* dst = &buffer[tile_y << 9 | y << 6 | tile_x << 3];
u32* dst = &buffer[((tile_y << 3) * stride) | (y * stride) | tile_x << 3];

for(int x = 0; x < 8; x++) {
*dst++ = RGB555(palette[tile_data & 255u]);
Expand Down Expand Up @@ -285,7 +292,7 @@ void SpriteViewer::RenderSprite(int index, u32* buffer) {
for(int y = 0; y < 8; y++) {
u32 tile_data = nba::read<u32>(vram, tile_address);

u32* dst = &buffer[tile_y << 9 | y << 6 | tile_x << 3];
u32* dst = &buffer[((tile_y << 3) * stride) | (y * stride) | tile_x << 3];

for(int x = 0; x < 8; x++) {
*dst++ = RGB555(palette[tile_data & 15u]);
Expand All @@ -299,6 +306,20 @@ void SpriteViewer::RenderSprite(int index, u32* buffer) {
}
}

void SpriteViewer::RenderSpriteAtlas() {
int sprite_index = 0;

atlas_image_rgb32.fill(0xFF000000u);

for(int y = 0; y < 8; y++) {
for(int x = 0; x < 16; x++) {
RenderSprite(sprite_index++, &((u32*)atlas_image_rgb32.bits())[y * 65536 + x * 64], 1024);
}
}

atlas_canvas->update();
}

bool SpriteViewer::eventFilter(QObject* object, QEvent* event) {
if(object == canvas && event->type() == QEvent::Paint) {
const QRect src_rect{0, 0, sprite_width, sprite_height};
Expand All @@ -309,5 +330,11 @@ bool SpriteViewer::eventFilter(QObject* object, QEvent* event) {
return true;
}

if(object == atlas_canvas && event->type() == QEvent::Paint) {
QPainter painter{atlas_canvas};
painter.drawImage(0, 0, atlas_image_rgb32);
return true;
}

return false;
}
6 changes: 5 additions & 1 deletion src/platform/qt/src/widget/sprite_viewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public slots:
void Update();

private:
void RenderSprite(int index, u32* buffer);
void RenderSprite(int index, u32* buffer, int stride);
void RenderSpriteAtlas();

nba::CoreBase* core;
u16* pram;
Expand All @@ -38,6 +39,9 @@ public slots:
QSpinBox* magnification_input;
QWidget* canvas;

QImage atlas_image_rgb32{1024, 512, QImage::Format_RGB32};
QWidget* atlas_canvas;

QCheckBox* check_sprite_enabled;
QLabel* label_sprite_position;
QLabel* label_sprite_size;
Expand Down

0 comments on commit 7ac7aaf

Please sign in to comment.