void Terrain::swapImg(sf::Image &img) { for(unsigned int i=0;i<img.GetWidth();i++) { for(unsigned int j=0;j<img.GetHeight()/2;j++) { sf::Color tmp = img.GetPixel(i,img.GetHeight()-1-j); img.SetPixel(i,img.GetHeight()-1-j,img.GetPixel(i,j)); img.SetPixel(i,j,tmp); } } }
sf::Color Terrain::getAverageTexColor(sf::Image &img) { unsigned long r=0; unsigned long g=0; unsigned long b=0; for(unsigned int i=0;i<img.GetHeight();i++) { for(unsigned int j=0;j<img.GetWidth();j++) { r+=long(img.GetPixel(j,i).r); g+=long(img.GetPixel(j,i).g); b+=long(img.GetPixel(j,i).b); } } r=r/(img.GetHeight()*img.GetWidth()); g=g/(img.GetHeight()*img.GetWidth()); b=b/(img.GetHeight()*img.GetWidth()); return sf::Color(r,g,b,0); }
Level::Level(const sf::Image &lvl, const sf::Texture &tileTex) { int imgWd = lvl.GetWidth(); int imgHt = lvl.GetHeight(); bool isWall[imgWd][imgHt]; for (int y=0; y<imgHt; ++y) for (int x=0; x<imgWd; ++x) isWall[x][y] = false; for (int y=0; y<imgHt; ++y) { for (int x=0; x<imgWd; ++x) { // search for new platfomrs if (!isWall[x][y] && lvl.GetPixel(x, y) == sf::Color::Black) { // figure out the collision rectangles int minHt = imgHt+1; int wd; // find the maximum rectangle width for (wd=0; x+wd<imgWd && lvl.GetPixel(x+wd, y) == sf::Color::Black; ++wd){ // find the minimum rectangle height int ht=0; while (y+ht<imgHt && lvl.GetPixel(x+wd, y+ht) == sf::Color::Black) ++ht; if (ht < minHt) minHt = ht; } // mark the platfom area as processed for (int x2=x; x2<x+wd; ++x2) { for (int ht=0; ht<minHt; ++ht) { isWall[x2][y+ht] = true; } } // construct platforms platforms.push_back(Platform(x*TILE_SIZE, y*TILE_SIZE, wd*TILE_SIZE, minHt*TILE_SIZE, tileTex)); } } } }