Presentation is loading. Please wait.

Presentation is loading. Please wait.

Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Starting Out with Games & Graphics in C++ Tony Gaddis Chapter 8 The.

Similar presentations


Presentation on theme: "Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Starting Out with Games & Graphics in C++ Tony Gaddis Chapter 8 The."— Presentation transcript:

1 Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Starting Out with Games & Graphics in C++ Tony Gaddis Chapter 8 The Vulture Trouble Game: Introducing Audio, Physics, and Text Effects

2 Copyright © 2010 Pearson Addison-Wesley 8.1 Introduction In this chapter we will: –Put programming topics into practice with the Vulture Trouble game –Incorporate music –Sound effects –Use a bit of physics –Manipulate the appearance and size of text 1-2

3 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music 1-3 Concept: The Dark GDK lets you play audio files that have been saved in the WAV, MIDI, or MP3 formats, as well as music tracks on an audio CD. The library provides numerous functions for working with these audio files.

4 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music The Dark GDK allows the playback of audio files that are stored in the following file formats: –WAV –MIDI –MP3 WAV files for sound MIDI or MP3 files for music There are two sets of functions for loading and playing audio: –One set for sound files –One set for music files 1-4

5 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music Audio files saved in the WAV format are considered sound files by the Dark GDK A sound file must be loaded into memory before it can be used You load a sound into memory by calling the dbLoadSound function 1-5 Sound Files : Loading a Sound File FileName is the name of the sound file you would like to load into memory SoundNumber is the number you are assigning to the sound

6 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music You play a sound loaded into memory by calling the dbPlaySound function 1-6 Sound Files : Playing a Sound SoundNumber is the number of the sound you want to play

7 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music To loop a sound means to play it repeatedly You can loop a sound loaded into memory by calling the dbLoopSound function 1-7 Sound Files : Looping a Sound SoundNumber is the number of the sound you want to loop The setUp function from Program 8-3 ( LoopSound.cpp )

8 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music You can stop a sound that is currently playing by calling the dbStopSound function You can determine whether a sound is playing by calling the dbSoundPlaying function, passing the sound number as an argument You can determine whether a sound is looping by calling the dbSoundLooping function, passing the sound number as an argument –These functions return 1 (true) if the sound is playing or looping –These functions return 0 (false) if the sound is not playing or looping 1-8 Sound Files : Stopping a Sound

9 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music When you call the dbPlaySound function, you can pass an optional second argument that allows you to skip part of the sound file 1-9 Sound Files : Playing or Looping Part of a Sound File The dbLoopSound function also allows you to loop a portion of the sound file Here are various optional formats in which the function may be called:

10 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music A sound can be removed from memory by calling the dbDeleteSound function, passing the sound number as an argument 1-10 Sound Files : Deleting a Sound from Memory Sound Files : Determining Whether a Sound Exists The dbSoundExist function can determine whether a sound is loaded in the program’s memory by passing the sound number as an argument Returns 1 (true) if sound file is loaded in memory Returns 0 (false) if sound file is not loaded in memory

11 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music The dbPauseSound function can be used to pause a sound that is playing by passing the sound number you want to pause as an argument 1-11 Sound Files : Pausing and Resuming a Sound The dbResumeSound function can be used to resume a sound that is paused by passing the sound number you want to resume as an argument You can determine whether a sound is paused by calling the dbSoundPaused function, passing the sound number as an argument Returns 1 (true) if sound is paused Returns 0 (false) if sound is playing

12 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music You can reference the same sound in memory by using the dbCloneSound function Memory efficient way to create multiple instances of a sound 1-12 Sound Files : Cloning a Sound ExistingSoundNumber is the number of the sound you want to clone NewSoundNumber is the new sound number that references the sound

13 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music You can adjust the volume of a sound with the dbSetSoundVolume function 1-13 Sound Files : Sound Volume You get the volume of a sound by calling the dbSoundVolume function, passing the sound number as an argument SoundNumber is the number of the sound for which you want to change the volume –Volume is an integer value that specifies a volume percentage –Value in the range 0 through 100 –Operates within the current operating system volume setting

14 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music You can get the playing speed of a sound by calling the dbSoundSpeed function, passing the sound number as an argument WAV files typically have a playing speed of 44,100Hz You can change the speed of a sound by calling the dbSetSoundSpeed function 1-14 Sound Files : Sound Speed SoundNumber is the number of the sound you want to set the speed for Speed sets the speed of the sound using a value from 100 to 100,000 Lower values produce slower, low-pitched sounds Higher values produce faster, high-pitched sounds

15 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music Sounds can pan from the left speaker to the right or vice-versa You set the sound pan by calling the dbSetSoundPan function 1-15 Sound Files : Panning Sounds SoundNumber is the number of the sound for which you are setting the pan Pan is an integer value ranging from -10,000 to 10,000 0 is equal distribution Negative values shift to left speaker Positive values shift to right speaker

