Skip to content

Commit

Permalink
Use a condition variable to wait for the gl_states in the render loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
j-jorge committed Feb 16, 2017
1 parent f45d857 commit cba0710
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 111 deletions.
6 changes: 0 additions & 6 deletions bear-engine/core/src/engine/code/game_local_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ void bear::engine::game_local_client::run()
if ( m_current_level == NULL )
load_level( m_game_description.start_level() );

m_screen->unset_pause();

run_level();

end_game();
Expand All @@ -143,8 +141,6 @@ void bear::engine::game_local_client::sleep()
m_sleep_status = m_status;
m_status = status_sleep;

m_screen->set_pause();

if ( m_current_level != NULL )
m_current_level->set_pause();
} // game_local_client::sleep()
Expand All @@ -158,8 +154,6 @@ void bear::engine::game_local_client::wake_up()
if ( m_status != status_sleep )
return;

m_screen->unset_pause();

if ( m_current_level != NULL )
m_current_level->unset_pause();

Expand Down
3 changes: 0 additions & 3 deletions bear-engine/core/src/visual/base_screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ namespace bear
public:
virtual ~base_screen() {}

virtual void set_pause() = 0;
virtual void unset_pause() = 0;

virtual void fullscreen( bool b ) = 0;
virtual claw::math::coordinate_2d<unsigned int> get_size() const = 0;
virtual claw::math::coordinate_2d<unsigned int>
Expand Down
74 changes: 21 additions & 53 deletions bear-engine/core/src/visual/code/gl_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,15 @@ void bear::visual::gl_renderer::set_gl_states( state_list& states )
{
boost::mutex::scoped_lock lock( m_mutex.gl_set_states );

m_render_ready = true;
m_states.clear();
m_states.swap( states );
m_render_ready = true;
}

if ( m_render_thread == NULL )
render_states();
else
m_render_condition.notify_one();
} // gl_renderer::set_gl_states()

/*----------------------------------------------------------------------------*/
Expand All @@ -489,35 +491,6 @@ void bear::visual::gl_renderer::set_background_color( const color_type& c )
m_background_color = c;
} // gl_renderer::set_background_color()

/*----------------------------------------------------------------------------*/
/**
* \brief Tells not to render anything for a while.
*/
void bear::visual::gl_renderer::set_pause()
{
boost::mutex::scoped_lock lock( m_mutex.loop_state );

m_mutex.gl_access.lock();

m_pause = true;
} // gl_renderer::set_pause()

/*----------------------------------------------------------------------------*/
/**
* \brief Turns the rendering process on again.
*/
void bear::visual::gl_renderer::unset_pause()
{
boost::mutex::scoped_lock lock( m_mutex.loop_state );

if ( m_pause == false )
return;

m_mutex.gl_access.unlock();

m_pause = false;
} // gl_renderer::unset_pause()

/*----------------------------------------------------------------------------*/
/**
* \brief Tells to stop the rendering process.
Expand All @@ -529,6 +502,12 @@ void bear::visual::gl_renderer::stop()
m_stop = true;
m_shader.clear();
}

{
boost::mutex::scoped_lock lock( m_mutex.gl_set_states );
m_render_ready = true;
m_render_condition.notify_one();
}

if ( m_render_thread != NULL )
{
Expand Down Expand Up @@ -577,11 +556,15 @@ bool bear::visual::gl_renderer::initialization_loop()
*/
void bear::visual::gl_renderer::render_loop()
{
// render every 15 milliseconds
static constexpr systime::milliseconds_type render_delta( 15 );

while ( true )
{
{
boost::mutex::scoped_lock states_lock( m_mutex.gl_set_states );

while( !m_render_ready )
m_render_condition.wait( states_lock );
}

// lock m_stop to ensure that stop() will block if called during the loop.
m_mutex.loop_state.lock();

Expand All @@ -591,22 +574,12 @@ void bear::visual::gl_renderer::render_loop()
break;
}

const systime::milliseconds_type start_date( systime::get_date_ms() );

if ( !m_pause )
{
render_states();
update_screenshot();
}

const systime::milliseconds_type end_date( systime::get_date_ms() );
render_states();
update_screenshot();

// Release the mutex while we sleep so other threads can request to stop
// the loop.
m_mutex.loop_state.unlock();

if ( end_date - start_date < render_delta )
systime::sleep( render_delta - (end_date - start_date) );
}
} // gl_renderer::render_loop()

