Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon

29. April 2012

 

 

One of the most bizarre things missing from the PlayStation simulator was support for analog imagesticks, or gamepad support in general.  I had just assumed that you would be able to plug a gamepad into your PC to control the simulator, but I was wrong.  A keyboard only solution made owning an actual device pretty much an absolute requirement.  Fortunately a user on the PlayStation Suite Developer Forums YamatoKei released a code driven solution, which in his own words:

 

 

 

The simulator's input layout and limitations weren't to my taste, so I made myself a way around it:

Run a TCP server that reads PC's gamepad state and sends it back on a client request. And add a simple utility class/lib to connect to that server + decode data transparently from within the game. With a one-line change to revert-back to using the native input on real devices.

Uploaded here: http://dl.dropbox.com/u/1969613/openglForum/PadServer.7z

Code to use:

static ILXPAD pad1 = new ILXPAD_Net();
//static ILXPAD pad1 = new ILXPAD_Vita();
...
pad1.start();
...
pad1.update();
...
camera.roty -= pad1.RStick.X;
bool jump = pad1.Cur.CROSS;

Cheers. :Wink:

 

Just what the doctor ordered!

 

[Download Link]

 

Buyer beware, I haven’t actually tried this out, so if it doesn’t work, destroys your computer or causes a horde of angry cows to raze your house, I take no responsibility.

Programming ,

28. April 2012

 

I am going to continue to develop a number of PlayStation Suite SDK tutorials and finding them could start getting confusing.  Therefore I have put together an index page off all tutorials I have created and will continue to update it as I create more.  Tutorials are ordered in more or less chronological order that a developer should read them in.  I will put this in the side bar shortly, for now you can access the PS tutorial index here.

Programming ,

26. April 2012

 

I am noticing from search traffic that this is an extremely common question, people seem to want to know if you can use C++ with the PS Studio SDK.VitaCpp

 

 

Simple answer and you aren’t going to like it.

 

 

No, no you can’t.

 

 

 

I wouldn’t hold your breath either, it’s the mono runtime behind the scenes that is providing the portability across devices.  If you want native C++ support you need to have access to the full development suite.  Even becoming a PSN developer to develop PSN Mini games requires well over 1000$.

 

 

 

If for some reason you prefer C++’s syntax over C# ( this may in fact be a sign of insanity by the way… ), you *might* and I emphasize *might* be able to take advantage of this project CPlusPlus, at some point, but even then you would have to write pure CIL code.  Without native access, there would be very little point using C++ in this situation.

 

 

 

Therefore for all intents and purposes, no you cannot use C++ with PlayStation Suite. C# is the only game in town and I wouldn’t expect to see that change any time soon. 

Programming , ,

26. April 2012

 

In this tutorial we are going to manually manage the game loop manually.  Additionally we are going to look at the various ways of handling input from the gamepad.  This tutorial builds on the code we developed in the previous tutorial.  As you may recall, we created a “Hello World” sprite and centered it to the camera.  This time, we are going to give the user the ability to control the sprite’s position and size using the gamepad.

 

 

Let’s get straight to the code:

 

