1 / 42

Starting Out with Games & Graphics in C++ Tony Gaddis

Starting Out with Games & Graphics in C++ Tony Gaddis. Chapter 8 The Vulture Trouble Game: Introducing Audio, Physics, and Text Effects. 8.1 Introduction. In this chapter we will: Put programming topics into practice with the Vulture Trouble game Incorporate music Sound effects

robbin
Download Presentation

Starting Out with Games & Graphics in C++ Tony Gaddis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Starting Out with Games & Graphics in C++ Tony Gaddis Chapter 8The Vulture Trouble Game: Introducing Audio, Physics, and Text Effects

  2. 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

  3. 8.2 Playing Sound Effects and Music 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. 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

  5. 8.2 Playing Sound Effects and Music 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 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

  6. 8.2 Playing Sound Effects and Music Sound Files : Playing a Sound • SoundNumber is the number of the sound you want to play You play a sound loaded into memory by calling the dbPlaySound function

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

  8. 8.2 Playing Sound Effects and Music Sound Files : Stopping a Sound • 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

  9. 8.2 Playing Sound Effects and Music 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: When you call the dbPlaySound function, you can pass an optional second argument that allows you to skip part of the sound file

  10. 8.2 Playing Sound Effects and Music 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 A sound can be removed from memory by calling the dbDeleteSound function, passing the sound number as an argument

  11. 8.2 Playing Sound Effects and Music 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 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

  12. 8.2 Playing Sound Effects and Music 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 You can reference the same sound in memory by using the dbCloneSound function Memory efficient way to create multiple instances of a sound

  13. 8.2 Playing Sound Effects and Music Sound Files : Sound Volume • 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 • You get the volume of a sound by calling the dbSoundVolume function, passing the sound number as an argument You can adjust the volume of a sound with the dbSetSoundVolume function

  14. 8.2 Playing Sound Effects and Music 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 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

  15. 8.2 Playing Sound Effects and Music 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 Sounds can pan from the left speaker to the right or vice-versa You set the sound pan by calling the dbSetSoundPan function

  16. 8.2 Playing Sound Effects and Music 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 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

  17. 8.2 Playing Sound Effects and Music 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 Once you have a music file loaded into memory, you can play it with the dbPlayMusic function

  18. 8.2 Playing Sound Effects and Music 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 You can stop music that is currently playing by calling the dbStopMusic function

  19. 8.2 Playing Sound Effects and Music 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 • When music is no longer needed, it can be removed from memory by calling the dbDeleteMusic function, passing the music number as an argument

  20. 8.2 Playing Sound Effects and Music Music Files : Pausing and Resuming 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

  21. 8.2 Playing Sound Effects and Music Music Files : Music Volume • 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 • You get the volume of the music by calling the dbMusicVolume function, passing the music number as an argument You can adjust the volume of music with the dbSetMusicVolume function

  22. 8.2 Playing Sound Effects and Music Music Files : Music Speed • 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

  23. 8.2 Playing Sound Effects and Music Music Files : Loading Music from a CD • 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

  24. 8.3 Simulating Falling Objects 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. 8.3 Simulating Falling Objects Figure 8-3 A falling brick’s distance at various time intervals (not drawn to scale) • 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/s2 • Represented by the letter g • Speed and time help to determine the distance a falling object moves • In this formula: • dis the distance moved • g is 9.8, the rate of acceleration • tis the number of seconds the object has been falling

  26. 8.3 Simulating Falling Objects 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 Figure 8-4 Ball sprite from Program 8-8

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

  28. 8.4 Text Effects Concept: You can change the font, size, and style of text that is displayed by the dbPrint, dbText, and dbCenterText functions.

  29. 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

  30. 8.4 Text Effects Changing the Text Size • 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

  31. 8.4 Text Effects Changing the Text Font • 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

  32. 8.4 Text Effects Using Bold and Italic Styles Figure 8-4 Output of Program 8-10 • 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

  33. 8.4 Text Effects Using Bold and Italic Styles • 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

  34. 8.4 Text Effects Text Background Transparency Figure 8-7 Text displayed on transparent and opaque backgrounds 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

  35. 8.4 Text Effects Text Background Transparency • The dbTextBackgroundType returns a value of 1 if the background is transparent or 0 if the background is opaque You can change the color of the text background with the dbInk function

  36. 8.4 Text Effects Getting Text Width and Height Figure 8-8 Output of Program 8-11 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

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

  38. 8.5 The Vulture Trouble Game 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. 8.5 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 Figure 8-12 Images used in the Vulture Trouble Game

  40. 8.5 The Vulture Trouble Game • 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 Figure 8-10 Vulture Trouble title screen and introductory screen

  41. 8.5 The Vulture Trouble Game • 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 Figure 8-11 Vulture Trouble main screen and summary screen

  42. Chapter 8The Vulture Trouble Game: Introducing Audio, Physics, and Text Effects QUESTIONS ?

More Related