structural changes + a repo of a bunch of patterns to choose from
This commit is contained in:
144
src/Decoder.cpp
Normal file
144
src/Decoder.cpp
Normal file
@@ -0,0 +1,144 @@
|
||||
#include "Decoder.h"
|
||||
|
||||
|
||||
Decoder::Decoder() {
|
||||
|
||||
std::cout << "Loading patterns...";
|
||||
|
||||
for (std::experimental::filesystem::directory_entry p : std::experimental::filesystem::directory_iterator("../assets/patterns/")) {
|
||||
pattern_list.push_back(p.path().generic_string());
|
||||
}
|
||||
|
||||
std::cout << "Done" << std::endl;
|
||||
|
||||
}
|
||||
Decoder::~Decoder() {
|
||||
|
||||
}
|
||||
|
||||
pattern_info Decoder::decodePattern(std::string pattern) {
|
||||
|
||||
pattern_info info;
|
||||
|
||||
//for (int i = 0; i < dimensions.x * dimensions.y; i++) {
|
||||
// nodes[i] = 0;
|
||||
//}
|
||||
|
||||
|
||||
//std::ifstream file(filename);
|
||||
|
||||
//if (!file.is_open()) {
|
||||
// std::cout << "unable to open file" << std::endl;
|
||||
// return info;
|
||||
//}
|
||||
|
||||
//for (std::string line; std::getline(file, line); )
|
||||
//{
|
||||
// // Grab the header comments
|
||||
// if (line[0] == '#') {
|
||||
|
||||
// std::string data = line.substr(3, line.size() - 3);
|
||||
|
||||
// switch (line[1]) {
|
||||
|
||||
// case 'C': {
|
||||
// info.comments += data;
|
||||
// break;
|
||||
// }
|
||||
// case 'O': {
|
||||
// info.author += data;
|
||||
// break;
|
||||
// }
|
||||
// case 'N': {
|
||||
// info.title += data;
|
||||
// break;
|
||||
// }
|
||||
// default: {
|
||||
// std::cout << "Case : " << line[1] << " not supported" << std::endl;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// // grab the dimensions
|
||||
// else if (line[0] == 'x') {
|
||||
|
||||
// // Naively assume that it is in the exact form "x = <num>, y = <num>,"
|
||||
// std::stringstream ss(line);
|
||||
|
||||
// std::string temp;
|
||||
// ss >> temp >> temp >> temp;
|
||||
// info.dimensions.x = std::stoi(temp.substr(0, temp.size() - 1));
|
||||
|
||||
// ss >> temp >> temp >> temp;
|
||||
// info.dimensions.y = std::stoi(temp.substr(0, temp.size() - 1));
|
||||
// }
|
||||
|
||||
// // Decode the RLE
|
||||
// else {
|
||||
|
||||
// std::vector<std::vector<char>> grid;
|
||||
|
||||
// std::stringstream ss(line);
|
||||
// std::string token;
|
||||
|
||||
// while (std::getline(ss, token, '$')) {
|
||||
|
||||
// std::vector<char> char_line;
|
||||
// unsigned int token_pos = 0;
|
||||
// std::string tmp;
|
||||
|
||||
// while (token_pos < token.size()) {
|
||||
|
||||
// char status = -1;
|
||||
// if (token[token_pos] == 'b')
|
||||
// status = 0;
|
||||
// else if (token[token_pos] == 'o')
|
||||
// status = 1;
|
||||
// else if (token[token_pos] == '!')
|
||||
// break;
|
||||
// else
|
||||
// tmp += token[token_pos];
|
||||
|
||||
// token_pos++;
|
||||
|
||||
// if (status >= 0) {
|
||||
// if (tmp.empty()) {
|
||||
// char_line.push_back(status);
|
||||
// }
|
||||
// else {
|
||||
// int count = std::stoi(tmp);
|
||||
// for (int i = 0; i < count; i++) {
|
||||
// char_line.push_back(status);
|
||||
// }
|
||||
// tmp.clear();
|
||||
// }
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// grid.push_back(char_line);
|
||||
// }
|
||||
|
||||
// int y_mod = 200;
|
||||
// int x_mod = 200;
|
||||
// for (int y = 0; y < grid.size(); y++) {
|
||||
// for (int x = 0; x < grid.at(y).size(); x++) {
|
||||
// nodes[(y + y_mod) * dimensions.x + (x + x_mod)] = grid.at(y).at(x);
|
||||
// }
|
||||
// }
|
||||
|
||||
// }
|
||||
//}
|
||||
|
||||
//std::cout << info.author << std::endl;
|
||||
//std::cout << info.title << std::endl;
|
||||
//std::cout << info.comments << std::endl;
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
std::vector<std::string> Decoder::getPatternList() {
|
||||
|
||||
return pattern_list;
|
||||
|
||||
}
|
||||
@@ -303,9 +303,7 @@ int OpenCL::create_buffer(std::string buffer_name, cl_uint size, void* data, cl_
|
||||
if (buffer_map.count(buffer_name) > 0) {
|
||||
release_buffer(buffer_name);
|
||||
}
|
||||
|
||||
((char*)data)[10000];
|
||||
|
||||
|
||||
cl_mem buff = clCreateBuffer(
|
||||
context, flags,
|
||||
size, data, &error
|
||||
@@ -381,29 +379,37 @@ int OpenCL::set_kernel_arg(std::string kernel_name, int index, std::string buffe
|
||||
|
||||
bool OpenCL::load_config() {
|
||||
|
||||
std::cout << "Loading hardware config...";
|
||||
|
||||
std::ifstream input_file("device_config.bin", std::ios::binary | std::ios::in);
|
||||
|
||||
if (!input_file.is_open()) {
|
||||
std::cout << "No config file..." << std::endl;
|
||||
std::cout << "No config file found" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
device::packed_data data;
|
||||
input_file.read(reinterpret_cast<char*>(&data), sizeof(data));
|
||||
input_file.close();
|
||||
|
||||
std::cout << "config loaded, looking for device..." << std::endl;
|
||||
bool found = false;
|
||||
|
||||
for (auto d: device_list) {
|
||||
|
||||
if (memcmp(&d, &data, sizeof(device::packed_data)) == 0) {
|
||||
std::cout << "Found saved device" << std::endl;
|
||||
std::cout << "Found saved config" << std::endl;
|
||||
found = true;
|
||||
device_id = d.getDeviceId();
|
||||
platform_id = d.getPlatformId();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
input_file.close();
|
||||
if (!found) {
|
||||
std::cout << "No hardware matching config found" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
61
src/main.cpp
61
src/main.cpp
@@ -1,4 +1,3 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <random>
|
||||
@@ -8,6 +7,8 @@
|
||||
#include "OpenCL.h"
|
||||
#include "imgui/imgui-SFML.h"
|
||||
#include "imgui/imgui.h"
|
||||
#include <experimental/filesystem>
|
||||
#include "Decoder.h"
|
||||
|
||||
float elap_time() {
|
||||
static std::chrono::time_point<std::chrono::system_clock> start;
|
||||
@@ -28,16 +29,28 @@ const int WINDOW_Y = 1080;
|
||||
|
||||
void generate_nodes(sf::Uint8* nodes) {
|
||||
|
||||
int x_divisor = rand() % 49 + 1;
|
||||
int y_divisor = rand() % 10 + 1;
|
||||
|
||||
for (int i = 0; i < WINDOW_X * WINDOW_Y; i += 1) {
|
||||
|
||||
sf::Vector2i pos(i % WINDOW_X, i / WINDOW_X);
|
||||
|
||||
if ((pos.x % 5 == 0) || (pos.y % 8 == 0))
|
||||
if ((pos.x % x_divisor == 0) || (pos.y % y_divisor == 0))
|
||||
nodes[i] = 1;
|
||||
else
|
||||
nodes[i] = 0;
|
||||
}
|
||||
|
||||
for (int i = 0; i < WINDOW_X * WINDOW_Y; i += 1) {
|
||||
|
||||
sf::Vector2i pos(i % WINDOW_X, i / WINDOW_X);
|
||||
|
||||
if (rand() % 200 == 0)
|
||||
nodes[i] = 1;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct rle_info {
|
||||
@@ -57,6 +70,7 @@ rle_info load_rle(sf::Uint8* nodes, sf::Vector2i dimensions, std::string filenam
|
||||
nodes[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
std::ifstream file(filename);
|
||||
|
||||
if (!file.is_open()) {
|
||||
@@ -171,6 +185,9 @@ rle_info load_rle(sf::Uint8* nodes, sf::Vector2i dimensions, std::string filenam
|
||||
|
||||
int main() {
|
||||
|
||||
srand(time(NULL));
|
||||
Decoder d;
|
||||
|
||||
sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "conways-game-of-life-opencl");
|
||||
|
||||
int simulation_speed = 100;
|
||||
@@ -179,7 +196,7 @@ int main() {
|
||||
ImGui::SFML::Init(window);
|
||||
window.resetGLStates();
|
||||
|
||||
float physic_step = 0.166f;
|
||||
float physic_step = 0.0166f;
|
||||
float physic_time = 0.0f;
|
||||
double frame_time = 0.0, elapsed_time = 0.0, delta_time = 0.0, accumulator_time = 0.0, current_time = 0.0;
|
||||
|
||||
@@ -197,9 +214,9 @@ int main() {
|
||||
cl.create_buffer("image_res", sizeof(sf::Vector2i), &image_resolution);
|
||||
|
||||
sf::Uint8* nodes = new sf::Uint8[WINDOW_X * WINDOW_Y];
|
||||
//generate_nodes(nodes);
|
||||
generate_nodes(nodes);
|
||||
|
||||
load_rle(nodes, sf::Vector2i(WINDOW_X, WINDOW_Y), "../assets/puffer_breeder_1.rle");
|
||||
//load_rle(nodes, sf::Vector2i(WINDOW_X, WINDOW_Y), "../assets/snark.rle");
|
||||
cl.create_buffer("first_node_buffer", WINDOW_X * WINDOW_Y, (void*)nodes, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR);
|
||||
cl.create_buffer("second_node_buffer", WINDOW_X * WINDOW_Y, (void*)nodes, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR);
|
||||
|
||||
@@ -213,9 +230,10 @@ int main() {
|
||||
cl.set_kernel_arg("conways", 4, "buffer_flip");
|
||||
|
||||
sf::Clock sf_delta_clock;
|
||||
|
||||
|
||||
fps_counter render_fps;
|
||||
fps_counter physic_fps;
|
||||
|
||||
int c = 0;
|
||||
|
||||
while (window.isOpen())
|
||||
{
|
||||
@@ -249,20 +267,24 @@ int main() {
|
||||
elapsed_time = elap_time(); // Handle time
|
||||
delta_time = elapsed_time - current_time;
|
||||
current_time = elapsed_time;
|
||||
if (delta_time > 0.02f)
|
||||
delta_time = 0.02f;
|
||||
if (delta_time > 0.05f)
|
||||
delta_time = 0.05f;
|
||||
accumulator_time += delta_time;
|
||||
|
||||
while (accumulator_time >= physic_step) { // While the frame has sim time, update
|
||||
accumulator_time -= physic_step;
|
||||
physic_time += physic_step;
|
||||
//physic_fps.frame(accumulator_time);
|
||||
|
||||
|
||||
}
|
||||
|
||||
cl.run_kernel("conways", image_resolution);
|
||||
ImGui::SFML::Update(window, sf_delta_clock.restart());
|
||||
render_fps.frame(delta_time);
|
||||
|
||||
window.clear(sf::Color::White);
|
||||
|
||||
cl.run_kernel("conways", image_resolution);
|
||||
cl.draw(&window);
|
||||
|
||||
|
||||
@@ -272,7 +294,26 @@ int main() {
|
||||
window.setFramerateLimit(simulation_speed);
|
||||
}
|
||||
|
||||
if (ImGui::Button("Rerun")) {
|
||||
generate_nodes(nodes);
|
||||
cl.create_buffer("first_node_buffer", WINDOW_X * WINDOW_Y, (void*)nodes, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR);
|
||||
cl.create_buffer("second_node_buffer", WINDOW_X * WINDOW_Y, (void*)nodes, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR);
|
||||
cl.set_kernel_arg("conways", 2, "first_node_buffer");
|
||||
cl.set_kernel_arg("conways", 3, "second_node_buffer");
|
||||
}
|
||||
|
||||
const char* l[] = { "Apple", "Banana", "Cherry", "Kiwi", "Mango", "Orange", "Pineapple", "Strawberry", "Watermelon" };
|
||||
|
||||
if(ImGui::ListBox("asdf", &c, l, 9)) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
ImGui::End();
|
||||
|
||||
render_fps.draw();
|
||||
physic_fps.draw();
|
||||
|
||||
ImGui::Render();
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user