Chapter 7 Using the App Game Kit with C++ ITP 134 C++ Study Guide Chapter 7 Using the App Game Kit with C++ 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 7.1 The App Game Kit and the Structure of a Game Program CONCEPT: The App Game Kit, or AGK, is a commercial programming tool that you can use to write games in C++. It provides a programming template that supports the typical structure of a game program. We are using Tier2 in this class. This product provides the AGK programming language, and a C++ library that works with Microsoft Visual Studio. The Structure of a Typical Game Program The game programs are structured into 3 general phases or sections of your program (page 256) 1. Initialization: The phase occurs when the program starts and only executes once. The program loads resources needed such as graphics, images, sound files and set ups for the game play. 2. Game Loop: The game loop repeats continuously until the game is over. During each iteration, the program gets input from the user, moves objects on the screen. When the game is over the loop stops. 3. Shutdown: When the game is over, the program saves any data that must be kept (such as the user’s score) and then the program ends. Basic structure of a C++ program using the App Game Kit library (page 257) // includes section #include “template.h” using namespace AGK; App App; void app::Begin(void) { // Initialization code here { void app::Loop(void) { // Game Loop code here { void app::End(void) { // Shutdown code here { Creating an AGK Project for Visual C++ Follow the instructions given during class time to copy the template_vs10 into the correct folders, compile and run your programs. (page 259) App Game Kit Online Documentation See the links on the class web site for documentation ITP 134 – Mrs. Eaton Chapter 7 Study Guide – 2nd Edition Page 1 Chapter 7 Using the App Game Kit with C++ 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. 7.2 The Screen Coordinate System CONCEPT: A system of X- and Y-coordinates is used to identify the locations of pixels in a window. (page 262) 1. Default Screen Method: Uses a percentage based system starts at (0.0) in upper left and ends at (100,100) in bottom-right corner of the screen. (page 263) 2. Virtual Resolution Method: Based on the screen size of the window such as 640 wide by 480 high. The system starts at (0,0) in upper left and ends at (639, 479) at bottom-right. (page 263) Screen Functions Function Page Purpose Parameters or Arguments Example Program SetVirtualResolution (width,height); 264 Sets the virtual resolution of the program window. width and height are the screen sizes in pixels 7-1 Virtual Resolution Setup (pg 265) Sync(); 267 Display the current buffer to the screen. n/a 7-2 Sprite Demo (pg 267-8) SetSyncRate(fps, mode); 291 Set the sync rate or frame rate for the loop function execution Float Fps = frames per second integer modes: 0 = conserves power; good for mobile devices 1 = More CPU power, more accurate frame rate 7-2 Sprite Demo (pg 267-8) SetClearColor (red, green, blue); 297 Specify a color to fill the screen. Red, green, blue from 0-255 where 255 is full color ClearScreen(); 297 Clear the screen and fill with current color. Must specify color with SetClearColor first. SetWindowTitle (“String”); 305 Display text in the window’s title bar. String can be a variable or a literal. 7.3 Displaying Sprites CONCEPT: A sprite is a graphic image that is displayed in a game. (page 266) ITP 134 – Mrs. Eaton Chapter 7 Study Guide – 2nd Edition Page 2 Chapter 8 Input, Animation and Sound Sprite Functions Note: All the sprint functions use an index. This refers to the unique number associated with each sprite in the current program. Function Page Purpose Parameters or Arguments Example Program CreateSprite (Index,”ImageFile”); 264 ImageFile is the name of the image file. Default location is (0,0) 7-2 Sprite Demo (pages 267-8) SetSpriteDepth(Index, Depth); 272 Creates a sprite from an image file. Supports .png, .jpg or .bmp formats. Graphic file must be located in the Final folder of the project. Book is out of date. Change the drawing order of sprites drawn on the screen. Sprites are drawn on the screen/buffer in the order created in the program. Depth range from 0-1000. Depth 0 is closest to the front of screen. 7-5 Sprite Depth (page 273) SetSpritePosition (Index, X, Y); 273 Change the location of a sprite. By default, sprites are positioned in the (0,0) top left corner of the screen. (X,Y) are the coordinate position of the upper left corner of the sprite 7-6 Sprite Position (pages 274-5) SetSpriteX(Index, X); 275 Reset only the X or Y coordinate position of a sprite. This is useful to animate or move a sprite across a screen. 7-10 Loop Move Sprite (pages 289-91) 276 Get the current X and Y location of any existing sprite. 276 Get the current width and height of any existing sprite. SetSpriteScale(Index, Xscale, Yscale); 276 Scale the size of an existing sprite. X or Y are the coordinate position of the upper left corner of the sprite. Returns float X or Y variables as the coordinate position of the upper left corner of the sprite Returns the float width and height values in pixels. Xscale and Yscale are float values. To double the size use 2, use .5 to reduce to half original size. SetSpriteAngle(Index, Angle); 277 Rotate a sprite. Angle is a float value for degrees of rotation. GetSpriteAngle(Index); 277 Get the angle of rotation of a sprite. Returns the float value for the angle of rotation. ITP 134 – Mrs. Eaton Chapter 7 Study Guide – 2nd Edition SetSpriteY(Index, Y); GetSpriteX(Index); GetSpriteY(Index); GetSpriteWidth(Index); GetSpriteHeight(Index); 7-11 Game State (pages 292-3) 7-9 Spinning Sprite (page 287) Page 3 Chapter 8 Input, Animation and Sound More Sprite Functions Function Page Purpose Parameters or Arguments Example Program SetSpriteFlip(Index, Horizontal, Vertical); 278 Flips a sprite either horizontally or vertically. If Horizontal = 1, then flip horizontally. If Vertical is 1, then flip vertically. If 0, don’t flip. SetSpriteVisible(Index, Visible); 278 Hide or show a sprite. CloanSprite(Index, CloneIndex); 279 Make a clone copy of an existing sprite. You can change the original sprite without affecting the cloans. LoadImage (imageIndex, “filename”, black) 281 Load an image to use black as the alpha color. Visible = 1 show sprite Visibile = 0 hide sprite CloanIndex is the sprite number to assign to the cloan sprite imageIndex = image index (not sprite index) filename = name of image file black = 1 if black transparent DeleteSprite(Index); 284 GetSpriteExists(Index); 284 Delete a sprite from memory. Memory management. Determine if a sprite exists. 7-7 Dog the Beach (pages 282-3) 7.4 Working in the Game Loop CONCEPT: Most of your program’s work will be done in the app::Loop function, which is the game loop of an AGK application. (page 286) 7.5 Working with Colors and Transparency CONCEPT: The AGK uses the RGB (red, green and blue) system to specify colors for pixels. The pixels of images that are stored in .png files also have a fourth component, known as the alpha channel that specifies transparency. Function Page Purpose SetSpriteColorRed (Index, red); 297 Apply a color to an existing sprite giving it a tint. SetSpriteColorAlpha (Index, Alpha); 298 Specify the transparency of a sprite. ITP 134 – Mrs. Eaton Chapter 7 Study Guide – 2nd Edition SetSpriteColorGreen (Index, green); Parameters or Arguments Red, green, blue from 0-255 where 255 is full color Example Program SetSpriteColorBlue (Index, blue); Alpha range 0-255. 0 is transparence and 255 is opaque. Page 4 GetSpriteColorRed (Index); 299 GetSpriteColorGreen (Index); Chapter 8 Input, Animation and Sound Get the sprite’s current red, Returns the integer green or blue values. color values 0-255 where 255 is full color GetSpriteColorBlue (Index); 7.6 Displaying Text in the AGK Window CONCEPT: You can use the ack::Print and agk::PrintC functions to display text in the AGK window. Function Page Purpose Print(intValue); Print(floatValue); Print(“String”); 300 Print a line of output to the window. Includes a newline character at the end of output. PrintC(intValue); PrintC(floatValue); PrintC(“String”); 301 Print a line of output to the window. Does NOT Include a newline character at the end of output. Parameters or Arguments Can print either variables or use literal values. Example Program 7-12 Print Demo (pages 300-301) Can print either variables or use literal values. 7-13 Print Demo (pages 302-303) 7-14 AGK Print String (page 303-4) 7.7 Generating Random Numbers CONCEPT: Random numbers are used in many applications, including games. The AGK provides a function named agk::Random that generates random numbers. (page 305) Function Page Purpose Random(Min, Max); 306 Generates a random number Parameters or Arguments Random numbers are generated within the range of the min – max values Example Program 7-15 Random Position And Alpha (pages 307-9) Chapter 7 Program Examples Program 7-1 VirtualResolutionSetUp (pg 265) See VideoNote: Program 7-2 SpriteDemo (pg 267-268) Program 7-3 BeachBackground (pg 270) Program 7-4 HauntedHouse (pg 271) Program 7-5 SpriteDepth (pg 273) Program 7-6 SpritePosition (pg 274-275) Program 7-7 DogAtTheBeach (pg 282) Program 7-8 ImageSubFolderDemo (pg 284285) Program 7-9 SpinningSprite (pg 287-288) Program 7-10 LoopMoveSprite (pg 289-290) Program 7-11 GameState(pg 292-294) Program 7-12 PrintDemo (pg 300-301) Program 7-13 PrintCDemo (pg 302) ITP 134 – Mrs. Eaton Chapter 7 Study Guide – 2nd Edition Program 7-14 AGKPrintString (pg 303-304) In the Spotlight: Using Random Numbers to Position a Sprite and Set its Transparency Program 7-15 RandomPositionAndAlpha (pg 307-308) Page 5