300 likes | 400 Views
Chapter 10 Arrays and Tile Mapping. Starting Out with Games & Graphics in C++ Tony Gaddis. 10.1 Array Basics. Concept:
E N D
Chapter 10Arrays and Tile Mapping Starting Out with Games & Graphics in C++ Tony Gaddis
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.
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
10.1 Array Basics Array Elements and Subscripts Figure 10-1 Array 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
10.1 Array Basics Assigning Values to Array Elements Figure 10-2 Values assigned to each element You access the individual elements in an array by using their subscripts
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
10.1 Array Basics Using a Loop to Step through an Array Step through an entire array, performing the same operation on each element
10.1 Array Basics Array Initialization Implicit Array Sizing • 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
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
10.1 Array Basics Comparing Two Arrays Compare two arrays with a loop that steps through both arrays, comparing their corresponding elements
10.1 Array Basics Shuffling an Array • To shufflean 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
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
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
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.
10.2 Sorting Arrays Figure 10-13 Values in an array • 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
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
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.
10.3 Two-Dimensional Arrays Figure 10-21 A two-dimensional array 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
10.3 Two-Dimensional Arrays Declaring a Two-Dimensional Array • 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 • 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
10.3 Two-Dimensional Arrays Accessing the Elements in a Two-Dimensional Array Figure 10-24 Output of Program 10-12 Figure 10-25 Number stored in the values array in example output of Program 10-12 To access the elements in a two-dimensional array, you must use both subscripts
10.3 Two-Dimensional Arrays Initializing a Two-Dimensional Array Figure 10-26 Initialization of the numbers array When initializing a two-dimensional array, it helps visually to enclose each row’s values in a set of braces
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
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 two-dimensional array that specifies tiles and their locations on the screen.
10.4 Tile Maps Figure 10-26 A tile-based image • 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
10.4 Tile Maps Figure 10-30 Tiles • Images are constructed using only a few tiles • Most are duplicates
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.
10.4 Tile Maps A function is needed to display the tiles on the screen
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
Chapter 10Arrays and Tile Mapping QUESTIONS ?