LibGDX basic stage draw error - java

I'm trying to create a game, but my code keeps giving me error:
Exception in thread "LWJGL Application" java.lang.IllegalArgumentException: no uniform with name 'u_projModelView' in shader
at com.badlogic.gdx.graphics.glutils.ShaderProgram.fetchUniformLocation(ShaderProgram.java:287)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.fetchUniformLocation(ShaderProgram.java:277)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformMatrix(ShaderProgram.java:507)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformMatrix(ShaderProgram.java:498)
at com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20.flush(ImmediateModeRenderer20.java:147)
at com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20.end(ImmediateModeRenderer20.java:160)
at com.badlogic.gdx.graphics.glutils.ShapeRenderer.end(ShapeRenderer.java:1104)
at com.badlogic.gdx.graphics.glutils.ShapeRenderer.check(ShapeRenderer.java:1092)
at com.badlogic.gdx.graphics.glutils.ShapeRenderer.rect(ShapeRenderer.java:389)
at com.badlogic.gdx.scenes.scene2d.ui.Table.drawDebugRects(Table.java:1224)
at com.badlogic.gdx.scenes.scene2d.ui.Table.drawDebug(Table.java:1204)
at com.badlogic.gdx.scenes.scene2d.Group.drawDebugChildren(Group.java:156)
at com.badlogic.gdx.scenes.scene2d.Group.drawDebug(Group.java:139)
at com.badlogic.gdx.scenes.scene2d.Stage.drawDebug(Stage.java:169)
at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:132)
at net.lukshe.lukshegame2.screens.MainMenu.render(MainMenu.java:71)
at com.badlogic.gdx.Game.render(Game.java:46)
at net.lukshe.lukshegame2.LuksheGame.render(LuksheGame.java:23)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
MainMenu.java:
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
public class MainMenu implements Screen {
private Stage stage; //done
private TextureAtlas atlas; //done
private Skin skin; //done
private Table table; //done
private TextButton buttonplay, buttonexit;
private BitmapFont white; //done
private Label heading;
#Override
public void show() {
stage = new Stage();
atlas = new TextureAtlas("ui/button.pack");
skin = new Skin(atlas);
table = new Table(skin);
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
white = new BitmapFont(Gdx.files.internal("font/white.fnt"), false);
//Set the button style
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("buttonup");
textButtonStyle.down = skin.getDrawable("buttondown");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.pressedOffsetY = -1;
textButtonStyle.font = white;
textButtonStyle.fontColor = Color.BLACK;
//Creating exit button
buttonexit = new TextButton("Exit", textButtonStyle);
buttonexit.pad(20);
//Add button to the table
table.add(buttonexit);
//Debug
table.debug();
//Add table to the stage
stage.addActor(table);
}
#Override
public void render(float delta) {
//OpenGL settings
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
//Render at the speed of delta
stage.act(delta);
//Draw stage
stage.draw();
}
LuksheGame.java:
public class LuksheGame extends Game {
public static final String NAME = "Lukse game (LibGDX)", VERSION = "Pre-Alpha 0.0.0.2";
#Override
public void create() {
setScreen(new Splash());
}
#Override
public void dispose() {
super.dispose();
}
#Override
public void render() {
super.render();
}
#Override
public void resize(int width, int height) {
super.resize(width, height);
}
#Override
public void pause() {
super.pause();
}
#Override
public void resume() {
super.resume();
}
}
If someone can help me, please help.
EDIT:
MainMenu.java is called from Splash.java. In Splash class I'm using Tween engine to increase and decrease opacity of the splash screen.
Splash.java:
public class Splash implements Screen {
private SpriteBatch batch;
private Sprite splash;
private TweenManager twm;
#Override
public void show() {
batch = new SpriteBatch();
twm = new TweenManager();
Tween.registerAccessor(Sprite.class, new SpriteAccessor());
Texture splashtexture = new Texture(Gdx.files.internal("images/splash.png"));
splash = new Sprite(splashtexture);
splash.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Tween.set(splash, SpriteAccessor.alpha).target(0).start(twm);
Tween.to(splash, SpriteAccessor.alpha, 2).target(1).repeatYoyo(1, 2).setCallback(new TweenCallback() {
#Override
public void onEvent(int type, BaseTween<?> source) {
((Game) Gdx.app.getApplicationListener()).setScreen(new MainMenu());
}
}).start(twm);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
twm.update(delta);
batch.begin();
splash.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
batch.dispose();
splash.getTexture().dispose();
}
}

Related

LibGDX Fade Out / Fade In Screen Transition

I'm trying to get a nice fade in/ fade out between two of my screens, and after hours of searching online I have come up empty handed. I've attempted various solutions involving Actions to no avail, and I am not to keen on using TweenEngine, but I would appreciate any help!
Below is the closest solution i've found. This one simply delay the time before the screens switch, yet you don't see a fade in any way.
package com.aidanstrong.game;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.ColorAction;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Timer;
public class FadingGame extends Game {
GameScreen gameScreen;
UpgradeScreen upgradeScreen;
private Actor fadeActor = new Actor();
private ShapeRenderer fadeRenderer;
#Override
public void create() {
gameScreen = new GameScreen(this);
upgradeScreen = new UpgradeScreen(this);
setScreen(gameScreen);
fadeRenderer = new ShapeRenderer(8);
}
public void setScreenWithFade (final Screen screen, float duration) {
fadeActor.clearActions();
fadeActor.setColor(Color.CLEAR);
fadeActor.addAction(Actions.sequence(
Actions.color(Color.BLACK, duration/2f, Interpolation.fade),
Actions.run(new Runnable(){public void run(){setScreen(screen);}}),
Actions.color(Color.CLEAR, duration/2f, Interpolation.fade)
));
}
#Override
public void render (){
super.render();
fadeActor.act(Gdx.graphics.getDeltaTime());
float alpha = fadeActor.getColor().a;
if (alpha != 0){
fadeRenderer.begin(ShapeRenderer.ShapeType.Filled);
fadeRenderer.setColor(0, 0, 0, alpha);
fadeRenderer.rect(-1, -1, 2, 2); //full screen rect w/ identity matrix
fadeRenderer.end();
}
}
}
Add topLayer as an Image on your stage and add Action on that Actor. Try this test case:
public class GdxTest extends Game {
FirstScreen firstScreen;
SecondScreen secondScreen;
#Override
public void create() {
firstScreen=new FirstScreen();
secondScreen=new SecondScreen();
setScreen(firstScreen);
}
public static Texture getTexture(){
Pixmap pixmap;
try {
pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
}catch (GdxRuntimeException e)
{
pixmap=new Pixmap(1,1, Pixmap.Format.RGB565);
}
pixmap.setColor(Color.WHITE);
pixmap.drawRectangle(0,0,1,1);
return new Texture(pixmap);
}
}
FirstScreen
public class FirstScreen extends ScreenAdapter {
Stage stage;
Texture texture,white;
#Override
public void show() {
stage=new Stage();
final Image image = new Image(texture=new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth()/2, stage.getHeight()/2, Align.center);
stage.addActor(image);
final Image topLayer = new Image(new TextureRegion(white=GdxTest.getTexture()));
topLayer.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
topLayer.setColor(Color.BLACK);
stage.addActor(topLayer);
stage.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
topLayer.addAction(Actions.sequence(Actions.color(Color.BLACK,2),Actions.run(new Runnable() {
#Override
public void run() {
GdxTest gdxTest=((GdxTest)Gdx.app.getApplicationListener());
gdxTest.setScreen(gdxTest.secondScreen);
}
})));
super.clicked(event, x, y);
}
});
topLayer.addAction(Actions.fadeOut(2));
Gdx.input.setInputProcessor(stage);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
#Override
public void hide() {
dispose();
}
#Override
public void dispose() {
stage.dispose();
texture.dispose();
white.dispose();
}
}
SecondScreen
public class SecondScreen extends ScreenAdapter{
Stage stage;
Texture texture,white;
#Override
public void show() {
stage=new Stage();
final Image image = new Image(texture=new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth() / 2, stage.getHeight() / 2, Align.center);
image.setOrigin(Align.center);
image.rotateBy(90);
stage.addActor(image);
final Image topLayer = new Image(new TextureRegion(white=GdxTest.getTexture()));
topLayer.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
topLayer.setColor(Color.BLACK);
stage.addActor(topLayer);
topLayer.addAction(Actions.fadeOut(2));
Gdx.input.setInputProcessor(stage);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
stage.act();
}
#Override
public void hide() {
dispose();
}
#Override
public void dispose() {
stage.dispose();
texture.dispose();
white.dispose();
}
}
Or
You can use FBO, at the top of your stage.
EDIT
public class GdxTest extends ApplicationAdapter {
Stage stage;
FrameBuffer frameBuffer;
float delta;
#Override
public void create() {
stage=new Stage();
{
final Image image = new Image(new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth() / 2, stage.getHeight() / 2, Align.center);
stage.addActor(image);
}
{
final Image image = new Image(new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth()/3, stage.getHeight()/3, Align.center);
stage.addActor(image);
}
{
final Image image = new Image(new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth()*.75f, stage.getHeight()*.75f, Align.center);
stage.addActor(image);
}
Gdx.input.setInputProcessor(stage);
delta=1;
}
#Override
public void render() {
if(delta>0)delta-=.01f;
frameBuffer.begin();
Gdx.gl.glClearColor(0,0,0,delta);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
frameBuffer.end();
stage.act();
stage.draw();
stage.getBatch().setProjectionMatrix(stage.getBatch().getProjectionMatrix().idt());
stage.getBatch().begin();
stage.getBatch().draw(frameBuffer.getColorBufferTexture(),-1,1,2,-2);
stage.getBatch().end();
}
#Override
public void dispose() {
stage.dispose();
frameBuffer.dispose();
}
#Override
public void resize(int width, int height) {
if(frameBuffer !=null && (frameBuffer.getWidth()!=width || frameBuffer.getHeight()!=height )) {
frameBuffer.dispose();
frameBuffer=null;
}
if(frameBuffer==null){
try {
frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
}catch (GdxRuntimeException e){
frameBuffer=new FrameBuffer(Pixmap.Format.RGB565,width,height,false);
}
}
}
}

