Home Mobile PlayStation Mobile Development Cookbook

PlayStation Mobile Development Cookbook

By Michael Fleischauer
books-svg-icon Book
eBook $32.99 $22.99
Print $54.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $32.99 $22.99
Print $54.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Getting Started
About this book
With the PlayStation®Mobile SDK you can create stunning games for the PlayStation®Vita and PlayStation™Certified devices (PS Certified devices). It includes everything you need to get started, including an IDE for developing your code and even an emulator to test your creations. "PlayStation®Mobile Development Cookbook"| is an exciting and practical collection of recipes that help you make the most of this exciting new platform. It provides you with everything you need to create complete 2D or 3D games and applications that fully unlock the potential of the SDK. After quickly covering the basics, you'll learn how to utilize input sources like touch, gamepads, and motion controls, and then move on to more advanced content like creating and animating 2D graphics, networking, playing sound effects and music, adding physics, and then finally jumping into the world of 3D.
Publication date:
March 2013
Publisher
Packt
Pages
322
ISBN
9781849694186

 

Chapter 1. Getting Started

In this chapter we will cover:

  • Accessing the PlayStation Mobile portal

  • Installing the PlayStation Mobile SDK

  • Creating a simple game loop

  • Loading, displaying, and translating a textured image

  • "Hello World" drawing text on an image

  • Deploying to PlayStation Mobile certified Android devices

  • Deploying to a PlayStation Vita

  • Manipulating an image dynamically

  • Working with the filesystem

  • Handling system events

 

Introduction


The PlayStation Mobile (PSM) SDK represents an exciting opportunity for game developers of all stripes, from hobbyists to indie and professional developers. It contains everything you need to quickly develop a game using the C# programming language. Perhaps more importantly, it provides a market for those games. If you are currently using XNA, you will feel right at home with the PSM SDK.

You may be wondering at this point, Why develop for PlayStation Mobile at all? Obviously, the easiest answer is, so you can develop for PlayStation Vita, which of itself will be enough for many people. Perhaps, though the most important reason is that it represents a group of dedicated gamers hungry for games. While there are a wealth of games available for Android, finding them on the App Store is a mess, while supporting the literally thousands of devices is a nightmare. With PlayStation Mobile, you have a common development environment, targeting powerful devices with a dedicated store catering to gamers.

We are now going to jump right in and get those tools up and running. Of course, we will also write some code and show how easy it is to get it running on your device. PlayStation Mobile allows you to target a number of different devices and we will cover the three major targets (the Simulator, PlayStation Vita, and Android). You do not need to have a device to follow along, although certain functionality will not be available on the Simulator.

One thing to keep in mind with the PlayStation Mobile SDK is that it is essentially two SDKs in one. There is a much lower level set of libraries for accessing graphics, audio, and input, as well as a higher-level layer build over the top of this layer, mostly with the complete source available. Of course, underneath this all there is the .NET framework. In this chapter, we are going to deal with the lower level graphics interface. If the code seems initially quite long or daunting for what seems like a simple task, don't worry! There is a much easier way that we will cover later in the book.

 

Accessing the PlayStation Mobile portal


This recipe looks at creating a PSM portal account. For this process it is mandatory to download and use the PSM SDK.

Getting ready

You need to have a Sony Entertainment Network (SEN) account to register with the PSM portal. This is the standard account you use to bring your PlayStation device online, so you may already have one. If not, create one at http://bit.ly/Yiglfk before continuing.

How to do it...

  1. Open a web browser and log in to http://psm.playstation.net. Locate and click on the Register button.

  2. Sign in using the SEN account.

  3. Agree to the Terms and Conditions. You need to scroll to the bottom of the text before the Agree button is enabled. But, you always read the fine print anyways... don't you?

  4. Finally select the e-mail address and language you want for the PlayStation Mobile portal. You can use the same e-mail you used for your SEN account. Click on Register.

  5. An e-mail will be sent to the e-mail account you used to sign up. Locate the activation link and either click on it, or copy and paste into a browser window:

  6. Your account is now completed, and you can log in to the PSM developer portal now.

How it works...

A PlayStation Mobile account is mandatory to download the PSM tools. Many of the links to the portal require you to be logged in before they will work. It is very important that you create and activate your account and log in to the portal before continuing on with the book! All future recipes assume you are logged in to the portal.

 

