summaryrefslogtreecommitdiff
path: root/src/resources
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2006-11-02 21:26:57 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2006-11-02 21:26:57 +0000
commit19e15c87a1fc74f71fd6f9a743201a24ac582997 (patch)
treea615525476ef6bad8a2af4d8b90c8aea935f9109 /src/resources
parent29f07d2f98b82674708f1185f26ed3c482992b04 (diff)
downloadmana-19e15c87a1fc74f71fd6f9a743201a24ac582997.tar.gz
mana-19e15c87a1fc74f71fd6f9a743201a24ac582997.tar.bz2
mana-19e15c87a1fc74f71fd6f9a743201a24ac582997.tar.xz
mana-19e15c87a1fc74f71fd6f9a743201a24ac582997.zip
Merged trunk changes from revision 2716 to 2756 into the 0.1.0 branch.
Diffstat (limited to 'src/resources')
-rw-r--r--src/resources/ambientoverlay.cpp73
-rw-r--r--src/resources/ambientoverlay.h57
-rw-r--r--src/resources/mapreader.cpp128
3 files changed, 204 insertions, 54 deletions
diff --git a/src/resources/ambientoverlay.cpp b/src/resources/ambientoverlay.cpp
new file mode 100644
index 00000000..058b6083
--- /dev/null
+++ b/src/resources/ambientoverlay.cpp
@@ -0,0 +1,73 @@
+/*
+ * The Mana World
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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 2 of the License, or
+ * any later version.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+#include "ambientoverlay.h"
+
+#include "image.h"
+
+#include "../graphics.h"
+
+AmbientOverlay::AmbientOverlay(Image *img, float parallax,
+ float speedX, float speedY):
+ mImage(img), mParallax(parallax),
+ mPosX(0), mPosY(0),
+ mSpeedX(speedX), mSpeedY(speedY)
+{
+ mImage->incRef();
+}
+
+AmbientOverlay::~AmbientOverlay()
+{
+ mImage->decRef();
+}
+
+void AmbientOverlay::update(int timePassed, float dx, float dy)
+{
+ // Self scrolling of the overlay
+ mPosX -= mSpeedX * timePassed / 10;
+ mPosY -= mSpeedY * timePassed / 10;
+
+ // Parallax scrolling
+ mPosX += dx * mParallax;
+ mPosY += dy * mParallax;
+
+ int imgW = mImage->getWidth();
+ int imgH = mImage->getHeight();
+
+ // Wrap values
+ while (mPosX > imgW)
+ mPosX -= imgW;
+ while (mPosX < 0)
+ mPosX += imgW;
+
+ while (mPosY > imgH)
+ mPosY -= imgH;
+ while (mPosY < 0)
+ mPosY += imgH;
+}
+
+void AmbientOverlay::draw(Graphics *graphics, int x, int y)
+{
+ graphics->drawImagePattern(mImage,
+ (int) -mPosX, (int) -mPosY, x + (int) mPosX, y + (int) mPosY);
+}
diff --git a/src/resources/ambientoverlay.h b/src/resources/ambientoverlay.h
new file mode 100644
index 00000000..a939cbb4
--- /dev/null
+++ b/src/resources/ambientoverlay.h
@@ -0,0 +1,57 @@
+/*
+ * The Mana World
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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 2 of the License, or
+ * any later version.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+#ifndef _TMW_RESOURCES_AMBIENTOVERLAY_H_
+#define _TMW_RESOURCES_AMBIENTOVERLAY_H_
+
+class Graphics;
+class Image;
+
+class AmbientOverlay
+{
+ public:
+ /**
+ * Constructor.
+ */
+ AmbientOverlay(Image *img, float parallax,
+ float speedX, float speedY);
+
+ /**
+ * Destructor.
+ */
+ ~AmbientOverlay();
+
+ void update(int timePassed, float dx, float dy);
+
+ void draw(Graphics *graphics, int x, int y);
+
+ private:
+ Image *mImage;
+ float mParallax;
+ float mPosX; /**< Current layer X position. */
+ float mPosY; /**< Current layer Y position. */
+ float mSpeedX; /**< Scroll speed in X direction. */
+ float mSpeedY; /**< Scroll speed in Y direction. */
+};
+
+#endif
diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp
index c1ae911c..2aea3dc5 100644
--- a/src/resources/mapreader.cpp
+++ b/src/resources/mapreader.cpp
@@ -24,6 +24,7 @@
#include "mapreader.h"
#include <cassert>
+#include <iostream>
#include <zlib.h>
#include "resourcemanager.h"
@@ -51,7 +52,7 @@ inflateMemory(unsigned char *in, unsigned int inLength,
int ret;
z_stream strm;
- out = (unsigned char*)malloc(bufferSize);
+ out = (unsigned char*) malloc(bufferSize);
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
@@ -82,13 +83,13 @@ inflateMemory(unsigned char *in, unsigned int inLength,
ret = Z_DATA_ERROR;
case Z_DATA_ERROR:
case Z_MEM_ERROR:
- (void)inflateEnd(&strm);
+ (void) inflateEnd(&strm);
return ret;
}
if (ret != Z_STREAM_END)
{
- out = (unsigned char*)realloc(out, bufferSize * 2);
+ out = (unsigned char*) realloc(out, bufferSize * 2);
if (out == NULL)
{
@@ -105,10 +106,44 @@ inflateMemory(unsigned char *in, unsigned int inLength,
assert(strm.avail_in == 0);
outLength = bufferSize - strm.avail_out;
- (void)inflateEnd(&strm);
+ (void) inflateEnd(&strm);
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
}
+int
+inflateMemory(unsigned char *in, unsigned int inLength,
+ unsigned char *&out)
+{
+ unsigned int outLength = 0;
+ int ret = inflateMemory(in, inLength, out, outLength);
+
+ if (ret != Z_OK || out == NULL)
+ {
+ if (ret == Z_MEM_ERROR)
+ {
+ logger->log("Error: Out of memory while decompressing map data!");
+ }
+ else if (ret == Z_VERSION_ERROR)
+ {
+ logger->log("Error: Incompatible zlib version!");
+ }
+ else if (ret == Z_DATA_ERROR)
+ {
+ logger->log("Error: Incorrect zlib compressed data!");
+ }
+ else
+ {
+ logger->log("Error: Unknown error while decompressing map data!");
+ }
+
+ free(out);
+ out = NULL;
+ outLength = 0;
+ }
+
+ return outLength;
+}
+
Map*
MapReader::readMap(const std::string &filename)
{
@@ -126,33 +161,17 @@ MapReader::readMap(const std::string &filename)
// Inflate the gzipped map data
unsigned char *inflated;
- unsigned int inflatedSize = 0;
- int ret = inflateMemory((unsigned char*)buffer,
- fileSize, inflated, inflatedSize);
+ unsigned int inflatedSize = inflateMemory((unsigned char*) buffer,
+ fileSize, inflated);
free(buffer);
- if (ret == Z_MEM_ERROR)
+ if (inflated == NULL)
{
- logger->log("Error: Out of memory while decompressing map data!");
- return NULL;
- }
- else if (ret == Z_VERSION_ERROR)
- {
- logger->log("Error: Incompatible zlib version!");
- return NULL;
- }
- else if (ret == Z_DATA_ERROR)
- {
- logger->log("Error: Incorrect zlib compressed data!");
- return NULL;
- }
- else if (ret != Z_OK || inflated == NULL)
- {
- logger->log("Error: Unknown error while decompressing map data!");
+ logger->log("Could not decompress map file (%s)\n", filename.c_str());
return NULL;
}
- xmlDocPtr doc = xmlParseMemory((char*)inflated, inflatedSize);
+ xmlDocPtr doc = xmlParseMemory((char*) inflated, inflatedSize);
free(inflated);
// Parse the inflated map data
@@ -214,31 +233,7 @@ MapReader::readMap(xmlNodePtr node, const std::string &path)
}
}
- //set Overlays
- int i = 0;
- ResourceManager *resman = ResourceManager::getInstance();
-
- while (map->hasProperty("overlay" + toString(i) + "image"))
- {
- Image *overlayImage = resman->getImage(map->getProperty("overlay" + toString(i) + "image"));
- float scrollX = 0.0f;
- float scrollY = 0.0f;
- float parallax = 0.0f;
- if (map->hasProperty("overlay" + toString(i) + "scrollX"))
- {
- scrollX = atof(map->getProperty("overlay" + toString(i) + "scrollX").c_str());
- }
- if (map->hasProperty("overlay" + toString(i) + "scrollY"))
- {
- scrollY = atof(map->getProperty("overlay" + toString(i) + "scrollY").c_str());
- }
- if (map->hasProperty("overlay" + toString(i) + "parallax"))
- {
- parallax = atof(map->getProperty("overlay" + toString(i) + "parallax").c_str());
- }
- map->setOverlay (overlayImage, scrollX, scrollY, parallax);
- i++;
- }
+ map->initializeOverlays();
return map;
}
@@ -284,8 +279,8 @@ MapReader::readLayer(xmlNodePtr node, Map *map, int layer)
{
xmlFree(encoding);
- if (compression) {
- logger->log("Warning: no layer compression supported!");
+ if (compression && !xmlStrEqual(compression, BAD_CAST "gzip")) {
+ logger->log("Warning: only gzip layer compression supported!");
xmlFree(compression);
return;
}
@@ -313,12 +308,32 @@ MapReader::readLayer(xmlNodePtr node, Map *map, int layer)
int binLen;
unsigned char *binData =
- php_base64_decode(charData, strlen((char*)charData),
- &binLen);
+ php_base64_decode(charData, strlen((char*)charData), &binLen);
delete[] charData;
if (binData) {
+ if (compression) {
+ if (xmlStrEqual(compression, BAD_CAST "gzip")) {
+ // Inflate the gzipped layer data
+ unsigned char *inflated;
+ unsigned int inflatedSize =
+ inflateMemory(binData, binLen, inflated);
+
+ free(binData);
+ binData = inflated;
+ binLen = inflatedSize;
+
+ if (inflated == NULL)
+ {
+ logger->log("Error: Could not decompress layer!");
+ xmlFree(compression);
+ return;
+ }
+ }
+ xmlFree(compression);
+ }
+
for (int i = 0; i < binLen - 3; i += 4) {
int gid = binData[i] |
binData[i + 1] << 8 |
@@ -351,6 +366,11 @@ MapReader::readLayer(xmlNodePtr node, Map *map, int layer)
}
}
+ if (y < h)
+ std::cerr << "TOO SMALL!\n";
+ if (x)
+ std::cerr << "TOO SMALL!\n";
+
// There can be only one data element
break;
}