Browse Source

deux trois trucs

master
DemiSel 3 years ago
parent
commit
af34751be4

+ 12
- 3
lib/Agents/Agent.hpp View File

14
 
14
 
15
 	Agent();
15
 	Agent();
16
 	Agent(PictureEnvironment* iEnvironment);
16
 	Agent(PictureEnvironment* iEnvironment);
17
-	void loop();
17
+	virtual void loop();
18
 
18
 
19
-private:
20
-	void move();
19
+	virtual void move();
20
+	virtual void paint();
21
+};
22
+
23
+class RepaintAgent : public Agent
24
+{
25
+public:
26
+	RepaintAgent(PictureEnvironment* iEnvironment);
27
+	virtual void loop();
28
+	virtual void move();
29
+	virtual void paint();
21
 };
30
 };
22
 
31
 
23
 #endif // AGENT_H
32
 #endif // AGENT_H

+ 3
- 1
lib/Agents/Perception/Perception.hpp View File

6
 public:			 // Access specifier
6
 public:			 // Access specifier
7
 	int fWidth;	 // Attribute (int variable)
7
 	int fWidth;	 // Attribute (int variable)
8
 	int fHeight; // Attribute
8
 	int fHeight; // Attribute
9
-	std::vector<bool> fMovements;
9
+	std::vector<uint32_t> fSurroundings;
10
 
10
 
11
 	Perception();
11
 	Perception();
12
 	Perception(int iWidth, int iHeight);
12
 	Perception(int iWidth, int iHeight);
13
+	void setXY(uint32_t iValue, int iX, int iY);
14
+	uint32_t getXY(int iX, int iY);
13
 };
15
 };
14
 
16
 
15
 #endif // PERCEPTION_H
17
 #endif // PERCEPTION_H

+ 6
- 3
lib/Environment/PictureEnvironment.hpp View File

19
 	sf::Texture fTextureImage;
19
 	sf::Texture fTextureImage;
20
 	sf::Image fImage;
20
 	sf::Image fImage;
21
 
21
 
22
-	std::list<Agent> fAgents;
22
+	std::vector<std::unique_ptr<Agent>> fAgents;
23
 	std::list<Agent>::iterator fAgentsIterator;
23
 	std::list<Agent>::iterator fAgentsIterator;
24
 
24
 
25
 	PictureEnvironment(std::string& iFilePath, sf::RenderWindow* iWindow, util::Platform* iPlatform);
25
 	PictureEnvironment(std::string& iFilePath, sf::RenderWindow* iWindow, util::Platform* iPlatform);
26
-	Perception getPerception(int iX, int iY);
26
+	void getPerception(Perception* ioPercept, int iX, int iY);
27
 	void makeAgents(int iNumber);
27
 	void makeAgents(int iNumber);
28
-	void paint(int iX, int iY, const sf::Color Color);
28
+	void makeRepaintAgents(int iNumber);
29
+	bool paint(int iX, int iY, const sf::Color Color);
30
+
31
+	uint32_t getCell(int iX, int iY);
29
 
32
 
30
 	int getWidth();
33
 	int getWidth();
31
 	int getHeight();
34
 	int getHeight();

+ 14
- 6
src/Agents/Agent.cpp View File

1
 #include <Agents/Agent.hpp>
1
 #include <Agents/Agent.hpp>
2
+#include <Agents/Perception/Perception.hpp>
2
 #include <Environment/PictureEnvironment.hpp>
3
 #include <Environment/PictureEnvironment.hpp>
3
 
4
 
4
 Agent::Agent()
5
 Agent::Agent()
5
 {
6
 {
6
 }
7
 }
7
 
8
 
9
+int PERCEPTION_SIZE = 2;
10
+
8
 Agent::Agent(PictureEnvironment* iEnvironment)
11
 Agent::Agent(PictureEnvironment* iEnvironment)
