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
|
; copy c bytes of data from de to hl
; if LCD on, copy during h-blank only
SafeCopyDataDEtoHL:
ld a, [wLCDC] ;
bit LCDC_ENABLE_F, a ;
jr nz, .lcd_on ; assert that LCD is on
.lcd_off_loop
ld a, [de]
inc de
ld [hli], a
dec c
jr nz, .lcd_off_loop
ret
.lcd_on
jp HblankCopyDataDEtoHL
; returns v*BGMap0 + BG_MAP_WIDTH * e + d in hl.
; used to map coordinates at de to a BGMap0 address.
DECoordToBGMap0Address:
ld l, e
ld h, $0
add hl, hl
add hl, hl
add hl, hl
add hl, hl
add hl, hl
ld a, l
add d
ld l, a
ld a, h
adc HIGH(v0BGMap0)
ld h, a
ret
; Apply SCX and SCY correction to xy coordinates at de
AdjustCoordinatesForBGScroll:
push af
ldh a, [hSCX]
rra
rra
rra
and $1f
add d
ld d, a
ldh a, [hSCY]
rra
rra
rra
and $1f
add e
ld e, a
pop af
ret
; Draws a bxc text box at de printing a name in the left side of the top border.
; The name's text id must be at hl when this function is called.
; Mostly used to print text boxes for talked-to NPCs, but occasionally used in duels as well.
DrawLabeledTextBox:
ld a, [wConsole]
cp CONSOLE_SGB
jr nz, .draw_textbox
ld a, [wTextBoxFrameType]
or a
jr z, .draw_textbox
; Console is SGB and frame type is != 0.
; The text box will be colorized so a SGB command needs to be sent as well
push de
push bc
call .draw_textbox
pop bc
pop de
jp ColorizeTextBoxSGB
.draw_textbox
push de
push bc
push hl
; top left tile of the box
ld hl, wc000
ld a, TX_SYMBOL
ld [hli], a
ld a, SYM_BOX_TOP_L
ld [hli], a
; white tile before the text
ld a, FW_SPACE
ld [hli], a
; text label
ld e, l
ld d, h
pop hl
call CopyText
ld hl, wc000 + 3
call GetTextLengthInTiles
ld l, e
ld h, d
; white tile after the text
ld a, TX_HALF2FULL
ld [hli], a
ld a, FW_SPACE
ld [hli], a
pop de
push de
ld a, d
sub b
sub $4
jr z, .draw_top_border_right_tile
ld b, a
.draw_top_border_line_loop
ld a, TX_SYMBOL
ld [hli], a
ld a, SYM_BOX_TOP
ld [hli], a
dec b
jr nz, .draw_top_border_line_loop
.draw_top_border_right_tile
ld a, TX_SYMBOL
ld [hli], a
ld a, SYM_BOX_TOP_R
ld [hli], a
ld [hl], TX_END
pop bc
pop de
push de
push bc
call InitTextPrinting
ld hl, wc000
call ProcessText
pop bc
pop de
ld a, [wConsole]
cp CONSOLE_CGB
jr z, .cgb
; DMG or SGB
inc e
call DECoordToBGMap0Address
; top border done, draw the rest of the text box
jr ContinueDrawingTextBoxDMGorSGB
.cgb
call DECoordToBGMap0Address
push de
call CopyCurrentLineAttrCGB ; BG Map attributes for current line, which is the top border
pop de
inc e
; top border done, draw the rest of the text box
jp ContinueDrawingTextBoxCGB
; Draws a bxc text box at de to print menu data in the overworld.
; Also used to print a text box during a duel.
; When talking to NPCs, DrawLabeledTextBox is used instead.
DrawRegularTextBox:
ld a, [wConsole]
cp CONSOLE_CGB
jr z, DrawRegularTextBoxCGB
cp CONSOLE_SGB
jp z, DrawRegularTextBoxSGB
; fallthrough
DrawRegularTextBoxDMG:
call DECoordToBGMap0Address
; top line (border) of the text box
ld a, SYM_BOX_TOP
lb de, SYM_BOX_TOP_L, SYM_BOX_TOP_R
call CopyLine
; fallthrough
; continue drawing a labeled or regular textbox on DMG or SGB:
; body and bottom line of either type of textbox
ContinueDrawingTextBoxDMGorSGB:
dec c
dec c
.draw_text_box_body_loop
ld a, SYM_SPACE
lb de, SYM_BOX_LEFT, SYM_BOX_RIGHT
call CopyLine
dec c
jr nz, .draw_text_box_body_loop
; bottom line (border) of the text box
ld a, SYM_BOX_BOTTOM
lb de, SYM_BOX_BTM_L, SYM_BOX_BTM_R
; fallthrough
; copies b bytes of data to sp-$1f and to hl, and returns hl += BG_MAP_WIDTH
; d = value of byte 0
; e = value of byte b
; a = value of bytes [1, b-1]
; b is supposed to be BG_MAP_WIDTH or smaller, else the stack would get corrupted
CopyLine:
add sp, -BG_MAP_WIDTH
push hl
push bc
ld hl, sp+$4
dec b
dec b
push hl
ld [hl], d
inc hl
.loop
ld [hli], a
dec b
jr nz, .loop
ld [hl], e
pop de
pop bc
pop hl
push hl
push bc
ld c, b
ld b, $0
call SafeCopyDataDEtoHL
pop bc
pop de
; advance pointer BG_MAP_WIDTH positions and restore stack pointer
ld hl, BG_MAP_WIDTH
add hl, de
add sp, BG_MAP_WIDTH
ret
; DrawRegularTextBox branches here on CGB console
DrawRegularTextBoxCGB:
call DECoordToBGMap0Address
; top line (border) of the text box
ld a, SYM_BOX_TOP
lb de, SYM_BOX_TOP_L, SYM_BOX_TOP_R
call CopyCurrentLineTilesAndAttrCGB
; fallthrough
; continue drawing a labeled or regular textbox on CGB:
; body and bottom line of either type of textbox
ContinueDrawingTextBoxCGB:
dec c
dec c
.draw_text_box_body_loop
ld a, SYM_SPACE
lb de, SYM_BOX_LEFT, SYM_BOX_RIGHT
push hl
call CopyLine
pop hl
call BankswitchVRAM1
ld a, [wTextBoxFrameType] ; on CGB, wTextBoxFrameType determines the palette and the other attributes
ld e, a
ld d, a
xor a
call CopyLine
call BankswitchVRAM0
dec c
jr nz, .draw_text_box_body_loop
; bottom line (border) of the text box
ld a, SYM_BOX_BOTTOM
lb de, SYM_BOX_BTM_L, SYM_BOX_BTM_R
call CopyCurrentLineTilesAndAttrCGB
ret
; d = id of top left tile
; e = id of top right tile
; a = id of rest of tiles
; Assumes b = SCREEN_WIDTH and that VRAM bank 0 is loaded
CopyCurrentLineTilesAndAttrCGB:
push hl
call CopyLine
pop hl
; fallthrough
CopyCurrentLineAttrCGB:
call BankswitchVRAM1
ld a, [wTextBoxFrameType] ; on CGB, wTextBoxFrameType determines the palette and the other attributes
ld e, a
ld d, a
call CopyLine
call BankswitchVRAM0
ret
; DrawRegularTextBox branches here on SGB console
DrawRegularTextBoxSGB:
push bc
push de
call DrawRegularTextBoxDMG
pop de
pop bc
ld a, [wTextBoxFrameType]
or a
ret z
; fallthrough
ColorizeTextBoxSGB:
push bc
push de
ld hl, wTempSGBPacket
ld de, AttrBlkPacket_TextBox
ld c, SGB_PACKET_SIZE
.copy_sgb_command_loop
ld a, [de]
inc de
ld [hli], a
dec c
jr nz, .copy_sgb_command_loop
pop de
pop bc
ld hl, wTempSGBPacket + 4
; set X1, Y1 to d, e
ld [hl], d
inc hl
ld [hl], e
inc hl
; set X2, Y2 to d+b-1, e+c-1
ld a, d
add b
dec a
ld [hli], a
ld a, e
add c
dec a
ld [hli], a
ld a, [wTextBoxFrameType]
and $80
jr z, .send_packet
; reset ATTR_BLK_CTRL_INSIDE if bit 7 of wTextBoxFrameType is set.
; appears to be irrelevant, as the inside of a textbox uses the white color,
; which is the same in all four SGB palettes.
ld a, ATTR_BLK_CTRL_LINE
ld [wTempSGBPacket + 2], a
.send_packet
ld hl, wTempSGBPacket
call SendSGB
ret
AttrBlkPacket_TextBox:
sgb ATTR_BLK, 1 ; sgb_command, length
db 1 ; number of data sets
; Control Code, Color Palette Designation, X1, Y1, X2, Y2
db ATTR_BLK_CTRL_INSIDE + ATTR_BLK_CTRL_LINE, 0 << 0 + 1 << 2, 0, 0, 0, 0 ; data set 1
ds 6 ; data set 2
ds 2 ; data set 3
|