using System; using System.Collections.Generic; using Sce.Pss.Core; using Sce.Pss.Core.Environment; using Sce.Pss.Core.Graphics; using Sce.Pss.Core.Input; using Sce.Pss.HighLevel.GameEngine2D; using Sce.Pss.HighLevel.GameEngine2D.Base; using Sce.Pss.Core.Imaging; namespace HelloWorld { public class AppMain { public static void Main (string[] args) { Director.Initialize(); Scene scene = new Scene(); scene.Camera.SetViewFromViewport(); var width = Director.Instance.GL.Context.GetViewport().Width; var height = Director.Instance.GL.Context.GetViewport().Height; Image img = new Image(ImageMode.Rgba,new ImageSize(width,height),new ImageColor(255,0,0,0)); img.DrawText("Hello World", new ImageColor(255,0,0,255), new Font(FontAlias.System,170,FontStyle.Regular), new ImagePosition(0,150)); Texture2D texture = new Texture2D(width,height,false,PixelFormat.Rgba); texture.SetPixels(0,img.ToBuffer()); img.Dispose(); TextureInfo ti = new TextureInfo(); ti.Texture = texture; SpriteUV sprite = new SpriteUV(); sprite.TextureInfo = ti; sprite.Quad.S = ti.TextureSizef; sprite.CenterSprite(); sprite.Position = scene.Camera.CalcBounds().Center; scene.AddChild(sprite); Director.Instance.RunWithScene(scene,true); bool gameOver = false; while(!gameOver) { Sce.Pss.HighLevel.GameEngine2D.Director.Instance.Update(); if(Input2.GamePad.GetData(0).Left.Release) { sprite.Rotate(Sce.Pss.HighLevel.GameEngine2D.Base.Math.Deg2Rad(90)); } if(Input2.GamePad0.Right.Release) { sprite.Rotate(Sce.Pss.HighLevel.GameEngine2D.Base.Math.Deg2Rad(-90)); } if((Sce.Pss.Core.Input.GamePad.GetData(0).Buttons & GamePadButtons.Up) == GamePadButtons.Up) { sprite.Quad.S = new Vector2(sprite.Quad.S.X += 10.0f,sprite.Quad.S.Y += 10.0f); sprite.CenterSprite(); } if((Sce.Pss.Core.Input.GamePad.GetData(0).Buttons & GamePadButtons.Down) == GamePadButtons.Down) { sprite.Quad.S = new Vector2(sprite.Quad.S.X -= 10.0f,sprite.Quad.S.Y -= 10.0f); sprite.CenterSprite(); } if(Input2.GamePad0.Circle.Press == true) gameOver = true; Sce.Pss.HighLevel.GameEngine2D.Director.Instance.Render(); Sce.Pss.HighLevel.GameEngine2D.Director.Instance.GL.Context.SwapBuffers(); Sce.Pss.HighLevel.GameEngine2D.Director.Instance.PostSwap(); } Director.Terminate(); } } }

 

 

 

The top portion of the code is completely unchanged, our new additions start at the line:

Director.Instance.RunWithScene(scene,true);

 

The key addition here is the second parameter “true”.  This bool is telling the Director singleton that we are going to handle the game loop ourselves, this means we need to call 4 methods manually ( described in a moment ).  Next up we create a bool gameOver, which is going to control when we exit our game loop.  Obviously we don’t want to exit right away, so we default it to false.  Speaking of game loops, that’s what the while line does, loops over and over until gameOver is set to true.

 

Now in each iteration of our loop, there are four methods we have to call, Update(), Render(), GL.Context.SwapBuffer() and PostSwap().  Update() tells the director we have moved on to the next frame, Render() draws the frame, SwapBuffers displays what Render() drew on the backbuffer to the screen (makes it visible) and finally PostSwap() tells the director we’ve finished swapping buffers and it must be called after SwapBuffers().  Those four combined represent a complete game loop, all the rest of the code handles input from the game pad.

 

Just to make something perfectly clear here, I am using 3 different ways to check for input, *you won’t do this in your code*.  I am just doing it to illustrate all of your different options in one example.  You should just pick one ( probably Input2 ) and use only it.  Lets look at them one at a time.

 

if(Input2.GamePad.GetData(0).Left.Release) { sprite.Rotate(Sce.Pss.HighLevel.GameEngine2D.Base.Math.Deg2Rad(90)); }

 

 

This method is the most likely way you will deal with Input.  Input2 is a wrapper around the Input object to make things a bit simpler.  GetData takes a parameter telling it which control ( controller 1, controller 2, etc ) you want to poll, returning a GamePadData object, representing the state the controller is in.  We are then checking if the “Left” button has been released.  In the case Left is released, we then rotate our sprite 90 degrees.  Rotate takes an angle value in radians, so we use the Math.Deg2Rad() helper function to convert from degrees to radians.  Of course you could have passed the radian value in instead of converting, 1.5709633 is 90 degrees in radians, it’s just a bit harder to look at.

 

