int main() { img_r.LoadFromFile("..\\Imagenes\\rcircle.png"); img_b.LoadFromFile("..\\Imagenes\\bcircle.png"); sprite_b.SetImage(img_b); sprite_r.SetImage(img_r); sf::RenderWindow App(sf::VideoMode(800,600,32),"ventana"); const sf::Input& in = App.GetInput(); sprite_b.Scale(sprite_r.GetSize().x/sprite_b.GetSize().x,sprite_r.GetSize().y/sprite_b.GetSize().y); while(App.IsOpened()) { while(App.GetEvent(Event)) { if(Event.Type == sf::Event::Closed) App.Close(); if(Event.Type == sf::Event::MouseMoved){ // cursor.SetPosition(Event.MouseMove.X,Event.MouseMove.Y); } } if(in.IsKeyDown(sf::Key::Escape)) App.Close(); //cursor.SetPosition(in.GetMouseX(),in.GetMouseY()); App.Clear(); sprite_r.SetPosition(0,0); App.Draw(sprite_r); sprite_r.SetPosition(App.GetWidth()-img_r.GetWidth(),0); App.Draw(sprite_r); sprite_r.SetPosition(0,App.GetHeight()-img_r.GetHeight()); App.Draw(sprite_r); sprite_r.SetPosition(App.GetWidth()-img_r.GetWidth(),App.GetHeight()-img_r.GetHeight()); App.Draw(sprite_r); sprite_b.SetPosition((App.GetWidth()-sprite_b.GetSize().x)/2,(App.GetHeight()-sprite_b.GetSize().y)/2); App.Draw(sprite_b); App.Display(); } return 0; }
Bala::Bala(float _v, sf::Sprite& _cannon, sf::Image& imagen) { float cannonA, cannonX, cannonY, cannonH; //Cargo sprite sprite.SetImage(imagen); sprite.SetCenter(imagen.GetWidth()/2, imagen.GetHeight()/2); //Inicializo variables dt = 0.0f; cannonA = _cannon.GetRotation(); cannonX = _cannon.GetPosition().x; cannonY = _cannon.GetPosition().y; //Tomo el tamaño sobre el eje x como el largo del cañon //porque sin rotar la misma se encuentra en horizontal cannonH = _cannon.GetSize().x; //Descompongo la velocidad inicial vx = _v * (cos(cannonA/180*PI)); vy = _v * (sin(cannonA/180*PI)); //Posicion inicial bala según la posición del cañon //usando trigonometria xi = cannonX + ((cos(cannonA/180*PI) * cannonH)); yi = cannonY - ((sin(cannonA/180*PI) * cannonH)); sprite.SetPosition(xi, yi); }
// Constructor ParallaxLayer::ParallaxLayer(const sf::Image &img, float factor_x, bool repeat_x, float offset_x, float factor_y, bool repeat_y, float offset_y){ SetImage(img); // calculamos el ancho y alto de la imagen, esto // nos va a servir para evitar desbordes en el desplazamiento ancho_img=img.GetWidth(); alto_img=img.GetHeight(); // inicializamos las variables internas despl_x=despl_y=0; this->factor_x=factor_x; this->factor_y=factor_y; this->repeat_x=repeat_x; this->repeat_y=repeat_y; this->offset_x=offset_x; this->offset_y=offset_y; // seleccionamos la textura y habilitamos la repeticion // segun corresponda img.Bind(); if(repeat_x) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); if(repeat_y) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // ajustamos la posicion de la capa Sprite::SetPosition(offset_x, offset_y); SetScale(0.5,0.5); }
Obstacle::Obstacle(float x, float y, const sf::Image& image) { mSprite.SetImage(image); mSprite.SetPosition(x, y); mRect = sf::FloatRect(x, y, x+image.GetWidth(), y+image.GetHeight()); mHitPlayer = false; }
void ColorScale::draw(sf::Image& img,const sf::Vector2f& start,const sf::Vector2f& end,GradientStyle::GradientStyle style, int size) const { sf::Color (*pFunction)(sf::Color*,int,const sf::Vector2f&,const sf::Vector2f&,int,int); sf::Color* tab =new sf::Color[size]; fillTab(tab,size); switch (style) { case GradientStyle::Linear : pFunction = GradientLinear; break; case GradientStyle::Circle : pFunction = GradientCircle; break; case GradientStyle::Radial : pFunction = GradientRadial; break; case GradientStyle::Reflex : pFunction = GradientReflex; break; default: pFunction = GradientLinear; break; } for(int i=0;i<img.GetWidth();i++) { for(int j=0;j<img.GetHeight();j++) { img.SetPixel(i,j,pFunction(tab,size,start,end,i,j)); } } delete[] tab; }
void SFMLTexture_c::SetSize(uint16_t w, uint16_t h) { if (texture.GetWidth() != w && texture.GetHeight() != h) { texture.Create(w, h); } }
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); }
//uploads the blendmaps to the GFX, getting a handle to the GFX side tex void Terrain::makeBlendMap(GLuint& handle, sf::Image img) { glDeleteTextures(1,&handle); glGenTextures(1,&handle); glBindTexture(GL_TEXTURE_2D,handle); glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,img.GetWidth(),img.GetHeight(),0,GL_RGBA,GL_UNSIGNED_BYTE,img.GetPixelsPtr()); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); }
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); } } }
Player::Player(sf::Image& image, sf::Image& impulse) { mLife = 1.0f; mEnergyBar = 1.0f; mYDir = 0.0f; mXAdvancement = 0; mDestroyed=false; mSprite.SetImage(image); mSprite.SetPosition(350, 400); mSprite.SetCenter(image.GetWidth()/2, image.GetHeight()/2); mImpulse.SetImage(impulse); mImpulse.SetBlendMode(sf::Blend::Add); }
GLuint Terrain::uploadTextureGFX(sf::Image img) { GLuint handle; glGenTextures(1,&handle); glBindTexture(GL_TEXTURE_2D,handle); glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,img.GetWidth(),img.GetHeight(),0,GL_RGBA,GL_UNSIGNED_BYTE,img.GetPixelsPtr()); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST ); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glGenerateMipmap( GL_TEXTURE_2D); return handle; }
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)); } } } }
void Terrain::replacePartTexture(GLint xoffset, GLint yoffset, sf::Image img,GLuint handle) { glBindTexture(GL_TEXTURE_2D,handle); glTexSubImage2D(GL_TEXTURE_2D,0,xoffset,yoffset,img.GetWidth(),img.GetHeight(),GL_RGBA,GL_UNSIGNED_BYTE,img.GetPixelsPtr()); }
// Returns the image size as a float vector sf::Vector2f ImageSize(const sf::Image& image) { return sf::Vector2f(static_cast<float>(image.GetWidth()), static_cast<float>(image.GetHeight())); }
void setTileQueueBtns( cp::cpImageButton *b1, cp::cpImageButton *b2, cp::cpImageButton *b3, cp::cpImageButton *b4, cp::cpImageButton *b5, sf::Image placeHolder) { if( tileQueue.size() > 0 ) { int btnIndex = 0; //create a temp tile to make code look cleaner. Tile *tmptile = &tileQueue.at(tileQueueIndex + btnIndex); b1->SetImage( &chipsetImages.at( getChipsetIndex(tmptile->chipSetName))); //set the buttons sprite subrec. sf::IntRect subrec(tmptile->srcX, tmptile->srcY, tmptile->srcX + tmptile->width, tmptile->srcY + tmptile->height); b1->SetSubRect(subrec); b1->SetSize(placeHolder.GetWidth(), placeHolder.GetHeight()); b1->SetBackgroundColor(sf::Color::Black); float x = (placeHolder.GetWidth() / 2) - (tmptile->width / 2); float y = (placeHolder.GetHeight() / 2) - (tmptile->height / 2); b1->SetSpritePos( x, y); } if( tileQueue.size() > 1 ) { int btnIndex = 1; //create a temp tile to make code look cleaner. Tile *tmptile = &tileQueue.at(tileQueueIndex + btnIndex); b2->SetImage( &chipsetImages.at( getChipsetIndex(tmptile->chipSetName))); //set the buttons sprite subrec. sf::IntRect subrec(tmptile->srcX, tmptile->srcY, tmptile->srcX + tmptile->width, tmptile->srcY + tmptile->height); b2->SetSubRect(subrec); b2->SetSize(placeHolder.GetWidth(), placeHolder.GetHeight()); b2->SetBackgroundColor(sf::Color::Black); float x = (placeHolder.GetWidth() / 2) - (tmptile->width / 2); float y = (placeHolder.GetHeight() / 2) - (tmptile->height / 2); b2->SetSpritePos( x, y); } if( tileQueue.size() > 2 ) { int btnIndex = 2; //create a temp tile to make code look cleaner. Tile *tmptile = &tileQueue.at(tileQueueIndex + btnIndex); b3->SetImage( &chipsetImages.at( getChipsetIndex(tmptile->chipSetName))); //set the buttons sprite subrec. sf::IntRect subrec(tmptile->srcX, tmptile->srcY, tmptile->srcX + tmptile->width, tmptile->srcY + tmptile->height); b3->SetSubRect(subrec); b3->SetSize(placeHolder.GetWidth(), placeHolder.GetHeight()); b3->SetBackgroundColor(sf::Color::Black); float x = (placeHolder.GetWidth() / 2) - (tmptile->width / 2); float y = (placeHolder.GetHeight() / 2) - (tmptile->height / 2); b3->SetSpritePos( x, y); } if( tileQueue.size() > 3 ) { int btnIndex = 3; //create a temp tile to make code look cleaner. Tile *tmptile = &tileQueue.at(tileQueueIndex + btnIndex); b4->SetImage( &chipsetImages.at( getChipsetIndex(tmptile->chipSetName))); //set the buttons sprite subrec. sf::IntRect subrec(tmptile->srcX, tmptile->srcY, tmptile->srcX + tmptile->width, tmptile->srcY + tmptile->height); b4->SetSubRect(subrec); b4->SetSize(placeHolder.GetWidth(), placeHolder.GetHeight()); b4->SetBackgroundColor(sf::Color::Black); float x = (placeHolder.GetWidth() / 2) - (tmptile->width / 2); float y = (placeHolder.GetHeight() / 2) - (tmptile->height / 2); b4->SetSpritePos( x, y); } if( tileQueue.size() > 4 ) { int btnIndex = 4; //create a temp tile to make code look cleaner. Tile *tmptile = &tileQueue.at(tileQueueIndex + btnIndex); b5->SetImage( &chipsetImages.at( getChipsetIndex(tmptile->chipSetName))); //set the buttons sprite subrec. sf::IntRect subrec(tmptile->srcX, tmptile->srcY, tmptile->srcX + tmptile->width, tmptile->srcY + tmptile->height); b5->SetSubRect(subrec); //set the width and height of the tile b5->SetSize(placeHolder.GetWidth(), placeHolder.GetHeight()); //set the buttons background to black to help //show the tile with a constrat on it. b5->SetBackgroundColor(sf::Color::Black); float x = (placeHolder.GetWidth() / 2) - (tmptile->width / 2); float y = (placeHolder.GetHeight() / 2) - (tmptile->height / 2); b5->SetSpritePos( x, y); } }
int main() { img_r.LoadFromFile("..\\Imagenes\\rcircle.png"); img_b.LoadFromFile("..\\Imagenes\\bcircle.png"); sprite_b.SetImage(img_b); sprite_r[0].SetImage(img_r); sprite_r[1].SetImage(img_r); sprite_r[2].SetImage(img_r); sprite_r[3].SetImage(img_r); sf::RenderWindow App(sf::VideoMode(800,600,32),"ventana"); const sf::Input& in = App.GetInput(); sprite_b.Scale(sprite_r[0].GetSize().x/sprite_b.GetSize().x,sprite_r[0].GetSize().y/sprite_b.GetSize().y); sprite_r[0].SetPosition(0,0); sprite_r[1].SetPosition(App.GetWidth()-img_r.GetWidth(),0); sprite_r[2].SetPosition(0,App.GetHeight()-img_r.GetHeight()); sprite_r[3].SetPosition(App.GetWidth()-img_r.GetWidth(),App.GetHeight()-img_r.GetHeight()); sprite_b.SetPosition((App.GetWidth()-sprite_b.GetSize().x)/2,(App.GetHeight()-sprite_b.GetSize().y)/2); int index = -1; int pos_x,pos_y; while(App.IsOpened()) { while(App.GetEvent(Event)) { if(Event.Type == sf::Event::Closed) App.Close(); if(index == -1 && Event.Type == sf::Event::MouseButtonPressed) { int x = Event.MouseButton.X; int y = Event.MouseButton.Y; int sx,sy; int w,h; for(int i=0;i<4;i++) { sx = sprite_r[i].GetPosition().x; sy = sprite_r[i].GetPosition().y; h = sprite_r[i].GetSize().y; w = sprite_r[i].GetSize().x; if(sx < x && sx + w > x && sy < y && sy + h > y) { index = i; pos_x = x; pos_y = y; break; } } } if(index > -1 && Event.Type == sf::Event::MouseButtonReleased) { index = -1; } } if(in.IsKeyDown(sf::Key::Escape)) App.Close(); if(index > -1) { int off_x = in.GetMouseX() - pos_x; int off_y = in.GetMouseY() - pos_y; pos_x = in.GetMouseX(); pos_y = in.GetMouseY(); sprite_r[index].Move(off_x,off_y); } App.Clear(); App.Draw(sprite_r[3]); App.Draw(sprite_r[2]); App.Draw(sprite_r[1]); App.Draw(sprite_r[0]); App.Draw(sprite_b); App.Display(); } return 0; }