Chapter 8 Input, Animation and Sound

ITP 134 C++ Study Guide

Chapter 8 Input, Animation and Sound

Instructions: Use this Study Guide to help you understand the important points of this chapter. See the book for lots of great programming examples. Important concepts, keywords, statements and functions are shown in emphasized font.
Code syntax and examples are shown in code font.


Games & Graphics in C++
2nd edition by Tony Gaddis

Note: All the AGK statements have a prefix of agk:: so this is assumed for all of the following AGK functions when you use them in a program.

ITP 134 – Mrs. Eaton Chapter 8 Study Guide – 2nd Edition Page 5

Chapter 8 Input, Animation and Sound

8.1 The Mouse

CONCEPT: The AGK provides functions that you can use to work with the mouse. These functions allow you to track the mouse pointer’s location on the screen and determine when the user is pressing one of more of the mouse buttons. (page 315)

Mouse Functions

Function / Page / Purpose / Parameters or Arguments / Example Program
GetRawMouseX(); / 315 / Returns the X-coordinate position of the mouse pointer. / Returns X-coordinate as float variable / 8-1 Sprite Mouse (page 316)
GetRawMouseY(); / 315 / Returns the Y-coordinate position of the mouse pointer. / Returns Y-coordinate as float variable / 8-1 Sprite Mouse (page 316)
SetRawMouse
Visible(Visible); / 317 / Controls the mouse pointer’s visibility / 0 = invisible
1 = visible / n/a
GetRawMouse
LeftPressed();
/ 317 / Determines if the left mouse button has been pressed / Returns:
1 if left button pressed
0 otherwise / 8-2 Mouse Presses (page 318)
GetRawMouse
RightPressed();
/ 317 / Determines if the right mouse button has been pressed / Returns:
1 if right button pressed
0 otherwise / 8-2 Mouse Presses (page 318)
GetSpriteHit
(x, y); / 320 / Determines if a sprite has been hit by the mouse. / Returns the index of the hit sprite or 0 otherwise / 8-3 Virtual Light Switch (page 320-22)

8.2 Virtual Buttons

CONCEPT: The AGK allows you to create up to 12 virtual buttons on the screen. Virtual buttons can be used to determine when the user clicks on them with the mouse. (page 322)

Note: All of these AGK functions have an index. This index refers to the unique index for a virtual button not a sprite.

Function / Page / Purpose / Parameters or Arguments / Example Program
AddVirtualButton
(index, x, y, size); / 323 / Add a virtual button to the screen at the x,y position. / index = button index
x,y = center of button
size = diameter of button / 8-4 Virtual Buttons (page 326-8)
SetVirtualButtonPosition
(index, x, y); / 323 / Moves a virtual button to the screen at the x,y position. / index = button index
x,y = center of button / 8-4 Virtual Buttons (page 326-8)
SetVirtualButtonSize
(index, size); / 324 / Resizes a virtual button. / index = button index
size = diameter of button / n/a
SetVirtualButtonText
(index, “text”); / 324 / Displays text on top of a virtual button / index = button index
text = text to display on button / n/a
SetVirtualButtonColor
(index, red, green, blue); / 324 / Change the color of a virtual button / index = button index
red, green, blue from 0-255 where 255 is full color intensity / n/a
SetVirtualButtonAlpha
(index, alpha); / 324 / Changes the transparency level of a virtual button / index = button index
alpha = 0 – 255 where 0 is invisible and 255 is completely opaque / n/a
SetVirtualButtonVisible
(index, visible); / 325 / Hide or show a virtual button / index = button index
visible =
0 = hide button
1 = show button / n/a
SetVirtualButtonActive
(index, active); / 325 / Enable or disable a virtual button / index = button index
active =
0 = activate button
1 = disable button / n/a
SetVirtualButtonUp
(index, imageIndex); / 325-6 / Set the image for a virtual button for the up state / index = button index
imageIndex = image index / n/a
SetVirtualButtonDown
(index, imageIndex); / 325-6 / Set the image for a virtual button for the down state / index = button index
imageIndex = image index / n/a
GetVirtualButton
Pressed(index);
/ 326 / Determines if a virtual button has been pressed / Returns 1 when user initially presses, then 0 / 8-2 Mouse Presses (page 318)
GetVirtualButton
State(index);
/ 326 / Determines if a virtual button has been pressed / Returns 1 as long as button is held down, otherwise 0 / n/a
GetVirtualButton
Released(index);
/ 326 / Determines if a virtual button has been pressed / Returns 1 when user initially releases button, then 0 / n/a