if(Input2.GamePad0.Right.Release) { sprite.Rotate(Sce.Pss.HighLevel.GameEngine2D.Base.Math.Deg2Rad(-90)); }

 

 

This if statement is almost identical to the last one, but instead of using GetData(0), we use a handy alias (GamePad0) that represents exactly the same thing.  The only other difference is, in this case we are checking to see if the “Right” button has been released, and we are rotating by a negative value ( the other way ) if it is.

 

 

if((Sce.Pss.Core.Input.GamePad.GetData(0).Buttons & GamePadButtons.Up) == GamePadButtons.Up) { sprite.Quad.S = new Vector2(sprite.Quad.S.X += 10.0f,sprite.Quad.S.Y += 10.0f); sprite.CenterSprite(); } if((Sce.Pss.Core.Input.GamePad.GetData(0).Buttons & GamePadButtons.Down) == GamePadButtons.Down) { sprite.Quad.S = new Vector2(sprite.Quad.S.X -= 10.0f,sprite.Quad.S.Y -= 10.0f); sprite.CenterSprite(); }

 

 

This time we are using Input directly, using Input instead of Input2.  As you can see, the results are a bit more “raw”.  In this case we have to use bit masking to determine if a given button is pressed and there is no Released option.  In this case we are checking for the “Up” and “Down” buttons.  In the event that the user is pressing Up or Down, we are modifying the Scale of the quad our hello texture is pasted on.  Remember initially Quad.S is equal to the size of the screen in pixels.  If we press Up, we scale the image up 10 pixels in size, if we press down, we shrink it by 10 pixels in size.

 

 

Finally, we check ( using the Input2 method ), if the user has pressed the Circle button, in which case we set gameOver to true, causing our loop to exit and our program to end.

 

 

One thing to notice at this point is how we scaled the sprite.  Unlike rotate, we didn’t call a method, instead we modified the Quad.S(cale) property.  The actual transformation matrix of a node ( which SpriteUV is derived from ) is actually determined combining the Position, Scale, Skew and Rotation+Angle+RotationNormalize properties.  Therefore modifying any of these properties will translate the node accordingly.

 

 

Now run our game, if we press left or right, we rotate 90 degrees, while pressing up or down scales the images.  Finally press Circle to exit.

 

 

helloworld2

 

 

One last thing I suppose needs covering… how exactly do you press Left, Right, or Circle on the simulator?

Left directional key
Cursor key: ←

Up directional key
Cursor key: ↑

Right directional key
Cursor key: →

Down directional key
Cursor key: ↓

Square button
Alphabet: A

Triangle button
Alphabet: W

Circle button
Alphabet: D

Cross button
Alphabet: S

SELECT button
Alphabet: Z

START button
Alphabet: X

L button
Alphabet: Q

R button
Alphabet: E

 

 

Sadly, you cannot currently emulate the analog sticks using the simulator.  Obviously you can on the Vita device.

 

EDIT: Oops, forgot to include the project source code, which you can download here.

Programming , , ,

24. April 2012

 

 

There is a charter somewhere that states all tutorial series must start with the ubiquitous Hello World tutorial, and who am I to break the charter?  So that is exactly what we are going to do here, a simple Hello World. This is as much about getting up and running with PS Studio than it is about C# coding, as there are a couple small gotchas.  By the end of this tutorial you should be able to create, configure and run an application.  In future tutorials, I will assume you have these abilities.

 

If you haven’t already, download and install PlayStation®Suite SDK from here.  The install is pretty straight forward, take the defaults, next next next, done.  If you want some idea of what you just installed check out this post.  Now fire up PssStudio from your start menu.  Once loaded, select File->New-Solution, like this:

 

image

 

 

