Excellent iPhone Programming Chapters by Stanford University

Posted in IPhone App Programming on May 31st, 2011 by Admin

Must read for all iPhone developers, here.

[source Stanford University]

iPhoneDevelopmentBits

Tags: , , , , ,

Windows Gadget: WordPress Statistics v1.0.4

Posted in Programming Tips on May 31st, 2011 by Admin

It appears that WordPress has recently changed the way in which they deliver blog statistics through their API. This change caused the WordPress Statistics Windows Gadget that I created to break. I have submitted an update to the Windows Live Gallery and it is currently pending approval. From my experience it usually takes about one or two days for it to be approved. Please click here to check the gallery over the next few days to get an updated version of the gadget.


Nick Olsen’s Programming Tips

Tags: , , , ,

Raspberry Pi for £15 (less than $25)

Posted in Video Game Programming on May 30th, 2011 by Admin

Very excited to hear the continued development of the Raspberry Pi computer.  Not much bigger than a USB stick it will have the following specification (provisionally):

  • 700MHz ARM11
  • 128MB of SDRAM
  • OpenGL ES 2.0
  • 1080p30 H.264 high-profile decode
  • Composite and HDMI video output
  • USB 2.0
  • SD/MMC/SDIO memory card slot
  • General-purpose I/O
  • Open software (Ubuntu, Iceweasel, KOffice, Python)
See David Braben (of Frontier Developments) talk about the reasoning behind this little computer. Much of the UK is driven by the consumption of computer software and this little beast will open up that access to even more people, especially our children.  However, more importantly, we need to encourage the future producers of computer software and getting this into the hands of children could help.  With the right development environment it could bring forth an innovative era in computer software (and probably games) much like the sub-£100 ZX81 did back in 1981!


Programming: Robots & Video Games

Tags: , , ,

Complete reinstall!

Posted in Video Game Programming on May 30th, 2011 by Admin

My main hard drive was making funny noises for the past couple of months and it finally started to crash the computer while trying to read some sectors.  I keep regular backups so this isn’t so much of a problem.  I’ve installed a new hard drive (a full half terrabyte replacing a tenth of a terrabyte previously).

The only problem now is that I’ve had to install Windows 7 again, Avast anti-virus & Chrome, Thunderbird & Lightning, Firefox, Steam (obviously), StarcraftII (obviously), Java JDK, Eclipse IDE, Android SDK, TortoiseSVN, TortoiseHG, Microsoft Office, Printer driver, etc. etc. etc.  I won’t go on!  But I did reward myself by playing through the whole of Portal again in under 2 hours.  In comparison to Portal 2, I thought it was a lot slower than I remember.  The moving platforms were annoying after the wonderful light-bridges of Portal 2. The dialogue was still on the button, but knowing the jokes and story twists wasn’t so good the second time around.


Programming: Robots & Video Games

Tags: ,

Android vs iOS: A Freemium Game Comparison

Posted in IPhone App Programming on May 29th, 2011 by Admin

Matt Tubergen heads Recharge Studios, a wholly owned subsidiary of W3i that invests in the development and marketing/distribution of freemium mobile games.  W3i is a market leader in distributing and monetizing apps with over 500 million apps distributed for W3i clients. Recharge Studios is actively seeking new investment opportunities, if you have a great idea for a game contact us.

Everywhere we go we read about the battle between Android and iOS. It’s simply impossible to get away from the conversation of which platform is better, where’s the money, which monetizes better, what the cost of porting is and even which OS is sexier…  Setting all the noise aside I wanted to do a simple comparison to see what the actual popularity is for freemium on each platform.

In my search I choose to do a simple count of the top grossing free apps. I also segregated the counts out by relative deck placement (i.e. top 10 apps vs. top 200).

iOS has twice the amount of freemium apps in the top 200 grossing yet both have similar ratios in the top 10 grossing. What does this tell me? Freemium opportunities on Android exist, but the market has yet to meet the demand. See below for full details.

Android versus iOS market share for freemium game apps

Android versus iOS market share for freemium game apps

Why go freemium on Android?

Beyond the fact that freemium has exploded on iOS, there are many other reasons to go freemium on Android. The first and foremost reason to move your business model to Android is due to the ramped increase in piracy. I have heard multiple horror stories from AAA premium developers that for every 1 paid install they are seeing 100 pirated installs…. Ouch!

Other reasons for going freemium on Android include cost of user acquisition. Predictable user acquisition and discoverability is far worse in the Androids market comparable to iOS. Going free allows for a greater opportunity to increase distribution without risking cash for means of unknown distribution sources.

Do you have a question about freemium gaming or a topic you’d like us to explore? Let us know in the comments or catch us on twitter @rechargestudios or @w3i.

Freemium Game Blogs are published in partnership with the series on W3i’s corporate blog.


