-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
168 lines (136 loc) · 5.31 KB
/
main.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Authors: Daniel Hayden & Frank Rooney
// Date: 17/11/17
// Description: This file starts the program and contorls the program
// it creates the Grid Fish and Shark Objects and preforms logic on them
// to place them in their correct position on each new chronon
#include <iostream>
#include "Grid.h"
#include "Fish.h"
#include "Shark.h"
#include <SFML/Graphics.hpp>
#include "Config.h"
#include<omp.h> //OpenMP added
using namespace std;
int main()
{
#pragma omp parallel for num_threads(THREADS)
sf::RenderWindow window(sf::VideoMode(1360, 560), "Wator Ecosystem Simulator");//1285, 560
Grid grid; //Create a single grid sprite object
Fish fish; //Create a single fish sprite object
Shark shark; //Create a single shark sprite object
sf::Sprite GRID[GRID_ROWS][GRID_COLS];
srand (time (0)); //Makes rand() more random, only needs to be called once
//Fill FISH array wih -1's
for (int i=0; i<GRID_ROWS; i++){
for (int j=0; j<GRID_COLS; j++) {
fish.FISH[i][j]=-1;
}
}
//Fill FISHMOVE array wih -1's
for (int i=0; i<GRID_ROWS; i++){
for (int j=0; j<GRID_COLS; j++) {
fish.FISHMOVE[i][j]=-1;
}
}
//Enter fish notated by 1's at random locations into the FISH array
for (int i=0; i<nFish; i++){
fish.putFishOnMapAtRandomLocations();
}
//Fill SHARKS array wih -1's
for (int i=0; i<GRID_ROWS; i++){
for (int j=0; j<GRID_COLS; j++) {
shark.SHARKS[i][j]=-1;
}
}
//Fill SHARKSMOVE array wih -1's
for (int i=0; i<GRID_ROWS; i++){
for (int j=0; j<GRID_COLS; j++) {
shark.SHARKSMOVE[i][j]=-1;
}
}
//Enter shark notated by 1's at random locations into the SHARKS array
for (int i=0; i<nSharks; i++){
shark.putSharksOnMapAtRandomLocations();
}
//Do math to set FPS
// Speed (chronons) can be set in Config.h
sf::Time timePerFrame = sf::seconds(1.0f);
sf::Time timeSinceLastUpdate = sf::Time::Zero;
//The clock object keeps the time.
sf::Clock clock;
clock.restart();
int timeCounter=0;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
//Get the time since last update and restart the clock
timeSinceLastUpdate += clock.restart();
//Update every every chronon
//Only update when on new chronon
if (timeSinceLastUpdate > timePerFrame)
{
window.clear();
for (int i=0; i<GRID_ROWS; i++){
for (int j=0; j<GRID_COLS; j++) {
//Sharks eat fish if on same location
if(shark.SHARKS[i][j]!=-1 && fish.FISH[i][j]!=-1){
fish.FISH[i][j]=-1;
}
fish.removeStarvedFish(i, j);
//Fill GRID array with grid, shark and fish sprites
if(fish.FISH[i][j]==-1){
GRID[i][j]=grid.getGridSprite();
}
if (shark.SHARKS[i][j]!=-1){
GRID[i][j]=shark.getSharkSprite();
}
if (fish.FISH[i][j]!=-1){
GRID[i][j]=fish.getFishSprite();
}
//Move Fish
if(fish.FISH[i][j]!=-1 && fish.FISHMOVE[i][j]!=1){
fish.FISH[i][j]=timeCounter;
fish.removeStarvedFish(i, j);
fish.moveFish(fish.findMoveLocation(i,j), i, j, timeCounter);
}
//Move Sharks
if(shark.SHARKS[i][j]!=-1 && shark.SHARKSMOVE[i][j]!=1){
shark.SHARKS[i][j]=timeCounter;
shark.moveShark(shark.findMoveLocation(i,j), i, j, timeCounter);
}
}
}
//This loop preforms the same tasks as the one above but is needed
//to be called again to update the grid after the fish and shark movements
//are made. (updates the entire grid quicker to stop an issue where old fish/sharks were displayed)
for (int i=0; i<GRID_ROWS; i++){
for (int j=0; j<GRID_COLS; j++) {
fish.removeStarvedFish(i, j);
if(fish.FISH[i][j]==-1){
GRID[i][j]=grid.getGridSprite();
}
if (shark.SHARKS[i][j]!=-1){
GRID[i][j]=shark.getSharkSprite();
}
if (fish.FISH[i][j]!=-1){
GRID[i][j]=fish.getFishSprite();
}
//Set position of sprite to make grid and darw
GRID[i][j].setPosition(j * 40,i * 40);
window.draw(GRID[i][j]);
fish.FISHMOVE[i][j]=-1;
shark.SHARKSMOVE[i][j]=-1;
}
}
timeCounter++;
window.display();
//Reset the timeSinceLastUpdate to 0
timeSinceLastUpdate = sf::Time::Zero;
}
}
}