zoukankan      html  css  js  c++  java
  • libgdx学习记录16——资源加载器AssetManager

    AssetManager用于对游戏中的资源进行加载。当游戏中资源(图片、背景音乐等)较大时,加载时会需要较长时间,可能会阻塞渲染线程,使用AssetManager可以解决此类问题。

    主要优点:

    1. 大多数资源加载器AssetLoader都是异步加载,可以避免阻塞渲染线程。

    2. 通过引用计数来进行释放资源。

    3. 通过一个对象来管理所有其他资源。

    主要函数:

    load(path,type)加载某个路径的资源文件,后面type指定所要加载的资源类型。这个函数只是将资源文件加入到资源队列中,并不会开始加载。

    update()更新资源文件,正式加载。

    getProgress()返回加载的进度。这个进度并不是平滑的,而是指已加载的资源个数与所要加载的资源总数之比。例如加载3个资源,则只可能为0,0.33,0.66,1这几个值。

    finishedUpdate()强制进行同步加载。

    利用AssetManager可以很好的做成loading界面,作为游戏启动前的画面。

    具体代码:

     1 package com.fxb.newtest;
     2 
     3 import com.badlogic.gdx.ApplicationAdapter;
     4 import com.badlogic.gdx.Gdx;
     5 import com.badlogic.gdx.assets.AssetManager;
     6 import com.badlogic.gdx.audio.Music;
     7 import com.badlogic.gdx.graphics.Color;
     8 import com.badlogic.gdx.graphics.GL10;
     9 import com.badlogic.gdx.graphics.Texture;
    10 import com.badlogic.gdx.graphics.g2d.BitmapFont;
    11 import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
    12 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
    13 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
    14 import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
    15 
    16 public class Lib016_AssetManager extends ApplicationAdapter{
    17 
    18     AssetManager manager;
    19     SpriteBatch batch;
    20     BitmapFont font;
    21     Texture texture;
    22     Music music;
    23     
    24     ShapeRenderer rend;
    25     boolean isPlay = false;
    26     
    27     @Override
    28     public void create() {
    29         // TODO Auto-generated method stub
    30         super.create();
    31         
    32         manager = new AssetManager();
    33         batch = new SpriteBatch();
    34         rend = new ShapeRenderer();
    35         
    36         manager.load( "data/pal4_2.jpg", Texture.class );
    37         manager.load( "audio/xjwq.mp3", Music.class );
    38         manager.load( "font/default.fnt", BitmapFont.class );        
    39     }
    40 
    41     @Override
    42     public void render() {
    43         // TODO Auto-generated method stub
    44         super.render();
    45         Gdx.gl.glClearColor( 0, 1, 1, 1 );
    46         Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
    47         
    48         if( !manager.update() ){
    49             rend.setColor( Color.BLUE );
    50             rend.begin( ShapeType.Line );
    51             rend.rect( Gdx.graphics.getWidth()/4-1, (Gdx.graphics.getHeight()-20)/2-1, Gdx.graphics.getWidth()/2+2, 20+2 );
    52             rend.end();
    53             
    54             rend.setColor( Color.GREEN );
    55             rend.begin( ShapeType.Filled );
    56             rend.rect( Gdx.graphics.getWidth()/4, (Gdx.graphics.getHeight()-20)/2, Gdx.graphics.getWidth()/2*manager.getProgress(), 20  );
    57             rend.end();
    58         }
    59         else{
    60             texture = manager.get( "data/pal4_2.jpg", Texture.class );
    61             music = manager.get( "audio/xjwq.mp3", Music.class );
    62             font = manager.get( "font/default.fnt", BitmapFont.class );
    63             
    64             batch.begin();
    65             batch.draw( texture, Gdx.graphics.getWidth()/2-texture.getWidth()/2, 10 );
    66             TextBounds bounds = font.getBounds( "AssetManager Test" );
    67             font.draw( batch, "AssetManager Test", Gdx.graphics.getWidth()/2-bounds.width/2 ,10+texture.getHeight()+bounds.height+5 );
    68             batch.end();
    69             
    70             if( !isPlay ){
    71                 music.setVolume( 0.8f );
    72                 music.setLooping( true );
    73                 music.play();
    74                 isPlay = true;
    75             }
    76 
    77         }
    78     }
    79 
    80     @Override
    81     public void dispose() {
    82         // TODO Auto-generated method stub
    83         manager.dispose();
    84         rend.dispose();
    85         batch.dispose();
    86         super.dispose();
    87     }
    88 
    89 }

    运行结果:

    加载时的画面:

    加载完成后的画面:

    AssetManager在libgdx中应用很广,是一个很好的资源管理工具。

  • 相关阅读:
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第4章 读书笔记(待更新)
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第3章 读书笔记(待更新)
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第1,2章 读书笔记(待更新)
    Tkinter的Message组件
    Git 实操/配置/实践
    mysq5.7.32-win安装步骤
    行为型模式之模板方法
    结构型模式之组合模式
    结构型模式之享元模式
    结构型模式之外观模式
  • 原文地址:https://www.cnblogs.com/MiniHouse/p/3773578.html
Copyright © 2011-2022 走看看