Installing the PlayStation Mobile SDK


This recipe demonstrates how to install the PlayStation Mobile SDK.

Getting ready

First you need to download the PlayStation Mobile SDK; you can download it from http://bit.ly/W8rhhx.

How to do it...

  1. Locate the installation file you downloaded earlier and double-click to launch the installer. Say yes to any security related questions.

  2. Take the default settings when prompting, making sure to install the runtimes and GTK# libraries.

  3. The installer for the Vita drivers will now launch. There is no harm in installing them even if you do not have a Vita:

  4. Installation is now complete; a browser window with the current release notes will open.

How it works...

The SDK is now installed on your machines. Assuming you used default directories, the SDK will be installed to C:\Program Files (x86)\SCE\PSM if you are running 64 bit Windows, or to C:\Program Files\SCE\PSM if you are running 32 bit Windows. Additionally all of the documentation and samples have been installed under the Public account, located in C:\Users\Public\Documents\PSM .

There's more...

There are a number of samples available in the samples directory and you should certainly take a moment to check them out. They range in complexity from simple Hello World applications, up to a full blown 3rd person 3D role playing game (RPG). They are, however, often documented in Japanese and often rely on other samples, making learning from them a frustrating experience at times, at least, for those of us who do not understand Japanese!

See also

  • See the A Tour of the PlayStation Mobile SDK section in the Preface for a better understanding of what is included in the SDK you just installed

 

Creating a simple game loop


We are now going to create our first PSM SDK application, which is the main loop of your application. Actually all the code in this sample is going to be generated by PSM Studio for us.

Getting ready

From the start menu, locate and launch PSM Studio in the PlayStation Mobile folder.

How to do it...

  1. In PSM Studio, select the File | New | Solution... menu.

  2. In the resulting dialog box, in the left-hand panel expand C# and select PlayStation Suite, then in the right-hand panel, select PlayStation Suite Application. Fill in the Name field, which will automatically populate the Solution name field. Click on OK.

  3. Your workspace and boilerplate code will now be created; hit the F5 key or select the Run | Start Debugging menu to run your code in the Simulator.

Not much to look at, but it's your first running PlayStation Mobile application! Now let's take a quick look at the code it generated:

using System;
using System.Collections.Generic;
using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Environment;
using Sce.PlayStation.Core.Graphics;
using Sce.PlayStation.Core.Input;

namespace Ch1_Example1
{
  public class AppMain{
    private static GraphicsContext graphics;

    public static void Main (string[] args){
      Initialize ();

      while (true) {
        SystemEvents.CheckEvents ();
        Update ();
        Render ();
      }
    }

    public static void Initialize (){
      graphics = new GraphicsContext ();
    }

    public static void Update (){
      var gamePadData = GamePad.GetData (0);
    }

    public static void Render ()
{
      graphics.SetClearColor (0.0f, 0.0f, 0.0f, 0.0f);
      graphics.Clear ();
      graphics.SwapBuffers ();
    }
  }
}

Tip

Downloading the example code

You can download the example code files for all Packt Publishing books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

How it works...

This recipe shows us the very basic skeleton of an application. Essentially it loops forever, displaying a black screen.

private static GraphicsContext graphics;

The GraphicsContext variable represents the underlying OpenGL context. It is used to perform almost every graphically related action. Additionally, it contains the capabilities (resolution, pixel depth, and so on) of the underlying graphics device.

All C# based applications have a main function, and this one is no exception. Within Main() we call our Initialize() method, then loop forever, checking for events, updating, and finally rendering the frame. The Initialize() method simply creates a new GraphicsContext variable. The Update() method polls the first gamepad for updates (we will cover controls in more detail later).

Finally Render() uses our GraphicsContext variable to first clear the screen to black using an RGBA color value, then clears the screen and swaps the buffers, making it visible. Graphic operations in PSM SDK generally are drawn to a back buffer.

There's more...

The same process is used to create PlayStation Suite library projects, which will generate a DLL file. You can use almost any C# library that doesn't rely on native code (pInvoke or Unsafe); however, they need to be recompiled into a PSM compatible DLL format.

Color in the PSM SDK is normally represented as an RGBA value. The RGBA acronym stands for red, green, blue, and alpha. Each is an int variable type, with values ranging from 0 to 255 representing the strength of each primary color. Alpha represents the level of transparency, with 0 being completely transparent and 256 being opaque.

 

