Voici mes destructeurs :
Tile::~Tile () {
if (shadow != NULL) {
delete shadow;
}
if (wall != NULL) {
delete wall;
}
}
ShadowTile::~ShadowTile () {
delete shadow;
}
Map::~Map () {
delete gridMap;
}
GridMap::~GridMap () {
vector<Entity*> entities = getEntities();
for (unsigned int i = 0; i < entities.size(); i++) {
delete entities[i];
}
for (unsigned int i = 0; i < casesMap.size(); i++) {
delete casesMap[i];
}
for (unsigned int i = 0; i < collisionsPolygons.size(); i++) {
delete collisionsPolygons[i];
}
casesMap.clear ();
collisionsPolygons.clear();
}
MyCanvas::~MyCanvas () {
delete map;
delete lm;
delete caracter;
}
Dans MyFrame par contre je n'ai pas fais de destructeur.
Bref je rends les ombres avec une RenderTexture et c'est là que ça bug. :/
J'ai un autre projets ou c'est exactement le même code mais avec la SFML 1.6 et là ça ne bug pas.
Et pourtant pour la RenderTexture je fais juste un appel à créate dans la méthode init, et j'affiche tout avec la méthode update :
void MyCanvas::onInit() {
centerView = view.getCenter();
Vector2i position = mapCoordsToPixel(Vector2f(0, 0), view);
BoundingRectangle br (position.x, position.y, view.getSize().x, view.getSize().y);
ambientColor.r = 255;
ambientColor.g = 255;
ambientColor.b = 255;
renderTextShadows.create(view.getSize().x, view.getSize().y);
renderTextLights.create(view.getSize().x, view.getSize().y);
loadMap();
map->gridMap->checkVisibleTiles(centerView);
lm->checkVisibleLights(br);
lm->checkVisibleSegsWalls();
lm->checkVisibleShadows();
loadMonsters ();
/*if (PostFX::CanUsePostFX && effet.LoadFromFile("effets/colorize.sfx")) {
effet.SetTexture("framebuffer", NULL);
effet.SetParameter("color", 1.f, 1.f, 1.f);
} else
cout<<"Impossible d'appliquer l'effet."<<endl;*/
setView(view);
}
//Méthode appelée lors de la mise à jour de l'affichage. (Raffraichissement.)
void MyCanvas::onUpdate () {
clear();
bool day;
(ambientColor.r == 0 && ambientColor.g == 0 && ambientColor.b == 255) ? day = false : day = true;
Vector2f v2 (view.getCenter().x - view.getSize().x * 0.5f, view.getCenter().y - view.getSize().y * 0.5f);
//Rendu des ombres.
renderTextShadows.setView(view);
renderTextShadows.clear(Color::White);
if (day) {
RectangleShape rect;
rect.setPosition(Vector2f(v2.x, v2.y));
rect.setFillColor((Color(100, 100, 100, 128)));
rect.setSize(Vector2f (view.getSize().x, view.getSize().y));
vector<Shadow*> visibleShadows = lm->getAllVisibleShadows();
for (unsigned int i = 0; i < visibleShadows.size(); i++) {
renderTextShadows.draw(*visibleShadows[i]->getShadow());
//draw(*visibleShadows[i]->getShadow());
}
renderTextShadows.draw(rect, RenderStates(BlendAdd));
//draw(rect, RenderStates(BlendAdd));
}
renderTextShadows.display();
//Rendu des lumières.
renderTextLights.clear(ambientColor);
if (!day) {
vector<Light*> visibleLights = lm->getAllVisibleLights();
for (unsigned int i = 0; i < visibleLights.size(); i++) {
for (unsigned int j = 0; j < visibleLights[i]->getTris().size(); j++) {
renderTextLights.draw(*visibleLights[i]->getTris()[j], RenderStates(BlendAdd));
if (visibleLights[i]->getType() == Light::DIRECTIONNAL_LIGHT) {
for (unsigned int j = 0; j < visibleLights[i]->getConeTris().size(); j++)
renderTextLights.draw(*visibleLights[i]->getConeTris()[j], RenderStates(BlendAdd));
}
}
}
}
//Rendu de la map. (partie visible à l'écran.)
clear();
vector<Tile*> visiblesTiles = map->gridMap->getVisibleTiles();
bool shadowRendered = false;
if (caracter != NULL) {
addDynamicTile(caracter, visiblesTiles);
}
vector<Monster*> monsters = map->gridMap->getVisibleMonsters();
for (unsigned int i = 0; i < monsters.size(); i++) {
addDynamicTile(monsters[i]->getTile(), visiblesTiles);
}
for (unsigned int i = 0; i < visiblesTiles.size(); i++) {
if (visiblesTiles[i]->getZOrder() >= Tile::MIN_ENTITY_Z_ORDER && !shadowRendered && day) {
Sprite spriteShadows (renderTextShadows.getTexture());
spriteShadows.setPosition(v2.x, v2.y);
draw(spriteShadows, RenderStates(BlendMultiply));
//draw(spriteShadows, RenderStates(BlendAdd));
shadowRendered = true;
}
draw(visiblesTiles[i]->getSprite());
}
/* if (!day)
draw (Sprite(renderTextLights.getTexture()), RenderStates(BlendMultiply));*/
//Gestion des évènements.
processEvents();
//Dessin du personnage.
if (selectedCar->moving()) {
Vec2f cView (centerView.x, centerView.y);
Vec2f newPos = cView + selectedCar->getDir() * speed * clock.getElapsedTime().asSeconds();
if ((newPos - cView).magnitude() >
(points[currentPointIndex] - cView).magnitude())
newPos = points[currentPointIndex];
Vec2f diff = newPos - cView;
view.move(diff.x, diff.y);
setView(view);
centerView = view.getCenter();
caracter->getSprite().setPosition(centerView);
map->gridMap->checkVisibleTiles(centerView);
Vector2i position = mapCoordsToPixel(Vector2f(0, 0), view);
BoundingRectangle br (position.x, position.y, view.getSize().x, view.getSize().y);
lm->checkVisibleLights(br);
//map->gridMap->checkCollisions (centerView);
//br = BoundingRectangle (position.x - 100, position.y - 100, view.getSize().x + 100, view.getSize().y + 100);
lm->checkVisibleShadows();
lm->checkVisibleSegsWalls();
Vec2f newDir = points[currentPointIndex] - newPos;
newDir = newDir.normalize();
if (selectedCar->getDir().isOpposite(newDir)
|| points[currentPointIndex] == newPos) {
currentPointIndex++;
if (currentPointIndex >= points.size()) {
selectedCar->setMoving(false);
Vec2f nullDir(0, 0);
selectedCar->setDir(nullDir);
} else {
newDir = points[currentPointIndex] - cView;
newDir = newDir.normalize();
selectedCar->setDir(newDir);
}
}
}
//Dessin des monstres.
clock.restart();
//Draw(effet);
}
Et ça plante juste quand j'appuie sur une touche du clavier :
}
void MyCanvas::processEvents () {
Event event;
Vec2f center = caracter->getRelCoords();
Vec2f size (caracter->getSize().x, caracter->getSize().y);
Vec2f pos (center.x - size.x * 0.5f, center.y - size.y * 0.5f);
float elTime = clock.getElapsedTime().asSeconds();
bool keyboardMove = false;
while (pollEvent(event)) {
if (Keyboard::isKeyPressed(Keyboard::Right)) {
Network::sendMessage("MOVE*"+conversionIntString(pos.x + speed * elTime)+"#"+conversionIntString(pos.y)+"#"+conversionIntString(size.x)+"#"+conversionIntString(size.y));
string response = Network::getLastResponse();
selectedCar->setMoving(false);
if (response == "Ok") {
map->setOffsetFrameX(speed * elTime);
view.move(speed * elTime, 0);
keyboardMove = true;
}
} else if (Keyboard::isKeyPressed(Keyboard::Left)) {
Network::sendMessage("MOVE*"+conversionIntString(pos.x - speed * elTime)+"#"+conversionIntString(pos.y)+"#"+conversionIntString(size.x)+"#"+conversionIntString(size.y));
string response = Network::getLastResponse();
selectedCar->setMoving(false);
if (response == "Ok") {
map->setOffsetFrameX(-speed * elTime);
view.move(-speed * elTime, 0);
keyboardMove = true;
}
} else if (Keyboard::isKeyPressed(Keyboard::Up)) {
Network::sendMessage("MOVE*"+conversionIntString(pos.x)+"#"+conversionIntString(pos.y - 25)+"#"+conversionIntString(size.x)+"#"+conversionIntString(size.y));
string response = Network::getLastResponse();
selectedCar->setMoving(false);
if (response == "Ok") {
map->setOffsetFrameY(-speed * elTime);
view.move(0, -speed * elTime);
keyboardMove = true;
}
} else if (Keyboard::isKeyPressed(Keyboard::Down)) {
Network::sendMessage("MOVE*"+conversionIntString(pos.x)+"#"+conversionIntString(pos.y + speed * elTime)+"#"+conversionIntString(size.x)+"#"+conversionIntString(size.y));
string response = Network::getLastResponse();
selectedCar->setMoving(false);
if (response == "Ok") {
map->setOffsetFrameY(speed * elTime);
view.move(0, speed * elTime);
keyboardMove = true;
}
} else if (Mouse::isButtonPressed(Mouse::Left)) {
selectedCar->setMoving(false);
string startPos = conversionIntString(view.getCenter().x)+"#"+conversionIntString(view.getCenter().y);
Vector2i cMousePos = mapCoordsToPixel (Vector2f(Mouse::getPosition(*this).x, Mouse::getPosition(*this).y), view);
string finalPos = conversionIntString(cMousePos.x)+"#"+conversionIntString(cMousePos.y);
Vec2f fPos (cMousePos.x, cMousePos.y);
points.clear();
if (map->gridMap->getGridCellAt(fPos) != NULL) {
Vec2f cView (centerView.x, centerView.y);
if (map->gridMap->getGridCellAt(cView) == map->gridMap->getGridCellAt(fPos)) {
points.push_back(fPos);
} else {
Network::sendMessage("MOVE2*"+startPos+"#"+finalPos);
int size = conversionStringInt (Network::getLastResponse());
for (int i = 0; i < size; i++) {
vector<string> parts = split (Network::getLastResponse(), "*");
Vec2f point (conversionStringInt(parts[0]), conversionStringInt(parts[1]));
points.push_back(point);
}
}
currentPointIndex = 0;
selectedCar->setMoving(true);
Vec2f dir = points[currentPointIndex] - cView;
dir = dir.normalize();
selectedCar->setDir(dir);
}
}
if (keyboardMove) {
centerView = view.getCenter();
caracter->getSprite().setPosition(centerView);
map->gridMap->checkVisibleTiles(centerView);
Vector2i position = mapCoordsToPixel(Vector2f(0, 0), view);
BoundingRectangle br (position.x, position.y, view.getSize().x, view.getSize().y);
lm->checkVisibleLights(br);
//map->gridMap->checkCollisions (centerView);
lm->checkVisibleShadows();
lm->checkVisibleSegsWalls();
setView(view);
}
}
}
Le personnage bouge bien mais j'ai un écran tout noir à cause de ce bug.