Debian Codegraph @

src/gui/memory.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "gui.h"
21 #include <fmt/printf.h>
22 #include "imgui.h"
23 #include "imgui_internal.h"
24
25 #define CENTER_TEXT(text) \
26 ImGui::SetCursorPosX(ImGui::GetCursorPosX()+0.5*(ImGui::GetContentRegionAvail().x-ImGui::CalcTextSize(text).x));
27
28 void FurnaceGUI::drawMemory() {
29 if (nextWindow==GUI_WINDOW_MEMORY) {
30 memoryOpen=true;
31 ImGui::SetNextWindowFocus();
32 nextWindow=GUI_WINDOW_NOTHING;
33 }
34 if (!memoryOpen) return;
35 if (ImGui::Begin("Memory Composition",&memoryOpen,globalWinFlags,_("Memory Composition"))) {
36 ImDrawList* dl=ImGui::GetWindowDrawList();
37 ImGuiWindow* window=ImGui::GetCurrentWindow();
38 char tempID[1024];
39 bool have=false;
40
41 for (int i=0; i<e->song.systemLen; i++) {
42 DivDispatch* dispatch=e->getDispatch(i);
43 for (int j=0; j<4; j++) {
44 const DivMemoryComposition* mc=dispatch->getMemCompo(j);
45 if (mc==NULL) break;
46 have=true;
47
48 ImGui::Text("%s: %s",e->getSystemName(e->song.system[i]),mc->name.c_str());
49 ImGui::SameLine();
50 if (mc->capacity>=1024 && settings.memUsageUnit==1) {
51 ImGui::Text("%dK/%dK (%.1f%%)",(int)mc->used>>10,(int)mc->capacity>>10,100.0*(double)mc->used/(double)mc->capacity);
52 } else {
53 ImGui::Text("%d/%d (%.1f%%)",(int)mc->used,(int)mc->capacity,100.0*(double)mc->used/(double)mc->capacity);
54 }
55
56 ImVec2 size=ImVec2(ImGui::GetContentRegionAvail().x,36.0f*dpiScale);
57 ImVec2 minArea=window->DC.CursorPos;
58 ImVec2 maxArea=ImVec2(
59 minArea.x+size.x,
60 minArea.y+size.y
61 );
62 ImRect rect=ImRect(minArea,maxArea);
63 ImRect dataRect=rect;
64 ImRect entryRect=rect;
65 ImGuiStyle& style=ImGui::GetStyle();
66 ImGui::ItemSize(size,style.FramePadding.y);
67 snprintf(tempID,1023,"MC%d_%d",i,j);
68 if (ImGui::ItemAdd(rect,ImGui::GetID(tempID))) {
69 dl->AddRectFilled(rect.Min,rect.Max,ImGui::GetColorU32(uiColors[GUI_COLOR_MEMORY_BG]));
70
71 if (mc->memory!=NULL && mc->waveformView!=DIV_MEMORY_WAVE_NONE) {
72 dataRect.Max.y-=8.0f*dpiScale;
73 entryRect.Min.y=entryRect.Max.y-8.0f*dpiScale;
74 }
75
76 int curHover=-1;
77 int kIndex=0;
78 if (mc->capacity>0) for (const DivMemoryEntry& k: mc->entries) {
79 if (k.begin==k.end) {
80 kIndex++;
81 continue;
82 }
83 ImVec2 pos1=ImLerp(entryRect.Min,entryRect.Max,ImVec2((double)k.begin/(double)mc->capacity,(k.type==DIV_MEMORY_N163_LOAD)?0.5f:0.0f));
84 ImVec2 pos2=ImLerp(entryRect.Min,entryRect.Max,ImVec2((double)k.end/(double)mc->capacity,(k.type==DIV_MEMORY_N163_PLAY)?0.5f:1.0f));
85 ImVec2 linePos=pos1;
86 linePos.y=rect.Max.y;
87
88 dl->AddRectFilled(pos1,pos2,ImGui::GetColorU32(uiColors[GUI_COLOR_MEMORY_FREE+(int)k.type]));
89 dl->AddLine(pos1,linePos,ImGui::GetColorU32(ImGuiCol_Border),dpiScale);
90
91 if (ImGui::GetMousePos().x>=pos1.x &&
92 ImGui::GetMousePos().x<=pos2.x &&
93 ImGui::GetMousePos().y>=pos1.y &&
94 ImGui::GetMousePos().y<=pos2.y) {
95 curHover=kIndex;
96 }
97 kIndex++;
98 }
99
100 if (mc->memory!=NULL) {
101 switch (mc->waveformView) {
102 case DIV_MEMORY_WAVE_4BIT:
103 for (int k=0; k<(int)(mc->capacity<<1); k++) {
104 unsigned char nibble=mc->memory[k>>1];
105 if (k&1) {
106 nibble>>=4;
107 } else {
108 nibble&=15;
109 }
110
111 ImVec2 pos1=ImLerp(dataRect.Min,dataRect.Max,ImVec2((double)k/(double)(mc->capacity<<1),1.0f-((float)(nibble+1)/16.0f)));
112 ImVec2 pos2=ImLerp(dataRect.Min,dataRect.Max,ImVec2((double)(k+1)/(double)(mc->capacity<<1),1.0f));
113 dl->AddRectFilled(pos1,pos2,ImGui::GetColorU32(uiColors[GUI_COLOR_MEMORY_DATA]));
114 }
115 break;
116 case DIV_MEMORY_WAVE_8BIT_SIGNED:
117 for (int k=0; k<(int)mc->capacity; k++) {
118 signed char val=(signed char)mc->memory[k];
119 ImVec2 pos1=ImLerp(dataRect.Min,dataRect.Max,ImVec2((double)k/(double)(mc->capacity),1.0f-((float)(val+129)/256.0f)));
120 ImVec2 pos2=ImLerp(dataRect.Min,dataRect.Max,ImVec2((double)(k+1)/(double)(mc->capacity),1.0f));
121 dl->AddRectFilled(pos1,pos2,ImGui::GetColorU32(uiColors[GUI_COLOR_MEMORY_DATA]));
122 }
123 break;
124 default:
125 break;
126 }
127 }
128
129 if (ImGui::ItemHoverable(rect,ImGui::GetID(tempID),0)) {
130 if (curHover>=0 && curHover<(int)mc->entries.size()) {
131 const DivMemoryEntry& entry=mc->entries[curHover];
132 if (ImGui::BeginTooltip()) {
133 switch (entry.type) {
134 case DIV_MEMORY_SAMPLE:
135 case DIV_MEMORY_BANK0:
136 case DIV_MEMORY_BANK1:
137 case DIV_MEMORY_BANK2:
138 case DIV_MEMORY_BANK3:
139 case DIV_MEMORY_BANK4:
140 case DIV_MEMORY_BANK5:
141 case DIV_MEMORY_BANK6:
142 case DIV_MEMORY_BANK7: {
143 DivSample* sample=e->getSample(entry.asset);
144 ImGui::Text("%d: %s",curHover,sample->name.c_str());
145 if ((int)entry.type>=(int)DIV_MEMORY_BANK0) {
146 ImGui::Text(_("bank %d"),(int)entry.type-(int)DIV_MEMORY_BANK0);
147 }
148 if ((entry.end-entry.begin)>=1024 && settings.memUsageUnit==1) {
149 ImGui::Text("%d-%d ($%x-$%x): %dK ($%x)",(int)entry.begin,(int)entry.end-1,(int)entry.begin,(int)entry.end-1,(int)(entry.end-entry.begin)>>10,(int)(entry.end-entry.begin));
150 } else {
151 ImGui::Text("%d-%d ($%x-$%x): %d bytes ($%x)",(int)entry.begin,(int)entry.end-1,(int)entry.begin,(int)entry.end-1,(int)(entry.end-entry.begin),(int)(entry.end-entry.begin));
152 }
153 break;
154 }
155 default:
156 ImGui::Text("%d: %s",curHover,entry.name.c_str());
157 if ((entry.end-entry.begin)>=1024 && settings.memUsageUnit==1) {
158 ImGui::Text("%d-%d ($%x-$%x): %dK ($%x)",(int)entry.begin,(int)entry.end-1,(int)entry.begin,(int)entry.end-1,(int)(entry.end-entry.begin)>>10,(int)(entry.end-entry.begin));
159 } else {
160 ImGui::Text("%d-%d ($%x-$%x): %d bytes ($%x)",(int)entry.begin,(int)entry.end-1,(int)entry.begin,(int)entry.end-1,(int)(entry.end-entry.begin),(int)(entry.end-entry.begin));
161 }
162 break;
163 }
164
165 ImGui::EndTooltip();
166 }
167 }
168 }
169 }
170 }
171 }
172
173 if (!have) {
174 ImGui::SetCursorPosY(ImGui::GetCursorPosY()+(ImGui::GetContentRegionAvail().y-ImGui::GetFrameHeight()+ImGui::GetStyle().ItemSpacing.y)*0.5f);
175 CENTER_TEXT(_("no chips with memory"));
176 ImGui::Text(_("no chips with memory"));
177 }
178 }
179 if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows)) curWindow=GUI_WINDOW_MEMORY;
180 ImGui::End();
181 }