Expand All @@ -618,14 +591,9 @@ void bear::visual::gl_renderer::render_states()
{
boost::mutex::scoped_lock states_lock( m_mutex.gl_set_states );

if ( !m_render_ready )
return;

m_render_ready = false;

if ( m_gl_context == NULL )
return;


assert ( m_gl_context != NULL );
draw_scene();

// The destruction of the states may call a delete_something() which will need
Expand Down Expand Up @@ -1121,7 +1089,7 @@ bear::visual::gl_renderer::create_shader( GLenum type, const std::string& p )
* Constructs a gl_renderer instance with no rendering informations.
*/
bear::visual::gl_renderer::gl_renderer()
: m_stop( false ), m_pause( false ), m_window( NULL ), m_gl_context( NULL ),
: m_stop( false ), m_window( NULL ), m_gl_context( NULL ),
m_background_color(0, 0, 0), m_window_size( 640, 480 ),
m_view_size( 640, 480 ), m_fullscreen( false ),
m_video_mode_is_set( false ),
Expand Down
18 changes: 0 additions & 18 deletions bear-engine/core/src/visual/code/gl_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,6 @@ bear::visual::gl_screen::gl_screen
gl_renderer::get_instance().set_title( title );
} // gl_screen::gl_screen() [constructor]

/*----------------------------------------------------------------------------*/
/**
* \brief Tells not to render anything for a while.
*/
void bear::visual::gl_screen::set_pause()
{
gl_renderer::get_instance().set_pause();
} // gl_screen::set_pause()

/*----------------------------------------------------------------------------*/
/**
* \brief Turns the rendering process on again.
*/
void bear::visual::gl_screen::unset_pause()
{
gl_renderer::get_instance().unset_pause();
} // gl_screen::unset_pause()

/*----------------------------------------------------------------------------*/
/**
* \brief Turn fullscreen mode on/off.
Expand Down
18 changes: 0 additions & 18 deletions bear-engine/core/src/visual/code/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,6 @@ bear::visual::screen::~screen()
} // screen::~screen()


/*----------------------------------------------------------------------------*/
/**
* \brief Tells not to render anything for a while.
*/
void bear::visual::screen::set_pause()
{
m_impl->set_pause();
} // screen::set_pause()

/*----------------------------------------------------------------------------*/
/**
* \brief Turns the rendering process on again.
*/
void bear::visual::screen::unset_pause()
{
m_impl->unset_pause();
} // screen::unset_pause()

/*----------------------------------------------------------------------------*/
/**
* \brief Turn fullscreen mode on/off.
Expand Down
9 changes: 2 additions & 7 deletions bear-engine/core/src/visual/gl_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <claw/image.hpp>

#include <boost/thread.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/signals2/signal.hpp>

Expand Down Expand Up @@ -93,9 +94,6 @@ namespace bear
color_type get_background_color();
void set_background_color( const color_type& c );

void set_pause();
void unset_pause();

private:
void stop();

Expand Down Expand Up @@ -146,9 +144,6 @@ namespace bear
/** \brief Tells if we must stop the rendering process. */
bool m_stop;

/** \brief Tells if we must pause the rendering process. */
bool m_pause;

/** \brief The window created by SDL. */
SDL_Window* m_window;

Expand Down Expand Up @@ -177,8 +172,8 @@ namespace bear
/** \brief The next elements to render. */
state_list m_states;

/** \brief Tells if a rendering must be done. */
bool m_render_ready;
boost::condition_variable m_render_condition;

/** \brief A buffer in which we do the screenshots, to avoid an allocation
at each call. */
Expand Down
3 changes: 0 additions & 3 deletions bear-engine/core/src/visual/gl_screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ namespace bear
gl_screen( const claw::math::coordinate_2d<unsigned int>& size,
const std::string& title="", bool full=false );

void set_pause();
void unset_pause();

void fullscreen( bool b );
claw::math::coordinate_2d<unsigned int> get_size() const;
claw::math::coordinate_2d<unsigned int> get_container_size() const;
Expand Down
3 changes: 0 additions & 3 deletions bear-engine/core/src/visual/screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ namespace bear
const std::string& title="", bool full=false );
~screen();

void set_pause();
void unset_pause();

void fullscreen( bool b );
claw::math::coordinate_2d<unsigned int> get_size() const;
claw::math::coordinate_2d<unsigned int> get_container_size() const;
Expand Down

0 comments on commit cba0710

Please sign in to comment.