From 526dda3755b2cf17809d60eb6078889ba3569c5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Sun, 2 Jun 2013 16:34:06 -0300 Subject: [PATCH] Added a new example. --- .hgignore | 1 + include/eepp/ee.hpp | 2 +- premake4.lua | 6 + projects/linux/ee.creator.user | 97 ++++++++++- projects/linux/ee.files | 1 + src/eepp/graphics/cbatchrenderer.cpp | 5 + src/examples/vbo_fbo_batch/vbo_fbo_batch.cpp | 159 +++++++++++++++++++ 7 files changed, 265 insertions(+), 6 deletions(-) create mode 100644 src/examples/vbo_fbo_batch/vbo_fbo_batch.cpp diff --git a/.hgignore b/.hgignore index de74a9d4d..7f62533bf 100644 --- a/.hgignore +++ b/.hgignore @@ -19,6 +19,7 @@ eetest* eesound* eesprite* eefonts* +eevbo-fbo-batch* eeiv* ee.tag log.log diff --git a/include/eepp/ee.hpp b/include/eepp/ee.hpp index 413f48c60..03b39895c 100755 --- a/include/eepp/ee.hpp +++ b/include/eepp/ee.hpp @@ -37,7 +37,7 @@ EE::Gaming Not documented at all. @TODO Add more commented examples, showing at least the basic usage of the engine ( 10 or more examples at least ). - STATE: 5 examples available. + STATE: 6 examples available. @TODO Improve the map editor ( add triggers, tiles selection to copy paste in other zones of the map, undo/redo actions ). STATE: Needs at least to reoffset tiles and objects for the map resizing. diff --git a/premake4.lua b/premake4.lua index db5e9bd3c..aa8c23967 100644 --- a/premake4.lua +++ b/premake4.lua @@ -746,6 +746,12 @@ solution "eepp" files { "src/examples/fonts/*.cpp" } build_link_configuration( "eefonts" ) + project "eepp-vbo-fbo-batch" + kind "WindowedApp" + language "C++" + files { "src/examples/vbo_fbo_batch/*.cpp" } + build_link_configuration( "eevbo-fbo-batch" ) + if os.isfile("external_projects.lua") then dofile("external_projects.lua") end diff --git a/projects/linux/ee.creator.user b/projects/linux/ee.creator.user index 9e6151108..23914de88 100644 --- a/projects/linux/ee.creator.user +++ b/projects/linux/ee.creator.user @@ -1,6 +1,6 @@ - + ProjectExplorer.Project.ActiveTarget @@ -48,9 +48,9 @@ Desktop Desktop {388e5431-b31b-42b3-b9ad-9002d279d75d} - 16 + 17 0 - 8 + 9 /home/programming/eepp/make/linux @@ -429,6 +429,46 @@ debug-fonts GenericProjectManager.GenericBuildConfiguration + + /home/programming/eepp/make/linux + + + + false + -j4 eepp-vbo-fbo-batch + make + true + Make + + GenericProjectManager.GenericMakeStep + + 1 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + clean + make + %{buildDir} + Custom Process Step + + ProjectExplorer.ProcessStep + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + debug-test + debug-vbo-fbo-batch + GenericProjectManager.GenericBuildConfiguration + /home/programming/eepp/make/linux @@ -749,7 +789,7 @@ release-es GenericProjectManager.GenericBuildConfiguration - 17 + 18 0 @@ -1187,7 +1227,54 @@ false true - 9 + + true + + false + false + false + false + true + 0.01 + 10 + true + 25 + + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + + 2 + /home/programming/eepp/eevbo-fbo-batch-debug + false + + %{buildDir} + Run /home/programming/eepp/eevbo-fbo-batch-debug + eevbo-fbo-batch-debug + ProjectExplorer.CustomExecutableRunConfiguration + 3768 + true + false + false + true + + 10 diff --git a/projects/linux/ee.files b/projects/linux/ee.files index 04f649a4c..0f2be081b 100644 --- a/projects/linux/ee.files +++ b/projects/linux/ee.files @@ -610,3 +610,4 @@ ../../src/examples/sound/sound.cpp ../../src/examples/sprites/sprites.cpp ../../src/examples/fonts/fonts.cpp +../../src/examples/vbo_fbo_batch/vbo_fbo_batch.cpp diff --git a/src/eepp/graphics/cbatchrenderer.cpp b/src/eepp/graphics/cbatchrenderer.cpp index afe4c32a6..d16dc028e 100755 --- a/src/eepp/graphics/cbatchrenderer.cpp +++ b/src/eepp/graphics/cbatchrenderer.cpp @@ -8,6 +8,8 @@ namespace EE { namespace Graphics { cBatchRenderer::cBatchRenderer() : mVertex( NULL ), + mVertexSize( 0 ), + mTVertex( NULL ), mNumVertex(0), mTexture(NULL), mTF( cTextureFactory::instance() ), @@ -25,6 +27,9 @@ cBatchRenderer::cBatchRenderer() : } cBatchRenderer::cBatchRenderer( const eeUint& Prealloc ) : + mVertex( NULL ), + mVertexSize( 0 ), + mTVertex( NULL ), mNumVertex(0), mTexture(NULL), mTF( cTextureFactory::instance() ), diff --git a/src/examples/vbo_fbo_batch/vbo_fbo_batch.cpp b/src/examples/vbo_fbo_batch/vbo_fbo_batch.cpp new file mode 100644 index 000000000..1853826ba --- /dev/null +++ b/src/examples/vbo_fbo_batch/vbo_fbo_batch.cpp @@ -0,0 +1,159 @@ +#include + +// EE_MAIN_FUNC is needed for some platforms to export the main function as C function. +EE_MAIN_FUNC int main (int argc, char * argv []) +{ + // Create a new window + cWindow * win = cEngine::instance()->CreateWindow( WindowSettings( 1024, 768, "eepp - VBO - FBO and Batch Rendering" ), ContextSettings( true ) ); + + // Set window background color + win->BackColor( eeColor( 50, 50, 50 ) ); + + // Check if created + if ( win->Created() ) { + eePolygon2f Poly( eePolygon2f::CreateRoundedRectangle( 0, 0, 200, 50 ) ); + + // Create the Vertex Buffer, the vertex buffer stores the vertex data in the GPU, making the rendering much faster + // In the case that Vertex Buffer Object is not supported by the GPU, it will fallback to a inmediate-mode vertex buffer + cVertexBuffer * VBO = cVertexBuffer::New( VERTEX_FLAGS_PRIMITIVE, DM_TRIANGLE_FAN ); + cVertexBuffer * VBO2 = cVertexBuffer::New( VERTEX_FLAGS_PRIMITIVE, DM_TRIANGLE_FAN ); + + // Add the vertex and vertex colors to the Vertex Buffer + if ( NULL != VBO && NULL != VBO2 ) { + for ( Uint32 i = 0; i < Poly.Size(); i++ ) { + VBO->AddVertex( Poly[i] ); + VBO->AddColor( eeColorA( 100 + i, 255 - i, 150 + i, 100 ) ); + } + + Poly.Rotate( 90, Poly.ToAABB().Center() ); + + for ( Uint32 i = 0; i < Poly.Size(); i++ ) { + VBO2->AddVertex( Poly[i] ); + VBO2->AddColor( eeColorA( 100 + i, 255 - i, 150 + i, 100 ) ); + } + + // Compile the Vertex Buffer, this uploads the data to the GPU + VBO->Compile(); + VBO2->Compile(); + } + + // Create a new frame buffer. It will use Framebuffer Objects if available, otherwise it will try to fallback to PBuffers. + cFrameBuffer * FBO = cFrameBuffer::New( 200, 200 ); + + // The batch renderer class is designed to take control of almost all the rendering needed by the engine. + // Controls that the rendering is only done when is needed, preventing unneeded OpenGL API calls + // Usually the user will not need to use this class manually, since eepp controls this internally, + // but it's exposed to the programmer, to take control of this if needed. + // The engine uses the singleton class cGlobalBatchRenderer instance to render textures and primitives. + cBatchRenderer Batch; + + eeFloat ang = 0, scale = 1; + bool side = false; + + // 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(); + } + + // Bind the Frame Buffer, everything rendered from here will be rendered in the frame buffer + FBO->Bind(); + { + // Bind the buffered data ( activate the buffer ) + VBO->Bind(); + + // Draw the buffered data + VBO->Draw(); + + // Unbind the buffered data + VBO->Unbind(); + + // Same as above + VBO2->Bind(); + VBO2->Draw(); + VBO2->Unbind(); + } + // Unbind the frame buffer. Stops rendering to the frame buffer + FBO->Unbind(); + + // Draw the frame buffer many times + for ( int y = 0; y < 5; y++ ) { + for ( int x = 0; x < 5; x++ ) { + FBO->GetTexture()->Draw( x * 200, y * 200, -ang, 1.f, eeColorA(255,255,255,100) ); + } + } + + eeFloat HWidth = win->GetWidth() * 0.5f; + eeFloat HHeight = win->GetHeight() * 0.5f; + + // The batch can be rotated, scale and moved + Batch.BatchRotation( ang ); + Batch.BatchScale( scale ); + Batch.BatchCenter( eeVector2f( HWidth, HHeight ) ); + + // Create a quad to render + eeFloat aX = HWidth - 256.f; + eeFloat aY = HHeight - 256.f; + eeQuad2f TmpQuad( + eeVector2f( aX , aY ), + eeVector2f( aX , aY + 32.f ), + eeVector2f( aX + 32.f, aY + 32.f ), + eeVector2f( aX + 32.f, aY ) + ); + TmpQuad.Rotate( ang, eeVector2f( aX + 16.f, aY + 16.f ) ); + + // Begin drawing quads + Batch.QuadsBegin(); + + // Add some quads to the batch renderer + for ( Uint32 z = 0; z < 16; z++ ) { + for ( Uint32 y = 0; y < 16; y++ ) { + eeFloat tmpx = (eeFloat)z * 32.f; + eeFloat tmpy = (eeFloat)y * 32.f; + + // Add the quad to the batch + Batch.QuadsSetColor( eeColorA( z * 16, 255, 255, 150 ) ); + Batch.BatchQuadFree( TmpQuad[0].x + tmpx, TmpQuad[0].y + tmpy, TmpQuad[1].x + tmpx, TmpQuad[1].y + tmpy, TmpQuad[2].x + tmpx, TmpQuad[2].y + tmpy, TmpQuad[3].x + tmpx, TmpQuad[3].y + tmpy ); + } + } + + // Draw the batched quads + Batch.Draw(); + + // Add the rotation angle + ang+=win->Elapsed() * 0.1f; + ang = (ang>=360) ? 0 : ang; + + // Change the scale value + if (scale>=1.5f) { + scale = 1.5f; + side = true; + } else if (scale<=0.5f) { + side = false; + scale = 0.5f; + } + scale = (!side) ? scale+win->Elapsed() * 0.00025f : scale-win->Elapsed() * 0.00025f; + + // Draw frame + win->Display(); + } + + // Release the allocated objects ( VBOs and FBOs need to be released manually ) + eeSAFE_DELETE( VBO ); + eeSAFE_DELETE( VBO2 ); + eeSAFE_DELETE( FBO ); + } + + // Destroy the engine instance. Destroys all the windows and engine singletons. + cEngine::DestroySingleton(); + + // If was compiled in debug mode it will print the memory manager report + EE::MemoryManager::ShowResults(); + + return EXIT_SUCCESS; +}