Skip to content

Commit

Permalink
Have the TileViewer use MSXPalette
Browse files Browse the repository at this point in the history
  • Loading branch information
turbor committed May 23, 2022
1 parent 157b303 commit 20a221a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 88 deletions.
3 changes: 3 additions & 0 deletions src/PaletteView.ui
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@
</item>
<item>
<widget class="QCheckBox" name="cb_autosync">
<property name="toolTip">
<string>autosync to openMSX</string>
</property>
<property name="text">
<string>Autosync</string>
</property>
Expand Down
48 changes: 4 additions & 44 deletions src/TileViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,6 @@
#include <QMessageBox>


// static to feed to PaletteDialog and be used when VDP colors aren't selected
uint8_t TileViewer::defaultPalette[32] = {
// RB G
0x00, 0,
0x00, 0,
0x11, 6,
0x33, 7,
0x17, 1,
0x27, 3,
0x51, 1,
0x27, 6,
0x71, 1,
0x73, 3,
0x61, 6,
0x64, 6,
0x11, 4,
0x65, 2,
0x55, 5,
0x77, 7,
};


TileViewer::TileViewer(QWidget* parent)
Expand All @@ -43,7 +23,6 @@ TileViewer::TileViewer(QWidget* parent)
connect(cb_color0, &QCheckBox::stateChanged, this, &TileViewer::on_cb_color0_stateChanged);
connect(useVDPRegisters, &QCheckBox::stateChanged, this, &TileViewer::on_useVDPRegisters_stateChanged);

connect(editPaletteButton, &QPushButton::clicked, this, &TileViewer::on_editPaletteButton_clicked);
connect(useVDPPalette, &QCheckBox::stateChanged, this, &TileViewer::on_useVDPPalette_stateChanged);
connect(zoomLevel, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &TileViewer::on_zoomLevel_valueChanged);

Expand Down Expand Up @@ -83,7 +62,7 @@ TileViewer::TileViewer(QWidget* parent)
scrollArea->setWidget(imageWidget);

const auto* vram = VDPDataStore::instance().getVramPointer();
const auto* palette = VDPDataStore::instance().getPalettePointer();
auto* palette = VDPDataStore::instance().getPalette(paletteVDP);
//imageWidget->setNameTableAddress(0);
//imageWidget->setPatternTableAddress(0);
//imageWidget->setColorTableAddress(0);
Expand All @@ -106,7 +85,7 @@ TileViewer::TileViewer(QWidget* parent)
void TileViewer::VDPDataStoreDataRefreshed()
{
if (useVDPPalette->isChecked()) {
imageWidget->setPaletteSource(VDPDataStore::instance().getPalettePointer());
imageWidget->setPaletteSource(VDPDataStore::instance().getPalette(paletteVDP));
}
decodeVDPregs();
}
Expand Down Expand Up @@ -336,33 +315,14 @@ void TileViewer::on_useVDPRegisters_stateChanged(int state)
cb_color0->setEnabled(useManual);
}

void TileViewer::on_editPaletteButton_clicked(bool /*checked*/)
{
auto* p = new PaletteDialog();
p->setPalette(defaultPalette);
p->setAutoSync(true);
connect(p, &PaletteDialog::paletteSynced, imageWidget, &VramTiledView::refresh);
connect(p, &PaletteDialog::paletteSynced, this, &TileViewer::update_label_characterimage);
p->show();
//useVDPPalette->setChecked(false);
//QMessageBox::information(
// this,
// "Not yet implemented",
// "Sorry, the palette editor is not yet implemented, "
// "only disabling 'Use VDP palette registers' for now");
}

void TileViewer::on_useVDPPalette_stateChanged(int state)
{
const uint8_t* palette = VDPDataStore::instance().getPalettePointer();
if (state) {
imageWidget->setPaletteSource(palette);
imageWidget->setPaletteSource(VDPDataStore::instance().getPalette(paletteVDP));
} else {
if (palette) memcpy(defaultPalette, palette, 32);
imageWidget->setPaletteSource(defaultPalette);
imageWidget->setPaletteSource(VDPDataStore::instance().getPalette(paletteTiles));
}
imageWidget->refresh();
editPaletteButton->setEnabled(!state);
}

void TileViewer::on_zoomLevel_valueChanged(double d)
Expand Down
2 changes: 0 additions & 2 deletions src/TileViewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class TileViewer : public QDialog, private Ui::TileViewer
void on_cb_color0_stateChanged(int state);
void on_useVDPRegisters_stateChanged(int state);

void on_editPaletteButton_clicked(bool checked);
void on_useVDPPalette_stateChanged(int state);
void on_zoomLevel_valueChanged(double d);

Expand All @@ -53,7 +52,6 @@ class TileViewer : public QDialog, private Ui::TileViewer
int mouseOverY = 0;
int mouseOverChar = 0;

