Parcourir la source

ça commence a se déplacer

master
DemiSel il y a 3 ans
Parent
révision
60527e53b5

+ 5
- 0
lib/Agents/Agent.hpp Voir le fichier

@@ -8,11 +8,16 @@ class Agent
8 8
 public:
9 9
 	Perception fCurrentPerception;
10 10
 	PictureEnvironment* fEnvironment;
11
+	sf::Color fColor;
11 12
 	int fXPos;
12 13
 	int fYPos;
13 14
 
15
+	Agent();
14 16
 	Agent(PictureEnvironment* iEnvironment);
15 17
 	void loop();
18
+
19
+private:
20
+	void move();
16 21
 };
17 22
 
18 23
 #endif // AGENT_H

+ 1
- 1
lib/Agents/Perception/Perception.hpp Voir le fichier

@@ -6,7 +6,7 @@ class Perception
6 6
 public:			 // Access specifier
7 7
 	int fWidth;	 // Attribute (int variable)
8 8
 	int fHeight; // Attribute
9
-	std::vector<bool> fPercept;
9
+	std::vector<bool> fMovements;
10 10
 
11 11
 	Perception();
12 12
 	Perception(int iWidth, int iHeight);

+ 19
- 6
lib/Environment/PictureEnvironment.hpp Voir le fichier

@@ -1,21 +1,34 @@
1 1
 #ifndef PICTURE_ENVIRONMENT_H
2 2
 #define PICTURE_ENVIRONMENT_H
3
+#include "Platform/Platform.hpp"
3 4
 #include <Agents/Perception/Perception.hpp>
4 5
 
6
+class Agent;
7
+
5 8
 class PictureEnvironment
