Skip to content
This repository
Browse code

Replica Island plugin: Read and write *.bin files

Replica Island is an open source, side-scrolling video game for Android.
This plugin can read and write the *.bin files used with Replica Island.

This code was originally based on the existing Droidcraft plugin (hence
the copyright notices), but it has since been almost entirely rewritten
to use QDataStream, which provides us with portable (de)serialization of
little-endian floats.

Most of the tilesets are taken directly from Replica Island, which is
distributed under the Apache License.  A few tilesets, including the
hotspot tiles, were created by hand.
  • Loading branch information...
commit 2422222985191730843d517b19eceba5002edbae 1 parent f02c3a8
Eric Kidd emk authored committed
1  AUTHORS
@@ -52,3 +52,4 @@ perspective_walls.png - (C) Clint Bellanger, released as Public Domain
52 52 isometric_grass_and_water.png - (C) Clint Bellanger, released as GPL2, GPL3, CC-BY-SA3
53 53 tmw_desert_spacing.png - (C) The Mana World Development Team, GPL
54 54 sewer_tileset.png - (C) Blues Brothers RPG developers, GPL
  55 +Replica Island - (C) Chris Pruett and Genki Mine, Apache (except hotspots.png)
201 LICENSE.APACHE
... ... @@ -0,0 +1,201 @@
  1 + Apache License
  2 + Version 2.0, January 2004
  3 + http://www.apache.org/licenses/
  4 +
  5 + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
  6 +
  7 + 1. Definitions.
  8 +
  9 + "License" shall mean the terms and conditions for use, reproduction,
  10 + and distribution as defined by Sections 1 through 9 of this document.
  11 +
  12 + "Licensor" shall mean the copyright owner or entity authorized by
  13 + the copyright owner that is granting the License.
  14 +
  15 + "Legal Entity" shall mean the union of the acting entity and all
  16 + other entities that control, are controlled by, or are under common
  17 + control with that entity. For the purposes of this definition,
  18 + "control" means (i) the power, direct or indirect, to cause the
  19 + direction or management of such entity, whether by contract or
  20 + otherwise, or (ii) ownership of fifty percent (50%) or more of the
  21 + outstanding shares, or (iii) beneficial ownership of such entity.
  22 +
  23 + "You" (or "Your") shall mean an individual or Legal Entity
  24 + exercising permissions granted by this License.
  25 +
  26 + "Source" form shall mean the preferred form for making modifications,
  27 + including but not limited to software source code, documentation
  28 + source, and configuration files.
  29 +
  30 + "Object" form shall mean any form resulting from mechanical
  31 + transformation or translation of a Source form, including but
  32 + not limited to compiled object code, generated documentation,
  33 + and conversions to other media types.
  34 +
  35 + "Work" shall mean the work of authorship, whether in Source or
  36 + Object form, made available under the License, as indicated by a
  37 + copyright notice that is included in or attached to the work
  38 + (an example is provided in the Appendix below).
  39 +
  40 + "Derivative Works" shall mean any work, whether in Source or Object
  41 + form, that is based on (or derived from) the Work and for which the
  42 + editorial revisions, annotations, elaborations, or other modifications
  43 + represent, as a whole, an original work of authorship. For the purposes
  44 + of this License, Derivative Works shall not include works that remain
  45 + separable from, or merely link (or bind by name) to the interfaces of,
  46 + the Work and Derivative Works thereof.
  47 +
  48 + "Contribution" shall mean any work of authorship, including
  49 + the original version of the Work and any modifications or additions
  50 + to that Work or Derivative Works thereof, that is intentionally
  51 + submitted to Licensor for inclusion in the Work by the copyright owner
  52 + or by an individual or Legal Entity authorized to submit on behalf of
  53 + the copyright owner. For the purposes of this definition, "submitted"
  54 + means any form of electronic, verbal, or written communication sent
  55 + to the Licensor or its representatives, including but not limited to
  56 + communication on electronic mailing lists, source code control systems,
  57 + and issue tracking systems that are managed by, or on behalf of, the
  58 + Licensor for the purpose of discussing and improving the Work, but
  59 + excluding communication that is conspicuously marked or otherwise
  60 + designated in writing by the copyright owner as "Not a Contribution."
  61 +
  62 + "Contributor" shall mean Licensor and any individual or Legal Entity
  63 + on behalf of whom a Contribution has been received by Licensor and
  64 + subsequently incorporated within the Work.
  65 +
  66 + 2. Grant of Copyright License. Subject to the terms and conditions of
  67 + this License, each Contributor hereby grants to You a perpetual,
  68 + worldwide, non-exclusive, no-charge, royalty-free, irrevocable
  69 + copyright license to reproduce, prepare Derivative Works of,
  70 + publicly display, publicly perform, sublicense, and distribute the
  71 + Work and such Derivative Works in Source or Object form.
  72 +
  73 + 3. Grant of Patent License. Subject to the terms and conditions of
  74 + this License, each Contributor hereby grants to You a perpetual,
  75 + worldwide, non-exclusive, no-charge, royalty-free, irrevocable
  76 + (except as stated in this section) patent license to make, have made,
  77 + use, offer to sell, sell, import, and otherwise transfer the Work,
  78 + where such license applies only to those patent claims licensable
  79 + by such Contributor that are necessarily infringed by their
  80 + Contribution(s) alone or by combination of their Contribution(s)
  81 + with the Work to which such Contribution(s) was submitted. If You
  82 + institute patent litigation against any entity (including a
  83 + cross-claim or counterclaim in a lawsuit) alleging that the Work
  84 + or a Contribution incorporated within the Work constitutes direct
  85 + or contributory patent infringement, then any patent licenses
  86 + granted to You under this License for that Work shall terminate
  87 + as of the date such litigation is filed.
  88 +
  89 + 4. Redistribution. You may reproduce and distribute copies of the
  90 + Work or Derivative Works thereof in any medium, with or without
  91 + modifications, and in Source or Object form, provided that You
  92 + meet the following conditions:
  93 +
  94 + (a) You must give any other recipients of the Work or
  95 + Derivative Works a copy of this License; and
  96 +
  97 + (b) You must cause any modified files to carry prominent notices
  98 + stating that You changed the files; and
  99 +
  100 + (c) You must retain, in the Source form of any Derivative Works
  101 + that You distribute, all copyright, patent, trademark, and
  102 + attribution notices from the Source form of the Work,
  103 + excluding those notices that do not pertain to any part of
  104 + the Derivative Works; and
  105 +
  106 + (d) If the Work includes a "NOTICE" text file as part of its
  107 + distribution, then any Derivative Works that You distribute must
  108 + include a readable copy of the attribution notices contained
  109 + within such NOTICE file, excluding those notices that do not
  110 + pertain to any part of the Derivative Works, in at least one
  111 + of the following places: within a NOTICE text file distributed
  112 + as part of the Derivative Works; within the Source form or
  113 + documentation, if provided along with the Derivative Works; or,
  114 + within a display generated by the Derivative Works, if and
  115 + wherever such third-party notices normally appear. The contents
  116 + of the NOTICE file are for informational purposes only and
  117 + do not modify the License. You may add Your own attribution
  118 + notices within Derivative Works that You distribute, alongside
  119 + or as an addendum to the NOTICE text from the Work, provided
  120 + that such additional attribution notices cannot be construed
  121 + as modifying the License.
  122 +
  123 + You may add Your own copyright statement to Your modifications and
  124 + may provide additional or different license terms and conditions
  125 + for use, reproduction, or distribution of Your modifications, or
  126 + for any such Derivative Works as a whole, provided Your use,
  127 + reproduction, and distribution of the Work otherwise complies with
  128 + the conditions stated in this License.
  129 +
  130 + 5. Submission of Contributions. Unless You explicitly state otherwise,
  131 + any Contribution intentionally submitted for inclusion in the Work
  132 + by You to the Licensor shall be under the terms and conditions of
  133 + this License, without any additional terms or conditions.
  134 + Notwithstanding the above, nothing herein shall supersede or modify
  135 + the terms of any separate license agreement you may have executed
  136 + with Licensor regarding such Contributions.
  137 +
  138 + 6. Trademarks. This License does not grant permission to use the trade
  139 + names, trademarks, service marks, or product names of the Licensor,
  140 + except as required for reasonable and customary use in describing the
  141 + origin of the Work and reproducing the content of the NOTICE file.
  142 +
  143 + 7. Disclaimer of Warranty. Unless required by applicable law or
  144 + agreed to in writing, Licensor provides the Work (and each
  145 + Contributor provides its Contributions) on an "AS IS" BASIS,
  146 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  147 + implied, including, without limitation, any warranties or conditions
  148 + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
  149 + PARTICULAR PURPOSE. You are solely responsible for determining the
  150 + appropriateness of using or redistributing the Work and assume any
  151 + risks associated with Your exercise of permissions under this License.
  152 +
  153 + 8. Limitation of Liability. In no event and under no legal theory,
  154 + whether in tort (including negligence), contract, or otherwise,
  155 + unless required by applicable law (such as deliberate and grossly
  156 + negligent acts) or agreed to in writing, shall any Contributor be
  157 + liable to You for damages, including any direct, indirect, special,
  158 + incidental, or consequential damages of any character arising as a
  159 + result of this License or out of the use or inability to use the
  160 + Work (including but not limited to damages for loss of goodwill,
  161 + work stoppage, computer failure or malfunction, or any and all
  162 + other commercial damages or losses), even if such Contributor
  163 + has been advised of the possibility of such damages.
  164 +
  165 + 9. Accepting Warranty or Additional Liability. While redistributing
  166 + the Work or Derivative Works thereof, You may choose to offer,
  167 + and charge a fee for, acceptance of support, warranty, indemnity,
  168 + or other liability obligations and/or rights consistent with this
  169 + License. However, in accepting such obligations, You may act only
  170 + on Your own behalf and on Your sole responsibility, not on behalf
  171 + of any other Contributor, and only if You agree to indemnify,
  172 + defend, and hold each Contributor harmless for any liability
  173 + incurred by, or claims asserted against, such Contributor by reason
  174 + of your accepting any such warranty or additional liability.
  175 +
  176 + END OF TERMS AND CONDITIONS
  177 +
  178 + APPENDIX: How to apply the Apache License to your work.
  179 +
  180 + To apply the Apache License to your work, attach the following
  181 + boilerplate notice, with the fields enclosed by brackets "[]"
  182 + replaced with your own identifying information. (Don't include
  183 + the brackets!) The text should be enclosed in the appropriate
  184 + comment syntax for the file format. We also recommend that a
  185 + file or class name and description of purpose be included on the
  186 + same "printed page" as the copyright notice for easier
  187 + identification within third-party archives.
  188 +
  189 + Copyright [yyyy] [name of copyright owner]
  190 +
  191 + Licensed under the Apache License, Version 2.0 (the "License");
  192 + you may not use this file except in compliance with the License.
  193 + You may obtain a copy of the License at
  194 +
  195 + http://www.apache.org/licenses/LICENSE-2.0
  196 +
  197 + Unless required by applicable law or agreed to in writing, software
  198 + distributed under the License is distributed on an "AS IS" BASIS,
  199 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  200 + See the License for the specific language governing permissions and
  201 + limitations under the License.
