void TileLayer::draw(sf::RenderWindow* window, Camera* camera) { if (_map) { Point camera_pos = camera->position(); TileHelper tile_helper(_tile_width, _tile_height); Rectangle view = camera->get_view_rect(); Point draw_start = tile_helper.toTileCoords(view.left() - 32, view.top() - 32); Point draw_end = tile_helper.toTileCoords(view.right() + 32, view.bottom() + 32); // Some basic attempts at tile clipping draw_start.x = draw_start.x < 0 ? 0 : draw_start.x; draw_start.y = draw_start.y < 0 ? 0 : draw_start.y; draw_start.x = draw_start.x > _map_width ? _map_width : draw_start.x; draw_start.y = draw_start.y > _map_height ? _map_height : draw_start.y; draw_end.x = draw_end.x > _map_width ? _map_width : draw_end.x; draw_end.y = draw_end.y > _map_height ? _map_height : draw_end.y; draw_end.x = draw_end.x < 0 ? 0 : draw_end.x; draw_end.y = draw_end.y < 0 ? 0 : draw_end.y; for (I32 i = draw_start.y - 1; i < draw_end.y + 1; ++i) { if (i < 0 || i == _map_height) continue; for (I32 j = draw_start.x - 1; j < draw_end.x + 1; ++j) { if (j < 0 || j == _map_width) continue; _map[i][j]->draw(window, j * _tile_width - camera_pos.x, i * _tile_height - camera_pos.y); } } } }
/* Grabs the names and loads the tile sets for the level spider must be able to get to the "tileset" tags in one edge stores them in the level::tileset.vector */ void level::create_tilesets( pugi::xml_node spider ) { //temporary container for tileset image pointer SDL_Surface* temp; int first, Twidth ,id , last; //holds path to source char str[256]; pugi::xml_node name; //traverses the Tileset tags in the TMX file for( spider = spider.child("tileset"); spider != 0; spider = spider.next_sibling("tileset") ) { //spider is in the <tileset> tag //sets first so we can know which prototile we are setting for each tileset first = spider.attribute("firstgid").as_int(); Twidth = spider.attribute("tilewidth").as_int(); //std::cout << "present tileset first Gid " << temp.first << std::endl; name = spider.child("image"); //makes it so we can open the TMX files strcpy(str, "assets/"); strcat(str , (char*)name.attribute("source").value() ); if( loadMedia( str , temp , engine->get_screen()) ) { //add to vector of tilesets tilesets.push_back(temp); //tiles in this tileset last = name.attribute("width").as_int()/Twidth ; prototile.resize ( prototile.size() + last); std::cout <<"prototile is "<< prototile.size() << " tiles big.\n"; //set up our prototiles with some basic attributes for(int i = first; i < prototile.size(); i++) { prototile[i] = tile_helper( temp, 16, i-first); } //name.attribute("width").as_int()/Twidth ; //runs through the tile properties and creates prototile vector for( name = name.next_sibling("tile"); name != 0; name = name.next_sibling("tile") ); {//in tag <tile> if exists id = name.attribute("id").as_int(); //std::cout << "tile id:" << id << " has colision attribute : " << name.child("properties").child_value("property") << std::endl; //prototile[first+id].solid = name.child("properties").child("property").attribute("value").as_int(); } } else { SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Tileset creation error", "Now you f****d up This was ment to happen\n go change teh false thing", NULL); exit( 1 ); } } }