Loading, displaying, and translating a textured image


This recipe is going to create an application that loads a texture from an image file and displays it centered on the screen. This example is actually rather daunting, throwing quite a bit of information at a new developer. Don't worry if it seems overly complex for now; by the end of the book it will make more sense. If you feel overwhelmed, I recommend you continue the book and revisit this recipe later.

Getting ready

Following the instructions presented in the Creating a simple game loop recipe, create a new solution. I have named mine as Ch1_Example2.

How to do it...

  1. First, we need to add an image file to our project to use as a texture. This can be done by right-clicking our project in the Solution panel and selecting Add | Add Files... from the menu, as shown in the following screenshot:

  2. Now, we need to tell PSM Studio what to do with this file. Select our newly added image, right-click on it, and select Build Action | Content.

  3. Now, enter the following code:

    using System;
    using System.Collections.Generic;
    
    using Sce.PlayStation.Core;
    using Sce.PlayStation.Core.Environment;
    using Sce.PlayStation.Core.Graphics;
    using Sce.PlayStation.Core.Input;
    
    namespace Ch1_Example2
    {
      public class AppMain
      {
        private static GraphicsContext _graphics;
        private static Texture2D _texture;
        private static VertexBuffer _vertexBuffer;
        private static ShaderProgram _textureShaderProgram;
        private static Matrix4 _localMatrix;
        private static Matrix4 _projectionMatrix;
        private static Matrix4 _viewMatrix;
        private static float _viewportWidth;
        private static float _viewportHeight;
    
        public static void Main (string[] args){
          Initialize ();
    
          while (true) {
            SystemEvents.CheckEvents ();
            Update ();
            Render ();
          }
        }
    
        public static void Initialize (){
          _graphics = new GraphicsContext ();
          _viewportWidth = _graphics.GetFrameBuffer().Width;
          _viewportHeight = _graphics.GetFrameBuffer().Height;
    
          _texture = new Texture2D("/Application/FA-18H.png",false);
          _vertexBuffer = new VertexBuffer(4,VertexFormat.Float3,VertexFormat.Float2);
          _vertexBuffer.SetVertices(0,new float[] {
            1,0,0,
            _texture.Width,0,0,
            _texture.Width,_texture.Height,0,
            0,_texture.Height,0});
    
          _vertexBuffer.SetVertices(1,new float[]{
            0.0f,0.0f,
            1.0f,0.0f,
            1.0f,1.0f,
            0.0f,1.0f});
    
          _textureShaderProgram = new ShaderProgram("/Application/shaders/Texture.cgx");
    
          _projectionMatrix = Matrix4.Ortho(
            0,_viewportWidth,
            0,_viewportHeight,
            0.0f,32768.0f);
    
          _viewMatrix = Matrix4.LookAt(
            new Vector3(0,_viewportHeight,0),
            new Vector3(0,_viewportHeight,1),
            new Vector3(0,-1,0));
    
          _localMatrix = Matrix4.Translation(new Vector3(-_texture.Width/2,-_texture.Height/2,0.0f))
          * Matrix4.Translation(new Vector3(_viewportWidth/2,_viewportHeight/2,0.0f));
    }
    
        public static void Update (){
          var gamePadData = GamePad.GetData (0);
        }
    
        public static void Render (){
          _graphics.SetClearColor (0.0f, 0.0f, 0.0f, 0.0f);
          _graphics.Clear ();
    
          var worldViewProjection = _projectionMatrix * _viewMatrix * _localMatrix;
          _textureShaderProgram.SetUniformValue(0,ref worldViewProjection);
    
          _graphics.SetShaderProgram(_textureShaderProgram);
          _graphics.SetVertexBuffer(0,_vertexBuffer);
          _graphics.SetTexture(0,_texture);
    
          _graphics.DrawArrays(DrawMode.TriangleFan,0,4);
    
          _graphics.SwapBuffers ();
        }
      }
    }

How it works...

Phew! That sure seemed like a lot of code to simply display a single image on screen, didn't it? The truth is, you did a lot more than just load and draw a texture. Let's jump in and look at exactly what we just created.

First, we declared the following variables, in addition to our existing GraphicsContext variable:

  • _texture is our Texture2D object that is going to hold our textured image.

  • _vertexBuffer is a VertexBuffer object that holds the 3D quad geometry we are going to map our texture on.

  • _shaderProgram is a ShaderProgram variable, the texture shader needed to render our texture. The GraphicsContext variable requires at least one. Fortunately, a simple one with the extension .cgx was created for you already by PSM Studio when you created the project.

  • _localMatrix, _projectionMatrix, and _viewMatrix are Matrix4 objects, representing the textured object's position.

  • _viewportWidth and _viewportHeight contain the dimensions of our window.

The bulk of our activity is in the Initialize() method. Once again, we create a GraphicsContext variable, and then store the dimensions of the frame buffer in the _viewportHeight and _viewportWidth variables. Next, we create our Texture2D object, passing the constructor the filename and whether or not we want a mipmap generated.

Next, we create a _vertexBuffer object, which is going to be a fullscreen quad we can draw our texture on. We make two calls to SetVertices(). The first call is defining the x, y, and z float variables that make up the four vertices of the fullscreen quad. The second SetVertices function call is four x and y texture coordinates. Texture coordinates are represented with a value from 0 to 1.

Next, we create our _textureShaderProgram function using the default shader PSM Studio created for us. We will cover shaders in more detail later in this chapter.

Finally, we set up the _projectionMatrix, _viewMatrix, and _localMatrix objects. The projection matrix is an orthographical matrix that represents our screen. The view matrix represents the camera within the world, using Matrix4.LookAt. LookAt(), which requires 3 vectors, the first representing your eye's location in 3D space, the second, the 3D point you are looking at, and the third, the direction where "UP" is, in this case in the Y direction. Finally, the local matrix represents the position of texture, which we want to be centered in the middle of the screen.

Now, let's take a look at the Render() function, where our texture is going to be displayed to the screen. As before, we set the clear color to black and clear the buffer. Next, we generate our worldViewProjection matrix by multiplying our projection, view and local matrices together. We then bind our worldViewProjection matrix to our shader program and then set our shader program to the GraphicsContext variable. We also set our VertexBuffer object and Texture2D object to the GraphicsContext variable. The DrawArrays() call is what ties it all together, using our worldViewMatrix to transform our vertices from our VertexBuffer object and applying our texture map, rendering it all to the active buffer. Finally, we make that buffer visible, which draws it on screen.

Here is our program in action, rendering our sprite centered to the screen:

Again, if that seemed overly complex, don't panic! Most of this code only needs to be written once, and you have the option of not working at this low a level if you should choose!

There's more...

Build actions will be executed when your project is compiled, copying the content to the appropriate folder, performing whatever conversions are required. If you are used to XNA, this is similar to the functionality of the content pipeline, but not programmable.

Note

Why is there 3D in my 2D?

The bulk of this example was actually going through the process of faking a 2D environment using 3D. The reason is modern GPUs are optimized to work in 3D. If you look at the code to most modern 2D libraries, they are actually working in 3D. If you were to work with native 2D graphics libraries, your performance would be abysmal.

