OSDN Git Service

[added] test for SpriteBatch.draw( Sprite sprite ). No different to standard renderin...
authorbadlogicgames <badlogicgames@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Wed, 20 Oct 2010 13:56:30 +0000 (13:56 +0000)
committerbadlogicgames <badlogicgames@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Wed, 20 Oct 2010 13:56:30 +0000 (13:56 +0000)
tests/gdx-tests/src/com/badlogic/gdx/tests/SpriteBatchTest.java

index d91187d..d9ff776 100644 (file)
  ******************************************************************************/\r
 package com.badlogic.gdx.tests;\r
 \r
-import com.badlogic.gdx.Application;\r
-import com.badlogic.gdx.Files.FileType;\r
 import com.badlogic.gdx.Gdx;\r
 import com.badlogic.gdx.InputListener;\r
 import com.badlogic.gdx.RenderListener;\r
+import com.badlogic.gdx.Files.FileType;\r
 import com.badlogic.gdx.graphics.Color;\r
 import com.badlogic.gdx.graphics.Font;\r
-import com.badlogic.gdx.graphics.Font.FontStyle;\r
 import com.badlogic.gdx.graphics.GL10;\r
 import com.badlogic.gdx.graphics.Pixmap;\r
-import com.badlogic.gdx.graphics.Pixmap.Format;\r
+import com.badlogic.gdx.graphics.Sprite;\r
 import com.badlogic.gdx.graphics.SpriteBatch;\r
 import com.badlogic.gdx.graphics.Texture;\r
+import com.badlogic.gdx.graphics.Font.FontStyle;\r
+import com.badlogic.gdx.graphics.Pixmap.Format;\r
 import com.badlogic.gdx.graphics.Texture.TextureFilter;\r
 import com.badlogic.gdx.graphics.Texture.TextureWrap;\r
 \r
 public class SpriteBatchTest implements RenderListener, InputListener\r
 {\r
-       int SPRITES = 336 / 2;\r
+       int SPRITES = 1000000 / 2;\r
        \r
        long startTime = System.nanoTime();\r
        int frames = 0;\r
@@ -44,6 +44,7 @@ public class SpriteBatchTest implements RenderListener, InputListener
        SpriteBatch spriteBatch;\r
        float sprites[] = new float[SPRITES*6];\r
        float sprites2[] = new float[SPRITES*6];        \r
+       Sprite[] sprites3 = new Sprite[SPRITES*2];\r
        \r
        int renderMethod = 0;\r
        \r
@@ -60,6 +61,8 @@ public class SpriteBatchTest implements RenderListener, InputListener
                        renderNormal(  );\r
                if( renderMethod == 1 )\r
                        renderArray( );\r
+               if( renderMethod == 2 )\r
+                       renderSprites( );\r
        }               \r
        \r
        private void renderNormal( )\r
@@ -153,6 +156,52 @@ public class SpriteBatchTest implements RenderListener, InputListener
                frames++;\r
        }\r
        \r
