Initial commit

This commit is contained in:
2026-02-02 04:50:13 +01:00
commit 5b11698731
22592 changed files with 7677434 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This code is based on the CRAB engine
*
* Copyright (c) Arvind Raja Yadav
*
* Licensed under MIT
*
*/
#include "crab/TMX/TMXLayer.h"
namespace Crab {
using namespace TMX;
bool Layer::load(rapidxml::xml_node<char> *node) {
if (nodeValid(node))
return loadStr(_name, "name", node) && loadNum(_w, "width", node) && loadNum(_h, "height", node);
return false;
}
bool MapLayer::load(const Common::Path &path, rapidxml::xml_node<char> *node) {
if (Layer::load(node)) {
if (nodeValid("image", node, false)) {
_type = LAYER_IMAGE;
rapidxml::xml_node<char> *imgnode = node->first_node("image");
if (imgnode->first_attribute("source") != nullptr)
_img.load(path.append(imgnode->first_attribute("source")->value()));
} else {
_type = LAYER_NORMAL;
int i = 0;
Common::Array<TileInfo> t;
//.tmx stores tiles row-first
for (auto n = node->first_node("data")->first_node("tile"); n != nullptr; n = n->next_sibling("tile")) {
t.push_back(n);
if (++i >= _w) {
_tile.push_back(t);
t.clear();
i = 0;
}
}
}
// load properties associated with the layer, such as:
// Is it a prop layer or not? If yes, the prop dimensions
// The rate of scrolling of the layer, used for parallax scrolling
if (nodeValid("properties", node, false)) {
Common::String n, v;
for (auto p = node->first_node("properties")->first_node("property"); p != nullptr; p = p->next_sibling("property")) {
if (loadStr(n, "name", p) && loadStr(v, "value", p)) {
if (n == "prop" && v == "true")
_type = LAYER_PROP;
else if (n == "autohide" && v == "true")
_type = LAYER_AUTOHIDE;
else if (n == "autoshow" && v == "true")
_type = LAYER_AUTOSHOW;
else if (n == "x") {
_pos.x = stringToNumber<int>(v);
} else if (n == "y") {
_pos.y = stringToNumber<int>(v);
} else if (n == "w") {
_pos.w = stringToNumber<int>(v);
} else if (n == "h") {
_pos.h = stringToNumber<int>(v);
} else if (n == "scroll_rate_x") {
_rate.x = stringToNumber<float>(v);
_type = LAYER_PARALLAX;
} else if (n == "scroll_rate_y") {
_rate.y = stringToNumber<float>(v);
_type = LAYER_PARALLAX;
}
}
}
}
return true;
}
return false;
}
} // End of namespace Crab

115
engines/crab/TMX/TMXLayer.h Normal file
View File

@@ -0,0 +1,115 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This code is based on the CRAB engine
*
* Copyright (c) Arvind Raja Yadav
*
* Licensed under MIT
*
*/
#ifndef CRAB_TMXLAYER_H
#define CRAB_TMXLAYER_H
#include "crab/Rectangle.h"
#include "crab/image/Image.h"
#include "crab/TMX/TileInfo.h"
#include "common/list.h"
namespace Crab {
namespace TMX {
enum LayerType {
// Run of the mill layer full of tiles
LAYER_NORMAL,
// Layer containing a simple image instead of tiles
LAYER_IMAGE,
// We do not draw prop layers along with the non-prop layers,
// because their draw order depends on the positions of the sprites
LAYER_PROP,
// Parallax layers are drawn differently, but are still composed of tiles
LAYER_PARALLAX,
// Auto-hide layers are primarily used for interiors
// These are only visible if the player is colliding with them
LAYER_AUTOHIDE,
// Auto-show layers are the reverse of auto-hide layers
// These are only visible if the player is not colliding with them
LAYER_AUTOSHOW
};
class Layer {
public:
// Name of the layer
Common::String _name;
// Dimensions of the layer in terms of tiles
int _w, _h;
Layer() {
_w = 0;
_h = 0;
}
bool load(rapidxml::xml_node<char> *node);
};
// Currently we just use one general purpose layer object instead of multiple inherited classes and stuff
class MapLayer : public Layer {
public:
// The tiles in the layer
Common::Array<Common::Array<TileInfo>> _tile;
// The type of layer
LayerType _type;
// The image in the layer
pyrodactyl::image::Image _img;
// The coordinates to draw the prop in(x,y) and dimensions of the area(w,h) in terms of tiles
// This is also the collision rectangle of the prop and auto hide layer
Rect _pos;
Common::List<Rect> _boundRect;
// The rate of scrolling of image, used for parallax
Vector2f _rate;
// Is the player colliding with the layer? (used for auto hide layer)
bool _collide;
MapLayer() : _rate(1, 1) {
_type = LAYER_NORMAL;
_collide = false;
}
bool load(const Common::Path &path, rapidxml::xml_node<char> *node);
};
} // End of namespace TMX
} // End of namespace Crab
#endif // CRAB_TMXLAYER_H

