Autoscrolling a map, vertically

Started by
3 comments, last by GameDev.net 24 years, 5 months ago
I really need some help on this
Advertisement
I don't see anything that resembles world coordinates, like
world_wx, world_wy. This can be the coords. of the upper-left
corner of the screen where you should start extracting tile
image data from your 2D array.

Scolling vertically or horizontal increments or decrements
your world coords., and your map drawing function should take
this into account.

And there are basically two types of scrolling. One type
is tile by tile scrolling, where you scroll in tile blocks.
Such as 32 or 64 pixels at a time. This is the easiest to code,
but sometimes it looks jumpy. There is no clipping involved,
so it is fast. All the tiles are blitted to screen at fixed
positions. They never move, only the image data changes.

The other type is smooth scrolling. This is scrolling your
screen less than your tile's width or height, e.g. 1 pixel at
a time. This is slower than the first type of scrolling, but
it looks much better on a fast machine. It is slower because
you have to do clipping for all the border tiles on the screen
edges. Here, the tiles actually move! So be careful.

I would recommend that you try the first method before going
onto smooth scrolling.

Reaver

[This message has been edited by Reaver (edited October 29, 1999).]

How would I go about setting up world coordinates then?
I'm creating a game which require the map to be automatically scrolled vertically. The thing is that I can't seem to get it to work. So if someone could please look at this code, and help me out. Thanks

// Map.cpp
// Programmer: Steve Hawco
// Copyright (C) 1999, Neurotic Entertainment
//
// Work Log:
//
// 23/05/99: - Began work on some various map Functions

#include "defines.h"


int mapRow, mapIndex, tileIndex;

typedef struct
{
int posx;
int posy;
int obs;

}CELL;

CELL cells[]={
// x, y

{0,0},
{0,0},// blank wall 1
{0,64}, // 2
{0,128}, // 3
{0,192}, // 4
{0,256}, // 5
{0,320}, // 6
};


int MapOne[GAME_PORT_HEIGHT][GAME_PORT_WIDTH] = {
{3,3,3,1,2,1,3,1,1,1},
{3,3,3,1,2,1,3,1,1,1},
{3,3,3,1,2,1,3,1,1,1},
{3,3,3,1,2,1,3,1,1,1},
{3,3,3,1,2,1,3,1,1,1},
{3,3,3,1,2,1,3,1,1,1},
{3,3,3,1,2,1,3,1,1,1},
{3,3,3,1,2,1,3,1,1,1},
{3,3,3,1,2,1,3,1,1,1},
{1,1,1,1,1,1,3,1,1,1},
{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,2,1,1,1,1,1},
{1,1,1,2,1,1,1,1,1,1},
{1,1,1,2,1,1,1,1,1,1},
{1,1,1,2,1,1,1,1,1,1},
{1,1,1,2,1,1,1,1,1,1},

};

void DrawBase()
{

int tileSize = 64;
RECT tileRect;
int destx, desty;

for (mapRow = 0; mapRow < GAME_PORT_HEIGHT; mapRow++)
{
for (mapIndex = 0; mapIndex < GAME_PORT_WIDTH; mapIndex++)
{

tileIndex = MapOne[mapRow][mapIndex];
if(tileIndex > 0)
{
tileRect.left = cells[tileIndex].posx;
tileRect.top = cells[tileIndex].posy;
tileRect.right = tileRect.left + tileSize;
tileRect.bottom = tileRect.top + tileSize;

destx = mapIndex * tileSize;
desty = mapRow * tileSize-640;

BlitSurface( lpDDSOffscreen, lpDDSBack, destx,desty,&tileRect, FALSE);
}


}


while(MapOne[mapRow][mapIndex] < GAME_PORT_HEIGHT)
{
GAME_PORT_HEIGHT +32;
}
}



}



First off, I don't recommend looping through 2D array indexes.
It is much better to loop through screen pixel positions, e.g.:

// this is for a 15x15 tile grid
for(sy=0; sy < 480; sy+=32) {
for(sx=0; sx < 480; sx+=32) {
}
}

Let me give you example of world coordinates:

World coords. are usually located at the upper-left corner of the
screen and are usually in pixels. This tells you where to place your
screen on the world map. So, visualize a small rectangle (your screen)
in the middle of a huge rectangle (your world).

code:
0,0 +-----------+ |           | | 64,64     |         For example, the screen is at world coords. 64,64 |  +---+    |         on a large 4096x4096 pixel world map.  I can only |  |   |    |         see what is within the boundaries of the screen. |  +---+    | |           | |           | +-----------+             4096,4096

My tiles are 32x32 pixels, so since I scroll 32 pixels at a time, my
world coordinates wx,wy will always be multiples of 32. Initially they
are 0,0. If I scroll to the right, wx gets incremented by 32 every
time. If I scroll down, wy gets incremented by 32 every time.

So, let's say I have a huge world map, like 128x128 tiles. This is
4096 x 4096 pixels. My screen is 15x15 tiles or 480x480 pixels. That
is almost (128x128 / 15x15) 73 screens worth of scrolling! That means
my world coordinates can range from 0 to 3616. You can't scroll more
than 3616 pixels with this setup. Remember, only 480x480 pixels are
visible on the screen, so 4096-480 = 3616 max for both directions.

To get from world coordinates to 2D array indexes, you have to do a
conversion. You can simply divide by 32. This will give you your
index into your 2D array where your DD surface index is stored.

So, if my world map is at 64,64, the DD surface index of my first
tile image will come from the 2D array tilemap[64>>5][64>>5] or tilemap[2][2].

Write out all of this on paper if you need to figure it out. I
think you need to understand what world coords. are before you
write code for it.

Reaver

This topic is closed to new replies.

Advertisement