将代码迁移到GL20图形奇怪的闪烁行为

时间:2014-11-20 17:49:59

标签: libgdx

我试图根据LibGDX书中存在的代码做一些代码。将一些屏幕转换添加到流(在gles1中工作),并且由于LibGDX弃用了GL10,我试图将其重构为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

1 个答案:

答案 0 :(得分:0)

排名第一 - 注释掉代码非常令人困惑,我不知道它是否仍然被注释掉了。如果没有注释掉绘图代码,CanyonBunnyMain中的render方法肯定会导致闪烁。因为它会继续被调用,因为它是你的主要切入点。

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 for render()。

第二 - 我怀疑你在SplashScreen01或SplashScreen02中不需要超级(app)。我会这样做(对他们两个人来说)

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

然后在你的构造函数中改变这个

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

}

到这个

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

因此,上面的app声明成为传递给构造函数的app。适用于SplashScreen01和SplashScreen02。所以你的游戏可以继续其完整性和#34;完好。

我希望有帮助,......并解决您的问题。

约翰