How to show loading screen class before preferences [Libgdx & android]

So I am programming a game, and implemented preferences to save/load some simple data for sound/high score, nothing too much. The problem is that when I load preferences for the first time on a physical phone (samsung g. s3) it takes about 15sec and on s5 it takes about 5-8secs and in that time it shows black screen even though I have set the loading screen to be shown first, before preferences are used.
In my main application code I set screen to loadingScreen
loadingScreen = new com.package.game.Screens.LoadingScreen(this);
mainScreen = new com.package.game.Screens.MainScreen(this);
gameScreen = new com.package.game.Screens.GameScreen(this);
settingsScreen = new com.package.game.Screens.SettingsScreen(this);
this.setScreen(loadingScreen);
and then in my loading screen I access preferences:
#Override
public void show() {
this.progress_assets = 0f;
this.progress_assets = 0f;
font_loading = app.initFont(50, 1, 255, 255, 255, 1);
queueAssets();
loading_db();
}
private void loading_db() {
if(database.get_first_time()){
//do some introduction for first time run
Gdx.app.log("db","first time worked");
database.set_first_time();
progress_db=1f;
}else{
Gdx.app.log("db","first time is set false");
progress_db=1f;
}
}
and my preferences class:
package com.package.game.Engine;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.package.game.Application;
public class Database {
private Preferences preferences;
//database
private Preferences pref_settings;
private Preferences pref_score;
private Preferences pref_unlocks;
public Database(){
pref_settings = Gdx.app.getPreferences("com.package.game.settings");
pref_score = Gdx.app.getPreferences("com.package.game.score");
pref_unlocks = Gdx.app.getPreferences("com.package.game.unlocks");
}
//checking if first time run
public boolean get_first_time(){
return pref_settings.getBoolean("First_run",true);
}
public void set_first_time(){
pref_settings.putBoolean("First_run",false);
for(int i=0;i<7;i++){
pref_unlocks.putBoolean("unlock_"+i,true);
}
pref_unlocks.flush();
pref_settings.flush();
}
//settings
public boolean getSound(){
return pref_settings.getBoolean("sound_on",true);
}
public void setSound(boolean sound){
pref_settings.putBoolean("sound_on",sound);
pref_settings.flush();
}
//High Score Mode:
//Classic
public int getScore_classic(int place){
return pref_score.getInteger("score_classic_"+place,0);//default value 0 so we could place new score if we havent reached it
}
public void setScore_classic(int place,int scored){
pref_score.putInteger("score_classic_"+place,scored);
pref_score.flush();
}
//Recipe
public int getScoreRec(int place){
return pref_score.getInteger("score_recipe_"+place,0);//default value 0 so we could place new score if we havent reached it
}
public void setScoreRec(int place,int scored){
pref_score.putInteger("score_recipe_"+place,scored);
pref_score.flush();
}
//food unlocks
public boolean getFoodUnlock(int unlockID){
return pref_unlocks.getBoolean("unlock_"+unlockID,false);
}
public void setFoodUnlock(int unlockID,boolean state){
pref_unlocks.putBoolean("unlock_"+unlockID,state);
pref_unlocks.flush();
}
}
I'm not sure if creating 3 pref. files is good, but i would like to know how to show screen before pref. file being created/loaded.
Edit: Adding my main code
package com.mindutis.game;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.mindutis.game.Screens.LoadingScreen;
public class Application extends Game {
public static int V_WIDTH ;
public static int V_HEIGHT;
public OrthographicCamera camera;
public SpriteBatch batch;
public AssetManager assets;
public com.mindutis.game.Screens.LoadingScreen loadingScreen;
public com.mindutis.game.Screens.SplashScreen splashScreen;
public com.mindutis.game.Screens.MainScreen mainScreen;
public com.mindutis.game.Screens.GameScreen gameScreen;
public com.mindutis.game.Screens.SettingsScreen settingsScreen;
public BitmapFont f_game_name;
public String t_game_name;
public String[] font_type = new String[2];
private boolean firstFrame = true;
private boolean loading = true;
#Override
public void create() {
assets = new AssetManager();
camera = new OrthographicCamera();
V_WIDTH= Gdx.graphics.getWidth();
V_HEIGHT = Gdx.graphics.getHeight();
camera.setToOrtho(false, V_WIDTH, V_HEIGHT);
batch = new SpriteBatch();
//Global var.
//font initializer
font_type[0] = "fonts/comics_bold.ttf";
font_type[1] = "fonts/vdj.ttf";
//game name
t_game_name = " Catch a\nSandwich";
f_game_name = initFont(70, 0, 255, 255, 255, 1);
loadingScreen = new com.mindutis.game.Screens.LoadingScreen(this);
splashScreen = new com.mindutis.game.Screens.SplashScreen(this);
mainScreen = new com.mindutis.game.Screens.MainScreen(this);
gameScreen = new com.mindutis.game.Screens.GameScreen(this);
settingsScreen = new com.mindutis.game.Screens.SettingsScreen(this);
// this.setScreen(loadingScreen);
}
//int size=font size, int x = font type
public BitmapFont initFont(int size, int type, float red, float green, float blue, float alpha) {
BitmapFont font;
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal(font_type[type]));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = size;
font = generator.generateFont(parameter);
font.setColor(red / 255f, green / 255f, blue / 255f, alpha);
return font;
}
#Override
public void render() {
if (firstFrame){
Gdx.gl.glClearColor(.2f, .67f, .88f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
f_game_name.draw(batch, t_game_name, camera.viewportWidth / 2 - 150, camera.viewportHeight - 100);
batch.end();
firstFrame = false;
} else {
if(loading){
loading=false;
loadEverything();
}
// Notice you don't actually render anything so what you previously drew stays on the screen.
}
super.render();
}
private void loadEverything(){
// load your font, assets, prefs, etc.
this.setScreen(loadingScreen);
// setScreen(loadingScreen);
}
#Override
public void dispose() {
batch.dispose();
assets.dispose();
f_game_name.dispose();
loadingScreen.dispose();
splashScreen.dispose();
mainScreen.dispose();
gameScreen.dispose();
settingsScreen.dispose();
}
}
Loading screen class:
package com.mindutis.game.Screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.mindutis.game.Application;
import com.mindutis.game.Engine.Database;
public class LoadingScreen implements Screen {
private final Application app;
private Database database;
private BitmapFont font_loading;
private boolean load;
private ShapeRenderer shapeRenderer;
private Stage stage;
private String t_loading;
private float progress_assets,progress_db;
private boolean firstFrame = true;
private boolean loading = true;
public LoadingScreen(Application app) {
this.app = app;
database = new Database();
this.stage = new Stage(new FitViewport(com.mindutis.game.Application.V_WIDTH, com.mindutis.game.Application.V_HEIGHT, app.camera));
this.shapeRenderer = new ShapeRenderer();
t_loading = "Loading...";
load=false;
}
private void queueAssets() {
//good food
//bread
app.assets.load("img/Food/breadtop.png", Texture.class);
app.assets.load("img/Food/breadbot.png", Texture.class);
//rest of the food
app.assets.load("img/Food/food_sheet.png", Texture.class);
//misc
app.assets.load("img/Misc/coin.png", Texture.class);
app.assets.load("img/Misc/soon.png", Texture.class);
app.assets.load("img/Misc/shoptriangle.png", Texture.class);
//buttons
app.assets.load("img/Buttons/button.png", Texture.class);
app.assets.load("img/Buttons/soundBT.png", Texture.class);
app.assets.load("img/Buttons/btX.png", Texture.class);
app.assets.load("img/Buttons/btShop1.png", Texture.class);
//human
app.assets.load("img/Human/human.png", Texture.class);
app.assets.load("img/Human/human1.png", Texture.class);
app.assets.load("img/Human/human2.png", Texture.class);
}
#Override
public void show() {
this.progress_assets = 0f;
this.progress_assets = 0f;
font_loading = app.initFont(50, 1, 255, 255, 255, 1);
queueAssets();
loading_db();
}
private void loading_db() {
if(database.get_first_time()){
//do some introduction for first time run
Gdx.app.log("db","first time worked");
database.set_first_time();
progress_db=1f;
}else{
Gdx.app.log("db","first time is set false");
progress_db=1f;
}
}
#Override
public void render(float delta) {
if (firstFrame){
Gdx.gl.glClearColor(.2f, .67f, .88f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
app.batch.begin();
app.f_game_name.draw(app.batch, app.t_game_name, app.camera.viewportWidth / 2 - 150, app.camera.viewportHeight - 100);
app.batch.end();
firstFrame = false;
} else {
if(loading){
loading=false;}
//input
Gdx.input.setCatchBackKey(true);
Gdx.input.setInputProcessor(new InputAdapter() {
#Override
public boolean keyDown(int keycode) {
if (keycode == Input.Keys.BACK) {
// Do nothing
}
return false;
}
});
Gdx.gl.glClearColor(.2f, .67f, .88f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
update(delta);
app.batch.begin();
font_loading.draw(app.batch, t_loading, app.camera.viewportWidth / 2 - 150, 123);
app.f_game_name.draw(app.batch, app.t_game_name, app.camera.viewportWidth / 2 - 150, app.camera.viewportHeight - 100);
app.batch.end();
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.WHITE);
shapeRenderer.rect(32, 50, app.camera.viewportWidth - 64, 16);
shapeRenderer.setColor(Color.GREEN);
shapeRenderer.rect(32, 50, (progress_assets+progress_db)/2 * (app.camera.viewportWidth - 64), 16);
shapeRenderer.end();
}
// Notice you don't actually render anything so what you previously drew stays on the screen.
}
private void update(float delta) {
progress_assets = MathUtils.lerp(progress_assets, app.assets.getProgress(), .1f);
if (app.assets.update() && (progress_assets+progress_db)/2 >= app.assets.getProgress() - .01f) {
app.setScreen(app.mainScreen);
}
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, false);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
shapeRenderer.dispose();
font_loading.dispose();
stage.dispose();
}
}
You can quickly load a single texture to show while everything else loads. Load and show the texture the first time render() is called. Then the second time render() is called, load your stuff, dispose the texture, and switch screens.
public class LoadingScreen extends Screen {
private boolean firstFrame = true;
private Texture texture;
private ExtendViewport viewport;
private MyGame game;
private SpriteBatch batch;
private static final float LOAD_IMAGE_WIDTH = 480, LOAD_IMAGE_HEIGHT = 600;
// Use whatever the loading image dimensions are.
public LoadingScreen (MyGame game, SpriteBatch batch){
this.game = game;
this.batch = batch;
viewport = new ExtendViewport(LOAD_IMAGE_WIDTH, LOAD_IMAGE_HEIGHT);
}
public void resize (int width, int height) {
viewport.update(width, height, false);
viewport.getCamera().position.set(LOAD_IMAGE_WIDTH / 2, LOAD_IMAGE_HEIGHT / 2, 0);
viewport.getCamera().update();
}
public void show (){} // do nothing
public void render (float delta) {
if (firstFrame){
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
viewport.apply();
batch.setProjectionMatrix(viewport.getCamera().combined);
batch.setBlendingDisabled(true);
batch.begin();
batch.draw(texture, 0, 0, LOAD_IMAGE_WIDTH, LOAD_IMAGE_HEIGHT);
batch.end();
firstFrame = false;
} else {
loadEverything();
// Notice you don't actually render anything so what you previously drew stays on the screen.
}
}
private void loadEverything(){
// load your font, assets, prefs, etc.
texture.dispose();
main.setGameScreen();
}
}