374
engines/crab/TMX/TMXMap.cpp Normal file
View File

@@ -0,0 +1,374 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This code is based on the CRAB engine
*
* Copyright (c) Arvind Raja Yadav
*
* Licensed under MIT
*
*/
#include "crab/image/ImageManager.h"
#include "crab/text/TextManager.h"
#include "crab/TMX/TMXMap.h"
namespace Crab {
using namespace TMX;
using namespace pyrodactyl::image;
static bool propCompare(const MapLayer &l1, const MapLayer &l2) {
return l1._pos.y + l1._pos.h < l2._pos.y + l2._pos.h;
}
TMXMap::TMXMap() {
_tileRows = 0;
_tileCols = 0;
_pathRows = 0;
_pathCols = 0;
_w = 0;
_h = 0;
_spriteLayer = 0;
_grid = nullptr;
_movementCosts._noWalk = 0;
_movementCosts._open = 0;
_movementCosts._stairs = 0;
}
//------------------------------------------------------------------------
// Purpose: Load stuff via a .tmx file set to xml storage (no compression)
//------------------------------------------------------------------------
void TMXMap::load(const Common::Path &path, const Common::String &filename) {
XMLDoc conf(path.appendComponent(filename));
if (conf.ready()) {
rapidxml::xml_node<char> *node = conf.doc()->first_node("map");
if (nodeValid(node)) {
loadNum(_tileRows, "width", node);
loadNum(_tileCols, "height", node);
loadNum(_tileSize.x, "tilewidth", node);
loadNum(_tileSize.y, "tileheight", node);
// Pathfinding info load
_pathSize.x = 0;
_pathSize.y = 0;
loadNum(_pathSize.x, "pathwidth", node);
loadNum(_pathSize.y, "pathheight", node);
// Load costs...Not sure if this is right. (SZ)
loadNum(_movementCosts._open, "opencost", node);
loadNum(_movementCosts._noWalk, "nowalkcost", node);
loadNum(_movementCosts._stairs, "stairscost", node);
// if(path_size.x == 0)
// path_size.x = tile_size.x;
// if(path_size.y == 0)
// path_size.y = tile_size.y;
// Testing
if (_pathSize.x == 0)
_pathSize.x = 40;
if (_pathSize.y == 0)
_pathSize.y = 40;
_w = _tileRows * _tileSize.x;
_h = _tileCols * _tileSize.y;
_pathRows = (int)ceil((float)_w / (float)_pathSize.x + .5f); // Adding .5 before casting in order to round up (SZ)
_pathCols = (int)ceil((float)_h / (float)_pathSize.y + .5f);
g_engine->_imageManager->_tileset.load(path, node);
// Reset the layer at which sprites are drawn
_spriteLayer = 0;
uint layerCount = 0;
// We need to cycle through all tile and object layers in order to
// see the level at which the sprites will be drawn
for (auto groupnode = node->first_node(); groupnode != nullptr; groupnode = groupnode->next_sibling()) {
// Store the name for easy comparison
Common::String name = groupnode->name();
if (name == "layer" || name == "imagelayer") { // Is this a tile or an image layer
MapLayer l;
l.load(path, groupnode);
l._pos.x *= _tileSize.x;
l._pos.y *= _tileSize.y;
l._pos.w *= _tileSize.x;
l._pos.h *= _tileSize.y;
if (l._type == LAYER_PROP)
_prop.push_back(l);
else
_layer.push_back(l);
layerCount++;
} else if (name == "objectgroup") { // Is this an object layer
Common::String groupName;
loadStr(groupName, "name", groupnode);
if (groupName == "exit") {
for (auto n = groupnode->first_node("object"); n != nullptr; n = n->next_sibling("object")) {
pyrodactyl::level::Exit le(n);
_areaExit.push_back(le);
}
} else if (groupName == "walk") {
auto n = groupnode->first_node("object");
if (n != nullptr)
_areaWalk.load(n, true, "x", "y", "width", "height");
} else if (groupName == "no_walk") {
for (auto n = groupnode->first_node("object"); n != nullptr; n = n->next_sibling("object")) {
Shape s;
s.load(n);
_areaNowalk.push_back(s);
}
} else if (groupName == "trigger") {
for (auto n = groupnode->first_node("object"); n != nullptr; n = n->next_sibling("object")) {
Shape s;
s.load(n);
uint pos = _areaTrig.size();
loadNum(pos, "name", n);
if (_areaTrig.size() <= pos)
_areaTrig.resize(pos + 1);
_areaTrig[pos] = s;
}
} else if (groupName == "stairs") {
for (auto n = groupnode->first_node("object"); n != nullptr; n = n->next_sibling("object")) {
pyrodactyl::level::Stairs s;
s.load(n);
_areaStairs.push_back(s);
}
} else if (groupName == "music") {
for (auto n = groupnode->first_node("object"); n != nullptr; n = n->next_sibling("object")) {
pyrodactyl::level::MusicArea ma;
ma.load(n);
_areaMusic.push_back(ma);
}
} else if (groupName == "sprites")
_spriteLayer = layerCount;
}
}
// Sort the props in the level according to y axis
Common::sort(_prop.begin(), _prop.end(), propCompare);
}
}
}
//------------------------------------------------------------------------
// Purpose: Use this if you want to load a poly line from tmx
//------------------------------------------------------------------------
// void TMXMap::LoadPath(rapidxml::xml_node<char> *node)
//{
// int pos = 0;
// for (auto n = node->first_node("object"); n != nullptr; n = n->next_sibling("object"), ++pos)
// {
// Vector2i start;
// start.load(n);
//
// rapidxml::xml_node<char> *linenode = n->first_node("polyline");
// if (linenode != nullptr)
// {
// Common::String points, x, y;
// loadStr(points, "points", linenode);
//
// path.resize(pos + 1);
// bool comma = false;
// for (auto i = points.begin(); i != points.end(); ++i)
// {
// if (*i == ',') comma = true;
// else if (*i == ' ')
// {
// path[pos].push_back(GetPoint(start, x, y));
// comma = false;
// x.clear();
// y.clear();
// }
// else if (comma) y.push_back(*i);
// else x.push_back(*i);
// }
// path[pos].push_back(GetPoint(start, x, y));
// }
// }
//}
//------------------------------------------------------------------------
// Purpose: Convert point from string to vector
//------------------------------------------------------------------------
// const Vector2i TMXMap::GetPoint(const Vector2i &ref, Common::String &x, Common::String &y)
//{
// Vector2i v;
// v.x = ref.x + StringToNumber<int>(x);
// v.y = ref.y + StringToNumber<int>(y);
// return v;
//}
//------------------------------------------------------------------------
// Purpose: Clear all data from the level
//------------------------------------------------------------------------
void TMXMap::reset() {
g_engine->_imageManager->_tileset.reset();
_layer.clear();
_areaNowalk.clear();
_areaExit.clear();
_areaTrig.clear();
_areaStairs.clear();
_areaMusic.clear();
_prop.clear();
_spriteLayer = 0;
}
//------------------------------------------------------------------------
// Purpose: Draw functions
//------------------------------------------------------------------------
void TMXMap::drawDebug(const Rect &camera) {
using namespace pyrodactyl::text;
for (auto &i : _areaTrig)
i.draw(-camera.x, -camera.y, 0, 0, 254, 254);
for (auto &i : _areaExit)
i._dim.draw(-camera.x, -camera.y, 0, 254, 254, 254);
for (auto &i : _prop)
i._pos.draw(-camera.x, -camera.y, 254, 0, 254, 254);
for (auto &i : _areaNowalk)
i.draw(-camera.x, -camera.y, 254, 0, 0, 254);
for (auto &i : _areaMusic)
i.draw(-camera.x, -camera.y, 254, 254, 0, 254);
for (auto &i : _areaStairs) {
i.draw(-camera.x, -camera.y, 0, 254, 0, 254);
}
// Draw the pathfinding grid (SZ)
for (int x = 0; x < _grid->getDimensions().x; ++x) {
for (int y = 0; y < _grid->getDimensions().y; ++y) {
if (_grid->getNodeAtCoords(x, y)->getMovementCost() < 0.0f)
_grid->getNodeAtCoords(x, y)->getRect().draw(-camera.x, -camera.y, 0, 0, 0, 254);
}
}
for (auto &i : _layer)
i._pos.draw(-camera.x, -camera.y, 254, 216, 0);
_areaWalk.draw(-camera.x, -camera.y, 254, 254, 254, 254);
}
//------------------------------------------------------------------------
// Purpose: Collision functions
//------------------------------------------------------------------------
void TMXMap::collideWithNoWalk(const Rect boundingBox, Common::List<CollisionData> &colliders) {
CollisionData res;
for (auto &i : _areaNowalk) {
res = i.collide(boundingBox);
if (res._intersect)
colliders.push_back(res);
}
}
bool TMXMap::insideNoWalk(const Vector2i &pos) {
for (auto &i : _areaNowalk)
if (i.contains(pos))
return true;
return false;
}
bool TMXMap::insideWalk(const Rect &boundingBox) {
if (_areaWalk.contains(boundingBox))
return true;
return false;
}
bool TMXMap::insideWalk(const Vector2i &pos) {
if (_areaWalk.contains(pos))
return true;
return false;
}
bool TMXMap::collideWithTrigger(const Rect rect, int index) {
if (_areaTrig.size() > (uint)index)
return _areaTrig[index].collide(rect)._intersect;
return false;
}
void TMXMap::collideWithTrigger(const Rect rect, Common::Array<int> &collisionTable) {
int index = 0;
collisionTable.clear();
for (auto i = _areaTrig.begin(); i != _areaTrig.end(); ++i, ++index)
if (i->collide(rect)._intersect)
collisionTable.push_back(index);
}
bool TMXMap::collideWithExit(const Rect rect, LevelResult &res) {
for (const auto &i : _areaExit)
if (i._dim.collide(rect)._intersect) {
res._val = i._name;
res._x = i._entry.x;
res._y = i._entry.y;
return true;
}
return false;
}
bool TMXMap::collideWithStairs(const Rect rect, Vector2f &velMod) {
for (const auto &i : _areaStairs) {
if (i.collide(rect)._intersect) {
velMod = i._modifier;
return true;
}
}
// We are not colliding with any stairs, reset the modifier
velMod.x = 1.0f;
velMod.y = 1.0f;
return false;
}
bool TMXMap::collideWithMusic(const Rect rect, pyrodactyl::level::MusicInfo &music) {
for (const auto &i : _areaMusic) {
if (i.collide(rect)._intersect) {
music._id = i._id;
music._track = i._track;
music._loops = i._loops;
return true;
}
}
return false;
}
} // End of namespace Crab

