Skip to main content
GameDev.net gamedev.net
🔒 Locked

smooth scrolling map

Started by gameprogrammerwiz Jul 2, 2000 at 10:55 AM 6 replies 550+ views
Original Post
gameprogrammerwiz
gameprogrammerwiz
ok, i was reading Jim Adam's tutorial so that i can add smooth scolling to my tile engine...and im not sure that i understand it all that well. here's my code (sorry that its so hard to understand) void draw_map(void) { LPDIRECTDRAWSURFACE pdds; HRESULT ddrval; RECT mike; haxor = 1; for(int blah_y = 0; blah_y < 16; blah_y++) { for(int blah_x = 0; blah_x < 16; blah_x++) { offset_y = tile_y & 15; offset_x = tile_x & 15; mike.top = blah_y * 22 - offset_y; mike.left = blah_x * 22 - offset_x; mike.right = 44; mike.bottom = 44; pdds = lpddstiles[map[pos_y + blah_y][pos_x + blah_x]]; ddrval = lpddsback->BltFast(tile_x, tile_y, pdds, &mike, DDBLTFAST_SRCCOLORKEY); tile_x = tile_x + 44; } if(haxor == 1) { tile_y = tile_y + 22; tile_x = 22; } if(haxor == 2) { tile_y = tile_y + 22; tile_x = 0; } if(haxor == 1) { haxor = 2; } else haxor = 1; } tile_x = 0; tile_y = 0; } well, as you could have guessed, its not working all of that well. i get messed-up results each time i try to change anything. i know that i'm right on the edge of getting this right...there must be somthing that i'm incorrectly calculating. any ideas? (btw, the haxor integer is for my isometric stuff, so ignore it) Edited by - gameprogrammerwiz on 7/2/00 11:52:28 AM
==============================
whats a signature?
htm[s]l[/s]
Steven
Steven
Ok I''m making a few assumtions from your code here:

Your tiles are 44 x 22

First off the offsets are for tiles that are 16x16, so change the offsets to read:

offset_y = tile_y % tileheight;
offset_x = tile_x % tilewidth;

secondly, if your tiles are of the iso variety your y increase should be half the tile height.

the following lines:
pdds = lpddstiles[map[pos_y + blah_y][pos_x + blah_x]];
ddrval = lpddsback->BltFast(tile_x, tile_y, pdds, &mike, DDBLTFAST_SRCCOLORKEY);

reference pos_x, and pos_y these are not set anywhere.

Try the following bit of code it might fix your problem :