How fix in libgdx blinks screen in Desktop Mode?

when i run emulator and start application i see blinks screen, but on a PC in DesktopLauncher screen is normal.
I can not understand where I made mistakes.
render method override but it does not help.
class starter:
public class Drop extends Game {
Camera cam;
Game game;
SpriteBatch batch;
int tempGameScore = 0;
int dropsGatchered = 0;
Preferences preferences;//сохраняем игру
#Override
public void create() {
batch = new SpriteBatch();
this.setScreen(new MainMenuScreen(this));
}
#Override
public void render() {
super.render();
}
#Override
public void dispose() {
batch.dispose();
}
MainMenu:
I think I'm working wrong with the scene tool.
Can it is necessary as otherwise to draw a scene?
public class MainMenuScreen implements Screen {
Sprite texture;
final Drop game;
Skin skin;
Stage stage;
OrthographicCamera camera;
ImageButton newGameButton, exit, highScore;
Table table;
ImageButton.ImageButtonStyle btnplayStyle, btnscoreStyle, btnexitStyle;
private static TextureAtlas atlas, backAtlas;
public MainMenuScreen(final Drop gam) {
this.game = gam;
atlas = new TextureAtlas(Gdx.files.internal("texture/texture.pack"), true);
backAtlas = new TextureAtlas(Gdx.files.internal("texture/background.pack"), true);
texture = new Sprite(backAtlas.findRegion("background"));
skin = new Skin();
skin.addRegions(atlas);
table = new Table();
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
stage = new Stage();
stage.addActor(table);
Gdx.input.setInputProcessor(stage);// Make the stage consume events
table();
}
private void table() {
btnplayStyle = new ImageButton.ImageButtonStyle();
btnplayStyle.up = skin.getDrawable("play");//кнопка не нажата
btnplayStyle.over = skin.getDrawable("play");
btnplayStyle.down = skin.getDrawable("play"); // кнопка нажата
newGameButton = new ImageButton(btnplayStyle);
newGameButton.setSize(300, 200);
stage.addActor(newGameButton);
newGameButton.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new GameScreen(game));
}
});
//Button score
btnscoreStyle = new ImageButton.ImageButtonStyle();
btnscoreStyle.up = skin.getDrawable("records");//кнопка не нажата
btnscoreStyle.over = skin.getDrawable("records");
btnscoreStyle.down = skin.getDrawable("records"); // кнопка нажата
highScore = new ImageButton(btnscoreStyle);
highScore.setSize(300, 200);
stage.addActor(highScore);
highScore.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new Score(game) {
});
}
});
//Button EXIT
btnexitStyle = new ImageButton.ImageButtonStyle();
btnexitStyle.up = skin.getDrawable("exit");//кнопка не нажата
btnexitStyle.over = skin.getDrawable("exit");
btnexitStyle.down = skin.getDrawable("exit"); // кнопка нажата
exit = new ImageButton(btnexitStyle);
exit.setSize(300, 200);
stage.addActor(exit);
exit.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
game.batch.begin();
game.batch.draw(texture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
table.add(newGameButton).width(400).height(120);
table.getCell(newGameButton).spaceBottom(30);
table.row();
table.add(highScore).width(400).height(120);
table.getCell(highScore).spaceBottom(30);
table.row();
table.add(exit).width(400).height(120);
table.getCell(exit).spaceBottom(30);
table.row();
game.batch.end();
}
#Override
public void render(float delta) {
stage.act();
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
stage.dispose();
skin.dispose();
}
}
I think I'm working wrong with the scene tool
In MainMenuScreen you have to draw things in the render() method, not in the show() method. Like this:
#Override
public void render() {
stage.draw();
// ... possibly more drawing code
}