148
engines/crab/TMX/TMXMap.h Normal file
View File

@@ -0,0 +1,148 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This code is based on the CRAB engine
*
* Copyright (c) Arvind Raja Yadav
*
* Licensed under MIT
*
*/
#ifndef CRAB_TMXMAP_H
#define CRAB_TMXMAP_H
#include "crab/LevelResult.h"
#include "crab/Shape.h"
#include "crab/XMLDoc.h"
#include "crab/ai/spriteai.h"
#include "crab/level/LevelExit.h"
#include "crab/level/MusicArea.h"
#include "crab/level/Stairs.h"
#include "crab/TMX/TMXTileSet.h"
namespace Crab {
namespace TMX {
// For TMX version 1.0, orthogonal maps only
class TMXMap {
protected:
// The actual dimensions of the level
int _w, _h;
// The area you can move in
Rect _areaWalk;
// The non-walk able areas in the level
Common::Array<Shape> _areaNowalk;
// The trigger rectangles in the level
Common::Array<Shape> _areaTrig;
// Stairs modify the player walking speed
Common::Array<pyrodactyl::level::Stairs> _areaStairs;
// Music areas change the music if player collides with them
Common::Array<pyrodactyl::level::MusicArea> _areaMusic;
// Archived methods for loading poly lines in tiled
// void LoadPath(rapidxml::xml_node<char> *node);
// const Vector2i GetPoint(const Vector2i &ref, Common::String &x, Common::String &y);
public:
// The exits to different levels
Common::Array<pyrodactyl::level::Exit> _areaExit;
// The layer on top of which objects walk
uint _spriteLayer;
// Dimensions of the level in terms of tiles
int _tileRows, _tileCols;
// Dimensions of the level in terms of pathfinding grid cells (SZ)
int _pathRows, _pathCols;
// The width and height of tiles
Vector2i _tileSize;
// The width and height of pathfinding grid cells (SZ)
Vector2i _pathSize;
// The layers of tiles in the level
Common::Array<MapLayer> _layer;
// The props in the level
Common::Array<MapLayer> _prop;
PathfindingGrid *_grid; // The grid of graph nodes used for navigating.
// Movement costs
struct {
int _open, _noWalk, _stairs;
} _movementCosts;
TMXMap();
~TMXMap() {}
void reset();
void load(const Common::Path &path, const Common::String &filename);
void drawDebug(const Rect &camera);
bool insideWalk(const Rect &boundingBox);
bool insideWalk(const Vector2i &pos);
bool insideNoWalk(const Vector2i &pos);
void collideWithNoWalk(const Rect boundingBox, Common::List<CollisionData> &colliders);
bool collideWithExit(const Rect rect, LevelResult &res);
bool collideWithStairs(const Rect rect, Vector2f &velMod);
bool collideWithMusic(const Rect rect, pyrodactyl::level::MusicInfo &music);
bool collideWithTrigger(const Rect rect, int index);
void collideWithTrigger(const Rect rect, Common::Array<int> &collisionTable);
int w() {
return _w;
}
int h() {
return _h;
}
const Rect &areaWalk() const {
return _areaWalk;
}
const Common::Array<Shape> &areaNoWalk() const {
return _areaNowalk;
}
const Common::Array<pyrodactyl::level::Stairs> &areaStairs() const {
return _areaStairs;
}
};
} // End of namespace TMX
} // End of namespace Crab
#endif // CRAB_TMXMAP_H