3  src/plugins/plugins.pro
@@ -4,4 +4,5 @@ SUBDIRS = flare \
4 4 json \
5 5 lua \
6 6 tengine \
7   - tmw
  7 + tmw \
  8 + replicaisland
43 src/plugins/replicaisland/MAKING_TILESETS_FOR_OBJECTS.txt
... ... @@ -0,0 +1,43 @@
  1 +Replica Island has variably-sized objects, stored as individual images (and
  2 +sequences of images for frames). But we need to represent our objects as a
  3 +single tiled image.
  4 +
  5 +If you have a bunch of object images of various sizes, and you want to
  6 +make a tiled image, you place one image of each object in a directory,
  7 +numbering them sequentially and putting in transparent placeholders for
  8 +unused object IDs:
  9 +
  10 + 00_player.png
  11 + 01_coin.png
  12 + 02_ruby.png
  13 + 03_diary.png
  14 + 04_trans.png
  15 +
  16 +You can then convert these images into a tile image using the 'convert' and
  17 +'montage' commands from ImageMagick:
  18 +
  19 + # Trim transparent borders off of images. This is necessary to make
  20 + # nice tiles out of characters who are 1.5 tiles high with a 2-tile high
  21 + # image.
  22 + mkdir trimmed
  23 + for F in *.png; do
  24 + convert $F -bordercolor none -border 1x1 -trim +repage trimmed/$F
  25 + done
  26 + cd trimmed
  27 +
  28 + # Shrink images larger than 32x32.
  29 + mkdir shrunk
  30 + for F in *.png; do
  31 + convert $F -resize '32x32>' shrunk/$F
  32 + done
  33 + cd shrunk
  34 +
  35 + # Center images on 32x32 tiles.
  36 + mkdir fitted
  37 + for F in *.png; do
  38 + convert $F -background none -gravity center -extent 32x32 fitted/$F
  39 + done
  40 + cd fitted
  41 +
  42 + # Assemble tiles into a single image.
  43 + montage *.png -tile 7x -geometry +0+0 -background none objects.png
