-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathmain.cpp
More file actions
131 lines (110 loc) · 3.25 KB
/
main.cpp
File metadata and controls
131 lines (110 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <SFML/Graphics.hpp>
#include <vector>
#include <list>
#include <fstream>
#include <unistd.h>
#include "colony.hpp"
#include "config.hpp"
#include "display_manager.hpp"
std::string getCurrentPath()
{
std::string path;
static constexpr size_t PATH_SIZE = 1024;
path.resize(PATH_SIZE);
#if defined(__unix__)
size_t pos = readlink("/proc/self/exe", &path[0], PATH_SIZE);
path.resize(pos);
pos = path.rfind("/");
path.resize(++pos);
#endif
#if defined(_WIN32)
char currentPath[PATH_SIZE];
_getcwd(currentPath, sizeof(currentPath));
path = std::string(currentPath);
path += "\\";
#endif
return path;
}
void loadUserConf()
{
std::ifstream conf_file(getCurrentPath() + "conf.txt");
if (conf_file) {
conf_file >> Conf::WIN_WIDTH;
conf_file >> Conf::WIN_HEIGHT;
conf_file >> Conf::ANTS_COUNT;
}
else {
std::cout << "Couldn't find 'conf.txt', loading default" << std::endl;
}
}
int main()
{
Conf::loadTextures();
loadUserConf();
Conf::COLONY_POSITION = sf::Vector2f(Conf::WIN_WIDTH * 0.5f, Conf::WIN_HEIGHT * 0.5f);
sf::ContextSettings settings;
settings.antialiasingLevel = 4;
sf::RenderWindow window(sf::VideoMode(Conf::WIN_WIDTH, Conf::WIN_HEIGHT), "AntSim", sf::Style::Default, settings);
window.setFramerateLimit(60);
World world(Conf::WORLD_WIDTH, Conf::WORLD_HEIGHT);
Colony colony(Conf::COLONY_POSITION.x, Conf::COLONY_POSITION.y, Conf::ANTS_COUNT);
for (uint32_t i(0); i < 64; ++i) {
float angle = float(i) / 64.0f * (2.0f * PI);
world.addMarker(colony.position + 16.0f * sf::Vector2f(cos(angle), sin(angle)), Mode::ToHome, 10.0f, true);
}
DisplayManager display_manager(window, window, world, colony);
sf::Vector2f last_clic;
sf::Image food_map;
if (food_map.loadFromFile("map.bmp")) {
for (uint32_t x(0); x < food_map.getSize().x; ++x) {
for (uint32_t y(0); y < food_map.getSize().y; ++y) {
const sf::Vector2f position = float(world.markers.cell_size) * sf::Vector2f(to<float>(x), to<float>(y));
if (food_map.getPixel(x, y).g > 100) {
world.addFoodAt(position.x, position.y, 5);
} else if (food_map.getPixel(x, y).r > 100) {
world.addWall(position);
}
}
}
}
while (window.isOpen())
{
display_manager.processEvents();
// Add food on clic
if (display_manager.clic) {
const sf::Vector2i mouse_position = sf::Mouse::getPosition(window);
const sf::Vector2f world_position = display_manager.displayCoordToWorldCoord(sf::Vector2f(to<float>(mouse_position.x), to<float>(mouse_position.y)));
const float clic_min_dist = 2.0f;
if (getLength(world_position - last_clic) > clic_min_dist) {
if (display_manager.wall_mode) {
world.addWall(world_position);
}
else if (display_manager.remove_wall) {
world.removeWall(world_position);
}
else {
world.addFoodAt(world_position.x, world_position.y, 20);
}
last_clic = world_position;
}
}
const float dt = 0.016f;
if (!display_manager.pause) {
colony.update(dt, world);
world.update(dt);
}
window.clear(sf::Color(94, 87, 87));
display_manager.draw();
window.display();
}
// Free textures
Conf::freeTextures();
return 0;
}
#if defined(_WIN32)
#include <windows.h>
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline,
int cmdshow) {
return main();
}
#endif