User Tools

Site Tools


en:guides:beginners:viewport_and_camera

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
guides:beginners:viewport_and_camera [2015/10/23 03:17 (9 years ago)] – created sausageen:guides:beginners:viewport_and_camera [2020/08/19 12:15 (4 years ago)] (current) – Viewport -> MainViewport to match the changes in init's template iarwain
Line 1: Line 1:
 ====== Part 5 – Viewport and Camera ====== ====== Part 5 – Viewport and Camera ======
- 
-From here on, I'll assume that you performed step 4, and that your project is called "Project". 
  
 In order for anything to appear in your game, you will first need both a viewport and a camera. In order for anything to appear in your game, you will first need both a viewport and a camera.
Line 7: Line 5:
 A viewport is the rectangle that we can view the game world through. And a camera allows us to see all or part of that world at a time. A viewport is the rectangle that we can view the game world through. And a camera allows us to see all or part of that world at a time.
  
-Take a look at your Project.ini and you'll see that a [Viewport] section has already been specified for you. There are some commented out options like Size and BackgroundColor:+Take a look at your MyGame.ini and you'll see that a ''[MainViewport]'' section has already been specified for you.
  
 <code=ini> <code=ini>
-[Viewport+[MainViewport
-Camera             Camera +Camera             MainCamera
-;RelativePosition  = bottom right +
-;Position          = (100, 100, 0); NB: Position is in pixels and will be ignored if RelativePosition is specified +
-;RelativeSize      = (0.5, 0.5, 0.0) +
-;Size              = (200, 150, 0) NB: Size is in pixels and will be ignored if RelativeSize is specified +
-;BackgroundColor   = (255, 180, 0)+
 </code> </code>
  
-Our game will have a blue sky as a background, so change the config to be:+Let's give our game a blue sky as a background, by adding the ''BackgroundColor'' property to the config:
  
 <code=ini> <code=ini>
-[Viewport+[MainViewport
-Camera            = Camera+Camera            = MainCamera
 BackgroundColor   = (0, 180, 255) BackgroundColor   = (0, 180, 255)
 </code> </code>
  
-Secondly, notice the Camera parameter is set to Camera? This means it is set to the [Camera] section, which has also already been defined for you:+Secondly, notice the Camera parameter is set to ''MainCamera''? This means it is set to the ''[MainCamera]'' section, which has also already been defined for you:
  
 <code=ini> <code=ini>
-[Camera+[MainCamera
-FrustumWidth  @Display.ScreenWidth +FrustumWidth    1280 
-FrustumHeight = @Display.ScreenHeight +FrustumHeight   720 
-FrustumFar    1.0 +FrustumFar      2.0 
-FrustumNear   = 0.0 +FrustumNear     = 0.0 
-Position      = (400300, -1.0)+Position        = (0.00.0, -1.0) ; Objects with -1 <= Z <= 1 will be visible
 </code> </code>
  
-The camera is positioned into the center of the screen (400, 300) so that the whole screen can be viewed.+The ''Position'' is set to 0,0 which is in the center of the screen so that the whole screen can be viewed. The Z position is -1.0 which means the camera is set back a little from the action.        
  
-A frustum width and height does not have to be the same size as your screen, but for our game, we want it to be. So the width and height values are being inherited from the ScreenWidth and ScreenHeight parameters in the [Display] section.+Let's change the screen width and height so that it makes working through the tutorial easier (and easier to take screenshots :) ):
  
-The viewport needs to be created when our game runs, so let's take a look at the Project.cpp code:+<code=ini> 
 +[MainCamera] 
 +FrustumWidth    = 800 
 +FrustumHeight   = 600 
 +FrustumFar      = 2.0 
 +FrustumNear     = 0.0 
 +Position        = (0.0, 0.0, -1.0) ; Objects with -1 <= Z <= 1 will be visible 
 +</code> 
 + 
 +A frustum width and height does not have to be the same size as your screen, but for our game, we want it to be. So the ''ScreenWidth'' and ''ScreenHeight'' values from the ''[Display]'' section are being inherited from the ''FrustumWidth'' and ''FrustumHeight'' values, as you can see at: 
 + 
 +<code ini> 
 +[Display] 
 +ScreenWidth     = @MainCamera.FrustumWidth 
 +ScreenHeight    = @MainCamera.FrustumHeight 
 +... 
 +</code> 
 + 
 +The viewport needs to be created when our game runs, so let's take a look at the MyGame.cpp code:
  
 <code=cpp> <code=cpp>
Line 48: Line 59:
 { {
  
-    orxViewport_CreateFromConfig("Viewport");+    orxViewport_CreateFromConfig("MainViewport");
 </code> </code>
  
-Nice and simple. Our project already has this added. The init() function runs once when your game first executes, and this is where the viewport is created.+Nice and simple. Our project already has this added. The Init() function runs once when your game first executes, and this is where the viewport is created.
  
  
-The "Viewport" parameter specified in that function is the same name as the [Viewport] section in the config.+The "MainViewportstring parameter specified in that function is the same name as the ''[MainViewport]'' section in the config.
  
-Now, because the camera was specified in the [Viewport] section, we do not need to create a camera in code.+Because the camera was specified in the ''[MainViewport]'' section, we do not need to create a camera in code.
  
-Final thing for this step is to name our game. This name will show in the window title. Set this with the Title property in the [Display] section:+Final thing for this step is to name our game. This name will show in the window title. Set this with the ''Title'' property in the ''[Display]'' section. Also while you are here, set the ''Smoothing'' to false:
  
 <code=ini> <code=ini>
 [Display] [Display]
-ScreenWidth   800 +ScreenWidth     @MainCamera.FrustumWidth 
-ScreenHeight  600 +ScreenHeight    @MainCamera.FrustumHeight 
-Title         = Platform Hero+Title           = Platform Hero 
 +FullScreen      = false 
 +Smoothing       = false 
 +VSync           = true
 </code> </code>
  
-Compile and run to see your game window with a blue background and a default object in the centre.+This will change the name in the window title, but only if you are compiling in Release mode. You'll most likely be compiling in Debug mode right now. So you'll also need to change the title in the ''MyGamed.ini'' file: 
 + 
 +<code ini> 
 +[Display] 
 +Title           = Platform Hero (Debug) 
 +ShowFPS         = true 
 +</code> 
 + 
 +Compile and run to see your game window with a blue background and a default object in the center of the screen: 
 + 
 +{{:guides:beginners:beginners-19-object-position.png?nolink|}} 
 + 
 +----
  
-{{:guides:beginners:beginners-07-default-object.png?nolink|}}+Next: [[en:guides:beginners:objects|Part 6 – Objects]]
  
-NextPart 6 – Objects+{{section>en:guides:beginners:toc&noheader&nofooter&noeditbutton}}
en/guides/beginners/viewport_and_camera.1445595447.txt.gz · Last modified: 2017/05/30 00:50 (7 years ago) (external edit)