; inputs:
; b/c: y/x positions
map_set_visible_row:
- ld a, [player_viewradius]
- add a, a ; * 2 for complete view
- ld d, a ; loop counter
+ call map_load_view_range
@x_loop:
push de
push bc
; hl = tile
ld a, [hl]
- or a, TF0_VISIBLE
+ ; TODO: do not set uncovered flag here
+ ; once room uncovering is implemented
+ or a, TF0_VISIBLE | TF0_UNCOVERED
ld [hl], a
; next tile
dec d
jr nz, @x_loop REL
ret
+
+ ; loads the viewport starting position
+ ; returns:
+ ; b/c: y/x top left
+ ; d: view range (radius*2)
+map_load_view_start_pos:
+ ld a, [player_viewradius]
+ ld d, a ; d = viewradius value
+ ld a, [player+act_pos_y]
+ sub a, d
+ ld b, a ; = y start
+ ld a, [player+act_pos_x]
+ sub a, d
+ ld c, a ; = x start
+ ; loads the view range
+ ; returns:
+ ; d: view range (radius*2)
+map_load_view_range:
+ ld a, d
+ add a, a
+ ld d, a ; d = view radius * 2
+ ret
+
+
; sets up the visibility range for tiles around the player
; starts at player_y/x - viewport and runs marks every tile
; until player_y/x + viewport is reached
jr nz, @clear_loop REL
; now mark new tiles as visible
+ call map_load_view_start_pos
- ld a, [player_viewradius]
- ld d, a ; d = viewradius value
- ld a, [player+act_pos_y]
- sub a, d
- ld b, a ; = y start
-
- ld a, [player+act_pos_x]
- sub a, d
- ld c, a ; = x start
-
- ld a, d
- add a, a
- ld d, a ; d = view radius * 2
@y_loop:
push de
push bc
jr nz, @y_loop REL
ret
+
+
+ ; loads the render starting position
+ ; returns:
+ ; b/c: y/x top left with player centered
+map_load_render_start_pos:
+ ld a, [player+act_pos_y]
+ sub a, RENDER_BUF_H/2
+ ld b, a ; = y start
+
+ ld a, [player+act_pos_x]
+ sub a, RENDER_BUF_W/2
+ ld c, a ; = x start
+ ret
+
+ ; draws a row of tiles
+ ; based on the current input position
+ ; inputs:
+ ; hl: render buffer
+ ; bc: y/x position
+ ; returns:
+ ; hl: render buffer next row top left
+map_full_draw_row:
+ ld d, RENDER_BUF_W ; loop counter
+@x_loop:
+ push de
+ push bc
+
+ push hl
+ call map_get_tile
+ ld de, t_flags0
+
+ ld a, [hl] ; read tile type
+ add hl, de ; hl = flags
+
+ ld e, a ; a = tile type
+ ld d, 0 ; de = tile type offset
+
+
+ ; hl = tile flags
+ ld a, [hl] ; read flags
+ and a, TF0_UNCOVERED
+ jr z, @not_uncovered REL
+
+ ; it is uncovered
+ ; load tile
+ ld a, [hl] ; read flags again
+
+ ld hl, tile_gfx_table
+ add hl, de ; hl = tile
+
+ ; check if visible
+ and a, TF0_VISIBLE
+ ld a, [hl] ; a = tile
+ jr z, @is_visible REL
+ ; if not visible set 7th bit
+ and a, 0b10000000
+@is_visible:
+ jr @done REL
+@not_uncovered:
+ ld a, 0x7F ; not uncovered tile
+@done:
+
+ pop hl ; hl = render target
+ ld [hl+], a ; write tile id and ++
+
+ pop bc
+ pop de
+
+ ; next tile
+ inc c ; x++
+
+ dec d
+ jr nz, @x_loop REL
+ ret
; draws a full map copy into the current map view buffer
; draws the are around the players viewport
; render destination
ld hl, render_buffer
- ;
+ ; load start position into b/c
+ call map_load_render_start_pos
+ ld d, RENDER_BUF_H ; loop counter
+
+@y_loop:
+
+ push de
+ push bc
+ call map_full_draw_row
+ pop bc
+ pop de
+
+ ; go to next row
+ inc b ; y++
+
+ dec d ; count--
+ jr nz, @y_loop REL
@done:
; 4) go to render state