Mobile Orchard

Tags: , , ,

iPhone released in India, iPad 3 news, and more in this week’s mobile news

Posted in IPhone App Programming on May 29th, 2011 by Admin

The iPhone 4 will be available in India today.

Apple lawyers settle with white iPhone kid.

Rumor has it the Apple iPhone PR team is inviting press to cover WWDC, looks like we could see a new iPhone announcement.

Will Apple stop supporting the 3GS after iOS 5.0 is released?

It is being reported that the iPad 3 will have an AMOLED screen.


Mobile Orchard

Tags: , , , , , , , ,

How to set a transparent GLSurfaceView

Posted in Video Game Programming on May 28th, 2011 by Admin

If you want to set a GLSurfaceView to be transparent, the solution is quite simple but takes quite a bit of trial and error
to get there. Here is my trial and error to save you doing it too.

In the android documentation it states that it is possible to make a GLSurfaceView transparent by a call to

getHolder().setFormat(PixelFormat.TRANSLUCENT);

This might have been true once but on my device (HTC Desire, with API 8) this can cause the rendering to just look corrupted. It basically appears to be two badly coloured images of what I’m drawing.

Most of the material I found on the web states that you need to use setEGLConfigChooser to select the correct surface format that includes transparency. I guess calling setFormat(PixelFormat.TRANSLUCENT) isn’t doing anything to the OpenGL surface.

Common consent appears to say that the desired format is as follows:default
glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);

This gives 32 bits, 8 bits per channel including alpha although the default is 16 bits without alpha (RGB656)

However just calling this will cause my phone to crash. This is because the format is now out of sync with whatever getHolder().setFormat(PixelFormat.TRANSLUCENT) gave us. So instead I needed to call

glSurfaceView.getHolder().setFormat(PixelFormat.RGBA_8888);

Finally, the last and least documented requirement is to call

glSurfaceView.setZOrderOnTop(true);

You can easily check this works for you by modifying the SurfaceViewOverlay example in the APIDemos that are supplied with the SDK.

Before it calls

glSurfaceView.setRenderer(new CubeRenderer(false));

Add the lines

 glSurfaceView.setZOrderOnTop(true);
 glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
 glSurfaceView.getHolder().setFormat(PixelFormat.RGBA_8888);

So, the cubes will appear on top of the hideme buttons when you make them visible.

I hope this helps anyone struggling with a transparent GLSurfaceView.


Programming: Robots & Video Games

Tags: ,

VB Support for the XNA platform

Posted in Visual Basic on May 28th, 2011 by Admin

 

With the release of the Mango Toolset, Visual Basic support for XNA and Windows Phone has now been added. VB resources include VB XNA documentation and VB WP7 code samples and starter kits and this post is intended to walk you through some of the steps in creating a simple XNA application for Windows Phone 7 using VB.

 

What is XNA?

Microsoft XNA is the development framework and tools used to create application for the Xbox 360 / Desktop PC and also the windows Phone 7. It provides access to build managed game type applications. The goal as with any framework is to avoid the developer having to write a lot of low-level repetitive boilerplate code and to expose different aspects of game production into a single system.

The XNA toolset is not new although it has undergone a number of revisions. Although during its history it has only supported C# as a development language for a number of technical reasons, although some users managed to create workarounds to allow limited support. However with the advent of the Windows Phone 7 and its use of the XNA framework additional efforts were made to ensure Visual Basic developers were not left out of the game and with the Mango Tools update finally VB users are provided with full support within XNA.

 

Why is it important?

Windows Phone 7 as a platform is extremely important for Microsoft. The mobile platform had a fundamental reset to provide a much improved user experience. There are two different ways of developing applications for the platform – Silverlight and XNA.

The Silverlight support was implement a while back with specific project templates for the phone but XNA had a few more challenges which have required a bit more work. Silverlight is ideal for most applications that are not timing critical and works well in an event driven model, this is most similar to the type of applications most existing VB developers have created for Windows or the Web. Events are triggered (such as user clicking a button), code is then executed. However for many games, timing is obviously critical and this requires a different platform designed specifically for this type of application. This is best shown with the following diagram which shows the concept of the "Game Loop" – with more details shown a little bit later.

This post is only intended to show you how to get started with XNA development using Visual Basic. To fully realize the potential would take many more pages – entire books devoted to the subject have been written.

 

How do I use it?

The first thing to do is install the Windows Phone Developer Tools 7.1 Beta’ (Mango Toolset) onto a machine with Visual Studio 2010 SP1 installed. VS2010 SP1 is critical for this to work and also will change how Silverlight application developed using VB are now compiled. This will also install updated Windows Phone 7 templates.

