| Windows Phone 7 Sensors and XNA | |
| Camera Task: XNA Camera Demo | |
| Back to main page |
Release 1.0 (XNA V4.0) |
Pre-requisite: this tutorial assumes you have read through the Pinch Zoom Touch Panel Tutorial, in particular,
Behavior:
Note: at the time of writing (Sep 15th 2010), this program has only been tested on the WP7 emulator.
You will see screens similar to the following when you compile the attached source code, dispatch to WP7 device/emulator, and run the program.
Tap the screen to bring up the Photo Choose program as follow:![]()
Select any image/photograph, and the control will return to the tutorial with the selected image as the new background. As follows:![]()
As discussed, the solution structure of this tutorial is similar to the solution structure of the Pinch Zoom Touch Panel tutorial, where SimpleLibrary is referenced for the image and font output. The following are the main differences:
The Initialize() function:![]()
The above listing shows a structure similar to that from the structure of the Pinch Zoom Touch panel tutorial. The main difference is in the instance variables support (at Label E) for the Camera and Photo Chooser Tasks:
- PhotoChooserTask and CameraCaptureTask: conceptually, thses are references to threads that we can invoke at run time.
- mLoadCount: is a simple count of how many times we have invoked the camera or photo chooser tasks.
- mTaskStarted: this boolean indicates a task has already been started and ensures we do not attempt to start the same task more than once based on the same user action.
The Update() Function:![]()
The above listing shows the initialization of the Tasks are performed in the Initialize() function:
- Label I1: initialize gesture input for user interaction.
- Label I2 and I3: instantiate the tasks. In each case, since the tasks will be running asynchronously, we register event service routines to receive the results from these tasks: PhotoChosen(), and CamerDoneEvent() (these functions are implemented in the CameraDemo_Compute.cs file and will be described later in this tutorial: here.
![]()
The above listing shows that, with the tasks properly initialized, during update, we must check for users action (Label U1), move the little airplane (Label U2), and detect the quit condition (Label U3). The following listings focus on the code under each of labels U1 and U2.
![]()
mEffect.Play() plays a SoundEffect cue. Task.Play() is like an actual function call where the target task (phone or photo chooser) begins execution and takes over the application screen. In the mean time, our application looses control over the phone screen and thus will wait for the target task to complete and return the control of the phone screen . By setting mTaskStarted to true, we filter out input device noises that may result in a single user action invoking the same task multiple times. Notice in the above code, we never reset the value of mTaskStarted back to false. This variable will be reset by the routines that service the "task completed" event.
![]()
Simple code to move the airplane right-ward.
RetrivePhoto()![]()
This file implements the event service routine when the invoked tasks have completed. The following explains each function in this file according to their order in the above listing.
CameraDoneEvent()![]()
Simple construct a message (for Draw() to display) displaying the status of the photo retrieval. If successful, use the image name (file name) to create a Texture2D object to be used as the background image.
PhotoChooserDone()![]()
This is the actual routine that services the "Camera Task Completed" event. Once again, this routine is invoked asynchronously from a different thread so all it does is create a thread-safe way of calling RetrivePhoto().
![]()
Same as above, this is the event service routine for the "Photo Chooser Task Completed" event .
|
This work is supported in part by Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531, and Microsoft Higher Education. |
| 9/15/2010 | |