BIN  src/plugins/replicaisland/cave.png
BIN  src/plugins/replicaisland/collision_map.png
BIN  src/plugins/replicaisland/grass.png
BIN  src/plugins/replicaisland/hotspots.png
BIN  src/plugins/replicaisland/island.png
BIN  src/plugins/replicaisland/lab.png
BIN  src/plugins/replicaisland/objects.png
10 src/plugins/replicaisland/replicaisland.pro
... ... @@ -0,0 +1,10 @@
  1 +include(../plugin.pri)
  2 +
  3 +DEFINES += REPLICAISLAND_LIBRARY
  4 +
  5 +SOURCES += replicaislandplugin.cpp
  6 +HEADERS += replicaislandplugin.h\
  7 + replicaisland_global.h
  8 +
  9 +RESOURCES += \
  10 + replicaisland.qrc
14 src/plugins/replicaisland/replicaisland.qrc
... ... @@ -0,0 +1,14 @@
  1 +<RCC>
  2 + <qresource prefix="/">
  3 + <file>grass.png</file>
  4 + <file>island.png</file>
  5 + <file>sewage.png</file>
  6 + <file>cave.png</file>
  7 + <file>lab.png</file>
  8 + <file>titletileset.png</file>
  9 + <file>tutorial.png</file>
  10 + <file>collision_map.png</file>
  11 + <file>objects.png</file>
  12 + <file>hotspots.png</file>
  13 + </qresource>
  14 +</RCC>
