You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.4 KiB

9 years ago
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
6 years ago
#include <algorithm>
#include <iterator>
#include <iostream>
#include <functional>
9 years ago
#include <random>
#include "Node.h"
#include <thread>
#include <stack>
9 years ago
6 years ago
const int WINDOW_X = Node::x_bound;
const int WINDOW_Y = Node::y_bound;
9 years ago
float elap_time() {
8 years ago
static std::chrono::time_point<std::chrono::system_clock> start;
static bool started = false;
if (!started) {
start = std::chrono::system_clock::now();
started = true;
9 years ago
}
8 years ago
std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_time = now - start;
return static_cast<float>(elapsed_time.count());
9 years ago
}
void updateRange(std::vector<Node> *node_vec, int start_range_, int end_range_) {
for (int i = start_range_; i < end_range_; i++) {
node_vec->operator[](i).Update(node_vec);
}
}
6 years ago
bool inTriangle(sf::Vector2f p1, sf::Vector2f p2, sf::Vector2f p3, sf::Vector2f p){
6 years ago
std::vector<float> v;
float alpha = ((p2.y - p3.y)*(p.x - p3.x) + (p3.x - p2.x)*(p.y - p3.y)) /
((p2.y - p3.y)*(p1.x - p3.x) + (p3.x - p2.x)*(p1.y - p3.y));
v.push_back(alpha);
6 years ago
float beta = ((p3.y - p1.y)*(p.x - p3.x) + (p1.x - p3.x)*(p.y - p3.y)) /
((p2.y - p3.y)*(p1.x - p3.x) + (p3.x - p2.x)*(p1.y - p3.y));
v.push_back(beta);
float gamma = 1.0f - alpha - beta;
v.push_back(gamma);
return std::all_of(v.cbegin(), v.cend(), [](int i){ return i > 0; });
}
bool inCircle(sf::Vector2f origin, float r, sf::Vector2f p) {
return sqrt(pow(abs(origin.x - p.x), 2) + pow(abs(origin.y - p.y), 2)) < r;
}
float distFromCen(sf::Vector2f origin, sf::Vector2f p) {
return sqrt(pow(abs(origin.x - p.x), 2) + pow(abs(origin.y - p.y), 2));
}
9 years ago
6 years ago
int main() {
std::mt19937 rng(time(NULL));
std::uniform_int_distribution<int> rgen(0, 19);
9 years ago
6 years ago
srand(time(NULL));
9 years ago
std::vector<Node> node_vec;
// Init nodes, random value, push to front_stack
9 years ago
for (int x = 0; x < Node::x_bound; x++) {
for (int y = 0; y < Node::y_bound; y++) {
6 years ago
Node node(sf::Vector2i(x, y));
int varx = rand()%(20-3 + 1) + 3;
int vary = rand()%(20-3 + 1) + 3;
// 11/3 is interesting
//
if ((x % 11 == 0) || (y % 3 == 0)) {
node.Revive();
node.b_r = 205;
node.b_g = 190;
node.b_b = 80;
node.b_a = 255;
// bool in_circle = inCircle(sf::Vector2f(250,250), 186, sf::Vector2f(x,y));
// if (in_circle){
// node.Revive();
// float dist = distFromCen(sf::Vector2f(250,250), sf::Vector2f(x,y));
// node.b_r = std::min(255.0f, 90 + dist);
// node.b_g = 190 - std::min(140.0f, dist);
// node.b_b = 80;
// node.b_a = 255;
// }
9 years ago
}
6 years ago
node_vec.push_back(node);
9 years ago
}
}
// Init window, and loop data
6 years ago
sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "Conway");
window.setSize(sf::Vector2u(1200, 1200));
// window.setPosition(sf::Vector2i(0, 1000));
9 years ago
float step_size = 0.0005f;
double frame_time = 0.0, elapsed_time = 0.0, delta_time = 0.0, accumulator_time = 0.0, current_time = 0.0;
int frame_count = 0;
9 years ago
std::stack<std::thread> thread_stack;
9 years ago
sf::Uint8* pixel_array = new sf::Uint8[WINDOW_X * WINDOW_Y * 4];
sf::Texture texture;
texture.create(WINDOW_X, WINDOW_Y);
sf::Sprite sprite(texture);
9 years ago
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
// Time keeping
9 years ago
elapsed_time = elap_time();
delta_time = elapsed_time - current_time;
current_time = elapsed_time;
if (delta_time > 0.02f)
delta_time = 0.02f;
accumulator_time += delta_time;
while ((accumulator_time - step_size) >= step_size) {
accumulator_time -= step_size;
// Do nothing, FPS tied update()
9 years ago
}
for (int i = 0; i < 12; i++) {
thread_stack.emplace(updateRange, &node_vec, (node_vec.size() / 12)* i, (node_vec.size() / 12)* (i + 1));
9 years ago
}
while (!thread_stack.empty()) {
thread_stack.top().join();
thread_stack.pop();
}
9 years ago
for (int i = 0; i < node_vec.size(); i++) {
node_vec[i].ShiftState();
6 years ago
node_vec[i].Dim();
pixel_array[i * 4] = node_vec[i].r; // R?
pixel_array[i * 4 + 1] = node_vec[i].g; // G?
pixel_array[i * 4 + 2] = node_vec[i].b; // B?
pixel_array[i * 4 + 3] = node_vec[i].a; // A?
9 years ago
}
window.clear();
texture.update(pixel_array);
window.draw(sprite);
9 years ago
window.display();
}
return 0;
}