The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Problem With Viewport Camera[SDL2]
theatron


Joined: 03 Feb 2014
Posts: 15
I made piece of code to give my game a camera,
Code:
 SDL_RenderSetViewport(rend, &cam);
I used a viewport as a camera and made a rect as the position and dimensions for it. But everytime I move it to the left
Code:
 cam.x -= 2;
and it gets to end of the left side of the window everything I have rendered disappears, but when I move it to the right
Code:
 cam.x += 2;
it moves to the left. I changed the starting x so it wouldn't start at 0, it fixed the inverted direction but when it reaches the left side everything still goes invisible.

I have only been using SDL 2 for 3 months now, so if anyone has any tips what's happening to the renderer, please post it.
Thanks Very Happy
Naith


Joined: 03 Jul 2014
Posts: 158
That's not really how the viewport is supposed to be used. The viewport is used to "set the drawing area for rendering on the current target" (https://wiki.libsdl.org/SDL_RenderSetViewport).

What you want to do instead is offset your objects (player, enemies and such) by the camera position to make the illusion that the objects are moving to the left whenever the camera is moving to the right. Example code below:

Code:

#include "SDL.h"

#define WINDOW_WIDTH   800
#define WINDOW_HEIGHT   600
#define PLAYER_WIDTH   32
#define PLAYER_HEIGHT   32

float PlayerPosX = (WINDOW_WIDTH / 2)  - (PLAYER_WIDTH / 2);
float PlayerPosY = (WINDOW_HEIGHT / 2) - (PLAYER_HEIGHT / 2);

float CameraPosX = 0.0f;
float CameraPosY = 0.0f;

float SpeedX = 10.0f;
float SpeedY = 10.0f;

bool Running = true;

SDL_Rect PlayerRect = {(int)PlayerPosX, (int)PlayerPosY, PLAYER_WIDTH, PLAYER_HEIGHT};

SDL_Event Event;

int main(int argc, char* argv[])
{
   // Initialize SDL and such

   while(Running)
   {
      while(SDL_PollEvent(&Event))
      {
         switch(Event.type)
         {
         case SDL_QUIT:
            {
               Running = false;

               break;
            }

         case SDL_KEYDOWN:
            {
               // Move the camera

               if(Event.key.keysym.sym == SDL_SCANCODE_UP)
                  CameraPosY -= SpeedY;

               else if(Event.key.keysym.sym == SDL_SCANCODE_DOWN)
                  CameraPosY += SpeedY;

               if(Event.key.keysym.sym == SDL_SCANCODE_LEFT)
                  CameraPosX -= SpeedX;

               else if(Event.key.keysym.sym == SDL_SCANCODE_RIGHT)
                  CameraPosX += SpeedX;
               
               break;
            }
         }

         default:
            break;
      }

      // Offset the player quad by the camera position
      PlayerRect.x = (int)(PlayerPosX - CameraPosX);
      PlayerRect.y = (int)(PlayerPosY - CameraPosY);
   }

   // Quit SDL and such

   return 0;
}


Let me know if you have any more questions Smile
Naith


Joined: 03 Jul 2014
Posts: 158
Also:

Here you can learn more about the usage of the viewport: http://lazyfoo.net/tutorials/SDL/09_the_viewport/index.php
And learn more about camera scrolling: http://lazyfoo.net/tutorials/SDL/30_scrolling/index.php
theatron


Joined: 03 Feb 2014
Posts: 15
Ohhhh :O
When you think you of it like that you don't really need a Viewport, and thanks also for the tutorial on cameras! Very Happy