@@ -46,17 +46,18 @@ class Conway : public cl::sdk::InteropWindow {
4646 explicit Conway (int width, int height, bool fullscreen,
4747 cl_uint platform_id = 0 , cl_uint device_id = 0 ,
4848 cl_bitfield device_type = CL_DEVICE_TYPE_DEFAULT)
49- : InteropWindow{
50- sf::VideoMode (width, height),
51- " Conway's Game of Life" ,
52- fullscreen ? sf::Style::Fullscreen : sf::Style::Default,
53- sf::ContextSettings{ 0 , 0 , 0 , // Depth, Stencil, AA
54- 3 , 3 , // OpenGL version
55- sf::ContextSettings::Attribute::Core },
56- platform_id,
57- device_id,
58- device_type
59- }
49+ : InteropWindow{ sf::VideoMode (width, height),
50+ " Conway's Game of Life" ,
51+ fullscreen ? sf::Style::Fullscreen
52+ : sf::Style::Default,
53+ sf::ContextSettings{
54+ 0 , 0 , 0 , // Depth, Stencil, AA
55+ 3 , 3 , // OpenGL version
56+ sf::ContextSettings::Attribute::Core },
57+ platform_id,
58+ device_id,
59+ device_type },
60+ animating (true )
6061 {}
6162
6263protected:
@@ -88,6 +89,7 @@ class Conway : public cl::sdk::InteropWindow {
8889
8990 DoubleBuffer<cl::ImageGL> cl_images;
9091 cl::vector<cl::Memory> interop_resources;
92+ bool animating;
9193};
9294
9395inline bool checkError (const char * Title)
@@ -301,28 +303,32 @@ void Conway::initializeCL()
301303
302304void Conway::updateScene ()
303305{
304- auto conway =
305- cl::KernelFunctor<cl::ImageGL, cl::ImageGL, cl_float2>{ cl_program,
306- " conway" };
307- cl::Event acquire, release;
306+ if (animating)
307+ {
308+ auto conway =
309+ cl::KernelFunctor<cl::ImageGL, cl::ImageGL, cl_float2>{ cl_program,
310+ " conway" };
311+ cl::Event acquire, release;
308312
309- queue.enqueueAcquireGLObjects (&interop_resources, nullptr , &acquire);
313+ queue.enqueueAcquireGLObjects (&interop_resources, nullptr , &acquire);
310314
311- conway (cl::EnqueueArgs{ queue, cl::NDRange{ getSize ().x , getSize ().y } },
312- cl_images.front , cl_images.back ,
313- cl_float2{ 1 .f / getSize ().x , 1 .f / getSize ().y });
315+ conway (
316+ cl::EnqueueArgs{ queue, cl::NDRange{ getSize ().x , getSize ().y } },
317+ cl_images.front , cl_images.back ,
318+ cl_float2{ 1 .f / getSize ().x , 1 .f / getSize ().y });
314319
315- queue.enqueueReleaseGLObjects (&interop_resources, nullptr , &release);
320+ queue.enqueueReleaseGLObjects (&interop_resources, nullptr , &release);
316321
317- // Wait for all OpenCL commands to finish
318- if (!cl_khr_gl_event_supported)
319- cl::finish ();
320- else
321- release.wait ();
322+ // Wait for all OpenCL commands to finish
323+ if (!cl_khr_gl_event_supported)
324+ cl::finish ();
325+ else
326+ release.wait ();
322327
323- // Swap front and back buffer handles
324- std::swap (cl_images.front , cl_images.back );
325- std::swap (gl_images.front , gl_images.back );
328+ // Swap front and back buffer handles
329+ std::swap (cl_images.front , cl_images.back );
330+ std::swap (gl_images.front , gl_images.back );
331+ }
326332}
327333
328334void Conway::render ()
@@ -351,6 +357,12 @@ void Conway::event(const sf::Event& event)
351357 switch (event.type )
352358 {
353359 case sf::Event::Closed: close (); break ;
360+ case sf::Event::KeyPressed:
361+ if (event.key .code == sf::Keyboard::Key::Space)
362+ {
363+ animating = !animating;
364+ }
365+ break ;
354366 }
355367}
356368
0 commit comments