View File

@@ -0,0 +1,270 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This code is based on the CRAB engine
*
* Copyright (c) Arvind Raja Yadav
*
* Licensed under MIT
*
*/
#include "graphics/screen.h"
#include "crab/crab.h"
#include "crab/text/TextManager.h"
#include "crab/TMX/TMXTileSet.h"
namespace Crab {
using namespace TMX;
void TileSet::load(const Common::Path &path, rapidxml::xml_node<char> *node) {
if (nodeValid(node)) {
loadNum(_firstGid, "firstgid", node);
loadStr(_name, "name", node);
loadNum(_tileW, "tilewidth", node);
loadNum(_tileH, "tileheight", node);
_clip.w = _tileW;
_clip.h = _tileH;
if (nodeValid("image", node)) {
rapidxml::xml_node<char> *imgnode = node->first_node("image");
Common::String filename;
loadStr(filename, "source", imgnode);
_loc = path.appendComponent(filename);
_img.load(_loc);
_totalRows = _img.h() / _tileH;
_totalCols = _img.w() / _tileW;
debugC(kDebugGraphics, "Total rows : %d Total cols: %d gid: %d", _totalRows, _totalCols, _firstGid);
}
}
// Prevent divide by zero errors later
if (_totalCols == 0)
_totalCols = 1;
}
void TileSetGroup::reset() {
for (auto &i : _tileset)
i._img.deleteImage();
_tileset.clear();
}
void TileSetGroup::load(const Common::Path &path, rapidxml::xml_node<char> *node) {
reset();
for (auto n = node->first_node("tileset"); n != nullptr; n = n->next_sibling("tileset")) {
TileSet t;
t.load(path, n);
_tileset.push_back(t);
}
}
void TileSet::draw(const Vector2i &pos, const TileInfo &tile) {
if (tile._gid != 0) {
_clip.x = ((tile._gid - _firstGid) % _totalCols) * _tileW;
_clip.y = ((tile._gid - _firstGid) / _totalCols) * _tileH;
_img.draw(pos.x, pos.y, &_clip, tile._flip);
}
}
void TileSet::preDraw(const Vector2i &pos, const TileInfo &tile, Graphics::ManagedSurface *surf) {
if (tile._gid != 0) {
_clip.x = ((tile._gid - _firstGid) % _totalCols) * _tileW;
_clip.y = ((tile._gid - _firstGid) / _totalCols) * _tileH;
_img.draw(pos.x, pos.y, &_clip, tile._flip, surf);
}
}
void TileSetGroup::preDraw(MapLayer &layer, const Vector2i &tileSize, Graphics::ManagedSurface *surf) {
if (layer._type == LAYER_IMAGE || layer._type == LAYER_AUTOSHOW)
return;
_start.x = 0;
_start.y = 0;
_finish.x = layer._tile.size();
_finish.y = layer._tile[0].size();
_v.x = _start.y * tileSize.x;
_v.y = _start.x * tileSize.y;
for (int x = _start.x; x < _finish.x; ++x) {
for (int y = _start.y; y < _finish.y; ++y) {
for (int i = _tileset.size() - 1; i >= 0; --i)
if (layer._tile[x][y]._gid >= _tileset[i]._firstGid) {
_tileset[i].preDraw(_v, layer._tile[x][y], surf);
layer._boundRect.push_back(Rect(_v.x, _v.y, tileSize.x, tileSize.y));
break;
}
_v.x += tileSize.x;
}
_v.x = _start.y * tileSize.x;
_v.y += tileSize.y;
}
Common::List<Rect>::iterator rOuter, rInner;
// Process the bound rect list to find any rects to merge
for (rOuter = layer._boundRect.begin(); rOuter != layer._boundRect.end(); ++rOuter) {
rInner = rOuter;
while (++rInner != layer._boundRect.end()) {
if ((*rOuter).collide(*rInner)) {
rOuter->extend(*rInner);
layer._boundRect.erase(rInner);
rInner = rOuter;
}
}
}
}
void TileSetGroup::forceDraw(MapLayer &layer, const Rect &camera, const Vector2i &tileSize, const Rect &playerPos) {
if (layer._type == LAYER_IMAGE)
return;
layer._collide = layer._pos.collide(playerPos);
// Normal and prop layers are drawn this way
// The row and column we start drawing at
_start.x = playerPos.y / tileSize.y;
_start.y = playerPos.x / tileSize.x;
if (_start.x < 0 || _start.y < 0)
return;
// The row and column we end drawing at
_finish.x = (playerPos.y + playerPos.h) / tileSize.y + 1;
_finish.y = (playerPos.x + playerPos.w) / tileSize.x + 1;
if (layer._type == LAYER_AUTOSHOW) {
if (layer._collide)
return;
_start.x = camera.y / tileSize.y;
_start.y = camera.x / tileSize.x;
//The row and column we end drawing at
_finish.x = (camera.y + camera.h) / tileSize.y + 1;
_finish.y = (camera.x + camera.w) / tileSize.x + 1;
}
if (_finish.x > (int)layer._tile.size())
_finish.x = layer._tile.size();
if (_finish.y > (int)layer._tile[0].size())
_finish.y = layer._tile[0].size();
_v.x = _start.y * tileSize.x - camera.x;
_v.y = _start.x * tileSize.y - camera.y;
for (int x = _start.x; x < _finish.x; ++x) {
for (int y = _start.y; y < _finish.y; ++y) {
for (int i = _tileset.size() - 1; i >= 0; --i)
if (layer._tile[x][y]._gid >= _tileset[i]._firstGid) {
_tileset[i].draw(_v, layer._tile[x][y]);
break;
}
_v.x += tileSize.x;
}
_v.x = _start.y * tileSize.x - camera.x;
_v.y += tileSize.y;
}
}
void TileSetGroup::draw(MapLayer &layer, const Rect &camera, const Vector2i &tileSize, const Rect &playerPos, pyrodactyl::image::Image &img) {
if (layer._type == LAYER_IMAGE)
layer._img.draw(-1.0f * camera.x * layer._rate.x, -1.0f * camera.y * layer._rate.y);
else if (layer._type == LAYER_PARALLAX) {
// The row and column we start drawing at
_start.x = 0;
_start.y = 0;
// The row and column we end drawing at
_finish.x = layer._tile.size() - 1;
_finish.y = layer._tile[0].size() - 1;
_v.x = (_start.y * tileSize.x - camera.x) * layer._rate.x;
_v.y = (_start.x * tileSize.y - camera.y) * layer._rate.y;
for (int x = _start.x; x < _finish.x; ++x) {
for (int y = _start.y; y < _finish.y; ++y) {
for (int i = _tileset.size() - 1; i >= 0; --i)
if (layer._tile[x][y]._gid >= _tileset[i]._firstGid) {
_tileset[i].draw(_v, layer._tile[x][y]);
break;
}
_v.x += tileSize.x;
}
_v.x = (_start.y * tileSize.x - camera.x) * layer._rate.x;
_v.y += tileSize.y;
}
} else {
layer._collide = layer._pos.collide(playerPos);
// If player is inside the layer bounds, draw normally - else skip drawing
if (layer._type == LAYER_AUTOHIDE && !layer._collide)
return;
// If the player is outside the layer bounds, draw normally - else skip drawing
if (layer._type == LAYER_AUTOSHOW && layer._collide)
return;
// Normal and prop layers are drawn this way
// The row and column we start drawing at
_start.x = camera.y / tileSize.y;
_start.y = camera.x / tileSize.x;
// The row and column we end drawing at
_finish.x = (camera.y + camera.h) / tileSize.y + 1;
_finish.y = (camera.x + camera.w) / tileSize.x + 1;
if (_finish.x > (int)layer._tile.size())
_finish.x = layer._tile.size();
if (_finish.y > (int)layer._tile[0].size())
_finish.y = layer._tile[0].size();
_v.x = camera.x;
_v.y = camera.y;
Vector2i end;
end.x = camera.x + g_engine->_screen->w;
end.y = camera.y + g_engine->_screen->h;
Rect clip(_v.x, _v.y, end.x - _v.x, end.y - _v.y);
img.fastDraw(0, 0, &clip);
}
}
} // End of namespace Crab

