-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworldsim.cpp
More file actions
160 lines (142 loc) · 3.56 KB
/
worldsim.cpp
File metadata and controls
160 lines (142 loc) · 3.56 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
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
// worldsim.cpp
// worldsim main simulation file - loads world and objects
// Samuel Cagle
// Juston Li
// Xu Junjie, Kevin
// University of Oregon
// 2014-04-16
#include <cilk/cilk.h>
#include <iostream>
using std::cout;
using std::endl;
#include <string>
#include <SDL2/SDL.h>
#include "Components/Component.h"
#include "Components/NutrientComponent.h"
#include "Components/PositionComponent.h"
#include "units.h"
//#include "Systems/NutrientSystem.h"
#include "Systems/MovementSystem.h"
#include "Systems/OxygenSystem.h"
#include "Systems/RenderSystem.h"
#include "Systems/SurvivalSystem.h"
#include "Systems/SystemManager.h"
#include "Systems/WaterSystem.h"
#include "World.h"
using namespace gws;
const int screenWidth(800);
const int screenHeight(600);
const double FPSLock = 24;
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Texture* texture;
void createWindow() {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return;
}
window = SDL_CreateWindow("Genetic World Simulator",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
screenWidth, screenHeight,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN);
if (window == nullptr){
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
return;
}
/*
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == nullptr){
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
return ;
}
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, screenWidth, screenHeight); */
}
void destroyWindow() {
//SDL_DestroyRenderer(renderer);
//SDL_DestroyTexture(texture);
SDL_DestroyWindow(window);
SDL_Quit();
}
void createEntities(World& world, int num_ents) {
world.reserve(num_ents*2); /*Find out why it needs *2*/
cout << "Creating entities...\n";
// Create random entities;
int randEntity;
for (size_t i =0; i < num_ents; ++i) {
randEntity = rand() % 7;
switch (randEntity) {
case 0:
case 1:
case 2:
world.addRandomLake();
break;
case 3:
world.addRandomAnimal();
break;
case 4:
case 5:
case 6:
world.addRandomPlant();
break;
}
}
// Entities should be added to the world here
}
void runWorld(World& world) {
double frames = 0;
cout << "Running world...\n";
// Simulation loop should be here
bool quit = false;
SDL_Event event;
double start = omp_get_wtime();
while (!quit) {
while(SDL_PollEvent(&event)) {
//If the user has Xed out the window
if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.scancode == SDL_SCANCODE_ESCAPE)) {
//Quit the program
quit = true;
}
}
#ifdef FPS_LOCK
double SPF = 1/FPSLock;
double start = omp_get_wtime();
world.runSystems();
double end = omp_get_wtime();
double secondsElapsed = end - start;
double sleepTime = SPF-secondsElapsed;
if (sleepTime > 0 ) {
SDL_Delay(sleepTime*1000);
}
#else
world.runSystems();
#endif
frames++;
}
double end = omp_get_wtime();
double time = end - start;
double FPS = frames / time;
cout << FPS << " FPS";
}
void teardown()
{
cout << "Tearing down...\n";
}
int main (int argc, char** argv)
{
// Initialize SDL and create window
createWindow();
// Initialize the world
int entities = 5000; //Default 1000
World world(window, screenWidth, screenHeight);
if(argv[1] != NULL){
entities = atoi(argv[1]);
}
createEntities(world, entities);
// Run simulation
runWorld(world);
// Teardown
teardown();
destroyWindow(); // Destroy SDL constructs
return 0;
}