An explanation of 3D mathematics is beyond the scope of this book, but the Kahn Academy (see http://www.khanacademy.org/) is an excellent free resource with thousands of video tutorials.

Tip

The sprite I used for this example and throughout this book is from a wonderful free sprite library made available by GameDev.net user Prince Eugn. You can find more information and download the sprite pack at http://bit.ly/N7CPtE.

 

"Hello World" drawing text on an image


This recipe dynamically creates and displays a texture with text created using the imaging APIs.

Getting ready

This recipe builds on the code from the last recipe. Add the following using statement to the beginning of the program code:

using Sce.PlayStation.Core.Imaging

For the complete source code for this example, see Ch1_Example3.

How to do it...

  1. In the Initialize() function, instead of loading a texture from the file, we will create one using the following code:

    Image img = new Image(ImageMode.Rgba,new ImageSize(500,300),new ImageColor(0,0,0,0));
    img.DrawText("Hello World", 
       new ImageColor(255,255,255,255),
       new Font(FontAlias.System,96,FontStyle.Italic),
       new ImagePosition(0,150));
    _texture = new Texture2D(500,300,false,PixelFormat.Rgba);
    _texture.SetPixels(0,img.ToBuffer());
  2. Next, update the Render() method in the following code in which the bolded portions represent the changes:

    public static void Render (){
     _graphics.SetClearColor (0.0f, 0.0f, 0.0f, 0.0f);
     _graphics.Clear ();
     
     var worldViewProjection = _projectionMatrix * _viewMatrix * _localMatrix;
     _textureShaderProgram.SetUniformValue(0,ref worldViewProjection);
     
                    
     _graphics.SetShaderProgram(_textureShaderProgram);
     _graphics.SetVertexBuffer(0,_vertexBuffer);
     _graphics.SetTexture(0,_texture);
     
     _graphics.Enable(EnableMode.Blend);
     _graphics.SetBlendFunc(BlendFuncMode.Add, BlendFuncFactor.SrcAlpha, BlendFuncFactor.OneMinusSrcAlpha);
     _graphics.DrawArrays(DrawMode.TriangleFan,0,4);
     
     _graphics.SwapBuffers ();
    }

How it works...

Instead of loading a texture from an image file, we create an image dynamically. In the Image constructor, we pass the type of image we want created, the dimensions and the background color to fill it with.

Next, we draw on our newly created image using the DrawText() function, which takes as parameters the text to draw, the color to draw it in, the font to use (there is only one option, System) and the position to draw the text at. We then create a Texture2D object to hold our image. We pass its constructor the image dimensions, whether we want to generate a mipmap or not, as well as the pixel format to use. Finally, we assign the pixel data to our _texture object by calling SetPixel() function and passing in a byte array generated by calling ToBuffer() function on our image.

We had to make the change to the Render() method to support blending using the alpha channel, or background would not be properly visible through the transparent portions of the text. Run the code again without EnableMode.Blend enabled and your text will be illegible.

Now if we run our application, we will see the following screenshot:

There's more...

You can also load a font by name instead of using the built in system font. If you need precise control over your text positioning or size, be sure to check out the FontMetrics and CharMetrics classes in the documentation.

 

Deploying to PlayStation Mobile certified Android devices


This recipe covers deploying an application to an Android device. Running on an Android device requires a developer license that you can purchase in the PSM portal.

Getting ready

You need to have installed the PlayStation Mobile SDK to have access to required files. Of course you will also require a PlayStation Mobile compatible Android device. Make sure the Android ADB driver for your phone is installed on your computer; you can download a generic version from Google's Android development website if required.

How to do it...

  1. Attach your Android phone by USB to your computer. It may install a driver at this point, let it.

  2. Open a Command Prompt (Start | Run and type cmd.exe) and type "%SCE_PSM_SDK%/tools/update_psmdevassistant.bat" including the quotes. This will install the PSM Development Assistant on your device.

  3. On your device, locate and run the PSM Development Assistant application.

  4. On your computer, in the PlayStation Mobile folder in the Start menu, load Publishing Utility.

  5. Now you need to create a Publisher Key; you will only have to do this once. First click the Key Management tab, then the Generate Publisher Key button. Name your key and then enter your login information for the PSM portal. A key will be generated for you.

  6. Click on the Generate App Key Ring button. In the app.xml field locate the App.xml file of the project you want to run on your device and then click on OK. You will have to authenticate with your SEN ID again. If all goes well, your app will now be listed in the Registered App Key Ring list.

  7. Now fire up your project in PSM Studio. Make sure that you have the PlayStation Mobile Development Assistant running on your device and your phone is connected by a USB connection. Set Android as your target using the Project | PlayStation Mobile Device Target menu item and select your phone device.

  8. Run your application by pressing the F5 key or choosing Run or Debug from the menu and your application will be copied to your Android device and run.

There's more...

You need to make sure that USB debugging is enabled on your Android device. This varies from device to device, but is generally located in the Settings | Developer Options | USB debugging menu.

I ran into an error when trying to add my application to the key ring. To fix this, I recreated my Publisher Key. If you are working alone, this isn't a big process, but if you are part of a team, you will need to distribute the updated key.

 

Deploying to a PlayStation Vita


In this recipe, we will configure a PS Vita to be used with PSM Studio. Running on a PS Vita device requires a developer license, which you can purchase in the PSM portal.

Getting ready

You will need to have installed the PlayStation Mobile SDK to have the required Vita drivers installed. If you did not install the drivers as part of the install process, under a default install they are available at C:\Program Files (x86)\SCE\PSM\tools\vita\driver. Obviously you will need to have a PS Vita and a USB cable. You will need a project to run on your Vita; load either one of the examples we created earlier or one of the examples from the SDK.

How to do it...

  1. Before you can run on your Vita, you need to install the Development Assistant from the PS Store. Load the PS Store application on your PS Vita device, and then click on the ... button in the bottom-right corner of the screen. Select Download List. Scroll down and select PlayStation Mobile Development Assistant and click on Download. The application will be downloaded and installed.

  2. The PS Mobile Development application icon will now be available on your Vita. If you haven't already, connect your Vita to your PC using the USB cable. If prompted, allow it to install the driver.

  3. On your Vita, run the PS Suite SDK application.

  4. Make sure that your application has been added to the Registered App Key Ring list (see previous recipe for details).

  5. Load up PSM Studio and load a solution to run on your Vita. Now open the Project | PlayStation Suite Device Target | PS Vita menu item. If your PS Vita doesn't show up, select Refresh Device List... and try again. The device will show as off, if the Development Assistant isn't running.

  6. Run your application using either the F5 key or the Run | Start Debugging menu item.

There's more...

Be careful while connecting the USB cable to your Vita. For some unfathomable reason, you can easily put it in upside down! If you aren't getting a connection to your device, be sure to check if your cable is in upside down mode! I thought my Vita was broken the first time I encountered this, as I left it charging, or at least I thought I did. When I came back and it was completely dead, it took a few minutes of head-scratching until I figured out what was wrong.

See also

  • The example locations can be found in the Installing the PlayStation Mobile SDK recipe

  • See the Deploying to PlayStation Certified Android Devices recipe for details on managing App Keys using PublishingUtility

 

Manipulating an image dynamically


This recipe will demonstrate using the imaging SDK to resize and crop an image, and then saving that image to the gallery.

Getting ready

This recipe builds on the example created in the prior two recipes. The complete source code is available in Ch01_Example4.

How to do it...

In the Initialize() method, change the code as follows:

Image image = new Image(ImageMode.Rgba,new ImageSize(500,300),new ImageColor(0,0,0,0));
Image resizedImage, croppedImage;
image.DrawText("Hello World", 
   new ImageColor(255,255,255,255),
   new Font(FontAlias.System,96,FontStyle.Italic),
   new ImagePosition(0,150));
croppedImage = image.Crop (new ImageRect(0,0,250,300));
resizedImage = croppedImage.Resize(new ImageSize(500,300));
_texture = new Texture2D(resizedImage.Size.Width,resizedImage.Size.Height,false,PixelFormat.Rgba);
_texture.SetPixels(0,resizedImage.ToBuffer());
resizedImage.Export("My Images","HalfOfHelloWorld.png");
image.Dispose();
resizedImage.Dispose();
croppedImage.Dispose();

How it works...

First, we dynamically generate our image just like we did in the previous recipe. We then crop that image to half its width, by calling the Crop() function and providing an ImageRect variable half the size of the image. Crop returns a new image (it is not destructive to the source image) that we store in croppedImage. We then resize that image back to the original size by calling the Resize() function on croppedImage, with the original size specified as an ImageSize object. Like Crop(), Resize() is not destructive and returns a new image that we store in resizedImage. We then copy the pixels from resizedImage into our texture using SetPixels().

Next, we call Export(), which saves our cropped and resized image in a folder called My Images as a file named HalfOfHelloWorld.png on your device. Finally, we call Dispose() on all three images, to free up the memory they consumed.

Now if you run your code, instead of "Hello World", you simply get "Hello".

There's more...

When run on the simulator, export will save the image to your My Pictures folder. Be sure to call Dispose(), or wrap within a using statement all objects that implement IDisposable, or you will quickly run out of memory. Most resources like images and textures require disposal.

 

Working with the filesystem


This recipe illustrates the various ways you can access the filesystem.

Getting ready

The complete code for this example is available in Ch1_Example5. This example adds the following two new using statements:

  • using System.Linq;

  • using System.IO;

How to do it...

Create a new solution and enter the following code replacing the Main() function:

public static void Main (string[] args){
 const string DATA = "This is some data";
 
 byte[] persistentData = PersistentMemory.Read();
 
 byte[] restoredData = persistentData.Take(DATA.Length * 2).ToArray();
 string restoredString = System.Text.Encoding.Unicode.GetString(restoredData);
 
 byte[] stringAsBytes = System.Text.Encoding.Unicode.GetBytes(DATA);
 
 for(int i = 0; i < stringAsBytes.Length; i++)
  persistentData[i] = stringAsBytes[i];
 PersistentMemory.Write (persistentData);
 
 using(FileStream fs = File.Open("/Documents/Demo.txt",FileMode.OpenOrCreate)){
  if(fs.Length == 0)
   fs.Write(stringAsBytes,0,stringAsBytes.Length);
  else{
   byte[] fileContents = new byte[fs.Length];
   fs.Read(fileContents,0,(int)fs.Length);
  }
   
 }
}

How it works...

The first part of this sample demonstrates using the PersistentMemory class. It is a statically available class, so you cannot allocate one. Instead you access it using PersistentMemory.Read(). This returns a byte array that you can now modify. We read some data using the LINQ extension Take() method. We multiplied the size by 2 because the string is stored as UNICODE, which requires 2 bytes per character. We then convert those bytes into a Unicode string. Next, we convert our DATA string into a byte array, then write it to persistentData byte by byte in a for loop. Finally we commit our changes to persistentMemory by calling PersistentMemory.Write(), passing in our updated persisitentMemory byte array.

The next part of the recipe demonstrates traditional file access using the standard .NET libraries, in this case File and FileStream. We open a text file in the /Documents folder named Demo.txt, creating it if it doesn't exist. If the file doesn't exist, we write our byte array stringAsBytes to the file. Otherwise, we read the file contents into a new byte array fileContents.

The first time you run this example, PersistentMemory.Read() will contain gibberish, as nothing has been written to it yet. On the second run, it will start with the bytes composing your DATA string.

There's more...

Persistent storage is limited to 64 KB in space. It is meant for quickly storing information, such as configuration settings. It is physically stored on the filesystem in the /save directory in a file named pm.dat.

There are three primary locations for storing files:

  • /Application: This is where your application files reside, as well as files added with the "Content" build action. These files are read only.

  • /Documents: This is where you put files that require read and write access.

  • /Temp: This is where you put temporary files, that can be read and written. These files will be erased when your program exits.

The PlayStation Mobile SDK limits directory structures to at most 5 levels deep, including the filename. Therefore, /documents/1/2/3/myfile.txt is permitted, but /documents/1/2/3/4/myfile.txt is not.

Note

The PersistentMemory classes was deprecated in the 1.0 release of PSM SDK and should no longer be used. Use traditional .NET file methods instead.

See also

  • See the Loading, displaying, and translating a textured image recipe for details about how to add files to your app's folders using build actions

 

Handling system events


This recipe covers handling the OnRestored system event.

Getting ready

The complete code for this example is available in Ch01_Example06.

How to do it...

Replace Main() with the following code:

public class AppMain
{
 static bool _done = false;
 public static void Main (string[] args){
  
  SystemEvents.OnRestored += HandleSystemEventsOnRestored;
  while(!_done)  {
   SystemEvents.CheckEvents();
   // Loop until application minimized then restored.
  }
 }

 static void HandleSystemEventsOnRestored (object sender, RestoredEventArgs e)
 {
  Console.WriteLine ("System restored, ok to shut down");
  _done = true;
 }
}

How it works...

This code starts by wiring an OnRestored event handler to global class SystemEvents. We then loop until the _done bool is set to true. Within our loop we poll SystemEvents.CheckEvents() to see if any events have occurred. If an OnRestored event occurs, our event handler will be fired.

Our event handler HandeSystemEventsOnRestored() simply writes out a message to the console, then sets the _done bool to true, causing our loop to end, and our program to exit.

Run this example, then minimize the simulator or change applications on your device. When you refocus the application, it will fire the OnRestored event, causing your program to exit.

About the Author
  • Michael Fleischauer

    Michael Fleischauer has spent the last 16 years working as a programmer in a number of different industries from 3D tools creation to automotive and banking. Most recently he launched the internet start-up Flexamail. In his spare time he writes for and runs the game development site GameFromScratch.com, a popular destination for game development tutorials and news. Michael was recently made the first PlayStation Mobile MVP by Sony. Michael lives in Toronto, Canada with his wife and daughter.

    Browse publications by this author
PlayStation Mobile Development Cookbook
Unlock this book and the full library FREE for 7 days
Start now