libGDX actor draw() not called

Please forgive me for my english.
Began to explore libGDX and have a problem. When I add actor on stage, method draw() not called.
Tried to apply the method to draw a straight line, texture successfully drawn but it is not an actor, and this method is not correct.
Help please.
SpiderHunt.java
package com.spiderhunt;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.spiderhunt.screens.MainMenu;
public class SpiderHunt extends Game{
private SpriteBatch batch;
public MainMenu mainMenu;
private static SpiderHunt instance = new SpiderHunt();
public SpiderHunt(){
}
public static SpiderHunt getInstance() {
return instance;
}
public void create () {
//load textures
Assets.load();
batch = new SpriteBatch();
mainMenu = new MainMenu(batch);
this.setScreen(mainMenu);
}
public void showMainMenu(){
setScreen(mainMenu);
}
public void render (float delta) {
}
public void resize(int width, int height) {
}
public void pause() {
}
public void resume() {
}
public void dispose() {
}
}
MainMenu.java
package com.spiderhunt.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.spiderhunt.Assets;
import com.spiderhunt.buttons.btnPlay;
public class MainMenu implements Screen {
public btnPlay playButton;
public Stage stage;
public SpriteBatch batch;
class GoToGameListener extends ClickListener {
#Override
public void clicked(InputEvent event, float x, float y) {
//some code for click or push
}
}
public MainMenu(SpriteBatch batch_1) {
batch = batch_1;
stage = new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), batch);
Gdx.input.setInputProcessor(stage);
playButton = new btnPlay(); //make actor
stage.addActor(playButton);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//make background
batch.begin();
batch.draw(Assets.bgMenuRegion, 0, 0, 540, 960);
batch.end();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw(); //this action must do method draw() from actor but it not called !!!!!!!!!!
//batch.begin();
//playButton.draw(batch, 0); THIS CODE DRAW BUTTON, BUT IT NOT CORRECTLY ACTOR
//batch.end();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
}
btnPlay.java
package com.spiderhunt.buttons;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.spiderhunt.Assets;
public class btnPlay extends Actor {
public btnPlay(){
setSize(100, 40);
setPosition(100, 100);
}
public void draw(SpriteBatch batch, float parentAlpha) {
//!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
batch.setColor(getColor());
batch.draw(Assets.btnPlayRegion, 0, 0);
}
}
Assets.java
package com.spiderhunt;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class Assets {
public static Texture atlas;
//backgrounds
public static TextureRegion bgMenuRegion;
public static TextureRegion bgSelectLevelRegion;
//buttons
public static TextureRegion btnPlayRegion;
//objects
public static TextureRegion objFlyRegion;
public static void load(){
atlas = new Texture("atlas.png");
bgMenuRegion = new TextureRegion(atlas, 0, 0, 540, 960);
btnPlayRegion = new TextureRegion(atlas, 1111, 1244, 418, 112);
}
}
Thanks for the help.
And now i find my bug.
I replace SpriteBatch to Batch and it work.
change
public void draw(SpriteBatch batch, float parentAlpha) {
//!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
batch.setColor(getColor());
batch.draw(Assets.btnPlayRegion, 0, 0);
}
to
#Override
public void draw(Batch batch, float parentAlpha) {
//!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
batch.setColor(getColor());
batch.draw(Assets.btnPlayRegion, 0, 0);
}
The problem I believe is at this line:
new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), batch);
Remove batch from the Stage Initialization change it to this:
new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
You are passing the batch from the Screen to the stage and you are calling batch.end() before stage.draw(). However stage has its own batch so you do not have to pass a batch for the stage to draw. If for your own reasons (e.g. batch projection matrix configuration) you still want your Screen batch passed to the stage do not call batch.end() before stage.draw() since your batch of the stage and your main batch are the same. Call batch.end() after stage.draw()

