Jump to content
  • Advertisement
Sign in to follow this  
exgex

directx c++ unmanaged help (tiles)

This topic is 3983 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i now can display a background made of tiles and a custom map format, but i want to increase the map size which will make it bigger than the screen, so i will need to scroll it ... can someone tell me how do i make a map scroll thx also for my map editor, how do i make it so the user can just click and the tile will be drawn there thx [Edited by - exgex on January 12, 2008 10:06:57 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by exgex
i now can display a background made of tiles and a custom map format, but i want to increase the map size which will make it bigger than the screen, so i will need to scroll it ...
can someone tell me how do i make a map scroll thx


If we assume that your map is stored as some kind of 2D array of tile indicies, which I normally implement via a 1D vector and an overloaded operator() for lookup, the first thing to do is to set up a pair of variables that represent the position of the top-left corner of the screen in world co-ords (say, int Ox,Oy).

Rendering your map then becomes a case of something like this:


int Ox=0,Oy=0;
int TileSize=64; // or whatever

int TilesAcross=18; // number that fit across +1
int TilesDown=13; // number that fir down +1

GameMap Map; // supports operator()(int x,int y) for lookup

void RenderMap()
{
int Mx=Ox/TileSize,My=Oy/TileSize; // which map cell is top left?
int Dx=Ox%TileSize,Dy=Oy%TileSize; // how far across the top left cell are we in pixels?

for(int Y=0;Y<TilesDown;++Y)
{
for(int X=0;X<TilesAcross;++X)
{
int Px=X*TileSize-Dx;
int Py=Y*TileSize-Dy;

// Px,Py represents screen position to draw given cell

int MapCellIndex=Map(X+Mx,Y+My);

// MapCellIndex represents actual tile index at given cell

RenderMapTileAt(Px,Py,MapCellIndex);
}
}
}


By changing the values of Ox and Oy, thus changing the top-left corner of the screen in terms of the world co-ords, this will render the appropriate view of the map on the screen.

TilesAcross and TilesDown are just the number of tiles that fit across and down the screen respectively, plus one each to account for the case where you are scrolled halfway across a tile.

Quote:
Original post by exgex
also for my map editor, how do i make it so the user can just click and the tile will be drawn there
thx


That's a pretty general question. Again, I normally maintain the map in a 2D structure of some kind and render from this structure similarly to the above every frame. When the user clicks a cell, I just calculate which map cell the mouse is above and update that cell with the currently selected map cell index.

If I'm working in a Win32 based editor, I'll then just redraw the cell that has changed.

Share this post


Link to post
Share on other sites
for the rendering it does actually scroll, but it doesnt scroll right, like when i press up, it scrolls down until a certain point then it jumps back to the start and starts scrolling down again....

my code


int a[SCREEN_WIDTH/TILE_SIZE][SCREEN_HEIGHT/TILE_SIZE];
#define TILE_SIZE 16
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
void GAME_Draw()
{
int Mx=world_x/TILE_SIZE,My=world_y/TILE_SIZE; // which map cell is top left?
int Dx=world_x%TILE_SIZE,Dy=world_y%TILE_SIZE; // how far across the top left cell are we in pixels?

// Texture being used is 64 by 64:
D3DXVECTOR2 spriteCentre=D3DXVECTOR2(TILE_SIZE/2,TILE_SIZE/2);
// Screen position of the sprite
D3DXVECTOR2 trans=D3DXVECTOR2(0.0f,0.0f);
// Build our matrix to rotate, scale and position our sprite
D3DXMATRIX mat;
D3DXVECTOR2 scaling(0.98f,0.97f);
// out, scaling centre, scaling rotation, scaling, rotation centre, rotation, translation
D3DXMatrixTransformation2D(&mat,NULL,0.0,&scaling,&spriteCentre,NULL,&trans);
// Tell the sprite about the matrix
til_dirt->SetTransform(&mat);
til_dirt->Begin(D3DXSPRITE_ALPHABLEND);

for(w=0;w<=SCREEN_WIDTH/TILE_SIZE;w++)
{
for(h=0;h<=SCREEN_HEIGHT/TILE_SIZE;h++)
{

int Px=h*TILE_SIZE-Dx;
int Py=w*TILE_SIZE-Dy;

int MapCellIndex=a[h+Mx][w+My];

D3DXVECTOR3 center(0.0f,0.0f,0.0f); //center at 0,0
D3DXVECTOR3 position(Px, Py,0.0f);
//decide
if(a[w][h] == 1)
til_dirt->Draw(tex_dirt, NULL, &center, &position, D3DCOLOR_ARGB(255,255, 255, 255));
else if(a[w][h] == 2)
til_dirt->Draw(tex_block, NULL, &center, &position, D3DCOLOR_ARGB(255,255, 255, 255));
//end decision
y += TILE_SIZE;
}
x += TILE_SIZE;
}
til_dirt->End();
}

case WM_KEYDOWN:
if(wParam == VK_DOWN)
world_y ++;
else if(wParam == VK_UP)
world_y --;
else if(wParam == VK_LEFT)
world_x --;
else if(wParam == VK_RIGHT)
world_x ++;
return 0;

for(int i=0;i<=SCREEN_WIDTH/TILE_SIZE;i++)
{
for(int j=0;j<=SCREEN_HEIGHT/TILE_SIZE;j++)
{
if(rand() % 100 <= 50)
a[j] = 1;
else
a[j] = 2;
}
}





atm, i just use a temp array to fill the map

my actual screen is only 640x480 while the map is much bigger (1024x768) so the user would definitely have to scroll

these are just snippets, if the complete code helps more feel free to pm me or reply here and i shall post the full zipped file.

thanks again

[Edited by - exgex on January 14, 2008 6:49:26 AM]

Share this post


Link to post
Share on other sites
You are failing to use Mx and My to modify your indexing into the map array, so you are always rendering the top left section.


if(a[w][h] == 1)


needs to be


if(a[w+Mx][h+My] == 1)


and similarly for the second if statement.

w and h are quite misleading names for these variables, and you seem to have an unused x and y in there that would be better names for these.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!