0

所以,和其他人一样,我一直在使用 libgdx 开发一款针对 android 操作系统的游戏。我真的很想使用我的个人艺术(photoshop 画笔)而不是像素艺术。好吧,实际上我正在尝试融合 2,因为我们都知道 android 无法容纳足够的内存来显示我想使用的一些艺术作品。

例如:我有许多 512x512 的背景并排放置,构成整个“第一阶段”,看似多达 12 张以上的图像,每张图像约为 100k。这对 android 造成了负担,并显着降低了帧速率,低于 30 fps,使我无法在需要做的场景中添加更多图像。

问题:添加“背景”图像和背景装饰物的方法是什么?

他们不应该超过多少 Kb(大小)?

我将它们作为 Sprite 而不是纹理加载,是好还是坏?

背景图像保存为 .png 512x512 8bit,是好还是坏,哪个更好?

texturePacker 是否会为性能做任何事情,我是否应该对这些 512x512 或更大的文件进行纹理打包?

顺便说一句,我没有打包背景图片......

我是否必须求助于像素艺术和较小的图像尺寸才能在 android 上创建一个完整的横向滚动条?

我知道有很多问题,我试图解释我的困境,所以你会理解它作为一个整体......将所有这些问题视为 1 个问题:

我已经在这个游戏上工作了 7 个月了,我知道我必须在某个时候穿过这座桥,我已经完成了很多,我想说引擎已经接近完成了,意思是,地图/游戏编辑器,角色机制/能力,游戏机制(关卡障碍/要做的事情等)、goomba 和老板机制/能力/AI、故事、前景动画(64x64、64x128 图像没问题,即小文件大小)、项目、菜单、IT 都可以使用,除了 im有多个大背景尺寸的问题....

请感谢任何链接,文章,教程...我已经走得足够远了,知道如果我做错了什么,我可能不得不重做很多东西...。

我要玩一个完整的 20 多个小时的横向卷轴,在这么大的游戏中,我需要在 gfx 方面做什么?

4

4 回答 4

1

你的drawblocks函数每次调用它时都会调用新的textureregion,你每次都在创建一个新对象。您应该只调用一次 new 来创建对象,并在调用 drawblocks 时引用该对象。

创建您在创建屏幕时调用的某种 init 函数。

TextureRegion block1 = new TextureRegion(blockTextureRegion);

然后调用drawblocks函数。

