/
drawing.c
504 lines (332 loc) · 10.2 KB
/
drawing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#include "drawing.h"
#include "littlelibc.h"
#include "pads.h"
#include "main.h"
#include "gpu.h"
#include "utility.h"
#include "ttyredirect.h"
#include "hwregs.h"
#include "config.h"
#include "git_url.h"
// Little outline rectangles:
// Either a solid rect or 4 separate lines.
typedef struct Highlighter {
// target left, right, bottom and top values
int lTarg;
int rTarg;
int botTarg;
int topTarg;
// actual left, right bottom and top values
int lAct;
int rAct;
int botAct;
int topAct;
} highlighter;
highlighter h1;
highlighter h2;
typedef struct {
ulong tag;
uchar r0, g0, b0, code;
short x0, y0;
short w, h;
} NTILE;
ulong frameCounter = 0;
// Tell the GPU to finish up drawing, draw the font, vsync, etc.
void Draw(){
frameCounter++;
EndDrawing();
}
ulong GetFrameCount(){
return frameCounter;
}
// Wrapper for GPU drawTile
// -prevents flickering from weird positions
// -prevents spillover into the next buffer
// -converts RGB to BRG
void BorderTileColor( int inX, int inY, int inWidth, int inHeight, ulong inColor ){
// if it goes off the left, make it thinner
int outX = inX;
int outWidth = inWidth;
// same with the bottom?
int outY = inY;
int outHeight = inHeight;
ulong outColor = inColor;
ulong r = 0;
ulong b = 0;
if ( inX < 0 ){
outWidth += inX; //subtract that much
outX = 0;
}
if ( inY < 0 ){
outHeight += inY; // subtract that much
outY = 0;
}
if ( inX + inWidth >= SCREEN_WIDTH ){
outWidth = ( SCREEN_WIDTH - inX);
}
if ( outY + outHeight >= SCREEN_HEIGHT ){
//outHeight -= ( SCREEN_HEIGHT - outY);
outHeight = ( SCREEN_HEIGHT - outY );
}
if ( outWidth <= 0 )
return;
// swap some RGB
r = ( inColor & 0xFF0000 ) >> 16;
b = ( inColor & 0x0000FF ) << 16;
outColor = ( inColor & 0x00FF00 ) | r | b;
DrawTile( outX, outY, outWidth, outHeight, outColor );
}
void BorderTileRGB( int inX, int inY, int inWidth, int inHeight, char r, char g, char b){
BorderTileColor(
inX, inY,
inWidth, inHeight,
r | g << 8 | b << 16
);
}
void BorderTile( int inX, int inY, int inWidth, int inHeight ){
BorderTileColor( inX, inY, inWidth, inHeight, LIGHTBLUE );
BorderTileColor( inX, inY, inWidth, inHeight, LIGHTGREEN );
}
// Assumes a fixed time so we can hard code
// some of the factors. (E.g. time delta)
int Lerp( int from, int to ){
int delta;
int snap = 0;
if ( from == to ) return from;
delta = to - from;
snap = ( delta < 0 || delta > 0 );
delta = delta / 3;
// no floats, so snap within a pixel
if ( snap && delta == 0 ){
return to;
}
return from + delta;
}
void HighlightOffset( int inX, int inY, int inWidth, int inHeight, char colorOffset ){
NTILE t;
// int r = 0xA1, g = 0x9D, b = 0xFF; // Border color, too light
unsigned int r = 0x51;
unsigned int g = 0x5F;
unsigned int b = 0xFF; // Not true to C64, but looks good
int w = 0;
int h = 0;
// clamp it at 0xFF so we don't go over
r = ( r + colorOffset < 0xFF ) ? r + colorOffset : r;
g = ( g + colorOffset < 0xFF ) ? g + colorOffset : g;
b = ( b + colorOffset < 0xFF ) ? b + colorOffset : b;
h1.lTarg = inX - ( inWidth / 2 );
h1.rTarg = inX + ( inWidth / 2 );
h1.botTarg = inY + ( inHeight / 2 );
h1.topTarg = inY - ( inHeight / 2 );
h1.lAct = Lerp( h1.lAct, h1.lTarg );
h1.rAct = Lerp( h1.rAct, h1.rTarg );
h1.botAct = Lerp( h1.botAct, h1.botTarg );
h1.topAct = Lerp( h1.topAct, h1.topTarg );
w = h1.rAct - h1.lAct;
h = h1.botAct - h1.topAct;
BorderTileColor( h1.lAct, h1.topAct, w, h, r << 16 | g << 8 | b );
}
void Highlight( int inX, int inY, int inWidth, int inHeight ){
HighlightOffset( inX, inY, inWidth, inHeight, 0 );
}
// Basically "start a new draw call"
void DrawBG(){
StartDrawing();
BorderTileColor(
0, 0,
SCREEN_WIDTH,
SCREEN_HEIGHT * 2,
CLR_BACKGROUND
);
}
#pragma region logger
//
// Logger
//
//
// Logs a formatted message into a buffer, with optional line update ('\x08' prefix)
// ( vs FontPrint which seems to avoid the heap and transfer prims straight to VRAM/GPU )
//
// Pre-scan the input string and work out how many %params were passed in.
// Allows repeat buffer use and repeating/clearing individual lines
//
// notes: chars, shorts, etc are 'promoted' to 32bit so they fit one stack "slot"
// notes: strings are just a 32bit pointer
// notes: not tested with floats
// notes: trying to create a function prototype causes a nonworking binary
// - not sure it's worth the trouble debugging, so it's near the top of the file.
//
#define TEXT_COLS 64 // -12 inside border
#define TEXT_ROWS 32 // -8 inside border
#define LOG_LENGTH (TEXT_ROWS * TEXT_COLS)
#define LINE_LENGTH 100 // max per single log entry
#define MAX_ARGS 10 // See note on missing vprintf
// Some pointer abuse here 'cause I've been toying with putting the buffer on the stack...
char headerLogBuffer[ LOG_LENGTH ];
ulong logBase = 0; //
ulong logPos = 0; //
ulong lastLogPos = 0; // the start of the last line, for \r style line updates
int lastLineRepeatable = 0; // equivalent to \r
// Quickly adds a single char
// - does not check line wrapping
// - does not check buffer overflow
// - does not honor repeated line setups
// Indended for e.g. speeding up the hex editor rendering within known dimenstions
void BlahChar( char inChar ){
*(uchar*)logPos = inChar;
lastLogPos = logPos;
logPos += 1;
}
// Same deal
void BlahNewline(){
*(uchar*)logPos = 10;
lastLogPos = logPos;
logPos += 1;
}
// what, you think we were going to call it Log() after an intro like that?
void Blah( char* pMSG, ... ){
va_list vaList;
int offset = 0;
int var = 0;
int argsCounted = 0;
int originalLineLength = 0;
int generatedLineLength = 0;
int thisLineRepeatable = 0;
// A little extra wiggle room (e.g. 64 + CR + LF + \0 + mistakes )
// let's put 'em on the stack
//char tempBuffer[ 100 ];
ulong params[ MAX_ARGS ];
// Can point this at the tempbuffer to expand out % args...
// or if no args, just use it to read from the incoming message
char * lineBuffer;
// scan the input to work out how many args we passed in
while( pMSG[ offset ] != 0 && argsCounted < MAX_ARGS && offset < 200 ){
if ( pMSG[ offset ] == '%' ){
argsCounted++;
}
offset++;
}
originalLineLength = offset;
if ( argsCounted > 0 ){
char tempBuffer[100];
// pass the first arg before the ... param so the macro knows
// where to look in the stack
va_start( vaList, pMSG );
for( offset = 0; offset < argsCounted; offset++ ){
var = va_arg( vaList, ulong );
params[ offset ] = var;
}
// done with this
va_end( vaList );
// Clear the line buffer.
for( offset = 0; offset < LINE_LENGTH; offset++ ){
tempBuffer[ offset ] = 0;
}
if ( argsCounted < 4 ){
NewSPrintf( tempBuffer, pMSG, params[0], params[1], params[2] );
} else {
// If you're passing fewer than 10 params, it's not a problem as the formatter will just ignore them
// If you're passing more, then they'll be clipped. No problems.
NewSPrintf( tempBuffer, pMSG, params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9] );
}
// Get the generated length -1 (e.g. excluding the null terminator)
lineBuffer = tempBuffer;
while( lineBuffer[++generatedLineLength] != 0 ){};
} else {
// point the lineBuffer at this instead
lineBuffer = pMSG;
generatedLineLength = originalLineLength;
}
if ( lineBuffer[0] == '\x08' )
thisLineRepeatable = 1;
// Go back a line
if ( thisLineRepeatable && lastLineRepeatable ){
logPos = lastLogPos;
}
// we at risk of going over the buffer length? reset
// TODO: remove up to first newline or null terminator
if ( ( logPos + generatedLineLength ) >= ( logBase + LOG_LENGTH ) ){
ClearScreenText();
}
// Can't sprintf the formatted string into the buffer 'cause it'll add the null terminator
// just copy it in manually
for( offset = 0; offset < generatedLineLength; offset++ ){
*(uchar*)(offset+logPos) = lineBuffer[ offset ];
}
// record the where this line started... and where it ended
lastLogPos = logPos;
logPos += generatedLineLength;
lastLineRepeatable = thisLineRepeatable;
}
// Returns the address of the log buffer text
// TODO: this should be *sent* to the GPU to avoid circular references
ulong GetLogBuffer(){
logBase = (ulong)&headerLogBuffer;
return logBase;
}
// TODO: see above
ulong GetLogBufferEnd(){
return logPos;
}
// E.g. running sans GUI?
char bufferInitialised = 0;
void InitBuffer(){
ClearScreenText();
bufferInitialised = 1;
}
// No such issues prototyping this one...
void ClearScreenText(){
int i = 0;
GetLogBuffer();
logPos = logBase;
lastLogPos = logBase;
// A few null terminators will do.
// 12 seemed good.
for ( i = 0; i < 12; i++ ){
*(uchar*)(logBase+i) = 0;
}
}
#pragma endregion logger
// Shortcut for "draw the border, then the text, then vsync it"
void DBorder(){
if ( !bufferInitialised )
return;
DrawBG();
Draw();
}
// Holds a message till you hit X
void HoldMessage(){
MonitorPads();
DBorder();
while ( 1 ){
MonitorPads();
if ( Released( PADRdown ) ){
return;
}
}
}
void DrawQR( unsigned char * bytes, unsigned int dimensions, int xOffset, int yOffset, int xScale, int yScale ){
int row = 0;
int col = 0;
int numBits = dimensions * dimensions;
for( int i = 0; i < numBits; i++ ){
int x = xOffset + ( col * xScale );
int y = yOffset + ( row * yScale );
int whichByte = i / 8;
int whichBit = i % 8;
char mask = 1 << (7-whichBit);
char isBlack = (( bytes[ whichByte ] & mask ) != 0);
BorderTileColor( x, y, xScale, yScale, isBlack ? 0x000000 : 0xFFFFFF );
col++;
if ( col >= dimensions ){
col = 0;
row++;
}
}
}
void DrawGitQR() {
DrawQR( qrData_danhanspong,37,348,125,3,2);
};