Added music - screen flickers?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
mathe-matician
Posts: 14
Joined: Sat Aug 24, 2019 2:21 pm
Contact:

Added music - screen flickers?

Post by mathe-matician » Tue Apr 21, 2020 7:36 pm

Hello,

I added the nerdy nights sound engine with a few of my songs/sfx. As the game goes on there is a flicker that happens once and a while. It didn't happen during game play before I added the music. Does anyone have any suggestions on where/what to look at? On another side note, once and while another bug I have is that the timer/scoring gets messed up when I load in, especially after I'm already playing and reset/die/start a new round - but most of the time it functions just fine. Any help would be great!

Constructive criticism on this Frankenstein of a game would be great too :mrgreen:
Lumber.nes
(24.02 KiB) Downloaded 24 times

User avatar
gauauu
Posts: 696
Joined: Sat Jan 09, 2016 9:21 pm
Location: Central Illinois, USA
Contact:

Re: Added music - screen flickers?

Post by gauauu » Tue Apr 21, 2020 8:34 pm

I didn't spend much time on it, but it looks like something ran over the amount of cpu time you had. Either you called your music update subroutine during your NMI handler before doing PPU updates, so some of the PPU updates ran over vblank time, or you're taking too long on a frame in general, and ran over into the next frame. (I could be wrong, that's just what it looked like at first glance)

I didn't quite understand what was happening in the game well enough to give feedback on gameplay, but congrats making progress on your game. Keep at it.

User avatar
tokumaru
Posts: 11692
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Added music - screen flickers?

Post by tokumaru » Tue Apr 21, 2020 9:30 pm

I'm on my phone so I can't debug anything, but the Nerdy Nights tutorials do start out with a rather terrible program structure, where the game logic runs in the NMI *before* the PPU updates. This means that both the game logic AND the PPU updates have to fit inside the vblank time, which is less than 8% of the entire CPU time, while the remaining 92% of the time goes unused.

If that's how your program is structured (game logic first in the NMI, then PPU updates), then this is most likely your problem. A much better program structure would be the following:

Code: Select all

NMI:
  ;[update VRAM, OAM, etc. using data from the previous frame]
  ;[set the scroll via $2000 and $2005]
  ;- from this point on you don't need to be in vblank anymore -
  ;[call the sound engine]
  ;[read the controllers and update all of the game's state]
  RTI
If you're still getting flashes and jumps with a program structure like this, it probably means that your game logic is taking too long to complete, and a new NMI is firing before the previous one finished. In this case you need to create a variable that acts as a flag, that you set when the game logic is done to indicate to the next NMI handler that it can do everything normally. If this flag isn't set when the NMI fires, you should skip all PPU updates (except maybe for setting the scroll) and also the game logic. This is why games slow down when there are many enemies on screen at once - the CPU can't finish all the frame calculations in time, so the game engine has to delay the next frame.

Pokun
Posts: 1447
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Added music - screen flickers?

Post by Pokun » Tue Apr 21, 2020 11:39 pm

The first Nerdy Nights tutorial by Bunnyboy is doing that, but not the Nerdy Nights sound tutorial by Metalslime. The sound tutorial actually teaches you how to keep game logic and rendering separate, so this shouldn't be a problem if you do like Metalslime teaches you to. But yeah it sounds like OP is not doing this properly.
The game also seems to run at inconsistent speed, which makes me think you are not limiting the framerate by waiting for NMI at the end of the main loop.
I have no idea why the screen corrupts when restarting the game, make sure that everything that needs to be is reinitialized.

As for the game, I'm not sure what you are supposed to do? Chop up all the lumber? There doesn't seem to be nearly enough time to do that. Also graphics are very small. A lot of the things might be hard to see on a CRT TV. I'd rather use 16x16 metatiles and metasprites.

mathe-matician
Posts: 14
Joined: Sat Aug 24, 2019 2:21 pm
Contact:

Re: Added music - screen flickers?

Post by mathe-matician » Thu Apr 23, 2020 10:54 am

Thank you everyone for the responses! There were... many things wrong, mostly with how I originally structured my game - but your answers pointed me in the right direction.

I was running the sound engine after MOST of my drawing, but realized I was running some drawing after as well. I then realized I had a mix of game logic and drawing logic for my player/enemy attack, along with some things that controlled the score & timer in the wrong place.

I was using the bunnyboy SE - at the end of the SE he puts the note tables and songs - moving those to my buffer asm file instead of at the end of the SE was one of the steps to make it work for me (minus completely restructuring my game logic and drawing logic).

Thanks!

edit:
I also realize my noobness in using 8x8 tiles... not much else to say, everything is so small with a regular sized window haha I ended up making the fceux window larger. Definitely noted for my next project!

Not much else to the objective of the game besides chopping more lumber than the aliens to win.

Pokun
Posts: 1447
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Added music - screen flickers?

Post by Pokun » Thu Apr 23, 2020 12:23 pm

Ah well I also use 8x8 characters when I'm just testing things, since it requires much less code than to handle metasprites and things.

Oh so you are supposed to compete with the aliens. Then I guess I just cut too slowly and lost.

User avatar
gauauu
Posts: 696
Joined: Sat Jan 09, 2016 9:21 pm
Location: Central Illinois, USA
Contact:

Re: Added music - screen flickers?

Post by gauauu » Thu Apr 23, 2020 1:50 pm

mathe-matician wrote:
Thu Apr 23, 2020 10:54 am

I was running the sound engine after MOST of my drawing, but realized I was running some drawing after as well. I then realized I had a mix of game logic and drawing logic for my player/enemy attack, along with some things that controlled the score & timer in the wrong place.
There's never one ONE RIGHT WAY to do things, but in general, for many NES games, a structure like this works well:

- run all the game logic. (completely abstract with no concept of NES graphics)
- THEN buffer all the graphics updates (ie "render" character sprites to your OAM buffer, and prepare any background updates to some buffer)
- THEN wait for NMI/vblank.

When vblank happens:
- run the sprite OAM DMA update
- apply your background updates from the buffer
- set the scroll position and make sure that rendering is turned on.
- possibly run your music update here.

If you find a pattern like this and stick to it, it's a lot easier to make sure that you don't accidentally put graphical updates in the wrong place.

Post Reply