Viewport

From Sim Innovations Wiki
Jump to navigation Jump to search

Basics explained

Viewports rely on the viewport_rect API function. It can be used on images, text, running images and running text. A viewport is always a rectangle, and only one viewport can be applied to an image or text. The viewport lets you show an image or text within a specified boundary. The boundary stays at the same place, even if you move the image or text. It can be useful for temperature indicators, fuel indicators, running text within a box, etc...

How to use it

The API function states viewport_rect(object_id,x,y,width,height). The object ID can be either images or text. X are the number of pixels from the left top corner of the instrument, Y are the number of pixels from the top of the instrument, then you have the width and height of your viewport, which is the part that will be visible.

Example 1

We have an image and will give it the ID img_baby, first we will add this image.

img_baby = img_add("mysterybaby.png", 0, 0, 315, 490)

Now that we have added the image, we can put a viewport on it.

img_baby = img_add("mysterybaby.png", 0, 0, 315, 490)
viewport_rect(img_baby, 80, 200, 205, 90)

The result is that you will only see the specified part of the image, which is 205 x 90 pixels, the eyes in this case. But even when you will start moving the image (later examples), you will only see the part of the image which is within this viewport. Viewport rect api.png

Example 2

We have added an image with a viewport on it, but what will happen when we move the image? We will use this piece of code:

img_baby = img_add("mysterybaby.png", 0, 0, 315, 490)
viewport_rect(img_baby, 80, 200, 205, 90)
move(img_baby, nil, 30, nil, nil)

With this code we move the image 30 pixels down (Y position). The result:

Viewport rect moved.png

You can see that the viewport stays in place, but the image has moved 30 pixels downward.