level 13
先上源码地址吧,这次我是在电脑上,手机上都有。我把它分为两个压缩包。电脑上是用的codeblock,这里请注意,你还需要导入tiyxml和zlib库,手机上则不用了,直接运行就行
2015年06月20日 03点06分
2
回复
�ϻ�˫��
:。。。。?什么情况
2015年06月24日 00点06分
这两个库能在哪里找到?
2017年09月05日 10点09分
level 13
不知道大家是否知道Tiled Map,这个是地图编辑器。非常有名的一个地图编辑器,在cocos2dx或libgdx游戏引擎中都有用到,并且还封装成了类。算是极大的方便了,但是在SDL中却并没有这样的福利(没有这个也许是对我们的锤炼吧)。在好几次的调试,终于能够显示出
正确的
图形了,当然,现在实现的功能并没有多少,仅仅是实现了图片的SDL_Renderer而已。其他的功能我会接着研究。
使用的库: base64,zlib,tinyxml
Tiled我使用的是.tmx,(仅对这一种分析),我们先用记事本打开它试试吧。
<?xml version="1.0"encoding="UTF-8"?>
<map version="1.0"orientation="orthogonal" renderorder="right-down"width="20" height="15" tilewidth="32"tileheight="32" nextobjectid="1">
<tileset firstgid="1"name="blocks1" tilewidth="32" tileheight="32"spacing="2" margin="2">
<image source="blocks1.png" width="614"height="376"/>
</tileset>
<tileset firstgid="199"name="blocks2" tilewidth="32" tileheight="32"spacing="2" margin="2">
<image source="blocks2.png" width="614"height="376"/>
</tileset>
<layer name="layer1"width="20" height="15">
<data encoding="base64" compression="zlib">
eJxjYBgFo2Dog0dA/JiK5n0B4q9oYveB+AEQPyTTTHZGVP5HIP4ExJ+xqF0OxCuAeCUQrwLi1UC8BojXAvE6qBpJNPOYgXwWNDEQqAbiGiCuBeI6AhgEJEBmA7EUEEsDsQwQyyKZhQ8j25NLAp6Mxn+DQx0AfVkjjA==
</data>
</layer>
</map>
2015年06月20日 03点06分
3
level 13
看着是不是熟悉呢?不错,这个和xml格式一样(算是一样吧)。都有根节点,属性(Attribute),子节点等。下面咱们就逐步分析吧。(没说的暂时用不到,,当然,目前我也不知道什么用途)
<map version="1.0"orientation="orthogonal" renderorder="right-down"width="20" height="15" tilewidth="32"tileheight="32" nextobjectid="1">
version Tiled版本号,一般为1.0
orientation:地图朝向,目前支持“正交”和 “45度等距了两种方式”(这里仅讨论正交)
width:地图宽度
height:地图高度
tilewidth tileheight:单个图块大小
<tileset firstgid="1" name="blocks1"tilewidth="32" tileheight="32" spacing="2"margin="2"
firstgid:此图块集的第一个图块 在全局图块的位置
name:图块集的名称
tilewidth tileheight:图块大小
spacing:各土块之间的间距
margin:图块的边距。
<image source="blocks1.png"width="614" height="376"/>
source:资源的相对位置
width height:图块集的大小
2015年06月20日 03点06分
4
level 13
/*主要类*/
#ifndef __TMXTiledMap_H__
#define __TMXTiledMap_H__
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
#include "tinyxml.h"
#include "base64.h"
#include "zlib.h"
/*图块集*/
struct Tileset
{
int firstGirdID;
int tileWidth;
int tileHeight;
int spacing;
int margin;
int width;
int height;
int numColumns;
std::string name;
};
class TMXTiledMap
{
private:
//保存图块集
std::vector<Tileset> m_tilesets;
//保存数据> >中间最好有空格
std::vector<std::vector<int> > m_data;
//tmx文件的宽 高/个数,在render时用的到
int m_width;
int m_height;
//tileset size
int m_tileSize;
//save picture
std::map<std::string,SDL_Texture*> m_mapObjects;
//renderer
SDL_Renderer*m_pRenderer;
private:
//解析layer中的data
void parseTileLayer(TiXmlElement*pRoot);
//解析tileset
void parseTilesets(TiXmlElement*pTilesetRoot);
//根据id获得相应的图块集
Tileset getTilesetByID(int tileID);
//内部的draw,封装了SDL_RenderCopyEx
void drawTile(std::string id,int margin,int spacing,int x,int y,int width,int height,int currentRow,int currentFame);
public:
//解析,并绑定renderer
TMXTiledMap(std::string tmxPath,SDL_Renderer*ren);
void Draw();
};
#endif
2015年06月20日 03点06分
6
level 13
然后我在构造函数里对.tmx文件进行了解析。别惊讶,我是为了简便工作。然后就是各种赋值
值得一提的是下面的两个private函数
void parseTileLayer(TiXmlElement*pRoot);
void parseTilesets(TiXmlElement*pTilesetRoot);
这两个函数是在构造函数里被调用的,赋值嘛
嗯~,不分析了,下载下来的源代码中写的还算你比较清楚的。
这个tmx解析得还不完美。因为少了好多东西,不过我会接着完善然后发出来的。最近也是要考试的,,
2015年06月20日 04点06分
7