Be aware that XNA requires a higher-end video card to allow the emulator to run; most modern machines have cards capable of supporting DirectX10 and 11 which should be sufficient. This is important as Silverlight applications using the emulation don’t require the higher end video card.

 

Let’s create something using VB and XNA

The first thing you will notice is when you create a new project that there is now a folder containing marked "XNA Game Studio 4.0" as well as the existing "Silverlight for Windows Phone 7" folder.

clip_image002[4]

So let’s start by selecting "Windows Phone 7 4.0 Game" to demonstrate XNA.

The first thing you will notice that this Project type actually created 2 projects, a game project and a content project.

This post is working through a simple game application using XNA in order to show VB support. Although when running using the emulator you will not see the true experience, the application will be using the accelerometer sensor inputs of the phone when deployed to a windows Phone 7 device.

So the example is going to have a "Yoyo" type object that is bouncing around the screen and controlled by user inputs. The application will use some simple physics to implement the spring type functionality. Although this is really only a rough approximation of the physics involved it does utilize some items to simulate friction, gravity.

One of the main differences between typical VB "Event driven" applications and XNA "Game Type" applications is that for XNA – timing is absolutely critical. The XNA type applications generally follow a code flow similar to the following. The main bulk of the code is contained within the “Game Loop” cycle of Update/Draw.

clip_image003[4]

The frame rate for the game equates to the speed at which the game loop is running – obviously we want to make the application appear fluid which involves setting one high enough that the animation is going to look smooth. Too low a value and the animate will look jerky, too high a value and the application can get bogged down in calculations which are really not perceptible and the more unnecessary processing the application is doing, the shorter the battery life will be – so there are trade-offs that are made.

The good thing about this frame rate is that it can be controlled really easily and doesn’t really involve having to a lot of low level code. By default the frame rate normally used is roughly 30 frames per second. This is slightly quicker than what the average person can detect and therefore animation at this rate appears smooth.

This can be altered by modifying the line

TargetElapsed = Timespan.FromTicks(3333333)

Now this has been established we need to add a couple of resources used by the application. We will add these to our “Content” Project. We will add the following existing items to the Content project.

Yoyo.png

PieceOfString.Bmp

Target.png

To add the images, simply add using "Add an Existing Item" context menu options from solution explorer and selecting the images files. These items are now part of the content project. Not all graphic image formats are supported so you will need to be careful to select one that is. This is an important point that not all formats are supported for all actions. This relates to graphics/sounds etc. but this information is easily found within XNA links.

Next let’s open up game.vb in the game project and add a few fields which we are going to use for the application. These will be used to hold items required for the game.

Private WithEvents graphics As GraphicsDeviceManager
Private WithEvents spriteBatch As SpriteBatch

'//stores accelerometer's acceleration
Dim accelerometer As Microsoft.Devices.Sensors.Accelerometer
Dim accelX As Single = 0
Dim accelY As Single = 0
Dim spring As Single = 0.2
Dim friction As Single = 0.75
Dim gravity As Single = 17

'Finger Coordinates
Dim fingerPosition_X As Single = 0
Dim fingerPosition_Y As Single = 0

'Yoyo Object
Dim YoYo As Texture2D
Dim PieceOfString As Texture2D
Dim YoYo_Position_X As Single = 50
Dim YoYo_Position_Y As Single = 50
Dim YoYo_VelocityX As Single = 0
Dim YoYo_VelocityY As Single = 0

'Text Position and Score
Dim font1 As SpriteFont
Dim font_Position As Vector2
Dim ScreenCenter As Vector2
Dim textsize As Vector2
Dim Text As String = "Yoyo Attack" & vbLf & "by Spotty"

This has defined some basic items for our game but based upon the general flow diagram show above we will need to put some lines in the “LoadContent” method to use the content which we have already added into the content project (ie. Yoyo.png, PieceOfString.bmp)

We need to Load the content for our game application. The content is stored in the Content Project but when creating a new game, an automatic project to project reference was created between the two. So in the Game project, the LoadContent” method will have the following code added initialize our game objects with the correct content.

Protected Overrides Sub LoadContent()
    ' Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = New SpriteBatch(GraphicsDevice)
    YoYo = Content.Load(Of Texture2D)("Yoyo")
    PieceOfString = Content.Load(Of Texture2D)("PieceOfString")

    font1 = Content.Load(Of SpriteFont)("CourierNew")
    Dim viewport As Viewport = Me.GraphicsDevice.Viewport
    ScreenCenter = New Vector2(viewport.Width / 2, viewport.Height / 2)
    Text = "Yoyo Attack" & vbCrLf & "By Spotty"
    textsize = font1.MeasureString(Text)

End Sub

As the phone is a specific type of device that runs XNA (From the project templates you will have noticed you can target windows, Xbox 360 and Windows Phone 7 using XNA), for the purposes of this application we want to use the accelerometer on the phone so we will need to add a reference to allow us to capture this sensor input.