8.3 Virtual Joysticks

CONCEPT: The AGK provides up to 4 virtual joysticks that you can use to control sprites or other items in a game. Virtual joysticks are controlled using the mouse or other pointing device. (page 329)

Note: All of these AGK functions have an index. This index refers to the unique index for a virtual joystick.

Function / Page / Purpose / Parameters or Arguments / Example Program
AddVirtualJoystick
(index, x, y, size); / 329 / Add a virtual Joystick to the screen at the x,y position. / index = joystick index
x,y = center of Joystick
size = diameter of Joystick / 8-5 Virtual Joysticks (page 333-4)
SetVirtualJoystickPosition
(index, x, y); / 329 / Moves a virtual Joystick to the screen at the x,y position. / index = joystick index
x,y = center of Joystick / n/a
SetVirtualJoystickSize
(index, size); / 330 / Resizes a virtual Joystick. / index = joystick index
size = diameter of Joystick / n/a
SetVirtualJoystickAlpha
(index, alpha1, alpha2); / 330 / Changes the transparency level of a virtual Joystick.
Alpha levels 0 – 255 where 0 is invisible and 255 is / index = joystick index
completely opaque
alpha1=outer ring
alpha2=inner ring / n/a
SetVirtualJoystickVisible
(index, visible); / 331 / Hide or show a virtual Joystick / index = joystick index
visible =
0 = hide Joystick
1 = show Joystick / n/a
SetVirtualJoystickActive
(index, active); / 330 / Enable or disable a virtual Joystick / index = joystick index
active =
0 = activate Joystick
1 = disable Joystick / n/a
SetVirtualJoystickOuter
(index, imageIndex); / 331 / Set the image for a virtual Joystick for the outer ring / index = Joystick index
imageIndex = image index / n/a
SetVirtualJoystickInner
(index, imageIndex); / 331 / Set the image for a virtual Joystick for the inner ring / index = Joystick index
imageIndex = image index / n/a
GetVirtualJoystickExists
(index);
/ 332 / Determines if a virtual Joystick currently exists / index = Joystick index Returns 1 if exists;
0 otherwise / n/a
DeleteVirtualJoystick
State(index);
/ 332 / Deletes a virtual joystick and removes it from the screen / index = Joystick index / n/a
SetVirtualJoystickDeadZone
(deadzone); [correction]
/ 332-3 / Sets the dead zone area around all joysticks that determines how far you need to move it before it registers input / deadzone =
1 = disable the joystick
0 = very sensitive joystick
.15 is default value / n/a
GetVirtualJoystickX
(index); / 333 / Gets the x-coordinate position of a virtual joystick / index = joystick index
Returns x-coordinate / 8-5 Virtual Joysticks (page 333-4)
GetVirtualJoystickY
(index); / 333 / Gets the y-coordinate position of a virtual joystick / index = joystick index
Returns y-coordinate / 8-5 Virtual Joysticks (page 333-4)

8.4 The Keyboard

CONCEPT: The AGK provides functions that let you know whether certain keys are being pressed on the keyboard. Many games and animation programs allow the user to control objects on the screen with such keys. (page 336)

Note: All of these AGK functions have an index. This index refers to the unique index for a virtual joystick.

Moving Objects with the Keyboard Arrow Keys

Function / Page / Purpose / Parameters or Arguments / Example Program
GetDirectionX; / 337 / Determine which arrow key is being pressed / Returns the following:
0 if no key is pressed
negative if left arrow key
positive if right arrow key
range from -0.9 to 0.9 / 8-6 Direction Keys (page 337)
GetDirectionY; / 337 / Determine which arrow key is being pressed / Returns the following:
0 if no key is pressed
negative if down arrow key
positive if up arrow key
range from -0.9 to 0.9 / 8-6 Direction Keys (page 337)
GetRawKeyPressed; / 337 / Determines if a specified key has been pressed / Returns the following:
1 if specified key was pressed
Then returns 0 / 8-7 Last Key Pressed (page 339-40)
GetRawKeyboardState; / 337 / Determines if a specified key has been held down / Returns the following:
1 when the specified key is held down, otherwise returns 0 / 8-7 Last Key Pressed (page 339-40)
GetRawKeyReleased; / 337 / Determines if a specified key has been released / Returns the following:
1 if specified key was released
Then returns 0 / 8-7 Last Key Pressed (page 339-40)

