Similar presentations

2
Syllabus 1) First program and introduction to data types and control structures with applications for games learning how to use the programming environment Mar 25-27 2) Objects, encapsulation, abstract data types, data protection and scope April 1-3 3) Basic data structures and how to use them, opening files and performing operations on files – April 8-10 4) Algorithms on data structures, algorithms for specific tasks, simple AI and planning type algorithms, game AI algorithms April 15-17 Project 1 Due – April 17 5) More AI: search, heuristics, optimization, decision trees, supervised/unsupervised learning – April 22-24 6) Game API and/or event-oriented programming, model view controller, map reduce filter – April 29, May 1 7) Basic threads models and some simple databases SQLite May 6-8 8) Graphics programming, shaders, textures, 3D models and rotations May 13-15 Project 2 Due May 15 9) How to download an API and learn how to use functions in that API, Windows Foundation Classes May 20-22 10) Designing and implementing a simple game in C++ May 27-29 11) Selected topics – Gesture recognition & depth controllers like the Microsoft Kinect, Network Programming & TCP/IP, OSC June 3-5 12) Working on student projects - June 10-12 Final project presentations Project 3/Final Project Due June 12

3
Using the Virtual JoyStick and Keyboard directional controls with App Game Kit

4
8.3 Virtual Joysticks What is a virtual joystick? –A simulated joystick that you can display in your program and that the user can interact with How many can you create? –The AGK allows you to create up to 4 virtual joysticks How do you use them? –Virtual joysticks are controlled using the mouse or other pointing device

5
8.3 Virtual Joysticks How do you add a virtual joystick? –Call the agk::AddVirtualJoystick function –Passing the following arguments: The index number you want to assign the virtual joystick The virtual joystick’s center X-coordinate The virtual joystick’s center Y-coordinate The virtual joystick’s size (diameter of a circle) –For example: agk::AddVirtualJoystick(1,50,50,50);

7
8.3 Virtual Joysticks How do you change a virtual joystick’s position? –Call agk::SetVirtualJoystickPosition –Passing the following arguments: The virtual joystick’s index number The virtual joystick’s new center X-coordinate The virtual joystick’s new center Y-coordinate –For example: –agk::SetVirtualJoystickPosition(1,100,100);

8
8.3 Virtual Joysticks How do you change a virtual joystick’s size? –Call agk::SetVirtualJoystickSize Passing the following arguments: The virtual joystick’s index number The virtual joystick’s new size –For example: agk::SetVirtualJoystickSize(1,200);

9
8.3 Virtual Joysticks How do you change the transparency of a virtual joystick? –Call agk::SetVirtualJoystickAlpha Passing the following arguments: The virtual joystick’s index number A value (0 – 255) for the alpha channel –For example: agk::SetVirtualJoystickAlpha(1,255);

10
8.3 Virtual Joysticks How do you enable / disable a virtual joystick? –Call agk::SetVirtualJoystickActive –Passing the following arguments: The virtual joystick’s index number A value indicating if the virtual joystick is to be active –0 will set the virtual joystick as inactive –1 will activate the inactive virtual joystick For example: –agk::SetVirtualButtonActive(1,0);// disable –agk::SetVirtualButtonActive(1,1);// enable

11
8.3 Virtual Joysticks How do you hide or show a virtual joystick? –Call agk::SetVirtualJoystickVisible –Passing the following arguments: The virtual joystick’s index number A value indicating virtual joystick’s visibility –0 will hide the virtual joystick (but it remains active) –1 will show the previously hidden virtual joystick For example: –agk::SetVirtualJoystickVisible(1,0);// hide –agk::SetVirtualJoystickVisible(1,1);// show

12
8.3 Virtual Joysticks How do you change a virtual joystick’s images? –Make sure the images you want to use are located in the My Documents  AGK  template folder –Load the new images and then call the following functions to apply the changes: agk::SetVirtualJoystickImageOuter –Pass the index number of the virtual joystick –Pass the index number of the virtual joystick’s outer image agk::SetVirtualJoystickImageInner –Pass the index number of the virtual joystick –Pass the index number of the virtual joystick’s inner image

13
8.3 Virtual Joysticks –Here is a summary of the steps you must take to change a virtual joystick’s images: Load the new inner and outer joystick images Set the virtual joystick’s new outer image Set the virtual joystick’s new inner image –For example:

14
8.3 Virtual Joysticks How do you delete an existing virtual joystick? –Determine if the virtual joystick exists and delete it First, call agk::GetVirtualJoystickExists –Passing the index number of the joystick you want to check –Returns 1 if the joystick exists or 0 if it does not exist Then call agk::DeleteVirtualJoystick –Passing the index number of the joystick you want to delete For example:

15
8.3 Virtual Joysticks –What is a virtual joystick’s dead zone? Area around the joystick’s center that affects the distance you have to move the joystick before it registers input –How do you change a joystick’s the dead zone? Call agk::SetVirtualJoystickDeadZone –Passing the virtual joystick’s index number –A floating-point value between 0 and 1for the dead zone »A value of 0 would be very sensitive »A value of 1 would totally disable the joystick »The default value is 0.15 –For example: »agk::SetVirtualJoystickDeadZone(1,0.25);

