How to set cameras and viewport

Started by Deabriel, Sat 25/04/2020 01:45:57

Previous topic - Next topic

Deabriel

Hi again, I'm making a game that in some rooms (Normally when the room is inside a house or bulding) the screen will need to be zoomed. I'm making my game with the size: 430 x 240. One of the backgrounds of the rooms that I need to be zoomed have 180 of height. I tried to use the new cameras and viewports in the 3.5.0 ags version, but I don't understand how to set up the things, the ags documentations says:
To create a viewport
Code: ags
static Viewport* Viewport.Create()

To create a camera
Code: ags
static Camera* Camera.Create();

And other commands to use, but I don't know how to use them or where part in the script I need to put them.
Anyone can help me or give me an example of how to use them?
Thanks

Crimson Wizard

#1
The topic "Upgrading to AGS 3.5" explains general idea about cameras and viewports in 3.5.0: https://github.com/adventuregamestudio/ags-manual/wiki/UpgradeTo35#new-viewportcamera-system

First of all, do you want to zoom in existing viewport? Then you do not have to create new cameras/viewport, but use primary ones, which can be accessed as Screen.Viewport and Game.Camera.

Zooming is achieved by setting different sizes for viewport and camera. If you want to have full-screen viewport and stretch room to that, then set viewport to whole screen size, while keeping camera size of background.

In simple case this should work:
Code: ags

Screen.Viewport.SetSize(Screen.Width, Screen.Height);
Game.Camera.SetSize(Room.Width, Room.Height);

above code will display camera's contents size of room stretched to the viewport's rectangle on screen.

Where to put this - wherever you need this effect to be applied. If you'd want this to happen instantly before room appears on screen, then put this in room's "Before fade-in" event.

Deabriel

Quote from: Crimson Wizard on Sat 25/04/2020 02:30:05
The topic "Upgrading to AGS 3.5" explains general idea about cameras and viewports in 3.5.0: https://github.com/adventuregamestudio/ags-manual/wiki/UpgradeTo35#new-viewportcamera-system

First of all, do you want to zoom in existing viewport? Then you do not have to create new cameras/viewport, but use primary ones, which can be accessed as Screen.Viewport and Game.Camera.

Yeah I already read that manual but I didn't get it at first.

I want to have a different viewport/camera to switch between the primary.

I figured out that creating a viewport is something like this:

Code: ags
Viewport *viewport2 = Viewport.Create();


But after that I don't know how to link a camera to it.

Quote from: Crimson Wizard on Sat 25/04/2020 02:30:05
Zooming is achieved by setting different sizes for viewport and camera. If you want to have full-screen viewport and stretch room to that, then set viewport to whole screen size, while keeping camera size of background.

In simple case this should work:
Code: ags

Screen.Viewport.SetSize(Screen.Width, Screen.Height);
Game.Camera.SetSize(Room.Width, Room.Height);

above code will display camera's contents size of room stretched to the viewport's rectangle on screen.

Where to put this - wherever you need this effect to be applied. If you'd want this to happen instantly before room appears on screen, then put this in room's "Before fade-in" event.
I tried to use the code that you give me to test it but it gives me an error:
QuoteFailed to save room room3.crm; details below
room3.asc(8): Error (line 8): '.SetSize' is not a public member of 'Viewport'. Are you sure you spelt it correctly (remember, capital letters are important)?

Crimson Wizard

Quote from: Deabriel on Sat 25/04/2020 02:43:30
I want to have a different viewport/camera to switch between the primary.

Could you elaborate, what do you mean by this?

Quote from: Deabriel on Sat 25/04/2020 02:43:30
But after that I don't know how to link a camera to it.

Camera is linked as viewport2.Camera = some_camera;
which could be primary camera, or new one you create.


Quote from: Deabriel on Sat 25/04/2020 02:43:30
I tried to use the code that you give me to test it but it gives me an error:
QuoteFailed to save room room3.crm; details below
room3.asc(8): Error (line 8): '.SetSize' is not a public member of 'Viewport'. Are you sure you spelt it correctly (remember, capital letters are important)?

Sorry, it is Viewport.SetPosition, where you need to pass x,y,width,height in screen coordinates. So, for example, for full screen view, that would be:
Code: ags

viewport2.SetPosition(0,0,Screen.Width,Screen.Height);

Deabriel

Quote from: Crimson Wizard on Sat 25/04/2020 02:50:35

Could you elaborate, what do you mean by this?
I mean create a camera/viewport in one room and when I change to another room get back to the original viewport/camera.

Quote from: Crimson Wizard on Sat 25/04/2020 02:50:35
Camera is linked as viewport2.Camera = some_camera;
which could be primary camera, or new one you create.