This requires the addition of the Microsoft.Devices.Sensors.dll in the “Game” project which is a specific class library used for Sensor inputs on the phone. This should show up in the list of applicable references shown on the add references dialog.

clip_image005[4]

So within the Game Project Initialize Method we will add the following

Protected Overrides Sub Initialize()
    font_Position = New Vector2(graphics.GraphicsDevice.Viewport.Width / 2 - 100,     graphics.GraphicsDevice.Viewport.Height / 2)

    If accelerometer Is Nothing Then
        '// Instantiate the accelerometer sensor
        accelerometer = New Microsoft.Devices.Sensors.Accelerometer

        AddHandler accelerometer.ReadingChanged, AddressOf accelerometer_ReadingChanged
        accelerometer.Start()
    End If
    MyBase.Initialize()
End Sub

At this point you will notice a compile error on the AddHandler line because we don’t have a method called “Accelerometer_ReadingChanged” so we can use the “Generate from usage (GFU)” feature. This will generate a method with the following signature.

Private Sub accelerometer_ReadingChanged(sender As Object, e As Microsoft.Devices.Sensors.AccelerometerReadingEventArgs)

End Sub

Within this method we will add a few lines of code to set the acceleration fields with values which are used in the update method.

accelX = CType(e.X, Single)
accelY = CType(e.Y, Single)

So we now have much of the basic parts of the application but we still have not drawn anything on the screen. We are now going to code up the two major methods used for the game loop (update and draw)

So the first method which we will code up is the Update method.

Protected Overrides Sub Update(ByVal gameTime As GameTime)

        ' Allows the game to exit
        If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
            Me.[Exit]()
        End If

        'get the state of the touch panel
        Dim curTouches As TouchCollection = TouchPanel.GetState()

        ' Process touch locations
        For Each location As TouchLocation In curTouches
            Select Case location.State
                Case TouchLocationState.Pressed
                    fingerPosition_X = location.Position.X
                    fingerPosition_Y = location.Position.Y
                    Exit Select
                Case TouchLocationState.Released
                    'Don't care about released state in this demo
                    Exit Select
                Case TouchLocationState.Moved
                    fingerPosition_X = location.Position.X
                    fingerPosition_Y = location.Position.Y
                    Exit Select
            End Select
        Next

        Dim ax As Single = (fingerPosition_X - YoYo_Position_X) * spring
        Dim ay As Single = (fingerPosition_Y - YoYo_Position_Y) * spring

        'Calculate Yoyo velocity based upon Finger acceleration, gavity and friction coefficients
        YoYo_VelocityY += ay
        YoYo_VelocityY += gravity * accelY
        YoYo_VelocityY *= friction

        YoYo_VelocityX += ax
        YoYo_VelocityX += -gravity * accelX
        YoYo_VelocityX *= friction

        'Calculate Yoyo Position based upon velocity
        YoYo_Position_X += YoYo_VelocityX
        YoYo_Position_Y += YoYo_VelocityY

        MyBase.Update(gameTime)
    End Sub

The calculation of the yoyo velocity may seem a little complicated but it is based upon 3 factors Finger acceleration which is set as a result of the event we have just hooked up and the gravity and friction coefficient fields which we have established in the field declarations.

The method is calculating the Yoyo position based upon these inputs as well as determining if the back button as been pressed on the phone.

So now we have calculated our new position for the Yoyo, we need to draw the item.

Protected Overrides Sub Draw(ByVal gameTime As GameTime)

        Dim start As New Vector2(fingerPosition_X, fingerPosition_Y)
        Dim [end] As New Vector2(YoYo_Position_X, YoYo_Position_Y)
        Dim rotation As Single = CSng(Math.Atan2([end].Y - start.Y, [end].X - start.X))

        'calculate the distance between the start and end points
        Dim distance As Integer = CInt(Vector2.Distance(start, [end]))

        GraphicsDevice.Clear(Color.Navy)
        spriteBatch.Begin()

        'Yoyo Drawing
        If PieceOfString IsNot Nothing Then
            spriteBatch.Draw(PieceOfString, start, Nothing, Color.Gray, rotation, Vector2.Zero, _
                 New Vector2(distance, 1.0F), SpriteEffects.None, 0.0F)
        End If
        spriteBatch.Draw(YoYo, New Vector2(YoYo_Position_X - YoYo.Width / 2, YoYo_Position_Y - YoYo.Height / 2), Color.White)

        'Position Titles On Screen
        font_Position.X = ScreenCenter.X
        font_Position.Y = ScreenCenter.Y

        spriteBatch.DrawString(font1, Text, font_Position, Color.Bisque, MathHelper.ToRadians(270), Nothing, 1, SpriteEffects.None, 0)

        spriteBatch.End()

        MyBase.Draw(gameTime)
    End Sub

