MSTE - MegaStorm Tile-based Engine  v1.10
 All Classes Functions Groups Pages
Example03_Load.cpp
logotipo_mste.png
/*----------------------------------------------
MegaStorm Tile-based Engine - Roberto Prieto
Copyright (C) 2007-2012 MegaStorm Systems
----------------------------------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Roberto Prieto
contact@megastormsystems.com
http://www.megastormsystems.com
Note that all library licenses (including the one for this library)
are located at /licenses directory. Please, include them on your applications.
--------------------------------------------------------------------------
How to use MSTE. Example 3: load a MSM file
-------------------------------------------
- This example loads the MSM created by the previous example.
- Before exit, the level will be saved to an external MSM with current settings
and the layer 1 is exported to an external image file.
- Basic functions:
Cursor keys Up/Down: for moving through the level.
'p': pause/restore the movement
'c': get the the cell position on layer 1 of current mouse cursor
's': enable/disable the smooth scroll rendering
'a': enable/disable autoscrolling feature on X axis
*/
// ---Includes---
#include "CRM32Pro.h"
#include "../source/MSTE.h"
#include "../source/MSTE_MapData.h"
// ---Defines---
#define EXAMPLE_VERSION "MSTE - Example03: Load MSM"
// ---Global vars---
cMSTE *TE;
// ---Prototypes---
void RenderGraphics(int);
// -------------MAIN FUNCTION----------------
int main(int argc,char *argv[])
{
Uint8 done = 0;
SDL_Event event;
// -Log system initialize-
ILogSystem.Init("Example03_Load.log",LOG_FILE,LOG_NORMAL,EXAMPLE_VERSION);
// -CRM32Pro and SDL initialize-
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;
}
// -Desired config-
CRM32Pro.Config.Title = EXAMPLE_VERSION;
CRM32Pro.Config.VideoWidth = 640;
CRM32Pro.Config.VideoHeight = 400;
CRM32Pro.Config.bMTFriendly = 1;
// -Graphics system initialize-
if(!CRM32Pro.SetVideoMode())
{
CRM32Pro.Quit();
return 1;
}
// -Timer system-
ITimeSystem->Init();
ITimeSystem->SetRate(60,20);
CRM32Pro.SetRenderCallback(RenderGraphics);
// -Init scroll engine-
TE = new cMSTE;
TE->Init(2);
TE->SetRenderTarget(CRM32Pro.screen);
// -Load our previous saved level-
TE->LoadLevel("mylevel.msm");
// -Set on autospeed on layers 1 & 2 and set the speed on X axis for layer 1-
// -Main loop-
int iPause = -1;
int iSmooth = -1;
int iAutoMoveFlag = -1;
while(!done)
{
// Update all layers
TE->Update();
// Main system update and events loop
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;
TE->Screen2LayerCell(1,CRM32Pro.mouse_x,CRM32Pro.mouse_y,&iCX,&iCY);
ILogSystem.Msg(LOG_NORMAL," [Screen2LayerCell(1) => (%d,%d)\n",iCX,iCY);
TE->Screen2LayerAbsolute(1,CRM32Pro.mouse_x,CRM32Pro.mouse_y,&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);
TE->SetLayerFlags(0,TE_LAYERFLAG_AUTOSCROLL,iAutoMoveFlag);
TE->SetLayerFlags(1,TE_LAYERFLAG_AUTOSCROLL,iAutoMoveFlag);
}
break;
default:
break;
}
}
if(CRM32Pro.GetKeystate(SDLK_UP)) TE->MoveLayer(1,0,-2);
if(CRM32Pro.GetKeystate(SDLK_DOWN)) TE->MoveLayer(1,0,2);
}
// -Save current level status and export layer 1 to a PNG-
TE->SaveLevel("mylevel.msm");
TE->ExportImage(1,"layer1-image.png");
// -Free resources-
TE->InfoPrint();
TE->Quit();
delete TE;
// -End-
ITimeSystem->Info();
CRM32Pro.VideoInfo();
CRM32Pro.Quit();
return 0;
}
// Our render graphics function
// We render our graphics if a logic frame occurred(bLogicUpdate = 1) or if CRM32Pro.IsRenderNeeded() return 1
void RenderGraphics(int bLogicUpdate)
{
TE->Render();
}