16
8.3 Virtual Joysticks How do you get a virtual joystick’s input? –For the X-axis, call agk::GetVirtualJoystickX –For the Y-axis, call agk::GetVirtualJoystickY What parameters do these functions accept? –The virtual joystick’s index number What values do these functions return? –A floating-point value from -1.0 to 1.0 or 0 if not moving What causes the return values to be different? –Positive values are returned when moving down or right –Negative values are returned when moving up or left –A value of zero is returned if the joystick is in the dead zone

21
8.4 The Keyboard How do you move objects with the keyboard arrow keys? –For the X-axis, call agk::GetDirectionX –For the Y-axis, call agk::GetDirectionY What values do these functions return? –A floating-point value from -0.9 to 0.9 or 0 if not pressed What causes the return values to be different? –Positive values are returned when pressing down or right –Negative values are returned when pressing up or left –Program 8-6, for example

23
8.4 The Keyboard –How do you respond to specific key presses? Similar to responding to virtual button and mouse presses Call any one of the following three functions: –agk::GetRawKeyPressed (Was the key pressed?) –agk::GetRawKeyState (Was the key held down?) –agk::GetRawKeyReleased (Was the key released?) All three functions accept a single argument: –A value (0 – 255) representing the key code for the key –For example:

24
8.4 The Keyboard –How do you know which key code values to use? Many of the key codes are defined by the AGK –AGK defined key codes start with AGK_KEY_

25
8.4 The Keyboard How do you determine the last key that was pressed? –Call the agk::GetRawLastKey function Returns the key code for the last key that was pressed –Program 8-7, for example

30
Function Templates Function template: a pattern for a function that can work with many data types When written, parameters are left for the data types When called, compiler generates code for specific data types in function call

35
Function Template Notes All data types specified in template prefix must be used in template definition Function calls must pass parameters for all data types specified in the template prefix Like regular functions, function templates must be defined before being called

36
Function Template Notes A function template is a pattern No actual code is generated until the function named in the template is called A function template uses no memory When passing a class object to a function template, ensure that all operators in the template are defined or overloaded in the class definition

37
Where to Start When Defining Templates Templates are often appropriate for multiple functions that perform the same task with different parameter data types Develop function using usual data types first, then convert to a template: –add template prefix –convert data type names in the function to a type parameter (i.e., a T type) in the template

38
Class Templates Classes can also be represented by templates. When a class object is created, type information is supplied to define the type of data members of the class. Unlike functions, classes are instantiated by supplying the type name ( int, double, string, etc.) at object definition

43
Standard Template Library Two important types of data structures in the STL: –containers: classes that stores data and imposes some organization on it –iterators: like pointers; mechanisms for accessing elements in a container

44
Containers Two types of container classes in STL: –sequence containers: organize and access data sequentially, as in an array. These include vector, dequeue, and list –associative containers: use keys to allow data elements to be quickly accessed. These include set, multiset, map, and multimap

45
Iterators Generalization of pointers, used to access information in containers Four types: –forward (uses ++ ) –bidirectional (uses ++ and -- ) –random-access –input (can be used with cin and istream objects) –output (can be used with cout and ostream objects)

48
Exceptions Indicate that something unexpected has occurred or been detected Allow program to deal with the problem in a controlled manner Can be as simple or complex as program design requires

49
Exceptions - Terminology Exception: object or value that signals an error Throw an exception: send a signal that an error has occurred Catch/Handle an exception: process the exception; interpret the signal

50
Exceptions – Key Words throw – followed by an argument, is used to throw an exception try – followed by a block { }, is used to invoke code that throws an exception catch – followed by a block { }, is used to detect and process exceptions thrown in preceding try block. Takes a parameter that matches the type thrown.

51
Exceptions – Flow of Control 1)A function that throws an exception is called from within a try block 2)If the function throws an exception, the function terminates and the try block is immediately exited. A catch block to process the exception is searched for in the source code immediately following the try block. 3)If a catch block is found that matches the exception thrown, it is executed. If no catch block that matches the exception is found, the program terminates.

54
Exceptions – What Happens 1) try block is entered. totalDays function is called 2)If 1st parameter is between 0 and 7, total number of days is returned and catch block is skipped over (no exception thrown) 3)If exception is thrown, function and try block are exited, catch blocks are scanned for 1 st one that matches the data type of the thrown exception. catch block executes

59
Exceptions - Notes Predefined functions such as new may throw exceptions The value that is thrown does not need to be used in catch block. –in this case, no name is needed in catch parameter definition –catch block parameter definition does need the type of exception being caught

60
Exception Not Caught? An exception will not be caught if –it is thrown from outside of a try block –there is no catch block that matches the data type of the thrown exception If an exception is not caught, the program will terminate

61
Exceptions and Objects An exception class can be defined in a class and thrown as an exception by a member function An exception class may have: –no members: used only to signal an error –members: pass error data to catch block A class can have more than one exception class

69
What Happens After catch Block? Once an exception is thrown, the program cannot return to throw point. The function executing throw terminates (does not return), other calling functions in try block terminate, resulting in unwinding the stack If objects were created in the try block and an exception is thrown, they are destroyed.

70
Nested try Blocks try/catch blocks can occur within an enclosing try block Exceptions caught at an inner level can be passed up to a catch block at an outer level: catch ( ) {... throw; // pass exception up } // to next level