Archived

This topic is now archived and is closed to further replies.

Error in Tile code from Programming Roleplaying Games with DirectX

This topic is 4944 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

hey, I tried using the tile engine code from the book, and it works great, until I try using smooth scrolling, here's the code:
//Tile Engine code


BOOL cMap::Render(long XPos, long YPos, 
                  long NumRows, long NumColumns,
                  long ObjectLayer)
{
  long MapX, MapY; 
  long XOff, YOff;
  long Layer, Row, Column, i;
  char TileNum;
  char *MapPtr;

  // Error checking

  if(m_Map == NULL || m_Tiles == NULL)
    return FALSE;

  // Calculate smooth scrolling variables

  MapX = XPos / m_Tiles->GetWidth(0);
  MapY = YPos / m_Tiles->GetHeight(0);
  XOff = XPos % m_Tiles->GetWidth(0);
  YOff = YPos % m_Tiles->GetHeight(0);

  // Loop through each layer

  for(Layer=0;Layer<m_NumLayers;Layer++) {
    MapPtr = &m_Map[Layer*m_Width*m_Height];

    // Loop for each row and column

    for(Row=0;Row<NumRows+1;Row++) {
      for(Column=0;Column<NumColumns+1;Column++) {
        TileNum = MapPtr[(Column + XPos)*m_Width+Row+YPos];
        m_Tiles->Draw(0, TileNum, Row * m_Tiles->GetWidth(0) - XOff, Column * m_Tiles->GetHeight(0) - YOff);
      }
    }

    // Draw objects if on object layer

    if(Layer == ObjectLayer) {
      for(i=0;i<m_NumObjectsToDraw;i++)
        m_Tiles->Draw(0, m_Objects[i].Tile, m_Objects[i].XPos - XOff, m_Objects[i].YPos - YOff);
    }
  }

  return TRUE;
}
//My Code

BOOL cApp::Init()
{
	m_Graphics.Init();
	m_Graphics.SetMode(GethWnd(), TRUE, FALSE);
	ShowMouse(TRUE);

	m_Tiles.Create(&m_Graphics, 1);
	m_Tiles.Load(0, "Data\\Tile.bmp", 64, 64, D3DCOLOR_RGBA(0, 0, 0, 255));
		
	char MapData[10][10] = { 
		{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{ 0, 0, 1, 1, 1, 1, 1, 1, 0, 0},
		{ 0, 0, 1, 2, 2, 2, 2, 1, 0, 0},
		{ 0, 0, 1, 2, 3, 3, 2, 1, 0, 0},
		{ 0, 0, 1, 2, 3, 3, 2, 1, 0, 0},
		{ 0, 0, 1, 2, 2, 2, 2, 1, 0, 0},
		{ 0, 0, 1, 1, 1, 1, 1, 1, 0, 0},
		{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
	};

	m_Map.Create(1, 10, 10);
	m_Map.SetMapData(0, (char*)&MapData);
	m_Map.UseTiles(&m_Tiles);

	return TRUE;
}
BOOL cApp::Frame()
{
	if (m_Graphics.BeginScene())
	{
	m_Graphics.ClearDisplay(D3DCOLOR_RGBA(255, 255, 255, 255));
	long xoff = 0, yoff = 0; //Offset Variables, if not set to 0, the Tile's aren't rendered right

	m_Map.Render(xoff, yoff, 10, 10, 0);
	m_Graphics.EndScene();
	m_Graphics.Display();
	}

	return TRUE;
}
  
any help is appreciated. [edited by - Darc on May 30, 2004 4:19:42 PM]

Share this post


Link to post
Share on other sites