Skip to content

Game loop: some minor optimizations #2

@theNestruo

Description

@theNestruo

The Dispatcher

https://github.com/oisee/antique-toy/blob/main/chapters/ch18-gameloop/draft.md#the-dispatcher

The HL = game_state + A part can be optimized from 45T...

    ; Dispatch to current state handler (original)
    ld   a, (game_state)    ; 13T  load state index
    ld   l, a               ; 4T
    ld   h, 0               ; 7T
    ld   de, state_table    ; 10T
    add  hl, de             ; 11T  HL = state_table + offset

...to a slightly faster (42T) and slightly longer (1B):

    ; Dispatch to current state handler
    ld	hl, state_table
    ld	a, (game_state)
    add	l
    ld	l, a
    jr	nc, .else
    inc	h
.else:

Iterating Entities

https://github.com/oisee/antique-toy/blob/main/chapters/ch18-gameloop/draft.md#iterating-entities

    ; Check if entity is active
    ld   a, (ix + 9)        ; 19T  load flags byte (offset +9)
    bit  0, a               ; 8T   test ACTIVE flag
    jr   z, .skip           ; 12/7T skip if inactive

Can be rewritten as:

    ; Check if entity is active
    bit  0, (ix + 9)        ; 20T   test ACTIVE flag of flags byte (offset +9)
    jr   z, .skip           ; 12/7T skip if inactive

Saving 7T per entity (112T in total for 16 entities).

Update Dispatch by Type

https://github.com/oisee/antique-toy/blob/main/chapters/ch18-gameloop/draft.md#update-dispatch-by-type

Same optimization for HL = type_handlers + A as the one mentioned for The Dispatcher. Saves 3T per entity (48T in total for 16 entities).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions