Skip to content

Code examples

TheLX5 edited this page Feb 21, 2021 · 5 revisions

This section includes several ways that you can interact with this system. More will be added later.


Contents


Using !dss_tile_buffer

This is basically what I like to call the "standard" way of interacting with a recently found ExGFX via find_and_queue_gfx. This is only valid for 16x16 tiles.

If you want to use a single tile, you can use the following approach:

   lda !dss_tile_buffer+$00    ; this will draw the first tile from the ExGFX file
   sta $6302,y
   lda !dss_tile_buffer+$06    ; this will draw the seventh tile from the ExGFX file
   sta $6306,y
   lda !dss_tile_buffer+$0F    ; this will draw the fifteenth tile from the ExGFX file
   sta $630A,y

If you're drawing tiles with a loop you could opt for this other approach:

   phx                      ; remember to preserve X if you're using it for other things!
   lda tile_indices,x       ; use a table which holds the index for each tile you want from the buffer
   tax                      ; transfer it to X, X is preferred because it supports long addressing
   lda !dss_tile_buffer,x   ; load the tile data from !dss_tile_buffer
   plx                      ; get back your previous X
   sta $6302,y              ; store tile number

Simple GFX routine

This is the most basic routine you can write with DSS, feel free to use it as a base or a way to study the structure required for your graphics routines.

graphics_routine:
   lda #$59                 ; find or queue ExGFX D59
   %FindAndQueueGFX()
   bcs .gfx_loaded
   rts                      ; don't draw gfx if ExGFX D59 isn't ready
.gfx_loaded
   %GetDrawInfo()
   lda $00
   sta $6300,y
   lda $01
   sta $6301,y
   lda !dss_tile_buffer+$00 ; load the first tile in the buffer
   sta $6302,y
   lda #$15                 ; remember to use the second page!
   sta $6303,y
   lda #$00
   ldy #$02
   jsl $01B7B3
   rts

Handling 8x8 tiles

The most viable way I've found to take care of 8x8 tiles is to multiply every tilemap index by 4 and have a small 4 byte table to get the offset of each 8x8 tile.

   phx                        ; preserve X just in case 
   ldx $02                    ; let's assume your animation index (or whatever you use to select tiles) is at $02
   lda tilemap,x              ; load the correct tile index to use
   pha                        ; preserve the tile index
   and #$03                   ; use the lower 2 bits to determine which tile to show inside of a 16x16 tile
   tax                        ; transfer that to X
   lda small_tile_offsets,x   ; load the correct tile number offset
   sta $0F                    ; save the the tile number offset
   pla                        ; recover tile index
   lsr #2                     ; divide it by 4, we don't need the lower 2 bits
   tax                        ; transfer it to X
   lda !dss_tile_buffer,x     ; select the desired 16x16 tile based on the math above
   ora $0F                    ; add the necessary offset to the 16x16 tile in case a 8x8 is supposed to be drawn
   sta $6302,y                ; store tile number
   plx                        ; recover X

tilemap:                      ; example of tilemap indexes
   db $04                     ; this will load the top left 8x8 tile inside of the second 16x16 tile (this is you load 16x16 tiles)
   db $02                     ; this will load the bottom left 8x8 tile INSIDE of the first 16x16 tile
   db $0F                     ; this will load the bottom right 8x8 tile INSIDE of the fourth 16x16 tile
   db $09                     ; this will load the top right 8x8 tile inside of the third 16x16 tile

small_tile_offsets:           ; this table holds the offsets to add to any 16x16 tile
   db $00,$01,$10,$11

Loading multiple ExGFX

Loading multiple ExGFX files is how the Chuck is handled in this project. The idea just works because !dss_tile_buffer is never cleared between calls to find_and_queue_gfx and it never modifies values that it doesn't require to modify.

.find_D90
   lda #$90
   jsl find_and_queue_gfx
   bcs .loaded_D90
   rts
.loaded_D90
   lda !dss_tile_buffer+$00   ; copy the first tile number to the fourth value of the buffer
   sta !dss_tile_buffer+$03
   lda !dss_tile_buffer+$01   ; copy the second tile number to the fifth value of the buffer
   sta !dss_tile_buffer+$04

.find_D91
   lda #$91                   ; find or queue ExGFX D91
   jsl find_and_queue_gfx
   bcs .loaded_D91
   rts                        ; return because ExGFX D91 isn't loaded yet
.loaded_D91
                              ; at this point !dss_tile_buffer should have the data of both ExGFX D90 and D91
                              ; which would be arranged like this:
                              ;    first tile: ExGFX D91
                              ;    second tile: ExGFX D91
                              ;    third tile: ExGFX D91
                              ;    fourth tile: ExGFX D90
                              ;    fifth tile: ExGFX D90
                              ; this is assuming ExGFX D91 is 3 tiles long and D90 is two tiles long
Clone this wiki locally