View File

@@ -0,0 +1,109 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This code is based on the CRAB engine
*
* Copyright (c) Arvind Raja Yadav
*
* Licensed under MIT
*
*/
#ifndef CRAB_TMXTILESET_H
#define CRAB_TMXTILESET_H
#include "crab/image/Image.h"
#include "crab/TMX/TMXLayer.h"
namespace Crab {
namespace TMX {
struct TileSet {
// The name of the tileset
Common::String _name;
// The location of the tileset image on the disk
Common::Path _loc;
// The first gid of the tileset
GidFormat _firstGid;
// Dimensions of tiles
int _tileW, _tileH;
// Number of rows and columns of tiles
int _totalRows, _totalCols;
// The image used by the tileset
pyrodactyl::image::Image _img;
// Stuff used to store temporary data
// The rectangle used to store clip info
Rect _clip;
void init() {
_firstGid = 1;
_tileW = 1;
_tileH = 1;
_totalRows = 1;
_totalCols = 1;
}
TileSet() {
init();
}
TileSet(const Common::Path &path, rapidxml::xml_node<char> *node) {
init();
load(path, node);
}
void load(const Common::Path &path, rapidxml::xml_node<char> *node);
void draw(const Vector2i &pos, const TileInfo &tile);
void preDraw(const Vector2i &pos, const TileInfo &tile, Graphics::ManagedSurface *surf);
};
class TileSetGroup {
Common::Array<TileSet> _tileset;
// The latest tile position
Vector2i _v;
// The area that we have to draw
Vector2i _start, _finish;
public:
TileSetGroup() {}
void reset();
void load(const Common::Path &path, rapidxml::xml_node<char> *node);
void draw(MapLayer &layer, const Rect &camera, const Vector2i &tileSize, const Rect &playerPos, pyrodactyl::image::Image &img);
void preDraw(MapLayer &layer, const Vector2i &tileSize, Graphics::ManagedSurface *surf);
void forceDraw(MapLayer &layer, const Rect &camera, const Vector2i &tileSize, const Rect &playerPos);
};
} // End of namespace TMX
} // End of namespace Crab
#endif // CRAB_TMXTILESET_H

