// FILE: TileMap.h by Richard Hughes. 30/11/2003
//
// DESC: This declares the CTileMap and assosiated classs
#pragma once
// INCLUDES
#include "MemoryManager.h"
#include "ErrorLogging.h"
#include "Graphics.h"
#include "Image.h"
#include "Util.h"
#include "FileInfo.h"
/*********************************************************************************************/
//
// CLASS: tagTile
//
// DESC: This is a tile
//
/*********************************************************************************************/
typedef struct tagTile : public IMemObject {
tagTile( void )
{
}
~tagTile( void )
{
}
unsigned char* m_pData; // the image data
long m_lWidth, // the tiles width
m_lHeight; // the tiles height
unsigned long m_dwFormat; // RGB RGBA
unsigned long m_dwInternalFormat; // RGB RGBA
QUICK_SIZE;
}TILE, *LPTILE;
/*********************************************************************************************/
//
// CLASS: CTileMap
//
// DESC: This handles tilemaps. see doc for more info
//
/*********************************************************************************************/
class CTileMap : public IError {
CPointer < TILE > m_Tiles; // the tiles in the tilemap
public:
CTileMap( void )
{
}
~CTileMap( void )
{
}
// this loads in a tile map directly
ERROR_STRING Load( string strFName, int nHeight, int nWidth, long lTWidth, long lTHeight,
int nHBorder, int nVBorder );
// this loads certain tiles from a tilemap directly
ERROR_STRING Load( string strFName, int nHeight, int nWidth, long lTWidth, long lTHeight,
int nHBorder, int nVBorder, pint pList );
// this loads in a tilemap directly via a tilemap file
ERROR_STRING Load( string strFName );
// this loads in a tilemap indirectly via a tilemap file
ERROR_STRING Load( string strFName, pint pList, pstring pNames );
// this returns a tile
inline TILE& GetTile( int nIndex )
{
return ( m_Tiles[ nIndex ] );
} // end GetTile
QUICK_NAME( "CTileMap" );
QUICK_SIZE;
}; // end CTileMap
// EOF: TileMap.h
// FILE: TileMap.cpp by Richard Hughes. 30/11/2003
//
// DESC: This defines the CTileMap class
// INCLUDES
#include "TileMap.h"
/*********************************************************************************************/
//
// CLASS: CTileMap
//
// DESC: This handles tilemaps. see doc for more info
//
/*********************************************************************************************/
// this loads in a tilemap
// it takes the tilemaps filename, the amount of cells heigh and wide, the height and the width of the tiles,
// and the horizontal and vertical border
// it returns the nessercary ERROR_STRING error
ERROR_STRING CTileMap::Load( string strFName, int nHeight, int nWidth, long lTWidth, long lTHeight,
int nHBorder, int nVBorder )
{
Enter( "Load_Direct" );
int nBPP = 0; // the images BPP
// load in the tilemap
CImage Image;
ERROR( Image.Load( strFName ) );
// init the tiles
m_Tiles.Init( nWidth*nHeight );
// set the BPP
nBPP = ( Image.GetFormat( ) == CF_RGB ) ? 3 : 4;
// compute the tile area
UCHAR *pSource, *pDest; // pointers for saving
long Width = lTWidth * nBPP;
long Height = lTHeight * nBPP;
long TileArea = Width*Height;
/*
extract the image data into the cells
*/
// get the image data
unsigned char* p = Image.GetData( );
pSource = Image.GetData( );
// get the image width in bits ( RGB )
long lImageWidth = Image.GetWidth( ) * nBPP;
long l = 0;
// loop thru and load in all of the cells
for ( long idx = 0; idx < nWidth; idx++ )
for ( long idy = 0; idy < nHeight; idy++ ) {
l = idx+( idy*nWidth );
// set the image stats
m_Tiles[ l ].m_pData = new unsigned char[ TileArea ];
memset( m_Tiles[ l ].m_pData, 0, sizeof ( unsigned char ) * TileArea );
m_Tiles[ l ].m_lWidth = lTWidth;
m_Tiles[ l ].m_lHeight = lTHeight;
m_Tiles[ l ].m_dwFormat = Image.GetFormat( );
m_Tiles[ l ].m_dwInternalFormat = Image.GetInternalFormat( );
pDest = m_Tiles[ l ].m_pData;
int cx = idx*Width;
int cy = idy*lTHeight;
// extract bitmap data
pSource = Image.GetData( ) + cy*lImageWidth+cx;
for ( int y = 0; y < lTHeight; y++ )
{
// copy over the first line of the tile
memcpy( pDest, pSource, Width );
// goto the next line
pSource += lImageWidth;
pDest += Width;
} // end for
} // end for
RETURN_SUCCESS;
} // end CTileMap::Load
// FILE: 25DMap.h by Richard Hughes. 19/02/2005
//
// DESC: This file defines the 25DMap and assosiated classes
#ifndef __25DMap_h__
#define __25DMap_h__
// INCLUDES
#include <vector>
#include "ErrorLogging.h"
#include "Mesh.h"
#include "Tilemap.h"
#include "Animation.h"
#include "SpriteManager.h"
using namespace std;
/*********************************************************************************************/
//
// CLASS: CTile
//
// DESC: This is a tile
//
/*********************************************************************************************/
class CTile : public IError {
char m_strIdx[ MAX_STRING_LENGTH ]; // the tiles index
CAnimation m_Anim; // this tiles animation
int m_nFrame; // the current animation frame
CPointer < CQuad > m_pTiles; // the tile animations
float m_fX, // the tiles x pos
m_fY; // the tiles y pos
int m_nW, // the tiles width
m_nH; // the tiles height
float m_fSW, // the scale width
m_fSH; // the scale height
public:
CTile( void )
{
m_nFrame = 0;
m_fX = m_fY = 0.0f;
m_nW = m_nH = 0;
m_fSW = m_fSH = 1.0f;
}
~CTile( void )
{
}
// this loads the tile
ERROR_STRING Load( CPointer < CTileMap > pMap, int w, int h, string strFName );
//.....
#endif /* __25DMap_h__*/
// EOF: 25DMap.h
// FILE: 25DMap.cpp by Richard Hughes. 19/02/2005
//
// DESC: This file defines the 25DMap and assosiated classes
// INCLUDES
#include "25DMap.h"
#include "TexturePool.h"
#include "Texture.h"
#include "Sprite.h"
/*********************************************************************************************/
//
// CLASS: CTile
//
// DESC: This is a tile
//
/*********************************************************************************************/
// this loads the tile
// it takes the tilemap to load the tiles from, the tile dimentions and the tile file name
// it returns the necessary ERROR_STRING error
ERROR_STRING CTile::Load( CPointer < CTileMap > pMap, int w, int h, string strFName )
{
Enter( "Load" );
if ( !pMap )
RETURN_POINTER_NULL( "pMap" );
FILE* fp = NULL; // the file pointer
string str = ""; // temp
int n = 0; // temp
float f = 0.0f; // temp
bool bKey = false; // whether to use a color key for this tile
int r1 = 0; // the min red component of the color key
int r2 = 0; // the max red component of the color key
int g1 = 0; // the min green component of the color key
int g2 = 0; // the max green component of the color key
int b1 = 0; // the min blue component of the color key
int b2 = 0; // the max blue component of the color key
int nSize = 0; // the size of the animation
pint pAnim; // the animation indexes
pfloat pTimes; // the frame times
// set the tile dimentions
m_nW = w; m_nH = h;
// load in the tile file
// open the file
fp = fopen( strFName.data( ), "r" );
if ( POINTER_NULL( fp ) )
RETURN_NOT_OPENED( strFName );
// read in the index
fscanf( fp, "%s", m_strIdx );
// read in the number of frames
fscanf( fp, "%d", &nSize );
// init the animations
m_pTiles.Init( nSize );
pAnim.Init( nSize );
pTimes.Init( nSize );
// read in the animations
for ( int i = 0; i < nSize; i++ )
{
fscanf( fp, "%d", &n );
fscanf( fp, "%f", &f );
pAnim[ i ] = n;
pTimes[ i ] = f;
} // end for
// read in the color key if there is one
if ( fscanf( fp, "%d", &r1 ) != EOF &&
fscanf( fp, "%d", &r2 ) != EOF &&
fscanf( fp, "%d", &g1 ) != EOF &&
fscanf( fp, "%d", &g2 ) != EOF &&
fscanf( fp, "%d", &b1 ) != EOF &&
fscanf( fp, "%d", &b2 ) != EOF )
bKey = true;
// close the file
fclose( fp );
fflush( fp );
fp = NULL;
// load in the indexes
for ( i = 0; i < nSize; i++ )
{
// setup the quad
m_pTiles[ i ].Init( w,h, 0,0, VB_TEX2D_1 );
// load in the tile
str = strFName + ToString( " : %d", i );
CTexturePool::GetSingleton( ).LoadTex( pMap->GetTile( *pAnim[ i ] ), str, bKey, r1,r2, g1,g2, b1,b2 );
// attach tex
CTexturePool::GetSingleton( ).AttachTex( &m_pTiles[ i ], str, false );
// set the animation index
pAnim[ i ] = i;
} // end for
// create the animation
m_Anim.Set( pAnim, pTimes, 0, m_strIdx );
RETURN_SUCCESS;
} // end CTile::Load
// EOF: 25DMap.cpp

