Libgdx 将代码迁移到GL20图形

Libgdx 将代码迁移到GL20图形,libgdx,Libgdx,我试图根据LibGDX的书中的代码编写一些代码。向流中添加一些屏幕转换在gles1中起作用,因为LibGDX不推荐GL10,所以我尝试将其重构为GL20 我对大多数已弃用的函数或以不同方式调用的函数做了一些更改。但我的最终结果并不可行,因为它使我的屏幕闪烁 由于这个问题,我现在正专注于桌面版 SplashScreen01.java import com.badlogic.gdx.Gdx; import com.badlogic.gdx.InputProcessor; import com.bad

我试图根据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();
    }

}
因此,由于我不知道哪个文件不起作用,任何想要帮助代码的人都可以:

第一,注释掉的代码非常混乱,我不知道它是否仍然被注释掉。如果绘制代码没有注释掉,那么Canyonbunymain中的渲染方法肯定会导致闪烁。因为这是你的主要切入点,所以它会不断被调用

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();
         */
}
使该渲染方法永远为空。这个类中唯一需要的代码是create方法中的代码。不过,渲染仍然需要一个空的@Override

第二,我怀疑您在SplashScreen01或SplashScreen02中都不需要superapp。我会像这样为他们俩做

private float debugRebuildStage;
**private DirectedGame app;**
然后在你的构造函数中改变这个

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

}
对此

public SplashScreen01(DirectedGame app) {
 **this.app = app;** 
}
因此,上面的应用程序声明成为传递给构造函数的应用程序。用于SplashScreen01和SplashScreen02。因此,您的游戏可以保持完整性

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

约翰