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