static uint8_t defaultPalette[32];
};

#endif // TILEVIEWER_H
16 changes: 3 additions & 13 deletions src/TileViewer.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>911</width>
<width>1070</width>
<height>756</height>
</rect>
</property>
Expand Down Expand Up @@ -50,8 +50,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>615</width>
<height>369</height>
<width>695</width>
<height>368</height>
</rect>
</property>
</widget>
Expand Down Expand Up @@ -559,16 +559,6 @@ NT: $196E (base+$16E)</string>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QPushButton" name="editPaletteButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Edit palette..</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="useVDPPalette">
<property name="text">
Expand Down
35 changes: 10 additions & 25 deletions src/VramTiledView.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "VramTiledView.h"
#include "VDPDataStore.h"
#include "MSXPalette.h"
#include "Convert.h"
#include "ranges.h"
#include <QPainter>
Expand All @@ -10,7 +11,8 @@ VramTiledView::VramTiledView(QWidget* parent)
: QWidget(parent)
, image(512, 512, QImage::Format_RGB32)
{
ranges::fill(msxPalette, qRgb(80, 80, 80));
palette = nullptr;
setPaletteSource(VDPDataStore::instance().getPalette(paletteVDP));
setZoom(1.0f);

// Mouse update events when mouse is moved over the image, Quibus likes this
Expand Down Expand Up @@ -80,21 +82,6 @@ void VramTiledView::decode()
update();
}

void VramTiledView::decodePalette()
{
if (!palette) return;
//printf("VramTiledView::decodePallet palletpointer %p \n", palette);

for (int i = 0; i < 16; ++i) {
int r = (palette[2 * i + 0] & 0xf0) >> 4;
int b = (palette[2 * i + 0] & 0x0f);
int g = (palette[2 * i + 1] & 0x0f);

auto scale = [](int x) { return (x >> 1) | (x << 2) | (x << 5); };
msxPalette[i] = qRgb(scale(r), scale(g), scale(b));
//printf("VramTiledView::decodePallet color %d => r %d g %d b %d \n", i, r, g, b);
}
}

void VramTiledView::decodePatternTable()
{
Expand Down Expand Up @@ -211,12 +198,7 @@ void VramTiledView::setPixel2x2(int x, int y, QRgb c)
QRgb VramTiledView::getColor(int c)
{
// TODO do we need to look at the TP bit???
return msxPalette[c ? c : (tpBit ? 0 : borderColor)];
}

const uint8_t *VramTiledView::getPaletteSource() const
{
return palette;
return palette->color(c ? c : (tpBit ? 0 : borderColor));
}

int VramTiledView::getScreenMode() const
Expand Down Expand Up @@ -434,8 +416,7 @@ void VramTiledView::paintEvent(QPaintEvent* /*event*/)

void VramTiledView::refresh()
{
decodePalette();
decode();
decode();
}

QString VramTiledView::byteAsPattern(uint8_t byte)
Expand Down Expand Up @@ -568,9 +549,13 @@ void VramTiledView::setColorTableAddress(int adr)
decode();
}

void VramTiledView::setPaletteSource(const uint8_t* adr)
void VramTiledView::setPaletteSource(MSXPalette *adr)
{
if (palette == adr) return;
if (palette) {
disconnect(palette, &MSXPalette::paletteChanged, this, &VramTiledView::decode);
}
palette = adr;
connect(palette, &MSXPalette::paletteChanged, this, &VramTiledView::decode);
refresh();
}
8 changes: 4 additions & 4 deletions src/VramTiledView.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <cstdint>
#include <optional>

class MSXPalette;

class VramTiledView : public QWidget
{
Q_OBJECT
Expand All @@ -23,7 +25,7 @@ class VramTiledView : public QWidget
void setNameTableAddress(int adr);
void setPatternTableAddress(int adr);
void setColorTableAddress(int adr);
void setPaletteSource(const uint8_t *adr);
void setPaletteSource(MSXPalette *adr);
void setBorderColor(int value);
void setDrawGrid(bool value);
void setTpBit(bool value);
Expand Down Expand Up @@ -59,7 +61,6 @@ class VramTiledView : public QWidget
void paintEvent(QPaintEvent* e) override;

void decode();
void decodePalette();
void decodePatternTable();
void decodeNameTable();
void overLayNameTable();
Expand All @@ -83,10 +84,9 @@ class VramTiledView : public QWidget
QString byteAsPattern(uint8_t byte);

private:
QRgb msxPalette[16];
QImage image;
QPixmap pixImage;
const uint8_t* palette = nullptr;
MSXPalette* palette = nullptr;
const uint8_t* vramBase = nullptr;
float zoomFactor;
bool drawGrid = true;
Expand Down

0 comments on commit 20a221a

Please sign in to comment.