TweetFollow Us on Twitter

March 94 - TEN TIPS FOR GAME DEVELOPERS

TEN TIPS FOR GAME DEVELOPERS

BRIGHAM STEVENS

[IMAGE 114-118_Game_rev1.GIF]

In this column I'll give some general tips that are targeted at game developers but can in fact benefit any Macintosh application. Many of the tips are illustrated in the accompanying sample code (CopyBits ColorKarma) on this issue's CD. If you're considering writing a game for the Macintosh, or you want to improve your existing game or other application, these tips are for you. Here they are at a glance:

  1. The Macintosh gaming market is wide open.
  2. Bypass QuickDraw wisely.
  3. Use CopyBits correctly.
  4. Scroll graphics smoothly.
  5. Don't synchronize with the VBL interrupt.
  6. Use Sound Manager 3.0.
  7. Learn when to use (or not use) Apple events.
  8. Use the Time Manager.
  9. Use the Memory Manager effectively.
  10. Use a compatible copy-protection scheme (if any).

THE TIPS IN DETAIL

1. The Macintosh gaming market is wide open.
The Macintosh has infiltrated the homes, offices, and schools of every continent on the planet, and it has matured enough to be ready for entertainment software of all sorts. Hit Macintosh games have sold over 60,000 copies, and users are clamoring for more. If you walk into a software store, though, you'll notice that there isn't a very large selection of Macintosh game and entertainment software. Now is the time to take advantage of the lack of competition and get into the entertainment market with your games.

2. Bypass QuickDraw wisely.
Applications that bypass QuickDraw by accessing video memory directly may not work on future Macintosh platforms. The Macintosh is evolving, and some of the changes may be in the bus architecture, causing applications that write to video memory to break. We're not saying this is going to happen any time soon, but it's a good idea to be ready for it now so that your product will last in the marketplace.

A good compromise is to draw with custom drawing code into an off-screen GWorld, and then use CopyBits to transfer it to the screen. This avoids accessing hardware directly, and will always work. If you must write directly to the screen, it's important to follow the guidelines set forth in "Graphical Truffles: Writing Directly to the Screen" indevelop Issue 11, which states that you should always have a QuickDraw version of your code. If your program uses QuickDraw (or QuickDraw GX), it will always be compatible with every future Macintosh platform.

If you do bypass QuickDraw, your application should time the custom drawing code versus QuickDraw at run time, and then choose the fastest routines. This way, the fastest code will be used in cases where your code is running on a system that has an accelerated version of QuickDraw, or perhaps a different CPU altogether.

3. Use CopyBits correctly.
I've heard many developers say that CopyBits is slow, that it can't achieve the frame rates needed to do good games. If you use CopyBits correctly, however, youcan achieve a high animation frame rate.

Understanding all of the factors that affect CopyBits performance is critical to achieving high animation frame rates and still having processor bandwidth left over for the rest of the game. The following tips on CopyBits speed have been collated from many different sources, including Technical Notes, sample code, and other tomes of QuickDraw knowledge. (See also tip 4 below.)

  • CopyBits is more efficient with wider images than with tall ones.
  • CopyBits is more efficient with rectangular transfers, with no mask region.
  • CopyBits is more efficient when the source and destination have the same color table, and even better when the ctSeeds of the color tables are the same. (The accompanying sample code shows how to use the Palette Manager and how to set up the color tables in your application window and off-screen GWorld.)
  • CopyBits with a mask region is faster than either CopyMask or CopyDeepMask. Convert your masks to QuickDraw regions and then use CopyBits. (See the CopyBits vs. CopyMask snippet on this issue's CD.)
  • CopyBits is faster when the transfer source and destination are long-word aligned, especially on an 68040-based Macintosh.

For more details on these principles, see the Tech Note "Of Time and Space and _CopyBits."

4. Scroll graphics smoothly.
This tip applies to games with scrolling maps, painting programs, and any application that needs to scroll window content quickly and smoothly.

Many Macintosh applications suffer from flickering scrolling, caused by erasing the previous image before drawing the new image. To reduce flicker, you should redraw only the parts of the screen that change; don't erase anything first, unless absolutely necessary. When you're designing your scrolling code or animation engine, the philosophy to adopt is that every pixel should be touched onlyonce .

Another technique is to buffer your graphics into an off-screen GWorld: make all changes in the GWorld and then transfer the image to the screen with CopyBits. This can be slower, because the image is drawn twice, but it results in an especially smooth update. An example of a game that uses CopyBits in this way is MacPlay's Out of This World. This game draws into an off-screen GWorld using custom polygon-rendering code (thus ensuring a high frame rate); then, when the image is completely rendered, it's transferred to the screen with CopyBits (ensuring compatibility with future video hardware). For more on this subject, see "Graphical Truffles: Animation at a Glance" indevelop Issue 12 and "Drawing in GWorlds for Speed and Versatility" in Issue 10.

There's another method that isn't as smooth but uses less memory, and that is to use ScrollRect. ScrollRect was changed in System 7: if you pass nil for the updateRgn parameter of ScrollRect, it won't erase the area that has been uncovered, and you can then use CopyBits in a second step to copy in the new bits. (In System 6, ScrollRect will erase the area you're scrolling out of, causing the screen to flicker more.)The sample code demonstrates these techniques, showing the tradeoffs between memory footprint and smoothness/apparent speed.

5. Don't synchronize with the VBL interrupt.
Many developers have wanted to synchronize animation with the vertical blanking (VBL) interrupt to eliminate tears when the next frame of animation is drawn before the display hardware has completed the previous frame. It's possible to eliminate tears from small-sized animations, but the overall application will run more slowly because you'll be spending time waiting for the VBL period to start. This results in a much lower animation frame rate, and the application also loses processing power for the rest of the program. Note that QuickTime does not synchronize with the VBL interrupt.

Another headache to consider with respect to synchronizing with the VBL interrupt is that displays have different refresh rates, and each one's actual VBL period has a different length. This means that for your program to have accurate frame rates on different monitors, you'll have to time the refresh rate of the display you're animating on.

To work around not being able to synchronize with the VBL interrupt, you should try to interleave the animation processing so that you're never updating too many objects at one time. The Time Manager will allow you to break the processing up into separate tasks (see tip 8). If you're getting tears on objects, consider using fewer objects or smaller ones.

6. Use Sound Manager 3.0.
The Macintosh Sound Manager has recently been enhanced (version 3.0). It now can efficiently handle as many sound channels as memory and processor bandwidth can take. This means four channels on a Macintosh LC (which used to handle only one channel) and up to 16 or more on higher-end platforms. As a result, your application can play sound and still have enough CPU bandwidth for other animation and processing. The sample code demonstrates multiple sound channels playing asynchronously while animating an image. Also see "Somewhere in QuickTime: What's New With Sound Manager 3.0" in develop Issue 16 and "The Asynchronous Sound Helper" in Issue 11. Many bugs have been fixed in Sound Manager 3.0. You can now open a sound channel at the start of your program and then continuously use SndPlay to play sounds through it, without disposing of the channel between sounds. Previous versions of the Sound Manager had problems playing sampled sounds like this, so many developers adopted the technique of allocating and disposing of a new sound channel for each sound played.

On the AV Macintosh models, the Sound Manager uses the DSP. This requires the DSP Manager to load a new component every time you open a new channel, and may require disk access. So if you're running with Sound Manager 3.0 you shouldnot  open and close a sound channel for each sound played; doing so will cause your application to perform less than optimally, especially on the AV models.

See the source code file GameSounds.c, which is part of the CopyBits ColorKarma sample, for an example of a unit that manages asynchronous sound. If Sound Manager 3.0 or later is present, the code opens the sound channels at initialization and closes them when the program quits; otherwise it opens and closes the channels as sounds are played.

Sound Manager 3.0 also adds a new routine, named GetSndHeaderOffset, that makes it easier to use a bufferCmd to play sounds. Using a bufferCmd is faster than using SndPlay. See the sample code on the CD for an example.

Note that the sample code doesn't store the application's A5 register as part of the callback command, so that the interrupt code can set the flag associated with the channel. Instead it just stores a pointer to the flag. This allows the interrupt-time callback to be very small, since it doesn't have to save, set up, and restore A5; it just dereferences the pointer and sets the flag directly. The sample code gives an example of playing a sound asynchronously with a completion callback. I use thistechnique in just about any interrupt-time callback code I write, including VBL tasks, Time Manager tasks, and Device Manager completion routines.

If you don't use the Sound Manager at all, you're taking an unnecessary compatibility risk. Apple has always recommended against accessing the sound hardware directly. Applications that violate this rule have broken in the past, and they will break again.

7. Learn when to use (or not use) Apple events.
Apple events have simplified interapplication communication, making it easy to add value to your application. A game played against live human players is often more fun than a game played against a computer. Just about every night you'll find some Apple engineers huddled over their computers playing Bolo, Spaceward Ho!, or other network games.

Consider Velocity's Spectre, a network tank game that has been very successful. Spectre doesn't use Apple events; it uses custom DDP (datagram delivery protocol) socket listeners at the lowest level of AppleTalk to achieve high performance. But if your game doesn't require the same level of performance, you may benefit from the ease of use of Apple events.

If you require more performance than Apple events can provide, one option is to use the PPC Toolbox directly, which will allow you to still remain a step removed from direct AppleTalk. See the PPC Toolbox chapter ofInside Macintosh Volume VI for more information.

If you require even more performance, you can use AppleTalk Data Stream Protocol (ADSP) directly, or one of the other AppleTalk protocols. ADSP is a higher-level protocol that will allow you to do block transfers and not worry about losing packets and packet order.

It's hard to determine which networking protocol to use ahead of time. From my experience in using Apple events to synchronize animation and events between two Macintosh computers, I would say that if your game is a more than two-player, real-time arcade game, Apple events would probably not be the best solution. If your game is a turn-based strategy-type game, like Spaceward Ho!, RoboSport, Strategic Conquest, and many others, Apple events will work very well for you, no matter how many players are in the game.

For a simple example of using Apple events as a game messaging system, see ZAM 1.a13 on the CD.

8. Use the Time Manager.
The Macintosh Time Manager is very useful for game developers. Animation code often needs a heartbeat, to synchronize the timing and updates of every object. The Time Manager lets you break down your code into discrete tasks that run at a steady rate. This allows you to write modular code that updates smoothly.

One limitation of the Time Manager is that tasks fire at interrupt time, so they can't do much more than set a flag to inform the regular event loop that it's time to do something. The sample code on the CD shows how to place a wrapper around the Time Manager that allows you to execute tasks at non-interrupt time.

9. Use the Memory Manager effectively.
The Macintosh Memory Manager is very flexible, and a boon to most application programmers. However, for the game programmer it can be a performance problem unless it's used wisely. In a game, you should preallocate as much of your memory as possible. If you're using a dynamic object allocation scheme, you should design one that preallocates the objects and keeps track of which ones are in use or not. If you have many allocated blocks in a heap and then request a new one, you could send the Memory Manager into thrashing mode where it will try to move many blocks around to make space. This can cause your animation to be jerky or your whole game to freeze for an instant. So the best thing to do when performance matters is to minimize your use of the Memory Manager. To minimize Memory Manager use, you should not only allocate as much of your memory up front as possible but also avoid using relocatable blocks unless absolutely necessary. This means avoiding game architectures that rely on the Memory Manager for dynamic object allocation. Definitely allocate your nonrelocatable blocks first, and allocate handles later. This prevents heap fragmentation and avoids sending the Memory Manager into a tailspin.

Be aware that some parts of the Toolbox, like Apple events, expect Memory Manager structures. However, if the rest of the program's memory is allocated wisely to prevent heap fragmentation, even these allocations will happen quickly, with no impact on game performance.

10. Use a compatible copy-protection scheme (if any).
It's a matter of great controversy whether software should be copy-protected at all. No software protection scheme on the Macintosh has ever survived the talented efforts of Macintosh hackers. There's always someone who will defeat your copy protection, no matter how convoluted it may be. There are many who consider such protection a puzzle and a challenge to break, so by putting it in you may be inviting piracy.

But if you do decide you want some level of protection on your game, we strongly recommend against a disk-based protection scheme, which is guaranteed to break your program. Instead, we recommend using one (or a combination) of the methods described here.

One method is to use serial numbers: when the software is installed, ask the user to enter the serial number from the disk label, and then imprint the software with the person's name. Another method involves requiring the user to enter a password from the manual every once in a while. If you do this, it's a nice touch to allow users who send you the registration card to disable the password dialog; once you have the registration card, you can link the customer to a serial number.

Consider also making the following checks: At installation time, use Gestalt to determine the characteristics of the machine you're installed on, and save these to your preferences file. Also, use FindFolder to record the directory ID of the System Folder, which is the same until a new System Folder is created. Every time your application starts up, make the same Gestalt and FindFolder calls to check whether you're running on the same machine; if not, have the user reinstall the software and reenter the serial number, or reenter the password from the manual.

These techniques are the most compatible way to both protect your sales and minimize the kind of frustration customers experience with other password- or disk-based copy protection systems.

ARE YOU GAME?
That's not all! To help Macintosh game developers share tips, tricks, and information, Apple has set up the Game Development Discussion folder on AppleLink (in Developer Support: Developer Talk). This board is read by Macintosh game development companies, other developers, and Apple engineers on a daily basis. Also, there's a folder on this issue's CD, called Game Development, that contains special resources Apple has put together to aid all game developers.

So if you're tired of the scant choices on the Macintosh Entertainment shelf in your local software store, do something about it: write some games!