+       private void renderSprites( )\r
+       {\r
+               GL10 gl = Gdx.graphics.getGL10();\r
+               gl.glClearColor( 0.7f, 0.7f, 0.7f, 1 );\r
+               gl.glClear( GL10.GL_COLOR_BUFFER_BIT );\r
+               \r
+               float begin = 0;\r
+               float end = 0;\r
+               float draw1 = 0;\r
+               float draw2 = 0;\r
+               float drawText = 0;\r
+               \r
+               long start = System.nanoTime();\r
+               spriteBatch.begin();                    \r
+               begin = (System.nanoTime()-start)/1000000000.0f;\r
+                               \r
+               start = System.nanoTime();      \r
+               for( int i = 0; i < SPRITES; i++ )\r
+                       spriteBatch.draw( sprites3[i] );\r
+               draw1 = (System.nanoTime()-start)/1000000000.0f;\r
+               \r
+               start = System.nanoTime();                      \r
+               for( int i = SPRITES; i < SPRITES << 1; i++ )\r
+                       spriteBatch.draw( sprites3[i] );\r
+               draw2 = (System.nanoTime()-start)/1000000000.0f;\r
+                               \r
+               start = System.nanoTime();\r
+               spriteBatch.drawText( font, "Question?", 100, 300, Color.RED );         \r
+               spriteBatch.drawText( font, "and another this is a test", 200, 100, Color.WHITE );\r
+               spriteBatch.drawText( font, "all hail and another this is a test", 200, 200, Color.WHITE );\r
+               spriteBatch.drawText( font, "Sprite fps: " + Gdx.graphics.getFramesPerSecond(), 10, 40, Color.RED );\r
+               drawText = (System.nanoTime()-start)/1000000000.0f;\r
+               \r
+               start = System.nanoTime();\r
+               spriteBatch.end();\r
+               end = (System.nanoTime()-start)/1000000000.0f;\r
+               \r
+               if( System.nanoTime() - startTime > 1000000000 )\r
+               {\r
+                       Gdx.app.log( "SpriteBatch", "fps: " + frames + ", render calls: " + spriteBatch.renderCalls + ", " + begin + ", " + draw1 + ", " + draw2 + ", " + drawText + ", " + end );\r
+                       frames = 0;\r
+                       startTime = System.nanoTime();\r
+               }\r
+               frames++;\r
+       }\r
+       \r
        @Override\r
        public void surfaceChanged(int width, int height) \r
        {       \r
@@ -163,35 +212,47 @@ public class SpriteBatchTest implements RenderListener, InputListener
        public void surfaceCreated( ) \r
        {                                       \r
                if( spriteBatch == null )\r
-                       spriteBatch = new SpriteBatch( );               \r
+                       spriteBatch = new SpriteBatch( 10000 );         \r
                \r
                Pixmap pixmap = Gdx.graphics.newPixmap( Gdx.files.getFileHandle( "data/badlogicsmall.jpg", FileType.Internal ) );               \r
                texture = Gdx.graphics.newUnmanagedTexture( 32, 32, Format.RGB565, TextureFilter.Nearest, TextureFilter.Nearest, TextureWrap.ClampToEdge, TextureWrap.ClampToEdge );\r
                texture.draw( pixmap, 0, 0);\r
+               pixmap.dispose();\r
                \r
                pixmap = Gdx.graphics.newPixmap(32, 32, Format.RGBA8888 );\r
                pixmap.setColor(1, 1, 0, 0.5f );\r
                pixmap.fill();\r
-               texture2 = Gdx.graphics.newUnmanagedTexture( pixmap, TextureFilter.Nearest, TextureFilter.Nearest, TextureWrap.ClampToEdge, TextureWrap.ClampToEdge );\r
+//             texture2 = Gdx.graphics.newUnmanagedTexture( pixmap, TextureFilter.Nearest, TextureFilter.Nearest, TextureWrap.ClampToEdge, TextureWrap.ClampToEdge );\r
+               texture2 = texture;\r
+               pixmap.dispose();\r
                \r
                font = Gdx.graphics.newFont( "Arial", 32, FontStyle.Plain );\r
                \r
                for( int i = 0; i < sprites.length; i+=6 )\r
                {\r
                        sprites[i] = (int)(Math.random() * (Gdx.graphics.getWidth() - 32));\r
-                       sprites[i+1] = (int)(Math.random() * (Gdx.graphics.getHeight() - 32)) + 32;\r
+                       sprites[i+1] = (int)(Math.random() * (Gdx.graphics.getHeight() - 32));\r
                        sprites[i+2] = 0;\r
                        sprites[i+3] = 0;\r
                        sprites[i+4] = 32;\r
                        sprites[i+5] = 32;\r
                        sprites2[i] = (int)(Math.random() * (Gdx.graphics.getWidth() - 32));\r
-                       sprites2[i+1] = (int)(Math.random() * (Gdx.graphics.getHeight() - 32)) + 32;\r
+                       sprites2[i+1] = (int)(Math.random() * (Gdx.graphics.getHeight() - 32));\r
                        sprites2[i+2] = 0;\r
                        sprites2[i+3] = 0;\r
                        sprites2[i+4] = 32;\r
-                       sprites2[i+5] = 32;\r
+                       sprites2[i+5] = 32;                     \r
                }               \r
                \r
+               for( int i = 0; i < SPRITES * 2; i++ )\r
+               {\r
+                       sprites3[i] = new Sprite( texture, 0, 0, 32, 32 );                      \r
+                       sprites3[i].setBounds((int)(Math.random() * (Gdx.graphics.getWidth() - 32)), (int)(Math.random() * (Gdx.graphics.getHeight() - 32)), 32, 32 );\r
+                       if( i >= SPRITES )\r
+                               sprites3[i].texture = texture2;\r
+                       \r
+               }\r
+               \r
                Gdx.input.addInputListener( this );\r
        }\r
 \r
@@ -228,7 +289,7 @@ public class SpriteBatchTest implements RenderListener, InputListener
        @Override\r
        public boolean touchUp(int x, int y, int pointer) \r
        {\r
-               renderMethod = (renderMethod + 1) % 2;          \r
+               renderMethod = (renderMethod + 1) % 3;          \r
                return false;\r
        }\r
 \r