35 src/plugins/replicaisland/replicaisland_global.h
... ... @@ -0,0 +1,35 @@
  1 +/*
  2 + * Replica Island Tiled Plugin
  3 + * Copyright 2011, Eric Kidd <eric@kiddsoftware.com>
  4 + * Copyright 2011, seeseekey <seeseekey@googlemail.com>
  5 + *
  6 + * This file is part of Tiled.
  7 + *
  8 + * This program is free software; you can redistribute it and/or modify it
  9 + * under the terms of the GNU General Public License as published by the Free
  10 + * Software Foundation; either version 2 of the License, or (at your option)
  11 + * any later version.
  12 + *
  13 + * This program is distributed in the hope that it will be useful, but WITHOUT
  14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16 + * more details.
  17 + *
  18 + * You should have received a copy of the GNU General Public License along with
  19 + * this program. If not, see <http://www.gnu.org/licenses/>.
  20 + */
  21 +
  22 +#ifndef REPLICAISLAND_GLOBAL_H
  23 +#define REPLICAISLAND_GLOBAL_H
  24 +
  25 +#include <QtCore/qglobal.h>
  26 +
  27 +#if defined(REPLICAISLAND_LIBRARY)
  28 +# define REPLICAISLANDSHARED_EXPORT Q_DECL_EXPORT
  29 +# define REPLICAISLANDSHARED_IMPORT Q_DECL_EXPORT
  30 +#else
  31 +# define REPLICAISLANDSHARED_EXPORT Q_DECL_IMPORT
  32 +# define REPLICAISLANDSHARED_IMPORT Q_DECL_IMPORT
  33 +#endif
  34 +
  35 +#endif // REPLICAISLAND_GLOBAL_H
