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