openMSX
ImGuiSpriteViewer.cc
Go to the documentation of this file.
2
3#include "ImGuiCpp.hh"
4#include "ImGuiManager.hh"
5#include "ImGuiPalette.hh"
6#include "ImGuiUtils.hh"
7
8#include "MSXMotherBoard.hh"
9#include "SpriteChecker.hh"
10#include "SpriteConverter.hh"
11#include "VDP.hh"
12#include "VDPVRAM.hh"
13
14#include "xrange.hh"
15
16#include <imgui.h>
17
18#include <cassert>
19#include <cstdint>
20#include <span>
21
22namespace openmsx {
23
24using namespace std::literals;
25
26void ImGuiSpriteViewer::save(ImGuiTextBuffer& buf)
27{
28 savePersistent(buf, *this, persistentElements);
29}
30
31void ImGuiSpriteViewer::loadLine(std::string_view name, zstring_view value)
32{
33 loadOnePersistent(name, value, *this, persistentElements);
34}
35
36static void draw8(uint8_t pattern, uint32_t fgCol, uint32_t bgCol, std::span<uint32_t, 8> out)
37{
38 out[0] = (pattern & 0x80) ? fgCol : bgCol;
39 out[1] = (pattern & 0x40) ? fgCol : bgCol;
40 out[2] = (pattern & 0x20) ? fgCol : bgCol;
41 out[3] = (pattern & 0x10) ? fgCol : bgCol;
42 out[4] = (pattern & 0x08) ? fgCol : bgCol;
43 out[5] = (pattern & 0x04) ? fgCol : bgCol;
44 out[6] = (pattern & 0x02) ? fgCol : bgCol;
45 out[7] = (pattern & 0x01) ? fgCol : bgCol;
46}
47
48static void renderPatterns8(const VramTable& pat, std::span<uint32_t> output)
49{
50 auto zero = getColor(imColor::TRANSPARENT);
51 auto one = getColor(imColor::TEXT);
52 for (auto row : xrange(8)) {
53 for (auto column : xrange(32)) {
54 auto patNum = 32 * row + column;
55 auto offset = 8 * patNum;
56 for (auto y : xrange(8)) {
57 auto pattern = pat[offset + y];
58 auto out = subspan<8>(output, (8 * row + y) * 256 + 8 * column);
59 draw8(pattern, one, zero, out);
60 }
61 }
62 }
63}
64
65static void renderPatterns16(const VramTable& pat, std::span<uint32_t> output)
66{
67 auto zero = getColor(imColor::TRANSPARENT);
68 auto one = getColor(imColor::TEXT);
69 for (auto row : xrange(4)) {
70 for (auto column : xrange(16)) {
71 auto patNum = 4 * (16 * row + column);
72 auto offset = 8 * patNum;
73 for (auto y : xrange(16)) {
74 auto patternA = pat[offset + y + 0];
75 auto patternB = pat[offset + y + 16];
76 auto outA = subspan<8>(output, (16 * row + y) * 256 + 16 * column + 0);
77 auto outB = subspan<8>(output, (16 * row + y) * 256 + 16 * column + 8);
78 draw8(patternA, one, zero, outA);
79 draw8(patternB, one, zero, outB);
80 }
81 }
82 }
83}
84
85[[nodiscard]] static int getSpriteAttrAddr(int sprite, int mode)
86{
87 return (mode == 2 ? 512 : 0) + 4 * sprite;
88}
89[[nodiscard]] static int getSpriteColorAddr(int sprite, int mode)
90{
91 assert(mode == 2); (void)mode;
92 return 16 * sprite;
93}
94
95static void renderSpriteAttrib(const VramTable& att, int sprite, int mode, int size, int transparent,
96 float zoom, std::span<uint32_t, 16> palette, void* patternTex)
97{
98 int addr = getSpriteAttrAddr(sprite, mode);
99 int pattern = att[addr + 2];
100 if (size == 16) pattern /= 4;
101
102 int patternsPerRow = 256 / size;
103 int cc = pattern % patternsPerRow;
104 int rr = pattern / patternsPerRow;
105 float u1 = float(cc + 0) / float(patternsPerRow);
106 float u2 = float(cc + 1) / float(patternsPerRow);
107 float v1 = float(size * rr) * (1.0f / 64.0f);
108
109 auto getColor = [&](int color) -> gl::vec4 {
110 if (color == 0 && transparent) return {};
111 return ImGui::ColorConvertU32ToFloat4(palette[color]);
112 };
113
114 if (mode == 1) {
115 auto attrib = att[addr + 3];
116 auto color = attrib & 0x0f;
117 float v2 = float(size * (rr + 1)) * (1.0f / 64.0f);
118 ImGui::Image(patternTex, zoom * gl::vec2{float(size)}, {u1, v1}, {u2, v2}, getColor(color));
119 } else {
120 int colorBase = getSpriteColorAddr(sprite, mode);
121 gl::vec2 pos = ImGui::GetCursorPos();
122 for (auto y : xrange(size)) {
123 auto attrib = att[colorBase + y];
124 auto color = attrib & 0x0f;
125 ImGui::SetCursorPos({pos.x, pos.y + zoom * float(y)});
126 float v2 = v1 + (1.0f / 64.0f);
127 ImGui::Image(patternTex, zoom * gl::vec2{float(size), 1.0f}, {u1, v1}, {u2, v2}, getColor(color));
128 v1 = v2;
129 }
130 }
131}
132
134{
135 if (!show || !motherBoard) return;
136
137 ImGui::SetNextWindowSize({748, 1010}, ImGuiCond_FirstUseEver);
138 im::Window("Sprite viewer", &show, [&]{
139 auto* vdp = dynamic_cast<VDP*>(motherBoard->findDevice("VDP")); // TODO name based OK?
140 if (!vdp) return;
141 const auto& vram = vdp->getVRAM().getData();
142
143 auto modeToStr = [](int mode) {
144 if (mode == 0) return "no sprites";
145 if (mode == 1) return "1";
146 if (mode == 2) return "2";
147 assert(false); return "ERROR";
148 };
149 auto sizeToStr = [](int size) {
150 if (size == 8) return "8 x 8";
151 if (size == 16) return "16 x 16";
152 assert(false); return "ERROR";
153 };
154 auto yesNo = [](int x) {
155 if (x == 0) return "no";
156 if (x == 1) return "yes";
157 assert(false); return "ERROR";
158 };
159 auto attMult = [](int mode) { return 1 << ((mode == 2) ? 10 : 7); };
160
161 bool isMSX1 = vdp->isMSX1VDP();
162 auto displayMode = vdp->getDisplayMode();
163 bool planar = displayMode.isPlanar();
164 int vdpMode = displayMode.getSpriteMode(isMSX1);
165 int vdpVerticalScroll = vdp->getVerticalScroll();
166 int vdpLines = vdp->getNumberOfLines();
167
168 int vdpSize = vdp->getSpriteSize();
169 int vdpMag = vdp->isSpriteMag();
170 int vdpTransparent = vdp->getTransparency();
171
172 int vdpPatBase = vdp->getSpritePatternTableBase();
173 int vdpAttBase = vdp->getSpriteAttributeTableBase() & ~(attMult(vdpMode) - 1);
174
175 auto vramSize = std::min(vdp->getVRAM().getSize(), 0x20000u); // max 128kB
176
177 std::array<uint32_t, 16> palette;
178 auto msxPalette = manager.palette->getPalette(vdp);
179 ranges::transform(msxPalette, palette.data(),
180 [](uint16_t msx) { return ImGuiPalette::toRGBA(msx); });
181 // TODO? if (color0 < 16) palette[0] = palette[color0];
182
183 bool manMode = overrideAll || overrideMode;
184 bool manSize = overrideAll || overrideSize;
185 bool manMag = overrideAll || overrideMag;
186 bool manTrans = overrideAll || overrideTrans;
187 bool manPat = overrideAll || overridePat;
188 bool manAtt = overrideAll || overrideAtt;
189 bool manScroll = overrideAll || overrideScroll;
190 bool manLines = overrideAll || overrideLines;
191
192 im::TreeNode("Settings", ImGuiTreeNodeFlags_DefaultOpen, [&]{
193 im::Group([&]{
194 ImGui::TextUnformatted("VDP settings");
195 im::Disabled(manMode, [&]{
196 ImGui::AlignTextToFramePadding();
197 ImGui::StrCat("Sprite mode: ", modeToStr(vdpMode));
198 });
199 im::Disabled(manSize, [&]{
200 ImGui::AlignTextToFramePadding();
201 ImGui::StrCat("Sprite size: ", sizeToStr(vdpSize));
202 });
203 im::Disabled(manMag, [&]{
204 ImGui::AlignTextToFramePadding();
205 ImGui::StrCat("Sprites magnified: ", yesNo(vdpMag));
206 });
207 im::Disabled(manTrans, [&]{
208 ImGui::AlignTextToFramePadding();
209 ImGui::StrCat("Color 0 transparent: ", yesNo(vdpTransparent));
210 });
211 im::Disabled(manPat, [&]{
212 ImGui::AlignTextToFramePadding();
213 ImGui::StrCat("Pattern table: 0x", hex_string<5>(vdpPatBase));
214 });
215 im::Disabled(manAtt, [&]{
216 ImGui::AlignTextToFramePadding();
217 ImGui::StrCat("Attribute table: 0x", hex_string<5>(vdpAttBase | (vdpMode == 2 ? 512 : 0)));
218 });
219 im::Disabled(manScroll, [&]{
220 ImGui::AlignTextToFramePadding();
221 ImGui::StrCat("Vertical scroll: ", vdpVerticalScroll);
222 });
223 im::Disabled(manLines, [&]{
224 ImGui::AlignTextToFramePadding();
225 ImGui::StrCat("Visible lines: ", (vdpLines == 192) ? "192" : "212");
226 });
227 });
228 ImGui::SameLine();
229 im::Group([&]{
230 ImGui::Checkbox("Manual override", &overrideAll);
231 im::Group([&]{
232 im::Disabled(overrideAll, [&]{
233 ImGui::Checkbox("##o-mode", overrideAll ? &overrideAll : &overrideMode);
234 ImGui::Checkbox("##o-size", overrideAll ? &overrideAll : &overrideSize);
235 ImGui::Checkbox("##o-mag", overrideAll ? &overrideAll : &overrideMag);
236 ImGui::Checkbox("##o-trans", overrideAll ? &overrideAll : &overrideTrans);
237 ImGui::Checkbox("##o-pat", overrideAll ? &overrideAll : &overridePat);
238 ImGui::Checkbox("##o-att", overrideAll ? &overrideAll : &overrideAtt);
239 ImGui::Checkbox("##o-scroll", overrideAll ? &overrideAll : &overrideScroll);
240 ImGui::Checkbox("##o-lines", overrideAll ? &overrideAll : &overrideLines);
241 });
242 });
243 ImGui::SameLine();
244 im::Group([&]{
245 im::ItemWidth(ImGui::GetFontSize() * 9.0f, [&]{
246 im::Disabled(!manMode, [&]{
247 if (manMode && isMSX1) manualMode = 1;
248 im::Combo("##mode", modeToStr(manualMode), [&]{
249 if (ImGui::Selectable("1")) manualMode = 1;
250 if (!isMSX1 && ImGui::Selectable("2")) manualMode = 2;
251 });
252 });
253 im::Disabled(!manSize, [&]{
254 im::Combo("##size", sizeToStr(manualSize), [&]{
255 if (ImGui::Selectable(" 8 x 8")) manualSize = 8;
256 if (ImGui::Selectable("16 x 16")) manualSize = 16;
257 });
258 });
259 im::Disabled(!manMag, [&]{
260 im::Combo("##mag", yesNo(manualMag), [&]{
261 if (ImGui::Selectable("no")) manualMag = 0;
262 if (ImGui::Selectable("yes")) manualMag = 1;
263 });
264 });
265 im::Disabled(!manTrans, [&]{
266 im::Combo("##trans", yesNo(manualTransparent), [&]{
267 if (ImGui::Selectable("no")) manualTransparent = 0;
268 if (ImGui::Selectable("yes")) manualTransparent = 1;
269 });
270 });
271 im::Disabled(!manPat, [&]{
272 comboHexSequence<5>("##pat", &manualPatBase, 8 * 256, vramSize, 0);
273 });
274 im::Disabled(!manAtt, [&]{
275 comboHexSequence<5>("##att", &manualAttBase, attMult(manualMode), vramSize, manualMode == 2 ? 512 : 0);
276 });
277 im::Disabled(!manScroll, [&]{
278 ImGui::InputInt("##verticalScroll", &manualVerticalScroll);
279 manualVerticalScroll &= 0xff;
280 });
281 im::Disabled(!manLines, [&]{
282 ImGui::Combo("##lines", &manualLines, "192\000212\000256\000");
283 });
284 });
285 });
286 });
287 ImGui::SameLine();
288 ImGui::Dummy(ImVec2(25, 1));
289 ImGui::SameLine();
290 im::Group([&]{
291 ImGui::SetNextItemWidth(ImGui::GetFontSize() * 10.0f);
292 ImGui::Combo("Palette", &manager.palette->whichPalette, "VDP\000Custom\000Fixed\000");
293 if (ImGui::Button("Open palette editor")) {
294 manager.palette->window.raise();
295 }
296 ImGui::Separator();
297 ImGui::SetNextItemWidth(ImGui::GetFontSize() * 3.0f);
298 ImGui::Combo("Zoom", &zoom, "1x\0002x\0003x\0004x\0005x\0006x\0007x\0008x\000");
299 ImGui::Checkbox("grid", &grid);
300 ImGui::SameLine();
301 im::Disabled(!grid, [&]{
302 ImGui::ColorEdit4("Grid color", gridColor.data(),
303 ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_AlphaBar);
304 });
305
306 ImGui::TextUnformatted("Checkerboard:"sv);
307 simpleToolTip("Used as background in 'Sprite attribute' and 'Rendered sprites' view");
308 ImGui::SameLine();
309 ImGui::ColorEdit4("checkerboard color1", checkerBoardColor1.data(), ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel);
310 ImGui::SameLine();
311 ImGui::ColorEdit4("checkerboard color2", checkerBoardColor2.data(), ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel);
312 im::Indent([&]{
313 ImGui::SetNextItemWidth(ImGui::GetFontSize() * 6.0f);
314 ImGui::InputInt("size", &checkerBoardSize);
315 });
316 });
317 });
318 ImGui::Separator();
319
320 int mode = manMode ? manualMode : vdpMode;
321 int size = manSize ? manualSize : vdpSize;
322 int mag = manMag ? manualMag : vdpMag;
323 int verticalScroll = manScroll ? manualVerticalScroll : vdpVerticalScroll;
324 int lines = manLines ? (manualLines == 0 ? 192 :
325 manualLines == 1 ? 212 :
326 256)
327 : vdpLines;
328 int transparent = manTrans ? manualTransparent : vdpTransparent;
329
330 VramTable patTable(vram, planar);
331 unsigned patReg = (manPat ? (manualPatBase | ((8 * 256) - 1)) : vdp->getSpritePatternTableBase()) >> 11;
332 patTable.setRegister(patReg, 11);
333 patTable.setIndexSize(11);
334
335 VramTable attTable(vram, planar);
336 unsigned attReg = (manAtt ? (manualAttBase | (attMult(manualMode) - 1)) : vdp->getSpriteAttributeTableBase()) >> 7;
337 attTable.setRegister(attReg, 7);
338 attTable.setIndexSize((mode == 2) ? 10 : 7);
339
340 // create pattern texture
341 if (!patternTex.get()) {
342 patternTex = gl::Texture(false, false); // no interpolation, no wrapping
343 }
344 patternTex.bind();
345 std::array<uint32_t, 256 * 64> pixels;
346 if (mode != 0) {
347 if (size == 8) {
348 renderPatterns8 (patTable, pixels);
349 } else {
350 renderPatterns16(patTable, pixels);
351 }
352 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 64, 0,
353 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
354 } else {
355 pixels[0] = getColor(imColor::GRAY);
356 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0,
357 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
358 }
359
360 // create grid texture
361 int zm = 2 * (1 + zoom);
362 auto gColor = ImGui::ColorConvertFloat4ToU32(gridColor);
363 if (grid) {
364 auto gridSize = size * zm;
365 for (auto y : xrange(gridSize)) {
366 auto* line = &pixels[y * gridSize];
367 for (auto x : xrange(gridSize)) {
368 line[x] = (x == 0 || y == 0) ? gColor : 0;
369 }
370 }
371 if (!gridTex.get()) {
372 gridTex = gl::Texture(false, true); // no interpolation, with wrapping
373 }
374 gridTex.bind();
375 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gridSize, gridSize, 0,
376 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
377 }
378
379 // create checker board texture
380 if (checkerBoardSize) {
381 pixels[0] = pixels[3] = ImGui::ColorConvertFloat4ToU32(checkerBoardColor1);
382 pixels[1] = pixels[2] = ImGui::ColorConvertFloat4ToU32(checkerBoardColor2);
383 if (!checkerTex.get()) {
384 checkerTex = gl::Texture(false, true); // no interpolation, with wrapping
385 }
386 checkerTex.bind();
387 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0,
388 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
389 }
390
391 im::TreeNode("Sprite patterns", ImGuiTreeNodeFlags_DefaultOpen, [&]{
392 auto fullSize = gl::vec2(256, 64) * float(zm);
393 im::Child("##pattern", {0, fullSize.y}, 0, ImGuiWindowFlags_HorizontalScrollbar, [&]{
394 auto pos1 = ImGui::GetCursorPos();
395 gl::vec2 scrnPos = ImGui::GetCursorScreenPos();
396 ImGui::Image(patternTex.getImGui(), fullSize);
397 bool hovered = ImGui::IsItemHovered() && (mode != 0);
398 ImGui::SameLine();
399 im::Group([&]{
400 gl::vec2 zoomPatSize{float(size * zm)};
401 if (hovered) {
402 auto gridPos = trunc((gl::vec2(ImGui::GetIO().MousePos) - scrnPos) / zoomPatSize);
403 auto pattern = (size == 16) ? ((16 * gridPos.y) + gridPos.x) * 4
404 : ((32 * gridPos.y) + gridPos.x) * 1;
405 ImGui::StrCat("pattern: ", pattern);
406 auto recipPatTex = recip((size == 16) ? gl::vec2{16, 4} : gl::vec2{32, 8});
407 auto uv1 = gl::vec2(gridPos) * recipPatTex;
408 auto uv2 = uv1 + recipPatTex;
409 auto pos2 = ImGui::GetCursorPos();
410 int z = (size == 16) ? 3 : 6;
411 ImGui::Image(patternTex.getImGui(), float(z) * zoomPatSize, uv1, uv2);
412 if (grid) {
413 if (!zoomGridTex.get()) {
414 zoomGridTex = gl::Texture(false, true); // no interpolation, with wrapping
415 }
416 int s = z * zm;
417 for (auto y : xrange(s)) {
418 auto* line = &pixels[y * s];
419 for (auto x : xrange(s)) {
420 line[x] = (x == 0 || y == 0) ? gColor : 0;
421 }
422 }
423 zoomGridTex.bind();
424 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, s, s, 0,
425 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
426 ImGui::SetCursorPos(pos2);
427 ImGui::Image(zoomGridTex.getImGui(),
428 float(z) * zoomPatSize, {}, gl::vec2{float(size)});
429 }
430 } else {
431 ImGui::Dummy(zoomPatSize);
432 }
433 });
434 if (grid) {
435 ImGui::SetCursorPos(pos1);
436 ImGui::Image(gridTex.getImGui(), fullSize,
437 {}, (size == 8) ? gl::vec2{32.0f, 8.0f} : gl::vec2{16.0f, 4.0f});
438 }
439 });
440 });
441 ImGui::Separator();
442
443 im::TreeNode("Sprite attributes", ImGuiTreeNodeFlags_DefaultOpen, [&]{
444 auto zoomSize = float(zm * size);
445 auto fullSize = zoomSize * gl::vec2(8, 4);
446 im::Child("##attrib", {0, fullSize.y}, 0, ImGuiWindowFlags_HorizontalScrollbar, [&]{
447 if (mode == 0) {
448 ImGui::TextUnformatted("No sprites in this screen mode"sv);
449 } else {
450 gl::vec2 topLeft = ImGui::GetCursorPos();
451 gl::vec2 scrnPos = ImGui::GetCursorScreenPos();
452 if (checkerBoardSize) {
453 ImGui::SetCursorPos(topLeft);
454 ImGui::Image(checkerTex.getImGui(), fullSize,
455 {}, fullSize / (4.0f * float(checkerBoardSize)));
456 }
457 for (auto row : xrange(4)) {
458 for (auto column : xrange(8)) {
459 int sprite = 8 * row + column;
460 ImGui::SetCursorPos(topLeft + zoomSize * gl::vec2(float(column), float(row)));
461 renderSpriteAttrib(attTable, sprite, mode, size, transparent,
462 float(zm), palette, patternTex.getImGui());
463 }
464 }
465 ImGui::SetCursorPos(topLeft);
466 if (grid) {
467 ImGui::Image(gridTex.getImGui(), fullSize,
468 {}, gl::vec2{8, 4});
469 } else {
470 ImGui::Dummy(fullSize);
471 }
472 bool hovered = ImGui::IsItemHovered();
473 if (hovered) {
474 gl::vec2 zoomPatSize{float(size * zm)};
475 auto gridPos = trunc((gl::vec2(ImGui::GetIO().MousePos) - scrnPos) / zoomPatSize);
476 auto sprite = 8 * gridPos.y + gridPos.x;
477
478 ImGui::SameLine();
479 im::Group([&]{
480 ImGui::StrCat("sprite: ", sprite);
481 auto pos = ImGui::GetCursorPos();
482 if (checkerBoardSize) {
483 ImGui::Image(checkerTex.getImGui(), 3.0f * zoomPatSize,
484 {}, zoomPatSize / (4.0f * float(checkerBoardSize)));
485 }
486 ImGui::SetCursorPos(pos);
487 renderSpriteAttrib(attTable, sprite, mode, size, transparent,
488 float(3 * zm), palette, patternTex.getImGui());
489 });
490 ImGui::SameLine();
491 im::Group([&]{
492 int addr = getSpriteAttrAddr(sprite, mode);
493 ImGui::StrCat("x: ", attTable[addr + 1],
494 " y: ", attTable[addr + 0]);
495 ImGui::StrCat("pattern: ", attTable[addr + 2]);
496 if (mode == 1) {
497 auto c = attTable[addr + 3];
498 ImGui::StrCat("color: ", c & 15, (c & 80 ? " (EC)" : ""));
499 } else {
500 int colorBase = getSpriteColorAddr(sprite, mode);
501 im::StyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1), [&]{ // Tighten spacing
502 ImGui::TextUnformatted("Colors per line (hex):"sv);
503 for (auto y : xrange(4)) {
504 for (auto x : xrange(4)) {
505 auto line = 4 * y + x;
506 auto a = attTable[colorBase + line];
507 ImGui::StrCat(hex_string<1>(line), ": ",
508 hex_string<1>(a & 15),
509 (a & 0xe0 ? '*' : ' '),
510 ' ');
511 if (x != 3) ImGui::SameLine();
512 }
513 }
514 });
515 }
516 });
517 }
518 }
519 });
520 });
521 ImGui::Separator();
522
523 im::TreeNode("Rendered sprites", ImGuiTreeNodeFlags_DefaultOpen, [&]{
524 struct SpriteBox {
525 int x, y, w, h; // box
526 uint8_t sprite;
527 uint8_t vramX, vramY;
528 uint8_t pattern;
529 };
530
531 std::array<uint8_t, 256> spriteCount = {}; // zero initialize
532 std::array<std::array<SpriteChecker::SpriteInfo, 32 + 1>, 256> spriteBuffer; // uninitialized
533 std::array<SpriteBox, 32> spriteBoxes; // uninitialized
534
535 uint8_t spriteLimit = (mode == 1) ? 4 : 8;
536 uint8_t stopY = (mode == 1) ? 208 : 216;
537 uint8_t patMask = (size == 8) ? 0xff : 0xfc;
538 int magFactor = mag ? 2 : 1;
539 auto magSize = magFactor * size;
540
541 uint8_t spriteCnt = 0;
542 for (; spriteCnt < 32; ++spriteCnt) {
543 int addr = getSpriteAttrAddr(spriteCnt, mode);
544 uint8_t originalY = attTable[addr + 0];
545 if (enableStopY && (originalY == stopY)) break;
546 auto y = uint8_t(originalY + 1 - verticalScroll);
547 int initialY = y;
548
549 uint8_t x = attTable[addr + 1];
550 uint8_t pat = attTable[addr + 2] & patMask;
551 uint8_t att1 = attTable[addr + 3]; // only mode 1
552
553 bool anyEC = false;
554 bool anyNonEC = false;
555 for (int spriteY : xrange(size)) { // each line in the sprite
556 auto attr = [&]{
557 if (mode != 2) return att1;
558 int colorBase = getSpriteColorAddr(spriteCnt, mode);
559 return attTable[colorBase + spriteY];
560 }();
561
562 bool EC = attr & 0x80;
563 (EC ? anyEC : anyNonEC) = true;
564 int xx = EC ? x - 32 : x;
565
566 auto pattern = [&]{
567 uint8_t p0 = patTable[8 * pat + spriteY + 0];
568 SpriteChecker::SpritePattern result = p0 << 24;
569 if (size == 8) return result;
570 uint8_t p1 = patTable[8 * pat + spriteY + 16];
571 return result | (p1 << 16);
572 }();
573 if (mag) pattern = SpriteChecker::doublePattern(pattern);
574
575 for ([[maybe_unused]] int mm : xrange(magFactor)) {
576 auto count = spriteCount[y];
577 if (!enableLimitPerLine || (count < spriteLimit)) {
578 auto& spr = spriteBuffer[y][count];
579 spr.pattern = pattern;
580 spr.x = narrow<int16_t>(xx);
581 spr.colorAttrib = attr;
582
583 spriteCount[y] = count + 1;
584 spriteBuffer[y][count + 1].colorAttrib = 0; // sentinel (mode 2)
585 }
586 ++y; // wraps 256->0
587 }
588 }
589 assert(anyEC || anyNonEC);
590 spriteBoxes[spriteCnt] = SpriteBox{
591 anyEC ? x - 32 : x,
592 initialY,
593 magSize + (anyEC && anyNonEC ? 32 : 0),
594 magSize,
595 spriteCnt, x, originalY, pat};
596 }
597
598 std::array<uint32_t, 256 * 256> screen; // TODO screen6 striped colors
599 memset(screen.data(), 0, sizeof(uint32_t) * 256 * lines); // transparent
600 for (auto line : xrange(lines)) {
601 auto count = spriteCount[line];
602 if (count == 0) continue;
603 auto lineBuf = subspan<256>(screen, 256 * line);
604
605 if (mode == 1) {
606 auto visibleSprites = subspan(spriteBuffer[line], 0, count);
607 for (const auto& spr : view::reverse(visibleSprites)) {
608 uint8_t colIdx = spr.colorAttrib & 0x0f;
609 if (colIdx == 0 && transparent) continue;
610 auto color = palette[colIdx];
611
612 auto pattern = spr.pattern;
613 int x = spr.x;
614 if (!SpriteConverter::clipPattern(x, pattern, 0, 256)) continue;
615
616 while (pattern) {
617 if (pattern & 0x8000'0000) {
618 lineBuf[x] = color;
619 }
620 pattern <<= 1;
621 ++x;
622 }
623 }
624 } else if (mode == 2) {
625 auto visibleSprites = subspan(spriteBuffer[line], 0, count + 1); // +1 for sentinel
626
627 // see SpriteConverter
628 int first = 0;
629 while (true /*sentinel*/) {
630 if ((visibleSprites[first].colorAttrib & 0x40) == 0) [[likely]] {
631 break;
632 }
633 ++first;
634 }
635 for (int i = narrow<int>(count - 1); i >= first; --i) {
636 const auto& spr = visibleSprites[i];
637 uint8_t c = spr.colorAttrib & 0x0F;
638 if (c == 0 && transparent) continue;
639
640 auto pattern = spr.pattern;
641 int x = spr.x;
642 if (!SpriteConverter::clipPattern(x, pattern, 0, 256)) continue;
643
644 while (pattern) {
645 if (pattern & 0x8000'0000) {
646 uint8_t color = c;
647 // Merge in any following CC=1 sprites.
648 for (int j = i + 1; /*sentinel*/; ++j) {
649 const auto& info2 = visibleSprites[j];
650 if (!(info2.colorAttrib & 0x40)) break;
651 unsigned shift2 = x - info2.x;
652 if ((shift2 < 32) &&
653 ((info2.pattern << shift2) & 0x8000'0000)) {
654 color |= info2.colorAttrib & 0x0F;
655 }
656 }
657 // TODO screen 6
658 // auto pixL = palette[color >> 2];
659 // auto pixR = palette[color & 3];
660 // lineBuf[x * 2 + 0] = pixL;
661 // lineBuf[x * 2 + 1] = pixR;
662 lineBuf[x] = palette[color];
663 }
664 ++x;
665 pattern <<= 1;
666 }
667 }
668 }
669 }
670 if (!renderTex.get()) {
671 renderTex = gl::Texture(false, true); // no interpolation, with wrapping
672 }
673 renderTex.bind();
674 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, lines, 0,
675 GL_RGBA, GL_UNSIGNED_BYTE, screen.data());
676
677 std::array<SpriteBox, 2 * 32> clippedBoxes;
678 int nrClippedBoxes = 0;
679 auto addClippedBox = [&](SpriteBox b) {
680 assert(nrClippedBoxes < 64);
681 clippedBoxes[nrClippedBoxes] = b;
682 ++nrClippedBoxes;
683 };
684 for (int sprite : xrange(spriteCnt)) {
685 const auto& b = spriteBoxes[sprite];
686 int x = b.x;
687 int y = b.y;
688 int w = b.w;
689 int h = b.h;
690 if (x < 0) {
691 w += x;
692 if (w <= 0) continue;
693 x = 0;
694 } else if (x + w > 256) {
695 w = 256 - x;
696 }
697
698 int yEnd = y + h;
699 if (yEnd < 256) {
700 addClippedBox(SpriteBox{x, y, w, h, b.sprite, b.vramX, b.vramY, b.pattern});
701 } else {
702 addClippedBox(SpriteBox{x, y, w, 256 - y, b.sprite, b.vramX, b.vramY, b.pattern});
703 addClippedBox(SpriteBox{x, 0, w, yEnd - 256, b.sprite, b.vramX, b.vramY, b.pattern});
704 }
705 }
706
707 auto fullSize = float(zm) * gl::vec2(256, float(lines));
708 im::Child("##screen", {0.0f, fullSize.y}, 0, ImGuiWindowFlags_HorizontalScrollbar, [&]{
709 auto* drawList = ImGui::GetWindowDrawList();
710 gl::vec2 scrnPos = ImGui::GetCursorScreenPos();
711 auto boxColor = ImGui::ColorConvertFloat4ToU32(boundingBoxColor);
712 auto drawBox = [&](int x, int y, int w, int h) {
713 gl::vec2 tl = scrnPos + gl::vec2{float(x), float(y)} * float(zm);
714 gl::vec2 br = tl + gl::vec2{float(w), float(h)} * float(zm);
715 drawList->AddRect(tl, br, boxColor);
716 };
717
718 gl::vec2 topLeft = ImGui::GetCursorPos();
719 if (checkerBoardSize) {
720 ImGui::Image(checkerTex.getImGui(), fullSize,
721 {}, fullSize / (4.0f * float(checkerBoardSize)));
722 }
723 ImGui::SetCursorPos(topLeft);
724 ImGui::Image(renderTex.getImGui(), fullSize);
725 bool hovered = ImGui::IsItemHovered();
726 auto hoverPos = trunc((gl::vec2(ImGui::GetIO().MousePos) - scrnPos) / gl::vec2(float(zm)));
727 ImGui::SameLine();
728
729 im::Group([&]{
730 ImGui::Checkbox("Bounding box", &drawBoundingBox);
731 im::Indent([&]{
732 im::Disabled(!drawBoundingBox, [&]{
733 ImGui::ColorEdit4("color", boundingBoxColor.data(),
734 ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_AlphaBar);
735 ImGui::RadioButton("On hovered sprites", &boundingBoxOnAll, 0);
736 ImGui::RadioButton("On all sprites", &boundingBoxOnAll, 1);
737
738 });
739 });
740 {
741 auto maxStr = tmpStrCat("Max ", spriteLimit, " sprites per line");
742 ImGui::Checkbox(maxStr.c_str(), &enableLimitPerLine);
743 }
744 {
745 auto stopStr = tmpStrCat("Stop at y=", stopY);
746 ImGui::Checkbox(stopStr.c_str(), &enableStopY);
747 }
748
749 if (hovered) {
750 ImGui::Separator();
751 auto [hx, hy] = hoverPos;
752 ImGui::Text("x=%d y=%d", hx, hy);
753 ImGui::Spacing();
754
755 for (int i : xrange(nrClippedBoxes)) {
756 const auto& b = clippedBoxes[i];
757 if ((b.x <= hx) && (hx < (b.x + b.w)) &&
758 (b.y <= hy) && (hy < (b.y + b.h))) {
759 if (drawBoundingBox && (boundingBoxOnAll == 0)) {
760 drawBox(b.x, b.y, b.w, b.h);
761 }
762 ImGui::Text("sprite=%d x=%d y=%d pat=%d", b.sprite, b.vramX, b.vramY, b.pattern);
763 }
764 }
765 }
766 });
767
768 if (drawBoundingBox && (boundingBoxOnAll == 1)) {
769 for (int i : xrange(nrClippedBoxes)) {
770 const auto& b = clippedBoxes[i];
771 drawBox(b.x, b.y, b.w, b.h);
772 }
773 }
774 });
775 });
776
777 });
778}
779
780} // namespace openmsx
Most basic/generic texture: only contains a texture ID.
Definition GLUtil.hh:40
void bind() const
Makes this texture the active GL texture.
Definition GLUtil.hh:88
GLuint get() const
Returns the underlying openGL handler id.
Definition GLUtil.hh:74
void * getImGui() const
Return as a 'void*' (needed for 'Dear ImGui').
Definition GLUtil.hh:78
std::unique_ptr< ImGuiPalette > palette
ImGuiManager & manager
Definition ImGuiPart.hh:30
void save(ImGuiTextBuffer &buf) override
void paint(MSXMotherBoard *motherBoard) override
void loadLine(std::string_view name, zstring_view value) override
MSXDevice * findDevice(std::string_view name)
Find a MSXDevice by name.
uint32_t SpritePattern
Bitmap of length 32 describing a sprite pattern.
static constexpr SpritePattern doublePattern(SpritePattern a)
static bool clipPattern(int &x, SpriteChecker::SpritePattern &pattern, int minX, int maxX)
std::span< const uint8_t > getData() const
Only used by debugger.
Definition VDPVRAM.hh:564
Unified implementation of MSX Video Display Processors (VDPs).
Definition VDP.hh:67
VDPVRAM & getVRAM()
Get the VRAM object for this VDP.
Definition VDP.hh:162
void setRegister(unsigned value, unsigned extraLsbBits)
void setIndexSize(unsigned bits)
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
void StrCat(Ts &&...ts)
Definition ImGuiUtils.hh:44
void TextUnformatted(const std::string &str)
Definition ImGuiUtils.hh:25
vecN< 2, float > vec2
Definition gl_vec.hh:178
void Window(const char *name, bool *p_open, ImGuiWindowFlags flags, std::invocable<> auto next)
Definition ImGuiCpp.hh:63
void StyleVar(ImGuiStyleVar idx, float val, std::invocable<> auto next)
Definition ImGuiCpp.hh:190
bool TreeNode(const char *label, ImGuiTreeNodeFlags flags, std::invocable<> auto next)
Definition ImGuiCpp.hh:302
void Combo(const char *label, const char *preview_value, ImGuiComboFlags flags, std::invocable<> auto next)
Definition ImGuiCpp.hh:289
void ItemWidth(float item_width, std::invocable<> auto next)
Definition ImGuiCpp.hh:204
void Child(const char *str_id, const ImVec2 &size, ImGuiChildFlags child_flags, ImGuiWindowFlags window_flags, std::invocable<> auto next)
Definition ImGuiCpp.hh:110
void Disabled(bool b, std::invocable<> auto next)
Definition ImGuiCpp.hh:506
void Group(std::invocable<> auto next)
Definition ImGuiCpp.hh:236
void Indent(float indent_w, std::invocable<> auto next)
Definition ImGuiCpp.hh:224
This file implemented 3 utility functions:
Definition Autofire.cc:11
bool loadOnePersistent(std::string_view name, zstring_view value, C &c, const std::tuple< Elements... > &tup)
void simpleToolTip(std::string_view desc)
Definition ImGuiUtils.hh:78
void savePersistent(ImGuiTextBuffer &buf, C &c, const std::tuple< Elements... > &tup)
ImU32 getColor(imColor col)
auto transform(InputRange &&range, OutputIter out, UnaryOperation op)
Definition ranges.hh:279
size_t size(std::string_view utf8)
constexpr auto reverse(Range &&range)
Definition view.hh:514
constexpr auto subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)
Definition ranges.hh:481
TemporaryString tmpStrCat(Ts &&... ts)
Definition strCat.hh:742
constexpr auto xrange(T e)
Definition xrange.hh:132