#include "CRM32Pro.h"
#include "../source/MSTE.h"
#include "../source/MSTE_MapData.h"
#define EXAMPLE_VERSION "MSTE - Example04: Custom map"
{
public:
cMyCustomMap(int height,int width);
~cMyCustomMap();
int CellRender(
int map_x,
int map_y,
int surf_x,
int surf_y,
cMSTE *TE,
int iOpacity);
public:
unsigned int **iCustomMap;
int iW, iH;
};
void RenderGraphics(int);
int main(int argc,char *argv[])
{
Uint8 done = 0;
SDL_Event event;
ILogSystem.Init("Example04_Custom.log",LOG_FILE,LOG_NORMAL,EXAMPLE_VERSION);
if(CRM32Pro.Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0)
{
ILogSystem.Msg(LOG_NORMAL," [LOG] - Couldn't initialize CRM32Pro: %s\n",SDL_GetError());
return 1;
}
CRM32Pro.Config.Title = EXAMPLE_VERSION;
CRM32Pro.Config.VideoWidth = 640;
CRM32Pro.Config.VideoHeight = 400;
CRM32Pro.Config.bMTFriendly = 1;
if(!CRM32Pro.SetVideoMode())
{
CRM32Pro.Quit();
return 1;
}
ITimeSystem->Init();
ITimeSystem->SetRate(60,20);
CRM32Pro.SetRenderCallback(RenderGraphics);
cMyCustomMap *myMap;
{
myMap->iCustomMap[2][2] = 10;
myMap->iCustomMap[3][3] = 10;
myMap->iCustomMap[4][4] = 10;
myMap->iCustomMap[5][5] = 10;
myMap->iCustomMap[6][6] = 10;
myMap->iCustomMap[7][7] = 10;
}
int iPause = -1;
int iSmooth = -1;
int iAutoMoveFlag = -1;
while(!done)
{
while(CRM32Pro.Update(&event))
{
switch(event.type)
{
case SDL_QUIT:
done = 1;
break;
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE) done = 1;
if(event.key.keysym.sym == SDLK_c)
{
int iCX,iCY;
iCX = iCY = -1;
ILogSystem.Msg(LOG_NORMAL," [Screen2LayerCell(1) => (%d,%d)\n",iCX,iCY);
ILogSystem.Msg(LOG_NORMAL," [Screen2LayerAbsolute(1) => (%d,%d)\n",iCX,iCY);
}
if(event.key.keysym.sym == SDLK_p)
{
iPause = iPause * (-1);
}
if(event.key.keysym.sym == SDLK_s)
{
iSmooth = iSmooth * (-1);
}
if(event.key.keysym.sym == SDLK_a)
{
iAutoMoveFlag = iAutoMoveFlag * (-1);
}
break;
default:
break;
}
}
if(CRM32Pro.GetKeystate(SDLK_UP)) TE->
MoveLayer(1,0,-4);
if(CRM32Pro.GetKeystate(SDLK_DOWN)) TE->
MoveLayer(1,0,4);
}
delete TE;
ITimeSystem->Info();
CRM32Pro.VideoInfo();
return 0;
}
void RenderGraphics(int bLogicUpdate)
{
}
cMyCustomMap::cMyCustomMap(
int height,
int width) :
cMSTE_MapBase(height,width)
{
iW = width; iH = height;
if(iW > 0 && iH > 0)
{
iCustomMap = Create2DArray<unsigned int>(height,width);
for(int y = 0; y < iH; y++)
for(int x = 0; x < iW; x++) iCustomMap[y][x] = 5;
}
else iCustomMap = NULL;
strcpy(sMapType,"MyMap");
}
{
return new cMyCustomMap(y,x);
}
cMyCustomMap::~cMyCustomMap()
{
if(iCustomMap != NULL) Delete2DArray(iCustomMap);
iW = iH = 0;
iCustomMap = NULL;
}
int cMyCustomMap::CellRender(
int map_x,
int map_y,
int surf_x,
int surf_y,
cMSTE *TE,
int iOpacity)
{
if(iCustomMap[map_y][map_x] != 10)
return 1;
}
int cMyCustomMap::CheckMap()
{
int iRet = 1;
unsigned int iMax;
if(iCustomMap == NULL) return 0;
if(iW == 0) return 0;
if(iH == 0) return 0;
iMax = 20;
for(int i = 0; i < iH; i++)
for(int j = 0; j < iW; j++)
{
if(iCustomMap[i][j] < 5)
{
ILogSystem.Msg(LOG_NORMAL," · [MyCustomMap] Warning: map[%d][%d] has a wrong value of %d.\n",i,j,iCustomMap[i][j]);
iRet = 0;
}
else if(iCustomMap[i][j] > iMax)
{
ILogSystem.Msg(LOG_NORMAL," · [MyCustomMap] Warning: map[%d][%d] has a wrong value of %d.\n",i,j,iCustomMap[i][j]);
iRet = 0;
}
}
return iRet;
}
int cMyCustomMap::PrintInfo(char bTilesetReady, int iLogLevel)
{
ILogSystem.Msg(LOG_NORMAL," . Map type '%s' - Custom map size %dx%d\n",sMapType,iW,iH);
return 1;
}
int cMyCustomMap::SaveMapdataNode(int idXML, int iOutput)
{
char *szTmp = NULL;
int retvalue = 0;
retvalue = A2DtoCSV(iCustomMap,iW,iH,szTmp);
switch(iOutput)
{
case 1:
retvalue = CRM32Pro.XMLNodeChild(idXML);
retvalue = WriteTMXProperty(idXML,"custom-type",sMapType);
retvalue = WriteTMXProperty(idXML,"custom-width",iW);
retvalue = WriteTMXProperty(idXML,"custom-height",iH);
retvalue = WriteTMXProperty(idXML,"custom-data",szTmp);
CRM32Pro.XMLNodeParent(idXML);
break;
default:
case 0:
retvalue = CRM32Pro.XMLNodeCreate(idXML,"mapdata");
retvalue = CRM32Pro.XMLAttributeSet(idXML,"type",sMapType);
retvalue = CRM32Pro.XMLAttributeSet(idXML,"width",iW);
retvalue = CRM32Pro.XMLAttributeSet(idXML,"height",iH);
CRM32Pro.XMLTextSet(idXML,szTmp);
break;
}
if(szTmp != NULL) { delete []szTmp; szTmp = NULL; }
CRM32Pro.XMLNodeParent(idXML);
return retvalue;
}
int cMyCustomMap::LoadMapdataNode(int idXML, int iInput)
{
char *szTmp = NULL;
switch(iInput)
{
case 1:
CRM32Pro.XMLNodeStore(idXML);
CRM32Pro.XMLNodeParent(idXML);
CRM32Pro.XMLNodeChild(idXML);
do
{
if(!strcmp(CRM32Pro.XMLNodeGetName(idXML),"properties"))
{
if(!ReadTMXProperty(idXML,"custom-width",&iW)) return 0;
if(!ReadTMXProperty(idXML,"custom-height",&iH)) return 0;
if(!ReadTMXProperty(idXML,"custom-data",&szTmp)) return 0;
}
} while(CRM32Pro.XMLNodeNext(idXML));
CRM32Pro.XMLNodeRestore(idXML);
break;
default:
case 0:
if(!CRM32Pro.XMLAttributeGet(idXML,"width",&iW)) return 0;
if(!CRM32Pro.XMLAttributeGet(idXML,"height",&iH)) return 0;
if(iW <=0 || iH <= 0) return 0;
szTmp = CRM32Pro.XMLTextGet(idXML);
break;
}
if(szTmp == NULL) return 0;
if(iCustomMap != NULL) delete []iCustomMap;
iCustomMap = Create2DArray<unsigned int>(iH,iW);
if(CSVtoA2D(iCustomMap,iW,iH,szTmp)) return 1;
return 0;
}