OS Game Techniques

© Copyright 1992-93 Commodore-Amiga, Inc. All Rights Reserved
(Source : amiga developer CD V1.1)

Reasons to use the OS for games:
Things the OS can't currently do: All these are planned to be addressed in future OS releases. One of our goals is to make it possible to perform as many amiga tricks in normal intuition screens as is possible.

ECS-AA incompatibilities that the OS handles:
Future envisioned chip changes that the OS will handle correctly:
Game programming problems and solutions:
Q:	What is the graphics rendering routines are much slower than my own
	blitter code?

A:	Use the blitter yourself. Call OwnBlitter, do setup, call WaitBlit(),
	poke the blitter registers, and then DisownBlitter() when all blits are
	done.

	Note: OwnBlitter() is only 3 instructions (counting rts) when no-one
	else is trying to use the blitter.



Q:	What if input.device eats too many cycles?

A:	Install a high priority input handler which chokes off all events. This
	handler is also a convenient way to get keys and mouse events yourself.
	Simply store the raw keypresses and mouse moves in your own variables.

Q:	How do I change both bitmap pointers and colors in sync?

A:	Use a user-copper list to cause a copper interrupt on line 0 of your
	viewport. The copper interrupt handler will signal a high-priority
	task which calls LoadRGB32 and ChangeVPBitMap (or ScrollVPort) to
	cause the changes. This allows perfect 60hz animation on an A1200,
	even while moving the mouse as fast as possible, and inserting floppy
	disks.

	Under 3.0, you can also do this in an exclusive screen. You can
	tell if it was your screen which caused the copper interrupt by
	checking the flag VP_HIDE in your ViewPort->Modes.

	
Q:	I need to use the blitter in an interrupt driven manner instead of
	polling it for completion. Aren't the QBlit routines too slow?

A:	The QBlit/QBSBlit system was completely re-written for 3.0, and now
	has quite low overhead.

Q:	How do I determine elapsed time in my game?

A:	A simple, low overhead way to determine elapsed time is to call ReadEClock.
	This returns a 64 bit timer value which counts E Clocks, and returns how
	many EClocks happen per second. If you use these results properly,
	you can ensure that your game runs at the proper speed regardless of
	CPU type, chip speed, or PAL/NTSC clocking.

A1200 speed issues:

The A1200 has a fairly large number of wait-states when accessing chip-ram. ROM is zero wait-states. Due to the slow RAM speed, it may be better to use calculations for some things that you might have used tables for on the A500.

Add-on RAM will probably be faster than chip-ram, so it is worth segmenting your game so that parts of it can go into fast-ram if available.

For good performance, it is critical that you code your important loops to execute entirely from the on-chip 256-byte cache. A straight line loop 258 bytes long will execute far slower than a 254 byte one.

The '020 is a 32 bit chip. Longword accesses will be twice as fast when they are aligned on a long-word boundary. Aligning the entry points of routines on 32 bit boundaries can help, also. You should also make sure that the stack is always long-word aligned.

Write-accesses to chip-ram incur wait-states. However, other processor instructions can execute while results are being written to memory:

	move.l	d0,(a0)+	; store x coordinate
	move.l	d1,(a0)+	; store y coordinate
	add.l	d2,d0		; x+=deltax
	add.l	d3,d1		; y+=deltay

	will be slower than:

	move.l	d0,(a0)+	; store x coordinate
	add.l	d2,d0		; x+=deltax
	move.l	d1,(a0)+	; store y coordinate
	add.l	d3,d1		; y+=deltay

The 68020 adds a number of enhancements to the 68000 architecture, including new addressing modes and instructions. Some of these are unconditional speedups, while others only sometimes help:

Adressing modes:
New instructions:
Hardware resources:
  1. Blitter.
    Use OwnBlitter()/DisownBlitter() to claim and relinquish ownership of the blitter. YOU MUST USE THE GRAPHICS.LIBRARY WAITBLIT(). This is as fast as possible, uses no CPU registers, and knows about blitter bugs. You cannot possibly write one that is more efficient and works on all Amigas.
  2. Copper.
    If you really have to take over the copper, get the LoadView(NULL), do 2 WaitTOF()s, and then install your own copperlists in the cop1/2jmp registers. I do not recommend this though. Future chipsets may have faster and more efficient coppers with 32 bits, and we will want to use these. If you load the old copper registers behind graphics' back, we have no way of switching back to the old 16-bit mode.
    
    	temp=GfxBase->ActiView;
    	LoadView(NULL);
    	WaitTOF();
    	WaitTOF();
    	/* custom.cop1lc = ??? */
    
    	...
    
    	WaitTOF();
    	WaitTOF();
    	LoadView(temp);
    	custom.cop1lc=GfxBase->copinit;
    
  3. Audio.
    Use the Audio device. There are functions to change the volume, period, frequency, data etc that is played on any of the channels. If you must hit the audio hardware, you can ask for the channel you need with the highest priority (127), and the audio channel will never be stolen from you until you give the channel back to the system.
  4. Timers.
    Use the timer device. Some of the timer.device functions work as libraries, and so are easy to use. This allows you to be compatible should we use a 3rd cia time, say. The vertical blank can be used as a special low-frequency timer. See below. CIA timers can be allocated via the resource allocation calls. The "Resources" chapter of the V37 RKM: Devices manual has a good example.
  5. Input.
    Input will usually come from keyboard, mouse, joystick, infra-red etc. Mouse and joystick can be easily read from the hardware keyboard input could come from the keyboard.device, which knows how to handle keyboard timings, but it is easier by far to open an intuition window and read either RAWKEY or VANILLAKEY IDCMP messages. These either give the raw key number pressed, or the character the key pressed represents (useful for international games).
  6. Interrupts.
    Set up interrupt servers with high priority. Your server will then be the first called.
  7. Disk drives.
    Just use the DOS.library. It's so much easier, works on all possible drives, past, present and future, and makes s/w so much more friendly to the user. Floppy based copy protection can be accomplished by allocating the blitter and inhibiting the drive while checking for the key track.

Do's and Don'ts:
CPU Differences: