Skip to content

Commit

Permalink
Ability to pause sim
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasMagnus committed Sep 13, 2022
1 parent ff44fc3 commit d3b4be2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 10 additions & 0 deletions samples/extensions/khr/conway/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Conway : public cl::sdk::InteropWindow {
device_id,
device_type
}
, animating(true)
{}

protected:
Expand Down Expand Up @@ -88,6 +89,7 @@ class Conway : public cl::sdk::InteropWindow {

DoubleBuffer<cl::ImageGL> cl_images;
cl::vector<cl::Memory> interop_resources;
bool animating;
};

inline bool checkError(const char* Title)
Expand Down Expand Up @@ -301,6 +303,7 @@ void Conway::initializeCL()

void Conway::updateScene()
{
if (animating) {
auto conway =
cl::KernelFunctor<cl::ImageGL, cl::ImageGL, cl_float2>{ cl_program,
"conway" };
Expand All @@ -323,6 +326,7 @@ void Conway::updateScene()
// Swap front and back buffer handles
std::swap(cl_images.front, cl_images.back);
std::swap(gl_images.front, gl_images.back);
}
}

void Conway::render()
Expand Down Expand Up @@ -351,6 +355,12 @@ void Conway::event(const sf::Event& event)
switch (event.type)
{
case sf::Event::Closed: close(); break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Key::Space)
{
animating = !animating;
}
break;
}
}

Expand Down
11 changes: 10 additions & 1 deletion samples/extensions/khr/nbody/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class NBody : public cl::sdk::InteropWindow {
z_abs_range(32.f), mass_min(100.f), mass_max(500.f),
RMB_pressed(false),
dist(std::max({ x_abs_range, y_abs_range, z_abs_range }) * 3), phi(0),
theta(0), needMatrixReset(true)
theta(0), needMatrixReset(true), animating(true)
{}

protected:
Expand Down Expand Up @@ -115,6 +115,7 @@ class NBody : public cl::sdk::InteropWindow {
sf::Vector2<int> mousePos; // Variables to enable dragging
float dist, phi, theta; // Mouse polar coordinates
bool needMatrixReset; // Whether matrices need to be reset in shaders
bool animating;

void
mouseDrag(const sf::Event::MouseMoveEvent& event); // Handle mouse dragging
Expand Down Expand Up @@ -324,6 +325,7 @@ void NBody::initializeCL()

void NBody::updateScene()
{
if (animating){
auto nbody = cl::KernelFunctor<cl::BufferGL, cl::BufferGL, cl::Buffer,
cl_uint, cl_float>{ cl_program, "nbody" };
cl::Event acquire, release;
Expand All @@ -345,6 +347,7 @@ void NBody::updateScene()
// Swap front and back buffer handles
cl_pos_mass.swap();
gl_pos_mass.swap();
}
}

void NBody::render()
Expand Down Expand Up @@ -388,6 +391,12 @@ void NBody::event(const sf::Event& event)
checkError("glViewport(0, 0, getSize().x, getSize().y)");
needMatrixReset = true; // projection matrix need to be recalculated
break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Key::Space)
{
animating = !animating;
}
break;
case sf::Event::MouseButtonPressed:
if (event.mouseButton.button == sf::Mouse::Button::Right)
{
Expand Down

0 comments on commit d3b4be2

Please sign in to comment.