9
 {
12
 {
10
 	//uint32_t WHITE = 0xFFFFFFFF;
13
 	//uint32_t WHITE = 0xFFFFFFFF;
11
 	fEnvironment = iEnvironment;
14
 	fEnvironment = iEnvironment;
12
-
15
+	fCurrentPerception = Perception(PERCEPTION_SIZE, PERCEPTION_SIZE);
13
 	//fColor = sf::Color(rand() % 0xFFFFFFFFFFFF);
16
 	//fColor = sf::Color(rand() % 0xFFFFFFFFFFFF);
14
 	uint32_t vRand = (((double)rand() / RAND_MAX) * 0xFFFFFFFFU);
17
 	uint32_t vRand = (((double)rand() / RAND_MAX) * 0xFFFFFFFFU);
15
 
18
 
21
 
24
 
22
 void Agent::loop()
25
 void Agent::loop()
23
 {
26
 {
24
-	fEnvironment->getPerception(fXPos, fYPos);
27
+	fEnvironment->getPerception(&fCurrentPerception, fXPos, fYPos);
25
 	move();
28
 	move();
29
+	this->paint();
26
 }
30
 }
27
 
31
 
28
-int MOVESIZE = 5;
32
+int MOVESIZE = 10;
29
 
33
 
30
 void Agent::move()
34
 void Agent::move()
31
 {
35
 {
32
-	fXPos += (fXPos > MOVESIZE) ? (rand() % MOVESIZE) - (MOVESIZE / 2) : MOVESIZE;
33
-	fYPos += (fYPos > MOVESIZE) ? (rand() % MOVESIZE) - (MOVESIZE / 2) : MOVESIZE;
34
-	fEnvironment->paint(fXPos, fYPos, fColor);
36
+	fXPos += ((double)rand() / RAND_MAX) * MOVESIZE - ((double)MOVESIZE / 2);
37
+	fYPos += ((double)rand() / RAND_MAX) * MOVESIZE - ((double)MOVESIZE / 2);
35
 }
38
 }
39
+
40
+void Agent::paint()
41
+{
42
+	fEnvironment->paint(fXPos, fYPos, fColor);
43
+}

+ 11
- 1
src/Agents/Perception/Perception.cpp View File

6
 
6
 
7
 Perception::Perception(int iX, int iY)
7
 Perception::Perception(int iX, int iY)
8
 {
8
 {
9
-	fMovements = std::vector<bool>(iX * iY);
9
+	fSurroundings = std::vector<uint32_t>(iX * iY);
10
 	fWidth = iX;
10
 	fWidth = iX;
11
 	fHeight = iY;
11
 	fHeight = iY;
12
+}
13
+
14
+void Perception::setXY(uint32_t iValue, int iX, int iY)
15
+{
16
+	fSurroundings[iX * iY] = iValue;
17
+}
18
+
19
+uint32_t Perception::getXY(int iX, int iY)
20
+{
21
+	return fSurroundings[iX * iY];
12
 }
22
 }

+ 50
- 0
src/Agents/RepaintAgent.cpp View File

1
+#include <Agents/Agent.hpp>
2
+#include <Environment/PictureEnvironment.hpp>
3
+
4
+RepaintAgent::RepaintAgent(PictureEnvironment* iEnvironment) :
5
+	Agent(iEnvironment)
6
+{
7
+	int vWidth = fEnvironment->getWidth();
8
+	int vHeight = fEnvironment->getHeight();
9
+
10
+	fXPos = (((double)rand() / RAND_MAX) * vWidth);
11
+	fYPos = (((double)rand() / RAND_MAX) * vHeight);
12
+
13
+	fColor = sf::Color(fEnvironment->getCell(fXPos, fYPos));
14
+}
15
+
16
+int MOVESIZE_PAINT = 10;
17
+
18
+void RepaintAgent::move()
19
+{
20
+	double vRand = rand() % (MOVESIZE_PAINT + 1);
21
+	fXPos += vRand - MOVESIZE_PAINT / 2;
22
+	vRand = rand() % (MOVESIZE_PAINT + 1);
23
+	fYPos += vRand - MOVESIZE_PAINT / 2;
24
+}
25
+
26
+int BRUSH_SIZE_PAINT = 20;
27
+
28
+void RepaintAgent::paint()
29
+{
30
+	int vHalfBrush = BRUSH_SIZE_PAINT / 2;
31
+	int vXLoopIndex = fXPos - vHalfBrush;
32
+	int vYLoopIndex = fYPos - vHalfBrush;
33
+	while (vXLoopIndex < (fXPos + vHalfBrush))
34
+	{
35
+		while (vYLoopIndex < fYPos + vHalfBrush)
36
+		{
37
+			fEnvironment->paint(vXLoopIndex, vYLoopIndex, fColor);
38
+			vYLoopIndex++;
39
+		}
40
+		vYLoopIndex = fYPos - vHalfBrush;
41
+		vXLoopIndex++;
42
+	}
43
+}
44
+
45
+void RepaintAgent::loop()
46
+{
47
+	fEnvironment->getPerception(&fCurrentPerception, fXPos, fYPos);
48
+	move();
49
+	this->paint();
50
+}

+ 45
- 23
src/Environment/PictureEnvironment.cpp View File

4
 #include <Environment/PictureEnvironment.hpp>
4
 #include <Environment/PictureEnvironment.hpp>
5
 
5
 
6
 const sf::Vector2f DEFAUMT_SIMZE = sf::Vector2f(800, 600);
6
 const sf::Vector2f DEFAUMT_SIMZE = sf::Vector2f(800, 600);
7
-int PERCEPTION_SIZE = 4;
8
 
7
 
9
 PictureEnvironment::PictureEnvironment()
8
 PictureEnvironment::PictureEnvironment()
10
 {
9
 {
38
 	fShape.setTexture(&fTextureImage);
37
 	fShape.setTexture(&fTextureImage);
39
 }
38
 }
40
 
39
 
41
-Perception PictureEnvironment::getPerception(int iX, int iY)
40
+void PictureEnvironment::getPerception(Perception* ioPercept, int iX, int iY)
42
 {
41
 {
43
-	Perception vPerception = Perception(PERCEPTION_SIZE, PERCEPTION_SIZE);
44
-	iX++;
45
-	iY++;
46
-	return vPerception;
42
+	int vPerceptHalfWidth = ioPercept->fWidth / 2;
43
+	int vPerceptHalfHeight = ioPercept->fHeight / 2;
44
+	int vLoopX = iX - vPerceptHalfWidth;
45
+	int vLoopY = iY - vPerceptHalfHeight;
46
+	while (vLoopX < iX + vPerceptHalfWidth)
47
+	{
48
+		vLoopX++;
49
+		while (vLoopY < iY + vPerceptHalfHeight)
50
+		{
51
+			if (vLoopX > 0 && vLoopX < getWidth() && vLoopY > 0 && vLoopY < getHeight())
52
+			{
53
+				ioPercept->setXY(
54
+					fImage.getPixel(vLoopX, vLoopY).toInteger(),
55
+					1,
56
+					1);
57
+			}
58
+			vLoopY++;
59
+		}
60
+		vLoopY = iX - vPerceptHalfHeight;
61
+	}
47
 }
62
 }
48
 
63
 
49
 bool PictureEnvironment::loop()
64
 bool PictureEnvironment::loop()
56
 				fWindow->close();
71
 				fWindow->close();
57
 		}
72
 		}
