diff --git a/src/main.cpp b/src/main.cpp index 41f59a6..8e27792 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -73,8 +73,30 @@ void clear_nodes(sf::Uint8 *nodes, sf::Vector2i dimensions) { } } -// Set up the copy pattern to load at a position, mouse click? -// +std::unique_ptr generate_pattern_texture(pattern_info pattern) { + + sf::Vector2i dimensions = pattern.dimensions; + + std::vector pixels(dimensions.x * dimensions.y * 4, 0); + + for (int i = 0; i < dimensions.x * dimensions.y; i += 4) { + + if (pattern.nodes[i] != 0) { + pixels.at(i + 0) = 125; + pixels.at(i + 1) = 125; + pixels.at(i + 2) = 125; + pixels.at(i + 3) = 125; + } + } + + + std::unique_ptr texture(new sf::Texture); + texture->create(dimensions.x, dimensions.y); + texture->update(pixels.data()); + + return std::move(texture); + +} int main() { @@ -119,6 +141,10 @@ int main() { Decoder d; std::vector pattern_list = d.getPatternList(); + pattern_info pattern_info; + sf::Sprite pattern_sprite; + sf::Texture pattern_texture; + sf::Clock sf_delta_clock; fps_counter render_fps; @@ -141,38 +167,55 @@ int main() { if (event.type == sf::Event::Closed) { window.close(); } - if (event.type == sf::Event::MouseButtonPressed) { - + else if (event.type == sf::Event::MouseButtonPressed) { if (event.mouseButton.button == sf::Mouse::Right) { mouse_state = PRESSED; mouse_position = sf::Mouse::getPosition(); } + else if (event.mouseButton.button == sf::Mouse::Middle) { + mouse_position = sf::Mouse::getPosition(); + copy_pattern(nodes, sf::Vector2i(WINDOW_X, WINDOW_Y), static_cast(mouse_position), pattern_info); + 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"); + } } - if (event.type == sf::Event::MouseButtonReleased) { - mouse_state = DEPRESSED; + else if (event.type == sf::Event::MouseButtonReleased) { + if (event.mouseButton.button == sf::Mouse::Right) { + mouse_state = DEPRESSED; + } } - - if (event.type == sf::Event::MouseWheelScrolled) { + else if (event.type == sf::Event::MouseWheelScrolled) { zoom_level -= event.mouseWheelScroll.delta / 10; sf::View v = window.getView(); v.setSize(static_cast(window.getSize()) * zoom_level); window.setView(v); } - } + else if (event.type == sf::Event::MouseMoved) { + if (mouse_state == PRESSED) { - if (mouse_state == PRESSED) { - - sf::Vector2i mouse_delta = mouse_position - sf::Mouse::getPosition(); - mouse_position -= mouse_delta; + sf::Vector2i mouse_delta = mouse_position - sf::Mouse::getPosition(); + mouse_position -= mouse_delta; + + sf::View v = window.getView(); + auto center = v.getCenter() + (static_cast(mouse_delta) * zoom_level); + v.setCenter(center); + window.setView(v); + + } + + + pattern_sprite.setPosition(mouse_position.x, mouse_position.y); + - sf::View v = window.getView(); - auto center = v.getCenter() + (static_cast(mouse_delta) * zoom_level); - v.setCenter(center); - window.setView(v); + } } + + elapsed_time = elap_time(); // Handle time delta_time = elapsed_time - current_time; current_time = elapsed_time; @@ -196,13 +239,7 @@ int main() { if (ImGui::SliderInt("Simulation Speed", &simulation_speed, 30, 500)) { window.setFramerateLimit(simulation_speed); } - if (ImGui::Button("One shot")) { - - std::cout << "sim" << std::endl; - /*std::cout << buffer_flip << std::endl; - cl.map_buffer("buffer_flip", sizeof(char), &buffer_flip);*/ - } cl.run_kernel("conways", image_resolution); cl.draw(&window); if (buffer_flip == 1) @@ -220,13 +257,27 @@ int main() { } if (ImGui::Button("Load Pattern")) { - clear_nodes(nodes, sf::Vector2i(WINDOW_X, WINDOW_Y)); - pattern_info p = d.decodePattern(pattern_list.at(pattern_number)); - copy_pattern(nodes, sf::Vector2i(WINDOW_X, WINDOW_Y), sf::Vector2u(100, 300), p); - 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"); + pattern_info = d.decodePattern(pattern_list.at(pattern_number)); + + sf::Vector2i dimensions = pattern_info.dimensions; + + sf::Uint8* pixels = new sf::Uint8[dimensions.x * dimensions.y * 4]{ 0 }; + + for (int i = 0; i < dimensions.x * dimensions.y; i += 4) { + + if (pattern_info.nodes[i] != 0) { + pixels[i + 0] = 0; + pixels[i + 1] = 0; + pixels[i + 2] = 255; + pixels[i + 3] = 255; + } + } + + if (pattern_texture.create(dimensions.x, dimensions.y)) { + pattern_texture.update(pixels); + pattern_sprite.setTexture(pattern_texture, true); + } + } if (ImGui::Button("Rerun")) { @@ -249,14 +300,10 @@ int main() { ImGui::Render(); - - - - - - // ImGui screws stuff up after the render, rendering a drawable resets it - window.draw(sf::CircleShape(0)); + window.draw(sf::CircleShape(100)); + + window.draw(pattern_sprite); window.display(); }