【Cocos2d-x】 一个简单的将plist分割为原图的命令行工具

原创作品,转载请标明http://blog.csdn.net/xiejingfa/article/details/48023345


缘起

在学习Cocos2d-x时,经常需要到网上下载一些游戏实例。很多源码中的图片资源都是经过Texture Packer打包过的plist文件而没有提供单个的原图片文件。于是我就想着写一个脚本把打包好的plist文件和对应的png图片分割为原来的小图片,网上也有很多用Python实现的脚本。在这个我利用argparse和PIL制作了一个简易的命令行工具。


原理

由于plist文件本质上是基于xm的文本格式,里面记录了原始图片文件的名称、尺寸大小和位置信息。利用ElementTree从xml解析出上述信息,然后利用PIL把原始文件切割出来就可以得到单个的原始文件。argparse主要用来处理命令行信息。

源码如下,比较简单,这里就不一一解释:


# -*- coding:utf-8 -*-
"""
    This utility is used to parse plist file which is packed by Texture Packer to original images.
    usage:
        -plist specify the path of plist file(required parameter).
        -png specify the path of png file(required parameter).
        -dir specify a output directory(optional). By default, it will make a new directory named
             with plist filename in current directory to save images.
"""

from xml.etree import ElementTree
from PIL import Image
import argparse
import os
import sys


class PlistParser(object):

    # initializer
    def __init__(self, plist, png_image, output_dir):
        self.plist_file = plist
        self.png_file = png_image
        self.atlas_dir = output_dir

    # convert a xml tree to dict.
    def convert_tree_to_dict(self, tree):
        d = {}
        for index, item in enumerate(tree):
            if item.tag == 'key':
                if tree[index + 1].tag == 'string':
                    d[item.text] = tree[index + 1].text
                elif tree[index + 1].tag == 'true':
                    d[item.text] = True
                elif tree[index + 1].tag == 'false':
                    d[item.text] = False
                elif tree[index + 1].tag == 'dict':
                    d[item.text] = self.convert_tree_to_dict(tree[index + 1])
        return d

    # split png file into individual images.
    def split_png_from_plist(self):
        # generate output directory.
        target_file_dir = self.atlas_dir;
        if target_file_dir is None:
            target_file_dir = plist_filename.replace('.plist', '')
            if not os.path.isdir(target_file_dir):
                os.mkdir(target_file_dir)

        # open the source image.
        src_image = Image.open(png_filename)
        plist_content = open(plist_filename, 'r').read()
        plist_root = ElementTree.fromstring(plist_content)
        plist_dict = self.convert_tree_to_dict(plist_root[0])

        to_list = lambda x : x.replace('{', '').replace('}', '').split(',')
        for k, v in plist_dict['frames'].items():
            pos_str = str(v['frame'])
            rect_list = to_list(pos_str)
            width = int( rect_list[3] if v['rotated'] else rect_list[2] )
            height = int(rect_list[2] if v['rotated'] else rect_list[3] )
            bounding_box = (
                int(rect_list[0]),
                int(rect_list[1]),
                int(rect_list[0]) + width,
                int(rect_list[1]) + height,
            )
            size_list = [ int(x) for x in to_list(v['sourceSize']) ]

            rect_image = src_image.crop(bounding_box)
            if v['rotated']:
                rect_image = rect_image.rotate(90)

            outfile = os.path.join(target_file_dir, k)
            rect_image.save(outfile)


if __name__ == '__main__':
    # register all available parameters.
    parser = argparse.ArgumentParser(usage='please use unpacker.py -h to get usage information.')
    parser.add_argument('-plist', help='Specify the path of plist file.', type=str)
    parser.add_argument('-png', help='Specify the path of png file.', type=str)
    parser.add_argument('-dir', help='Specify a output directory.', type=str)

    # get parameters.
    args = parser.parse_args()
    plist_filename = args.plist
    png_filename = args.png
    output_dir = args.dir

    # test whether the file/dir is None
    if plist_filename is None:
        print 'make sure to use -plist to specify the plist file path.'
        sys.exit(1)
    if png_filename is None:
        print 'make sure to use -png to specify the source png image.'
        sys.exit(1)

    # test whether the file/dir exits
    if not os.path.exists(plist_filename):
        print 'error: plist file doesn\'t exist.'
        sys.exit(1)
    if not os.path.exists(png_filename):
        print 'error: png file doesn\'t exist.'
        sys.exit(1)
    if output_dir is not None and not os.path.isdir(output_dir):
        print 'error: %s is no an valid directory or doesn\'t exist.' % output_dir
        sys.exit(1)

    plist_parser = PlistParser(plist_filename, png_filename, output_dir)
    plist_parser.split_png_from_plist()
    print 'success.'


使用方法