6
-{				 // The class
7
-public:			 // Access specifier
8
-	int fWidth;	 // Attribute (int variable)
9
-	int fHeight; // Attribute
9
+{		// The class
10
+public: // Access specifier
10 11
 	std::string fFilePath;
11 12
 	sf::RenderWindow* fWindow;
13
+	util::Platform* fPlatform;
14
+	const sf::Vector2f fSize;
15
+	sf::Vector2f fInitialSize;
12 16
 	sf::Event fEvent;
13
-	sf::CircleShape fShape;
17
+	sf::RectangleShape fShape;
18
+
19
+	sf::Texture fTextureImage;
20
+	sf::Image fImage;
21
+
22
+	std::list<Agent> fAgents;
23
+	std::list<Agent>::iterator fAgentsIterator;
14 24
 
15
-	PictureEnvironment(std::string& iFilePath, sf::RenderWindow* iWindow);
25
+	PictureEnvironment(std::string& iFilePath, sf::RenderWindow* iWindow, util::Platform* iPlatform);
16 26
 	Perception getPerception(int iX, int iY);
27
+	void makeAgents(int iNumber);
28
+	void paint(int iX, int iY, const sf::Color Color);
17 29
 
18 30
 	bool loop();
31
+	void displayLockInput();
19 32
 
20 33
 private:
21 34
 	PictureEnvironment();

+ 22
- 1
src/Agents/Agent.cpp Voir le fichier

@@ -1,11 +1,32 @@
1 1
 #include <Agents/Agent.hpp>
2 2
 #include <Environment/PictureEnvironment.hpp>
3 3
 
4
+Agent::Agent()
5
+{
6
+}
7
+
4 8
 Agent::Agent(PictureEnvironment* iEnvironment)
5 9
 {
10
+	long WHITE = 0xFFFFFFFF;
6 11
 	fEnvironment = iEnvironment;
12
+
13
+	//fColor = sf::Color(rand() % 0xFFFFFFFFFFFF);
14
+	fColor = sf::Color(((rand() % WHITE) << 16) + 0xFF);
15
+	fXPos = 100;
16
+	fYPos = 100;
7 17
 }
8 18
 
9 19
 void Agent::loop()
10 20
 {
11
-}
21
+	fEnvironment->getPerception(fXPos, fYPos);
22
+	move();
23
+}
24
+
25
+int MOVESIZE = 10;
26
+
27
+void Agent::move()
28
+{
29
+	fXPos += (fXPos > MOVESIZE) ? rand() % MOVESIZE - 5 : MOVESIZE;
30
+	fYPos += (fYPos > MOVESIZE) ? rand() % MOVESIZE - 5 : MOVESIZE;
31
+	fEnvironment->paint(fXPos, fYPos, fColor);
32
+}

+ 1
- 1
src/Agents/Perception/Perception.cpp Voir le fichier

@@ -6,7 +6,7 @@ Perception::Perception()
6 6
 
7 7
 Perception::Perception(int iX, int iY)
8 8
 {
9
-	fPercept = std::vector<bool>(iX * iY);
9
+	fMovements = std::vector<bool>(iX * iY);
10 10
 	fWidth = iX;
11 11
 	fHeight = iY;
12 12
 }

+ 55
- 15
src/Environment/PictureEnvironment.cpp Voir le fichier

@@ -1,30 +1,42 @@
1
+#include "Platform/Platform.hpp"
2
+#include <Agents/Agent.hpp>
1 3
 #include <Agents/Perception/Perception.hpp>
2 4
 #include <Environment/PictureEnvironment.hpp>
3 5
 
6
+const sf::Vector2f DEFAUMT_SIMZE = sf::Vector2f(800, 600);
4 7
 int PERCEPTION_SIZE = 4;
5 8
 
6 9
 PictureEnvironment::PictureEnvironment()
7 10
 {
8 11
 }
9 12
 
10
-PictureEnvironment::PictureEnvironment(std::string& iFilePath, sf::RenderWindow* iWindow)
13
+PictureEnvironment::PictureEnvironment(std::string& iFilePath, sf::RenderWindow* iWindow, util::Platform* iPlatform) :
14
+	fSize(iWindow->getSize())
11 15
 {
16
+
12 17
 	fFilePath = iFilePath;
18
+	//sf::Texture fLoadedImage;
19
+	if (!fTextureImage.loadFromFile(fFilePath))
20
+	{
21
+		std::cout << "\n COULDNT LOAD PICTURE\n";
22
+	}
23
+	fImage = fTextureImage.copyToImage();
13 24
 
14 25
 	//memccpy(&fFilePath, &iFilePath, *iFilePath., 10);
15 26
 	std::cout << "Environement pour " << fFilePath;
16
-
27
+	fPlatform = iPlatform;
17 28
 	fWindow = iWindow;
29
+	float screenScalingFactor = fPlatform->getScreenScalingFactor(fWindow->getSystemHandle());
30
+	fWindow->create(sf::VideoMode(800.0f * screenScalingFactor, 600.0f * screenScalingFactor), "SFML works!");
31
+	fPlatform->setIcon(fWindow->getSystemHandle());
32
+	fInitialSize.x = 800.0f * screenScalingFactor;
33
+	fInitialSize.y = 600.0f * screenScalingFactor;
34
+	//fShape = sf::RectangleShape(fSize);
35
+	fShape = sf::RectangleShape(DEFAUMT_SIMZE);
18 36
 
19
-	fShape = sf::CircleShape(fWindow->getSize().x / 2);
20 37
 	fShape.setFillColor(sf::Color::White);
21 38
 
22
-	sf::Texture shapeTexture;
23
-	if (!shapeTexture.loadFromFile(fFilePath))
24
-	{
25
-		std::cout << "\n COULDNT LOAD PICTURE\n";
26
-	}
27
-	fShape.setTexture(&shapeTexture);
39
+	fShape.setTexture(&fTextureImage);
28 40
 }
29 41
 
30 42
 Perception PictureEnvironment::getPerception(int iX, int iY)
@@ -35,6 +47,19 @@ Perception PictureEnvironment::getPerception(int iX, int iY)
35 47
 	return vPerception;
36 48
 }
37 49
 