58
 		// fAgentsIterator = fAgents.begin();
73
 		// fAgentsIterator = fAgents.begin();
59
-		std::list<Agent>::iterator vAgentsIterator = fAgents.begin();
60
-		fAgents.begin();
74
+		std::vector<std::unique_ptr<Agent>>::iterator vAgentsIterator = fAgents.begin();
61
 		while (vAgentsIterator != fAgents.end())
75
 		while (vAgentsIterator != fAgents.end())
62
 		{
76
 		{
63
-
64
 			//std::cout << "loop" << vAgentsIterator->fColor->toInteger() << " at " << &vAgentsIterator << "\n";
77
 			//std::cout << "loop" << vAgentsIterator->fColor->toInteger() << " at " << &vAgentsIterator << "\n";
65
-			vAgentsIterator->loop();
78
+			vAgentsIterator->get()->loop();
66
 			vAgentsIterator++;
79
 			vAgentsIterator++;
67
 		}
80
 		}
68
 		fTextureImage.loadFromImage(fImage);
81
 		fTextureImage.loadFromImage(fImage);
89
 	return fImage.getSize().x;
102
 	return fImage.getSize().x;
90
 }
103
 }
91
 
104
 
105
+uint32_t PictureEnvironment::getCell(int iX, int iy)
106
+{
107
+	return fImage.getPixel(iX, iy).toInteger();
108
+}
109
+
92
 void PictureEnvironment::makeAgents(int iNumber)
