View difference between Paste ID: keH3EL9h and CHeBjNmw
SHOW: | | - or go back to the newest paste.
1
void Game::cleanGarbage()
2
{
3
	Clock dt;
4
	dt.restart();
5
6-
	for (int i = 0; i < this->garbage.size(); i++)
6+
	// Ces tableaux vont contenir les éléments "sauvés" (que tu ne garbages pas)
7
	vector<Independant*> newSceneIndependants;
8-
		vector<Independant*>::iterator it1_ = this->sceneIndependants.begin();
8+
	//vector<Independant*> newSceneIndependantsLayered[NB_LAYERS];	// TODO: je te passe le code pour
9-
		vector<Independant*>::iterator it2_ = this->sceneIndependantsLayered[(int)(this->garbage[i]->layer)].begin();
9+
	//vector<Independant*> newSceneIndependantsTyped[NB_TYPES];	// ces tableaux-là...
10-
		vector<Independant*>::iterator it3_ = this->sceneIndependantsTyped[(int)(this->garbage[i]->collider_type)].begin();
10+
	
11
	// On "cache" les tailles des tableaux pour éviter de faire un appel de fonction à chaque itération
12-
		it1_ = this->sceneIndependants.erase(it1_);
12+
	// En "const", c'est mieux parce qu'on est certains que ça ne changera pas dans le code qui suit.
13-
		it2_ = this->sceneIndependantsLayered[(int)(this->garbage[i]->layer)].erase(it2_);
13+
	// En "size_t" plutôt que "int", c'est le même type que ce que renvoie "size()" (attention à ne pas utiliser
14-
		it3_ = this->sceneIndependantsTyped[(int)(this->garbage[i]->collider_type)].erase(it3_);
14+
	// ça si tu fais des boucles à l'envers, c'est toujours positif - for(j=size-1;j > 0 ;j--) est une boucle
15
	// infinie si j est un size_t.
16
	const size_t sceneIndependantsSize = this->sceneIndependants.size();
17
	const size_t garbageSize = this->garbage.size();
18
		
19
	newSceneIndependants.reserve(sceneIndependantsSize);	// évite des réallocations, optim, pas obligé mais mieux
20
	
21
	for(size_t i=0 ; i < sceneIndependantsSize ; i++)
22
	{
23
		// On prend un "candidat" : sera-t-il sauvé ? (= pas présent dans la liste "garbage")
24
		Independant* pCandidate = this->sceneIndependants[i];
25
		bool bCandidateFound = false;
26
		// NB : avant, quand tu appelais "remove()", ça parcourait toute la liste en cherchant
27
		// un élément donné, donc c'était caché mais tu avais bien 2 boucles imbriquées aussi
28
		for(size_t j=0 ; j < garbageSize ; j++)
29
		{
30
			if(pCandidate == this->garbage[j])
31
			{
32
				bCandidateFound = true;
33
				break;	// pas nécessaire, mais c'est une optim : aucun intérêt à continuer à parcourir le tableau
34
			}
35
		}
36
		if(!bCandidateFound)
37
			newSceneIndependants.push_back(pCandidate);	// sauvé !
38
	}
39
40
	// Comme je disais, je te passe le code pour les version "layered" et "typed"...
41
42
	// Et à la fin, tu copies (fait une boucle en interne), dans l' "operator=" de std::vector:
43
	this->sceneIndependants = newSceneIndependants;
44
45
	//printf("| Clean: %d ",dt.getElapsedTime().asMilliseconds());
46
}