16 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music Before you can use a music file, you have to load it into memory You load a music file into memory by calling the dbLoadMusic function 1-16 Music Files : Loading a Music File FileName is the name of the music file you would like to load into memory MusicNumber is an integer number, in the range of 1 through 32, that you are assigning to the music

17 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music Once you have a music file loaded into memory, you can play it with the dbPlayMusic function 1-17 Music Files : Playing Music MusicNumber is the number of the music that you want to play Music Files : Looping Music To loop music means to play it repeatedly You can loop music by calling the dbLoopMusic function MusicNumber is the number of the music that you want to loop

18 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music You can stop music that is currently playing by calling the dbStopMusic function 1-18 Music Files : Stopping Music MusicNumber is the number of the music that you want to stop Music Files : Determining Whether Music is Playing or Looping You can determine whether music is playing by calling the dbMusicPlaying function, which returns a value of 1 or 0 You can determine whether music is looping by calling the dbMusicLooping function, which returns a value of 1 or 0

19 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music When music is no longer needed, it can be removed from memory by calling the dbDeleteMusic function, passing the music number as an argument 1-19 Music Files : Deleting Music from Memory Music Files : Determining Whether or Not Music Exists You can determine whether music is loaded in the program’s memory by calling the dbMusicPlaying function, passing the music number as an argument Returns 1 (true) if music exists Returns 0 (false) if music does not exist

20 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music Music that is currently playing can be paused by calling the dbPauseMusic function, passing the music number as an argument You can resume music that is paused by calling the dbResumeMusic function, passing the music number as an argument You can determine whether or not music is paused by calling the dbMusicPaused function, passing the music number as an argument Returns 1 (true) if the music is paused Returns 0 (false) if the music is not paused 1-20 Music Files : Pausing and Resuming Music

21 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music You can adjust the volume of music with the dbSetMusicVolume function 1-21 Music Files : Music Volume You get the volume of the music by calling the dbMusicVolume function, passing the music number as an argument MusicNumber is the number of the music for which you want to change the volume Volume is an integer value that specifies a volume percentage –Value in the range 0 through 100 –Operates within the current operating system volume setting

22 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music You can change the music speed with the dbSetMusicSpeed function MusicNumber is the number of the music you want to change Speed is an integer value that specifies the speed as a percentage You can get the speed of the music by calling the dbMusicSpeed function, passing the music number as an argument 1-22 Music Files : Music Speed

23 Copyright © 2010 Pearson Addison-Wesley 8.2 Playing Sound Effects and Music You can load music from a CD by calling the dbLoadCDMusic function –Only one CD track may be loaded at a time –Remember to delete a CD track before loading another You can get the number of tracks on a CD by calling the dbGetNumberofCDTracks function –Returns the number of tracks –Returns zero if no CD media is available 1-23 Music Files : Loading Music from a CD

24 Copyright © 2010 Pearson Addison-Wesley 8.3 Simulating Falling Objects 1-24 Concept: When an object in the real world falls to Earth, its speed increases as it falls. If you want to write a program that realistically simulates falling objects, you will need to incorporate this acceleration into your program.

25 Copyright © 2010 Pearson Addison-Wesley 8.3 Simulating Falling Objects Gravity is a force that attracts objects to one another Acceleration is an increase in an object’s speed A falling object’s acceleration is 9.8 meters per second, each second –Written as 9.8m/s 2 –Represented by the letter g Speed and time help to determine the distance a falling object moves In this formula: –d is the distance moved –g is 9.8, the rate of acceleration –t is the number of seconds the object has been falling 1-25 Figure 8-3 A falling brick’s distance at various time intervals (not drawn to scale)

26 Copyright © 2010 Pearson Addison-Wesley 8.3 Simulating Falling Objects 1-26 Figure 8-4 Ball sprite from Program 8-8 The game loop from Program 8-8 ( FreeFall.cpp ) Program 8-8 shows how we can approximate the motion of a falling object, at a speed that is slow enough to observe, yet fast enough to seem realistic When the program runs, it displays the ball sprite shown in Figure 8-4 falling from the top of the screen to the bottom

27 Copyright © 2010 Pearson Addison-Wesley 8.3 Simulating Falling Objects To simulate this type of motion: –Update the falling object’s position along both the X and Y axes 1-27 Simulating Motion in Two Directions Figure 8-5 Four frames captured from Program 8-9’s output The game loop from Program 8-9 ( DualMotion.cpp )

28 Copyright © 2010 Pearson Addison-Wesley 8.4 Text Effects 1-28 Concept: You can change the font, size, and style of text that is displayed by the dbPrint, dbText, and dbCenterText functions.

