I'm wondering how I can improve on the code I've been working on over the past couple days and I've been reading a lot but making little progress. I'm wondering how I can make more of a generic system for handling hardware sprites and meta sprites. My current code is pretty ugly and I'm using constants in NESASM to refer to the RAM buffer for my player sprite like this:
And then I've got a drawing function that is basically doing this:
There's a little bit more math in there to calculate the offsets for the other tiles for my player's meta sprite as well.
Now this all works just fine but it's not the way I would approach it if I were coding in something like C or C++. I'm having a hard time wrapping my head around doing any abstraction when I'm coding in assembly. I see some people talking about using arrays and tables and things like that but I'm not quite sure how those are implemented in 6502 assembly.
I've added my sprites and nametables to a section of memory starting at $E000, which I understand to be cartridge rom. Is this the space that is typically used to store things like arrays and tables? For example if I wanted to create some simple sprite animations, would a typical approach be to do something like this:
Because the 6502 has an address bus width of 16, does that mean that my animation "data structure" if you could call it that, is 7 bytes long? Sorry for these simple questions. The tutorials I'm finding range from incredibly simple to very complex with little in between. I just want to make sure that I'm getting this right.
Are further abstractions possible here in terms of data structures? Could I for example implement something like a C struct? What would that look like? Or is that specific to the assembler that I'm using?
What would be a very simple next step from this approach I've outlined above to something that uses a bit more abstraction and possibly a more generalized approach?
Code:
PLAYER_Y = $0200
PLAYER_TILE = $0201
PLAYER_ATTR = $0202
PLAYER_X = $0203
player_x .rs 1
player_y .rs 1
PLAYER_TILE = $0201
PLAYER_ATTR = $0202
PLAYER_X = $0203
player_x .rs 1
player_y .rs 1
And then I've got a drawing function that is basically doing this:
Code:
DrawPlayer:
LDA player_x
STA PLAYER_X
LDA player_y
STA PLAYER_Y
LDA player_x
STA PLAYER_X
LDA player_y
STA PLAYER_Y
There's a little bit more math in there to calculate the offsets for the other tiles for my player's meta sprite as well.
Now this all works just fine but it's not the way I would approach it if I were coding in something like C or C++. I'm having a hard time wrapping my head around doing any abstraction when I'm coding in assembly. I see some people talking about using arrays and tables and things like that but I'm not quite sure how those are implemented in 6502 assembly.
I've added my sprites and nametables to a section of memory starting at $E000, which I understand to be cartridge rom. Is this the space that is typically used to store things like arrays and tables? For example if I wanted to create some simple sprite animations, would a typical approach be to do something like this:
Code:
walk_anim_ptr .rs 1
Code:
walk_animation_array:
.db $03, walk1, walk2, walk3 ; length, 1, 2, 3
walk1:
.db $80, $32, $00, $80
.db $80, $33, $00, $88
.db $88, $34, $00, $80
.db $88, $35, $00, $88
walk2:
.db $80, $32, $00, $80
.db $80, $33, $00, $88
.db $88, $34, $00, $80
.db $88, $35, $00, $88
walk3:
.db $80, $32, $00, $80
.db $80, $33, $00, $88
.db $88, $34, $00, $80
.db $88, $35, $00, $88
.db $03, walk1, walk2, walk3 ; length, 1, 2, 3
walk1:
.db $80, $32, $00, $80
.db $80, $33, $00, $88
.db $88, $34, $00, $80
.db $88, $35, $00, $88
walk2:
.db $80, $32, $00, $80
.db $80, $33, $00, $88
.db $88, $34, $00, $80
.db $88, $35, $00, $88
walk3:
.db $80, $32, $00, $80
.db $80, $33, $00, $88
.db $88, $34, $00, $80
.db $88, $35, $00, $88
Because the 6502 has an address bus width of 16, does that mean that my animation "data structure" if you could call it that, is 7 bytes long? Sorry for these simple questions. The tutorials I'm finding range from incredibly simple to very complex with little in between. I just want to make sure that I'm getting this right.
Are further abstractions possible here in terms of data structures? Could I for example implement something like a C struct? What would that look like? Or is that specific to the assembler that I'm using?
What would be a very simple next step from this approach I've outlined above to something that uses a bit more abstraction and possibly a more generalized approach?