MSTE - MegaStorm Tile-based Engine  v1.10
 All Classes Functions Groups Pages
Example01_Init.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 1: Initialization
-------------------------------------------------
- This example shows how to initialize and the basic usage of the engine
- We manually create a basic level with three linked layers using parallax scrolling.
- Basic functions:
Cursor keys: for moving through the level.
'1' to '6' keys: to go to the begin, middle or end of X or Y axis.
'p': pause/restore the movement
'c': get the the cell position on layer 2 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 - Example01: Init"
// ---Global vars---
cMSTE *TE;
cMSTE_MapBase *Map0,*Map1,*Map2;
CRM32Pro_CTile *tilset;
// ---Prototypes---
void ManualLoad();
void RenderGraphics(int);
// -------------MAIN FUNCTION----------------
int main(int argc,char *argv[])
{
Uint8 done = 0;
SDL_Event event;
// -Log system initialize-
ILogSystem.Init("Example01_Init.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 = 320;
CRM32Pro.Config.VideoHeight = 240;
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(3,"MyFirstLevel");
TE->SetRenderTarget(CRM32Pro.screen);
// -Load our test map-
ManualLoad();
// -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_1)
if(event.key.keysym.sym == SDLK_2)
if(event.key.keysym.sym == SDLK_3)
if(event.key.keysym.sym == SDLK_4)
if(event.key.keysym.sym == SDLK_5)
if(event.key.keysym.sym == SDLK_6)
if(event.key.keysym.sym == SDLK_c)
{
int iCX,iCY;
iCX = iCY = -1;
TE->Screen2LayerCell(2,CRM32Pro.mouse_x,CRM32Pro.mouse_y,&iCX,&iCY);
ILogSystem.Msg(LOG_NORMAL," [Screen2LayerCell(2) => (%d,%d)\n",iCX,iCY);
TE->Screen2LayerAbsolute(2,CRM32Pro.mouse_x,CRM32Pro.mouse_y,&iCX,&iCY);
ILogSystem.Msg(LOG_NORMAL," [Screen2LayerAbsolute(2) => (%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(2,0,-4);
if(CRM32Pro.GetKeystate(SDLK_DOWN)) TE->MoveLayer(2,0,4);
if(CRM32Pro.GetKeystate(SDLK_LEFT)) TE->MoveLayer(2,-4,0);
if(CRM32Pro.GetKeystate(SDLK_RIGHT)) TE->MoveLayer(2,4,0);
}
// -Free resources-
TE->InfoPrint();
TE->Quit();
delete TE;
if(Map0 != NULL) delete Map0;
if(Map1 != NULL) delete Map1;
if(Map2 != NULL) delete Map2;
if(tilset != NULL) delete tilset;
// -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();
}
// Create a manual map with three layers
void ManualLoad()
{
// Loading the tileset to be used on all layers
tilset = new CRM32Pro_CTile;
if(tilset->Create("tiles.bmp","test"))
{
tilset->SetTileSet(1,48,48);
tilset->SetColorKey(0xFF00FF);
}
else { delete tilset; tilset = NULL; }
TE->LoadTileset(tilset);
// Layer '0' initialization
Map0 = new cMSTE_MapBase(16,16);
Map0->SetMapBaseValue(-1,-1,1); // Fill all map with value 1
Map0->SetCellWidth(48);
Map0->SetCellHeight(48);
if(Map0->CheckMap() == 0) ILogSystem.Msg(LOG_NORMAL," - ManualLoad() has found problems checking the map on layer 0\n");
// Assign the mapdata to layer '0'
TE->SetLayerMapData(0,Map0);
// Layer '1' initialization
Map1 = new cMSTE_MapBase(16,16);
Map1->SetCellWidth(48);
Map1->SetCellHeight(48);
Map1->iTilemap[0][3] = 2;
Map1->iTilemap[0][8] = 2;
Map1->iTilemap[1][11] = 2;
Map1->iTilemap[1][15] = 2;
Map1->iTilemap[2][2] = 2;
Map1->iTilemap[3][5] = 2;
Map1->iTilemap[3][8] = 2;
Map1->iTilemap[3][13] = 2;
Map1->iTilemap[4][3] = 2;
Map1->iTilemap[5][9] = 2;
Map1->iTilemap[6][1] = 2;
Map1->iTilemap[6][14] = 2;
Map1->iTilemap[7][4] = 2;
Map1->iTilemap[7][8] = 2;
Map1->iTilemap[8][10] = 2;
Map1->iTilemap[9][3] = 2;
Map1->iTilemap[10][8] = 2;
Map1->iTilemap[10][13] = 2;
Map1->iTilemap[11][1] = 2;
Map1->iTilemap[11][12] = 2;
Map1->iTilemap[12][5] = 2;
Map1->iTilemap[13][8] = 2;
Map1->iTilemap[14][2] = 2;
Map1->iTilemap[14][11] = 2;
Map1->CheckMap();
// Assign the mapdata to layer '1'
TE->SetLayerMapData(1,Map1);
// Layer '2' initialization
Map2 = new cMSTE_MapBase(16,16);
Map2->SetCellWidth(48);
Map2->SetCellHeight(48);
Map2->SetMapBaseValue(-1,0,4);
Map2->SetMapBaseValue(-1,15,4);
Map2->SetMapBaseValue(0,-1,4);
Map2->SetMapBaseValue(15,-1,4);
Map2->iTilemap[1][4] = 3;
Map2->iTilemap[1][8] = 4;
Map2->iTilemap[2][4] = 3;
Map2->iTilemap[2][5] = 3;
Map2->iTilemap[2][6] = 3;
Map2->iTilemap[2][8] = 4;
Map2->iTilemap[2][11] = 3;
Map2->iTilemap[2][12] = 3;
Map2->iTilemap[2][13] = 3;
Map2->iTilemap[3][1] = 4;
Map2->iTilemap[3][2] = 4;
Map2->iTilemap[3][3] = 4;
Map2->iTilemap[3][5] = 3;
Map2->iTilemap[3][6] = 3;
Map2->iTilemap[3][12] = 3;
Map2->iTilemap[3][13] = 3;
Map2->iTilemap[4][8] = 3;
Map2->iTilemap[4][9] = 3;
Map2->iTilemap[4][10] = 3;
Map2->iTilemap[5][4] = 3;
Map2->iTilemap[5][5] = 3;
Map2->iTilemap[5][6] = 3;
Map2->iTilemap[5][8] = 3;
Map2->iTilemap[5][10] = 3;
Map2->iTilemap[5][13] = 4;
Map2->iTilemap[5][14] = 4;
Map2->iTilemap[6][4] = 3;
Map2->iTilemap[6][6] = 3;
Map2->iTilemap[6][8] = 3;
Map2->iTilemap[6][9] = 3;
Map2->iTilemap[6][10] = 3;
Map2->iTilemap[7][4] = 3;
Map2->iTilemap[7][5] = 3;
Map2->iTilemap[7][6] = 3;
Map2->iTilemap[7][13] = 3;
Map2->iTilemap[7][14] = 3;
Map2->iTilemap[8][9] = 4;
Map2->iTilemap[8][10] = 4;
Map2->iTilemap[8][11] = 4;
Map2->iTilemap[9][3] = 3;
Map2->iTilemap[9][4] = 3;
Map2->iTilemap[9][6] = 3;
Map2->iTilemap[9][7] = 4;
Map2->iTilemap[9][9] = 4;
Map2->iTilemap[9][10] = 3;
Map2->iTilemap[9][11] = 4;
Map2->iTilemap[9][14] = 3;
Map2->iTilemap[10][3] = 3;
Map2->iTilemap[10][4] = 3;
Map2->iTilemap[10][6] = 4;
Map2->iTilemap[10][7] = 3;
Map2->iTilemap[10][9] = 4;
Map2->iTilemap[10][10] = 4;
Map2->iTilemap[10][11] = 4;
Map2->iTilemap[10][14] = 3;
Map2->iTilemap[11][13] = 3;
Map2->iTilemap[11][14] = 3;
Map2->iTilemap[12][2] = 4;
Map2->iTilemap[12][5] = 3;
Map2->iTilemap[12][6] = 3;
Map2->iTilemap[12][8] = 4;
Map2->iTilemap[12][9] = 4;
Map2->iTilemap[13][5] = 3;
Map2->iTilemap[13][6] = 3;
Map2->iTilemap[13][7] = 3;
Map2->iTilemap[13][10] = 3;
Map2->iTilemap[13][13] = 4;
Map2->iTilemap[14][3] = 4;
Map2->iTilemap[14][9] = 4;
Map2->iTilemap[14][13] = 4;
Map2->CheckMap();
// Assign the mapdata to layer '2'
TE->SetLayerMapData(2,Map2);
// Enable our layers
TE->SetLayerName(0,"background");
TE->SetLayerParallaxRatio(0,0.25);
TE->SetLayerName(1,"moons");
TE->SetLayerName(2,"playfield");
}