libgdx学习记录20——多线程MultiThread资源处理

在libgdx中,一般的逻辑流程都在rende()函数中执行,这个函数是由opengl的渲染线程调用的,一般的图形显示和逻辑处理都在这个线程中。

一般情形下,在这个线程中处理就行了。但是当某些逻辑处理比较费时,可能会引起画面卡顿、不连贯等现象。这时,可以将主要的逻辑处理放在另一个线程中,然后再即将进行图形渲染时再将其传送到render线程中执行。

回传采用Gdx.app.postRunnable()函数,该函数调用后,回传的那部分代码会在下一次render()函数调用之前执行。

具体实例:

  1 package com.fxb.newtest;
  2 
  3 import com.badlogic.gdx.ApplicationAdapter;
  4 import com.badlogic.gdx.Gdx;
  5 import com.badlogic.gdx.graphics.Color;
  6 import com.badlogic.gdx.graphics.GL10;
  7 import com.badlogic.gdx.graphics.Texture;
  8 import com.badlogic.gdx.graphics.g2d.BitmapFont;
  9 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 10 import com.badlogic.gdx.graphics.g2d.TextureRegion;
 11 import com.badlogic.gdx.utils.Array;
 12 
 13 public class Lib020_Thread extends ApplicationAdapter{
 14 
 15     float currentTime;
 16     //float lastTime;
 17     
 18     SpriteBatch batch;
 19     BitmapFont font;
 20     int count = 0;
 21     
 22     Array<TextureRegion> array1 = new Array<TextureRegion>();
 23 
 24     
 25     Thread thread = new Thread(){
 26         @Override
 27         public void run() {
 28             // TODO Auto-generated method stub
 29             //final int a = 2;
 30             
 31             try{
 32                 while(true)
 33                 {
 34                     Thread.sleep( 1000 );        
 35                     count++;    
 36                     
 37                     if( count == 3 ){
 38                         Gdx.app.postRunnable(new Runnable(){
 39                             @Override
 40                             public void run() {
 41                                 // TODO Auto-generated method stub
 42                                 TextureRegion region = new TextureRegion( new Texture( Gdx.files.internal( "data/pal4_1.jpg" ) ) );
 43                                 array1.add( region );
 44                             }
 45                         });
 46                     }
 47                     if( count == 5 ){
 48                         Gdx.app.postRunnable(new Runnable(){
 49                             @Override
 50                             public void run() {
 51                                 // TODO Auto-generated method stub
 52                                 TextureRegion region = new TextureRegion( new Texture( Gdx.files.internal( "data/badlogic.jpg" ) ) );
 53                                 array1.add( region );
 54                             }
 55                         });
 56                     }
 57                 }            
 58             }
 59             catch(Exception e){
 60                 e.printStackTrace();
 61             }            
 62         }
 63     };
 64     
 65     
 66     @Override
 67     public void create() {
 68         // TODO Auto-generated method stub
 69         super.create();
 70         
 71         batch = new SpriteBatch();
 72         font = new BitmapFont();
 73         font.setColor( Color.BLACK );
 74         
 75         thread.start();
 76         
 77         //array1.add( new TextureRegion( new Texture( Gdx.files.internal( "data/badlogic.jpg" ) ) ) );        
 78     }
 79 
 80     @Override
 81     public void render() {
 82         // TODO Auto-generated method stub
 83         super.render();
 84         Gdx.gl.glClearColor( 1, 1, 1, 1 );
 85         Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
 86         
 87         batch.begin();
 88         
 89         
 90         float width = 0;
 91         for( int i=0; i<array1.size; ++i ){
 92             batch.draw( array1.get(i), width, 0 );
 93             width += array1.get(i).getRegionWidth()+5;
 94         }
 95         font.draw( batch, ""+count, 100, 280 );    
 96         batch.end();
 97 
 98     }
 99 
100     @Override
101     public void dispose() {
102         // TODO Auto-generated method stub
103         super.dispose();
104         font.dispose();
105         batch.dispose();
106     }
107     
108 }

运行效果:

刚开始没有加入图片纹理区域:

第3秒后加入第一幅图片纹理区域:

第5秒加入第二幅图片纹理区域:

当某些逻辑运算或资源加载比较费时时,可以考虑多线程方式。

posted @ 2014-06-09 16:20  丛林小阁楼  阅读(736)  评论(0编辑  收藏  举报