Detect clic on actor with ClickListener : no response?

EDIT:
Following Per's answer: I added this and it works fine :
private class GameScreen implements Screen {
private Stage mStage;
private InputMultiplexer inputMultiplexer = new InputMultiplexer();
public GameScreen() {
Gdx.input.setInputProcessor(inputMultiplexer);
mStage = new Stage(0, 0, true);
MyInput mi = new MyInput(){ //which implements inputProcessor
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
General.Log("gamescreen touchDown");
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// TODO Auto-generated method stub
return false;
}
};
inputMultiplexer.addProcessor(mi);
inputMultiplexer.addProcessor(mStage);
}
I would like to detect a click on a ui actor, I registered stage as the inputProcessor
Gdx.input.setInputProcessor(stage);
And I added this to my actor:
setBounds(0, 0, texture.getWidth(), texture.getHeight());
But still no response...
It is hard to help you without getting more info on your setup. But this code snippet works fine for me, look and see what you do different. And if you cant get it right create a small project with just the code that fails, easier for us to help you then.
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
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.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
public class MyGame extends Game {
#Override
public void create() {
setScreen(new GameScreen());
}
private class GameScreen implements Screen {
private Stage mStage;
public GameScreen() {
mStage = new Stage(0, 0, true);
}
#Override
public void render(float delta) {
mStage.act(delta);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
mStage.draw();
}
#Override
public void resize(int width, int height) {
Gdx.gl.glViewport(0, 0, width, height);
mStage.setViewport(width, height);
}
#Override
public void show() {
Texture texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 0, 0, 40, 40);
for (int y = 0; y < 4; ++y) {
for (int x = 0; x < 4; ++x) {
Image imageActor = new Image(region);
imageActor.setPosition(x * 50, y * 50);
imageActor.addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y) {
System.out.println("clicked");
}
});
mStage.addActor(imageActor);
}
}
Gdx.input.setInputProcessor(mStage);
}
#Override
public void hide() {}
#Override
public void pause() {}
#Override
public void resume() {}
#Override
public void dispose() {}
}
}
With the ApplicationListener:
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
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.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
public class MyGame implements ApplicationListener {
private Stage mStage;
#Override
public void create() {
mStage = new Stage(0, 0, true);
Texture texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 0, 0, 40, 40);
for (int y = 0; y < 4; ++y) {
for (int x = 0; x < 4; ++x) {
Image imageActor = new Image(region);
imageActor.setPosition(x * 50, y * 50);
imageActor.addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y) {
System.out.println("clicked");
}
});
mStage.addActor(imageActor);
}
}
Gdx.input.setInputProcessor(mStage);
}
#Override
public void resize(int width, int height) {
Gdx.gl.glViewport(0, 0, width, height);
mStage.setViewport(width, height);
}
#Override
public void render() {
mStage.act(Gdx.graphics.getDeltaTime());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
mStage.draw();
}
#Override
public void pause() {}
#Override
public void resume() {}
#Override
public void dispose() {}
}

Categories

Resources