Common Key Codes defined by AGK

See Table 8-1 on page 338

CONSTANT NAME KEY
ACK_KEY_UP Up Arrow Key
ACK_KEY_DOWN Down Arrow Key
ACK_KEY_LEFT Left Arrow Key
ACK_KEY_RIGHT Right Arrow Key
ACK_KEY_SPACE Spacebar Key
ACK_KEY_TAB Tab Key
ACK_KEY_ENTER Enter Key


8.5 Animation

CONCEPT: You can create a simple animation by displaying a sequence of images one after the other. This can be done by manually loading and displaying separate images or via a texture atlas, which contains all the animation frames in a single image. (page 340)

Function / Page / Purpose / Parameters or Arguments / Example Program
LoadImage
(imageIndex, “filename”, black) / 281 / Load an image to use black as the alpha color. / indexImage = index of image file
filename = name of image file
black = 1 if black transparent / 8-6 CelAnimation (page 341-3)

Playing Sprite Animations with the AGK

Function / Page / Purpose / Parameters or Arguments / Example Program
SetSpriteAnimation
(spriteIndex, frameWidth,
FrameHeight,
FrameCount) / 345-346 / Set up an a sprite animation using images in a “texture atlas”, or animated sprite. / spriteIndex = number of sprite
frameWidth = width in pixels
frameHeight = height in pixels
frameCount = number of frames / 8-9 Clock Texture Atlas (page 346-347)
PlaySprite
(spriteIndex, fps, loop,fromFrame,
toFrame) / 346 / Play a sprite animation using images in a “texture atlas”, animated sprite. Need to set up first. / spriteIndex = number of sprite
fps = frames per second
loop = 1 to loop; 0 otherwise
fromFrame = starting frame
toFrame = ending frame / 8-9 Clock Texture Atlas (page 346-347)

8.6 Music and Sound

CONCEPT: The ACK lets you play audio files that have been saved in the MP3 and WAV formats. The library provides numerous functions for working with audio files.

Function / Page / Purpose / Parameters or Arguments / Example Program
LoadSound
(soundNumber, “filename”) / 348 / Load a sound file into memory so you can play it later. / soundNumber = number of sound
filename = wav or mp3 filename / 8-10 Play Sound (page 349)
PlaySound
(soundNumber) / 348 / Play a previously loaded sound file. / soundNumber = number of sound / 8-10 Play Sound (page 349)
StopSound
(soundNumber) / 351 / Stop a sound file currently playing. / soundNumber = number of sound / 8-11 Sprite Sounds(page 350-1)
LoadMusic
(musicNumber, “filename”) / 352 / Load a music file into memory so you can play it later. Good for background music. / musicNumber = number of music
filename = mp3 filename / 8-10 Play Music (page 352-3)
PlayMusic
(soundNumber) / 352 / Play a previously loaded music file. / musicNumber = number of music / 8-10 Play Music (page 352-3)
Timer(); [Added] / 354 / Use timer to measure in seconds. / Returns the number of elapsed seconds since the program started or the timer was reset. / 8-13 Bug Zapper
(page 354-361)
ResetTimer(); [Added] / 354 / Resets the game timer / Resets the game timer. / 8-13 Bug Zapper
(page 354-361)

8.7 Bug Zapper Game

·  The Bug Zapper game displays an animated sprite of a bug. The user zaps the bug by clicking it with the mouse. When this happens, a new bug appears at a random location on the screen. The game will run for 10 seconds before ending. The object of the game is to zap as many bugs as possible within the allotted time. When the game ends, a screen displays the number of bugs that were zapped.

·  Game Functions

1.  generateNewBug - Generates a new bug at a random location

2.  displayIntro – displays the intro screen and hides the grass and bug sprites

3.  hideIntro – hides the intro screen and makes the grass and bug sprites visible

4.  playGame – processes a frame of game play

5.  closingScreen – displays the closing screen

ITP 134 – Mrs. Eaton Chapter 8 Study Guide – 2nd Edition Page 5