In the “New Solution” dialog, on the left hand side expand C# then select PlayStation Suite.  Select PlayStation Suite Application on the right, then fill in whatever name you want ( I  am using HelloWorld ).  This should automatically fill in Solution Name, it is your choice if you want to create a subdirectory or not, in this case I will.  Fill out the dialog like this:

 

image

 

 

Click OK, and your solution will be created.  Now we need to add an app.cfg file to your application, or it will fail to run.  Note, this is not that same as a .NET application configuration file.  In the Solution Explorer, right click your project name ( HelloWorld in my case ), then select Add->New File… like such:

 

 

image

 

 

Choose Misc on the left, then Empty Text File, name it app.cfg and click New.

 

image

 

The newly created file will open in the text editor, fill it in with the following:

 

memory: resource_heap_size : 16384 managed_heap_size : 16384 input: gamepad : false touch : false motion : false

 

 

This file is telling what kind of machine your application is targeting.  If you forget this step, the simulator pss.exe will just max out a CPU core, never responding.  Now in the Solution panel, locate and double click AppMain.cs, this is where our application code will reside.

 

image

 

 

The actual process of doing Hello World on Vita is actually incredibly involved, as you need to do almost everything yourself.  In this example however, I am going to take advantage of GameEngine2D, which is an included 2D Game engine that makes many of the drudge worthy tasks much easier.  That means we need to add a reference to GameEngine2D.  Adding a reference is simply saying “I am going to use the code included in this library” In order to add a reference to GameEngine2D, right click on References and choose Edit References… like such:

 

image

 

Then a dialog will pop up, locate Sce.Pss.HighLevel.GameEngine2D and check the box to it’s left.  Then click OK.

 

image

 

 

Now we can add a using entry to tell our code to use the newly referenced library.  This is where we run into a bug in MonoDevelop ( PS Studio ).  Look at the following auto-completion screenshot:

 

image

 

 

Hmmm… where is HighLevel?  We added the reference, it should be there.  Well, on adding a new reference it seems the intellisense/auto-completion isn’t updated properly.  I live by auto-complete so this is a big deal to me.  Simply close and re-open your solution and presto:

 

 

image

 

 

Thankfully, you don’t really need to add references all that often, so while it’s inconvenient to have to exit and restart, its not the end of the world.  PS Studio loads quickly enough to barely notice.

 

 

Alright, finally time for the code!

 

 

using System; using System.Collections.Generic; using Sce.Pss.Core; using Sce.Pss.Core.Environment; using Sce.Pss.Core.Graphics; using Sce.Pss.Core.Input; using Sce.Pss.HighLevel.GameEngine2D; using Sce.Pss.HighLevel.GameEngine2D.Base; using Sce.Pss.Core.Imaging; namespace HelloWorld { public class AppMain { public static void Main (string[] args) { Director.Initialize(); Scene scene = new Scene(); scene.Camera.SetViewFromViewport(); var width = Director.Instance.GL.Context.GetViewport().Width; var height = Director.Instance.GL.Context.GetViewport().Height; Image img = new Image(ImageMode.Rgba, new ImageSize(width,height), new ImageColor(255,0,0,0)); img.DrawText("Hello World", new ImageColor(255,0,0,255), new Font(FontAlias.System,170,FontStyle.Regular), new ImagePosition(0,150)); Texture2D texture = new Texture2D(width,height,false, PixelFormat.Rgba); texture.SetPixels(0,img.ToBuffer()); img.Dispose(); TextureInfo ti = new TextureInfo(); ti.Texture = texture; SpriteUV sprite = new SpriteUV(); sprite.TextureInfo = ti; sprite.Quad.S = ti.TextureSizef; sprite.CenterSprite(); sprite.Position = scene.Camera.CalcBounds().Center; scene.AddChild(sprite); Director.Instance.RunWithScene(scene); } } }

 

 

