Skip to content

Commit 71e1921

Browse files
committed
Optimize and improve behavior of gfx_PrintInt and gfx_PrintUInt
Rather than the length argument specifying the exact number of digits to print, it now specifies the minimum number of characters to print. This results in the following changes in behavior: * If the number to be printed would exceed the specified length, it is still printed in full. Previously, the calculation of the first digit would overflow instead and print a garbage character, possibly taking a long time to do so. * For gfx_PrintInt, the minus sign for negative numbers counts toward the length. This has the effect that positive and negative values that don't exceed the specified length will be padded to the same total length, including the minus sign. Implements #153.
1 parent 00f38a1 commit 71e1921

File tree

2 files changed

+70
-64
lines changed

2 files changed

+70
-64
lines changed

src/graphx/graphx.asm

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3012,7 +3012,7 @@ _DrawCharacters:
30123012
or a,a
30133013
ret z
30143014
call _PrintChar
3015-
PrintChar_3 = $-3
3015+
PrintChar_2 = $-3
30163016
inc hl ; move to the next one
30173017
jr _DrawCharacters
30183018

@@ -3085,8 +3085,6 @@ gfx_SetTextConfig:
30853085
ld hl,PrintChar_1
30863086
ld (hl),de
30873087
ld hl,PrintChar_2
3088-
ld (hl),de
3089-
ld hl,PrintChar_3
30903088
ld (hl),de ; modify all the interal routines to use the clipped text routine
30913089
push bc
30923090
pop hl
@@ -3298,79 +3296,87 @@ smcByte _TextHeight
32983296
pop hl ; restore hl and stack pointer
32993297
ret
33003298

3299+
;-------------------------------------------------------------------------------
3300+
gfx_PrintInt:
3301+
; Places an int at the current cursor position
3302+
; Arguments:
3303+
; arg0 : Number to print
3304+
; arg1 : Number of characters to print
3305+
; Returns:
3306+
; None
3307+
pop de
3308+
pop hl
3309+
push hl
3310+
push de
3311+
add hl,hl
3312+
db $3E ; xor a,a -> ld a,*
3313+
33013314
;-------------------------------------------------------------------------------
33023315
gfx_PrintUInt:
33033316
; Places an unsigned int at the current cursor position
33043317
; Arguments:
33053318
; arg0 : Number to print
3306-
; arg1 : Number of characters to print
3319+
; arg1 : Minimum number of characters to print
33073320
; Returns:
33083321
; None
3309-
ld iy,0
3310-
add iy,sp
3311-
ld hl,(iy+3) ; hl = uint
3312-
ld c,(iy+6) ; c = num chars
3313-
_PrintUInt:
3314-
ld a,8
3315-
sub a,c
3316-
ret c ; make sure less than 8
3317-
rla
3318-
rla
3319-
rla
3320-
ld (.offset),a ; select the jump we need
3321-
jr $
3322-
.offset = $-1
3323-
ld bc,-10000000
3322+
xor a,a
3323+
pop de
3324+
pop hl ; hl = uint
3325+
pop bc ; c = min num chars
3326+
push bc
3327+
push hl
3328+
push de
3329+
jr nc,.begin ; c ==> actually a negative int
3330+
ex de,hl
3331+
or a,a
3332+
sbc hl,hl
3333+
sbc hl,de ; hl = -int
3334+
ld a,'-'
3335+
call .printchar
3336+
dec c
3337+
jr nz,.begin
3338+
inc c
3339+
.begin:
3340+
ld de,-10000000
33243341
call .num1
3325-
ld bc,-1000000
3342+
ld de,-1000000
33263343
call .num1
3327-
ld bc,-100000
3344+
ld de,-100000
33283345
call .num1
3329-
ld bc,-10000
3346+
ld de,-10000
33303347
call .num1
3331-
ld bc,-1000
3348+
ld de,-1000
33323349
call .num1
3333-
ld bc,-100
3350+
ld de,-100
33343351
call .num1
3335-
ld bc,-10
3352+
ld de,-10
33363353
call .num1
3337-
ld bc,-1
3354+
ld de,-1
33383355
.num1:
3339-
ld a,'0'-1
3356+
xor a,a
33403357
.num2:
33413358
inc a
3342-
add hl,bc
3359+
add hl,de
33433360
jr c,.num2
3344-
sbc hl,bc
3345-
jp _PrintChar ; print the character needed
3346-
PrintChar_1 := $-3
3347-
3348-
;-------------------------------------------------------------------------------
3349-
gfx_PrintInt:
3350-
; Places an int at the current cursor position
3351-
; Arguments:
3352-
; arg0 : Number to print
3353-
; arg1 : Number of characters to print
3354-
; Returns:
3355-
; None
3356-
ld iy,0
3357-
lea bc,iy
3358-
add iy,sp
3359-
ld c,(iy+6) ; c = num chars
3360-
ld hl,(iy+3) ; hl = int
3361-
bit 7,(iy+5) ; if negative
3362-
jr z,.positive
3361+
sbc hl,de
3362+
dec a ; a = next digit
3363+
jr nz,.printdigit ; z ==> digit is zero, maybe don't print
3364+
ld a,c
3365+
inc c
3366+
cp a,8
3367+
ret c ; nc ==> a digit has already been
3368+
; printed, or must start printing
3369+
; to satisfy min num chars
3370+
xor a,a
3371+
.printdigit:
3372+
add a,'0'
3373+
ld c,a ; mark that a digit has been printed
3374+
.printchar:
33633375
push bc
3364-
push hl
3365-
pop bc
3366-
sbc hl,hl
3367-
sbc hl,bc
3368-
ld a,'-'
3369-
call _PrintChar ; place negative symbol
3370-
PrintChar_2 := $-3
3376+
call _PrintChar
3377+
PrintChar_1 := $-3
33713378
pop bc
3372-
.positive:
3373-
jp _PrintUInt ; handle integer
3379+
ret
33743380

33753381
;-------------------------------------------------------------------------------
33763382
gfx_GetStringWidth:

src/graphx/graphx.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -846,24 +846,24 @@ void gfx_PrintChar(const char c);
846846
/**
847847
* Prints a signed integer
848848
*
849-
* Outputs at the current cursor position.
849+
* Outputs at the current cursor position. Padded with leading zeros if
850+
* necessary to satisfy the specified minimum length.
850851
* @param n Integer to print
851-
* @param length Length of integer character display
852+
* @param length Minimum number of characters to print
852853
* @note By default, no text clipping is performed (configurable with gfx_SetTextConfig)
853-
* @note Values range from -8388608 to 8388607
854-
* @note Length must be between 0-8
854+
* @note \c length must be between 1 and 8, inclusive
855855
*/
856856
void gfx_PrintInt(int n, uint8_t length);
857857

858858
/**
859859
* Prints an unsigned integer
860860
*
861-
* Outputs at the current cursor position.
861+
* Outputs at the current cursor position. Padded with leading zeros if
862+
* necessary to satisfy the specified minimum length.
862863
* @param n Unsigned integer to print
863-
* @param length Length of unsigned integer character display
864+
* @param length Minimum number of characters to print
864865
* @note By default, no text clipping is performed (configurable with gfx_SetTextConfig)
865-
* @note Values range from 0-16777215
866-
* @note Length must be between 0-8
866+
* @note \c length must be between 1 and 8, inclusive
867867
*/
868868
void gfx_PrintUInt(unsigned int n, uint8_t length);
869869

0 commit comments

Comments
 (0)