324 src/plugins/replicaisland/replicaislandplugin.cpp
... ... @@ -0,0 +1,324 @@
  1 +/*
  2 + * Replica Island Tiled Plugin
  3 + * Copyright 2011, Eric Kidd <eric@kiddsoftware.com>
  4 + * Copyright 2011, seeseekey <seeseekey@googlemail.com>
  5 + *
  6 + * This file is part of Tiled.
  7 + *
  8 + * This program is free software; you can redistribute it and/or modify it
  9 + * under the terms of the GNU General Public License as published by the Free
  10 + * Software Foundation; either version 2 of the License, or (at your option)
  11 + * any later version.
  12 + *
  13 + * This program is distributed in the hope that it will be useful, but WITHOUT
  14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16 + * more details.
  17 + *
  18 + * You should have received a copy of the GNU General Public License along with
  19 + * this program. If not, see <http://www.gnu.org/licenses/>.
  20 + */
  21 +
  22 +#include "replicaislandplugin.h"
  23 +
  24 +#include "map.h"
  25 +#include "tile.h"
  26 +#include "tileset.h"
  27 +#include "tilelayer.h"
  28 +#include "compression.h"
  29 +
  30 +#include <QtEndian>
  31 +#include <QFile>
  32 +#include <QFileInfo>
  33 +#include <QTemporaryFile>
  34 +
  35 +using namespace ReplicaIsland;
  36 +
  37 +ReplicaIslandPlugin::ReplicaIslandPlugin()
  38 +{
  39 +}
  40 +
  41 +Tiled::Map *ReplicaIslandPlugin::read(const QString &fileName)
  42 +{
  43 + using namespace Tiled;
  44 +
  45 + // Read data.
  46 + QFile file(fileName);
  47 + if (!file.open(QIODevice::ReadOnly)) {
  48 + mError = tr("Cannot open Replica Island map file!");
  49 + return 0;
  50 + }
  51 + QDataStream in(&file);
  52 + in.setByteOrder(QDataStream::LittleEndian);
  53 + in.setFloatingPointPrecision(QDataStream::SinglePrecision);
  54 +
  55 + // Parse file header.
  56 + quint8 mapSignature, layerCount, backgroundIndex;
  57 + in >> mapSignature >> layerCount >> backgroundIndex;
  58 + if (in.status() == QDataStream::ReadPastEnd || mapSignature != 96) {
  59 + mError = tr("Can't parse file header!");
  60 + return 0;
  61 + }
  62 +
  63 + // Create our map, setting width and height to 0 until we load a layer.
  64 + Map *map = new Map(Map::Orthogonal, 0, 0, 32, 32);
  65 + map->setProperty("background_index", QString::number(backgroundIndex));
  66 +
  67 + // Load our Tilesets.
  68 + QList<Tileset *> typeTilesets, tileIndexTilesets;
  69 + loadTilesetsFromResources(map, typeTilesets, tileIndexTilesets);
  70 +
  71 + // Load each of our layers.
  72 + for (quint8 i = 0; i < layerCount; i++) {
  73 + // Parse layer header.
  74 + quint8 type, tileIndex, levelSignature;
  75 + float scrollSpeed;
  76 + qint32 width, height;
  77 + in >> type >> tileIndex >> scrollSpeed
  78 + >> levelSignature >> width >> height;
  79 + if (in.status() == QDataStream::ReadPastEnd || levelSignature != 42) {
  80 + delete map;
  81 + mError = tr("Can't parse layer header!");
  82 + return 0;
  83 + }
  84 +
  85 + // Make sure our width and height are consistent.
  86 + if (map->width() == 0)
  87 + map->setWidth(width);
  88 + if (map->height() == 0)
  89 + map->setHeight(height);
  90 + if (map->width() != width || map->height() != height) {
  91 + delete map;
  92 + mError = tr("Inconsistent layer sizes!");
  93 + return 0;
  94 + }
  95 +
  96 + // Create a layer object.
  97 + TileLayer *layer =
  98 + new TileLayer(layerTypeToName(type), 0, 0, width, height);
  99 + layer->setProperty("type", QString::number(type));
  100 + layer->setProperty("tile_index", QString::number(tileIndex));
  101 + layer->setProperty("scroll_speed", QString::number(scrollSpeed, 'f'));
  102 + map->addLayer(layer);
  103 +
  104 + // Look up the tileset for this layer.
  105 + Tileset *tileset =
  106 + tilesetForLayer(type, tileIndex, typeTilesets, tileIndexTilesets);
  107 +
  108 + // Read our tile data all at once.
  109 + QByteArray tileData(width*height, '\0');
  110 + int bytesRead = in.readRawData(tileData.data(), tileData.size());
  111 + if (bytesRead != tileData.size()) {
  112 + delete map;
  113 + mError = tr("File ended in middle of layer!");
  114 + return 0;
  115 + }
  116 + quint8 *tp = reinterpret_cast<quint8 *>(tileData.data());
  117 +
  118 + // Add the tiles to our layer.
  119 + for (int y = 0; y < height; y++) {
  120 + for (int x = 0; x < width; x++) {
  121 + quint8 tile_id = *tp++;
  122 + if (tile_id != 255) {
  123 + Tile *tile = tileset->tileAt(tile_id);
  124 + layer->setCell(x, y, Cell(tile));
  125 + }
  126 + }
  127 + }
  128 + }
  129 +
  130 + // Make sure we read the entire *.bin file.
  131 + if (in.status() != QDataStream::Ok || !in.atEnd()) {
  132 + delete map;
  133 + mError = tr("Unexpected data at end of file!");
  134 + return 0;
  135 + }
  136 +
  137 + return map;
  138 +}
  139 +
  140 +void ReplicaIslandPlugin::loadTilesetsFromResources(
  141 + Tiled::Map *map,
  142 + QList<Tiled::Tileset *> &typeTilesets,
  143 + QList<Tiled::Tileset *> &tileIndexTilesets)
  144 +{
  145 + // Create tilesets for type 0 to 3, inclusive.
  146 + typeTilesets.append(NULL); // Use a tileIndexTileset.
  147 + typeTilesets.append(loadTilesetFromResource("collision_map"));
  148 + typeTilesets.append(loadTilesetFromResource("objects"));
  149 + typeTilesets.append(loadTilesetFromResource("hotspots"));
  150 + addTilesetsToMap(map, typeTilesets);
  151 +
  152 + // Create tilesets for tileIndex 0 to 7, inclusive.
  153 + tileIndexTilesets.append(loadTilesetFromResource("grass"));
  154 + tileIndexTilesets.append(loadTilesetFromResource("island"));
  155 + tileIndexTilesets.append(loadTilesetFromResource("sewage"));
  156 + tileIndexTilesets.append(loadTilesetFromResource("cave"));
  157 + tileIndexTilesets.append(loadTilesetFromResource("lab"));
  158 + // The titletileset is also known as "lighting".
  159 + tileIndexTilesets.append(loadTilesetFromResource("titletileset"));
  160 + tileIndexTilesets.append(loadTilesetFromResource("tutorial"));
  161 + addTilesetsToMap(map, tileIndexTilesets);
  162 +}
  163 +
  164 +Tiled::Tileset *
  165 +ReplicaIslandPlugin::loadTilesetFromResource(const QString &name)
  166 +{
  167 + using namespace Tiled;
  168 +
  169 + Tileset *tileset = new Tileset(name, 32, 32);
  170 + tileset->loadFromImage(QImage(":/" + name + ".png"), name + ".png");
  171 + return tileset;
  172 +}
  173 +
  174 +void ReplicaIslandPlugin::addTilesetsToMap(Tiled::Map *map,
  175 + QList<Tiled::Tileset *> tilesets)
  176 +{
  177 + using namespace Tiled;
  178 +
  179 + QList<Tileset *>::iterator i = tilesets.begin();
  180 + for (; i != tilesets.end(); ++i)
  181 + if (*i)
  182 + map->addTileset(*i);
  183 +}
  184 +
  185 +Tiled::Tileset *ReplicaIslandPlugin::tilesetForLayer(
  186 + char type, char tileIndex,
  187 + QList<Tiled::Tileset *> typeTilesets,
  188 + QList<Tiled::Tileset *> tileIndexTilesets)
  189 +{
  190 + if (type == 0)
  191 + return tileIndexTilesets[tileIndex];
  192 + else
  193 + return typeTilesets[type];
  194 +}
  195 +
  196 +QString ReplicaIslandPlugin::layerTypeToName(char type)
  197 +{
  198 + switch (type) {
  199 + case 0: return "Background";
  200 + case 1: return "Collision";
  201 + case 2: return "Objects";
  202 + case 3: return "Hot spots";
  203 + default: return "Unknown layer type";
  204 + }
  205 +}
  206 +
  207 +QString ReplicaIslandPlugin::nameFilter() const
  208 +{
  209 + return tr("Replica Island map files (*.bin)");
  210 +}
  211 +
  212 +bool ReplicaIslandPlugin::supportsFile(const QString &fileName) const
  213 +{
  214 + // Check the file extension first.
  215 + if (QFileInfo(fileName).suffix() != QLatin1String("bin"))
  216 + return false;
  217 +
  218 + // Since we may have lots of Android-related *.bin files that aren't
  219 + // maps, check our signature byte, too.
  220 + QFile f(fileName);
  221 + if (!f.open(QIODevice::ReadOnly))
  222 + return false;
  223 + char signature;
  224 + qint64 read = f.read(&signature, 1);
  225 + return (read == 1 || signature == 96);
  226 +}
  227 +
  228 +QString ReplicaIslandPlugin::errorString() const
  229 +{
  230 + return mError;
  231 +}
  232 +
  233 +// Writer
  234 +bool ReplicaIslandPlugin::write(const Tiled::Map *map, const QString &fileName)
  235 +{
  236 + using namespace Tiled;
  237 +
  238 + // Open up a temporary file for saving the level.
  239 + QTemporaryFile temp;
  240 + if (!temp.open()) {
  241 + mError = tr("Cannot open temporary file for writing!");
  242 + return false;
  243 + }
  244 +
  245 + // Create an output stream for serializing data.
  246 + QDataStream out(&temp);
  247 + out.setByteOrder(QDataStream::LittleEndian);
  248 + out.setFloatingPointPrecision(QDataStream::SinglePrecision);
  249 +
  250 + // Write out the signature and file header.
  251 + out << static_cast<quint8>(96); // Signature.
  252 + out << static_cast<quint8>(map->layerCount());
  253 + bool ok;
  254 + out << static_cast<quint8>(map->property("background_index").toInt(&ok));
  255 + if (!ok) {
  256 + mError = tr("You must define a background_index property on the map!");
  257 + return false;
  258 + }
  259 +
  260 + // Write out each layer.
  261 + for (int i = 0; i < map->layerCount(); i++) {
  262 + TileLayer *layer = map->layerAt(i)->asTileLayer();
  263 + if (!layer) {
  264 + mError = tr("Can't save non-tile layer!");
  265 + return false;
  266 + }
  267 + if (!writeLayer(out, layer))
  268 + return false;
  269 + }
  270 +
  271 + // Overwrite our destination file with our temporary file. We only
  272 + // do this once we know we've saved a valid map.
  273 + temp.close();
  274 + QFile::remove(fileName);
  275 + if (!QFile::copy(temp.fileName(), fileName)) {
  276 + mError = tr("Couldn't overwrite old version; may be deleted!");
  277 + return false;
  278 + }
  279 +
  280 + return true;
  281 +}
  282 +
  283 +// Write out a map layer.
  284 +bool ReplicaIslandPlugin::writeLayer(QDataStream &out, Tiled::TileLayer *layer)
  285 +{
  286 + using namespace Tiled;
  287 +
  288 + // Write out the layer header.
  289 + bool ok;
  290 + out << static_cast<quint8>(layer->property("type").toInt(&ok));
  291 + if (!ok) {
  292 + mError = tr("You must define a type property on each layer!");
  293 + return false;
  294 + }
  295 + out << static_cast<quint8>(layer->property("tile_index").toInt(&ok));
  296 + if (!ok) {
  297 + mError = tr("You must define a tile_index property on each layer!");
  298 + return false;
  299 + }
  300 + out << layer->property("scroll_speed").toFloat(&ok);
  301 + if (!ok) {
  302 + mError = tr("You must define a scroll_speed property on each layer!");
  303 + return false;
  304 + }
  305 + out << static_cast<quint8>(42); // Layer signature.
  306 + out << static_cast<qint32>(layer->width());
  307 + out << static_cast<qint32>(layer->height());
  308 +
  309 + // Write out the raw tile data. We assume that the user has used the
  310 + // correct tileset for this layer.
  311 + for (int y = 0; y < layer->height(); y++) {
  312 + for (int x = 0; x < layer->width(); x++) {
  313 + Tile *tile = layer->cellAt(x, y).tile;
  314 + if (tile)
  315 + out << static_cast<quint8>(tile->id());
  316 + else
  317 + out << static_cast<quint8>(255);
  318 + }
  319 + }
  320 +
  321 + return true;
  322 +}
  323 +
  324 +Q_EXPORT_PLUGIN2(ReplicaIsland, ReplicaIslandPlugin)
