Chapter 10
Arrays and Tile Mapping
Starting Out with
Games & Graphics in C++
Tony Gaddis
Addison Wesley
is an imprint of
© 2010 Pearson Addison-Wesley. All rights reserved.
10.1 Array Basics
Concept:
In the programs you have designed so
far, you have used variables to store
data in memory. The simplest way to
store a value in memory is to store it in
a variable. Variables work well in many
situations, but they have limitations.
Copyright © 2010 Pearson Addison-Wesley
1-2
10.1 Array Basics
•
•
•
•
Variables can only hold one value at a time
– Not well suited for storing and processing sets of data
– Must be declared and individually processed
Arrays are specifically designed for storing and processing sets of data
– Named storage location in memory, like variables
– Can hold a group of values, unlike variables
– All values must be of the same data type
– Cannot store a mixture of data types
Number inside braces is called the size declarator
– Specifies the number of values that the array can hold
An array’s size cannot be changed while the program is running
– Named constants make array sizes easier to maintain
Copyright © 2010 Pearson Addison-Wesley
1-3
10.1 Array Basics
Array Elements and Subscripts
•
•
The storage locations in arrays are known as elements
– Located in consecutive memory locations
Each element in an array is assigned a unique number called a subscript
– Used to identify specific elements in an array
– First element is assigned the subscript 0
– Second element is assigned the subscript 1
– And so forth
Figure 10-1 Array subscripts
Copyright © 2010 Pearson Addison-Wesley
1-4
10.1 Array Basics
Assigning Values to Array Elements
• You access the individual elements in an array by using their
subscripts
Figure 10-2 Values assigned to each element
Copyright © 2010 Pearson Addison-Wesley
1-5
10.1 Array Basics
No Array Bounds Checking in C++
•
The C++ language does not perform array bounds checking
– Array subscript values are not checked by the compiler
Copyright © 2010 Pearson Addison-Wesley
1-6
10.1 Array Basics
Using a Loop to Step through an Array
•
Step through an entire array, performing the same operation on each element
Copyright © 2010 Pearson Addison-Wesley
1-7
10.1 Array Basics
Array Initialization
•
You can optionally initialize an array with values when you declare it
•
The series of values separated with commas and enclosed in curly braces is
called an initialization list
– Values are stored in the array elements in the order they appear in the list
Implicit Array Sizing
Copyright © 2010 Pearson Addison-Wesley
1-8
10.1 Array Basics
Passing an Array as an Argument to a Function
•
Passing an array as an argument typically requires two arguments
– The array itself
– An integer specifying the number of elements in the array
Copyright © 2010 Pearson Addison-Wesley
1-9
10.1 Array Basics
Comparing Two Arrays
•
Compare two arrays with a loop that steps through both arrays, comparing
their corresponding elements
Copyright © 2010 Pearson Addison-Wesley
1-10
10.1 Array Basics
Shuffling an Array
• To shuffle an array
means to randomly
rearrange its contents
– For each element in
the array
– Randomly select
another element
– Swap the contents
of this element with
the randomly
selected element
Copyright © 2010 Pearson Addison-Wesley
1-11
10.1 Array Basics
Swapping Array Elements
•
To successfully swap the contents of two variables, we need a third variable
to serve as a temporary storage location
Copyright © 2010 Pearson Addison-Wesley
1-12
10.1 Array Basics
Partially Filled Arrays
•
When you process a
partially filled array:
– Process only the
elements that contain
valid items
– Use an integer
variable that holds the
number of items in the
array
– Increment the integer
variable each time we
add an item to the
array
Copyright © 2010 Pearson Addison-Wesley
1-13
10.2 Sorting Arrays
Concept:
A sorting algorithm rearranges the
contents of an array so they appear
in a specific order. The selection
sort is a specific example of a
sorting algorithm.
Copyright © 2010 Pearson Addison-Wesley
1-14
10.2 Sorting Arrays
• Many programming tasks require that data in an array be sorted in
some order
• A sorting algorithm is a technique for stepping through an array
and rearranging its contents in some order
– Ascending order means from lowest to highest
– Descending order means from highest to lowest
• We will examine the selection sort algorithm
– Smallest value is moved to element 0
– Next smallest value is moved to element 1
– Process continues until all of the elements are in proper order
Figure 10-13 Values in an array
Copyright © 2010 Pearson Addison-Wesley
1-15
10.2 Sorting Arrays
Figure 10-14 Values in the array after the first swap Figure 10-15 Values in the array after the second swap
Figure 10-16 Values in the array after the third swap Figure 10-17 Values in the array after the fourth swap
Figure 10-18 Values in the array after the fifth swap
Copyright © 2010 Pearson Addison-Wesley
1-16
10.2 Sorting Arrays
Copyright © 2010 Pearson Addison-Wesley
1-17
10.3 Two-Dimensional Arrays
Concept:
A two-dimensional array is like
several identical arrays put
together. It is useful for storing
multiple sets of data.
Copyright © 2010 Pearson Addison-Wesley
1-18
10.3 Two-Dimensional Arrays
•
•
Two-dimensional arrays are useful for working with multiple sets of data
Think of a two-dimensional array as having rows and columns of elements
Figure 10-21 A two-dimensional array
Copyright © 2010 Pearson Addison-Wesley
1-19
10.3 Two-Dimensional Arrays
Declaring a Two-Dimensional Array
•
To declare a two-dimensional array, two size declarators are required:
– The first one is for the number for the rows
– The second one is for the number of columns
•
When processing data, each element has two subscripts:
– One for its row
– Another for its column
Figure 10-23 Subscripts for each element of the values array
Copyright © 2010 Pearson Addison-Wesley
1-20
10.3 Two-Dimensional Arrays
Accessing the Elements in a Two-Dimensional Array
•
To access the elements in a two-dimensional array, you must use both subscripts
Figure 10-24 Output of Program 10-12
Figure 10-25
Number stored
in the values
array in example
output of
Program 10-12
Copyright © 2010 Pearson Addison-Wesley
1-21
10.3 Two-Dimensional Arrays
Initializing a Two-Dimensional Array
•
When initializing a two-dimensional array, it helps visually to enclose each
row’s values in a set of braces
Figure 10-26 Initialization of the
numbers array
Copyright © 2010 Pearson Addison-Wesley
1-22
10.3 Two-Dimensional Arrays
Passing a Two-Dimensional Array to a Function
•
When a two-dimensional array is passed to a function, the parameter must
contain a size declarator for the number of columns
Copyright © 2010 Pearson Addison-Wesley
1-23
10.4 Tile Maps
Concept:
Tiles are small rectangular images
that are commonly used to
construct the background imagery
in a game. A tile map is a twodimensional array that specifies
tiles and their locations on the
screen.
Copyright © 2010 Pearson Addison-Wesley
1-24
10.4 Tile Maps
•
•
Tiles are small rectangular images that can be put together to form a larger
image
Used in early video games, still used by many game programmers today
– Memory efficient
– Increase performance
Figure 10-26 A tile-based image
Copyright © 2010 Pearson Addison-Wesley
1-25
10.4 Tile Maps
• Images are constructed using only a few tiles
– Most are duplicates
Figure 10-30 Tiles
Copyright © 2010 Pearson Addison-Wesley
1-26
10.4 Tile Maps
•
A tile map is an array that maps the location of each tile on the screen
– Each element holds the image number of the tile
– Rows and columns of the tile map correspond to rows and columns on
the screen.
Copyright © 2010 Pearson Addison-Wesley
1-27
10.4 Tile Maps
•
A function is needed to display the tiles on the screen
Copyright © 2010 Pearson Addison-Wesley
1-28
10.4 Tile Maps
Displaying Layered Sets of Tiles
•
You can display layered sets of tiles by using two tile maps
– One for background
– Another for obstacles
Copyright © 2010 Pearson Addison-Wesley
1-29
Chapter 10
Arrays and Tile Mapping
QUESTIONS
Addison Wesley
is an imprint of
?
© 2010 Pearson Addison-Wesley. All rights reserved.