Ok I get it now, but how you turn off on camers? I test it and the new viewport/camera is over the primary viewport/camera.

Quote from: Crimson Wizard on Sat 25/04/2020 02:50:35
Sorry, it is Viewport.SetPosition, where you need to pass x,y,width,height in screen coordinates. So, for example, for full screen view, that would be:
Code: ags

viewport2.SetPosition(0,0,Screen.Width,Screen.Height);

[/quote]
This works fine, but how can you do it in a way that don't get it too misshapen.
Before:
Spoiler
[close]
After:
Spoiler
[close]

I trying changing to different camera width sizes but alway get too misshapen.

Btw thanks for the advices and help.

Crimson Wizard

Quote from: Deabriel on Sat 25/04/2020 03:32:16
I mean create a camera/viewport in one room and when I change to another room get back to the original viewport/camera.

Well, you could as well resize primary viewport and camera when you enter each room, according to that room's properties. Or, if you have only few rooms that need different camera, do it only there.

There's a boolean property called Screen.AutoSizeViewportOnRoomLoad - which is true by default. What it does, when new room is loaded it automatically resizes both viewport and camera to size of screen, which matches old AGS behavior.
If you rely on this property, you may ignore other, common, rooms, and only adjust primary viewport and camera in this specific room in "Before fade-in".

Of course, if you feel like using extra camera is more convenient, you can do that too.

Quote from: Deabriel on Sat 25/04/2020 03:32:16
Ok I get it now, but how you turn off on camers? I test it and the new viewport/camera is over the primary viewport/camera.

Use viewport.Visible to turn it on and off (just like GUI, etc). Another alternative, you may keep same Viewport but switch camera in it by assigning viewport.Camera. Guess it's about finding what's more convenient for your case.


Quote from: Deabriel on Sat 25/04/2020 03:32:16
I trying changing to different camera width sizes but alway get too misshapen.

If this room's aspect ratio (width/height proportions) is different from game's aspect ratio, then this will happen. Perhaps, recalculate viewport's position to have similar aspect ratio with the room. Note: you will have black borders around viewport if it does not cover full screen.

Deabriel

Quote from: Crimson Wizard on Sat 25/04/2020 03:50:46

If this room's aspect ratio (width/height proportions) is different from game's aspect ratio, then this will happen. Perhaps, recalculate viewport's position to have similar aspect ratio with the room. Note: you will have black borders around viewport if it does not cover full screen.
I was a few hours trying to discover how to make the following.
I have rooms (example: streets) that have the background size: 426 x 240. And rooms (example: houses or the bay of the picture) that have the background size: 320 x 180.
The game is setting up in the general settings with the resolution: 426 x 240. (This is the main resolution that I want)

Of course with the resolution settings in 426 x 240, the street room looks perfect:
Spoiler
[close]

If I change the resolution of the game in the general settings to: 320 x 180, the room of the bay looks perfect (like I want):
Spoiler
[close]

But if I use the game resolution that I want (426 X 240) the bay looks tiny (of course). So I use the camera/viewport, but I can't get works fine. The only way to get the graphics not misshapen is to use the viewport (640 x 360) but the graphics are scaled x2 and that's so big.

It's there a way with the viewport/camera to get the view that I get when I put the resolution settings in 320 x 180. But using my 426 x 240 resolution?

Crimson Wizard

#7
Previously I suggested that aspect ratio is different, but that may not be the case. Although, according to this online calculator 426x240 is not precise 16:9 ratio (closest precise matches I found are 416 x 234 and 448 x 252), but I do not know if that's important.

The reason for the image smudging you have is probably that these resolutions are not integer multiplies of each other (not x2, x3 etc, but something like x1.33)
With pixel gfx this means that some pixels will be duplicated x2, and others will stay x1, because it's impossible to have 1/3 of pixel.

AGS supports a workaround technique for similar case called "Render sprites at screen resolution", which scales things using final window/screen resolution you are running in, rather than game's native resolution. Because final res is usually much larger (depends on player's monitor), it allows to have smoother image. But it's hard to predict what exactly happens, and whether it will work same for every player.

(Unfortunately, I can't remember if it affects camera scaling. Logically, it should, but I'll double check a bit later; if not, it should be fixed in the next patch.)

Is this option currently on for your game? If not, try turning it in setup.


Other than above, the only choice I see is to adjust viewport or camera, either make viewport smaller or larger than the screen (in last case you won't see some border pieces), or camera slightly smaller than the room (this will make scrolling room), until it looks better, but this is something that could be only found during experiment.

Deabriel

Well, I tried to activate the option Render sprites at screen resolution and work it perfect!! I checked in my notebook too (it have different aspect ratio) and work it perfect too.

I recorded the final result:


Thank you so much for the help


SMF spam blocked by CleanTalk