Originally posted by: Mega Mario Man
Originally posted by: sempressimo
I am using the code in these tutorials to load a full screen background. It works nicely, now I wan to load another "room", (i.e. load the tiles again into the nametable). I repurposed the routine into a subrotine, but the second time I call it, the new "room" won't load properly, instead I get a garbage version of the room;
LoadBackground:
LDA $2002 ; read PPU status to reset the high/low latch
LDA #$20
STA $2006 ; write the high byte of $2000 address
LDA #$00
STA $2006 ; write the low byte of $2000 address
;LDX room_number ; grab the actual room pointer
LDA #$00
STA pointerLo ; put the low byte of the address of background into pointer
LDA #HIGH(Room1)
STA pointerHi ; put the high byte of the address into pointer
LDX #$00 ; start at pointer + 0
LDY #$00
OutsideLoop:
InsideLoop:
LDA [pointerLo], y ; copy one background byte from address in pointer plus Y
STA $2007 ; this runs 256 * 4 times
INY ; inside loop counter
CPY #$00
BNE InsideLoop ; run the inside loop 256 times before continuing down
INC pointerHi ; low byte went 0 to 256, so high byte needs to be changed now
INX
CPX #$04
BNE OutsideLoop ; run the outside loop 256 times before continuing down
LDA #%10010000 ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
STA $2000
LDA #%00011110 ; enable sprites, enable background, no clipping on left side
STA $2001
RTS
Can this code be used to re-load the background or I am missing something?
Also can anyone point me to a tutorial on how to show a main screen, and the load the game level, as it should be the same concept I am trying to get working.
Let your NMI enable your PPU. Don't do this in the main code. Doing it in the main code will make it look like garbage (what you are seeing).
LDA #%00011110 ; enable sprites, enable background, no clipping on left side
STA $2001
Here is what my NMI looks like:
LDA #%10010000 ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
STA $2000
LDA EnablePPUFlag ;Only Enable the PPU when this flag is enabled in the code.
BEQ .SKIP
LDA #%00011110 ;enable sprites, enable background, no clipping on left side
STA $2001
DEC EnablePPUFlag
.SKIP:
(This is a Subroutine in my main code. The NMI calls this when I tell it to with the EnablePPUFlag)
EnablePPU: ;<--------------------***********Subroutine called by the NMI to turn on the PPU
LDA #%00011110 ; enable sprites, enable background, no clipping on left side
STA $2001
RTS
-----------
This is what my code looks like.
LDA #$00 ;<-------------------------**********Turn off the PPU in the Main Code First
STA $2001
LDA #LOW(PlayingBackground) ;<-----------------**********Load background routine*************
STA BackgroundAddr
LDA #HIGH(PlayingBackground)
STA BackgroundAddr+1
JSR LoadBackground
LoadBackground:
LDA $2002
LDA #$20
STA $2006 ; write the high byte of $2000 address
LDA #$00
STA $2006 ; write the low byte of $2000 address
LDX #$04 ; Loop X 4 times
LDY #$00 ; Loop Y 256 times
LoadBackgroundsLoop:
LDA [BackgroundAddr],y ;Load background from table in the pointer
STA $2007 ;Write to screen
INY
BNE LoadBackgroundsLoop
; Outer loop
INC BackgroundAddr+1 ; increment high byte of address backg to next 256 byte chunk
DEX ; one chunk done so X = X - 1.
BNE LoadBackgroundsLoop ; if X isn't zero, do again
LoadBackGroundDone:
RTS
INC EnablePPUFlag ;<--------------********This flag will tell the NMI to turn PPU back on*******
---------------------------------------------------------
SIDE NOTE: You don't have to do anything with this code when turning the PPU on and off. That code only belongs in the PPU
LDA #%10010000 ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
STA $2000
Hope that made sense. Good luck
Thanks Mega Mario Man, now I have room switching working. I do get a quick black flash when switching, I know is because I am turning on and off the PPU, just wondering if this is the standard way or if there is a way around this?
Also, if I want to update just some background tiles, like the score for example, can I just update the value in my backgrounds .db(s) directly and that is it? Or does the PPU has to be turned off for this also?
]]>