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 std::array<uint32_t, 16> palette;
176 auto msxPalette = manager.palette->getPalette(vdp);
177 ranges::transform(msxPalette, palette.data(),
178 [](uint16_t msx) { return ImGuiPalette::toRGBA(msx); });
179 // TODO? if (color0 < 16) palette[0] = palette[color0];
180
181 bool manMode = overrideAll || overrideMode;
182 bool manSize = overrideAll || overrideSize;
183 bool manMag = overrideAll || overrideMag;
184 bool manTrans = overrideAll || overrideTrans;
185 bool manPat = overrideAll || overridePat;
186 bool manAtt = overrideAll || overrideAtt;
187 bool manScroll = overrideAll || overrideScroll;
188 bool manLines = overrideAll || overrideLines;
189
190 im::TreeNode("Settings", ImGuiTreeNodeFlags_DefaultOpen, [&]{
191 im::Group([&]{
192 ImGui::TextUnformatted("VDP settings");
193 im::Disabled(manMode, [&]{
194 ImGui::AlignTextToFramePadding();
195 ImGui::StrCat("Sprite mode: ", modeToStr(vdpMode));
196 });
197 im::Disabled(manSize, [&]{
198 ImGui::AlignTextToFramePadding();
199 ImGui::StrCat("Sprite size: ", sizeToStr(vdpSize));
200 });
201 im::Disabled(manMag, [&]{
202 ImGui::AlignTextToFramePadding();
203 ImGui::StrCat("Sprites magnified: ", yesNo(vdpMag));
204 });
205 im::Disabled(manTrans, [&]{
206 ImGui::AlignTextToFramePadding();
207 ImGui::StrCat("Color 0 transparent: ", yesNo(vdpTransparent));
208 });
209 im::Disabled(manPat, [&]{
210 ImGui::AlignTextToFramePadding();
211 ImGui::StrCat("Pattern table: 0x", hex_string<5>(vdpPatBase));
212 });
213 im::Disabled(manAtt, [&]{
214 ImGui::AlignTextToFramePadding();
215 ImGui::StrCat("Attribute table: 0x", hex_string<5>(vdpAttBase | (vdpMode == 2 ? 512 : 0)));
216 });
217 im::Disabled(manScroll, [&]{
218 ImGui::AlignTextToFramePadding();
219 ImGui::StrCat("Vertical scroll: ", vdpVerticalScroll);
220 });
221 im::Disabled(manLines, [&]{
222 ImGui::AlignTextToFramePadding();
223 ImGui::StrCat("Visible lines: ", (vdpLines == 192) ? "192" : "212");
224 });
225 });
226 ImGui::SameLine();
227 im::Group([&]{
228 ImGui::Checkbox("Manual override", &overrideAll);
229 im::Group([&]{
230 im::Disabled(overrideAll, [&]{
231 ImGui::Checkbox("##o-mode", overrideAll ? &overrideAll : &overrideMode);
232 ImGui::Checkbox("##o-size", overrideAll ? &overrideAll : &overrideSize);
233 ImGui::Checkbox("##o-mag", overrideAll ? &overrideAll : &overrideMag);
234 ImGui::Checkbox("##o-trans", overrideAll ? &overrideAll : &overrideTrans);
235 ImGui::Checkbox("##o-pat", overrideAll ? &overrideAll : &overridePat);
236 ImGui::Checkbox("##o-att", overrideAll ? &overrideAll : &overrideAtt);
237 ImGui::Checkbox("##o-scroll", overrideAll ? &overrideAll : &overrideScroll);
238 ImGui::Checkbox("##o-lines", overrideAll ? &overrideAll : &overrideLines);
239 });
240 });
241 ImGui::SameLine();
242 im::Group([&]{
243 im::ItemWidth(ImGui::GetFontSize() * 9.0f, [&]{
244 im::Disabled(!manMode, [&]{
245 im::Combo("##mode", modeToStr(manualMode), [&]{
246 if (ImGui::Selectable("1")) manualMode = 1;
247 if (ImGui::Selectable("2")) manualMode = 2;
248 });
249 });
250 im::Disabled(!manSize, [&]{
251 im::Combo("##size", sizeToStr(manualSize), [&]{
252 if (ImGui::Selectable(" 8 x 8")) manualSize = 8;
253 if (ImGui::Selectable("16 x 16")) manualSize = 16;
254 });
255 });
256 im::Disabled(!manMag, [&]{
257 im::Combo("##mag", yesNo(manualMag), [&]{
258 if (ImGui::Selectable("no")) manualMag = 0;
259 if (ImGui::Selectable("yes")) manualMag = 1;
260 });
261 });
262 im::Disabled(!manTrans, [&]{
263 im::Combo("##trans", yesNo(manualTransparent), [&]{
264 if (ImGui::Selectable("no")) manualTransparent = 0;
265 if (ImGui::Selectable("yes")) manualTransparent = 1;
266 });
267 });
268 im::Disabled(!manPat, [&]{
269 comboHexSequence<5>("##pat", &manualPatBase, 8 * 256);
270 });
271 im::Disabled(!manAtt, [&]{
272 comboHexSequence<5>("##att", &manualAttBase, attMult(manualMode), manualMode == 2 ? 512 : 0);
273 });
274 im::Disabled(!manScroll, [&]{
275 ImGui::InputInt("##verticalScroll", &manualVerticalScroll);
276 manualVerticalScroll &= 0xff;
277 });
278 im::Disabled(!manLines, [&]{
279 ImGui::Combo("##lines", &manualLines, "192\000212\000256\000");
280 });
281 });
282 });
283 });
284 ImGui::SameLine();
285 ImGui::Dummy(ImVec2(25, 1));
286 ImGui::SameLine();
287 im::Group([&]{
288 ImGui::SetNextItemWidth(ImGui::GetFontSize() * 10.0f);
289 ImGui::Combo("Palette", &manager.palette->whichPalette, "VDP\000Custom\000Fixed\000");
290 if (ImGui::Button("Open palette editor")) {
291 manager.palette->window.raise();
292 }
293 ImGui::Separator();
294 ImGui::SetNextItemWidth(ImGui::GetFontSize() * 3.0f);
295 ImGui::Combo("Zoom", &zoom, "1x\0002x\0003x\0004x\0005x\0006x\0007x\0008x\000");
296 ImGui::Checkbox("grid", &grid);
297 ImGui::SameLine();
298 im::Disabled(!grid, [&]{
299 ImGui::ColorEdit4("Grid color", gridColor.data(),
300 ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_AlphaBar);
301 });
302
303 ImGui::TextUnformatted("Checkerboard:"sv);
304 simpleToolTip("Used as background in 'Sprite attribute' and 'Rendered sprites' view");
305 ImGui::SameLine();
306 ImGui::ColorEdit4("checkerboard color1", checkerBoardColor1.data(), ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel);
307 ImGui::SameLine();
308 ImGui::ColorEdit4("checkerboard color2", checkerBoardColor2.data(), ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel);
309 im::Indent([&]{
310 ImGui::SetNextItemWidth(ImGui::GetFontSize() * 6.0f);
311 ImGui::InputInt("size", &checkerBoardSize);
312 });
313 });
314 });
315 ImGui::Separator();
316
317 int mode = manMode ? manualMode : vdpMode;
318 int size = manSize ? manualSize : vdpSize;
319 int mag = manMag ? manualMag : vdpMag;
320 int verticalScroll = manScroll ? manualVerticalScroll : vdpVerticalScroll;
321 int lines = manLines ? (manualLines == 0 ? 192 :
322 manualLines == 1 ? 212 :
323 256)
324 : vdpLines;
325 int transparent = manTrans ? manualTransparent : vdpTransparent;
326
327 VramTable patTable(vram, planar);
328 unsigned patReg = (manPat ? (manualPatBase | ((8 * 256) - 1)) : vdp->getSpritePatternTableBase()) >> 11;
329 patTable.setRegister(patReg, 11);
330 patTable.setIndexSize(11);
331
332 VramTable attTable(vram, planar);
333 unsigned attReg = (manAtt ? (manualAttBase | (attMult(manualMode) - 1)) : vdp->getSpriteAttributeTableBase()) >> 7;
334 attTable.setRegister(attReg, 7);
335 attTable.setIndexSize((mode == 2) ? 10 : 7);
336
337 // create pattern texture
338 if (!patternTex.get()) {
339 patternTex = gl::Texture(false, false); // no interpolation, no wrapping
340 }
341 patternTex.bind();
342 std::array<uint32_t, 256 * 64> pixels;
343 if (mode != 0) {
344 if (size == 8) {
345 renderPatterns8 (patTable, pixels);
346 } else {
347 renderPatterns16(patTable, pixels);
348 }
349 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 64, 0,
350 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
351 } else {
352 pixels[0] = getColor(imColor::GRAY);
353 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0,
354 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
355 }
356
357 // create grid texture
358 int zm = 2 * (1 + zoom);
359 auto gColor = ImGui::ColorConvertFloat4ToU32(gridColor);
360 if (grid) {
361 auto gridSize = size * zm;
362 for (auto y : xrange(gridSize)) {
363 auto* line = &pixels[y * gridSize];
364 for (auto x : xrange(gridSize)) {
365 line[x] = (x == 0 || y == 0) ? gColor : 0;
366 }
367 }
368 if (!gridTex.get()) {
369 gridTex = gl::Texture(false, true); // no interpolation, with wrapping
370 }
371 gridTex.bind();
372 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gridSize, gridSize, 0,
373 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
374 }
375
376 // create checker board texture
377 if (checkerBoardSize) {
378 pixels[0] = pixels[3] = ImGui::ColorConvertFloat4ToU32(checkerBoardColor1);
379 pixels[1] = pixels[2] = ImGui::ColorConvertFloat4ToU32(checkerBoardColor2);
380 if (!checkerTex.get()) {
381 checkerTex = gl::Texture(false, true); // no interpolation, with wrapping
382 }
383 checkerTex.bind();
384 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0,
385 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
386 }
387
388 im::TreeNode("Sprite patterns", ImGuiTreeNodeFlags_DefaultOpen, [&]{
389 auto fullSize = gl::vec2(256, 64) * float(zm);
390 im::Child("##pattern", {0, fullSize.y}, 0, ImGuiWindowFlags_HorizontalScrollbar, [&]{
391 auto pos1 = ImGui::GetCursorPos();
392 gl::vec2 scrnPos = ImGui::GetCursorScreenPos();
393 ImGui::Image(patternTex.getImGui(), fullSize);
394 bool hovered = ImGui::IsItemHovered() && (mode != 0);
395 ImGui::SameLine();
396 im::Group([&]{
397 gl::vec2 zoomPatSize{float(size * zm)};
398 if (hovered) {
399 auto gridPos = trunc((gl::vec2(ImGui::GetIO().MousePos) - scrnPos) / zoomPatSize);
400 auto pattern = (size == 16) ? ((16 * gridPos.y) + gridPos.x) * 4
401 : ((32 * gridPos.y) + gridPos.x) * 1;
402 ImGui::StrCat("pattern: ", pattern);
403 auto recipPatTex = recip((size == 16) ? gl::vec2{16, 4} : gl::vec2{32, 8});
404 auto uv1 = gl::vec2(gridPos) * recipPatTex;
405 auto uv2 = uv1 + recipPatTex;
406 auto pos2 = ImGui::GetCursorPos();
407 int z = (size == 16) ? 3 : 6;
408 ImGui::Image(patternTex.getImGui(), float(z) * zoomPatSize, uv1, uv2);
409 if (grid) {
410 if (!zoomGridTex.get()) {
411 zoomGridTex = gl::Texture(false, true); // no interpolation, with wrapping
412 }
413 int s = z * zm;
414 for (auto y : xrange(s)) {
415 auto* line = &pixels[y * s];
416 for (auto x : xrange(s)) {
417 line[x] = (x == 0 || y == 0) ? gColor : 0;
418 }
419 }
420 zoomGridTex.bind();
421 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, s, s, 0,
422 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
423 ImGui::SetCursorPos(pos2);
424 ImGui::Image(zoomGridTex.getImGui(),
425 float(z) * zoomPatSize, {}, gl::vec2{float(size)});
426 }
427 } else {
428 ImGui::Dummy(zoomPatSize);
429 }
430 });
431 if (grid) {
432 ImGui::SetCursorPos(pos1);
433 ImGui::Image(gridTex.getImGui(), fullSize,
434 {}, (size == 8) ? gl::vec2{32.0f, 8.0f} : gl::vec2{16.0f, 4.0f});
435 }
436 });
437 });
438 ImGui::Separator();
439
440 im::TreeNode("Sprite attributes", ImGuiTreeNodeFlags_DefaultOpen, [&]{
441 auto zoomSize = float(zm * size);
442 auto fullSize = zoomSize * gl::vec2(8, 4);
443 im::Child("##attrib", {0, fullSize.y}, 0, ImGuiWindowFlags_HorizontalScrollbar, [&]{
444 if (mode == 0) {
445 ImGui::TextUnformatted("No sprites in this screen mode"sv);
446 } else {
447 gl::vec2 topLeft = ImGui::GetCursorPos();
448 gl::vec2 scrnPos = ImGui::GetCursorScreenPos();
449 if (checkerBoardSize) {
450 ImGui::SetCursorPos(topLeft);
451 ImGui::Image(checkerTex.getImGui(), fullSize,
452 {}, fullSize / (4.0f * float(checkerBoardSize)));
453 }
454 for (auto row : xrange(4)) {
455 for (auto column : xrange(8)) {
456 int sprite = 8 * row + column;
457 ImGui::SetCursorPos(topLeft + zoomSize * gl::vec2(float(column), float(row)));
458 renderSpriteAttrib(attTable, sprite, mode, size, transparent,
459 float(zm), palette, patternTex.getImGui());
460 }
461 }
462 ImGui::SetCursorPos(topLeft);
463 if (grid) {
464 ImGui::Image(gridTex.getImGui(), fullSize,
465 {}, gl::vec2{8, 4});
466 } else {
467 ImGui::Dummy(fullSize);
468 }
469 bool hovered = ImGui::IsItemHovered();
470 if (hovered) {
471 gl::vec2 zoomPatSize{float(size * zm)};
472 auto gridPos = trunc((gl::vec2(ImGui::GetIO().MousePos) - scrnPos) / zoomPatSize);
473 auto sprite = 8 * gridPos.y + gridPos.x;
474
475 ImGui::SameLine();
476 im::Group([&]{
477 ImGui::StrCat("sprite: ", sprite);
478 auto pos = ImGui::GetCursorPos();
479 if (checkerBoardSize) {
480 ImGui::Image(checkerTex.getImGui(), 3.0f * zoomPatSize,
481 {}, zoomPatSize / (4.0f * float(checkerBoardSize)));
482 }
483 ImGui::SetCursorPos(pos);
484 renderSpriteAttrib(attTable, sprite, mode, size, transparent,
485 float(3 * zm), palette, patternTex.getImGui());
486 });
487 ImGui::SameLine();
488 im::Group([&]{
489 int addr = getSpriteAttrAddr(sprite, mode);
490 ImGui::StrCat("x: ", attTable[addr + 1],
491 " y: ", attTable[addr + 0]);
492 ImGui::StrCat("pattern: ", attTable[addr + 2]);
493 if (mode == 1) {
494 auto c = attTable[addr + 3];
495 ImGui::StrCat("color: ", c & 15, (c & 80 ? " (EC)" : ""));
496 } else {
497 int colorBase = getSpriteColorAddr(sprite, mode);
498 im::StyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1), [&]{ // Tighten spacing
499 ImGui::TextUnformatted("Colors per line (hex):"sv);
500 for (auto y : xrange(4)) {
501 for (auto x : xrange(4)) {
502 auto line = 4 * y + x;
503 auto a = attTable[colorBase + line];
504 ImGui::StrCat(hex_string<1>(line), ": ",
505 hex_string<1>(a & 15),
506 (a & 0xe0 ? '*' : ' '),
507 ' ');
508 if (x != 3) ImGui::SameLine();
509 }
510 }
511 });
512 }
513 });
514 }
515 }
516 });
517 });
518 ImGui::Separator();
519
520 im::TreeNode("Rendered sprites", ImGuiTreeNodeFlags_DefaultOpen, [&]{
521 struct SpriteBox {
522 int x, y, w, h; // box
523 uint8_t sprite;
524 uint8_t vramX, vramY;
525 uint8_t pattern;
526 };
527
528 std::array<uint8_t, 256> spriteCount = {}; // zero initialize
529 std::array<std::array<SpriteChecker::SpriteInfo, 32 + 1>, 256> spriteBuffer; // uninitialized
530 std::array<SpriteBox, 32> spriteBoxes; // uninitialized
531
532 uint8_t spriteLimit = (mode == 1) ? 4 : 8;
533 uint8_t stopY = (mode == 1) ? 208 : 216;
534 uint8_t patMask = (size == 8) ? 0xff : 0xfc;
535 int magFactor = mag ? 2 : 1;
536
537 uint8_t spriteCnt = 0;
538 for (; spriteCnt < 32; ++spriteCnt) {
539 int addr = getSpriteAttrAddr(spriteCnt, mode);
540 uint8_t originalY = attTable[addr + 0];
541 if (enableStopY && (originalY == stopY)) break;
542 auto y = uint8_t(originalY + 1 - verticalScroll);
543 int initialY = y;
544
545 uint8_t x = attTable[addr + 1];
546 uint8_t pat = attTable[addr + 2] & patMask;
547 uint8_t att1 = attTable[addr + 3]; // only mode 1
548
549 bool anyEC = false;
550 bool anyNonEC = false;
551 for (int spriteY : xrange(size)) { // each line in the sprite
552 auto attr = [&]{
553 if (mode != 2) return att1;
554 int colorBase = getSpriteColorAddr(spriteCnt, mode);
555 return attTable[colorBase + spriteY];
556 }();
557
558 bool EC = attr & 0x80;
559 (EC ? anyEC : anyNonEC) = true;
560 int xx = EC ? x - 32 : x;
561
562 auto pattern = [&]{
563 uint8_t p0 = patTable[8 * pat + spriteY + 0];
564 SpriteChecker::SpritePattern result = p0 << 24;
565 if (size == 8) return result;
566 uint8_t p1 = patTable[8 * pat + spriteY + 16];
567 return result | (p1 << 16);
568 }();
569 if (mag) pattern = SpriteChecker::doublePattern(pattern);
570
571 for ([[maybe_unused]] int mm : xrange(magFactor)) {
572 auto count = spriteCount[y];
573 if (!enableLimitPerLine || (count < spriteLimit)) {
574 auto& spr = spriteBuffer[y][count];
575 spr.pattern = pattern;
576 spr.x = narrow<int16_t>(xx);
577 spr.colorAttrib = attr;
578
579 spriteCount[y] = count + 1;
580 spriteBuffer[y][count + 1].colorAttrib = 0; // sentinel (mode 2)
581 }
582 ++y; // wraps 256->0
583 }
584 }
585 assert(anyEC || anyNonEC);
586 spriteBoxes[spriteCnt] = SpriteBox{
587 anyEC ? x - 32 : x,
588 initialY,
589 anyEC && anyNonEC ? size + 32 : size,
590 size,
591 spriteCnt, x, originalY, pat};
592 }
593
594 std::array<uint32_t, 256 * 256> screen; // TODO screen6 striped colors
595 memset(screen.data(), 0, sizeof(uint32_t) * 256 * lines); // transparent
596 for (auto line : xrange(lines)) {
597 auto count = spriteCount[line];
598 if (count == 0) continue;
599 auto lineBuf = subspan<256>(screen, 256 * line);
600
601 if (mode == 1) {
602 auto visibleSprites = subspan(spriteBuffer[line], 0, count);
603 for (const auto& spr : view::reverse(visibleSprites)) {
604 uint8_t colIdx = spr.colorAttrib & 0x0f;
605 if (colIdx == 0 && transparent) continue;
606 auto color = palette[colIdx];
607
608 auto pattern = spr.pattern;
609 int x = spr.x;
610 if (!SpriteConverter::clipPattern(x, pattern, 0, 256)) continue;
611
612 while (pattern) {
613 if (pattern & 0x8000'0000) {
614 lineBuf[x] = color;
615 }
616 pattern <<= 1;
617 ++x;
618 }
619 }
620 } else if (mode == 2) {
621 auto visibleSprites = subspan(spriteBuffer[line], 0, count + 1); // +1 for sentinel
622
623 // see SpriteConverter
624 int first = 0;
625 while (true /*sentinel*/) {
626 if ((visibleSprites[first].colorAttrib & 0x40) == 0) [[likely]] {
627 break;
628 }
629 ++first;
630 }
631 for (int i = narrow<int>(count - 1); i >= first; --i) {
632 const auto& spr = visibleSprites[i];
633 uint8_t c = spr.colorAttrib & 0x0F;
634 if (c == 0 && transparent) continue;
635
636 auto pattern = spr.pattern;
637 int x = spr.x;
638 if (!SpriteConverter::clipPattern(x, pattern, 0, 256)) continue;
639
640 while (pattern) {
641 if (pattern & 0x8000'0000) {
642 uint8_t color = c;
643 // Merge in any following CC=1 sprites.
644 for (int j = i + 1; /*sentinel*/; ++j) {
645 const auto& info2 = visibleSprites[j];
646 if (!(info2.colorAttrib & 0x40)) break;
647 unsigned shift2 = x - info2.x;
648 if ((shift2 < 32) &&
649 ((info2.pattern << shift2) & 0x8000'0000)) {
650 color |= info2.colorAttrib & 0x0F;
651 }
652 }
653 // TODO screen 6
654 // auto pixL = palette[color >> 2];
655 // auto pixR = palette[color & 3];
656 // lineBuf[x * 2 + 0] = pixL;
657 // lineBuf[x * 2 + 1] = pixR;
658 lineBuf[x] = palette[color];
659 }
660 ++x;
661 pattern <<= 1;
662 }
663 }
664 }
665 }
666 if (!renderTex.get()) {
667 renderTex = gl::Texture(false, true); // no interpolation, with wrapping
668 }
669 renderTex.bind();
670 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, lines, 0,
671 GL_RGBA, GL_UNSIGNED_BYTE, screen.data());
672
673 std::array<SpriteBox, 2 * 32> clippedBoxes;
674 int nrClippedBoxes = 0;
675 auto addClippedBox = [&](SpriteBox b) {
676 assert(nrClippedBoxes < 64);
677 clippedBoxes[nrClippedBoxes] = b;
678 ++nrClippedBoxes;
679 };
680 for (int sprite : xrange(spriteCnt)) {
681 const auto& b = spriteBoxes[sprite];
682 int x = b.x;
683 int y = b.y;
684 int w = b.w;
685 int h = b.h;
686 if (x < 0) {
687 w += x;
688 if (w <= 0) continue;
689 x = 0;
690 } else if (x + w > 256) {
691 w = 256 - x;
692 }
693
694 int yEnd = y + h;
695 if (yEnd < 256) {
696 addClippedBox(SpriteBox{x, y, w, h, b.sprite, b.vramX, b.vramY, b.pattern});
697 } else {
698 addClippedBox(SpriteBox{x, y, w, 256 - y, b.sprite, b.vramX, b.vramY, b.pattern});
699 addClippedBox(SpriteBox{x, 0, w, yEnd - 256, b.sprite, b.vramX, b.vramY, b.pattern});
700 }
701 }
702
703 auto fullSize = float(zm) * gl::vec2(256, float(lines));
704 im::Child("##screen", {0.0f, fullSize.y}, 0, ImGuiWindowFlags_HorizontalScrollbar, [&]{
705 auto* drawList = ImGui::GetWindowDrawList();
706 gl::vec2 scrnPos = ImGui::GetCursorScreenPos();
707 auto boxColor = ImGui::ColorConvertFloat4ToU32(boundingBoxColor);
708 auto drawBox = [&](int x, int y, int w, int h) {
709 gl::vec2 tl = scrnPos + gl::vec2{float(x), float(y)} * float(zm);
710 gl::vec2 br = tl + gl::vec2{float(w), float(h)} * float(zm);
711 drawList->AddRect(tl, br, boxColor);
712 };
713
714 gl::vec2 topLeft = ImGui::GetCursorPos();
715 if (checkerBoardSize) {
716 ImGui::Image(checkerTex.getImGui(), fullSize,
717 {}, fullSize / (4.0f * float(checkerBoardSize)));
718 }
719 ImGui::SetCursorPos(topLeft);
720 ImGui::Image(renderTex.getImGui(), fullSize);
721 bool hovered = ImGui::IsItemHovered();
722 auto hoverPos = trunc((gl::vec2(ImGui::GetIO().MousePos) - scrnPos) / gl::vec2(float(zm)));
723 ImGui::SameLine();
724
725 im::Group([&]{
726 ImGui::Checkbox("Bounding box", &drawBoundingBox);
727 im::Indent([&]{
728 im::Disabled(!drawBoundingBox, [&]{
729 ImGui::ColorEdit4("color", boundingBoxColor.data(),
730 ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_AlphaBar);
731 ImGui::RadioButton("On hovered sprites", &boundingBoxOnAll, 0);
732 ImGui::RadioButton("On all sprites", &boundingBoxOnAll, 1);
733
734 });
735 });
736 {
737 auto maxStr = tmpStrCat("Max ", spriteLimit, " sprites per line");
738 ImGui::Checkbox(maxStr.c_str(), &enableLimitPerLine);
739 }
740 {
741 auto stopStr = tmpStrCat("Stop at y=", stopY);
742 ImGui::Checkbox(stopStr.c_str(), &enableStopY);
743 }
744
745 if (hovered) {
746 ImGui::Separator();
747 auto [hx, hy] = hoverPos;
748 ImGui::Text("x=%d y=%d", hx, hy);
749 ImGui::Spacing();
750
751 for (int i : xrange(nrClippedBoxes)) {
752 const auto& b = clippedBoxes[i];
753 if ((b.x <= hx) && (hx < (b.x + b.w)) &&
754 (b.y <= hy) && (hy < (b.y + b.h))) {
755 if (drawBoundingBox && (boundingBoxOnAll == 0)) {
756 drawBox(b.x, b.y, b.w, b.h);
757 }
758 ImGui::Text("sprite=%d x=%d y=%d pat=%d", b.sprite, b.vramX, b.vramY, b.pattern);
759 }
760 }
761 }
762 });
763
764 if (drawBoundingBox && (boundingBoxOnAll == 1)) {
765 for (int i : xrange(nrClippedBoxes)) {
766 const auto& b = clippedBoxes[i];
767 drawBox(b.x, b.y, b.w, b.h);
768 }
769 }
770 });
771 });
772
773 });
774}
775
776} // namespace openmsx
Most basic/generic texture: only contains a texture ID.
Definition GLUtil.hh:39
void bind() const
Makes this texture the active GL texture.
Definition GLUtil.hh:87
GLuint get() const
Returns the underlying openGL handler id.
Definition GLUtil.hh:73
void * getImGui() const
Return as a 'void*' (needed for 'Dear ImGui').
Definition GLUtil.hh:77
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:43
void TextUnformatted(const std::string &str)
Definition ImGuiUtils.hh:24
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:77
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