Moving veiwport ? (SDL)

Started by
3 comments, last by samuraicrow 17 years, 10 months ago
Hi, Im just started making a Tile based SDL game(C++). It's a 2D RPG and the player should be moving in fairly big worlds. I have no real problem drawing the map, but i dont know how i shall move the viewport so the world moves width the player. I dont wanna limit my world to 1 screen! So please any suggestions? And by the way, i maked this topic in isometric games too my mistake sorry!
Advertisement
You'll have to scroll the objects yourself. Bascially when you move two screens over you move the background two screens over.

I have some tutorials
Scrolling
Tiling and scrolling

</maximum spammage>

Learn to make games with my SDL 2 Tutorials

Essentially you want to give each game object a set of "real" coordinates that say where they are in the world. For example, you have a rock at [10, 15]. Then you have a "camera", which is really just a point in space. When it comes time to draw all of your objects, you pass the camera's location to them. Then they draw themselves based on the location of the camera. For example, when the camera is at [0, 0], you'd draw your rock at [10, 15]. In this case the "real" coordinates are the same as the "screen" coordinates, because the camera is at the origin. However, if you move to the right by 1 unit to [1, 0], then the rock should visually move to the left; you'd draw it at [9, 15]. So basically, the draw location of an object is equal to the real location minus the camera location. And of course, you don't draw the object at all if it's offscreen.

You can also pull off some interesting effects by moving the draw location by a multiple of the camera location - e.g. draw = real - .8 * camera. Here the draw location scrolls more slowly than the camera moves. Draw this object behind all the others, and now you have a background layer! Or if you put this object in front of the other objects, then you have something that is moving to keep up with the camera.
Jetblade: an open-source 2D platforming game in the style of Metroid and Castlevania, with procedurally-generated levels
Hello hms117,

First and foremost, you should have a camera class or set of functions, which manage a virtual camera which moves around based on user input.

In a tile-based game, your mouseclicks or keypresses move the camera up/down/left/right or north/south/east/west.

Let's set some rules:
- our game window is 640x480 pixels
- the camera's x,y coordinates denote the pixel at the center of the screen
- each tile is 32x32 pixels
- the entire game map is made up of 1000x1000 tiles
- therefore, the game area which the camera can scroll over, is 32000x32000 pixels
- we accept mouse input to move the camera
- we move pixels at a time (e.g. 10 pixels per second)

Let's set the camera to 1000,1000 (pixels) ... this means the upper left corner is at 680,720.
To render all the tiles between this, we figure out the following:

tile_x = 680 / 32tile_y = 720 / 32


This gives us the appropriate tile in the map, which is at cell 21,22.

But what about staggering? If we simply start rendering the entire array of nearby tiles like this, whenever the player moves, the tiles will jump 32 pixels each time the camera traverses a tile in any direction.

Instead, what we do is offset the tile based on how much of a full tile is offscreen. This is gotten by utilizing the modulus operator.

tile_x_offset = 680 % 32tile_y_offset = 720 % 32


Thus, when we draw tiles, we need to offset each one with the above calculated offsets.

for (j = 0 - tile_y_offset; j < 640; j += 32){	for (i = 0 - tile_x_offset; i < 480; i += 32)	{		x = i / 32;		y = j / 32;		draw_tile(map[x][y], i, j);	}}


This is by no means perfect, it is all pseudocode, but this is generally how to accomplish smooth scrolling over your game map.

I hope this helps!
Reading MatrixCubed's post made me remember that OpenGL has automated camera moving facilities and can be used with 2d graphics which is simplified by the use of hxRender. It should help you set up your tiles as textured quads and then you can move the camera around and the tile mapping will be automated by OpenGL.

This topic is closed to new replies.

Advertisement