void draw_map(void)
{
LPDIRECTDRAWSURFACE pdds;
HRESULT ddrval;
RECT mike;

haxor = 1;

for(int blah_y = 0; blah_y < 16; blah_y++)
{
for(int blah_x = 0; blah_x < 16; blah_x++)
{
offset_y = tile_y % tileheight;
offset_x = tile_x % tilewidth;
xtile = tile_x / tilewidth;
ytile = tile_y / tileheight;

mike.top = 0;
mike.left = 0;
mike.right = tilewidth;
mike.bottom = tileheight;

pdds = lpddstiles[map[ytile + blah_y][xtile + blah_x]];
ddrval = lpddsback->BltFast(blah_x * tilewidth - offset_x, blah_y * tileheight - offset_y, pdds, &mike, DDBLTFAST_SRCCOLORKEY);

tile_x = tile_x + tilewidth;
}

tile_y = tile_y + 22;

if(haxor == 1)
{
tile_x = tilewidth >> 1;
}
else
{
tile_x = 0;
}

haxor = 1 - haxor;

tile_x = 0;
tile_y = 0;
}


I''m not 100% certain about the exact syntax of the Direct Draw calls, so check them in the SDK help. I''m assuming that you have 1 tile per surface, and each surface is tilewidth * tilwheight in size.

If you need any more help contact me and I will try and explain it better.
When I find my code in tons of trouble,Friends and colleages come to me,Speaking words of wisdom:"Write in C."My Web Site
gameprogrammerwiz
gameprogrammerwiz
actually, pos_x and pos_y are set in another function that conrols keypresses.

i played around with the code for a while and i got it to work...but not smooth scrolling. my tiles are 44x44 in size, not 44x22. HOWEVER: if haxor is equal to 2 then tile_y only adds 22, because of the isometric tile size. nothing about the haxor integer needs to be changed.

i pretty much got it back to the same point i had it at before i tried to implement smooth scrolling.

=================================================================

void draw_map(void)
{
LPDIRECTDRAWSURFACE pdds;
HRESULT ddrval;
RECT mike;

haxor = 1;

for(int blah_y = 0; blah_y < 16; blah_y++)
{
for(int blah_x = 0; blah_x < 16; blah_x++)
{
offset_y = tile_y % 22;
offset_x = tile_x % 44;


mike.top = 0;
mike.left = 0;
mike.right = 44;
mike.bottom = 44;

pdds = lpddstiles[map[pos_y + blah_y][pos_x + blah_x]];
ddrval = lpddsback->BltFast(blah_x * 44 - offset_x, blah_y * 22 - offset_y, pdds, &mike, DDBLTFAST_SRCCOLORKEY);

tile_x = tile_x + 44;
}

if(haxor == 1)
{
tile_y = tile_y + 22;
tile_x = 22;
}

if(haxor == 2)
{
tile_y = tile_y + 22;
tile_x = 0;
}

if(haxor == 1) { haxor = 2; }
else haxor = 1;
}

tile_x = 0;
tile_y = 0;
}

Edited by - gameprogrammerwiz on July 3, 2000 4:00:01 AM
==============================
whats a signature?
htm[s]l[/s]
Steven
Steven
OK, so I take it that pos_x and pos_y refer to the top left tile and not the top left point to be displayed. By this i mean that pos_x % 44 == 0 and the same with the pos_y co-ord.

If that is the case then I''ll assume that the tile_x and tile_y are your world co-ords (but not in iso style), i.e. tile_x goes from 0 to 44 * map width and tile_y goes from 0 to 22 * map height.

Something I''ve just noticed - why are you setting tile_x and tile_y to 0 at the end of the draw loop?

Did you try the code I supplied you with by changing tilewidth and tileheight to your tile width and height values? The reason I ask this is I''ve just finnished implementing the smooth scrolling in a game I''m developing at the moment and I got it to work within about an hour of starting. Granted that I''m using a 2D 32x32 square tileset, converting this code to ISO tiles should be easy.

Here''s the code I''m using :

void DXMap::DrawMap(LPDIRECTDRAWSURFACE7 dest,int world_x, int world_y)
{
int xo, yo;
int tx, ty;
int stx, sty;
int x, y;
int s;

xo = world_x % Tiles->TileWidth;
yo = world_y % Tiles->TileHeight;

stx = tx = ( world_x / Tiles->TileWidth ) % m_nMapWidth;
sty = ty = ( world_y / Tiles->TileHeight ) % m_nMapHeight;

x = -xo;
y = -yo;

while( y < eng->m_nScreenHeight )
{
while( x < eng->m_nScreenWidth )
{
s = tx + ( ty * m_nMapWidth );
Tiles->TransBlt( dest, x, y, m_Map[s].Tile );
tx++;
tx %= m_nMapWidth;
x += Tiles->TileWidth;
}
x = -xo;
tx = stx;
y += Tiles->TileHeight;
ty ++;
ty %= m_nMapHeight;
}
}


in the above code eng is a class that I use to hold the main DX surfaces, Tiles is a class that contains all of my tiles, and the variable names are self explantary ( i think so anyway )

One tip for you, since mike is the same for all surfaces set it outside of the render loop, this will increase the speed of the engine slightly.

the code you have looks like it should work, I''m not working on an iso system at the moment, but what you have there is correct.

Have you a screen shoot of the results you get?
When I find my code in tons of trouble,Friends and colleages come to me,Speaking words of wisdom:"Write in C."My Web Site
gameprogrammerwiz
gameprogrammerwiz
ok, ive tried two more engine's code, (the isometrix code and i tried Jame's McCue's code too) and neither of them worked correctly.

steven, i tried the code you gave me, and it only blit one tile in the top-left corner of the screen.

heres the code (i converted some of the params to fit with my existing code):

void draw_map(void)
{
LPDIRECTDRAWSURFACE pdds;
HRESULT ddrval;

int xo, yo;
int tx, ty;
int stx, sty;
int x, y;
int s;

xo = pos_x % 44;
yo = pos_y % 44;

stx = tx = (pos_x / 44) % 32;
sty = ty = (pos_y / 44) % 32;

x =- xo;
y =- yo;

while(y < 16)
{
while(x < 16)
{
s = tx + (ty * 32);

pdds = lpddstiles[map[pos_y + y][pos_x + x]];
ddrval = lpddsback->BltFast(tx, ty, pdds, NULL, DDBLTFAST_SRCCOLORKEY);

tx++;
tx %= 32;
x += 44;
}

x =- xo;
tx = stx;
y += 44;
ty++;
ty %= 44;
}
}

by the way, my map is 32x32 tiles in size, but i only blit 16 of them at a time (in other words: a viewport).

Edited by - gameprogrammerwiz on July 3, 2000 3:34:00 PM
==============================
whats a signature?
htm[s]l[/s]
Steven
Steven
Hmm, well that code would only blit one square (since I was using the full screen I moved my x and y co-ord by the tile width and height respectivly.

Ok here''s a fix (i think)


void draw_map(void)
{
LPDIRECTDRAWSURFACE pdds;
HRESULT ddrval;

int xo, yo;
int tx, ty;
int stx, sty;
int x, y;

xo = pos_x % 44;
yo = pos_y % 44;

stx = tx = (pos_x / 44) % 32;
sty = ty = (pos_y / 44) % 32;

x =- xo;
y =- yo;

while(y < 16 * 22)
{
while(x < 16 * 44)
{
pdds = lpddstiles[map[pos_y + ty][pos_x + tx]];
ddrval = lpddsback->BltFast(x, y, pdds, NULL, DDBLTFAST_SRCCOLORKEY);

tx++;
tx %= 32;
x += 44;
}

x =- xo;
tx = stx;
y += 44;
ty++;
ty %= 44;
}
}


I removed the s variable as you are not using it, I may have the first while statement wrong, if so try while( y < 16 * 44 )

If this does not work, then I''ll eat a big book o programming and try again 8)

When I find my code in tons of trouble,Friends and colleages come to me,Speaking words of wisdom:"Write in C."My Web Site
Possibility
Possibility
Ok, here is a suggestion for you guys, when you write code do this:

        
Before all of the code put in:
"[source]"
then the code here
then after all of the code put in
"[/source]"

but of course dont put in the qutoes

if you want to see how it is done, click on the Edit Message for my this very post and you can see


Possibility


Edited by - Possibility on July 3, 2000 3:59:05 PM

Edited by - Possibility on July 3, 2000 4:00:02 PM
gameprogrammerwiz
gameprogrammerwiz
ok, i got it working (sort of).

here it is: isotile.zip (60k)

i couldnt implement my haxor function, so now it looks kinda funny.

i'll play around with this some more to get it working correctly. i think that i understand it.

=================================================================

i just noticed that my webserver is down right now (due to the freedom of communication law)...try it and if its still down try again later.

Edited by - gameprogrammerwiz on July 3, 2000 4:36:03 PM

Edited by - gameprogrammerwiz on July 3, 2000 4:40:57 PM
==============================
whats a signature?
htm[s]l[/s]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.