29 Copyright © 2010 Pearson Addison-Wesley 8.4 Text Effects Text appears on the screen using the default font and size Font is the name of the typeface that defines the way characters appear –Times Roman, Arial, and Courier are examples –Default Dark GDK font is Fixedsys Text size is measured in points –One point is 1/72 of an inch –Default Dark GDK text size is 12 points Dark GDK provides functions for changing –Font and text size –Style of the text to make it appear bold and/or italic 1-29

30 Copyright © 2010 Pearson Addison-Wesley 8.4 Text Effects The dbSetTextSize function can be used to set the point size of the current font –Size setting remains in effect until changed –Can be set to any size supported by the current font type –Unsupported text sizes will not result in an error, but the text size will not change You can get the current text size by calling the dbFontSize function –Returns an integer value for the current text size 1-30 Changing the Text Size

31 Copyright © 2010 Pearson Addison-Wesley 8.4 Text Effects You can change the current text font by calling the dbSetTextFont function, passing the font name as a string argument –Font setting remains in effect until changed –Unsupported or misspelled font names will not result in an error, but the text font will not change You can get the name of the current font by calling the dbTextFont function –Returns a string containing the current font name 1-31 Changing the Text Font

32 Copyright © 2010 Pearson Addison-Wesley 8.4 Text Effects dbSetTextToBold(); –sets text style to bold dbSetTextToItalic(); –sets text style to italic dbSetTextToBoldItalic(); –sets text style to bold and italic dbSetTextToNormal(); –sets text style to normal 1-32 Using Bold and Italic Styles Figure 8-4 Output of Program 8-10

33 Copyright © 2010 Pearson Addison-Wesley 8.4 Text Effects The dbTextStyle function returns an integer value specifying the text style –0 is normal (non-bold and non-italic) –1 is italic –2 is bold –3 is bold and italic 1-33 Using Bold and Italic Styles

34 Copyright © 2010 Pearson Addison-Wesley 8.4 Text Effects Text is displayed in a rectangular area called the text background that is transparent by default You call the dbSetTextOpaque function to change the text background to opaque, which is black by default You can change the text background back to transparent again, by calling the dbSetTextTransparent function 1-34 Text Background Transparency Figure 8-7 Text displayed on transparent and opaque backgrounds

35 Copyright © 2010 Pearson Addison-Wesley 8.4 Text Effects You can change the color of the text background with the dbInk function 1-35 Text Background Transparency The dbTextBackgroundType returns a value of 1 if the background is transparent or 0 if the background is opaque

36 Copyright © 2010 Pearson Addison-Wesley You can use the dbTextWidth and dbTextHeight functions to get the width and height of a string, in pixels, using the current font and text size 1-36 8.4 Text Effects Getting Text Width and Height Figure 8-8 Output of Program 8-11

37 Copyright © 2010 Pearson Addison-Wesley 8.4 Text Effects The cursor marks the position at which text is displayed The dbSetCursor function changes the location of the dbPrint function’s output 1-37 Setting the Cursor Position Figure 8-9 Output of Program 8-12

38 Copyright © 2010 Pearson Addison-Wesley 8.5 The Vulture Trouble Game 1-38 Concept: A greedy vulture has stolen eggs from a farmer’s hen house. The vulture realizes he’s been caught, and is dropping the eggs one by one. The player’s objective is to use a basket to catch as many eggs as possible. If the player does not catch an egg, it hits the ground and breaks.

39 Copyright © 2010 Pearson Addison-Wesley 8.5 The Vulture Trouble Game 1-39 Figure 8-12 Images used in the Vulture Trouble Game farm.bmp is the background image vulture.bmp contains frames for the animated sprite egg.bmp shows an egg basket.bmp is the basket the player uses to catch eggs hitBasket.bmp will be displayed briefly when an egg hits the basket brokenEgg.bmp will be displayed briefly when an egg hits the ground

40 Copyright © 2010 Pearson Addison-Wesley 8.5 The Vulture Trouble Game 1-40 Figure 8-10 Vulture Trouble title screen and introductory screen The game’s title screen is displayed when the game starts The introductory screen, displayed after the player presses a key, gives instructions for playing the game

41 Copyright © 2010 Pearson Addison-Wesley 8.5 The Vulture Trouble Game 1-41 Figure 8-11 Vulture Trouble main screen and summary screen The game’s main screen is where the game takes place The vulture will drop a total of 40 eggs and the player will try to catch as many of them as possible The summary screen appears after the last egg has been dropped and tallies up the player’s score displaying the results

42 Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 8 The Vulture Trouble Game: Introducing Audio, Physics, and Text Effects QUESTIONS ?


Download ppt "Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Starting Out with Games & Graphics in C++ Tony Gaddis Chapter 8 The."

Similar presentations


Ads by Google