2014-11-20 34 views
0

我试图做一些基于LibGDX书中存在的代码。向流中添加一些屏幕转换(在gles1中工作),并且由于LibGDX不赞成使用GL10,我试图将其重构为GL20。将代码迁移到GL20图形奇怪的闪烁行为

我做了一些改变,其中大部分给予以不同方式调用的不推荐使用的函数或函数。但是我的最终结果并不接近可行,因为它让我的屏幕闪烁。

由于这个问题,我现在专注于桌面版本。

SplashScreen01.java

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.InputProcessor; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.Texture.TextureFilter; 
import com.badlogic.gdx.graphics.g2d.Sprite; 
import com.badlogic.gdx.graphics.g2d.TextureRegion; 
import com.badlogic.gdx.math.Interpolation; 
import com.badlogic.gdx.scenes.scene2d.Stage; 
import com.badlogic.gdx.scenes.scene2d.ui.Image; 
import com.badlogic.gdx.scenes.scene2d.ui.Stack; 
import com.badlogic.gdx.scenes.scene2d.ui.Table; 
import com.badlogic.gdx.utils.viewport.StretchViewport; 
import com.packtpub.libgdx.canyonbunny.screens.transitions.ScreenTransition; 
import com.packtpub.libgdx.canyonbunny.screens.transitions.ScreenTransitionSlice; 
import com.packtpub.libgdx.canyonbunny.util.Constants; 


public class SplashScreen01 extends AbstractGameScreen { 

private static final String TAG = SplashScreen01.class.getName(); 

private Stage stage; 
Texture splashTexture; 

Sprite splashSprite; 
boolean inTransition; 
float screenTimingLeft; 

private Image imgBackground; 

// debug 
private final float DEBUG_REBUILD_INTERVAL = 5.0f; 
private boolean debugEnabled = false; 
private float debugRebuildStage; 

public SplashScreen01(DirectedGame app) { 
    super(app); 

} 

/* 
* cycled function for each graphic frame renders the SplashScreen depending in the deltatime 
* @param deltatime - graphics timer counter 
*/ 
@Override 
public void render(float deltaTime) { 

    checkNextScreen(deltaTime); 

    //clear screen with color GREY 
    Gdx.gl.glClearColor(126/255.0f, 126/255.0f, 126/255.0f, 1.0f); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    if (debugEnabled) { 
     debugRebuildStage -= deltaTime; 
     if (debugRebuildStage <= 0) { 
      debugRebuildStage = DEBUG_REBUILD_INTERVAL; 
      rebuildStage(); 
     } 
    } 
    stage.act(deltaTime); 
    stage.draw(); 
    // Table.drawDebug(stage); 

} 

/* 
* Change the internal Viewport in case of a screen size alteration 
*/ 
@Override 
public void resize(int width, int height) { 
    stage.setViewport(new StretchViewport(Constants.VIEWPORT_GUI_WIDTH, Constants.VIEWPORT_GUI_HEIGHT)); 
} 

/* 
* First initialization of this class, first status initializer 
*/ 
@Override 
public void show() { 
    stage = new Stage(); 

    try { 
     splashTexture = new Texture("images/LogoSingleGrey768h.png"); 
     splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); 
    } catch (Exception e) { 
     //graphic Splash Screen: SomeOne Tempered the Game files system 
    } 
    screenTimingLeft = Constants.SPLASH_SCREEN_01_TOTAL_DURATION; 
    rebuildStage(); 
} 

@Override 
public void hide() { 

    splashTexture.dispose(); 
    stage.dispose(); 
} 

@Override 
public void pause() { 
} 

/** 
* Rebuild Stage preparing elements and gathering them together 
*/ 
private void rebuildStage() { 

    // build all layers 
    Table layerBackground = buildBackgroundLayer(); 

    // assemble stage for menu screen 
    stage.clear(); 
    Stack stack = new Stack(); 
    stage.addActor(stack); 
    stack.setSize(Constants.VIEWPORT_GUI_WIDTH, Constants.VIEWPORT_GUI_HEIGHT); 
    stack.add(layerBackground); 
} 

//check if it is time for next screen 
private void checkNextScreen(float deltaTime) { 
    screenTimingLeft -= deltaTime; 
    if (!inTransition && screenTimingLeft < 0) { 
     inTransition = true; 
     //call next screen 
     ScreenTransition transition = ScreenTransitionSlice.init(Constants.SPLASH_SCREEN_02_ANIM_DURATION, ScreenTransitionSlice.UP_DOWN, 20, Interpolation.pow5Out); 
     game.setScreen(new SplashScreen02(game), transition); 
    } 
} 