And last but not least we need to correctly clean up our application when it closes which requires calling out Content dispose methods

    Protected Overrides Sub UnloadContent()
        YoYo.Dispose()
        PieceOfString.Dispose()
        target.Dispose()
    End Sub

So now we can debug the application, this will start up an instance of the emulator. As we don’t have a physical device we can use the mouse to click on the emulator and flick the Yoyo around. It works like a spring and we have written the code to control this, but it’s not very game-like and we want to add some more game features.

So we will create a simple item which well to create a 2nd item that will bounce around the screen and act as a target. This object has less physical properties but will have simple X/Y fixed velocity to determine its movement characteristics but will introduce boundaries and collision detection. A critical point here is that adding this 2nd item should absolutely NOT result in the game slowing down noticeable.

So we will add a few more fields used to store important data of the Target object.

   'Target Object
    Dim target As Texture2D
    Dim TargetX As Single = 10
    Dim TargetY As Single = 10
    Dim TargetVelocityX As Single = 7
    Dim TargetVelocityY As Single = 10
    Dim TargetRadius As Single = 0

Then add need to add some content for the target object. So we will add Target.png to the content project and add code to Game project “LoadContent” method to ensure that we initialize the Target object with an appropriate image from Content project.

target = Content.Load(Of Texture2D)("Target")

The next thing we need to do is control is the ability of the Target Object to detect when it has reached the edge of the screen and make it appear to bounce of the edge. The important thing here is that that the Target object has two important items associated with it (Vector2D to control direction of movement and Texture2D that determines the appearance of the object such as size, content etc.). Based upon the field initialized values we have some initial values but these will change when it hits the edge so we need to write a little code.

For the physics we will assume that this object has zero friction and a fixed velocity. This should make implementing it a little easier. We will need to add some logic in the Update method to ensure we are calculating its new position. This code will need to include some logic to determine if it has reached the edges of the screen and the rebound behavior. Think of a Pool table and the ball bouncing off the edges.

So I’ll add an Enumeration for Direction

Public Enum Direction
    Up
    Down
    Left
    Right
End Enum

And in add a couple more fields

Dim x_Direction As Direction = Direction.Down
Dim y_Direction As Direction = Direction.Left

This will be used to determine the direction of travel in the collision detect logic. We can then create a simple method to control the Edge bounce behavior of the target object.

Private Sub DetectTargetEdgeBounce()
        If TargetX < 0 Then
            'If hit Top Edge then
            x_Direction = Direction.Down
        ElseIf TargetX > GraphicsDevice.Viewport.Width Then
            'Else IF Hit Bottom Edge then
            x_Direction = Direction.Up
        End If

        If TargetY < 0 Then
            'If hit Top Edge then
            y_Direction = Direction.Right
        ElseIf TargetY >= GraphicsDevice.Viewport.Height Then
            'Else IF Hit Bottom Edge then
            y_Direction = Direction.Left
        End If

        'Determine new coordinatied based upon direction
        If x_Direction = Direction.Down Then
            TargetX = TargetX + TargetVelocityX
        ElseIf x_Direction = Direction.Up Then
            TargetX = TargetX - TargetVelocityX
        End If

        If y_Direction = Direction.Right Then
            TargetY = TargetY + TargetVelocityY
        ElseIf y_Direction = Direction.Left Then
            TargetY = TargetY - TargetVelocityY
        End If
    End Sub

The logic I have implemented is easy as it treats the X border detection and Y border detection as separate conditions and reverses the direction of travel when a collision is detected. There are many other ways of achieving the same logic but this is easy to understand.

We now need to make a couple more additions to the Update and draw methods. The following line should be added to the “Update” method calculate the new coordinate of the object based upon current coordinate, direction of travel, Velocity and screen size.

DetectTargetEdgeBounce() 'Movement of Target

Now we have determined the new position of the target object by called the “DetectTargetEdgeBounce” method, we need to ensure we draw it at this calculated position. This is as simple as adding the following lines to the Draw Method

spriteBatch.Draw(target, New Vector2(TargetX - Target.Width / 2, TargetY - Target.Height / 2), Color.Green)

So we now have two objects on the application – one is controlled by the user finger and exhibits some physical properties such as acceleration/gravity/Friction and the other is a much simpler one which appears to have no friction and fixed velocity that is bouncing off the edges of the screen.

We now have to implement a bit more logic to make it more game-like. This would involve some collision detection logic. We have a few logic items we want implement.

1. When Yoyo and target collide and the yoyo has velocity we score points.

2. The longer the collision duration the more points we score

3. We cannot touch the target – otherwise we will lose all points.

4. As we score more points the target becomes quicker.

