Add TMXTexture. Update a sample game.

This commit is contained in:
Kirigaya Kazuto 2017-06-24 15:01:02 +08:00
parent 0cedd5e157
commit cc30fd015b
5 changed files with 102 additions and 1 deletions

View File

@ -1,11 +1,40 @@
#include "MiniEngine/MiniEngine.h"
#include "MiniEngine/MiniEngine_Event.h"
using namespace MiniEngine;
#include "tmxparser.h"
#include "tmxtexture.h"
#include <utility>
using namespace std;
int AppMain()
{
Window wnd("Tower Defense",1024,768);
Renderer rnd(wnd);
Map mp("map.tmx");
MapTexture mpt;
mpt.mp=mp;
mpt.reload(rnd);
Looper lp;
lp.updater=[&](){
rnd.clear();
for(int i=0;i<mpt.mp.data.size();i++)
{
for(int line=0;line<mpt.mp.data.at(i).height;line++)
{
for(int col=0;col<mpt.mp.data.at(i).width;col++)
{
rnd.copy(mpt.getDrawInfoByID(mpt.mp.data.at(i).data.at(line).at(col)).first,
mpt.getDrawInfoByID(mpt.mp.data.at(i).data.at(line).at(col)).second,
Rect(col*mpt.mp.tilewidth,line*mpt.mp.tileheight,mpt.mp.tilewidth,mpt.mp.tileheight));
}
}
}
rnd.update();
};
lp.add(SDL_QUIT,[&](){lp.stop();});
lp.run();
return 0;
}

View File

@ -11,6 +11,8 @@ Map::Map(const std::string& TMXFilename)
file<> fin(TMXFilename.c_str());
doc.parse<0>(fin.data());
xml_node<>* mapbasic=doc.first_node("map");
tilewidth=atoi(mapbasic->first_attribute("tilewidth")->value());
tileheight=atoi(mapbasic->first_attribute("tileheight")->value());
xml_node<>* tset=mapbasic->first_node("tileset");
if(tset==nullptr)
{

View File

@ -25,5 +25,7 @@ class Map
public:
std::vector<Layer> data;
std::vector<Tileset> tiledata;
int tilewidth,tileheight;
Map(const std::string& TMXFilename);
Map()=default;
};

45
tmxtexture.cpp Normal file
View File

@ -0,0 +1,45 @@
#include "tmxtexture.h"
using namespace MiniEngine;
Rect TilesetTexture::getRectByID(int GID)
{
int Col=tset.columns;
int ThisID=GID-tset.firstgid;
int Height=tset.tileheight;
int Width=tset.tilewidth;
return Rect( (ThisID%Col)*Width,
(ThisID/Col)*Height,
Width,Height);
}
void TilesetTexture::reload(const Renderer& rnd)
{
text=rnd.loadTexture(tset.source);
}
void MapTexture::reload(const Renderer& rnd)
{
textvec.clear();
for(auto& tset:mp.tiledata)
{
TilesetTexture tt;
tt.tset=tset;
tt.reload(rnd);
textvec.push_back(tt);
}
}
std::pair<Texture,Rect> MapTexture::getDrawInfoByID(int GID)
{
int sz=textvec.size();
for(int i=0;i<sz;i++)
{
if(GID<textvec.at(i).tset.firstgid||GID>textvec.at(i).tset.firstgid+textvec.at(i).tset.tilecount)
{
/// Not in this range.
continue;
}
else
{
return std::make_pair(textvec.at(i).text,textvec.at(i).getRectByID(GID));
}
}
return std::make_pair(Texture(),Rect());
}

23
tmxtexture.h Normal file
View File

@ -0,0 +1,23 @@
#pragma once
#include "tmxparser.h"
#include "MiniEngine/MiniEngine.h"
#include <vector>
class TilesetTexture
{
public:
Tileset tset;
MiniEngine::Texture text;
MiniEngine::Rect getRectByID(int GID);
void reload(const MiniEngine::Renderer& rnd);
};
class MapTexture
{
public:
Map mp;
std::vector<TilesetTexture> textvec;
void reload(const MiniEngine::Renderer& rnd);
std::pair<MiniEngine::Texture,MiniEngine::Rect> getDrawInfoByID(int GID);
};