50
+void PictureEnvironment::makeAgents(int iNumber)
51
+{
52
+	Agent vTmpAgent;
53
+	fAgents = std::list<Agent>(iNumber);
54
+	fAgentsIterator = fAgents.begin();
55
+
56
+	for (int i = 0; i < iNumber; i++)
57
+	{
58
+		vTmpAgent = Agent(this);
59
+		fAgents.insert(fAgentsIterator, vTmpAgent);
60
+	}
61
+}
62
+
38 63
 bool PictureEnvironment::loop()
39 64
 {
40 65
 	if (fWindow->isOpen())
@@ -44,16 +69,15 @@ bool PictureEnvironment::loop()
44 69
 			if (fEvent.type == sf::Event::Closed)
45 70
 				fWindow->close();
46 71
 		}
47
-		fShape = sf::CircleShape(fWindow->getSize().x / 2);
48
-		fShape.setFillColor(sf::Color::White);
49 72
 
50
-		sf::Texture shapeTexture;
51
-		if (!shapeTexture.loadFromFile(fFilePath))
73
+		fAgentsIterator = fAgents.begin();
74
+		for (uint i = 0; i < fAgents.size(); i++)
52 75
 		{
53
-			std::cout << "\n COULDNT LOAD PICTURE\n";
76
+			fAgentsIterator->loop();
54 77
 		}
55
-		fShape.setTexture(&shapeTexture);
56 78
 
79
+		fTextureImage.loadFromImage(fImage);
80
+		fShape.setTexture(&fTextureImage);
57 81
 		fWindow->clear();
58 82
 		fWindow->draw(fShape);
59 83
 		fWindow->display();
@@ -64,4 +88,20 @@ bool PictureEnvironment::loop()
64 88
 		return false;
65 89
 	}
66 90
 	return true;
91
+}
92
+
93
+void PictureEnvironment::displayLockInput()
94
+{
95
+	fWindow->create(sf::VideoMode(fInitialSize.x, fInitialSize.y), "Last peek");
96
+	fWindow->clear();
97
+	fWindow->draw(fShape);
98
+	fWindow->display();
99
+	int i;
100
+	std::cin >> i;
101
+}
102
+
103
+void PictureEnvironment::paint(int iX, int iY, const sf::Color iColour)
104
+{
105
+	//std::cout << "juste avant : " << iX << " et " << iY;
106
+	fImage.setPixel(iX, iY, iColour);
67 107
 }

+ 13
- 6
src/Main.cpp Voir le fichier

@@ -1,6 +1,7 @@
1 1
 
2
-#include "Platform/Platform.hpp"
2
+#include <Agents/Agent.hpp>
3 3
 #include <Environment/PictureEnvironment.hpp>
4
+#include <unistd.h>
4 5
 
5 6
 int main()
6 7
 {
@@ -14,16 +15,22 @@ int main()
14 15
 	sf::RenderWindow window;
15 16
 
16 17
 	// in Windows at least, this must be called before creating the window
17
-	float screenScalingFactor = platform.getScreenScalingFactor(window.getSystemHandle());
18
+
18 19
 	// Use the screenScalingFactor
19
-	window.create(sf::VideoMode(800.0f * screenScalingFactor, 600.0f * screenScalingFactor), "SFML works!");
20
-	platform.setIcon(window.getSystemHandle());
21 20
 
22
-	PictureEnvironment vEnvironment(filepath, &window);
21
+	//
22
+
23
+	PictureEnvironment vEnvironment(filepath, &window, &platform);
23 24
 
24
-	while (vEnvironment.loop())
25
+	vEnvironment.makeAgents(100);
26
+	int i = 0;
27
+	while (i++ < 10000)
25 28
 	{
29
+		vEnvironment.loop();
30
+		usleep(30);
26 31
 	}
27 32
 
33
+	//vEnvironment.displayLockInput();
34
+
28 35
 	return 0;
29 36
 }

Chargement…
Annuler
Enregistrer