Hello! So i am an amateur game programmer that started with C++ and SFML and now is trying to learn how to do games for the NES. Up until now i have been using the Nerdy Nights tutorials to learn how to do stuff and now was trying to do collision and platformer physics, the following is my code just to make the character go left or right.
It is working perfectly, but i have seen somewhere that it's a good thing to separate game logic from the NMI, so i moved my control handling code to after the forever label
But then the changes brought with it two problems, the first one is that the sprites move way too fast(when i programmed games with SFML i used a delta time to move stuff according to the framerate, since there is no floating point numbers here i don't really know what to do) and the second is that it seems the control reading is bugged since the sprite starts moving without any input.
tl;dr: How to properly separate my game logic from the NMI?
Code:
.proc nmi_handler
LDA #$00
STA $2003
LDA #$02
STA $4014
LDA #$01
STA $4016
LDA #$00
STA $4016
;Reads every button press and do some shift magic to put everything in a variable(controle)
LDX #$00
LoopLendoControle:
LDA $4016
LSR A
ROL controle
INX
CPX #$08
BNE LoopLendoControle
;Checks if right button is pressed, if so move every sprite to the right
LDA controle
AND #%00000001
BEQ fimDireita
LDX #$00
@LoopSprites:
;$0203 as it is the first x position for the sprites.
INC $0203, x
INX
INX
INX
INX
CPX #$10
BNE @LoopSprites
fimDireita:
;Checks if left button is pressed, if so move every sprite to the left
LDA controle
AND #%00000010
BEQ fimEsquerda
LDX #$00
@LoopSprites:
DEC $0203, x
INX
INX
INX
INX
CPX #$10
BNE @LoopSprites
fimEsquerda:
RTI
.endproc
.proc main
LDX $2002
LDX #$3f
STX $2006
LDX #$00
STX $2006
JSR CarregarPaleta
JSR CarregarSprites
JSR CarregarBackground
LDA #%10010000
STA $2000
LDA #%00011110
STA $2001
forever:
JMP forever
.endproc
LDA #$00
STA $2003
LDA #$02
STA $4014
LDA #$01
STA $4016
LDA #$00
STA $4016
;Reads every button press and do some shift magic to put everything in a variable(controle)
LDX #$00
LoopLendoControle:
LDA $4016
LSR A
ROL controle
INX
CPX #$08
BNE LoopLendoControle
;Checks if right button is pressed, if so move every sprite to the right
LDA controle
AND #%00000001
BEQ fimDireita
LDX #$00
@LoopSprites:
;$0203 as it is the first x position for the sprites.
INC $0203, x
INX
INX
INX
INX
CPX #$10
BNE @LoopSprites
fimDireita:
;Checks if left button is pressed, if so move every sprite to the left
LDA controle
AND #%00000010
BEQ fimEsquerda
LDX #$00
@LoopSprites:
DEC $0203, x
INX
INX
INX
INX
CPX #$10
BNE @LoopSprites
fimEsquerda:
RTI
.endproc
.proc main
LDX $2002
LDX #$3f
STX $2006
LDX #$00
STX $2006
JSR CarregarPaleta
JSR CarregarSprites
JSR CarregarBackground
LDA #%10010000
STA $2000
LDA #%00011110
STA $2001
forever:
JMP forever
.endproc
It is working perfectly, but i have seen somewhere that it's a good thing to separate game logic from the NMI, so i moved my control handling code to after the forever label
Code:
.proc nmi_handler
LDA #$00
STA $2003
LDA #$02
STA $4014
;Here used to be the game logic, now it is after the 'forever' label.
RTI
.endproc
.proc main
LDX $2002
LDX #$3f
STX $2006
LDX #$00
STX $2006
JSR CarregarPaleta
JSR CarregarSprites
JSR CarregarBackground
LDA #%10010000
STA $2000
LDA #%00011110
STA $2001
forever:
;Same code as before
LDA #$01
STA $4016
LDA #$00
STA $4016
LDX #$00
LoopLendoControle:
LDA $4016
LSR A
ROL controle
INX
CPX #$08
BNE LoopLendoControle
LDA controle
AND #%00000001
BEQ fimDireita
LDX #$00
@LoopSprites:
INC $0203, x
INX
INX
INX
INX
CPX #$10
BNE @LoopSprites
fimDireita:
LDA controle
AND #%00000010
BEQ fimEsquerda
LDX #$00
@LoopSprites:
DEC $0203, x
INX
INX
INX
INX
CPX #$10
BNE @LoopSprites
fimEsquerda:
JMP forever
.endproc
LDA #$00
STA $2003
LDA #$02
STA $4014
;Here used to be the game logic, now it is after the 'forever' label.
RTI
.endproc
.proc main
LDX $2002
LDX #$3f
STX $2006
LDX #$00
STX $2006
JSR CarregarPaleta
JSR CarregarSprites
JSR CarregarBackground
LDA #%10010000
STA $2000
LDA #%00011110
STA $2001
forever:
;Same code as before
LDA #$01
STA $4016
LDA #$00
STA $4016
LDX #$00
LoopLendoControle:
LDA $4016
LSR A
ROL controle
INX
CPX #$08
BNE LoopLendoControle
LDA controle
AND #%00000001
BEQ fimDireita
LDX #$00
@LoopSprites:
INC $0203, x
INX
INX
INX
INX
CPX #$10
BNE @LoopSprites
fimDireita:
LDA controle
AND #%00000010
BEQ fimEsquerda
LDX #$00
@LoopSprites:
DEC $0203, x
INX
INX
INX
INX
CPX #$10
BNE @LoopSprites
fimEsquerda:
JMP forever
.endproc
But then the changes brought with it two problems, the first one is that the sprites move way too fast(when i programmed games with SFML i used a delta time to move stuff according to the framerate, since there is no floating point numbers here i don't really know what to do) and the second is that it seems the control reading is bugged since the sprite starts moving without any input.
tl;dr: How to properly separate my game logic from the NMI?