Sprites using the virtual resolution

Description

Placing a sprite on screen using the approach of a virtual resolution is demonstrated in this example program.

Overview

The process involved is as follows:

Virtual Resolution

Let's begin by setting our virtual resolution when our program starts:

SetVirtualResolution ( 320, 480 )

Two parameters are passed into the function SetVirtualResolution - width and height. In this instance we're setting our screen size to be a width of 320 and height of 480.

Load an image

In this next step we're going to load an image. This is achieved by calling the command LoadImage. This command either accepts or returns an ID number, followed by a filename. Let’s see this in practice:

image = LoadImage ( "blue.png" )

This line calls LoadImage passing in the filename "blue.png", it then returns an ID number which gets saved in the variable image. This ID number is now treated as a handle to our image and can be used later on when referring to it.

An alternative option is to specify the ID number manually e.g.

LoadImage ( 1, "blue.png" )

Creating a sprite

Now that we have an image loaded into memory we're ready to create a sprite and attach this image to it. This is achieved by calling the command CreateSprite. This command either accepts or returns an ID number followed by an ID number of the image you wish to attach to the sprite:

sprite = CreateSprite ( image )

Here we call CreateSprite passing in an ID number for our image and returning an ID number for the sprite. We’re effectively saying create a sprite, attach this image to it and give us back an ID number for this sprite. This ID number for the sprite can be stored for use later on when dealing with sprite properties, for example.

Main loop

Up to this point we have set a virtual resolution, loaded an image and created a sprite that uses this image. We're nearly ready to run our application and see the results. Prior to this we need to ensure one more thing - in our main loop we must call Sync to ensure the screen gets updated and its contents drawn. The command Sync takes no parameters and can be called in your main loop like this:

do
    sync ( )
loop

Full code listing

Everything is now in place. Here's the final code for our program:

SetVirtualResolution ( 320, 480 )

image = LoadImage ( "blue.png" ) sprite = CreateSprite ( image )
do Sync ( ) loop

Conclusion

We're now all set to run our application and see the results on screen. Once you're ready to finish the application you can close it by either pressing the escape key on the Windows and Macintosh platforms, or the home button on mobile devices.