PHEW!  Pretty long for a Hello World eh?  Truth of the matter is, had I not used GameEngine2D it would have easily been 4 or 5 times longer!  We will cover what is going on behind the scenes ( the stuff GameEngine2D is handling ) in a later post.  For now, just assume this is the way it works, at least while you are getting started.  Now lets take a quick walk through the code and look at what’s happening here.

 

First we add our additional includes, Sce.Pss.HighLevel.GameEngine2D and Sce.Pss.HighLevel.GameEngine2D.Base.  Our app consists of a single method, Main, as the event loop is actually managed by the Director.

 

Speaking of which, that is what the first line does, initializes the Director singleton.  A singleton is a globally available object that is allocated on it’s first use.  Director is the heart of your game, even though most of the complexity is hidden away.  Remember, you have full source code for GameEngine2D available if you want to peek behind the curtain.

 

Next up we create a Scene.  Again, Scene is an abstraction provided by GameEngine2D which can be thought of as a collection of visible “stuff”, the bits and pieces that compose what we want to display to the user and the Camera used to display them.  We then call SetViewFromViewport() which creates a camera sized to the window. Generally without GameEngine2D, you would have to do this yourself, setting up an orthographic project.

 

The next two lines get the width and height, as reported by the Director’s OpenGL context.  We then create an Image the same dimensions as our viewport.  The Image class is used to hold binary image data, such as from a PNG or JPG file, but in this case we are create a new blank image.  Once we create our Image, we call it’s DrawText method to draw our “Hello World” message.  ImageColor represents the color we want to draw the image ( red in this case ), Font represents the font to draw the text in ( there aren’t really many options here ), while ImagePosition represents the location within the image to draw the text at.

 

Now that we have created an image, we need to create a texture out of it.  Textures are most often thought of as the images mapped around 3D objects and in a way, that is still what we are doing here.  GPUs have no concept of image files or pixels, they deal only with textures.  That is why we copy our image data into the texture, using the SetPixels method of Texture2D and the ToBuffer() method of Image, to turn the images pixel data into an array Texture2D can use.  At this point we are done with our Image, so we Dispose() it.

 

We next create a TextureInfo object and assign our newly created Texture to it.  TextureInfo caches UV location information about the texture, as well as taking ownership of the texture itself.  More importantly, it’s a TextureInfo object that Sprite expects, so that is what we create.  Speaking of sprite, that is what we create next in the form of a SpriteUV.

 

Since modern GPUs don’t really deal with pixels anymore, everything is pretty much made out of textured polygons and SpriteUV is no exception.  It is essentially a rectangular polygon that faces the camera with our image textured on it, all sprites are.  Next up we set the sprite’s Quad ( the rectangle the texture is plastered to ) to be equal to the size of our texture, which in this case is the same size as our view.  We now position our sprite smack in the middle of the view.

 

Now that we created our Image data, copied that image data into a Texture2D that was then assigned to a TextureInfo object, which in turn was assigned to a SpriteUV, we are now ready to add that fully textured sprite to our scene, which we do by calling AddChild().  Yeah, it sounds very convoluted, but you will find it natural very soon and generally you just load your textures from file anyways, greatly simplifying this process.

 

Anyways, now that we have our scene populated with our texture, which is the same size as the screen, we go ahead and tell the Director to do it’s thing, via RunWithScene().  You can think of this as the game loop, although the internals of what it’s doing are hidden from you.

 

 

Don’t worry, it’s really not as complicated as it seems.  Now lets take a look at the fruits of our labour.  To run it in PS Studio, select the Run menu and either choose Start Without Debugging or Start Debugging, depending if you want to be able to debug or not, like such:

 

image

 

 

And presto, all our hard work resulted in…

 

image

 

May not be much to look at, but you successfully created a game you can run on a Vita!

 

Speaking of which, if you need details of running on an actual device instead of in the simulator, read this post.

 

The complete project file for this tutorial are available for download here.

 

 

 

Continue on to Hello World Part 2: Hello Harder.

Programming , , ,

Month List

Popular Comments