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.
    How to do it...
  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.
    How to do it...

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.