Writing a NES game day 10: Working with graphics
Tags: nes, assembly, 6502, blog
Published: 2023-07-22 12:23:11

Working with graphics for the NES can be a little... I won't say difficult, but it is not a walk in the park. As you have your chr file with tiles you have to accomodate them in the screen loading them into memory and using variables for the movement (and then again, the less variables, better for the future as we have to save all the possible memory)

Given the CHR file that I have, I made things like this for the first version of the title screen:

```
principal:
    .byte $1B,$12,$17,$0D,$0E,$15,$12,$0A,$24,$24,$24,$24,$24,$24,$24,$24 
    .byte $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 

    .byte $24,$24,$1D,$11,$0E,$24,$15,$0E,$10,$0E,$17,$0D,$24,$24,$24,$24 
    .byte $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 

    .byte $24,$24,$24,$24,$18,$0F,$24,$1D,$11,$0E,$24,$0F,$18,$1E,$1B,$24 
    .byte $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24  

    .byte $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$10 
    .byte $0E,$16,$1C,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 
```

It is not, but let's say it is kind of an array of hexadecimal numbers, each one corresponding to a tile in the chr.

After accomodating then in my "engine title zone":

```
enginetitle:
    LDA $2002
    LDA #$20
    STA $2006
    LDA #$00
    STA $2006
    LDX #00
:
    LDA principal, X
    STA $2007
    INX
    CPX #$80
    BNE :-

    LDA $2002
    LDA #$23
    STA $2006
    LDA #$C0
    STA $2006
    LDX #$00
    waitforkeypress:
        LDA button
        CMP #%00010000
        BNE waitforkeypress
    LDA #PLAYING
    STA gamestate
    JMP clearscreen
    
    JMP GAMEENGINEDONE

```

I made it appear as this (and as you can see in the waitforkeypress loop, it waits for start button).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfvviq5vi2jiplvnozzw.PNG)

I have to quit the render of the character from there, I put it in the vblank zone, have to see how to put it somewhere else, maybe a jump to see if it is in the presentation stage...

Remember from the first days that I had the character being load from spritedata? It is something similar to the backgrond, something like this:

```
spritedata:
    ;      Y   tile attr   X
    .byte $80, $00, $00, $10
    .byte $80, $01, $00, $18
    .byte $88, $10, $00, $10
    .byte $88, $11, $00, $18
```

Well, now I need it to have some variables to move it in the x/y axis, so I needed to create a couple of variables in the ZEROPAGE segment:

```
p1x: .res 1
p2x: .res 1
p1y: .res 1
p2y: .res 1
```

And create a function (sprites load from $0200)

```
updatesprites:
    ;I will have to do something to UPDATE ALL in the future
    LDA p1y
    STA $0200
    STA $0204
    LDA p2y
    STA $0208
    STA $020C
    LDA p1x
    STA $0203
    STA $020B
    LDA p2x
    STA $0207
    STA $020F
    LDA #$00 ;this is a tile, should have a variable? (also used for attributes now)
    STA $0201
    STA $0202
    STA $0206
    STA $020A
    STA $020E
    LDA #$01
    STA $0205
    LDA #$10
    STA $0209
    LDA #$11
    STA $020D
    
    CLI
    LDA #%10000000
    STA $2000

    LDA #%00010000
    STA $2001
    RTS
```

Maybe there are better ways to do this, but I am still learning.