Every good programming technique

Discussion of hardware and software development for Super NES and Super Famicom.

Moderator: Moderators

Forum rules
  • For making cartridges of your Super NES games, see Reproduction.
psycopathicteen
Posts: 3001
Joined: Wed May 19, 2010 6:12 pm

Every good programming technique

Post by psycopathicteen »

There's a lot of good programming advice scattered across this board, so I think I'll try to make a list of every programming technique here. If there are any contradictions in this list, that means it is something that should be discussed.

Here's some techniques that I personally recommend doing:
-Set up DMA channels for OAM and tile scrolling before vblank, so all that's left to do in vblank is writing to $420b.
-Place sprites offscreen by doing a DMA fill with the value $e0 to the remainder of OAM during vblank.
-Region based level object spawning

Here's some techniques that should be thought about before doing:
-Using linked lists on objects to avoid searching through object slots


Some techniques that sound good on paper, but I haven't tried yet:
-Using 10.6, 11.5 or 12.4 fixed point coordinates
Last edited by psycopathicteen on Mon Apr 18, 2016 3:57 pm, edited 2 times in total.
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Every good programming technique

Post by tepples »

12.4 is also a fine choice so long as your levels aren't more than 16 screens wide. It might speed up background collisions if you're using 16x16 pixel metatiles. But it might be a little slower to draw because of all the shifting to translate to a pixel value, and object widths suddenly become 2-byte values (and thus larger in the size lookup table).
psycopathicteen
Posts: 3001
Joined: Wed May 19, 2010 6:12 pm

Re: Every good programming technique

Post by psycopathicteen »

The only question is having several fireballs shot in a circle at once, might not look like a circle.
Stef
Posts: 259
Joined: Mon Jul 01, 2013 11:25 am

Re: Every good programming technique

Post by Stef »

I'm using 10.6 fixed point (signed) variable in SGDK, it's really convenient as you can manipulate it directly through 16 bits registers and it provides a good compromize between float part accuracy and integer part [-512..511]
Of course that is not always enough but for me it's definitely a good choice.
psycopathicteen
Posts: 3001
Joined: Wed May 19, 2010 6:12 pm

Re: Every good programming technique

Post by psycopathicteen »

Today I've been setting up a link list object system, and it's a lot more confusing than I thought it would be. I think I should only use a linked list for open slots, doing it for active slots I find unnecessary.
User avatar
rainwarrior
Posts: 8062
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Every good programming technique

Post by rainwarrior »

For open slots, instead of a linked list, maybe just don't start at 0 every time?

Like, just store the last used slot, and start your next search there (and loop around). That puts the newest objects at the end of your search, and the oldest objects at the beginning, so you're likely to hit an open slot on the first test. Worst case tests them all, but average case you're probably getting an open slot the first time almost every time.
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Every good programming technique

Post by tokumaru »

I do it for active slots so I can group objects. Update order is important for some object interactions (carrying, pushing, etc.) So I process object groups in the same order every time, catering to these dependencies. Another reason to group objects is to minimize collision tests, so each object only collides against groups of objects it's supposed to interact with. You can have a group fit enemies, another for bullets, and so on.

For composite objects I have a "link" field, that each part uses to connect to the next part. For a spider I'd probably have pointers (using the general purpose bytes) to all legs in the main object, and each leg section would link to the next.
rainwarrior wrote:For open slots, instead of a linked list, maybe just don't start at 0 every time?
Why? Linked lists are so cheap to maintain, you can remove or insert an item with like, 3 instructions.
psycopathicteen
Posts: 3001
Joined: Wed May 19, 2010 6:12 pm

Re: Every good programming technique

Post by psycopathicteen »

Why? Linked lists are so cheap to maintain, you can remove or insert an item with like, 3 instructions
Really? In order to clear an object I had to change the previous slot's "next" value, and the next slot's "previous" value, and change the "next" value to the "first open slot" value, and the "first open slot" value with the address of the slot being cleared.

Did I completely miss the point of linked lists?
User avatar
rainwarrior
Posts: 8062
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Every good programming technique

Post by rainwarrior »

tokumaru wrote:Why? Linked lists are so cheap to maintain, you can remove or insert an item with like, 3 instructions.
I don't see the point in debating it outside the context of a specific implementation?

I just wanted to offer another technique for "find an open slot to use" besides a linked list. I've found this one useful many times.
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Every good programming technique

Post by tokumaru »

psycopathicteen wrote:Did I completely miss the point of linked lists?
Ah, so you're using doubly-linked lists... I use singly-linked lists. Any reason in particular why you need to scan the lists in both directions?
psycopathicteen
Posts: 3001
Joined: Wed May 19, 2010 6:12 pm

Re: Every good programming technique

Post by psycopathicteen »

tokumaru wrote:
psycopathicteen wrote:Did I completely miss the point of linked lists?
Ah, so you're using doubly-linked lists... I use singly-linked lists. Any reason in particular why you need to scan the lists in both directions?
So the previous object can be linked up to the next object when clearing the current object.
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Every good programming technique

Post by tokumaru »

psycopathicteen wrote:So the previous object can be linked up to the next object when clearing the current object.
I felt the need to reference the previous object in a few cases too, but instead of complicating the linking system because of this one reason, I simply had each object save its index in a variable, that I can check when processing the next object.
psycopathicteen
Posts: 3001
Joined: Wed May 19, 2010 6:12 pm

Re: Every good programming technique

Post by psycopathicteen »

So when you're removing a multijointed boss, you'll replace the previous object's "next slot" variable with the "next slot" variable of it's last child object?, and also link the last child object to the beginning of the open list.
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Every good programming technique

Post by tokumaru »

I don't know the exact structure you're using for composite objects, but assuming you do process them in the order specified by the list, simply saving each object's index in a variable (a single global variable, overwritten by each object) as they're processed will allow every object to know which the one immediately before it is.
psycopathicteen
Posts: 3001
Joined: Wed May 19, 2010 6:12 pm

Re: Every good programming technique

Post by psycopathicteen »

I tried your suggestion of using a shared "previous" variable, but I ran into a problem. When there's two consecutive objects dying at once, the second object links the already dead first object to the next object, instead of linking the last active object to the next object.

I don't know what your doing, but I'm going to have the object handler detect blank slots, and do the link-list rearranging inside the object handler routine.
Post Reply