cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

font.s (3718B)


      1        .include        "global.s"
      2
      3        .title  "Font utilities"
      4        .module FontUtils
      5
      6        .globl  .memset_small
      7        .globl  _font_ibm
      8        .globl  __current_1bpp_colors, .fg_colour, .bg_colour
      9        .globl  _set_native_tile_data, _set_tile_1bpp_data
     10
     11        ; Structure offsets
     12        sfont_handle_sizeof     = 3
     13        sfont_handle_font       = 1
     14        sfont_handle_first_tile = 0
     15
     16        ; Encoding types - lower 2 bits of font
     17        FONT_256ENCODING        = 0
     18        FONT_128ENCODING        = 1
     19        FONT_NOENCODING         = 2
     20
     21        ; Other bits
     22        FONT_BCOMPRESSED        = 2
     23        
     24        ; Maximum number of fonts
     25        .MAX_FONTS              = 6
     26
     27        .area   _DATA
     28        ; The current font
     29font_current::
     30        .ds     sfont_handle_sizeof
     31        ; Cached copy of the first free tile
     32font_first_free_tile:
     33        .ds     1
     34        ; Table containing descriptors for all of the fonts
     35font_table:
     36        .ds     (sfont_handle_sizeof*.MAX_FONTS)
     37
     38        .area   _HOME
     39
     40; void vmemcpy (unsigned int dst, const void *src, unsigned int size) __z88dk_callee __preserves_regs(iyh,iyl);
     41_font_init::
     42        xor a
     43        ld (font_first_free_tile), a
     44
     45        ld hl, #font_table
     46        ld c, #(sfont_handle_sizeof*.MAX_FONTS)
     47        call .memset_small
     48
     49        ret
     50
     51_font_set::
     52        pop de
     53        pop hl
     54        push hl
     55        push de
     56
     57font_set::
     58        ld de, #font_current
     59        ld bc, #sfont_handle_sizeof
     60        ldir
     61        ret
     62
     63_font_color::
     64        pop de
     65        ex (sp), hl
     66        push de
     67        ld (__current_1bpp_colors), hl
     68        ret
     69
     70font_load_ibm::
     71        ld hl, #_font_ibm
     72        jp font_load
     73
     74_font_load::
     75        pop de
     76        pop hl
     77        push hl
     78        push de
     79
     80        ;; load font in HL to free slor
     81font_load::
     82        push hl
     83        ; find free slot
     84        ld b, #.MAX_FONTS
     85        ld hl, #(font_table+sfont_handle_font)
     861$:
     87        ld a, (hl)
     88        inc hl
     89        or (hl)
     90        jr z, 2$        ; found
     91        inc hl
     92        inc hl
     93        dec b
     94        jr nz, 1$
     95
     96        ld hl, #0       ; no free slot
     97        ret
     982$:
     99        ; fill free slot with passed pointer
    100        pop de
    101        ld (hl), d
    102        dec hl
    103        ld (hl), e
    104        ld a, (font_first_free_tile)
    105        dec hl
    106        ld (hl),a          
    107        
    108        push hl
    109        call font_set
    110
    111        call load_font_tiles
    112
    113        ld hl, (font_current+sfont_handle_font)
    114
    115        inc hl          ; Number of tiles used
    116        ld a, (font_first_free_tile)
    117        add (hl)
    118        ld (font_first_free_tile), a
    119
    120        pop hl          ; return handle in hl
    121        ret
    122
    123load_font_tiles:
    124        ld hl, (font_current+sfont_handle_font)
    125
    126        inc hl
    127        ld e, (hl)
    128        ld d, #0
    129        
    130        dec hl
    131        ld a, (hl)              ; a = flags
    132        push af
    133        and #3
    134
    135        ld bc, #128
    136        cp #FONT_128ENCODING    ; 0 for 256 char encoding table, 1 for 128 char
    137        jr z, 1$
    138
    139        ld bc, #0
    140        cp #FONT_NOENCODING
    141        jr z, 1$
    142
    143        ld bc, #256             ; Must be 256 element encoding
    1441$:
    145        inc hl
    146        inc hl                  ; Points to the start of the encoding table
    147        add hl, bc           
    148
    149        ld a,(font_current+sfont_handle_first_tile)
    150        ld c, a
    151        ld b, #0
    152
    153        pop af                  ; Recover flags
    154        bit FONT_BCOMPRESSED, a ; Is this font compressed?
    155        jp z, 2$
    156
    157        push hl
    158        ld hl, (__current_1bpp_colors)
    159        ex (sp), hl
    160        push hl
    161        push de
    162        push bc
    163        call _set_tile_1bpp_data
    164        ret 
    1652$:
    166        push hl
    167        push de
    168        push bc 
    169        call _set_native_tile_data
    170        ret