Skip to content

Commit

Permalink
Speed up swf rendering by reusing a single OpenGL context
Browse files Browse the repository at this point in the history
* alternative implementation of Oomek's work in PR #509
* Instead of creating a context for each swf object we now reuse a single context for all swf
* removed call to glPushAttrib( GL_ALL_ATTRIB_BITS )
  • Loading branch information
mickelson committed Dec 26, 2018
1 parent 78a22e0 commit 553cc8e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
77 changes: 50 additions & 27 deletions src/swf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@

#include <iostream>

#include "fe_base.hpp" // logging

namespace
{
gameswf::render_handler *swf_render=NULL;
gameswf::sound_handler *swf_sound=NULL;
int swf_count=0;
FeZipStream *swf_zip=NULL;
sf::Context *swf_context=NULL;

static tu_file *swf_file_opener( const char *url )
{
Expand All @@ -58,10 +61,22 @@ namespace
return new tu_file(url, "rb");
}

void delete_swf_context()
{
if ( swf_context )
{
delete swf_context;
swf_context=NULL;
FeDebug() << "Deleted swf context" << std::endl;
}
}

void open_swf()
{
if ( swf_render == NULL )
{
FeDebug() << "Initializing game_swf renderer" << std::endl;

#ifdef USE_GLES
swf_render = gameswf::create_render_handler_ogles();
#else
Expand All @@ -84,6 +99,36 @@ namespace
#else
gameswf::set_glyph_provider( gameswf::create_glyph_provider_tu() );
#endif

// One time initialization of an sf::Context to be used for swf rendering
//
if ( !swf_context )
{
swf_context = new sf::Context();
std::atexit( delete_swf_context );

FeDebug() << "Created swf context" << std::endl;

swf_context->setActive( true );

// alpha blending
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

glEnable( GL_LINE_SMOOTH );
glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );

glMatrixMode( GL_PROJECTION );

#ifndef USE_GLES
glOrtho( -1.f, 1.f, 1.f, -1.f, -1, 1 );
#endif

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glDisable( GL_LIGHTING );
}
}
swf_count++;
}
Expand All @@ -93,6 +138,8 @@ namespace
swf_count--;
if ( swf_count == 0 )
{
FeDebug() << "Uninitializing game_swf renderer" << std::endl;

gameswf::set_render_handler( NULL );
delete swf_render;
swf_render = NULL;
Expand Down Expand Up @@ -182,31 +229,6 @@ bool FeSwf::open_from_file( const std::string &file )

m_texture.setSmooth( true );

m_context.setActive( true );
m_texture.setActive();

// alpha blending
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

glEnable( GL_LINE_SMOOTH );
glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );

glMatrixMode( GL_PROJECTION );

#ifndef USE_GLES
glOrtho( -1.f, 1.f, 1.f, -1.f, -1, 1 );
#endif

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glDisable( GL_LIGHTING );

#ifndef USE_GLES
glPushAttrib( GL_ALL_ATTRIB_BITS );
#endif

m_imp->root->set_display_viewport( 0, 0,
m_imp->root->get_movie_width(),
m_imp->root->get_movie_height() );
Expand All @@ -229,12 +251,13 @@ const sf::Texture &FeSwf::get_texture() const

bool FeSwf::tick()
{
m_context.setActive( true );
return do_frame( true );
}

bool FeSwf::do_frame( bool is_tick )
{
swf_context->setActive( true );

m_texture.setActive();
m_texture.clear( sf::Color::Transparent );

Expand All @@ -256,7 +279,7 @@ bool FeSwf::do_frame( bool is_tick )
}

m_texture.display();
m_context.setActive( false );
swf_context->setActive( false );

return ( m_imp->root != NULL );
}
Expand Down
1 change: 0 additions & 1 deletion src/swf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class FeSwf

FeSwfState *m_imp;
sf::RenderTexture m_texture;
sf::Context m_context;
};

#endif

0 comments on commit 553cc8e

Please sign in to comment.