Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
PlayStation Mobile Development Cookbook
PlayStation Mobile Development Cookbook

PlayStation Mobile Development Cookbook: Over 65 recipes that will help you create and develop amazing mobile applications!

eBook
€28.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

PlayStation Mobile Development Cookbook

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.

Left arrow icon Right arrow icon

Key benefits

  • Learn how you can create your own fantastic PlayStation¬ÆMobile (PSM) applications
  • Develop 2D games quickly and easily, complete with graphics, audio, and input
  • Discover how to construct your own 3D world, import models, and even create texture and vertex shaders

Description

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.

Who is this book for?

If you've got some prior experience with C# and want to create awesome projects for the PlayStation®Vita and PlayStation™Certified devices, then this book is for you.

What you will learn

  • Discover how to handle multiple sources of input to really help you create something unique
  • Load and animate sprites within your own 2D game to get up and running quickly with the SDK
  • Harness the power of the GameEngine 2D library to make your workflow easier
  • Add engaging physics to your game projects with amazing ease
  • Learn how to play a variety of sound effects and music and increase player immersion
  • Create and navigate a 3D world, taking your visuals to the next level
  • Use 3D models and shaders to make your projects look stunning
  • Add multiplayer functionality for exciting competitive and cooperative gameplay
Estimated delivery fee Deliver to Latvia

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 25, 2013
Length: 322 pages
Edition : 1st
Language : English
ISBN-13 : 9781849694186
Vendor :
Sony Computer Entertainment
Category :
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Latvia

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

Product Details

Publication date : Mar 25, 2013
Length: 322 pages
Edition : 1st
Language : English
ISBN-13 : 9781849694186
Vendor :
Sony Computer Entertainment
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 83.98
HLSL Development Cookbook
€41.99
PlayStation Mobile Development Cookbook
€41.99
Total 83.98 Stars icon

Table of Contents

9 Chapters
Getting Started Chevron down icon Chevron up icon
Controlling Your PlayStation Mobile Device Chevron down icon Chevron up icon
Graphics with GameEngine2D Chevron down icon Chevron up icon
Performing Actions with GameEngine2D Chevron down icon Chevron up icon
Working with Physics2D Chevron down icon Chevron up icon
Working with GUIs Chevron down icon Chevron up icon
Into the Third Dimension Chevron down icon Chevron up icon
Working with the Model Library Chevron down icon Chevron up icon
Finishing Touches Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(3 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Hallucinagenii Jan 27, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book covers so much so well that I don't know where I would be without it when it comes to the Playstation Mobile SDK. It is an excellent starting point and reference. I would highly recommend this book - and with the SDK being so simple to use with the aid of this book and Sony Mobile Developer's licenses being so cheap it is definitely worth it!
Amazon Verified review Amazon
Ben Sheron May 16, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Lays out the basics of playstation mobile development in a clear, concise and often charming way using examples. I was thoroughly impressed.
Amazon Verified review Amazon
Nicolas Garcia Jan 20, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I bought this book because I wanted to start developing for the PS Vita... and that's what it is all about. Some C# knowledge (or Java) would help you to start and this book will take you from that point to deploying your own apps/games on your PS Vita without wasting money (developers' tool and license are free right now!) or time. It doesn't get so heavy and practically covers everything on the PS (Controls, colors, sprites,...). On the other hand I think a second book would be helpful, or at least if you want to build your own best selling game!
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela