Bienvenue, Invité. Merci de vous connecter ou de vous inscrire.
Avez-vous perdu votre e-mail d'activation ?

Auteur Sujet: Texture redimension  (Lu 722 fois)

0 Membres et 1 Invité sur ce sujet

Tifoux

  • Newbie
  • *
  • Messages: 1
    • Voir le profil
Texture redimension
« le: Janvier 01, 2015, 09:13:54 pm »
Bonjour je suis nouveau voila je me suis lancer dans l'aventure de créer un mini editeur de map 2d iso avec la SFML

Mes je rencontre quelque petit soucis, je ne sais pas comment redimensionner mes sprite(tiles) par rapport à ma grille isométrique?
#include "map.h"
#include <iostream>
Map::Map(sf::RenderWindow& _window) :
        window(_window)
{
        MapHeight = 17;
        MapWidth = 15;
        CommonCellHeight = 26;
        CommonCellWidth = 26;
        PreviewCellId = -1;
        BuildMap();
}

Map::~Map()
{
}

void Map::BuildMap()
{
        cells.clear();
        int cellId = 0;
        double cellWidth = GetMaxScaling();
        double cellHeight = std::ceil(cellWidth/2);
        int width = window.getSize().x;
        int height = window.getSize().y;

        int offsetX = (int) ((width - ((MapWidth + 0.5) * cellWidth))/2);
        int offsetY = (int) ((height - ((MapHeight + 0.5) * cellHeight))/2);

        double midCellHeight = cellHeight/2;
        double midCellWidth = cellWidth/2;
        for( int y = 0; y < 2*MapHeight-1;y++)
        {
                if(y%2== 0)
                {
                        for(int x = 0; x < MapWidth; x++)
                        {
                #pragma region           recupère les différents points de la cellule
                                sf::Vector2f left(offsetX + x*cellWidth,offsetY + y*midCellHeight + midCellHeight);
                                sf::Vector2f top(offsetX + x*cellWidth + midCellWidth,offsetY + y*midCellHeight);
                                sf::Vector2f right(offsetX + x*cellWidth + cellWidth,offsetY + y*midCellHeight + midCellHeight);
                                sf::Vector2f down(offsetX + x*cellWidth + midCellWidth,offsetY + y*midCellHeight + cellHeight);
                                std::vector<sf::Vector2f> points;
                                points.push_back(left);
                                points.push_back(top);
                                points.push_back(right);
                                points.push_back(down);
#pragma endregion
                                Cell current_cell(points,(short) cellId,width,height);
                                cells.push_back(current_cell);
                                cellId++;
                        }
                }else
                {
                        for (int x = 0; x < MapWidth-1; x++)
                        {

                                sf::Vector2f left(offsetX + x * cellWidth + midCellWidth,offsetY + y * midCellHeight + midCellHeight);
                                sf::Vector2f top(offsetX + x * cellWidth + cellWidth,offsetY + y * midCellHeight);
                                sf::Vector2f right(offsetX + x * cellWidth + cellWidth + midCellWidth,offsetY + y * midCellHeight + midCellHeight);
                                sf::Vector2f down(offsetX + x * cellWidth + cellWidth,offsetY + y * midCellHeight + cellHeight);
                                std::vector<sf::Vector2f> points;
                                points.push_back(left);
                                points.push_back(top);
                                points.push_back(right);
                                points.push_back(down);
                                Cell current_cell(points,(short) cellId,width,height);
                                current_cell.points = points;
                                current_cell.Id =(short) cellId;
                                cells.push_back(current_cell);
                                cellId++;
                        }
                }
        }
        RealCellWidth = cellWidth;
        RealCellHeight = cellHeight;
        PourceOfTile = RealCellWidth / RealCellHeight;
}
bool Map::build()
{
        sf::Texture texture;
        int CELL_H_HALF = window.getSize().y / (RealCellHeight * 2 - 2);
        int CELL_W_HALF = window.getSize().x / (RealCellWidth * 2 - 2);
        if (texture.loadFromFile("C:\\Users\\Tifoux\\Documents\\Visual Studio 2012\\Projects\\MapEditor\\Win32Project1\\Debug\\285.png"))
        {
                sf::Sprite sprite;
                sprite.setTexture(texture);
                sf::Vector2u size=      texture.getSize();
                sf::Vector2u newSize;
                newSize.x = size.x * PourceOfTile;
                newSize.y = size.y * PourceOfTile;
                for(int i =0; i < cells.size();i++)
                {
                        Cell cell = cells[i];  
                        int Base_x = 53;
                        int Base_y = 4;
                        int pos_x = Base_x * PourceOfTile;
                        int pos_y= Base_y* PourceOfTile;
                        sf::Vector2f center = cell.Center();
                        center.x -=( Base_x +CELL_W_HALF)-10;

                        center.y -= (Base_y - CELL_H_HALF) +( RealCellHeight/2) +5;
                        sprite.setPosition(center.x, center.y);
                        window.draw(sprite);

                }
        }
        for(int i =0; i < cells.size();i++)
        {
                Cell cell = cells[i];

                sf::ConvexShape polygon;
                polygon.setPointCount(4);
                polygon.setPoint(0,cell.points[0]);
                polygon.setPoint(1,cell.points[1]);
                polygon.setPoint(2, cell.points[2]);
                polygon.setPoint(3, cell.points[3]);
                polygon.setOutlineColor(sf::Color::White);
                polygon.setFillColor(sf::Color::Transparent);
                polygon.setOutlineThickness(1);
                window.draw(polygon);          
        }
                       
       
        if(PreviewCellId > -1)
        {
                Cell cell = cells[PreviewCellId];
                sf::ConvexShape polygon;
                polygon.setPointCount(4);
                polygon.setPoint(0,cell.points[0]);
                polygon.setPoint(1,cell.points[1]);
                polygon.setPoint(2, cell.points[2]);
                polygon.setPoint(3, cell.points[3]);
                polygon.setOutlineColor(sf::Color::White);
                sf::Color color(255,140,0,255);
                polygon.setFillColor(color);
                window.draw(polygon);
        }
        return true;
}


double Map::GetMaxScaling()
{
        int width = window.getSize().x;
        int height = window.getSize().y;
        double cellWidth = width/(double) (MapWidth + 1);
        double cellHeight = height /(double) (MapHeight + 1);
        cellWidth = std::min(cellHeight*2,cellWidth);
        return cellWidth;
}

void Map::MouseClick()
{

}

bool Map::MouseMove()
{
        int preview = -1;
        bool update = false;
        int width = window.getSize().x;
        int height = window.getSize().y;

        for(int i = 0; i < cells.size();i++)
        {

                Cell cell = cells[i];

                sf::IntRect r1 = cell.Retangle;

                sf::Vector2i posMouse = sf::Mouse::getPosition(window);

                sf::Vector2i position(posMouse.x - RealCellWidth, posMouse.y - RealCellHeight);
                sf::Vector2i size(RealCellWidth, RealCellHeight);

                sf::IntRect r2(position, size);

                sf::IntRect result;

                bool b3 = r1.intersects(r2, result); // true

                Point p;
                p.X = posMouse.x;

                p.Y =posMouse.y;

                std::vector<sf::Vector2f> pts;

                pts.push_back(cell.points[0]);
                pts.push_back(cell.points[1]);
                pts.push_back(cell.points[2]);
                pts.push_back(cell.points[3]);
                if(b3 && PointInPoly( p,pts))
                {
                        preview = cell.Id;
                        break;
                }
        }
        PreviewCellId = preview;
        return update;
}
 


j'arrive à construire la grille correctement j'arrive a récupéré la celulle ID et c'est cordonnée par rapport la cellule que ma souris traverse!

j'ai mis en pièce joint le tile que j'utilise est son rendue!

J'espère pouvoir avoir de l'aide de votre par merci d'avance!

Bonne Année!

 

anything