|
11 | 11 | #include "draw.h" |
12 | 12 | #include "fs.h" |
13 | 13 |
|
| 14 | +static u8 *top_screen0 = NULL; |
| 15 | +static u8 *top_screen1 = NULL; |
| 16 | +static u8 *bot_screen0 = NULL; |
| 17 | +static u8 *bot_screen1 = NULL; |
| 18 | + |
14 | 19 | static char debugstr[DBG_N_CHARS_X * DBG_N_CHARS_Y] = { 0 }; |
15 | 20 | static u32 debugcol[DBG_N_CHARS_Y] = { DBG_COLOR_FONT }; |
16 | 21 |
|
17 | | -void ClearScreen(u8* screen, int width, int color) |
| 22 | +void InitScreen(int argc, char *argv[]) |
| 23 | +{ |
| 24 | + if (argc >= 2) { |
| 25 | + /* newer entrypoints */ |
| 26 | + u8 **fb = (u8 **)(void *)argv[1]; |
| 27 | + top_screen0 = fb[0]; |
| 28 | + top_screen1 = fb[1]; |
| 29 | + bot_screen0 = fb[2]; |
| 30 | + bot_screen1 = fb[3]; |
| 31 | + } else { |
| 32 | + /* outdated entrypoints */ |
| 33 | + top_screen0 = (u8 *)(*(u32 *)0x23FFFE00); |
| 34 | + top_screen1 = (u8 *)(*(u32 *)0x23FFFE00); |
| 35 | + bot_screen0 = (u8 *)(*(u32 *)0x23FFFE08); |
| 36 | + bot_screen1 = (u8 *)(*(u32 *)0x23FFFE08); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +void ClearScreen(u8 *screen, int width, int color) |
18 | 41 | { |
19 | | - if (color == COLOR_TRANSPARENT) color = COLOR_BLACK; |
20 | | - for (int i = 0; i < (width * SCREEN_HEIGHT); i++) { |
21 | | - *(screen++) = color >> 16; // B |
22 | | - *(screen++) = color >> 8; // G |
23 | | - *(screen++) = color & 0xFF; // R |
24 | | - } |
| 42 | + if (color == COLOR_TRANSPARENT) |
| 43 | + color = COLOR_BLACK; |
| 44 | + |
| 45 | + for (int i = 0; i < (width * SCREEN_HEIGHT); i++) { |
| 46 | + *(screen++) = color >> 16; // B |
| 47 | + *(screen++) = color >> 8; // G |
| 48 | + *(screen++) = color & 0xFF; // R |
| 49 | + } |
25 | 50 | } |
26 | 51 |
|
27 | 52 | void ClearScreenFull(bool clear_top, bool clear_bottom) |
28 | 53 | { |
29 | | - if (clear_top) { |
30 | | - ClearScreen(TOP_SCREEN0, SCREEN_WIDTH_TOP, STD_COLOR_BG); |
31 | | - ClearScreen(TOP_SCREEN1, SCREEN_WIDTH_TOP, STD_COLOR_BG); |
32 | | - } |
33 | | - if (clear_bottom) { |
34 | | - ClearScreen(BOT_SCREEN0, SCREEN_WIDTH_BOT, STD_COLOR_BG); |
35 | | - ClearScreen(BOT_SCREEN1, SCREEN_WIDTH_BOT, STD_COLOR_BG); |
36 | | - } |
| 54 | + if (clear_top) { |
| 55 | + ClearScreen(top_screen0, SCREEN_WIDTH_TOP, STD_COLOR_BG); |
| 56 | + ClearScreen(top_screen1, SCREEN_WIDTH_TOP, STD_COLOR_BG); |
| 57 | + } |
| 58 | + if (clear_bottom) { |
| 59 | + ClearScreen(bot_screen0, SCREEN_WIDTH_BOT, STD_COLOR_BG); |
| 60 | + ClearScreen(bot_screen1, SCREEN_WIDTH_BOT, STD_COLOR_BG); |
| 61 | + } |
37 | 62 | } |
38 | 63 |
|
39 | | -void DrawCharacter(u8* screen, int character, int x, int y, int color, int bgcolor) |
| 64 | +void DrawCharacter(u8 *screen, int character, int x, int y, int color, int bgcolor) |
40 | 65 | { |
41 | | - for (int yy = 0; yy < 8; yy++) { |
42 | | - int xDisplacement = (x * BYTES_PER_PIXEL * SCREEN_HEIGHT); |
43 | | - int yDisplacement = ((SCREEN_HEIGHT - (y + yy) - 1) * BYTES_PER_PIXEL); |
44 | | - u8* screenPos = screen + xDisplacement + yDisplacement; |
45 | | - |
46 | | - u8 charPos = font[character * 8 + yy]; |
47 | | - for (int xx = 7; xx >= 0; xx--) { |
48 | | - if ((charPos >> xx) & 1) { |
49 | | - *(screenPos + 0) = color >> 16; // B |
50 | | - *(screenPos + 1) = color >> 8; // G |
51 | | - *(screenPos + 2) = color & 0xFF; // R |
52 | | - } else if (bgcolor != COLOR_TRANSPARENT) { |
53 | | - *(screenPos + 0) = bgcolor >> 16; // B |
54 | | - *(screenPos + 1) = bgcolor >> 8; // G |
55 | | - *(screenPos + 2) = bgcolor & 0xFF; // R |
56 | | - } |
57 | | - screenPos += BYTES_PER_PIXEL * SCREEN_HEIGHT; |
58 | | - } |
59 | | - } |
| 66 | + for (int yy = 0; yy < 8; yy++) { |
| 67 | + int xDisplacement = (x * BYTES_PER_PIXEL * SCREEN_HEIGHT); |
| 68 | + int yDisplacement = ((SCREEN_HEIGHT - (y + yy) - 1) * BYTES_PER_PIXEL); |
| 69 | + u8 *screenPos = screen + xDisplacement + yDisplacement; |
| 70 | + |
| 71 | + u8 charPos = font[character * 8 + yy]; |
| 72 | + for (int xx = 7; xx >= 0; xx--) { |
| 73 | + if ((charPos >> xx) & 1) { |
| 74 | + *(screenPos + 0) = color >> 16; // B |
| 75 | + *(screenPos + 1) = color >> 8; // G |
| 76 | + *(screenPos + 2) = color & 0xFF; // R |
| 77 | + } else if (bgcolor != COLOR_TRANSPARENT) { |
| 78 | + *(screenPos + 0) = bgcolor >> 16; // B |
| 79 | + *(screenPos + 1) = bgcolor >> 8; // G |
| 80 | + *(screenPos + 2) = bgcolor & 0xFF; // R |
| 81 | + } |
| 82 | + screenPos += BYTES_PER_PIXEL * SCREEN_HEIGHT; |
| 83 | + } |
| 84 | + } |
60 | 85 | } |
61 | 86 |
|
62 | | -void DrawString(u8* screen, const char *str, int x, int y, int color, int bgcolor) |
| 87 | +void DrawString(u8 *screen, const char *str, int x, int y, int color, int bgcolor) |
63 | 88 | { |
64 | | - for (size_t i = 0; i < strlen(str); i++) |
65 | | - DrawCharacter(screen, str[i], x + i * 8, y, color, bgcolor); |
| 89 | + for (size_t i = 0; i < strlen(str); i++) |
| 90 | + DrawCharacter(screen, str[i], x + i * 8, y, color, bgcolor); |
66 | 91 | } |
67 | 92 |
|
68 | 93 | void DrawStringF(int x, int y, bool use_top, const char *format, ...) |
69 | 94 | { |
70 | | - char str[512] = { 0 }; // 512 should be more than enough |
71 | | - va_list va; |
72 | | - |
73 | | - va_start(va, format); |
74 | | - vsnprintf(str, 512, format, va); |
75 | | - va_end(va); |
76 | | - |
77 | | - for (char* text = strtok(str, "\n"); text != NULL; text = strtok(NULL, "\n"), y += 10) { |
78 | | - if (use_top) { |
79 | | - DrawString(TOP_SCREEN0, text, x, y, STD_COLOR_FONT, STD_COLOR_BG); |
80 | | - DrawString(TOP_SCREEN1, text, x, y, STD_COLOR_FONT, STD_COLOR_BG); |
81 | | - } else { |
82 | | - DrawString(BOT_SCREEN0, text, x, y, STD_COLOR_FONT, STD_COLOR_BG); |
83 | | - DrawString(BOT_SCREEN1, text, x, y, STD_COLOR_FONT, STD_COLOR_BG); |
84 | | - } |
85 | | - } |
| 95 | + char str[512] = { 0 }; // 512 should be more than enough |
| 96 | + va_list va; |
| 97 | + |
| 98 | + va_start(va, format); |
| 99 | + vsnprintf(str, 512, format, va); |
| 100 | + va_end(va); |
| 101 | + |
| 102 | + for (char *text = strtok(str, "\n"); text != NULL; text = strtok(NULL, "\n"), y += 10) { |
| 103 | + if (use_top) { |
| 104 | + DrawString(top_screen0, text, x, y, STD_COLOR_FONT, STD_COLOR_BG); |
| 105 | + DrawString(top_screen1, text, x, y, STD_COLOR_FONT, STD_COLOR_BG); |
| 106 | + } else { |
| 107 | + DrawString(bot_screen0, text, x, y, STD_COLOR_FONT, STD_COLOR_BG); |
| 108 | + DrawString(bot_screen1, text, x, y, STD_COLOR_FONT, STD_COLOR_BG); |
| 109 | + } |
| 110 | + } |
86 | 111 | } |
87 | 112 |
|
88 | 113 | void DebugClear() |
89 | 114 | { |
90 | | - memset(debugstr, 0x00, DBG_N_CHARS_X * DBG_N_CHARS_Y); |
91 | | - for (u32 y = 0; y < DBG_N_CHARS_Y; y++) |
92 | | - debugcol[y] = DBG_COLOR_FONT; |
93 | | - ClearScreen(TOP_SCREEN0, SCREEN_WIDTH_TOP, DBG_COLOR_BG); |
94 | | - ClearScreen(TOP_SCREEN1, SCREEN_WIDTH_TOP, DBG_COLOR_BG); |
| 115 | + memset(debugstr, 0x00, DBG_N_CHARS_X * DBG_N_CHARS_Y); |
| 116 | + for (u32 y = 0; y < DBG_N_CHARS_Y; y++) |
| 117 | + debugcol[y] = DBG_COLOR_FONT; |
| 118 | + ClearScreen(top_screen0, SCREEN_WIDTH_TOP, DBG_COLOR_BG); |
| 119 | + ClearScreen(top_screen1, SCREEN_WIDTH_TOP, DBG_COLOR_BG); |
95 | 120 | } |
96 | 121 |
|
97 | 122 | void DebugSet(const char **strs) |
98 | 123 | { |
99 | | - if (strs != NULL) for (int y = 0; y < DBG_N_CHARS_Y; y++) { |
100 | | - int pos_dbgstr = DBG_N_CHARS_X * (DBG_N_CHARS_Y - 1 - y); |
101 | | - snprintf(debugstr + pos_dbgstr, DBG_N_CHARS_X, "%-*.*s", DBG_N_CHARS_X - 1, DBG_N_CHARS_X - 1, strs[y]); |
102 | | - debugcol[y] = DBG_COLOR_FONT; |
103 | | - } |
104 | | - |
105 | | - int pos_y = DBG_START_Y; |
106 | | - u32* col = debugcol + (DBG_N_CHARS_Y - 1); |
107 | | - for (char* str = debugstr + (DBG_N_CHARS_X * (DBG_N_CHARS_Y - 1)); str >= debugstr; str -= DBG_N_CHARS_X, col--) { |
108 | | - if (*str != '\0') { |
109 | | - DrawString(TOP_SCREEN0, str, DBG_START_X, pos_y, *col, DBG_COLOR_BG); |
110 | | - DrawString(TOP_SCREEN1, str, DBG_START_X, pos_y, *col, DBG_COLOR_BG); |
111 | | - pos_y += DBG_STEP_Y; |
112 | | - } |
113 | | - } |
| 124 | + if (strs != NULL) { |
| 125 | + for (int y = 0; y < DBG_N_CHARS_Y; y++) { |
| 126 | + int pos_dbgstr = DBG_N_CHARS_X * (DBG_N_CHARS_Y - 1 - y); |
| 127 | + snprintf(debugstr + pos_dbgstr, DBG_N_CHARS_X, "%-*.*s", DBG_N_CHARS_X - 1, DBG_N_CHARS_X - 1, strs[y]); |
| 128 | + debugcol[y] = DBG_COLOR_FONT; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + int pos_y = DBG_START_Y; |
| 133 | + u32 *col = debugcol + (DBG_N_CHARS_Y - 1); |
| 134 | + for (char *str = debugstr + (DBG_N_CHARS_X * (DBG_N_CHARS_Y - 1)); str >= debugstr; str -= DBG_N_CHARS_X, col--) { |
| 135 | + if (*str != '\0') { |
| 136 | + DrawString(top_screen0, str, DBG_START_X, pos_y, *col, DBG_COLOR_BG); |
| 137 | + DrawString(top_screen1, str, DBG_START_X, pos_y, *col, DBG_COLOR_BG); |
| 138 | + pos_y += DBG_STEP_Y; |
| 139 | + } |
| 140 | + } |
114 | 141 | } |
115 | 142 |
|
116 | 143 | void DebugColor(u32 color, const char *format, ...) |
117 | 144 | { |
118 | | - static bool adv_output = true; |
119 | | - char tempstr[128] = { 0 }; // 128 instead of DBG_N_CHARS_X for log file |
120 | | - va_list va; |
121 | | - |
122 | | - va_start(va, format); |
123 | | - vsnprintf(tempstr, 128, format, va); |
124 | | - va_end(va); |
125 | | - |
126 | | - if (adv_output) { |
127 | | - memmove(debugstr + DBG_N_CHARS_X, debugstr, DBG_N_CHARS_X * (DBG_N_CHARS_Y - 1)); |
128 | | - memmove(debugcol + 1, debugcol, (DBG_N_CHARS_Y - 1) * sizeof(u32)); |
129 | | - } else { |
130 | | - adv_output = true; |
131 | | - } |
132 | | - |
133 | | - *debugcol = color; |
134 | | - if (*tempstr != '\r') { // not a good way of doing this - improve this later |
135 | | - snprintf(debugstr, DBG_N_CHARS_X, "%-*.*s", DBG_N_CHARS_X - 1, DBG_N_CHARS_X - 1, tempstr); |
136 | | - } else { |
137 | | - snprintf(debugstr, DBG_N_CHARS_X, "%-*.*s", DBG_N_CHARS_X - 1, DBG_N_CHARS_X - 1, tempstr + 1); |
138 | | - adv_output = false; |
139 | | - } |
140 | | - |
141 | | - DebugSet(NULL); |
| 145 | + static bool adv_output = true; |
| 146 | + char tempstr[128] = { 0 }; // 128 instead of DBG_N_CHARS_X for log file |
| 147 | + va_list va; |
| 148 | + |
| 149 | + va_start(va, format); |
| 150 | + vsnprintf(tempstr, 128, format, va); |
| 151 | + va_end(va); |
| 152 | + |
| 153 | + if (adv_output) { |
| 154 | + memmove(debugstr + DBG_N_CHARS_X, debugstr, DBG_N_CHARS_X * (DBG_N_CHARS_Y - 1)); |
| 155 | + memmove(debugcol + 1, debugcol, (DBG_N_CHARS_Y - 1) * sizeof(u32)); |
| 156 | + } else { |
| 157 | + adv_output = true; |
| 158 | + } |
| 159 | + |
| 160 | + *debugcol = color; |
| 161 | + if (*tempstr != '\r') { // not a good way of doing this - improve this later |
| 162 | + snprintf(debugstr, DBG_N_CHARS_X, "%-*.*s", DBG_N_CHARS_X - 1, DBG_N_CHARS_X - 1, tempstr); |
| 163 | + } else { |
| 164 | + snprintf(debugstr, DBG_N_CHARS_X, "%-*.*s", DBG_N_CHARS_X - 1, DBG_N_CHARS_X - 1, tempstr + 1); |
| 165 | + adv_output = false; |
| 166 | + } |
| 167 | + |
| 168 | + DebugSet(NULL); |
142 | 169 | } |
143 | 170 |
|
144 | 171 | void Debug(const char *format, ...) |
145 | 172 | { |
146 | | - char tempstr[128] = { 0 }; // 128 instead of DBG_N_CHARS_X for log file |
147 | | - va_list va; |
| 173 | + char tempstr[128] = { 0 }; // 128 instead of DBG_N_CHARS_X for log file |
| 174 | + va_list va; |
148 | 175 |
|
149 | | - va_start(va, format); |
150 | | - vsnprintf(tempstr, 128, format, va); |
151 | | - DebugColor(DBG_COLOR_FONT, tempstr); |
152 | | - va_end(va); |
| 176 | + va_start(va, format); |
| 177 | + vsnprintf(tempstr, 128, format, va); |
| 178 | + DebugColor(DBG_COLOR_FONT, tempstr); |
| 179 | + va_end(va); |
153 | 180 | } |
154 | 181 |
|
155 | 182 | void ShowProgress(u64 current, u64 total) |
156 | 183 | { |
157 | | - const u32 progX = SCREEN_WIDTH_TOP - 40; |
158 | | - const u32 progY = SCREEN_HEIGHT - 20; |
159 | | - |
160 | | - if (total > 0) { |
161 | | - char progStr[8]; |
162 | | - snprintf(progStr, 8, "%3llu%%", (current * 100) / total); |
163 | | - DrawString(TOP_SCREEN0, progStr, progX, progY, DBG_COLOR_FONT, DBG_COLOR_BG); |
164 | | - DrawString(TOP_SCREEN1, progStr, progX, progY, DBG_COLOR_FONT, DBG_COLOR_BG); |
165 | | - } else { |
166 | | - DrawString(TOP_SCREEN0, " ", progX, progY, DBG_COLOR_FONT, DBG_COLOR_BG); |
167 | | - DrawString(TOP_SCREEN1, " ", progX, progY, DBG_COLOR_FONT, DBG_COLOR_BG); |
168 | | - } |
| 184 | + const u32 progX = SCREEN_WIDTH_TOP - 40; |
| 185 | + const u32 progY = SCREEN_HEIGHT - 20; |
| 186 | + |
| 187 | + if (total > 0) { |
| 188 | + char progStr[8]; |
| 189 | + snprintf(progStr, 8, "%3llu%%", (current * 100) / total); |
| 190 | + DrawString(top_screen0, progStr, progX, progY, DBG_COLOR_FONT, DBG_COLOR_BG); |
| 191 | + DrawString(top_screen1, progStr, progX, progY, DBG_COLOR_FONT, DBG_COLOR_BG); |
| 192 | + } else { |
| 193 | + DrawString(top_screen0, " ", progX, progY, DBG_COLOR_FONT, DBG_COLOR_BG); |
| 194 | + DrawString(top_screen1, " ", progX, progY, DBG_COLOR_FONT, DBG_COLOR_BG); |
| 195 | + } |
169 | 196 | } |
0 commit comments