As a result of this simple logic conditions we will change colors/Images, update score and play sounds. All these are elements of a simple game.

So let’s start with implementing some Collision Detection/Scoring code, as this is would appear to be the core component here. Each item has a single point representing it but obviously the image is bigger than that which is why we need to use both the Vector2d class for position and the Texture2d class for Size. As this seems to be something that we would want to keep together when referring to an game world “Object” , we can create a Type to represent this.

Structure WorldObject
    Sub New(Pos As Vector2, Texture As Texture2D)
        _Position = Pos
        _Texture = Texture
    End Sub
    Public Property _Texture As Texture2D
    Public Property _Position As Vector2
End Structure

We will also want to create a method to detect if these two items have collided based upon their positions and sizes. The following is a simple method which will determine a collision between two “WorldObject” Items, returning true if they have collided.

Public Function IsCollisonDetected(Item1 As WorldObject, Item2 As WorldObject) As Boolean
        'Determine Bounding Rectangle for Item1
        Dim Object_Item1 As New Rectangle(CInt(Item1._Position.X), CInt(Item1._Position.Y), Item1._Texture.Width, Item1._Texture.Height)
        Dim Object_Item2 As New Rectangle(CInt(Item2._Position.X), CInt(Item2._Position.Y), Item2._Texture.Width, Item2._Texture.Height)

        If Object_Item1.Intersects(Object_Item2) Then
            Return True
        Else
            Return False
        End If
    End Function

Now we have to add a little more code to determine the call this and determine some actions when a collision occurs. The first is to add a score field, which we will initialize with a zero value.

Dim Score As Integer = 0

Then in the “Update” method we will add the following code to call the IsCollisonDetected method for the various objects and determine if collisions have occurred between the Yoyo and Target objects and the Finger and Target objects.

So following code this should address the game logic items 1,2 ,3 and 4 in our list of game logic items.

        Dim YoyoObject As New WorldObject(New Vector2(YoYo_Position_X, YoYo_Position_Y), YoYo)
        Dim TargetObject As New WorldObject(New Vector2(TargetX, TargetY), YoYo)
        Dim FingerObject As New WorldObject(New Vector2(fingerPosition_X, fingerPosition_Y), YoYo)
        If IsCollisonDetected(FingerObject, YoyoObject) Then
            Score = 0
        Else
            'Collision Detects 
            If IsCollisonDetected(TargetObject, YoyoObject) Then
                If CInt(YoYo_VelocityX) <> 0 OrElse CInt(YoYo_VelocityX) <> 0 Then Score = Score + 1
                CollisionSound.Play(1, 0, 0)
            End If
        End If

        Text = "YoYo Attack" & vbLf & "by Spotty" & vbCrLf & "Score:" & Score.ToString & vbCrLf & "Time Remaining:" & CalculateTimeRemaining()

        UpdateTargetSpeed(Score)

We don’t have an UpdateTargetSpeed method so will use the GFU to generate a method and adding the following code within the method

Sub UpdateTargetspeed(Score As Integer)
        'Update Speed depending upon score
        Select Case Score
            Case Is < 10
                TargetVelocityX = 10
                TargetVelocityY = 10
            Case 11 To 20
                TargetVelocityX = 12
                TargetVelocityY = 12
            Case 21 To 30
                TargetVelocityX = 14
                TargetVelocityY = 14
            Case 31 To 50
                TargetVelocityX = 15
                TargetVelocityY = 15
            Case Is > 51
                TargetVelocityX = 20
                TargetVelocityY = 20
            Case Else
                TargetVelocityX = 10
                TargetVelocityY = 10
        End Select
    End Sub

So now we have a basic game – with some scoring, collision detection and functionality but we want to add a few more elements such as sounds. So let’s add a background sounds track and some collision detect sounds.

This is simply a matter of adding the audio content to the Content Project. In our example we can add the following three wav files to our Content Project – ( BackGroundMusic.wav, Tada.wav and Ding.wav), just as we did for our graphics used in the application. For our sounds we will have a background music track and then add some sounds into the “Update” method when collisions are detected.

We then simply need to add the following code in the “LoadContent” and “Update “Methods.

The following lines need to be added into the appropriate methods

Required Fields

    Dim BgSound As SoundEffect
    Dim BGInstance As SoundEffectInstance
    Dim CollisionSound As SoundEffect
    Dim TargetTouchSound As SoundEffect

“LoadContent” Method

    CollisionSound = Content.Load(Of SoundEffect)("Tada")
    TargetTouchSound = Content.Load(Of SoundEffect)("Ding")
    BgSound = Content.Load(Of SoundEffect)("BackgroundMusic")

    BGInstance = BgSound.CreateInstance
    BGInstance.IsLooped = True
    BGInstance.Volume = 0.5 'half Volume
    BGInstance.Play()

