SDL moving map

Pages: 12
closed account (NUj6URfi)
If i was to recreate pokemon how would i do the moving map?
I would just have a lot of the map already loaded and show a small portion of it. Wherever the character is in the center you can subtract some to x and y for a corner of the screen. A lot of small subtractions and updates would make the screen move.
closed account (NUj6URfi)
So what is the formula?
This is one such problem where if you know how to do the prerequisites to it (drawing a map, moving player on the map), you should already understand how to do it.

To draw a [tile based] map you just loop through each tile and if it's "on screen" (which can be determined by its position+size compared to the screen size), draw it at its position.

To draw a "scrolling" tile based map, you simply offset the position of all tiles by a 'scroll' variable (ie, a value you add to each tile position). This changes which tiles are "on screen" as well as where they're drawn.

Then to "move the map" you simply have to change this scroll value and redraw the screen.



Note that this process is somewhat simplified in more modern graphic libs as you can apply a 'global' effect to shift everything over and therefore do not have to adjust each tile individually. SDL 1.x however has no such mechanism as it only does plain rect->rect copies so you're stuck having to do it yourself.
closed account (NUj6URfi)
Well, I'm a newb. I don't even know how to set map variables and create a map. So how do you do that?
Have you tried out lazy foo's sdl tutorial yet?
For beginners it is a great way to start, in my opinion.
http://lazyfoo.net/SDL_tutorials/
closed account (NUj6URfi)
I have. I look for more pointed tutorials.
If you have done the tutorial then you should be able to do this. Like Disch said:
This is one such problem where if you know how to do the prerequisites to it (drawing a map, moving player on the map), you should already understand how to do it.


Specifically I would look at lesson 6 for clip blitting and sprite sheets
Once you have a sprite sheet of tiles, you could make a map array that holds different sprite tokens. Then output a section of the array to the screen, by traversing the array and outputting the correct sprite tile.
Last edited on
closed account (NUj6URfi)
Can you post commented code? Like

[code]
//Legit code

//load images to whatever

//legit code


And how would i set map attributes like walls and have everything in a grid?
I'm not going to write complete map code and comment it just to explain a concept ;P However I will refer you to some posts I've made in the past which go over this topic in more detail:

http://www.cplusplus.com/forum/general/18315/#msg93254

http://www.cplusplus.com/forum/general/66124/#msg357372
closed account (NUj6URfi)
So now if i want to load a huge map, how would i center it on the player?
Have different tiles for the walls
1: Upper left corner
2: Upper wall
3: Upper right corner
4: Right wall
5: Lower right corner
6: Lower wall
etc.

The easiest quickest way I can think of having a map programmed would be to have an integer array that contained a number corresponding to a tile.
Then you would just iterate over a section of the map to output a section of it.

Lets say the character is at x and the 9x9 tile map looks like this:
/-------\
|       |
|   x   |
|       |
\-------/


But the screen only has enough space for 3x3 tiles because I am programming for something with a tiny screen in this example.
So the output on the screen would be:
   
 x 
   


The current section of the map would be the rectangle from (1, 3) to (3, 5) inclusive and zero-based (also x and y is swapped because that usually how matrices are indexed)
Then to shift the view 1 to the right you would output the rectange (1, 4) to (3, 6)

Hope that helps.
closed account (NUj6URfi)
So wait i renamed the variables and i don't get this code.

1
2
        DrawImage( /* draw 32x32 pixels from whatever tileset image you have.  Use t.nGfxX
                 and t.nGfxY as the source coords for the image */ );


What does it mean? I don't get the drawimage function.
Last edited on
closed account (NUj6URfi)
Plus this code has a error:

1
2
3
1,1,1,1,1,1,1,1,1,1
};
const TileProperties& GetTile(int PLAYERTOTAL_WIDTH, int PLAYERTOTAL_HEIGHT){



Plus how do you get the floor to be a different texture than the wall? It doesn't look like it does that.
Last edited on
closed account (NUj6URfi)
Well?
closed account (NUj6URfi)
Also, I renamed map to room1 and set room (which is an int) to equal room1 yet i get an error of room1 undeclared. WHy is that?
closed account (NUj6URfi)
Wait i fixed the undeclared error.
He was giving you some commented code. You are going to need to translate the comments into code.
closed account (NUj6URfi)
Okay let me clarify. Right now my error is that i get a :

expected init-declarator before '&' token
expected `,' or `;' before '&' token

for the code line:

const TileProperties& GetTile(int a, int b){
Last edited on
You need to define the type TileProperties then I think.

Edit:
1
2
3
4
5
6
struct TileProperties
{
    bool bWall;  // true if the tile is a solid wall
    int  nGfxX;  // X coord of the graphic
    int  nGfxY;   // Y coord of the graphic
};


Try this definition instead
1
2
3
4
5
6
typedef struct TileProperties
{
    bool bWall;  // true if the tile is a solid wall
    int  nGfxX;  // X coord of the graphic
    int  nGfxY;   // Y coord of the graphic
}TileProperties;


Or another solution would be to type out struct TileProperties everywhere.
Last edited on
Pages: 12