REFERENCES

  • Macintosh Technical Note "Of Time and Space and _CopyBits" (QuickDraw 21).
  • "Somewhere in QuickTime: What's New With Sound Manager 3.0" by Jim Reekes, develop  Issue 16.
  • "Graphical Truffles: Animation at a Glance" by Edgar Lee, develop  Issue 12.
  • "The Asynchronous Sound Helper" by Bryan K. ("Beaker") Ressler, and "Graphical Truffles: Writing Directly to the Screen" by Brigham Stevens and Bill Guschwan, develop  Issue 11.
  • "Drawing in GWorlds for Speed and Versatility" by Konstantin Othmer and Mike Reed, develop  Issue 10.
  • Guide to Macintosh Family Hardware , 2nd ed. (Addison-Wesley, 1990).
  • Snow Crash,  by Neal Stephenson. A fast-paced book to keep you up late at night. Stephenson says this book was inspired by the original Macintosh Human Interface Guidelines . (Imagine what he would have done if he'd had the new improved edition!)

BRIGHAM STEVENS (Internet vikingmind@aol.com) Since you last saw Brigham here, he has spoken at the Worldwide Developers Conference on Macintosh game development, moved to San Francisco, jumped out of a perfectly good airplane, become addicted to flight simulators, spent 150 hours in a car with no stereo, tossed his cookies on the steps of the Smithsonian Air and Space Museum in Washington DC, quit Apple to start a game development/consulting company in San Francisco, and become a vampire. You'll see some games from him later on in 1994. *

For more on VBL interrupts, see the Guide to Macintosh Family Hardware, second edition.*

Sound Manager 3.0 is available for licensing, so you can distribute it with your products to ensure that your customers and applications will receive its benefits. You can reach Apple's software licensing department at AppleLink SW.LICENSE. *

If you create a demo version of a game by commenting out code or imposing a time limit to game play, be aware that these kinds of demos are often hacked into full-blown pirate applications. We strongly suggest that when you create a demo version, you take out all nonessential pieces of code and sufficiently cripple the remaining software (so that a hacker can't simply paste in missing resources to get a full, unprotected copy). *


Thanks to Konstantin Othmer, Jim Reekes, and John Wang for reviewing this column. And special thanks to the people who contributed to the information in this column, including (but not limited to) C. K. Haun, Tony Myles, Craig Fryar, Mike Schlachter, Bill Dugan of Interplay Productions, and the rest of the developers I've worked with over the past couple of years.*

 
AAPL
$100.40
Apple Inc.
-0.18
MSFT
$45.13
Microsoft Corpora
+0.18
GOOG
$582.89
Google Inc.
-1.60

MacTech Search:
Community Search:

Software Updates via MacUpdate

MacFamilyTree 7.2.4 - Create and explore...
MacFamilyTree gives genealogy a facelift: it's modern, interactive, incredibly fast, and easy to use. We're convinced that generations of chroniclers would have loved to trade in their genealogy... Read more
Videobox 4.1.1 - Download Flash video th...
Videobox allows you to quickly and easily download Flash video from most all of the popular video sites on the internet. Videobox will convert the video into a native Quicktime format so it's ready... Read more
Web Snapper 3.3.5 - Capture entire Web p...
Web Snapper lets you capture Web pages exactly as they appear in your browser. You can send them to a file as images or vector-based, multi-page PDFs. It captures the whole Web page - eliminating... Read more
Picasa 3.9.138 - Organize, edit, and sha...
Picasa and Picasa Web Albums allows you to organize, edit, and upload your photos to the Web from your computer in quick, simple steps. Arrange your photos into folders and albums and erase their... Read more
Tidy Up 3.0.15.0 - Find duplicate files...
Tidy Up is a complete duplicate finder and disk-tidiness utility. With Tidy Up you can search for duplicate files and packages by the owner application, content, type, creator, extension, time... Read more
Parallels Desktop 10.0 - Run Windows app...
Parallels Desktop is simply the world's bestselling, top-rated, and most trusted solution for running Windows applications on your Mac. With Parallels Desktop for Mac, you can seamlessly run both... Read more
Apple Final Cut Pro X 10.1.3 - Professio...
Apple Final Cut Pro X is a professional video editing solution.Completely redesigned from the ground up, Final Cut Pro adds extraordinary speed, quality, and flexibility to every part of the post-... Read more
Apple Compressor 4.1.3 - Adds power and...
Compressor adds power and flexibility to Final Cut Pro X export. Customize output settings, work faster with distributed encoding, and tap into a comprehensive set of delivery features. Powerful... Read more
Chromium 36.0.1985.143 - Fast and stable...
Chromium is an open-source browser project that aims to build a safer, faster, and more stable way for all Internet users to experience the web. FreeSMUG-Free OpenSource Mac User Group build is... Read more
Macgo Blu-ray Player 2.10.6.1691 - Blu-r...
Macgo Mac Blu-ray Player can bring you the most unforgettable Blu-ray experience on your Mac. Overview Macgo Mac Blu-ray Player can satisfy just about every need you could possibly have in a Blu-ray... Read more

Latest Forum Discussions

See All

Outcast Odyssey – An Interview with the...
At this year’s San Diego Comic-Con, fans got the chance to meet and mingle with several of the artists behind Magic Pixel Games and Namco Bandai’s upcoming card battler, Outcast Odyssey. Considering many of these artists have worked on comics in the... | Read more »
BoxPop Review
BoxPop Review By Jennifer Allen on August 21st, 2014 Our Rating: :: UNTAXING PUZZLESUniversal App - Designed for iPhone and iPad Move from tile to tile to clear the board in this slightly too simple and easy puzzle game.   | Read more »
Breakdown Buddy Review
Breakdown Buddy Review By Jennifer Allen on August 21st, 2014 Our Rating: :: FLAWED CONCEPTiPhone App - Designed for the iPhone, compatible with the iPad There’s a good idea here, but Breakdown Buddy isn’t as useful as it tries to... | Read more »
jamstik, the Guitar You Can Take Everywh...
jamstik, the Guitar You Can Take Everywhere, is Now Available for Purchase Posted by Jessica Fisher on August 21st, 2014 [ permalink ] Zivix has announced that their guitar-in-your-pocket, | Read more »
Star Walk 2 - Guide to the Sky Day and N...
Star Walk 2 - Guide to the Sky Day and Night 1.0.0 Device: iOS Universal Category: Education Price: $2.99, Version: 1.0.0 (iTunes) Description: The next generation of the best-selling Star Walk, winner of Apple Design Award, used by... | Read more »
Forks Over Knives Review
Forks Over Knives Review By Jennifer Allen on August 21st, 2014 Our Rating: :: TASTY AND HEALTHYUniversal App - Designed for iPhone and iPad Want some tasty new recipe ideas but want to keep it healthy? Forks Over Knives is the... | Read more »
Invaders! From Outer Space (Games)
Invaders! From Outer Space 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: | Read more »
Dementia: Book of the Dead (Games)
Dementia: Book of the Dead 1.00 Device: iOS Universal Category: Games Price: $2.99, Version: 1.00 (iTunes) Description: EXCLUSIVE CONTENT ONLY ON THE APP STORE. Medieval England. Times of knights, witches and hunters. What other... | Read more »
Wan Nyan Slash (Games)
Wan Nyan Slash 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: Wan Nyan Slash is an infinite adorable demon slaying slashing action game! Play as the wandering samurai Wan and Nyan as they... | Read more »
Fallin Love - The Game of Love (Games)
Fallin Love - The Game of Love 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: GRAVITATE AROUND LOVE | Read more »

Price Scanner via MacPrices.net

21-inch iMacs on sale for up to $150 off MSRP
B&H Photo has 21″ iMacs on sale for up to $150 off MSRP including free shipping plus NY sales tax only. B&H will also include a free copy of Parallels Desktop software: - 21″ 2.7GHz iMac: $... Read more
27-inch 3.2GHz iMac on sale for $1698, save $...
Abt has the 27″ 3.2GHz iMac on sale for $1698 including free shipping. Their price is $101 off MSRP. Read more
Mac Backup Guru 2.0 Drive Backup/Cloneing Uti...
Mac Backup Guru developer MacDaddy has released Mac Backup Guru 2.0, offering new and enhanced advanced features, such as bootable backups, synchronised volumes and folders, and a Snapshot mode that... Read more
Operate GE’s New Free-Standing KItchen Range...
Think you accidentally left the oven on? Switch it off while on the go. The new free-standing Profile™ Series gas and electric ranges are GE’s second cooking appliances, following their wall oven, to... Read more
Parallels Announces Parallels Desktop 10 for...
The no. 1-selling software for running Windows applications on a Mac becomes an even easier choice for millions of consumers and IT professionals worldwide with the launch of the most powerful... Read more
Apple now offering certified refurbished 2014...
 The Apple Store is now offering Apple Certified Refurbished 2014 MacBook Airs for up to $180 off the cost of new models. An Apple one-year warranty is included with each MacBook, and shipping is... Read more
Best Buy’s College Student Deals: $100 off Ma...
Take an additional $100 off all MacBooks and iMacs, $50 off iPad Airs and iPad minis, at Best Buy Online with their College Students Deals Savings, valid through September 6th. Anyone with a valid .... Read more
MacBook Airs on sale for $100 off MSRP, free...
B&H Photo has three 2014 MacBook Airs on sale for $100 off MSRP. Shipping is free, and B&H charges NY sales tax only. They also include free copies of Parallels Desktop and LoJack for Laptops... Read more
Razer Taipan Mouse For Gamers And Non-Gamers...
If you’re a serious gamer on either Mac or Windows PCs, a serious gaming mouse is a necessity for first-tier performance. However, even if like me you’re not much of a gamer, there’s still a strong... Read more
15-inch 2.2GHz MacBook Pro on sale for $1899,...
Adorama has the new 15″ 2.2GHz Retina MacBook Pro on sale for $1899 including free shipping plus NY & NJ sales tax only. Their price is $100 off MSRP, and it’s the lowest price available for this... Read more

Jobs Board

Position Opening at *Apple* - Apple (United...
**Job Summary** As more and more people discover Apple , they visit our stores seeking ways to incorporate our products into their lives. It's your job, as a Store Read more
Position Opening at *Apple* - Apple (United...
…Summary** As a Specialist, you help create the energy and excitement around Apple products, providing the right solutions and getting products into customers' hands. You Read more
Project Manager, *Apple* Financial Services...
**Job Summary** Apple Financial Services (AFS) offers consumers, businesses and educational institutions ways to finance Apple purchases. We work with national and Read more
*Apple* Solutions Consultant (ASC)- Retail S...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Position Opening at *Apple* - Apple (United...
…customers purchase our products, you're the one who helps them get more out of their new Apple technology. Your day in the Apple Store is filled with a range of Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.