Tiled Map第一弹 基础使用 SDL2
c4droid吧
全部回复
仅看楼主
level 13
小牛_牛sky 楼主
2015年06月20日 03点06分 1
level 13
小牛_牛sky 楼主
先上源码地址吧,这次我是在电脑上,手机上都有。我把它分为两个压缩包。电脑上是用的codeblock,这里请注意,你还需要导入tiyxml和zlib库,手机上则不用了,直接运行就行
2015年06月20日 03点06分 2
2015年06月20日 03点06分
回复
[无效] Сţ_ţsky
:@你
2015年06月23日 18点06分
回复
�ϻ�˫��
:。。。。?什么情况
2015年06月24日 00点06分
这两个库能在哪里找到?[哈哈]
2017年09月05日 10点09分
level 13
小牛_牛sky 楼主
不知道大家是否知道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
小牛_牛sky 楼主
看着是不是熟悉呢?不错,这个和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
小牛_牛sky 楼主
/*主要类*/
#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
注释上说的还算比较清楚吧,不太懂的可以@我
2015年06月20日 03点06分
level 13
小牛_牛sky 楼主
然后我在构造函数里对.tmx文件进行了解析。别惊讶,我是为了简便工作。然后就是各种赋值
值得一提的是下面的两个private函数
void parseTileLayer(TiXmlElement*pRoot);
void parseTilesets(TiXmlElement*pTilesetRoot);
这两个函数是在构造函数里被调用的,赋值嘛
嗯~,不分析了,下载下来的源代码中写的还算你比较清楚的。
这个tmx解析得还不完美。因为少了好多东西,不过我会接着完善然后发出来的。最近也是要考试的,,
2015年06月20日 04点06分 7
level 12

2015年06月20日 04点06分 8
嗯嗯[哈哈]
2015年06月20日 04点06分
level 13
小牛_牛sky 楼主
2015年06月20日 04点06分 9
level 13
小牛_牛sky 楼主
2015年06月20日 04点06分 10
level 13
小牛_牛sky 楼主
2015年06月20日 04点06分 11
level 13
先留名,
2015年06月20日 04点06分 12
。。。。插这么快←_←
2015年06月20日 04点06分
level 13
小牛_牛sky 楼主
要不顺便申个精@lf澜风 @傻傻_痴痴
2015年06月20日 04点06分 13
level 13
小牛_牛sky 楼主
顺便来个手机上的图
2015年06月20日 04点06分 14
[啊]支持横屏不,
2015年06月20日 04点06分
回复 啊o额iu鱼 :目前来说,不支持,暂时不在考虑范围[酷]
2015年06月20日 04点06分
回复 啊o额iu鱼 :我知道怎么支持横屏了!
2015年06月20日 04点06分
level 13
膜拜大神[钱][乖]
2015年06月20日 04点06分 16
不是大神⊙▽⊙[太开心][太开心]
2015年06月20日 04点06分
level 12
运行哪个文件啊[惊哭]
2015年06月20日 04点06分 17
makefile,先长按,然后用makefile编译sdl2,就好了
2015年06月20日 04点06分
回复
Сţ_ţsky
:ok可以了,[狂汗]原理二进制文件名是main啊
2015年06月20日 04点06分
回复
������
:是啊,你没这样用过?
2015年06月20日 07点06分
level 13
顶起
2015年06月20日 04点06分 18
level 13
顶上去
2015年06月20日 05点06分 19
[哈哈][哈哈][哈哈]
2015年06月20日 07点06分
level 12
前排

====祝top gear早日回归====
2015年06月20日 06点06分 20
早就不是前排了[阴险]
2015年06月20日 07点06分
level 12
666,好炸
2015年06月20日 08点06分 21
level 13
xml?
2015年06月20日 15点06分 22
1 2 尾页