static int lua_TileSet_setOpacity(lua_State* state) { // Get the number of parameters. int paramCount = lua_gettop(state); // Attempt to match the parameters to a valid binding. switch (paramCount) { case 2: { if ((lua_type(state, 1) == LUA_TUSERDATA) && lua_type(state, 2) == LUA_TNUMBER) { // Get parameter 1 off the stack. float param1 = (float)luaL_checknumber(state, 2); TileSet* instance = getInstance(state); instance->setOpacity(param1); return 0; } lua_pushstring(state, "lua_TileSet_setOpacity - Failed to match the given parameters to a valid function signature."); lua_error(state); break; } default: { lua_pushstring(state, "Invalid number of parameters (expected 2)."); lua_error(state); break; } } return 0; }
void j1Map::Draw() { if(map_loaded == false) return; p2List_item<MapLayer*>* item = data.layers.start; for(; item != NULL; item = item->next) { MapLayer* layer = item->data; if(layer->properties.Get("Nodraw") != 0) continue; for(int y = 0; y < data.height; ++y) { for(int x = 0; x < data.width; ++x) { int tile_id = layer->Get(x, y); if(tile_id > 0) { TileSet* tileset = GetTilesetFromTileId(tile_id); SDL_Rect r = tileset->GetTileRect(tile_id); iPoint pos = MapToWorld(x, y); App->render->Blit(tileset->texture, pos.x, pos.y, &r); } } } } }
std::vector<Polygon2d> GenerateHitboxes(TileSet &tileSet, TileMap &tileMap) { std::vector<Polygon2d> hitboxes; const int tileWidth = tileSet.tileSize.x; const int tileHeight = tileSet.tileSize.y; if(tileSet.IsDirty()) return hitboxes; for(int layer = 0; layer < 3; layer++) { for(int col = 0; col < tileMap.GetColumnsCount(); col++) { for(int row = 0; row < tileMap.GetRowsCount(); row++) { //Note : a hitbox is also added for empty/non-collidable tiles to ease the hitbox update when changing a tile Polygon2d newPolygon; if(tileMap.GetTile(layer, col, row) != -1 && tileSet.GetTileHitbox(tileMap.GetTile(layer, col, row)).collidable) { newPolygon = tileSet.GetTileHitbox(tileMap.GetTile(layer, col, row)).hitbox; } newPolygon.Move(col * tileWidth, row * tileHeight); hitboxes.push_back(newPolygon); } } } return hitboxes; }
void j1Map::Draw() { if(map_loaded == false) return; //STL CHANGE list<MapLayer*>::iterator item = data.layers.begin(); for(; item != data.layers.end(); ++item) { MapLayer* layer = *item; if(layer->properties.Get("Nodraw") != 0) continue; for(int y = 0; y < data.height; ++y) { for(int x = 0; x < data.width; ++x) { int tile_id = layer->Get(x, y); if(tile_id > 0) { TileSet* tileset = GetTilesetFromTileId(tile_id); SDL_Rect r = tileset->GetTileRect(tile_id); iPoint pos = MapToWorld(x, y); App->render->Blit(tileset->texture, pos.x, pos.y, &r); } } } } }
void j1Map::Draw() { if(map_loaded == false) return; // TODO 5: Prepare the loop to draw all tilesets + Blit MapLayer* layer = data.layers.start->data; for(int y = 0; y < data.height; ++y) { for(int x = 0; x < data.width; ++x) { int tile_id = layer->Get(x, y); if(tile_id > 0) { // TODO 10(old): Complete the draw function TileSet* tileset = data.tilesets.start->data; SDL_Rect r = tileset->GetTileRect(tile_id); iPoint pos = MapToWorld(x, y); App->render->Blit(tileset->texture, pos.x, pos.y, &r); } } } }
static int lua_TileSet_getWidth(lua_State* state) { // Get the number of parameters. int paramCount = lua_gettop(state); // Attempt to match the parameters to a valid binding. switch (paramCount) { case 1: { if ((lua_type(state, 1) == LUA_TUSERDATA)) { TileSet* instance = getInstance(state); float result = instance->getWidth(); // Push the return value onto the stack. lua_pushnumber(state, result); return 1; } lua_pushstring(state, "lua_TileSet_getWidth - Failed to match the given parameters to a valid function signature."); lua_error(state); break; } default: { lua_pushstring(state, "Invalid number of parameters (expected 1)."); lua_error(state); break; } } return 0; }
static int lua_TileSet_addRef(lua_State* state) { // Get the number of parameters. int paramCount = lua_gettop(state); // Attempt to match the parameters to a valid binding. switch (paramCount) { case 1: { if ((lua_type(state, 1) == LUA_TUSERDATA)) { TileSet* instance = getInstance(state); instance->addRef(); return 0; } lua_pushstring(state, "lua_TileSet_addRef - Failed to match the given parameters to a valid function signature."); lua_error(state); break; } default: { lua_pushstring(state, "Invalid number of parameters (expected 1)."); lua_error(state); break; } } return 0; }
MovementMap::MovementMap(std::string file, const TileSet& tileSet) { tileWidth = tileSet.GetTileWidth(); tileHeight = tileSet.GetTileHeight(); currentLayer = 0; BuildMovementMap(file); }
static int lua_TileSet_draw(lua_State* state) { // Get the number of parameters. int paramCount = lua_gettop(state); // Attempt to match the parameters to a valid binding. switch (paramCount) { case 1: { if ((lua_type(state, 1) == LUA_TUSERDATA)) { TileSet* instance = getInstance(state); unsigned int result = instance->draw(); // Push the return value onto the stack. lua_pushunsigned(state, result); return 1; } lua_pushstring(state, "lua_TileSet_draw - Failed to match the given parameters to a valid function signature."); lua_error(state); break; } case 2: { if ((lua_type(state, 1) == LUA_TUSERDATA) && lua_type(state, 2) == LUA_TBOOLEAN) { // Get parameter 1 off the stack. bool param1 = gameplay::ScriptUtil::luaCheckBool(state, 2); TileSet* instance = getInstance(state); unsigned int result = instance->draw(param1); // Push the return value onto the stack. lua_pushunsigned(state, result); return 1; } lua_pushstring(state, "lua_TileSet_draw - Failed to match the given parameters to a valid function signature."); lua_error(state); break; } default: { lua_pushstring(state, "Invalid number of parameters (expected 1 or 2)."); lua_error(state); break; } } return 0; }
sf::VertexArray GenerateVertexArray(TileSet &tileSet, TileMap &tileMap) { sf::VertexArray vertexArray(sf::Quads); int tileWidth = tileSet.tileSize.x; int tileHeight = tileSet.tileSize.y; if(tileSet.IsDirty()) return vertexArray; for(int layer = 0; layer < 3; layer++) { for(int col = 0; col < tileMap.GetColumnsCount(); col++) { for(int row = 0; row < tileMap.GetRowsCount(); row++) { TileTextureCoords coords; if(tileMap.GetTile(layer, col, row) != -1) { coords = tileSet.GetTileTextureCoords(tileMap.GetTile(layer, col, row)); } else { coords = tileSet.GetTileTextureCoords(0); } { sf::Vertex vertex(sf::Vector2f(col * tileWidth, row * tileHeight), coords.topLeft); if(tileMap.GetTile(layer, col, row) == -1) vertex.color.a = 0; vertexArray.append(vertex); } { sf::Vertex vertex(sf::Vector2f(col * tileWidth, (row + 1) * tileHeight), coords.bottomLeft); if(tileMap.GetTile(layer, col, row) == -1) vertex.color.a = 0; vertexArray.append(vertex); } { sf::Vertex vertex(sf::Vector2f((col + 1) * tileWidth, (row + 1) * tileHeight), coords.bottomRight); if(tileMap.GetTile(layer, col, row) == -1) vertex.color.a = 0; vertexArray.append(vertex); } { sf::Vertex vertex(sf::Vector2f((col + 1) * tileWidth, row * tileHeight), coords.topRight); if(tileMap.GetTile(layer, col, row) == -1) vertex.color.a = 0; vertexArray.append(vertex); } } } } return vertexArray; }
int TileSetManager::getLocalGid(int gid) const{ int size = (int)m_TileSet.size(); TileSet* set = 0; for(int i = 1; i < size; i++){ if(m_TileSet[i]->getFirstGit() > gid){ set = m_TileSet[i-1]; break; } } if(!set) set = m_TileSet[size-1]; return gid - set->getFirstGit(); }
void TileSetManager::drawTile(GMSpriteBatch *s, const GMRect2D &dest, double radian, int gid) const{ int size = (int)m_TileSet.size(); TileSet* set = 0; for(int i = 1; i < size; i++){ if(m_TileSet[i]->getFirstGit() > gid){ set = m_TileSet[i-1]; break; } } if(!set) set = m_TileSet[size-1]; set->getImage()->draw(s, dest, radian, gid); }
void j1Map::Draw() { if(map_loaded == false) return; //STL CHANGE //NOTE: well //Camera Culling //---------------------- SDL_Rect cam = App->render->camera; //---------------------- list<MapLayer*>::iterator item = data.layers.begin(); for(; item != data.layers.end(); ++item) { MapLayer* layer = *item; //NOTE: when drawing navigation map, framerate drops to the half if (!App->debug) if(layer->properties.Get("Nodraw") != 0) continue; for(int y = 0; y < data.height; ++y) { for(int x = 0; x < data.width; ++x) { int tile_id = layer->Get(x, y); if(tile_id > 0) { TileSet* tileset = GetTilesetFromTileId(tile_id); SDL_Rect r = tileset->GetTileRect(tile_id); iPoint pos = MapToWorld(x, y); //NOTE: Maybe this has to be implemented on Render.cpp //NOTE: changing the offset of the tiles because Ric cheated with the original, think about make it general for any map //NOTE: because of test sake //---------------------- if (layer->name == "Background") App->render->Blit(tileset->texture, pos.x - data.tile_width / 2 + tileset->offset_x, pos.y, &r); else if (layer->name == "Navigation") App->render->Blit(tileset->texture, pos.x - data.tile_width / 2 , pos.y, &r); //---------------------- } } } } }
TileSet::TileSet(const TileSet &other, int scale) { _tileWidth = scale * other.tileWidth(); _tileHeight = scale * other.tileHeight(); _tileCount = other.tileCount(); _name = other.name(); _images = (TileImage **)malloc(_tileCount * sizeof(TileImage *)); for (int i = 0; i < _tileCount; ++i) { QImage tileImage = other.getTileImage(i)->image().scaled(_tileWidth, _tileHeight); _images[i] = new TileImage(tileImage); } }
//------------------------------------------------------------------------------ TileSet* A10_Game::getTileset(string path) { if(this->tilesets.find(path) == this->tilesets.end()) { TileSet* ts = new TileSet(kernel->graphicsMgr); ts->loadFromFile(path); this->tilesets[path] = ts; return ts; } return this->tilesets[path]; };
int WangTiles::SimpleCompaction(const TileSet & tileSet, vector< vector<Tile> > & result) { const int numTiles = tileSet.NumTiles(); const int numHColors = tileSet.NumHColors(); const int numVColors = tileSet.NumVColors(); const int numTilesPerColor = tileSet.NumTilesPerColor(); // find the best aspect ratio const int maxFactor = floor(sqrt(numTiles)); const int height = numVColors*numVColors; const int width = numHColors*numHColors*numTilesPerColor; { result = vector< vector<Tile> > (height); for(int i = 0; i < result.size(); i++) { result[i] = vector<Tile>(width); } } { int i = 0; int j = 0; for(int e1 = 0; e1 < numVColors; e1++) for(int e3 = 0; e3 < numVColors; e3++) for(int e0 = 0; e0 < numHColors; e0++) for(int e2 = 0; e2 < numHColors; e2++) { const vector<Tile> & tiles = tileSet.Tiles(e0, e1, e2, e3); for(int k = 0; k < tiles.size(); k++) { result[i][j] = tiles[k]; j++; if(j >= result[i].size()) { i++; j = 0; } } } } // done return 1; }
static int lua_TileSet_setTileSource(lua_State* state) { // Get the number of parameters. int paramCount = lua_gettop(state); // Attempt to match the parameters to a valid binding. switch (paramCount) { case 4: { if ((lua_type(state, 1) == LUA_TUSERDATA) && lua_type(state, 2) == LUA_TNUMBER && lua_type(state, 3) == LUA_TNUMBER && (lua_type(state, 4) == LUA_TUSERDATA || lua_type(state, 4) == LUA_TNIL)) { // Get parameter 1 off the stack. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2); // Get parameter 2 off the stack. unsigned int param2 = (unsigned int)luaL_checkunsigned(state, 3); // Get parameter 3 off the stack. bool param3Valid; gameplay::ScriptUtil::LuaArray<Vector2> param3 = gameplay::ScriptUtil::getObjectPointer<Vector2>(4, "Vector2", true, ¶m3Valid); if (!param3Valid) { lua_pushstring(state, "Failed to convert parameter 3 to type 'Vector2'."); lua_error(state); } TileSet* instance = getInstance(state); instance->setTileSource(param1, param2, *param3); return 0; } lua_pushstring(state, "lua_TileSet_setTileSource - Failed to match the given parameters to a valid function signature."); lua_error(state); break; } default: { lua_pushstring(state, "Invalid number of parameters (expected 4)."); lua_error(state); break; } } return 0; }
void Map::ForEachReachableTile( const Unit* unit, ForEachReachableTileCallback callback ) { assertion( unit, "Cannot find reachable tiles for null Unit!" ); assertion( callback.IsValid(), "Cannot call invalid callback on reachable tiles!" ); // Find all reachable tiles for the Unit. TileSet reachableTiles; FindReachableTiles( unit, reachableTiles ); for( auto it = reachableTiles.begin(); it != reachableTiles.end(); ++it ) { // Invoke the callback on all reachable tiles. callback.Invoke( *it, unit ); } }
void EditorState::setTileSetIndex(int tileSetIndex) { Q_ASSERT(tileSetIndex >= 0 && tileSetIndex < _tiles.size()); _tileSetIndex = tileSetIndex; TileSet *tileSet = currentTileSet(); PgmPtr *tiles = (PgmPtr *)malloc(tileSet->tileCount() * sizeof(PgmPtr)); for (int i = 0; i < tileSet->tileCount(); ++i) { TileImage *tile = tileSet->getTileImage(i); PgmMem *tileMem = new PgmMem(tile->image()); tiles[i] = tileMem->dataCopy(); tileMem->release(); } tileMap.tiles = tiles; }
void GameView::drawMap(Surface &window) { TileSet * ts = TileInterface::getTileSet(); unsigned long world_x; unsigned long world_y; unsigned short map_x; unsigned short map_y; WorldViewInterface::getMainCamera()->getViewStart(window.getWidth(), window.getHeight(), &world_x, &world_y); MapInterface::pointXYtoMapXY( world_x, world_y, &map_x, &map_y ); unsigned short tile_size = ts->getTileXsize(); long partial_y = world_y % tile_size; int y = 0; if ( partial_y ) { y -= partial_y; } long partial_x = world_x % tile_size; int start_x = 0; if ( partial_x ) { start_x -= partial_x; } unsigned int tile = 0; WorldMap * map = MapInterface::getMap(); unsigned short tmx; for ( ; y < (int)window.getHeight(); y += tile_size ) { tmx = map_x; for ( int x = start_x; x < (int)window.getWidth(); x += tile_size ) { tile = map->getValue(tmx++, map_y); blitTile(window, tile, x, y); } map_y ++; } }
bool DataLoader::loadTilesetList(FILE *fp) { int num_tilesets; fread(&num_tilesets, 1, sizeof(int), fp); gameData->tileSetList = new std::vector<TileSet*>(); int i; for(i = 0; i < num_tilesets; i++) { TileSet *tileSet = new TileSet(); fread(&(tileSet->id), 1, sizeof(int), fp); tileSet->file = std::string(loadString(fp)); tileSet->name = std::string(loadString(fp)); fread(&(tileSet->width), 1, sizeof(int), fp); fread(&(tileSet->height), 1, sizeof(int), fp); fread(&(tileSet->maxX), 1, sizeof(int), fp); fread(&(tileSet->maxY), 1, sizeof(int), fp); fread(&(tileSet->tileSize), 1, sizeof(int), fp); tileSet->collisionMatrix.resize(boost::extents[tileSet->maxX][tileSet->maxY]); int j, k; for(j = 0; j < tileSet->maxX; j++) { for(k = 0; k < tileSet->maxY; k++) { int valor; fread(&valor, 1, sizeof(int), fp); tileSet->collisionMatrix[j][k] = valor; } } tileSet->reloadTilesetImage(); gameData->tileSetList->push_back(tileSet); } return true; }
void j1Map::Draw() { if(map_loaded == false) return; // TODO 4: Make sure we draw all the layers and not just the first one p2List_item<MapLayer*>* layer = data.layers.start; while (layer) { //This makes appear the Navigation Layer if (layer->data->name == "Meta" && App->input->GetKey(SDL_SCANCODE_I) == KEY_DOWN) { if (layer->data->properties.GetPropertyValue("Draw") == 0) layer->data->properties.SetPropertyValue("Draw", 1); else layer->data->properties.SetPropertyValue("Draw", 0); } //------------------------------------ if (layer->data->properties.GetPropertyValue("Draw")) { for (int y = 0; y < data.height; ++y) { for (int x = 0; x < data.width; ++x) { int tile_id = layer->data->Get(x, y); if (tile_id > 0) { TileSet* tileset = GetTilesetFromTileId(tile_id); if (tileset != NULL) { SDL_Rect r = tileset->GetTileRect(tile_id); iPoint pos = MapToWorld(x, y); App->render->Blit(tileset->texture, pos.x + tileset->offset_x, pos.y + tileset->offset_y, &r); } } } } } layer = layer->next; } }
void CEditor::inc_tilepos() { TileSet* curTileSet = EditorInterface::getTileSet(); switch (current_object) { case 0: // tiles { if( current_tilepos+1 < curTileSet->getAllTilesOfType( TileClassificationType::FLOOR ).size() ) { current_tilepos++; tilepos_offset--; } } break; case 1: // environment { if( current_tilepos+1 < curTileSet->getAllTilesOfType( TileClassificationType::ENVIRONMENT ).size() ) { current_tilepos++; tilepos_offset--; } } break; case 2: // shadows { if( current_tilepos+1 < curTileSet->getAllTilesOfType( TileClassificationType::SHADOW ).size() ) { current_tilepos++; tilepos_offset--; } } break; case 3: // collisionboxes break; case 4: // NPCs if( current_tilepos+1 < editorNPCs.size() ) { current_tilepos++; tilepos_offset--; } break; } }
static int lua_TileSet_getNode(lua_State* state) { // Get the number of parameters. int paramCount = lua_gettop(state); // Attempt to match the parameters to a valid binding. switch (paramCount) { case 1: { if ((lua_type(state, 1) == LUA_TUSERDATA)) { TileSet* instance = getInstance(state); void* returnPtr = ((void*)instance->getNode()); if (returnPtr) { gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject)); object->instance = returnPtr; object->owns = false; luaL_getmetatable(state, "Node"); lua_setmetatable(state, -2); } else { lua_pushnil(state); } return 1; } lua_pushstring(state, "lua_TileSet_getNode - Failed to match the given parameters to a valid function signature."); lua_error(state); break; } default: { lua_pushstring(state, "Invalid number of parameters (expected 1)."); lua_error(state); break; } } return 0; }
static int lua_TileSet_setColor(lua_State* state) { // Get the number of parameters. int paramCount = lua_gettop(state); // Attempt to match the parameters to a valid binding. switch (paramCount) { case 2: { if ((lua_type(state, 1) == LUA_TUSERDATA) && (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TNIL)) { // Get parameter 1 off the stack. bool param1Valid; gameplay::ScriptUtil::LuaArray<Vector4> param1 = gameplay::ScriptUtil::getObjectPointer<Vector4>(2, "Vector4", true, ¶m1Valid); if (!param1Valid) { lua_pushstring(state, "Failed to convert parameter 1 to type 'Vector4'."); lua_error(state); } TileSet* instance = getInstance(state); instance->setColor(*param1); return 0; } lua_pushstring(state, "lua_TileSet_setColor - Failed to match the given parameters to a valid function signature."); lua_error(state); break; } default: { lua_pushstring(state, "Invalid number of parameters (expected 2)."); lua_error(state); break; } } return 0; }
void UpdateHitboxes(std::vector<Polygon2d> &polygons, sf::Vector2f position, int layer, int col, int row, TileSet &tileSet, TileMap &tileMap) { if(tileSet.IsDirty()) return; const int vertexPos = layer * tileMap.GetColumnsCount() * tileMap.GetRowsCount() + col * tileMap.GetRowsCount() + row; const int tileWidth = tileSet.tileSize.x; const int tileHeight = tileSet.tileSize.y; if(tileMap.GetTile(layer, col, row) != -1 && tileSet.GetTileHitbox(tileMap.GetTile(layer, col, row)).collidable) { polygons[vertexPos] = tileSet.GetTileHitbox(tileMap.GetTile(layer, col, row)).hitbox; } else { polygons[vertexPos] = Polygon2d(); } polygons[vertexPos].Move(position.x + col * tileWidth, position.y + row * tileHeight); }
void TileMap::generateTileSets(unsigned int numLevels) { osg::ref_ptr<const Profile> profile = createProfile(); _tileSets.clear(); double width = (_maxX - _minX); // double height = (_maxY - _minY); for (unsigned int i = 0; i < numLevels; ++i) { unsigned int numCols, numRows; profile->getNumTiles(i, numCols, numRows); double res = (width / (double)numCols) / (double)_format.getWidth(); TileSet ts; ts.setUnitsPerPixel(res); ts.setOrder(i); _tileSets.push_back(ts); } }
void TileMapImporter::CheckTilesCount(const TileSet &tileSet) { //Warn the user if there are not same amount of tiles in the current tileset and the imported file //note: if the tileset has been imported too, the number of tiles should already be equal. const Tmx::Tileset *importedTileset = m_map->GetTileset(0); const Tmx::Image *importedImage = importedTileset->GetImage(); const int importedTilesCount = (importedImage->GetWidth() - importedTileset->GetMargin() * 2 + importedTileset->GetSpacing()) / (importedTileset->GetTileWidth() + importedTileset->GetSpacing()) * (importedImage->GetHeight() - importedTileset->GetMargin() * 2 + importedTileset->GetSpacing()) / (importedTileset->GetTileHeight() + importedTileset->GetSpacing()); if(importedTilesCount != tileSet.GetTilesCount()) { gd::LogWarning(_("There are not as many tiles in the object's tileset as in the file. The result may not be correct.")); } }
bool M_PathFinding::LoadMapData() { bool ret = true; LOG("-- Pathfinding: Loading meta data"); C_List_item<MapLayer*>* item; for (item = App->map->data.layers.start; item; item = item->next) { if (item->data->name == "Test") { mapData.height = item->data->height; mapData.width = item->data->width; mapData.data = new uint[mapData.height*mapData.width]; for (int i = 0; i < mapData.width * mapData.height; i++) { int id = item->data->data[i]; TileSet* tileset = App->map->GetTilesetFromTileId(id); Tile* tile = tileset->GetTileFromId(id); if (tile) { if (tile->properties.GetProperty("Walkable") == 1) mapData.data[i] = 1; else mapData.data[i] = 0; } } ret = true; } } if (!ret) LOG("-- Pathfinding: Could not load meta tileset --"); else LOG("-- Pathfinding: Meta tilesed loaded correctly --"); return ret; }
void UpdateVertexArray(sf::VertexArray &vertexArray, int layer, int col, int row, TileSet &tileSet, TileMap &tileMap) { if(tileSet.IsDirty()) return; const int vertexPos = 4 * (layer * tileMap.GetColumnsCount() * tileMap.GetRowsCount() + col * tileMap.GetRowsCount() + row); TileTextureCoords newCoords = tileMap.GetTile(layer, col, row) != -1 ? tileSet.GetTileTextureCoords(tileMap.GetTile(layer, col, row)) : tileSet.GetTileTextureCoords(0); vertexArray[vertexPos].texCoords = newCoords.topLeft; vertexArray[vertexPos + 1].texCoords = newCoords.bottomLeft; vertexArray[vertexPos + 2].texCoords = newCoords.bottomRight; vertexArray[vertexPos + 3].texCoords = newCoords.topRight; for(int i = 0; i < 4; i++) { if(tileMap.GetTile(layer, col, row) == -1) { vertexArray[vertexPos + i].color.a = 0; } else { vertexArray[vertexPos + i].color.a = 255; } } }