openMSX
ImGuiCharacter.cc
Go to the documentation of this file.
1#include "ImGuiCharacter.hh"
2
3#include "ImGuiCpp.hh"
4#include "ImGuiManager.hh"
5#include "ImGuiPalette.hh"
6#include "ImGuiUtils.hh"
7
8#include "DisplayMode.hh"
9#include "MSXMotherBoard.hh"
10#include "VDP.hh"
11#include "VDPVRAM.hh"
12
13#include "one_of.hh"
14#include "ranges.hh"
15#include "view.hh"
16
17#include <imgui.h>
18
19namespace openmsx {
20
21using namespace std::literals;
22
23void ImGuiCharacter::save(ImGuiTextBuffer& buf)
24{
25 savePersistent(buf, *this, persistentElements);
26}
27
28void ImGuiCharacter::loadLine(std::string_view name, zstring_view value)
29{
30 loadOnePersistent(name, value, *this, persistentElements);
31}
32
33void ImGuiCharacter::initHexDigits()
34{
35 smallHexDigits = gl::Texture(false, false); // no interpolation, no wrapping
36
37 // font definition: 16 glyphs 0-9 A-F, each 5 x 8 pixels
38 static constexpr int charWidth = 5;
39 static constexpr int charHeight = 8;
40 static constexpr int totalWidth = 16 * charWidth;
41 static constexpr int totalSize = totalWidth * charHeight;
42 static constexpr std::string_view glyphs =
43 " ... ... ................................................ ........ .........."
44 ".MMM...M. .MMM..MMM..M.M..MMM..MMM..MMM..MMM..MMM..MMM..MM....MM..MM...MMM..MMM."
45 ".M.M..MM. ...M....M..M.M..M....M......M..M.M..M.M..M.M..M.M..M....M.M..M....M..."
46 ".M.M...M. .MM..MMM..M.M..MMM..MMM. .M..MMM..MMM..MMM..MM...M. .M.M..MMM..MMM."
47 ".M.M. .M. .M.. ...M..MMM....M..M.M. .M..M.M....M..M.M..M.M..M. .M.M..M....M..."
48 ".M.M. .M. .M......M....M....M..M.M. .M..M.M....M..M.M..M.M..M....M.M..M....M. "
49 ".MMM. .M. .MMM..MMM. .M..MMM..MMM. .M..MMM..MMM..M.M..MM....MM..MM...MMM..M. "
50 " ... ... .......... ............. ...................... ........ ........ ";
51 static_assert(glyphs.size() == totalSize);
52
53 // transform to 32-bit RGBA
54 std::array<uint32_t, totalSize> pixels;
55 for (auto [c, p] : view::zip_equal(glyphs, pixels)) {
56 p = (c == ' ') ? ImColor(0.0f, 0.0f, 0.0f, 0.0f) // transparent
57 : (c == '.') ? ImColor(0.0f, 0.0f, 0.0f, 0.7f) // black semi-transparent outline
58 : ImColor(1.0f, 1.0f, 1.0f, 0.7f); // white semi-transparent
59 }
60
61 // and upload as a texture
62 smallHexDigits.bind();
63 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, totalWidth, charHeight, 0,
64 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
65}
66
68{
69 if (!show || !motherBoard) return;
70 ImGui::SetNextWindowSize({692, 886}, ImGuiCond_FirstUseEver);
71 im::Window("Tile viewer", &show, [&]{
72 auto* vdp = dynamic_cast<VDP*>(motherBoard->findDevice("VDP")); // TODO name based OK?
73 if (!vdp) return;
74 const auto& vram = vdp->getVRAM().getData();
75
76 int vdpMode = [&] {
77 auto base = vdp->getDisplayMode().getBase();
78 if (base == DisplayMode::TEXT1) return TEXT40;
79 if (base == DisplayMode::TEXT2) return TEXT80;
80 if (base == DisplayMode::GRAPHIC1) return SCR1;
81 if (base == DisplayMode::GRAPHIC2) return SCR2;
82 if (base == DisplayMode::GRAPHIC3) return SCR4;
83 if (base == DisplayMode::MULTICOLOR) return SCR3;
84 return OTHER;
85 }();
86 auto modeToStr = [](int mode) {
87 if (mode == TEXT40) return "screen 0, width 40";
88 if (mode == TEXT80) return "screen 0, width 80";
89 if (mode == SCR1) return "screen 1";
90 if (mode == SCR2) return "screen 2";
91 if (mode == SCR3) return "screen 3";
92 if (mode == SCR4) return "screen 4";
93 if (mode == OTHER) return "non-character";
94 assert(false); return "ERROR";
95 };
96 auto patMult = [](int mode) { return 1 << (mode == one_of(SCR2, SCR4) ? 13 : 11); };
97 auto colMult = [](int mode) { return 1 << (mode == one_of(SCR2, SCR4) ? 13 :
98 mode == TEXT80 ? 9 : 6); };
99 auto namMult = [](int mode) { return 1 << (mode == TEXT80 ? 12 : 10); };
100 int vdpFgCol = vdp->getForegroundColor() & 15;
101 int vdpBgCol = vdp->getBackgroundColor() & 15;
102 int vdpFgBlink = vdp->getBlinkForegroundColor() & 15;
103 int vdpBgBlink = vdp->getBlinkBackgroundColor() & 15;
104 bool vdpBlink = vdp->getBlinkState();
105 int vdpPatBase = vdp->getPatternTableBase() & ~(patMult(vdpMode) - 1);
106 int vdpColBase = vdp->getColorTableBase() & ~(colMult(vdpMode) - 1);
107 int vdpNamBase = vdp->getNameTableBase() & ~(namMult(vdpMode) - 1);
108 int vdpLines = vdp->getNumberOfLines();
109 int vdpColor0 = vdp->getTransparency() ? vdpBgCol
110 : 16; // no replacement
111 auto vramSize = std::min(vdp->getVRAM().getSize(), 0x20000u); // max 128kB
112 bool isMSX1 = vdp->isMSX1VDP();
113
114 bool manMode = overrideAll || overrideMode;
115 bool manFgCol = overrideAll || overrideFgCol;
116 bool manBgCol = overrideAll || overrideBgCol;
117 bool manFgBlink = overrideAll || overrideFgBlink;
118 bool manBgBlink = overrideAll || overrideBgBlink;
119 bool manBlink = overrideAll || overrideBlink;
120 bool manPat = overrideAll || overridePat;
121 bool manCol = overrideAll || overrideCol;
122 bool manNam = overrideAll || overrideNam;
123 bool manRows = overrideAll || overrideRows;
124 bool manColor0 = overrideAll || overrideColor0;
125
126 im::TreeNode("Settings", ImGuiTreeNodeFlags_DefaultOpen, [&]{
127 static const char* const color0Str = "0\0001\0002\0003\0004\0005\0006\0007\0008\0009\00010\00011\00012\00013\00014\00015\000none\000";
128 im::Group([&]{
129 ImGui::TextUnformatted("VDP settings");
130 im::Disabled(manMode, [&]{
131 ImGui::AlignTextToFramePadding();
132 ImGui::StrCat("Screen mode: ", modeToStr(vdpMode));
133 });
134 im::Disabled(manFgCol, [&]{
135 ImGui::AlignTextToFramePadding();
136 ImGui::StrCat("Foreground color: ", vdpFgCol);
137 });
138 im::Disabled(manBgCol, [&]{
139 ImGui::AlignTextToFramePadding();
140 ImGui::StrCat("Background color: ", vdpBgCol);
141 });
142 im::Disabled(manFgBlink, [&]{
143 ImGui::AlignTextToFramePadding();
144 ImGui::StrCat("Foreground blink color: ", vdpFgBlink);
145 });
146 im::Disabled(manBgBlink, [&]{
147 ImGui::AlignTextToFramePadding();
148 ImGui::StrCat("Background blink color: ", vdpBgBlink);
149 });
150 im::Disabled(manBlink, [&]{
151 ImGui::AlignTextToFramePadding();
152 ImGui::StrCat("Blink: ", vdpBlink ? "enabled" : "disabled");
153 });
154 im::Disabled(manPat, [&]{
155 ImGui::AlignTextToFramePadding();
156 ImGui::StrCat("Pattern table: 0x", hex_string<5>(vdpPatBase));
157 });
158 im::Disabled(manCol, [&]{
159 ImGui::AlignTextToFramePadding();
160 ImGui::StrCat("Color table: 0x", hex_string<5>(vdpColBase));
161 });
162 im::Disabled(manNam, [&]{
163 ImGui::AlignTextToFramePadding();
164 ImGui::StrCat("Name table: 0x", hex_string<5>(vdpNamBase));
165 });
166 im::Disabled(manRows, [&]{
167 ImGui::AlignTextToFramePadding();
168 ImGui::StrCat("Visible rows: ", (vdpLines == 192) ? "24" : "26.5");
169 });
170 im::Disabled(manColor0, [&]{
171 ImGui::AlignTextToFramePadding();
172 ImGui::StrCat("Replace color 0: ", getComboString(vdpColor0, color0Str));
173 });
174 });
175 ImGui::SameLine();
176 im::Group([&]{
177 ImGui::Checkbox("Manual override", &overrideAll);
178 im::Group([&]{
179 im::Disabled(overrideAll, [&]{
180 ImGui::Checkbox("##o-mode", overrideAll ? &overrideAll : &overrideMode);
181 ImGui::Checkbox("##o-fgCol", overrideAll ? &overrideAll : &overrideFgCol);
182 ImGui::Checkbox("##o-bgCol", overrideAll ? &overrideAll : &overrideBgCol);
183 ImGui::Checkbox("##o-fgBlink", overrideAll ? &overrideAll : &overrideFgBlink);
184 ImGui::Checkbox("##o-bgBlink", overrideAll ? &overrideAll : &overrideBgBlink);
185 ImGui::Checkbox("##o-blink", overrideAll ? &overrideAll : &overrideBlink);
186 ImGui::Checkbox("##o-pat", overrideAll ? &overrideAll : &overridePat);
187 ImGui::Checkbox("##o-col", overrideAll ? &overrideAll : &overrideCol);
188 ImGui::Checkbox("##o-nam", overrideAll ? &overrideAll : &overrideNam);
189 ImGui::Checkbox("##o-rows", overrideAll ? &overrideAll : &overrideRows);
190 ImGui::Checkbox("##o-color0", overrideAll ? &overrideAll : &overrideColor0);
191 });
192 });
193 ImGui::SameLine();
194 im::Group([&]{
195 im::ItemWidth(ImGui::GetFontSize() * 9.0f, [&]{
196 im::Disabled(!manMode, [&]{
197 if (isMSX1 && (manualMode == one_of(TEXT80, SCR4))) manualMode = TEXT40;
198 im::Combo("##mode", modeToStr(manualMode), [&]{
199 if (ImGui::Selectable("screen 0,40")) manualMode = TEXT40;
200 if (!isMSX1 && ImGui::Selectable("screen 0,80")) manualMode = TEXT80;
201 if (ImGui::Selectable("screen 1")) manualMode = SCR1;
202 if (ImGui::Selectable("screen 2")) manualMode = SCR2;
203 if (ImGui::Selectable("screen 3")) manualMode = SCR3;
204 if (!isMSX1 && ImGui::Selectable("screen 4")) manualMode = SCR4;
205 });
206 });
207 static const char* const range0_15 = "0\0001\0002\0003\0004\0005\0006\0007\0008\0009\00010\00011\00012\00013\00014\00015\000";
208 im::Disabled(manualMode != one_of(TEXT40, TEXT80), [&]{
209 im::Disabled(!manFgCol, [&]{
210 ImGui::Combo("##fgCol", &manualFgCol, range0_15);
211 });
212 im::Disabled(!manBgCol, [&]{
213 ImGui::Combo("##bgCol", &manualBgCol, range0_15);
214 });
215 });
216 im::Disabled(manualMode != TEXT80, [&]{
217 im::Disabled(!manFgBlink, [&]{
218 ImGui::Combo("##fgBlink", &manualFgBlink, range0_15);
219 });
220 im::Disabled(!manBgBlink, [&]{
221 ImGui::Combo("##bgBlink", &manualBgBlink, range0_15);
222 });
223 im::Disabled(!manBlink, [&]{
224 ImGui::Combo("##blink", &manualBlink, "disabled\000enabled\000");
225 });
226 });
227 im::Disabled(!manPat, [&]{
228 comboHexSequence<5>("##pattern", &manualPatBase, patMult(manualMode), vramSize, 0);
229 });
230 im::Disabled(manualMode == one_of(TEXT40, SCR3), [&]{
231 im::Disabled(!manCol, [&]{
232 comboHexSequence<5>("##color", &manualColBase, colMult(manualMode), vramSize, 0);
233 });
234 });
235 im::Disabled(!manNam, [&]{
236 comboHexSequence<5>("##name", &manualNamBase, namMult(manualMode), vramSize, 0);
237 });
238 im::Disabled(!manRows, [&]{
239 ImGui::Combo("##rows", &manualRows, "24\00026.5\00032\000");
240 });
241 im::Disabled(!manColor0, [&]{
242 ImGui::Combo("##Color 0 replacement", &manualColor0, color0Str);
243 });
244 });
245 });
246 });
247
248 ImGui::SameLine();
249 ImGui::Dummy(ImVec2(25, 1));
250 ImGui::SameLine();
251 im::Group([&]{
252 ImGui::SetNextItemWidth(ImGui::GetFontSize() * 10.0f);
253 ImGui::Combo("Palette", &manager.palette->whichPalette, "VDP\000Custom\000Fixed\000");
254 if (ImGui::Button("Open palette editor")) {
255 manager.palette->window.raise();
256 }
257 ImGui::Separator();
258 ImGui::SetNextItemWidth(ImGui::GetFontSize() * 3.0f);
259 ImGui::Combo("Zoom", &zoom, "1x\0002x\0003x\0004x\0005x\0006x\0007x\0008x\000");
260 ImGui::Checkbox("grid", &grid);
261 ImGui::SameLine();
262 im::Disabled(!grid, [&]{
263 ImGui::ColorEdit4("Grid color", gridColor.data(),
264 ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_AlphaBar);
265 });
266 ImGui::Checkbox("Name table overlay", &nameTableOverlay);
267 });
268 });
269 int manualLines = (manualRows == 0) ? 192
270 : (manualRows == 1) ? 212
271 : 256;
272
273 int mode = manMode ? manualMode : vdpMode;
274 if (mode == SCR4) mode = SCR2;
275
276 int lines = manRows ? manualLines : vdpLines;
277 int color0 = manColor0 ? manualColor0 : vdpColor0;
278
279 VramTable patTable(vram);
280 unsigned patReg = (manPat ? (manualPatBase | (patMult(manualMode) - 1)) : vdp->getPatternTableBase()) >> 11;
281 patTable.setRegister(patReg, 11);
282
283 VramTable colTable(vram);
284 unsigned colReg = (manCol ? (manualColBase | (colMult(manualMode) - 1)) : vdp->getColorTableBase()) >> 6;
285 colTable.setRegister(colReg, 6);
286
287 VramTable namTable(vram);
288 unsigned namReg = (manNam ? (manualNamBase | (namMult(manualMode) - 1)) : vdp->getNameTableBase()) >> 10;
289 namTable.setRegister(namReg, 10);
290 namTable.setIndexSize((mode == TEXT80) ? 12 : 10);
291
292 std::array<uint32_t, 16> palette;
293 auto msxPalette = manager.palette->getPalette(vdp);
294 ranges::transform(msxPalette, palette.data(),
295 [](uint16_t msx) { return ImGuiPalette::toRGBA(msx); });
296 if (color0 < 16) palette[0] = palette[color0];
297
298 auto fgCol = manFgCol ? manualFgCol : vdpFgCol;
299 auto bgCol = manBgCol ? manualBgCol : vdpBgCol;
300 auto fgBlink = manFgBlink ? manualFgBlink : vdpFgBlink;
301 auto bgBlink = manBgBlink ? manualBgBlink : vdpBgBlink;
302 auto blink = manBlink ? bool(manualBlink) : vdpBlink;
303
304 bool narrow = mode == TEXT80;
305 int zx = (1 + zoom) * (narrow ? 1 : 2);
306 int zy = (1 + zoom) * 2;
307 gl::vec2 zm{float(zx), float(zy)};
308
309 // Render the patterns to a texture, we need this both to display the name-table and the pattern-table
310 auto patternTexSize = [&]() -> gl::ivec2 {
311 if (mode == TEXT40) return {192, 64}; // 8 rows of 32 characters, each 6x8 pixels
312 if (mode == TEXT80) return {192, 128}; // 8 rows of 32 characters, each 6x8 pixels, x2 for blink
313 if (mode == SCR1) return {256, 64}; // 8 rows of 32 characters, each 8x8 pixels
314 if (mode == SCR2) return {256, lines == 192 ? 192 : 256}; // 8 rows of 32 characters, each 8x8 pixels, all this 3 or 4 times
315 if (mode == SCR3) return { 64, 64}; // 8 rows of 32 characters, each 2x2 pixels, all this 4 times
316 return {1, 1}; // OTHER -> dummy
317 }();
318 auto patternTexChars = [&]() -> gl::vec2 {
319 if (mode == SCR3) return {32.0f, 32.0f};
320 if (mode == SCR2) return {32.0f, lines == 192 ? 24.0f : 32.0f};
321 if (mode == TEXT80) return {32.0f, 16.0f};
322 return {32.0f, 8.0f};
323 }();
324 auto recipPatTexChars = recip(patternTexChars);
325 auto patternDisplaySize = [&]() -> gl::ivec2 {
326 if (mode == TEXT40) return {192, 64};
327 if (mode == TEXT80) return {192, 128};
328 if (mode == SCR2) return {256, lines == 192 ? 192 : 256};
329 if (mode == SCR3) return {256, 256};
330 return {256, 64}; // SCR1, OTHER
331 }();
332 std::array<uint32_t, 256 * 256> pixels; // max size for SCR2
333 renderPatterns(mode, palette, fgCol, bgCol, fgBlink, bgBlink, patTable, colTable, lines, pixels);
334 if (!patternTex.get()) {
335 patternTex = gl::Texture(false, false); // no interpolation, no wrapping
336 }
337 patternTex.bind();
338 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, patternTexSize.x, patternTexSize.y, 0,
339 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
340
341 // create grid texture
342 auto charWidth = mode == one_of(TEXT40, TEXT80) ? 6 : 8;
343 auto charSize = gl::vec2{float(charWidth), 8.0f};
344 auto charZoom = zm * charSize;
345 auto zoomCharSize = gl::vec2(narrow ? 6.0f : 12.0f, 12.0f) * charSize; // hovered size
346 if (grid) {
347 auto gColor = ImGui::ColorConvertFloat4ToU32(gridColor);
348 auto gridWidth = charWidth * zx;
349 auto gridHeight = 8 * zy;
350 for (auto y : xrange(gridHeight)) {
351 auto* line = &pixels[y * gridWidth];
352 for (auto x : xrange(gridWidth)) {
353 line[x] = (x == 0 || y == 0) ? gColor : 0;
354 }
355 }
356 if (!gridTex.get()) {
357 gridTex = gl::Texture(false, true); // no interpolation, with wrapping
358 }
359 gridTex.bind();
360 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gridWidth, gridHeight, 0,
361 GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
362 }
363 if (nameTableOverlay && !smallHexDigits.get()) {
364 initHexDigits();
365 }
366
367 auto printPatternNr = [](unsigned pat) {
368 ImGui::StrCat("Pattern: ", pat, " (0x", hex_string<3>(pat), ')');
369 };
370 auto printAddressName = [](std::string_view name) {
371 ImGui::StrCat(name, " address:");
372 };
373 auto printAddress = [&](std::string_view name, unsigned address) {
374 printAddressName(name);
375 ImGui::StrCat(" 0x", hex_string<5>(address));
376 };
377 auto printAddressRange8 = [&](std::string_view name, unsigned address) {
378 printAddressName(name);
379 ImGui::StrCat(" 0x", hex_string<5>(address), "-0x", hex_string<5>(address + 7));
380 };
381 auto printPatternColorAddress = [&](unsigned pattern) {
382 printAddressRange8("Pattern", patTable.getAddress(8 * pattern));
383 if (mode == one_of(SCR1, SCR2, SCR4)) {
384 printAddressRange8("Color", colTable.getAddress((mode == SCR1) ? (pattern / 8) : (8 * pattern)));
385 }
386 };
387
388 ImGui::Separator();
389 im::TreeNode("Pattern Table", ImGuiTreeNodeFlags_DefaultOpen, [&]{
390 auto size = gl::vec2(patternDisplaySize) * zm;
391 auto textLines = (mode == TEXT40) ? 3.0f : 5.0f;
392 auto previewHeight = zoomCharSize.y + textLines * ImGui::GetTextLineHeightWithSpacing();
393 im::Child("##pattern", {0, std::max(size.y, previewHeight)}, 0, ImGuiWindowFlags_HorizontalScrollbar, [&]{
394 auto pos1 = ImGui::GetCursorPos();
395 gl::vec2 scrnPos = ImGui::GetCursorScreenPos();
396 ImGui::Image(patternTex.getImGui(), size);
397 bool hovered = ImGui::IsItemHovered() && (mode != OTHER);
398 ImGui::SameLine();
399 im::Group([&]{
400 if (hovered) {
401 auto gridPos = trunc((gl::vec2(ImGui::GetIO().MousePos) - scrnPos) / charZoom);
402 auto pat = gridPos.x + 32 * gridPos.y;
403 printPatternNr(pat);
404 auto uv1 = gl::vec2(gridPos) * recipPatTexChars;
405 auto uv2 = uv1 + recipPatTexChars;
406 auto pos2 = ImGui::GetCursorPos();
407 ImGui::Image(patternTex.getImGui(), zoomCharSize, uv1, uv2);
408 if (grid) {
409 ImGui::SetCursorPos(pos2);
410 ImGui::Image(gridTex.getImGui(),
411 zoomCharSize, {}, charSize);
412 }
413 printPatternColorAddress(pat);
414 } else {
415 ImGui::Dummy(zoomCharSize);
416 }
417 });
418 if (grid) {
419 ImGui::SetCursorPos(pos1);
420 ImGui::Image(gridTex.getImGui(), size,
421 {}, patternTexChars);
422 }
423 });
424 });
425 ImGui::Separator();
426 im::TreeNode("Name Table", ImGuiTreeNodeFlags_DefaultOpen, [&]{
427 float rows = float(lines) * (1.0f / 8.0f); // 24, 26.5 or 32
428 auto columns = [&] {
429 if (mode == TEXT40) return 40;
430 if (mode == TEXT80) return 80;
431 return 32;
432 }();
433 auto charsSize = gl::vec2(float(columns), rows); // (x * y) number of characters
434 auto msxSize = charSize * charsSize; // (x * y) number of MSX pixels
435 auto hostSize = msxSize * zm; // (x * y) number of host pixels
436
437 im::Child("##name", {0.0f, hostSize.y}, 0, ImGuiWindowFlags_HorizontalScrollbar, [&]{
438 gl::vec2 scrnPos = ImGui::GetCursorScreenPos();
439 auto* drawList = ImGui::GetWindowDrawList();
440
441 auto getPattern = [&](unsigned column, unsigned row) {
442 auto block = [&]() -> unsigned {
443 if (mode == TEXT80 && blink) {
444 auto colPat = colTable[10 * row + (column / 8)];
445 return (colPat & (0x80 >> (column % 8))) ? 1 : 0;
446 }
447 if (mode == SCR2) return row / 8;
448 if (mode == SCR3) return row % 4;
449 return 0;
450 }();
451 return namTable[columns * row + column] + 256 * block;
452 };
453 auto getPatternUV = [&](unsigned pattern) -> std::pair<gl::vec2, gl::vec2> {
454 auto patRow = pattern / 32;
455 auto patCol = pattern % 32;
456 gl::vec2 uv1 = gl::vec2{float(patCol), float(patRow)} * recipPatTexChars;
457 gl::vec2 uv2 = uv1 + recipPatTexChars;
458 return {uv1, uv2};
459 };
460
461 if (mode == OTHER) {
462 drawList->AddRectFilled(scrnPos, scrnPos + hostSize, getColor(imColor::GRAY));
463 } else {
464 auto rowsCeil = int(ceilf(rows));
465 auto numChars = rowsCeil * columns;
466 drawList->PushClipRect(scrnPos, scrnPos + hostSize, true);
467 drawList->PushTextureID(patternTex.getImGui());
468 drawList->PrimReserve(6 * numChars, 4 * numChars);
469 for (auto row : xrange(rowsCeil)) {
470 for (auto column : xrange(columns)) {
471 gl::vec2 p1 = scrnPos + charZoom * gl::vec2{float(column), float(row)};
472 gl::vec2 p2 = p1 + charZoom;
473 auto pattern = getPattern(column, row);
474 auto [uv1, uv2] = getPatternUV(pattern);
475 drawList->PrimRectUV(p1, p2, uv1, uv2, 0xffffffff);
476 }
477 }
478 drawList->PopTextureID();
479 if (nameTableOverlay) {
480 drawList->PushTextureID(smallHexDigits.getImGui());
481 drawList->PrimReserve(12 * numChars, 8 * numChars);
482 static constexpr gl::vec2 digitSize{5.0f, 8.0f};
483 static constexpr float texDigitWidth = 1.0f / 16.0f;
484 static constexpr gl::vec2 texDigitSize{texDigitWidth, 1.0f};
485 auto digitOffset = narrow ? gl::vec2{0.0f, digitSize.y} : gl::vec2{digitSize.x, 0.0f};
486 for (auto row : xrange(rowsCeil)) {
487 for (auto column : xrange(columns)) {
488 gl::vec2 p1 = scrnPos + charZoom * gl::vec2{float(column), float(row)};
489 gl::vec2 p2 = p1 + digitOffset;
490 auto pattern = getPattern(column, row);
491 gl::vec2 uv1{narrow_cast<float>((pattern >> 4) & 15) * texDigitWidth, 0.0f};
492 gl::vec2 uv2{narrow_cast<float>((pattern >> 0) & 15) * texDigitWidth, 0.0f};
493 drawList->PrimRectUV(p1, p1 + digitSize, uv1, uv1 + texDigitSize, 0xffffffff);
494 drawList->PrimRectUV(p2, p2 + digitSize, uv2, uv2 + texDigitSize, 0xffffffff);
495 }
496 }
497 drawList->PopTextureID();
498 }
499 drawList->PopClipRect();
500 }
501
502 auto pos1 = ImGui::GetCursorPos();
503 ImGui::Dummy(hostSize);
504 bool hovered = ImGui::IsItemHovered() && (mode != OTHER);
505 ImGui::SameLine();
506 im::Group([&]{
507 if (hovered) {
508 auto gridPos = trunc((gl::vec2(ImGui::GetIO().MousePos) - scrnPos) / charZoom);
509 ImGui::StrCat("Column: ", gridPos.x, " Row: ", gridPos.y);
510 auto pattern = getPattern(gridPos.x, gridPos.y);
511 printPatternNr(pattern);
512 auto [uv1, uv2] = getPatternUV(pattern);
513 auto pos2 = ImGui::GetCursorPos();
514 ImGui::Image(patternTex.getImGui(), zoomCharSize, uv1, uv2);
515 if (grid) {
516 ImGui::SetCursorPos(pos2);
517 ImGui::Image(gridTex.getImGui(),
518 zoomCharSize, {}, charSize);
519 }
520 auto nameIndex = columns * gridPos.y + gridPos.x;
521 printAddress("Name", namTable.getAddress(nameIndex));
522 printPatternColorAddress(pattern);
523 if (mode == TEXT80) {
524 printAddress("Color", colTable.getAddress(nameIndex / 8));
525 }
526 } else {
527 gl::vec2 textSize = ImGui::CalcTextSize("Column: 31 Row: 23"sv);
528 ImGui::Dummy(max(zoomCharSize, textSize));
529 }
530 });
531 if (grid) {
532 ImGui::SetCursorPos(pos1);
533 ImGui::Image(gridTex.getImGui(), hostSize, {}, charsSize);
534 }
535 });
536 });
537 });
538}
539
540static void draw6(uint8_t pattern, uint32_t fgCol, uint32_t bgCol, std::span<uint32_t, 6> out)
541{
542 out[0] = (pattern & 0x80) ? fgCol : bgCol;
543 out[1] = (pattern & 0x40) ? fgCol : bgCol;
544 out[2] = (pattern & 0x20) ? fgCol : bgCol;
545 out[3] = (pattern & 0x10) ? fgCol : bgCol;
546 out[4] = (pattern & 0x08) ? fgCol : bgCol;
547 out[5] = (pattern & 0x04) ? fgCol : bgCol;
548}
549
550static void draw8(uint8_t pattern, uint32_t fgCol, uint32_t bgCol, std::span<uint32_t, 8> out)
551{
552 out[0] = (pattern & 0x80) ? fgCol : bgCol;
553 out[1] = (pattern & 0x40) ? fgCol : bgCol;
554 out[2] = (pattern & 0x20) ? fgCol : bgCol;
555 out[3] = (pattern & 0x10) ? fgCol : bgCol;
556 out[4] = (pattern & 0x08) ? fgCol : bgCol;
557 out[5] = (pattern & 0x04) ? fgCol : bgCol;
558 out[6] = (pattern & 0x02) ? fgCol : bgCol;
559 out[7] = (pattern & 0x01) ? fgCol : bgCol;
560}
561
562void ImGuiCharacter::renderPatterns(int mode, std::span<const uint32_t, 16> palette,
563 int fgCol, int bgCol, int fgBlink, int bgBlink,
564 VramTable& pat, VramTable& col, int lines, std::span<uint32_t> output)
565{
566 switch (mode) {
567 case TEXT40:
568 case TEXT80: {
569 pat.setIndexSize(11);
570 col.setIndexSize(9); // only matters for TEXT80
571 auto fg = palette[fgCol];
572 auto bg = palette[bgCol];
573 auto fgB = palette[fgBlink];
574 auto bgB = palette[bgBlink];
575 for (auto row : xrange(8)) {
576 for (auto column : xrange(32)) {
577 auto patNum = 32 * row + column;
578 auto offset = 8 * patNum;
579 for (auto y : xrange(8)) {
580 auto pattern = pat[offset + y];
581 auto out = subspan<6>(output, (8 * row + y) * 192 + 6 * column);
582 draw6(pattern, fg, bg, out);
583 if (mode == TEXT80) {
584 auto out2 = subspan<6>(output, (64 + 8 * row + y) * 192 + 6 * column);
585 draw6(pattern, fgB, bgB, out2);
586 }
587 }
588 }
589 }
590 break;
591 }
592 case SCR1:
593 pat.setIndexSize(11);
594 col.setIndexSize(6);
595 for (auto row : xrange(8)) {
596 for (auto group : xrange(4)) { // 32 columns, split in 4 groups of 8
597 auto color = col[4 * row + group];
598 auto fg = palette[color >> 4];
599 auto bg = palette[color & 15];
600 for (auto subColumn : xrange(8)) {
601 auto column = 8 * group + subColumn;
602 auto patNum = 32 * row + column;
603 auto offset = 8 * patNum;
604 for (auto y : xrange(8)) {
605 auto pattern = pat[offset + y];
606 auto out = subspan<8>(output, (8 * row + y) * 256 + 8 * column);
607 draw8(pattern, fg, bg, out);
608 }
609 }
610 }
611 }
612 break;
613 case SCR2:
614 pat.setIndexSize(13);
615 col.setIndexSize(13);
616 for (auto row : xrange((lines == 192 ? 3 : 4) * 8)) {
617 for (auto column : xrange(32)) {
618 auto patNum = 32 * row + column;
619 auto offset = 8 * patNum;
620 for (auto y : xrange(8)) {
621 auto pattern = pat[offset + y];
622 auto color = col[offset + y];
623 auto fg = palette[color >> 4];
624 auto bg = palette[color & 15];
625 auto out = subspan<8>(output, (8 * row + y) * 256 + 8 * column);
626 draw8(pattern, fg, bg, out);
627 }
628 }
629 }
630 break;
631 case SCR3:
632 pat.setIndexSize(11);
633 col.setIndexSize(13); // not used?
634 for (auto group : xrange(4)) {
635 for (auto row : xrange(8)) {
636 for (auto column : xrange(32)) {
637 auto patNum = 32 * row + column;
638 auto offset = 8 * patNum + 2 * group;
639 for (auto y : xrange(2)) {
640 auto out = subspan<2>(output, (16 * group + 2 * row + y) * 64 + 2 * column);
641 auto pattern = pat[offset + y];
642 out[0] = palette[pattern >> 4];
643 out[1] = palette[pattern & 15];
644 }
645 }
646 }
647 }
648 break;
649 default:
650 output[0] = getColor(imColor::GRAY);
651 break;
652 }
653}
654
655} // 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
static constexpr uint8_t GRAPHIC3
static constexpr uint8_t MULTICOLOR
static constexpr uint8_t GRAPHIC1
static constexpr uint8_t TEXT2
static constexpr uint8_t GRAPHIC2
static constexpr uint8_t TEXT1
void paint(MSXMotherBoard *motherBoard) override
void save(ImGuiTextBuffer &buf) override
void loadLine(std::string_view name, zstring_view value) override
std::unique_ptr< ImGuiPalette > palette
ImGuiManager & manager
Definition ImGuiPart.hh:30
MSXDevice * findDevice(std::string_view name)
Find a MSXDevice by name.
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)
auto getAddress(unsigned index) const
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
auto CalcTextSize(std::string_view str)
Definition ImGuiUtils.hh:38
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
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
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 savePersistent(ImGuiTextBuffer &buf, C &c, const std::tuple< Elements... > &tup)
ImU32 getColor(imColor col)
const char * getComboString(int item, const char *itemsSeparatedByZeros)
auto transform(InputRange &&range, OutputIter out, UnaryOperation op)
Definition ranges.hh:279
Definition view.hh:15
constexpr To narrow(From from) noexcept
Definition narrow.hh:37
constexpr auto xrange(T e)
Definition xrange.hh:132