/** 
* Aggregates objects and actors for background layer 
* 
* @return layer - Table layer with the elements 
*/ 
private Table buildBackgroundLayer() { 
    Table layer = new Table(); 

    TextureRegion splashRegion = new TextureRegion(splashTexture, 0, 0, 768, 384); 

    imgBackground = new Image(splashRegion); 

    layer.add(imgBackground); 
    return layer; 
} 

@Override 
public InputProcessor getInputProcessor() { 
    return stage; 
} 

}

ScreenTransitionSlice.java

/******************************************************************************* 
* Copyright 2013 Andreas Oehlke 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
******************************************************************************/ 

package com.packtpub.libgdx.canyonbunny.screens.transitions; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.math.Interpolation; 
import com.badlogic.gdx.utils.Array; 

public class ScreenTransitionSlice implements ScreenTransition { 

    public static final int UP = 1; 
    public static final int DOWN = 2; 
    public static final int UP_DOWN = 3; 

    private static final ScreenTransitionSlice instance = new ScreenTransitionSlice(); 

    private float duration; 
    private int direction; 
    private Interpolation easing; 
    private Array<Integer> sliceIndex = new Array<Integer>(); 

    public static ScreenTransitionSlice init(float duration, int direction, int numSlices, Interpolation easing) { 
     instance.duration = duration; 
     instance.direction = direction; 
     instance.easing = easing; 
     // create shuffled list of slice indices which determines the order of slice animation 
     instance.sliceIndex.clear(); 
     for (int i = 0; i < numSlices; i++) 
      instance.sliceIndex.add(i); 
     instance.sliceIndex.shuffle(); 
     return instance; 
    } 

    @Override 
    public float getDuration() { 
     return duration; 
    } 

    @Override 
    public void render(SpriteBatch batch, Texture currScreen, Texture nextScreen, float alpha) { 
     float w = currScreen.getWidth(); 
     float h = currScreen.getHeight(); 
     float x = 0; 
     float y = 0; 
     int sliceWidth = (int) (w/sliceIndex.size); 

     Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     batch.begin(); 
     batch.draw(currScreen, 0, 0, 0, 0, w, h, 1, 1, 0, 0, 0, currScreen.getWidth(), currScreen.getHeight(), false, true); 
     if (easing != null) 
      alpha = easing.apply(alpha); 
     for (int i = 0; i < sliceIndex.size; i++) { 
      // current slice/column 
      x = i * sliceWidth; 
      // vertical displacement using randomized list of slice indices 
      float offsetY = h * (1 + sliceIndex.get(i)/(float) sliceIndex.size); 
      switch (direction) { 
       case UP: 
        y = -offsetY + offsetY * alpha; 
        break; 
       case DOWN: 
        y = offsetY - offsetY * alpha; 
        break; 
       case UP_DOWN: 
        if (i % 2 == 0) { 
         y = -offsetY + offsetY * alpha; 
        } else { 
         y = offsetY - offsetY * alpha; 
        } 
        break; 
      } 
      batch.draw(nextScreen, x, y, 0, 0, sliceWidth, h, 1, 1, 0, i * sliceWidth, 0, sliceWidth, nextScreen.getHeight(), false, true); 
     } 
     batch.end(); 
    } 

} 

所以,因为我真的不知道哪个文件是不可以这样做的工作,任何人都愿意帮助代码在: https://bitbucket.org/LisarteBarbosa/canyonbunny/src/7da43bb21833cfc7c0037e55182f380d31d7e34b/core/src/com/packtpub/libgdx/canyonbunny/?at=master

https://bitbucket.org/LisarteBarbosa/canyonbunny/src

回答

0

第一 - 注释掉代码是非常混乱,我不知道它是否仍然被注释掉。如果绘图代码未被注释掉,CanyonBunnyMain中的渲染方法肯定会导致闪烁。因为它会继续被调用,因为它是您的主要入口点。

public void render() { 
/* 
Gdx.gl.glClearColor(1, 0, 0, 1); 
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.begin(); batch.draw(img, 0, 0); batch.end(); 
     */ 
} 

使render()方法永远为空。你在这个类中唯一需要的代码就是create()方法中的代码。不过,你仍然需要一个空的@Override来渲染()。

第二 - 我怀疑你不需要SplashScreen01或SplashScreen02中的超级(app)。我会做这样的(对他们俩的)

private float debugRebuildStage; 
**private DirectedGame app;** 

,然后在你的构造改变这种

public SplashScreen01(DirectedGame app) { 
    super(app); 

} 

这个

public SplashScreen01(DirectedGame app) { 
**this.app = app;** 
} 

因此,应用程序声明之上成为传递给构造函数的应用程序。对于SplashScreen01和SplashScreen02。所以你的游戏可以保持完整的“完整性”。

我希望这有助于......并解决您的问题。

约翰