这个小工具提供了三个参数选项:-plist、-png、dir,他们的作用是:

-plist:用于指定plist的文件所在路径

-png:用于指定plist对于的png图片所在路径

  -dir:用于指出分割后得到的原始图片的输出目录。如果没有指定,默认情况下会在plist文件所在目录下新建一个与plist文件同名的文件夹用于存放图片


比如:我的plist文件存放在C:\Users\Administrator\Desktop\test\atlas.plist,对应的png图片存放在C:\Users\Administrator\Desktop\test\atlas.png。使用命令

                   


解析成功后就会在C:\Users\Administrator\Desktop\test\atlas得到相应的原始图片。


代码已经上传到GitHub:https://github.com/xiejingfa/unpacker


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Cocos2d-x一个用于游戏开发的开源框架,它提供了用于制作2D游戏的工具和功能。若要利用Cocos2d-x读取传奇wzl文件,需要对wzl文件的格式进行分析,并使用Cocos2d-x提供的读取文件的功能,如果文件为文本格式,可以使用C++的fstream库来读取文件,如果文件为二进制格式,则可以使用C++的fread函数读取文件。 在读取文件的数据后,需要进一步的解析数据并使用Cocos2d-x的绘图功能绘制出游戏界面。 总的来说,要利用Cocos2d-x一个读取传奇wzl文件的程序,需要对文件格式进行分析,掌握C++的文件读取技巧,以及熟悉Cocos2d-x的游戏开发流程。 ### 回答2: Cocos2d-x一个开源的跨平台游戏引擎,它支持多种编程语言,包括C++和Lua。要利用Cocos2d-x一个程序读取传奇wzl文件,我们可以按照以下步骤进行: 1. 首先,需要了解传奇wzl文件的格式和结构。wzl文件是传奇游戏的资源文件,包含了游戏中的图像、音乐等资源数据。我们需要分析wzl文件的结构,了解其中的数据类型和存储方式。 2. 使用Cocos2d-x提供的文件读取功能,打开并读取wzl文件。可以使用C++的文件操作库或Cocos2d-x中的FileUtils类来实现。 3. 解析wzl文件的数据结构。根据对wzl文件的分析,我们可以确定每个资源的存储位置和数据结构。可以使用C++的文件读取功能从wzl文件中读取相应的数据段。 4. 对读取的数据进行处理。根据传奇游戏的资源类型,可以使用Cocos2d-x的相关功能将数据转换成可用的资源,例如将图像数据转换成Cocos2d-x中的Sprite对象。 5. 显示资源数据。将转换后的游戏资源在屏幕上进行展示。可以使用Cocos2d-x提供的精灵(Sprite)、图层(Layer)等功能将资源显示在游戏界面上。 需要注意的是,读取传奇wzl文件需要对文件解析和数据处理有一定的了解和经验。而Cocos2d-x一个强大的游戏引擎,提供了丰富的功能和工具来实现游戏的开发。借助Cocos2d-x文件读取和数据处理功能,我们可以更方便地读取传奇wzl文件并使用其中的资源数据。 ### 回答3: Cocos2d-x一个开源的跨平台游戏开发框架,它支持C++和Lua等编程语言,可用于开发各种类型的游戏。如果要利用Cocos2d-x编写一个程序来读取传奇wzl文件,需要进行以下步骤。 首先,我们需要了解传奇wzl文件的格式和结构。通过分析wzl文件的数据结构,可以确定存储在文件中的信息,例如角色、物品、地图等。然后,我们可以根据这些信息设计程序的数据结构来存储和管理相关数据。 接下来,在Cocos2d-x中创建一个新的项目,然后在项目中添加读取wzl文件的必要代码。可以使用C++的文件读取功能来打开wzl文件,并读取其中的数据。根据wzl文件的格式,解析和提取所需的数据,并将其存储到程序中的数据结构中。 在读取数据之后,我们可以利用Cocos2d-x的图形渲染和动画功能来展示传奇游戏中的角色、物品和地图等信息。使用适当的精灵和纹理资源,将提取的数据呈现在游戏界面上。 此外,为了增强程序的交互性,还可以添加适当的用户输入和操作功能。例如,可以实现点击角色进行移动或攻击的功能,或者通过菜单选项来进行游戏设置和控制等。 最后,进行必要的测试和调试,确保程序能够正确读取和展示传奇wzl文件中的内容。如果发现问题,可以根据需要进行修改和优化,直到程序达到预期的效果。 综上所述,借助Cocos2d-x框架,我们可以编写一个程序来读取传奇wzl文件,并展示其中的角色、物品和地图等内容。通过处理文件和数据,利用Cocos2d-x框架的图形渲染和动画功能,我们可以创造一个类似传奇游戏的体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值