That actually sped it up quite a bit

master
MitchellHansen 9 years ago
parent 93b44aaaee
commit 1c4dafb62b

@ -33,19 +33,19 @@ void Node::Update(std::vector<Node> *node_vec) {
// x , y - 1 // x , y - 1
// x - 1 , y // x - 1 , y
if (position.x < x_bound - 1 && position.x > -1 && position.y + 1 < y_bound - 1 && position.y + 1 > -1) { if (position.x < x_bound - 1 && position.x > -1 && position.y + 1 < y_bound - 1 && position.y + 1 > -1) {
if (node_vec->at(multi_to_linear(sf::Vector2i(position.x, position.y + 1))).CurrentState()) if (node_vec->operator[](multi_to_linear(sf::Vector2i(position.x, position.y + 1))).CurrentState())
neighbors++; neighbors++;
} }
if (position.x + 1 < x_bound - 1 && position.x + 1 > -1 && position.y < y_bound - 1 && position.y > -1) { if (position.x + 1 < x_bound - 1 && position.x + 1 > -1 && position.y < y_bound - 1 && position.y > -1) {
if (node_vec->at(multi_to_linear(sf::Vector2i(position.x + 1, position.y))).CurrentState()) if (node_vec->operator[](multi_to_linear(sf::Vector2i(position.x + 1, position.y))).CurrentState())
neighbors++; neighbors++;
} }
if (position.x < x_bound - 1 && position.x > -1 && position.y - 1 < y_bound - 1 && position.y - 1 > -1) { if (position.x < x_bound - 1 && position.x > -1 && position.y - 1 < y_bound - 1 && position.y - 1 > -1) {
if (node_vec->at(multi_to_linear(sf::Vector2i(position.x, position.y - 1))).CurrentState()) if (node_vec->operator[](multi_to_linear(sf::Vector2i(position.x, position.y - 1))).CurrentState())
neighbors++; neighbors++;
} }
if (position.x - 1 < x_bound - 1 && position.x - 1 > -1 && position.y < y_bound - 1 && position.y > -1) { if (position.x - 1 < x_bound - 1 && position.x - 1 > -1 && position.y < y_bound - 1 && position.y > -1) {
if (node_vec->at(multi_to_linear(sf::Vector2i(position.x - 1, position.y))).CurrentState()) if (node_vec->operator[](multi_to_linear(sf::Vector2i(position.x - 1, position.y))).CurrentState())
neighbors++; neighbors++;
} }
@ -55,19 +55,19 @@ void Node::Update(std::vector<Node> *node_vec) {
// x - 1, y - 1 // x - 1, y - 1
if (position.x + 1 < x_bound - 1 && position.x + 1 > -1 && position.y + 1 < y_bound - 1 && position.y + 1 > -1) { if (position.x + 1 < x_bound - 1 && position.x + 1 > -1 && position.y + 1 < y_bound - 1 && position.y + 1 > -1) {
if (node_vec->at(multi_to_linear(sf::Vector2i(position.x + 1, position.y + 1))).CurrentState()) if (node_vec->operator[](multi_to_linear(sf::Vector2i(position.x + 1, position.y + 1))).CurrentState())
neighbors++; neighbors++;
} }
if (position.x + 1 < x_bound - 1 && position.x + 1 > -1 && position.y - 1 < y_bound - 1 && position.y - 1 > -1) { if (position.x + 1 < x_bound - 1 && position.x + 1 > -1 && position.y - 1 < y_bound - 1 && position.y - 1 > -1) {
if (node_vec->at(multi_to_linear(sf::Vector2i(position.x + 1, position.y - 1))).CurrentState()) if (node_vec->operator[](multi_to_linear(sf::Vector2i(position.x + 1, position.y - 1))).CurrentState())
neighbors++; neighbors++;
} }
if (position.x - 1 < x_bound - 1 && position.x - 1 > -1 && position.y + 1 < y_bound - 1 && position.y + 1 > -1) { if (position.x - 1 < x_bound - 1 && position.x - 1 > -1 && position.y + 1 < y_bound - 1 && position.y + 1 > -1) {
if (node_vec->at(multi_to_linear(sf::Vector2i(position.x - 1, position.y + 1))).CurrentState()) if (node_vec->operator[](multi_to_linear(sf::Vector2i(position.x - 1, position.y + 1))).CurrentState())
neighbors++; neighbors++;
} }
if (position.x - 1 < x_bound - 1 && position.x - 1 > -1 && position.y - 1 < y_bound - 1 && position.y - 1 > -1) { if (position.x - 1 < x_bound - 1 && position.x - 1 > -1 && position.y - 1 < y_bound - 1 && position.y - 1 > -1) {
if (node_vec->at(multi_to_linear(sf::Vector2i(position.x - 1, position.y - 1))).CurrentState()) if (node_vec->operator[](multi_to_linear(sf::Vector2i(position.x - 1, position.y - 1))).CurrentState())
neighbors++; neighbors++;
} }

@ -4,6 +4,7 @@
#include <random> #include <random>
#include <windows.h> #include <windows.h>
#include "Node.h" #include "Node.h"
#include <stack>
const int WINDOW_X = 600; const int WINDOW_X = 600;
const int WINDOW_Y = 800; const int WINDOW_Y = 800;
@ -29,21 +30,21 @@ int main() {
std::uniform_int_distribution<int> rgen(0, 4); std::uniform_int_distribution<int> rgen(0, 4);
std::vector<Node> node_vec; std::vector<Node> node_vec;
std::stack<Node*>* front_stack = new std::stack<Node*>();
std::stack<Node*>* back_stack = new std::stack<Node*>();
// Init nodes, random value, push to front_stack
for (int x = 0; x < Node::x_bound; x++) { for (int x = 0; x < Node::x_bound; x++) {
for (int y = 0; y < Node::y_bound; y++) { for (int y = 0; y < Node::y_bound; y++) {
node_vec.push_back(Node(sf::Vector2i(x, y))); node_vec.push_back(Node(sf::Vector2i(x, y)));
if (rgen(rng) == 1) { if (rgen(rng) == 1) {
node_vec.at(node_vec.size() - 1).Revive(); node_vec.at(node_vec.size() - 1).Revive();
front_stack->push(&node_vec.at(node_vec.size() - 1));
} }
} }
} }
// Spites for drawing, probably where the biggest slowdown is
for (int i = 0; i < node_vec.size(); i++) {
node_vec.at(i).ShiftState();
}
sf::RectangleShape live_node; sf::RectangleShape live_node;
sf::RectangleShape dead_node; sf::RectangleShape dead_node;
@ -54,22 +55,25 @@ int main() {
dead_node.setSize(sf::Vector2f(WINDOW_X / Node::x_bound, WINDOW_Y / Node::y_bound)); dead_node.setSize(sf::Vector2f(WINDOW_X / Node::x_bound, WINDOW_Y / Node::y_bound));
// Init window, and loop data
sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "Classic Games"); sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "Classic Games");
float step_size = 0.0005f; 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; 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;
while (window.isOpen()) { while (window.isOpen()) {
std::cout << elapsed_time / frame_count << "\n";
sf::Event event; sf::Event event;
while (window.pollEvent(event)) { while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) if (event.type == sf::Event::Closed)
window.close(); window.close();
} }
// Time keeping
elapsed_time = elap_time(); elapsed_time = elap_time();
delta_time = elapsed_time - current_time; delta_time = elapsed_time - current_time;
current_time = elapsed_time; current_time = elapsed_time;
@ -80,12 +84,12 @@ int main() {
while ((accumulator_time - step_size) >= step_size) { while ((accumulator_time - step_size) >= step_size) {
accumulator_time -= step_size; accumulator_time -= step_size;
// Do nothing, FPS tied update()
} }
// Implicit dead node color
window.clear(sf::Color::Black); window.clear(sf::Color::Black);
for (int i = 0; i < node_vec.size(); i++) { for (int i = 0; i < node_vec.size(); i++) {
node_vec.at(i).Update(&node_vec); node_vec.at(i).Update(&node_vec);
} }
@ -105,6 +109,7 @@ int main() {
} }
} }
frame_count++;
window.display(); window.display();

Loading…
Cancel
Save