Skip to content

Commit

Permalink
MGTK: Add UnionRects call; saves 78 bytes in DeskTop, 110 in IconTK.
Browse files Browse the repository at this point in the history
No functional changes.
  • Loading branch information
inexorabletash committed Feb 3, 2025
1 parent 190fd6b commit 77daafd
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 88 deletions.
76 changes: 7 additions & 69 deletions desktop/main.s
Original file line number Diff line number Diff line change
Expand Up @@ -4695,11 +4695,8 @@ _Preamble:

;; Make `ubox` bound both viewport and icons; needed to ensure
;; offset cases are handled.
MGTK_CALL MGTK::UnionRects, unionrects_viewport_iconbb
COPY_STRUCT MGTK::Rect, iconbb_rect, ubox
copy16 #viewport, $06
copy16 #ubox, $08
jsr UnionRects

rts

;;; --------------------------------------------------
Expand Down Expand Up @@ -5027,57 +5024,6 @@ ScrollUpdate := ScrollManager::ActivateCtlsSetThumbs

;;; ============================================================

;;; Inputs: $06 = `rect1_ptr`, $08 = `rect2_ptr`
;;; Output: rect2 grown to encompass rect1; $06/$08 modified
.proc UnionRects
rect1_ptr := $06
rect2_ptr := $08

ldy #0
jsr do_pair
ldy #2
FALL_THROUGH_TO do_pair

;; Process a pair (e.g. x1 and x2, or y1 and y2)
do_pair:
;; left or top
jsr compare
bpl :+
jsr assign
: iny
iny
iny

;; right or bottom
jsr compare
bmi :+
jsr assign
:
rts

compare:
lda (rect1_ptr),y
cmp (rect2_ptr),y
iny
lda (rect1_ptr),y
sbc (rect2_ptr),y
bvc :+ ; signed compare
eor #$80
: rts

assign:
dey
lda (rect1_ptr),y
sta (rect2_ptr),y
iny
lda (rect1_ptr),y
sta (rect2_ptr),y
rts

.endproc ; UnionRects

;;; ============================================================

.proc CmdCheckDrives
copy #0, pending_alert
jsr CmdCloseAll
Expand Down Expand Up @@ -8226,22 +8172,14 @@ more: lda cached_window_entry_list,x
sub16 tmp_rect::y2, #kMaxIconTotalHeight, tmp_rect::y1
END_IF

;; First icon (index 0) - just use its coordinates as min/max
lda icon_num
bne compare

IF_ZERO
;; First icon (index 0) - just use its coordinates as min/max
COPY_STRUCT MGTK::Rect, tmp_rect, iconbb_rect
jmp next

;; --------------------------------------------------
;; Compare X coords

compare:
copy16 #tmp_rect, $06
copy16 #iconbb_rect, $08
jsr UnionRects

;; --------------------------------------------------
ELSE
;; Expand bounding rect to encompass icon's rect
MGTK_CALL MGTK::UnionRects, unionrects_tmp_iconbb
END_IF

next: inc icon_num
jmp check_icon
Expand Down
13 changes: 12 additions & 1 deletion desktop/res.s
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,22 @@ addr: .addr 0
;;; Used by DeskTop to compute bounding box for icons
DEFINE_RECT iconbb_rect, 0, 0, 0, 0

;;; Used by DeskTop to pad `iconbb_rect`
;;; Used by DeskTop to pad/expand `iconbb_rect`
.params bbox_pad_iconbb_rect
.addr iconbb_rect
.word kIconBBoxPaddingX, kIconBBoxPaddingY
.endparams
.params unionrects_tmp_iconbb
.addr tmp_rect
.addr iconbb_rect
.endparams

;;; Used by DeskTop to expand `iconbb_rect` in `ScrollManager`
.params unionrects_viewport_iconbb
.addr window_grafport::maprect
.addr iconbb_rect
.endparams


;;; ============================================================
;;; Alerts
Expand Down
11 changes: 10 additions & 1 deletion mgtk/MGTK.md
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ Rect rect
This must only be used following a `SaveScreenRect` call and must be
passed the same rectangle dimensions. No error checking is done.

#### InflateRect ($57)
#### InflateRect ($58)
Expand the referenced rectangle by `xdelta` and `ydelta`, which can be negative.