110
 void PictureEnvironment::makeAgents(int iNumber)
93
 {
111
 {
94
 
112
 
95
-	fAgents = std::list<Agent>();
113
+	fAgents = std::vector<std::unique_ptr<Agent>>();
96
 
114
 
97
 	for (int i = 0; i < iNumber; i++)
115
 	for (int i = 0; i < iNumber; i++)
98
 	{
116
 	{
99
-		Agent vTmpAgent = Agent(this);
100
-		fAgents.push_back(vTmpAgent);
101
-		//std::cout << "pushback" << vTmpAgent.fColor.toInteger() << " at " << &vTmpAgent << "\n"
102
-		//		  << " size : " << fAgents.size() << "\n";
117
+		fAgents.push_back(std::make_unique<Agent>(this));
103
 	}
118
 	}
104
-	std::list<Agent>::iterator vAgentsIterator = fAgents.begin();
105
-	while (vAgentsIterator != fAgents.end())
106
-	{
119
+}
107
 
120
 
108
-		//std::cout << "loop" << vAgentsIterator->fColor->toInteger() << " at " << &vAgentsIterator << "\n";
109
-		vAgentsIterator++;
121
+void PictureEnvironment::makeRepaintAgents(int iNumber)
122
+{
123
+	fAgents = std::vector<std::unique_ptr<Agent>>();
124
+	for (int i = 0; i < iNumber; i++)
125
+	{
126
+		fAgents.push_back(std::make_unique<RepaintAgent>(this));
110
 	}
127
 	}
111
 }
128
 }
112
 
129
 
120
 	std::cin >> i;
137
 	std::cin >> i;
121
 }
138
 }
122
 
139
 
123
-void PictureEnvironment::paint(int iX, int iY, const sf::Color iColour)
140
+bool PictureEnvironment::paint(int iX, int iY, const sf::Color iColour)
124
 {
141
 {
125
-	//std::cout << "juste avant : " << iX << " et " << iY;
126
-	fImage.setPixel(iX, iY, iColour);
142
+
143
+	bool doable = iX > 0 && iX < getWidth() && iY > 0 && iY < getHeight();
144
+	if (doable)
145
+	{
146
+		fImage.setPixel(iX, iY, iColour);
147
+	}
148
+	return doable;
127
 }
149
 }

+ 5
- 3
src/Main.cpp View File

9
 #if defined(_DEBUG)
9
 #if defined(_DEBUG)
10
 	std::cout << "Hello World!" << std::endl;
10
 	std::cout << "Hello World!" << std::endl;
11
 #endif
11
 #endif
12
-	std::string filepath = "C:\\Users\\VroumVroumMachine\\Pictures\\dickbutt.jpg";
12
+	//std::string filepath = "C:\\Users\\VroumVroumMachine\\Pictures\\dickbutt.jpg";
13
 	//std::string filepath = "C:\\Users\\VroumVroumMachine\\Pictures\\caisseenre.png";
13
 	//std::string filepath = "C:\\Users\\VroumVroumMachine\\Pictures\\caisseenre.png";
14
+	std::string filepath = "C:\\Users\\VroumVroumMachine\\Pictures\\RepAgents\\PA315024.jpg";
14
 	//std::string filepath = "content/sfml.png";
15
 	//std::string filepath = "content/sfml.png";
15
 	sf::RenderWindow window;
16
 	sf::RenderWindow window;
16
 
17
 
22
 
23
 
23
 	PictureEnvironment vEnvironment(filepath, &window, &platform);
24
 	PictureEnvironment vEnvironment(filepath, &window, &platform);
24
 
25
 
25
-	vEnvironment.makeAgents(1000);
26
+	//vEnvironment.makeAgents(100);
27
+	vEnvironment.makeRepaintAgents(50);
26
 	//int i = 0;
28
 	//int i = 0;
27
 	while (vEnvironment.loop())
29
 	while (vEnvironment.loop())
28
 	{
30
 	{
29
 		;
31
 		;
30
-		usleep(30);
32
+		usleep(10);
31
 	}
33
 	}
32
 
34
 
33
 	//vEnvironment.displayLockInput();
35
 	//vEnvironment.displayLockInput();

Loading…
Cancel
Save