[Moeng-cvs] BBRpg/src/shared tiled_map.cpp, 1.10, 1.11 tiled_map.h, 1.7, 1.8
Status: Alpha
Brought to you by:
b_lindeijer
From: Bjørn L. <b_l...@us...> - 2007-02-08 17:52:03
|
Update of /cvsroot/moeng/BBRpg/src/shared In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv24535/shared Modified Files: tiled_map.cpp tiled_map.h Log Message: Added some sanity checks to prevent crashing and adapted three for loops to Lua 5.1. Probably a lot more will need fixing though. Index: tiled_map.cpp =================================================================== RCS file: /cvsroot/moeng/BBRpg/src/shared/tiled_map.cpp,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** tiled_map.cpp 23 Oct 2004 11:54:19 -0000 1.10 --- tiled_map.cpp 8 Feb 2007 17:51:50 -0000 1.11 *************** *** 21,25 **** #include <libxml/xmlreader.h> ! // Vector class ============================================================== --- 21,28 ---- #include <libxml/xmlreader.h> ! inline int clamp(int a, int min, int max) ! { ! return (a < min) ? min : ((a > max) ? max : a); ! } // Vector class ============================================================== *************** *** 70,74 **** return result; } ! Vector Vector::operator-(const Vector &v) { --- 73,77 ---- return result; } ! Vector Vector::operator-(const Vector &v) { *************** *** 149,156 **** // Tile class ================================================================ ! Tile::Tile() { - tileType = NULL; - obstacle = 0; } --- 152,159 ---- // Tile class ================================================================ ! Tile::Tile(): ! tileType(NULL), ! obstacle(0) { } *************** *** 169,178 **** { xmlTextWriterStartElement(writer, BAD_CAST "tile"); ! if (tileType) { xmlTextWriterWriteAttribute(writer, BAD_CAST "name", BAD_CAST tileType->getName()); } ! if (obstacle) { char obs[5]; --- 172,181 ---- { xmlTextWriterStartElement(writer, BAD_CAST "tile"); ! if (tileType) { xmlTextWriterWriteAttribute(writer, BAD_CAST "name", BAD_CAST tileType->getName()); } ! if (obstacle) { char obs[5]; *************** *** 185,189 **** BAD_CAST "obstacle", BAD_CAST obs); } ! xmlTextWriterEndElement(writer); } --- 188,192 ---- BAD_CAST "obstacle", BAD_CAST obs); } ! xmlTextWriterEndElement(writer); } *************** *** 404,408 **** map<const char*, TileType*, ltstr>::iterator found = tileTypes.find(tileName); ! if (found != tileTypes.end()) { return (*found).second; --- 407,411 ---- map<const char*, TileType*, ltstr>::iterator found = tileTypes.find(tileName); ! if (found != tileTypes.end()) { return (*found).second; *************** *** 535,539 **** { char strbuf[16]; ! xmlTextWriterStartElement(writer, BAD_CAST "layer"); --- 538,542 ---- { char strbuf[16]; ! xmlTextWriterStartElement(writer, BAD_CAST "layer"); *************** *** 569,573 **** { xmlChar *prop; ! // Load the map header prop = xmlGetProp(cur, BAD_CAST "width"); --- 572,576 ---- { xmlChar *prop; ! // Load the map header prop = xmlGetProp(cur, BAD_CAST "width"); *************** *** 577,581 **** int h = atoi((char*)prop); xmlFree(prop); ! resizeTo(w, h); --- 580,584 ---- int h = atoi((char*)prop); xmlFree(prop); ! resizeTo(w, h); *************** *** 583,587 **** int x = 0; int y = 0; ! // Load the tile data while (cur != NULL) { --- 586,590 ---- int x = 0; int y = 0; ! // Load the tile data while (cur != NULL) { *************** *** 590,599 **** x++; if (x == width) {x = 0; y++;} ! } cur = cur->next; } } ! Tile *TiledMapLayer::getTile(Point tile) { if (tile.x < 0 || tile.x >= width || --- 593,602 ---- x++; if (x == width) {x = 0; y++;} ! } cur = cur->next; } } ! Tile *TiledMapLayer::getTile(const Point &tile) { if (tile.x < 0 || tile.x >= width || *************** *** 638,643 **** if (modify) { Point mapSize = getMapSize(); ! cam.x = MAX(0, MIN(mapSize.x - rect.w, cam.x)); ! cam.y = MAX(0, MIN(mapSize.y - rect.h, cam.y)); } --- 641,646 ---- if (modify) { Point mapSize = getMapSize(); ! cam.x = clamp(cam.x, 0, mapSize.x - rect.w); ! cam.y = clamp(cam.y, 0, mapSize.y - rect.h); } *************** *** 679,683 **** pack_fputs((*i)->className, file); pack_fputs("\n", file); ! } // Extra newline fixes last tile not loaded. --- 682,686 ---- pack_fputs((*i)->className, file); pack_fputs("\n", file); ! } // Extra newline fixes last tile not loaded. *************** *** 701,711 **** for (i = objects.begin(); i != objects.end(); i++) { xmlTextWriterStartElement(writer, BAD_CAST "object"); ! snprintf(strbuf, 16, "%d", int(TILES_W * (*i)->x)); xmlTextWriterWriteAttribute(writer, BAD_CAST "x", BAD_CAST strbuf); ! snprintf(strbuf, 16, "%d", int(TILES_H * (*i)->y)); xmlTextWriterWriteAttribute(writer, BAD_CAST "y", BAD_CAST strbuf); ! snprintf(strbuf, 64, "%d", int(TILES_W * (*i)->x)); xmlTextWriterWriteAttribute(writer, BAD_CAST "type", --- 704,714 ---- for (i = objects.begin(); i != objects.end(); i++) { xmlTextWriterStartElement(writer, BAD_CAST "object"); ! snprintf(strbuf, 16, "%d", int(TILES_W * (*i)->x)); xmlTextWriterWriteAttribute(writer, BAD_CAST "x", BAD_CAST strbuf); ! snprintf(strbuf, 16, "%d", int(TILES_H * (*i)->y)); xmlTextWriterWriteAttribute(writer, BAD_CAST "y", BAD_CAST strbuf); ! snprintf(strbuf, 64, "%d", int(TILES_W * (*i)->x)); xmlTextWriterWriteAttribute(writer, BAD_CAST "type", *************** *** 713,717 **** xmlTextWriterEndElement(writer); ! } xmlTextWriterEndElement(writer); --- 716,720 ---- xmlTextWriterEndElement(writer); ! } xmlTextWriterEndElement(writer); *************** *** 741,748 **** fclose(f); ! xmlDocPtr doc = xmlReadMemory(map_string, size, NULL, NULL, 0); delete[] map_string; ! if (doc) { console.log(CON_LOG, CON_VDEBUG, "- Looking for root node"); --- 744,751 ---- fclose(f); ! xmlDocPtr doc = xmlReadMemory(map_string, size, NULL, NULL, 0); delete[] map_string; ! if (doc) { console.log(CON_LOG, CON_VDEBUG, "- Looking for root node"); *************** *** 754,758 **** return 1; } ! console.log(CON_LOG, CON_VDEBUG, "- Loading map from XML tree"); loadFrom(cur, tileRepository); --- 757,761 ---- return 1; } ! console.log(CON_LOG, CON_VDEBUG, "- Loading map from XML tree"); loadFrom(cur, tileRepository); *************** *** 768,777 **** return 1; } ! console.log(CON_LOG, CON_VDEBUG, "- Loading map from packfile"); this->loadFrom(file, tileRepository); pack_fclose(file); } ! return 0; } --- 771,780 ---- return 1; } ! console.log(CON_LOG, CON_VDEBUG, "- Loading map from packfile"); this->loadFrom(file, tileRepository); pack_fclose(file); } ! return 0; } *************** *** 809,813 **** double(y) / TILES_H, className); ! delete[] className; } --- 812,816 ---- double(y) / TILES_H, className); ! delete[] className; } *************** *** 822,826 **** int layerNr = 0; xmlChar *prop; ! removeObjects(); --- 825,829 ---- int layerNr = 0; xmlChar *prop; ! removeObjects(); *************** *** 829,833 **** cur = cur->xmlChildrenNode; ! while (cur != NULL) { if (xmlStrEqual(cur->name, BAD_CAST "layer")) { --- 832,836 ---- cur = cur->xmlChildrenNode; ! while (cur != NULL) { if (xmlStrEqual(cur->name, BAD_CAST "layer")) { *************** *** 846,853 **** int y = atoi((char*)prop); xmlFree(prop); ! // Spawn the object prop = xmlGetProp(cur, BAD_CAST "type"); ! console.log(CON_LOG, CON_VDEBUG, "- Adding %s at (%d, %d)", (char*)prop, x, y); --- 849,856 ---- int y = atoi((char*)prop); xmlFree(prop); ! // Spawn the object prop = xmlGetProp(cur, BAD_CAST "type"); ! console.log(CON_LOG, CON_VDEBUG, "- Adding %s at (%d, %d)", (char*)prop, x, y); *************** *** 1109,1119 **** cameraScreenRect.y + cameraScreenRect.h - 1)); ! start.x = MAX(0, MIN(width - 1, start.x)); ! start.y = MAX(0, MIN(height - 1, start.y)); ! for (int y = start.y; y <= end.y; y++) { ! for (int x = start.x; x <= end.x; x++) { tempTile = layer->getTile(Point(x, y)); tempTileType = tempTile->getType(); if (tempTileType) { --- 1112,1125 ---- cameraScreenRect.y + cameraScreenRect.h - 1)); ! start.x = clamp(start.x, 0, width - 1); ! start.y = clamp(start.y, 0, height - 1); ! for (int y = start.y; y <= end.y; y++) ! { ! for (int x = start.x; x <= end.x; x++) ! { tempTile = layer->getTile(Point(x, y)); + if (!tempTile) continue; tempTileType = tempTile->getType(); if (tempTileType) { *************** *** 1168,1172 **** line(dest, tx + 2, ty + th - 3, tx + tw - 3, ty + th - 3, makecol(255,0,0)); ! line(dest, tx + 3, ty + th - 2, tx + tw - 2, ty + th - 2, makecol(0,0,0)); } --- 1174,1178 ---- line(dest, tx + 2, ty + th - 3, tx + tw - 3, ty + th - 3, makecol(255,0,0)); ! line(dest, tx + 3, ty + th - 2, tx + tw - 2, ty + th - 2, makecol(0,0,0)); } *************** *** 1199,1207 **** Point SquareMap::mapToTile(Point mapCoords) { ! return Point( ! MIN(width - 1, MAX(0, mapCoords.x / tileWidth)), ! MIN(height - 1, MAX(0, mapCoords.y / tileHeight)), ! mapCoords.z ! ); } --- 1205,1211 ---- Point SquareMap::mapToTile(Point mapCoords) { ! return Point(clamp(mapCoords.x / tileWidth, 0, width - 1), ! clamp(mapCoords.y / tileHeight, 0, height - 1), ! mapCoords.z); } Index: tiled_map.h =================================================================== RCS file: /cvsroot/moeng/BBRpg/src/shared/tiled_map.h,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** tiled_map.h 23 Oct 2004 11:54:19 -0000 1.7 --- tiled_map.h 8 Feb 2007 17:51:50 -0000 1.8 *************** *** 183,187 **** float getOpacity(); ! Tile* getTile(Point tileCoords); private: --- 183,187 ---- float getOpacity(); ! Tile* getTile(const Point &tileCoords); private: |