89 src/plugins/replicaisland/replicaislandplugin.h
... ... @@ -0,0 +1,89 @@
  1 +/*
  2 + * Replica Island Tiled Plugin
  3 + * Copyright 2011, Eric Kidd <eric@kiddsoftware.com>
  4 + * Copyright 2011, seeseekey <seeseekey@googlemail.com>
  5 + *
  6 + * This file is part of Tiled.
  7 + *
  8 + * This program is free software; you can redistribute it and/or modify it
  9 + * under the terms of the GNU General Public License as published by the Free
  10 + * Software Foundation; either version 2 of the License, or (at your option)
  11 + * any later version.
  12 + *
  13 + * This program is distributed in the hope that it will be useful, but WITHOUT
  14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16 + * more details.
  17 + *
  18 + * You should have received a copy of the GNU General Public License along with
  19 + * this program. If not, see <http://www.gnu.org/licenses/>.
  20 + */
  21 +
  22 +#ifndef REPLICAISLANDPLUGIN_H
  23 +#define REPLICAISLANDPLUGIN_H
  24 +
  25 +#include "replicaisland_global.h"
  26 +
  27 +#include "map.h"
  28 +#include "mapwriterinterface.h"
  29 +#include "mapreaderinterface.h"
  30 +
  31 +#include <QObject>
  32 +
  33 +namespace Tiled {
  34 +
  35 +class TileLayer;
  36 +
  37 +};
  38 +
  39 +namespace ReplicaIsland {
  40 +
  41 +/**
  42 + * Read and write maps in Replica Island format. Replica Island is an
  43 + * open source side-scrolling video game for Android.
  44 + */
  45 +class REPLICAISLANDSHARED_EXPORT ReplicaIslandPlugin :
  46 + public QObject,
  47 + public Tiled::MapWriterInterface,
  48 + public Tiled::MapReaderInterface
  49 +{
  50 + Q_OBJECT
  51 + Q_INTERFACES(Tiled::MapReaderInterface)
  52 + Q_INTERFACES(Tiled::MapWriterInterface)
  53 +
  54 +public:
  55 + /**
  56 + * Create an instance of the plugin.
  57 + */
  58 + ReplicaIslandPlugin();
  59 +
  60 + // MapReaderInterface
  61 + Tiled::Map *read(const QString &fileName);
  62 + QString nameFilter() const;
  63 + bool supportsFile(const QString &fileName) const;
  64 + QString errorString() const;
  65 +
  66 + // MapWriterInterface
  67 + bool write(const Tiled::Map *map, const QString &fileName);
  68 +
  69 +private:
  70 + QString mError;
  71 +
  72 + // MapReaderInterface support.
  73 + void loadTilesetsFromResources(Tiled::Map *map,
  74 + QList<Tiled::Tileset *> &typeTilesets,
  75 + QList<Tiled::Tileset *> &tileIndexTilesets);
  76 + Tiled::Tileset *loadTilesetFromResource(const QString &name);
  77 + void addTilesetsToMap(Tiled::Map *map, QList<Tiled::Tileset *> tilesets);
  78 + Tiled::Tileset *tilesetForLayer(char type, char tileIndex,
  79 + QList<Tiled::Tileset *> typeTilesets,
  80 + QList<Tiled::Tileset *> tileIndexTilesets);
  81 + QString layerTypeToName(char type);
  82 +
  83 + // MapWriterInterface support.
  84 + bool writeLayer(QDataStream &out, Tiled::TileLayer *layer);
  85 +};
  86 +
  87 +} // namespace ReplicaIsland
  88 +
  89 +#endif // REPLICAISLANDPLUGIN_H
BIN  src/plugins/replicaisland/sewage.png
BIN  src/plugins/replicaisland/titletileset.png
BIN  src/plugins/replicaisland/tutorial.png

0 comments on commit 2422222

Please sign in to comment.
Something went wrong with that request. Please try again.