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
how to implementation Role camera with SDL ?
matrixbirds


Joined: 30 Aug 2013
Posts: 4
my function
Code:
 loadPictureRes()  // from picture file 

Code:
 loadMap()  // from context file  which save the maps infomation

Code:
 renderMap() //tile the all picture and composition a map


Role camera: one guy moves and visible area of the map will following change
and now, i have no ideal to deal the Role camera...

Rolling Eyes
mars1990


Joined: 21 Aug 2013
Posts: 10
Location: Poland
Improve your English since it's REALLY hard to understand what you're trying to achieve.

Tell us something more about your game. Is it top-down RPG? You're using tile maps? Provide some info. Link to the screenshot from the game could help too.

From this...spaghetti...you wrote, I assume that you're trying to make your character stand in the middle of the window/screen while moving around the map in some tile-based game.
matrixbirds


Joined: 30 Aug 2013
Posts: 4
mars1990 wrote:
Improve your English since it's REALLY hard to understand what you're trying to achieve.

Tell us something more about your game. Is it top-down RPG? You're using tile maps? Provide some info. Link to the screenshot from the game could help too.

From this...spaghetti...you wrote, I assume that you're trying to make your character stand in the middle of the window/screen while moving around the map in some tile-based game.


yes i want to make the character stand in the middle of the window/screen while moving around the map in some tile-based game...
mars1990


Joined: 21 Aug 2013
Posts: 10
Location: Poland
Ok. So basically you want to keep your character in fixed screen coordinates and alter his world/map coordinates. The world coordinates are simply the Xth and Yth tile of the map. So you set fixed screen coordinates to be in the middle of the screen. Then you apply some movement to the map coordinates (don't get confused. those map coordinates are attributes of the player, not the world), so by pressing arrow up
(in y-axis pointing downwards) you substract 1 from y, by pressing arrow down you add 1 to y, etc.. Then you pass your character's world coordinates to the camera entity which takes them as the center of camera (width and height of camera are fixed values in tiles, like 10 by 10, or whatever you would like to have). And in the drawing routine, the map takes camera's coordinates and performs double for loop in order to draw tiles that are only within the camera.
matrixbirds


Joined: 30 Aug 2013
Posts: 4
mars1990 wrote:
Ok. So basically you want to keep your character in fixed screen coordinates and alter his world/map coordinates. The world coordinates are simply the Xth and Yth tile of the map. So you set fixed screen coordinates to be in the middle of the screen. Then you apply some movement to the map coordinates (don't get confused. those map coordinates are attributes of the player, not the world), so by pressing arrow up
(in y-axis pointing downwards) you substract 1 from y, by pressing arrow down you add 1 to y, etc.. Then you pass your character's world coordinates to the camera entity which takes them as the center of camera (width and height of camera are fixed values in tiles, like 10 by 10, or whatever you would like to have). And in the drawing routine, the map takes camera's coordinates and performs double for loop in order to draw tiles that are only within the camera.


i use textfile to save the map infomation , then i tile all of them....

my idea: make the 'visual area' array e.g. the array size 2*2 just tile the part of the map,
then the character move will update the array

textfile :
0 1 2 3 4
0 X X X X X
1 X X X X X
2 X X X X X
3 X X X X X

use a [4 * 5] array to save map infomation

tile the visual area:

1.assume the character original area
0 1
0 X X
1 X X

2.move to the right (one step)
1 2
1 X X
2 X X

just tile these ignore other

i think its complex, do u have another idea?
matrixbirds


Joined: 30 Aug 2013
Posts: 4
Code:
 
          0 1
        0 X X
        1 X X

          1 2
        0 X V
        1 X O
           
mars1990


Joined: 21 Aug 2013
Posts: 10
Location: Poland
I don't get why there are those 0 1 2 3...
Store only tiles. Indexes are redundant. You can load whole map into the memory (even 1000x1000 is not that much). Then you only need to loop through what you've loaded, taking into account the player's position (viewport to be specific). So when your character is at (20,30) and your screen is 13x9 tiles, you want to loop like this:
Code:
for(int h = camPosY; h < camPosY + camHeight; h++)
{
   for(int w = camPosX; w < camPosX + camWidth; w++)
   {
      int textureIndex = layer[h][w];
      pos.x = w*tileWidth - camPosition.x*tileWidth;
      pos.y = h*tileHeight - camPosition.y*tileHeight;
      CWindow::Draw(tileTextures.at(textureIndex), pos, NULL, 0);
   }
}

Where camPosY is 30-4, camPosX is 20-6 (simplified). camHeight is camPosY + 9, camWidth is camPosX +13.
I calculate camPosX and Y in this way:
Code:
mPosition.x = position.x + position.w/2 - mPosition.w/2;

mPosition stores X and Y coordinates of the camera. position stores player's position.

I wrote everything by myself. I store my maps as:
00002
11102
00102
00102
00111
Where numbers are indexes for tile files. So in the line with int textureIndex = layer[h][w]; I take the tile index at actual position from loaded map/layer, then I calculate X and Y position where it should be placed on the screen (in pixels, that's why there is tileWidth being used), and eventually I call my drawing function.