100
engines/crab/TMX/TileInfo.h Normal file
View File

@@ -0,0 +1,100 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This code is based on the CRAB engine
*
* Copyright (c) Arvind Raja Yadav
*
* Licensed under MIT
*
*/
#ifndef CRAB_TILEINFO_H
#define CRAB_TILEINFO_H
#include "crab/loaders.h"
namespace Crab {
namespace TMX {
typedef uint GidFormat;
// Bits on the far end of the 32-bit global tile ID are used for tile flags
const static GidFormat FlippedHorizontallyFlag = 0x80000000;
const static GidFormat FlippedVerticallyFlag = 0x40000000;
const static GidFormat FlippedAntiDiagonallyFlag = 0x20000000;
struct TileInfo {
// The gid of the tile
GidFormat _gid;
// Do we need to flip this tile?
TextureFlipType _flip;
TileInfo() {
_gid = 0;
_flip = FLIP_NONE;
}
TileInfo(rapidxml::xml_node<char> *node) {
// Load the gid of the tile
if (!loadNum(_gid, "gid", node))
_gid = 0;
bool horizontal = (_gid & FlippedHorizontallyFlag) != 0;
bool vertical = (_gid & FlippedVerticallyFlag) != 0;
bool antidiagonal = (_gid & FlippedAntiDiagonallyFlag) != 0;
// Find how the tile is flipped
if (horizontal) {
if (vertical) {
if (antidiagonal)
_flip = FLIP_XYD;
else
_flip = FLIP_XY;
} else if (antidiagonal)
_flip = FLIP_DX;
else
_flip = FLIP_X;
} else if (vertical) {
if (antidiagonal)
_flip = FLIP_DY;
else
_flip = FLIP_Y;
} else if (antidiagonal)
_flip = FLIP_D;
else
_flip = FLIP_NONE;
// Clear the flags
_gid &= ~(FlippedHorizontallyFlag | FlippedVerticallyFlag | FlippedAntiDiagonallyFlag);
}
bool operator==(const TileInfo& other) const {
return (_gid == other._gid) && (_flip == other._flip);
}
};
} // End of namespace TMX
} // End of namespace Crab
#endif // CRAB_TILEINFO_H