Parameters:
Expand All @@ -1134,6 +1134,15 @@ Parameters:
.word ydelta pixels
```

#### UnionRects ($59)
Expand the second rectangle to encompass the first rectangle.

Parameters:
```
.addr rect1 Address of MGTK::Rect. No modified
.addr rect2 Address of MGTK::Rect. Expanded if needed.
```

# Creating Applications and DeskTop Desk Accessories

### Application Use
Expand Down
4 changes: 4 additions & 0 deletions mgtk/mgtk.inc
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,10 @@ InflateRect = $58
;;; .word xdelta
;;; .word ydelta

UnionRects = $59
;;; .addr rect1 Address of MGTK::Rect
;;; .addr rect2 Address of MGTK::Rect

;;; ============================================================
;;; Graphics Primitives Constants

Expand Down
53 changes: 53 additions & 0 deletions mgtk/mgtk.s
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ jump_table:
.addr SaveScreenRectImpl ; $56 SaveScreenRect
.addr RestoreScreenRectImpl ; $57 RestoreScreenRect
.addr InflateRectImpl ; $58 InflateRect
.addr UnionRectsImpl ; $59 UnionRects

;; Entry point param lengths
;; (length, ZP destination, hide cursor flag)
Expand Down Expand Up @@ -504,6 +505,7 @@ param_lengths:
PARAM_DEFN 8, $92, 1 ; $56 SaveScreenRect
PARAM_DEFN 8, $92, 1 ; $57 RestoreScreenRect
PARAM_DEFN 6, $82, 0 ; $58 InflateRect
PARAM_DEFN 4, $82, 0 ; $59 UnionRectsImpl

;;; ============================================================
;;; Pre-Shift Tables
Expand Down Expand Up @@ -10650,6 +10652,57 @@ ydelta .word

;;; ============================================================

.proc UnionRectsImpl
PARAM_BLOCK, $82
rect1_ptr .addr
rect2_ptr .addr
END_PARAM_BLOCK

ldy #0
jsr do_pair
ldy #2
FALL_THROUGH_TO do_pair

;; Process a pair (e.g. x1 and x2, or y1 and y2)
do_pair:
;; left or top
jsr compare
bpl :+
jsr assign
: iny
iny
iny

;; right or bottom
jsr compare
bmi :+
jsr assign
:
rts

compare:
lda (rect1_ptr),y
cmp (rect2_ptr),y
iny
lda (rect1_ptr),y
sbc (rect2_ptr),y
bvc :+ ; signed compare
eor #$80
: rts

assign:
dey
lda (rect1_ptr),y
sta (rect2_ptr),y
iny
lda (rect1_ptr),y
sta (rect2_ptr),y
rts

.endproc ; UnionRectsImpl

;;; ============================================================

.endscope ; mgtk

;; Room for future expansion
Expand Down
23 changes: 6 additions & 17 deletions toolkits/icontk.s
Original file line number Diff line number Diff line change
Expand Up @@ -1696,28 +1696,17 @@ stash_rename_rect:
COPY_STRUCT MGTK::Rect, bitmap_rect, bounding_rect

;; Union of rectangles (expand `bounding_rect` to encompass `label_rect`)
scmp16 label_rect::x1, bounding_rect::x1
IF_NEG
copy16 label_rect::x1, bounding_rect::x1
END_IF
scmp16 label_rect::y1, bounding_rect::y1
IF_NEG
copy16 label_rect::y1, bounding_rect::y1
END_IF

scmp16 bounding_rect::x2, label_rect::x2
IF_NEG
copy16 label_rect::x2, bounding_rect::x2
END_IF
scmp16 bounding_rect::y2, label_rect::y2
IF_NEG
copy16 label_rect::y2, bounding_rect::y2
END_IF
MGTK_CALL MGTK::UnionRects, unionrects_label_bounding

jsr PopPointers ; do not tail-call optimise!
rts
.endproc ; CalcIconBoundingRect

.params unionrects_label_bounding
.addr label_rect
.addr bounding_rect
.endparams

kIconPolySize = (8 * .sizeof(MGTK::Point)) + 2

;;; Input: $06 points at icon
Expand Down

0 comments on commit 77daafd

Please sign in to comment.