“Update” Method (Full Method Shown with additional lines enlarged)

Protected Overrides Sub Update(ByVal gameTime As GameTime)
        ' Allows the game to exit
        If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
            Me.[Exit]()
        End If

        'get the state of the touch panel
        Dim curTouches As TouchCollection = TouchPanel.GetState()

        ' Process touch locations
        For Each location As TouchLocation In curTouches
            Select Case location.State
                Case TouchLocationState.Pressed
                    fingerPosition_X = location.Position.X
                    fingerPosition_Y = location.Position.Y
                    Exit Select
                Case TouchLocationState.Released
                    'Don't care about released state in this demo
                    Exit Select
                Case TouchLocationState.Moved
                    fingerPosition_X = location.Position.X
                    fingerPosition_Y = location.Position.Y
                    Exit Select
            End Select
        Next

        Dim ax As Single = (fingerPosition_X - YoYo_Position_X) * spring
        Dim ay As Single = (fingerPosition_Y - YoYo_Position_Y) * spring

        'Calculate Yoyo velocity based upon Finger acceleration, gavity and friction coefficients
        YoYo_VelocityY += ay
        YoYo_VelocityY += gravity * accelY
        YoYo_VelocityY *= friction

        YoYo_VelocityX += ax
        YoYo_VelocityX += -gravity * accelX
        YoYo_VelocityX *= friction

        'Calculate Yoyo Position based upon velocity
        YoYo_Position_X += YoYo_VelocityX
        YoYo_Position_Y += YoYo_VelocityY

        DetectTargetEdgeBounce() 'Movement of Target

        'Detect Collisions
        Dim YoyoObject As New WorldObject(New Vector2(YoYo_Position_X, YoYo_Position_Y), YoYo)
        Dim TargetObject As New WorldObject(New Vector2(TargetX, TargetY), YoYo)

        Dim FingerObject As New WorldObject(New Vector2(fingerPosition_X, fingerPosition_Y), YoYo)
        If IsCollisonDetected(FingerObject, YoyoObject) Then
         TargetTouchSound.Play(1, 0, 0)
            Score = 0
        Else
            If IsCollisonDetected(TargetObject, YoyoObject) Then
                If CInt(YoYo_VelocityX) <> 0 OrElse CInt(YoYo_VelocityX) <> 0 Then Score = Score + 1
                CollisionSound.Play(1, 0, 0)
            End If
        End If

        Text = "YoYo Attack" & vbLf & "by Spotty" & vbCrLf & "Score:" & Score.ToString & vbCrLf & "Time Remaining:" & CalculateTimeRemaining()
        UpdateTargetspeed(Score)

        MyBase.Update(gameTime)
    End Sub

What’s not there in XNA / Phone applications?

We now have a basic game, with sound working on the phone using XNA. For those developers who may have tried experimenting a little with the basic code and using some of the other VB functions, you may have noticed that some expected items are found and others not. XNA uses a feature of the VB Compiler known as VB Core which embeds a reduced version of the Visual Basic runtime into the generated assembly. As this is a new platform for VB, many of these older legacy runtime functions contained in the desktop version of Microsoft.VisualBasic.dll do not exist in the VBCore runtime. Examples of this include the left, mid, right functions. This is intentional and provides VB support or the platform without unnecessarily bloating the assembly size with duplicate of existing framework functionality. So many of the types/functions etc. contained with the desktop versions Microsoft.VisualBasic.dll will not be present.

However, any as with any project types that utilizes the VB Core functionality, if you absolutely cannot live without some missing functionality you can add it simply by creating a method with the same name. Tools such as Reflector enable you to dissemble the code in Microsoft.VisualBasic.dll and see a source code equivalent for that function. Some functionality is not supported on new platforms using VBCore which also may be why they are not implemented – (a good example being Late Binding).

 

Other Changes

All phone projects now utilize the VB Core functionality to embed the required VB runtime functionality in your assemblies. This means that when you build your application (whether Silverlight or XNA) that there will no longer be a separate Microsoft.VisualBasic.dll that needs to be deployed as was the case with Silverlight Phone applications developed prior to the Mango Tools Update. To see this you can use a tool like ILDASM or Reflector to look at the assembly and you will now notice that the assembly now contains a Microsoft.VisualBasic namespace containing the reduced runtime.

To upgrade your existing windows phone 7 Silverlight projects to utilize this new feature, add the following line project file to

<VBRuntime>Embed</VBRuntime>

Also remove any reference to Microsoft.VisualBasic.dll from the .VBProj file.

 

Summary

XNA now fully supports Visual Basic on all platforms (Xbox360/Windows and Windows Phone 7). After completing this walkthrough VB developers should have an idea on how to write XNA applications. Although the model for developing game applications is different from that used by most VB Developers it is easy to get started. With the platform already well developed there are a huge number of resources available which VB Developers can use. VB developers can join the party and start creating to great applications using XNA.

 

