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