MSTE - MegaStorm Tile-based Engine  v1.10
 All Classes Functions Groups Pages
Example02_Import.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 2: Import
-------------------------------------------------
- This example imports two images for creating a level with 2 layers.
- Both layer will have parallax scrolling and LoopX features.
- Before exit, the level will be saved to an external MSM with current settings.
- Basic functions:
Cursor keys: 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
*/
// ---Includes---
#include "CRM32Pro.h"
#include "../source/MSTE.h"
#include "../source/MSTE_MapData.h"
// ---Defines---
#define EXAMPLE_VERSION "MSTE - Example02: Import"
// ---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("Example02_Import.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,"MySecondLevel");
TE->SetRenderTarget(CRM32Pro.screen);
// -Importing two images with alpha per-pixel creating a new level-
TE->ImportImage(0, "import_bg.bmp", 64, 64);
TE->ImportImage(1, "import_fg.png", 32, 32);
// -Main loop-
int iPause = -1;
int iSmooth = -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);
}
default:
break;
}
}
if(CRM32Pro.GetKeystate(SDLK_UP)) TE->MoveLayer(1,0,-4);
if(CRM32Pro.GetKeystate(SDLK_DOWN)) TE->MoveLayer(1,0,4);
if(CRM32Pro.GetKeystate(SDLK_LEFT)) TE->MoveLayer(1,-4,0);
if(CRM32Pro.GetKeystate(SDLK_RIGHT)) TE->MoveLayer(1,4,0);
}
// -Save current level status-
TE->SaveLevel("mylevel.msm");
// -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();
}