case 0: spriteBatch.draw(block1, block.getPosition...

这样,您只需调用一次 new 而不是每次调用 drawblocks 时。

于 2013-08-26T02:18:48.383 回答
0

就像之前提到的人一样。必须删除 draw() 方法中的任何“新”语句!这绝对是你问题的原因!

您必须了解每帧都会调用draw方法!因此,如果您多次分配 40kb 大小的纹理 - 每秒 60 次,这将占用大量内存带宽!

那里也存在严重的内存泄漏,因为您创建的每一帧都创建了一个纹理区域并且不调用 .dispose()。

你的方法是在类 init 上创建纹理区域(例如在构造函数中)并将它们存储在私有 TextureRegion 属性中,然后使用它们..

整个绘制方法中再次没有新关键字!这也适用于新的 Color(1,1,1,1) .. 改用 priv 属性并分配一次或使用 Color.WHITE..

你使用的所有字符串也是如此!没有 + 的字符串允许.. 也没有 .toString() 等

偏执你甚至不应该在draw中声明一个float/int属性。创建一个私有和重用......

于 2013-12-13T05:44:34.550 回答
0

这是未经编辑的代码,因此您可以了解一下,我使用的是 WVC 方法,这是主要的渲染方法,如果您想查看此处调用的方法,请告诉我。我知道代码需要优化所以如果您看到任何重大问题,请让我知道我应该做什么.. ty

public void render() {
 player01_class player01 = world.getPlayer01();

 if ((player01.getPosition().x) > (CAMERA_WIDTH / 2))  {
     cam.position.set((player01.getPosition().x), (cam.position.y), 0);
     //cam.zoom = 3f;
     cam.update();  


 } else if ((player01.getPosition().x) < (CAMERA_WIDTH / 2))  {
     cam.position.set((player01.getPosition().x), (cam.position.y), 0);
     //cam.zoom = 3f;
     cam.update();           
 }

 if ((player01.getPosition().y) > (CAMERA_HEIGHT / 2)) {
     cam.position.set((cam.position.x), (player01.getPosition().y) , 0);
     //cam.zoom = 3f;
     cam.update();  
 } else if ((player01.getPosition().y) < (CAMERA_HEIGHT) / 2) {
     cam.position.set((cam.position.x), (cam.position.y) , 0);
     //cam.zoom = 3f;
     cam.update();  

 }


 clipBounds = new Rectangle(cam.position.x - 10, cam.position.y - 10, cam.viewportWidth, cam.viewportHeight);

 ScissorStack.calculateScissors(cam, spriteBatch.getTransformMatrix(), clipBounds, scissors);
 ScissorStack.pushScissors(scissors);

 spriteBatch.setProjectionMatrix(cam.combined);  
 spriteBatch.begin();


 //bg_pilot.setPosition(player01.getPosition().x / 1.05f - 10, 0);
 //bg_pilot.draw(spriteBatch, 1f);


 pangodavillageBG_x = player01.getPosition().x;

 spriteBatch.draw(new TextureRegion(pangodavillageBG01), pangodavillageBG_x / 1.80f, 2 , 25,  25);   
 spriteBatch.draw(new TextureRegion(pangodavillageBG02), pangodavillageBG_x / 1.80f + 25, 2 , 25,  25);  
 spriteBatch.draw(new TextureRegion(pangodavillageBG03), pangodavillageBG_x / 1.80f + 50, 2 , 25,  25);  
 spriteBatch.draw(new TextureRegion(pangodavillageBG04), pangodavillageBG_x / 1.80f + 75, 2 , 25,  25);  

 spriteBatch.draw(new TextureRegion(pangodavillageFG01), pangodavillageBG_x / 2.00f, 0 , 25,  25);   
 spriteBatch.draw(new TextureRegion(pangodavillageFG02), pangodavillageBG_x / 2.00f + 25, 0 , 25,  25);  
 spriteBatch.draw(new TextureRegion(pangodavillageFG03), pangodavillageBG_x / 2.00f + 50, 0 , 25,  25);  
 spriteBatch.draw(new TextureRegion(pangodavillageFG04), pangodavillageBG_x / 2.00f + 75, 0 , 25,  25);  



// drawClouds();   

 UpdateTime();

 drawBlocks(); 

 //if (controller.edit_toggle)  {
 drawEditBlocks();
 //}

 drawItems(); 

 drawPowerUps();

 drawPlayer01();

 drawBadGuy();

 drawUIItems();



 //stage.act(Gdx.graphics.getDeltaTime());
// stage.draw();


 bmpfont.draw(spriteBatch, "FPS: " +  Gdx.graphics.getFramesPerSecond(),  cam.position.x - 10,  cam.position.y + 7);
 bmpfont.drawMultiLine(spriteBatch, "playerX: " + (int)player01.getPosition().x + " Y: " + (int)player01.getPosition().y, cam.position.x - 10,  cam.position.y + 6, 1f, HAlignment.LEFT); 
// bmpfont.drawMultiLine(spriteBatch, "rx1: " + rectx1 + " ry1: " + recty1,  cam.position.x - 10,  cam.position.y + 5, 1, HAlignment.LEFT);
// bmpfont.drawMultiLine(spriteBatch, "block: " + currentBlock_counter, cam.position.x - 10,  cam.position.y + 5, 1, HAlignment.LEFT);

 //bmpfont.drawMultiLine(spriteBatch, "badx: " + badGuy.getPosition().x + " badguyy: " +  badGuy.getPosition().y,  cam.position.x - 10,  cam.position.y + 4, 1, HAlignment.LEFT);    
 //bmpfont.drawMultiLine(spriteBatch, "cvelocity: " + badGuy.getAcceleration() + " VBGy: " + badGuy.blockCollidedwithBGY , cam.position.x - 10,  cam.position.y + 3, 1f, HAlignment.LEFT); 
 bmpfont.drawMultiLine(spriteBatch, "mouseX: " + mouseX + " Y: " + mouseY  , cam.position.x - 10, cam.position.y + 5, 1f, HAlignment.LEFT); 
 //bmpfont.drawMultiLine(spriteBatch, "mousexX: " + mouseRawX + " Y: " + mouseRawY  , cam.position.x - 10, cam.position.y + 3, 1f, HAlignment.LEFT); 

 bmpfont.drawMultiLine(spriteBatch, "camposX: " + (int)cam.position.x + " camposY: " + (int)cam.position.y, cam.position.x - 10, cam.position.y + 4, 1f, HAlignment.LEFT);

 bmpfont.drawMultiLine(spriteBatch, "H: " + WorldTime_hour + " M: " + WorldTime_min + " S: " + (int)WorldTime_sec, cam.position.x + 6, cam.position.y + 7, 1f, HAlignment.LEFT);

 //bmpfont.drawMultiLine(spriteBatch, "TileNumber: " + mapTile_ID + "  block_collide_bool: " +  map_block_collide_bool, cam.position.x - 10, cam.position.y + 3, 1f, HAlignment.LEFT); 

 //bmpfont.drawMultiLine(spriteBatch, " aY: " + addButtonRect, cam.position.x, cam.position.y + 2, 1f, HAlignment.LEFT);        
// bmpfont.drawMultiLine(spriteBatch, "m: " + rect_mouse + "  a: " + addButtonRect, cam.position.x - 10, cam.position.y + 2, 1f, HAlignment.LEFT);      
 //bmpfont.drawMultiLine(spriteBatch, "mapTileID: " + mapTile_ID, cam.position.x - 10, cam.position.y + 2, 1f, HAlignment.LEFT);        

// bmpfont.drawMultiLine(spriteBatch, "Game Mode: " + edit_toggle , cam.position.x - 10, cam.position.y + 1, 1f, HAlignment.LEFT);      


 spriteBatch.end();

 if (debug) {
     drawDebug();
     //drawCollisionBlocks(); 
    // drawCollisionToggleBlocks();

     drawEditMenu();


}

 spriteBatch.begin();

   //updateParticles();

 spriteBatch.end();

// spriteBatch.setProjectionMatrix(cam.combined);  
 spriteBatch.begin();

 bmpfont.setScale(.03f); 
 bmpfont.setColor(new Color(1,1,1,1));

 bmpfont.draw(spriteBatch, "ADD", cam.position.x - 9.5f, cam.position.y + 9.65f);
 bmpfont.draw(spriteBatch, "DEL", cam.position.x - 7.9f, cam.position.y + 9.65f);

 bmpfont.draw(spriteBatch, "MODE", cam.position.x - 5.4f, cam.position.y + 9.65f);

 bmpfont.draw(spriteBatch, "BLK", cam.position.x - 2.9f, cam.position.y + 9.65f);
 bmpfont.draw(spriteBatch, "ITM", cam.position.x - 1.35f, cam.position.y + 9.65f);
 bmpfont.draw(spriteBatch, "BAD", cam.position.x - -0.1f, cam.position.y + 9.65f);

 spriteBatch.end();

 spriteBatch.flush();
 ScissorStack.popScissors();


 bmpfont.setColor(new Color(1,1,1,1));
 bmpfont.setScale(.04f); 

}

这是drawBlocks方法:

  private void drawBlocks()  {

   player01_class player01 = world.getPlayer01();


   displayBlock_x[3] = player01.getPosition().x;
   displayBlock_y[3] = cam.position.y + 8;


   for (Block block : world.getBlocks()) {

 //spriteBatch.draw(blockTextureRegion, block.getPosition().x * ppuX, block.getPosition().y * ppuY, Block.SIZE * ppuX, Block.SIZE * ppuY);

   switch (block.blockID)

    {

    case 0:
          spriteBatch.draw(new TextureRegion(blockTextureRegion), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 1:
          spriteBatch.draw(new TextureRegion(blockTextureRegion_metal), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 2:
          spriteBatch.draw(new TextureRegion(btr_basket), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 3:
          spriteBatch.draw(new TextureRegion( btr_sand ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;
    case 4:
          spriteBatch.draw(new TextureRegion( btr_stone1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 5:
          spriteBatch.draw(new TextureRegion( btr_brick ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 6:
          spriteBatch.draw(new TextureRegion( btr_jewel ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 7:
          spriteBatch.draw(new TextureRegion(btr_waterlike), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 8:
          spriteBatch.draw(new TextureRegion(btr_dirtfull1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 9:
          spriteBatch.draw(new TextureRegion( btr_dirtleftside1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 10:
          spriteBatch.draw(new TextureRegion(btr_dirtrightside1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 11:
          spriteBatch.draw(new TextureRegion( btr_dirttop1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 12:
          spriteBatch.draw(new TextureRegion( btr_dirt_leftgrassend1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 13:
          spriteBatch.draw(new TextureRegion( btr_dirt_rightgrassend1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;
    case 14:
          spriteBatch.draw(new TextureRegion( btr_grass1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 15:
          spriteBatch.draw(new TextureRegion(btr_grass_leftend1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 16:
          spriteBatch.draw(new TextureRegion( btr_grass_rightend1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 17:
          spriteBatch.draw(new TextureRegion(btr_grasspatch1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 18:
          spriteBatch.draw(new TextureRegion(sand_full_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());               
    break;

    case 19:
          spriteBatch.draw(new TextureRegion(marble_white_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 20:
          spriteBatch.draw(new TextureRegion(marble_darkwhite_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 21:
          spriteBatch.draw(new TextureRegion(stone_darkgrey_2), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 22:
          spriteBatch.draw(new TextureRegion( stone_darkgrey_3 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;
    case 23:
          spriteBatch.draw(new TextureRegion( stone_grey_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 24:
          spriteBatch.draw(new TextureRegion( stone_grey_2 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 25:
          spriteBatch.draw(new TextureRegion( stoneinset_grey_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 26:
          spriteBatch.draw(new TextureRegion(stoneinset_darkgrey_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 27:
          spriteBatch.draw(new TextureRegion(stoneoutset_grey_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 28:
          spriteBatch.draw(new TextureRegion( stoneoutset_darkgrey_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 29:
          spriteBatch.draw(new TextureRegion(stone_decogrey_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 30:
          spriteBatch.draw(new TextureRegion( stone_decodarkgrey_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 31:
          spriteBatch.draw(new TextureRegion( stone_decogrey_2 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 32:
          spriteBatch.draw(new TextureRegion( stone_decodarkgrey_2 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 33:
          spriteBatch.draw(new TextureRegion( stone_decoblue_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 34:
          spriteBatch.draw(new TextureRegion(stone_decodarkblue_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 35:
          spriteBatch.draw(new TextureRegion( stone_decogreen_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 36:
          spriteBatch.draw(new TextureRegion(stone_decodarkgreen_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 37:
          spriteBatch.draw(new TextureRegion(stone_decoyellow_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());            
    break;

    case 38:
          spriteBatch.draw(new TextureRegion( stone_decodarkyellow_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 39:
          spriteBatch.draw(new TextureRegion( stone_decoorange_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 40:
          spriteBatch.draw(new TextureRegion( stone_decodarkorange_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;
    case 41:
          spriteBatch.draw(new TextureRegion( stone_decopurple_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 42:
          spriteBatch.draw(new TextureRegion(stone_decodarkpurple_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 43:
          spriteBatch.draw(new TextureRegion( woodpanel_top_1 ), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 44:
          spriteBatch.draw(new TextureRegion(woodpanel_mid_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   
    break;

    case 45:
          spriteBatch.draw(new TextureRegion(woodpanel_bottom_1), block.getPosition().x , block.getPosition().y , block.getSize(), block.getSize());   

    break;
    }

  }

      if (currentBlock_counter > 45)  {

   currentBlock_counter = 0;

      }

    }  

这是drawPlayer方法:

private void drawPlayer01()  {
  player01_class player01 = world.getPlayer01();


player01Frame = player01.isFacingLeft() ? playerIdleLeft : playerIdleRight;

if(player01.getState().equals(State.WALKING)) {

    player01Frame = player01.isFacingLeft() ?        walkLeftAnimation.getKeyFrame(player01.getStateTime(), true) : 

    walkRightAnimation.getKeyFrame(player01.getStateTime(), true);

} else if (player01.getState().equals(State.JUMPING)) { 

    if (player01.getVelocity().y > 0) { 
        player01Frame = player01.isFacingLeft() ? player01JumpLeft : player01JumpRight;     
    } else {    
        player01Frame = player01.isFacingLeft() ? player01FallLeft : player01FallRight;     
    }

}  else if (player01.getState().equals(State.ATTACKING)) {  

    player01Frame = player01.isFacingLeft() ? playerAtkLeft : playerAtkRight;

}

//spriteBatch.draw(player01Frame, player01.getPosition().x * ppuX, player01.getPosition().y * ppuY, player01.SIZE * ppuX, player01.SIZE * ppuY);

//Attack 
if (player01.boolWeapon_isStriking)  {
    int a = 0;

    player01Frame = player01.isFacingLeft() ? playerAtkLeft : playerAtkRight;       
    sword01Frame = player01.isFacingLeft() ? sword01L : sword01R;       

    //if(System.currentTimeMillis() - lastPressProcessed > 20) {

        if (!player01.isFacingLeft()) {

            player01.weaponPosition.set(player01.getPosition().x + 1, player01.getPosition().y);

        } else if (player01.isFacingLeft()){

            //player01Frame = player01.isFacingLeft() ? sword01L : sword01R;        
            player01.weaponPosition.set(player01.getPosition().x - 1.5f, player01.getPosition().y);

        }

     //spriteBatch.draw(player01Frame, player01.getPosition().x, player01.getPosition().y, player01.getSizeW() , player01.getSizeH() );

     spriteBatch.draw(new TextureRegion(sword01Frame), player01.weaponPosition.x, player01.weaponPosition.y - 0.00f, player01.getWeaponSizeW() , player01.getWeaponSizeH()); 

}

spriteBatch.draw(player01Frame, player01.getPosition().x, player01.getPosition().y, player01.getSizeW() , player01.getSizeH() );

}

于 2013-07-10T15:52:31.110 回答
0

你应该阅读这个https://code.google.com/p/libgdx/wiki/TexturePacker

利用

batch.begin();

你的精灵渲染代码在这里

batch.end();

这将帮助您获得更高的 fps

并且纹理打包器确实可以提高性能使用

于 2013-07-09T12:51:23.710 回答