
Bonus Chapters Chapter 25: Performance: Texture Atlases 4! Advantage 1: Use less memory ....................................................................................... 5! Advantage 2: Run faster ................................................................................................... 7! Using texture atlases ...................................................................................................... 11! Pixel formats .................................................................................................................... 14! Manual texture atlas generation .................................................................................. 18! Texture atlas pitfalls ....................................................................................................... 23! Challenge: Preloading power ....................................................................................... 29! Chapter 26: Performance: Tips and Tricks 32! Getting started ................................................................................................................ 33! Introducing Instruments .................................................................................................... 34! Touring the code .............................................................................................................. 36! Reducing sprites and physics bodies ........................................................................... 37! Reducing particle systems .............................................................................................. 39! Other performance tips and tricks ............................................................................... 42! Challenges ........................................................................................................................ 44! Chapter 27: Making Art for Programmers 46! Choose your path: hire or DIY? ..................................................................................... 47! How to find and hire an artist ....................................................................................... 48! Paying your artist ............................................................................................................ 49! Getting started ................................................................................................................ 51! Start with a sketch ........................................................................................................... 52! Getting the sketch into Illustrator .................................................................................. 57! 2 Tracing the sketch with vector lines .............................................................................. 61! Custom stroke widths for a more natural line ............................................................ 64! Coloring your artwork .................................................................................................... 67! A bit about shadow and light ....................................................................................... 78! Exporting PNG files ........................................................................................................ 84! Challenges ........................................................................................................................ 86! 3 Chapter 25: Performance: Texture Atlases By Rod Strougo A great game needs more than just fun and polish – it also needs to perform well. Customers expect a smooth frame rate while they play. Nothing breaks the player’s concentration like a slow frame rate, or a mid-game stutter! One of the most important performance optimizations you can make to your game is to use texture atlases. As you may have noticed, you’ve already been using texture atlases for most of the games in this book! Starting with Cat Nap back in Chapter 9, “Intermediate Physics,” you put your sprites inside a folder that ends with .atlas. Behind the scenes, Sprite Kit generated a texture atlas for you that looked something like this: Although you learned that using texture atlases is a good practice, you didn’t learn why. You also didn’t learn about some neat optimizations you can make to your texture atlases – good practice on top of good practice – all of which adds up to your game using less memory and running faster. iOS Games by Tutorials Chapter 25: Performance: Texture Atlases In this chapter, you’ll take a deeper look at texture atlases and exactly why it’s important to use them. You’ll also learn how you can save memory in your game by choosing the proper pixel format for your images, as well as how you can avoid some common pitfalls with texture atlases. Let’s begin by making the case for texture atlases and examining their two major advantages. Advantage 1: Use less memory The first benefit of using texture atlases is that it decreases your game’s memory usage. Memory is a precious resource on iOS devices. The iPhone 4S has only 512MB memory versus 4GB+ on a typical Mac Mini. Reducing the amount of memory your app uses is important for several reasons: • Increase performance. After flash storage, accessing memory is the second slowest part of the hardware. The more you can reduce memory requirements, the faster your app will run. • Keep your app from crashing. If your game has a lot of images and textures, it’s quite possible for your app to use more memory than iOS currently has available. iOS will try its best to terminate other apps to make up the difference, but if that fails iOS may terminate your app due to insufficient memory. To customers, this will look like a crash, so is definitely something you want to avoid. • Keep your app running in the background. When memory gets low, iOS terminates apps that are running in the background, beginning with the largest memory hogs. If you decrease your memory requirements, you decrease the chances of iOS terminating your app when it’s in the background. This makes your customers happy because your app appears to launch faster. The biggest use of memory in your games by far will be textures. Therefore, when it comes to reducing your memory load, this is the first area you should address. Determining memory costs of textures The first question to ask yourself when considering your game’s memory load is, “How much memory are my textures using?” You might think that’s as simple as looking at the size of your images, but unfortunately that’s not the case. The GPUs on iOS devices can only handle a special format called PVRTC natively. Every other image format must be stored uncompressed for the GPU to be able to render it. As an example, a 1420x640 pixel, all-green PNG background takes up a measly 57KB on flash storage, but once loaded into memory it consumes 3640KB! 5 iOS Games by Tutorials Chapter 25: Performance: Texture Atlases Each image has a pixel format, which specifies how many bits track each color for a pixel. How many bits you use per pixel is what determines whether your image supports 8, 16, 256, thousands or millions of colors. The more bits per pixel, the larger the number of colors that you can store in an image, and generally the better the image looks. The iOS GPU supports millions of colors and the highest quality pixel format is called RGBA8888. This means each pixel has 32 bits – 8 bits each for the Red, Green, Blue and Alpha values. By default, Sprite Kit creates textures and texture atlases in this format, but later in this chapter you’ll learn how you can specify lower quality formats if you’re willing to sacrifice quality in return for memory savings. The simple formula you can use to determine how much memory a particular image uses is: (Length x Width x Bits per Pixel)/8 = Bytes in RAM. For example, iOS will uncompress a 128x128 image to (128 x 128 x 32) / 8 = 65K bytes in memory. Why texture atlases help Now that you know how to calculate the memory costs of textures, let’s see why texture atlases help save memory. Imagine you wanted to use these four 161x161 images in Cat Nap: If you added these images to your game individually rather than in an .atlas folder, using the formula above, iOS will uncompress each image to (161 x 161 x 32) / 8 = ~101K in memory, or ~405K for all four images. If you put these sprites in an .atlas folder instead, Sprite Kit will automatically generate a texture atlas for you. A texture atlas is just a single larger image containing all of the smaller images inside it, as well as instructions for how to extract each of the sub-images. Here is what it might look like for the cat images above: 6 iOS Games by Tutorials Chapter 25: Performance: Texture Atlases In this example, Sprite Kit generates an image that is 277x309 pixels. Using the formula above, iOS will uncompress this to (277x 309 x 32) / 8 = ~334K in memory. This is 71K in savings! The best part is usually the more images you pack into a texture atlas, the more memory savings you get. Advantage 2: Run faster The second advantage of using texture atlases is that it makes your game run faster –above and beyond the performance boost you get from Advantage 1, using less memory. Sprite Kit is built on top of the OpenGL ES graphics API. To draw a texture on the screen with OpenGL, there are two steps: 1. Bind the texture. First OpenGL needs to make the texture you want to use active. This is called binding the texture, and it’s an expensive operation. 2. Draw the texture. Then OpenGL draws a portion of the texture (i.e. the portion
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages87 Page
-
File Size-