Hi guys,
I've recently started looking into programming for the NES. So far I've been playing around with sprites and nametables, just trying to get a feel for how it all works.
In one such experiment, I have the following two subroutines:
This works fine; when run, the program displays a Mario metasprite, with a Luigi metasprite underneath him.
My question is, how possible would it be to combine the create_mario and create_luigi subroutines into a single subroutine, using two variables (stored possibly in the stack/zero page/registers - wherever is best?) to define the sprite data and sprite RAM offsets?
Like in the following pseudocode:
Any thoughts? I reiterate that I am new to all of this, so apologies if this question is in any way idiotic!
(I'm using ca65, if this affects the answer)
I've recently started looking into programming for the NES. So far I've been playing around with sprites and nametables, just trying to get a feel for how it all works.
In one such experiment, I have the following two subroutines:
Code:
create_mario:
ldx #$00
@loop:
lda spriteData, x
sta $0200, x
inx
cpx #$10
bne @loop
rts
;;;;;;;;;;;;;;;;;;;;;;
create_luigi:
ldx #$00
@loop:
lda spriteData+16, x
sta $0200+16, x
inx
cpx #$10
bne @loop
rts
;;;;;;;;;;;;;;;;;;;;;;
spriteData:
.byte $80, $32, $00, $80 ; mario sprite 0
.byte $80, $33, $00, $88 ; mario sprite 1
.byte $88, $34, $00, $80 ; mario sprite 2
.byte $88, $35, $00, $88 ; mario sprite 3
.byte $90, $32, $01, $80 ; luigi sprite 0
.byte $90, $33, $01, $88 ; luigi sprite 1
.byte $98, $34, $01, $80 ; luigi sprite 2
.byte $98, $35, $01, $88 ; luigi sprite 3
ldx #$00
@loop:
lda spriteData, x
sta $0200, x
inx
cpx #$10
bne @loop
rts
;;;;;;;;;;;;;;;;;;;;;;
create_luigi:
ldx #$00
@loop:
lda spriteData+16, x
sta $0200+16, x
inx
cpx #$10
bne @loop
rts
;;;;;;;;;;;;;;;;;;;;;;
spriteData:
.byte $80, $32, $00, $80 ; mario sprite 0
.byte $80, $33, $00, $88 ; mario sprite 1
.byte $88, $34, $00, $80 ; mario sprite 2
.byte $88, $35, $00, $88 ; mario sprite 3
.byte $90, $32, $01, $80 ; luigi sprite 0
.byte $90, $33, $01, $88 ; luigi sprite 1
.byte $98, $34, $01, $80 ; luigi sprite 2
.byte $98, $35, $01, $88 ; luigi sprite 3
This works fine; when run, the program displays a Mario metasprite, with a Luigi metasprite underneath him.
My question is, how possible would it be to combine the create_mario and create_luigi subroutines into a single subroutine, using two variables (stored possibly in the stack/zero page/registers - wherever is best?) to define the sprite data and sprite RAM offsets?
Like in the following pseudocode:
Code:
create_sprite:
ldx #$00
@loop:
lda spriteData+[SPRITE_DATA_OFFSET], x
sta $0200+[SPRITE_RAM_OFFSET], x
inx
cpx #$10
bne @loop
rts
ldx #$00
@loop:
lda spriteData+[SPRITE_DATA_OFFSET], x
sta $0200+[SPRITE_RAM_OFFSET], x
inx
cpx #$10
bne @loop
rts
Any thoughts? I reiterate that I am new to all of this, so apologies if this question is in any way idiotic!
(I'm using ca65, if this affects the answer)