diff --git a/projects/linux/ee.creator.user b/projects/linux/ee.creator.user index ba5493693..35b8cbbb8 100644 --- a/projects/linux/ee.creator.user +++ b/projects/linux/ee.creator.user @@ -1,6 +1,6 @@ - + ProjectExplorer.Project.ActiveTarget @@ -54,9 +54,9 @@ Desktop Desktop {388e5431-b31b-42b3-b9ad-9002d279d75d} - 6 + 0 0 - 2 + 0 /home/programming/eepp/make/linux diff --git a/src/examples/empty_window/empty_window.cpp b/src/examples/empty_window/empty_window.cpp index bc03ddf3a..83deae505 100644 --- a/src/examples/empty_window/empty_window.cpp +++ b/src/examples/empty_window/empty_window.cpp @@ -1,41 +1,44 @@ #include +cWindow * win = NULL; + +void MainLoop() +{ + win->Clear(); + + // Create an instance of the primitive renderer + cPrimitives p; + + // Change the color + p.SetColor( eeColorA( 0, 255, 0, 150 ) ); + + // Update the input + win->GetInput()->Update(); + + // Check if ESCAPE key is pressed + if ( win->GetInput()->IsKeyDown( KEY_ESCAPE ) ) { + // Close the window + win->Close(); + } + + // Draw a circle + p.DrawCircle( eeVector2f( win->GetWidth() * 0.5f, win->GetHeight() * 0.5f ), 200 ); + + // Draw frame + win->Display(false); +} + EE_MAIN_FUNC int main (int argc, char * argv []) { - // Create a new window - cWindow * win = cEngine::instance()->CreateWindow( WindowSettings( 960, 640, "eepp - Empty Window" ) ); + // Create a new window with vsync enabled + win = cEngine::instance()->CreateWindow( WindowSettings( 960, 640, "eepp - Empty Window" ), ContextSettings( true ) ); // Check if created if ( win->Created() ) { // Set window background color win->BackColor( eeColor( 50, 50, 50 ) ); - // Create an instance of the primitive renderer - cPrimitives p; - - // Change the color - p.SetColor( eeColorA( 0, 255, 0, 150 ) ); - - // Application loop - while ( win->Running() ) { - // Update the input - win->GetInput()->Update(); - - // Check if ESCAPE key is pressed - if ( win->GetInput()->IsKeyDown( KEY_ESCAPE ) ) { - // Close the window - win->Close(); - } - - // Draw a circle - p.DrawCircle( eeVector2f( win->GetWidth() * 0.5f, win->GetHeight() * 0.5f ), 200 ); - - // Draw frame - win->Display(); - - // Sleep thread for 10 ms - Sys::Sleep( 10 ); - } + win->RunMainLoop( &MainLoop, 60 ); } // Destroy the engine instance. Destroys all the windows and engine singletons. diff --git a/src/examples/external_shader/external_shader.cpp b/src/examples/external_shader/external_shader.cpp index 934b73a64..95f5ce138 100644 --- a/src/examples/external_shader/external_shader.cpp +++ b/src/examples/external_shader/external_shader.cpp @@ -3,13 +3,22 @@ /// This example is based on the WebGL demo from http://minimal.be/lab/fluGL/ namespace Demo_ExternalShader { +#if defined( EE_ARM ) || EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN +static eeFloat sqrt_aprox[20001]; +#endif + Uint32 ParticlesNum = 30000; + cWindow * win = NULL; +cInput * imp = NULL; cShaderProgram * ShaderProgram = NULL; bool ShadersSupported = false; eeFloat tw; eeFloat th; eeFloat aspectRatio; +eeVector3ff * vertices = eeNewArray( eeVector3ff, ParticlesNum ); +eeVector3ff * velocities = eeNewArray( eeVector3ff, ParticlesNum ); +eeColorAf * colors = eeNewArray( eeColorAf, ParticlesNum ); void videoResize( cWindow * w ) { /// Video Resize event will re-setup the 2D projection and states, so we must rebuild them. @@ -81,6 +90,118 @@ void videoResize( cWindow * w ) { } using namespace Demo_ExternalShader; +void MainLoop() +{ + win->Clear(); + + imp->Update(); + + if ( imp->IsKeyDown( KEY_ESCAPE ) ) + { + win->Close(); + } + + if ( imp->IsKeyUp( KEY_F ) ) + { + if ( win->Windowed() ) { + win->Size( win->GetDesktopResolution().Width(), win->GetDesktopResolution().Height(), false ); + } else { + win->Size( 960, 640, true ); + win->Center(); + } + } + + eeFloat p; + eeVector2f mf = imp->GetMousePosf(); + eeFloat tratio = tw / th; + eeFloat touchX = ( mf.x / tw - 1 ) * tratio; + eeFloat touchY = -( mf.y / th - 1 ); + bool touch = imp->MouseLeftPressed(); + + for( Uint32 i = 0; i < ParticlesNum; i+=2 ) + { + // copy old positions + vertices[i].x = vertices[i+1].x; + vertices[i].y = vertices[i+1].y; + + // inertia + velocities[i].x *= velocities[i].z; + velocities[i].y *= velocities[i].z; + + // horizontal + p = vertices[i+1].x; + p += velocities[i].x; + + if ( p < -aspectRatio ) { + p = -aspectRatio; + velocities[i].x = eeabs(velocities[i].x); + } else if ( p > aspectRatio ) { + p = aspectRatio; + velocities[i].x = -eeabs(velocities[i].x); + } + vertices[i+1].x = p; + + // vertical + p = vertices[i+1].y; + p += velocities[i].y; + if ( p < -aspectRatio ) { + p = -aspectRatio; + velocities[i].y = eeabs(velocities[i].y); + } else if ( p > aspectRatio ) { + p = aspectRatio; + velocities[i].y = -eeabs(velocities[i].y); + + } + vertices[i+1].y = p; + + if ( touch ) { + eeFloat dx = touchX - vertices[i].x; + eeFloat dy = touchY - vertices[i].y; + eeFloat distance = dx * dx + dy * dy; + + #if !defined( EE_ARM ) && EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN + eeFloat d = eesqrt( distance ); + #else + eeFloat d = sqrt_aprox[ (Int32)(distance * 1000) ]; + #endif + + if ( d < 2.f ) { + if ( d < 0.03f ) { + vertices[i+1].x = Math::Randf( -1, 1 ) * aspectRatio; + vertices[i+1].y = Math::Randf( -1, 1 ); + velocities[i].x = 0; + velocities[i].y = 0; + } else { + dx /= d; + dy /= d; + d = ( 2 - d ) * 0.5; + d *= d; + velocities[i].x += dx * d * .01; + velocities[i].y += dy * d * .01; + } + } + } + } + + /// VertexPointer assigns values by default to the attribute "dgl_Vertex" + /// TextureCoordPointer to "dgl_MultiTexCoord0" + GLi->VertexPointer( 3, GL_FLOAT, sizeof(eeVector3ff), reinterpret_cast ( &vertices[0] ), ParticlesNum * sizeof(float) * 3 ); + + /// ColorPointer to "dgl_FrontColor" + GLi->ColorPointer( 4, GL_FP, sizeof(eeColorAf), reinterpret_cast ( &colors[0] ), ParticlesNum * sizeof(eeFloat) * 4 ); + + /// Draw the lines + GLi->DrawArrays( DM_LINES, 0, ParticlesNum ); + + /// Stop the simulation if the window is not visible + while ( !win->Visible() ) { + imp->Update(); /// To get the real state of the window you need to update the window input + Sys::Sleep( 100 ); /// Sleep 100 ms + } + + win->Display( false ); +} + EE_MAIN_FUNC int main (int argc, char * argv []) { win = cEngine::instance()->CreateWindow( WindowSettings( 960, 640, "eepp - External Shaders" ), ContextSettings( true ) ); @@ -90,7 +211,7 @@ EE_MAIN_FUNC int main (int argc, char * argv []) /// This will work without shaders too ShadersSupported = GLi->ShadersSupported(); - cInput * imp = win->GetInput(); + imp = win->GetInput(); /// We really don't need shaders for this, but the purpose of the example is to show how to work with external shaders if ( ShadersSupported ) { @@ -133,9 +254,6 @@ EE_MAIN_FUNC int main (int argc, char * argv []) win->PushResizeCallback( cb::Make1( &videoResize ) ); Uint32 i; - eeVector3ff * vertices = eeNewArray( eeVector3ff, ParticlesNum ); - eeVector3ff * velocities = eeNewArray( eeVector3ff, ParticlesNum ); - eeColorAf * colors = eeNewArray( eeColorAf, ParticlesNum ); for (i = 0; i < ParticlesNum; i++ ) { @@ -145,8 +263,7 @@ EE_MAIN_FUNC int main (int argc, char * argv []) } /** Optimized for ARM ( pre-cache sqrt ) */ - #ifdef EE_ARM - static eeFloat sqrt_aprox[20001]; + #if defined( EE_ARM ) || EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN eeFloat tFloat = 0; for ( int i = 0; i <= 20000; i++ ) { sqrt_aprox[i] = eesqrt( tFloat ); @@ -154,115 +271,7 @@ EE_MAIN_FUNC int main (int argc, char * argv []) } #endif - while ( win->Running() ) - { - imp->Update(); - - if ( imp->IsKeyDown( KEY_ESCAPE ) ) - { - win->Close(); - } - - if ( imp->IsKeyUp( KEY_F ) ) - { - if ( win->Windowed() ) { - win->Size( win->GetDesktopResolution().Width(), win->GetDesktopResolution().Height(), false ); - } else { - win->Size( 960, 640, true ); - win->Center(); - } - } - - eeFloat p; - eeVector2f mf = imp->GetMousePosf(); - eeFloat tratio = tw / th; - eeFloat touchX = ( mf.x / tw - 1 ) * tratio; - eeFloat touchY = -( mf.y / th - 1 ); - bool touch = imp->MouseLeftPressed(); - - for( i = 0; i < ParticlesNum; i+=2 ) - { - // copy old positions - vertices[i].x = vertices[i+1].x; - vertices[i].y = vertices[i+1].y; - - // inertia - velocities[i].x *= velocities[i].z; - velocities[i].y *= velocities[i].z; - - // horizontal - p = vertices[i+1].x; - p += velocities[i].x; - - if ( p < -aspectRatio ) { - p = -aspectRatio; - velocities[i].x = eeabs(velocities[i].x); - } else if ( p > aspectRatio ) { - p = aspectRatio; - velocities[i].x = -eeabs(velocities[i].x); - } - vertices[i+1].x = p; - - // vertical - p = vertices[i+1].y; - p += velocities[i].y; - if ( p < -aspectRatio ) { - p = -aspectRatio; - velocities[i].y = eeabs(velocities[i].y); - } else if ( p > aspectRatio ) { - p = aspectRatio; - velocities[i].y = -eeabs(velocities[i].y); - - } - vertices[i+1].y = p; - - if ( touch ) { - eeFloat dx = touchX - vertices[i].x; - eeFloat dy = touchY - vertices[i].y; - eeFloat distance = dx * dx + dy * dy; - - #ifndef EE_ARM - eeFloat d = eesqrt( distance ); - #else - eeFloat d = sqrt_aprox[ (Int32)(distance * 1000) ]; - #endif - - if ( d < 2.f ) { - if ( d < 0.03f ) { - vertices[i+1].x = Math::Randf( -1, 1 ) * aspectRatio; - vertices[i+1].y = Math::Randf( -1, 1 ); - velocities[i].x = 0; - velocities[i].y = 0; - } else { - dx /= d; - dy /= d; - d = ( 2 - d ) * 0.5; - d *= d; - velocities[i].x += dx * d * .01; - velocities[i].y += dy * d * .01; - } - } - } - } - - /// VertexPointer assigns values by default to the attribute "dgl_Vertex" - /// TextureCoordPointer to "dgl_MultiTexCoord0" - GLi->VertexPointer( 3, GL_FLOAT, sizeof(eeVector3ff), reinterpret_cast ( &vertices[0] ), ParticlesNum * sizeof(float) * 3 ); - - /// ColorPointer to "dgl_FrontColor" - GLi->ColorPointer( 4, GL_FP, sizeof(eeColorAf), reinterpret_cast ( &colors[0] ), ParticlesNum * sizeof(eeFloat) ); - - /// Draw the lines - GLi->DrawArrays( DM_LINES, 0, ParticlesNum ); - - /// Stop the simulation if the window is not visible - while ( !win->Visible() ) { - imp->Update(); /// To get the real state of the window you need to update the window input - Sys::Sleep( 100 ); /// Sleep 100 ms - } - - win->Display(); - } + win->RunMainLoop( &MainLoop ); eeSAFE_DELETE_ARRAY( vertices ); eeSAFE_DELETE_ARRAY( velocities ); diff --git a/src/test/eetest.cpp b/src/test/eetest.cpp index 299292299..121b5ad39 100644 --- a/src/test/eetest.cpp +++ b/src/test/eetest.cpp @@ -1,5 +1,15 @@ #include "eetest.hpp" +Demo_Test::cEETest * MY_INSTANCE = NULL; + +#if EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN +#include + +void MainLoop() { + MY_INSTANCE->Update(); +} +#endif + namespace Demo_Test { void cEETest::Init() { @@ -113,18 +123,21 @@ void cEETest::Init() { mVBO = cVertexBuffer::New( VERTEX_FLAGS_PRIMITIVE, DM_TRIANGLE_FAN ); - if ( NULL != mVBO ) { - for ( Uint32 i = 0; i < Poly.Size(); i++ ) { - mVBO->AddVertex( Poly[i] ); - mVBO->AddColor( eeColorA( 100 + i, 255 - i, 150 + i, 200 ) ); - } + if ( NULL != mVBO ) { + for ( Uint32 i = 0; i < Poly.Size(); i++ ) { + mVBO->AddVertex( Poly[i] ); + mVBO->AddColor( eeColorA( 100 + i, 255 - i, 150 + i, 200 ) ); + } - mVBO->Compile(); - } + mVBO->Compile(); + } PhysicsCreate(); +#if EE_PLATFORM != EE_PLATFORM_EMSCRIPTEN Launch(); +#endif + } else { cEngine::DestroySingleton(); @@ -911,9 +924,7 @@ void cEETest::LoadTextures() { CL2.AddFrame(TN[0], eeSizef(96, 96) ); CL2.Color( eeColorA( 255, 255, 255, 255 ) ); - if ( cImage::IsImage( MyPath + "atlases/bnb.png" ) ) { - mTGL = eeNew( cTextureAtlasLoader, ( MyPath + "atlases/bnb" + EE_TEXTURE_ATLAS_EXTENSION ) ); - } + mTGL = eeNew( cTextureAtlasLoader, ( MyPath + "atlases/bnb" + EE_TEXTURE_ATLAS_EXTENSION ) ); mBlindy.AddFramesByPattern( "rn" ); mBlindy.Position( 320.f, 0.f ); @@ -982,16 +993,20 @@ void cEETest::Run() { void cEETest::ParticlesThread() { while ( mWindow->Running() ) { - if ( MultiViewportMode || Screen == 2 ) { - PSElapsed = cElapsed.Elapsed(); - - for ( Uint8 i = 0; i < PS.size(); i++ ) - PS[i].Update( PSElapsed ); - } + UpdateParticles(); Sys::Sleep(10); } } +void cEETest::UpdateParticles() { + if ( MultiViewportMode || Screen == 2 ) { + PSElapsed = cElapsed.Elapsed(); + + for ( Uint8 i = 0; i < PS.size(); i++ ) + PS[i].Update( PSElapsed ); + } +} + void cEETest::Screen1() { Map.Draw(); } @@ -1569,27 +1584,45 @@ void cEETest::Input() { } } +void cEETest::Update() { + mWindow->Clear(); + + et = mWindow->Elapsed(); + + Input(); + + mResLoad.Update(); + + if ( mFontLoader.IsLoaded() ) { + Render(); + } else { + mFontLoader.Update(); + } + +#if EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN + UpdateParticles(); +#endif + + if ( KM->IsKeyUp(KEY_F12) ) mWindow->TakeScreenshot( MyPath + "screenshots/" ); //After render and before Display + + mWindow->Display(false); +} + void cEETest::Process() { Init(); if ( NULL != mWindow && mWindow->Created() ) { - do { - et = mWindow->Elapsed(); + #if EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN + cLog::instance()->Write( "Entering the main loop" ); + MY_INSTANCE = this; - Input(); - - mResLoad.Update(); - - if ( mFontLoader.IsLoaded() ) { - Render(); - } else { - mFontLoader.Update(); + emscripten_set_main_loop(MainLoop, 0, 1); + #else + // Application loop + while ( mWindow->Running() ) { + Update(); } - - if ( KM->IsKeyUp(KEY_F12) ) mWindow->TakeScreenshot( MyPath + "screenshots/" ); //After render and before Display - - mWindow->Display(); - } while( mWindow->Running() ); + #endif } End(); diff --git a/src/test/eetest.hpp b/src/test/eetest.hpp index 39862d622..66b8ee497 100644 --- a/src/test/eetest.hpp +++ b/src/test/eetest.hpp @@ -72,6 +72,7 @@ class cEETest : private cThread { typedef cb::Callback0 SceneCb; void Init(); + void Update(); void End(); void Process(); void Render(); @@ -80,6 +81,7 @@ class cEETest : private cThread { void ParticlesThread(); void Particles(); + void UpdateParticles(); void LoadTextures(); void CmdSetPartsNum ( const std::vector < String >& params );