Useful Web Links

· Windows Phone SDK Beta

· WPDT 7.1 Documentation

Announcement: http://create.msdn.com/en-US/news/WPDT_7.1_Beta

Download page: http://create.msdn.com/en-us/resources/downloads

Getting Started: http://create.msdn.com/en-us/home/getting_started

· Blogs:

 

Spotty Bowles


The Visual Basic Team

Tags: ,

Quick SMTP client code for the iPhone: skpsmtpmessage

Posted in IPhone App Programming on May 27th, 2011 by Admin

This should be very useful for developers who are looking to provide email functionality in their iphone app. You can download and read more info about it here

[source Alex blog]

iPhoneDevelopmentBits

Tags: , , , , ,

Using Async for Multi-Threading (Alan Berman)

Posted in Visual Basic on May 24th, 2011 by Admin

Asynchronous programming has been around for awhile in Visual Studio. The new Async CTP feature, with the Await statement, provides a simpler, more organized code pattern than existing alternatives for asynchronous programming. A big side-benefit that falls out is that the same code pattern can be used to implement some types of multi-threading.

Asynchronous processing allows the program to continue executing during a long wait, such as during data requests over the internet. Multi-threaded processing helps to optimize code execution for a large number of very short waits, such as data requests from a local hard drive. Multithreading also gives Windows the discretion to assign each thread to a different processor core. The differences are also discussed in the Async Feature Control Flow blog.

Doing multi-threading with the Await statement can simplify your code, and result in a code base that uses a single pattern for asynchronous and multi-threaded code. Someone looking at your code later on won’t need to refresh their memory on the nuances of thread synchronization.

Caveat: If you have a large-scale application, or one that needs to be highly optimized, it may be better to continue to multi-thread using System.Threading types. The Await statement has additional overhead to store and retrieve a state machine. You can create metrics to compare two techniques.

This is an example of conventional processing with threads. It uses an AutoResetEvent for thread synchronization.

Imports System.Threading

Module Module1
    Sub Main()
        ' Process using threads.
        ProcessWithThreads()

        Console.WriteLine("done")
        Console.ReadKey()

        ' Output:
        '  starting worker thread
        '  main thread processing
        '  worker thread processing
        '  sending signal in worker thread
        '  received signal in main thread
        '  done
    End Sub

    Private autoEvent As New AutoResetEvent(False)

    <MTAThread()> _
    Private Sub ProcessWithThreads()
        Console.WriteLine("starting worker thread")

        ThreadPool.QueueUserWorkItem(AddressOf ThreadWorker, autoEvent)

        Console.WriteLine("main thread processing")

        ' Wait for work method to signal.
        autoEvent.WaitOne()
        Console.WriteLine("received signal in main thread")
    End Sub

    Private Sub ThreadWorker(ByVal stateInfo As Object)
        Console.WriteLine("worker thread processing")

        ' Do some work.
        Thread.Sleep(1500)

        ' Signal that work is finished.
        Console.WriteLine("sending signal in worker thread")
        CType(stateInfo, AutoResetEvent).Set()
    End Sub
End Module

The following example is the Async equivalent. It uses TaskEx.Run to start the worker thread. The Await statement waits for the worker method to complete. The fact that the AsyncWorker method is complete signals that the worker thread is complete.

Imports System.Threading.Tasks

Module Module2
    Sub Main()
        ' Process using Async.
        ' The Wait method waits for the task returned by the
        ' Await statement to complete.
        ProcessWithAsync().Wait()
        Console.WriteLine("done")
        Console.ReadKey()

        ' Output:
        '  starting worker thread
        '  main thread processing
        '  worker thread processing
        '  worker thread done, signaling the Await
        '  received signal in main thread
        '  done
    End Sub

    Private Async Function ProcessWithAsync() As Task
        Console.WriteLine("starting worker thread")

        ' This would normally be Task.Run.  It's TaskEx.Run
        ' in the CTP only.
        Dim theTask As Task = TaskEx.Run(AddressOf AsyncWorker)

        Console.WriteLine("main thread processing")

        Await theTask
        Console.WriteLine("received signal in main thread")
    End Function

    Private Sub AsyncWorker()
        Console.WriteLine("worker thread processing")
        System.Threading.Thread.Sleep(1500)
        Console.WriteLine("worker thread done, signaling the Await")
    End Sub

End Module

More information about the Async CTP (SP1 Refresh) can be found on the Details and Download Page. Try out the “Walkthrough: Getting Started with Async”.

Forum for Feedback and Questions | Microsoft Connect for Bugs and Suggestions


The Visual Basic Team

Tags: , , , ,