Libgdx viewport not stretching - java

I'm currently having a problem creating a stretchedViewPort in libgdx.
As you can see in the image I added the gamescreen is not stretched over the whole screen and there are black spaces. I'm wondering what I'm doing wrong.
I provided the relevant parts of the code below.
Furthermore I also tried to use asign the viewport in to the stage in the oncreate function like this: m_stage = new Stage(m_viewport); But in that case I got a black screen and nothing was rendered (tough there were no compile errors).
Can someone point out what I'm doing wrong or what I'm missing.
Thanks in advance!
Kind regards,
Cavasta
package com.block.it;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
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;
import com.badlogic.gdx.utils.viewport.StretchViewport;
public class GameMenu implements Screen {
private BlockIt m_blockIt;
private Texture m_btexture = new Texture(Gdx.files.internal("mainmenu1.png"));
private Image m_background = new Image(m_btexture);
private Texture m_starttexture = new Texture(Gdx.files.internal("buttons/c_start.png"));
private Image m_startGame = new Image(m_starttexture);
private Texture m_optionstexture = new Texture(Gdx.files.internal("buttons/c_controls.png"));
private Image m_options = new Image(m_optionstexture);
private Texture m_highscoretexture = new Texture(Gdx.files.internal("buttons/c_highscores.png"));
private Image m_highscore = new Image(m_highscoretexture);
private Texture m_continueTexture = new Texture(Gdx.files.internal("buttons/c_resume_game.png"));
private Image m_continue = new Image(m_continueTexture);
private Texture m_playerIcon = new Texture(Gdx.files.internal("player/player_move_front.png"));
private Image m_player = new Image(m_playerIcon);
private int m_buttonPressed = -1;
private boolean m_gameInProgress = false;
private StretchViewport m_viewport;
private PerspectiveCamera m_camera;
private Stage m_stage = new Stage();
public GameMenu(BlockIt blockIt, boolean gameInProgress) {
m_blockIt = blockIt;
m_camera = new PerspectiveCamera();
m_viewport = new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), m_camera);
m_viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
m_stage = new Stage();
}
#Override
public void show() {
if (!m_gameInProgress) {
m_startGame.setX(227);
m_startGame.setY(270);
m_startGame.addListener(new ClickListener() {
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
// m_blockIt.startNewGame();
}
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
// switch to a new texture
/*Texture newTexture = new Texture(Gdx.files.internal("buttons/c_new_game.png"));
m_startGame.setDrawable(new SpriteDrawable(new Sprite(newTexture)));
*/
m_player.setX(-64);
m_player.setY(300);
m_stage.addActor(m_player);
m_buttonPressed = 0;
return true;
}
});
} else {
m_continue.setX(227);
m_continue.setY(270);
m_continue.addListener(new ClickListener() {
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
}
});
}
m_stage.addActor(m_background); //adds the image as an actor to the stage
if (m_gameInProgress) {
m_stage.addActor(m_continue);
} else {
m_stage.addActor(m_startGame);
}
Gdx.input.setInputProcessor(m_stage);
}
#Override
public void render(float delta) {
m_camera.update();
Gdx.gl.glClearColor(0, 0, 0, 1); //sets clear color to black
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); //clear the batch
m_stage.act(); //update all actors
m_stage.draw(); //draw all actors on the Stage.getBatch()
}
#Override
public void resize(int width, int height
) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}

You have to set the camera and viewport to the value u used to program the world:
eg. If your world is 800*480:
camera = new OrthographicCamera(800,480);
fitViewport = new FitViewport(800, 480, camera);

You have to update the viewport when the window is resized:
#Override
public void resize(int width, int height) {
m_viewport.update(width, height, true);
}

Related

Why are my actors not being drawn with the correct bounds in LibGDX?

I have been having a hell of a time trying to get this stage thing figured out. I basically want to draw a background image that fills the entire stage. The image is 1920x1080. The problem is that the image is being drawn at half the width and half of the height that it should be. The application is set to fullscreen and the inside corner of the image is at the center of the screen. I have tested the dimensions of the camera, the viewport, and the stage. All are correct. What am I doing wrong? By the way, I am using a custom game state manager to handle different screens so I apologize for the seemingly needless extra code.
Application.java
public class Application extends ApplicationAdapter {
public static boolean DEBUG = false;
public static final String TITLE = "Hexatan";
public static final int V_WIDTH = 720, V_HEIGHT = 480;
public static final float SCALE = 2.0f;
private GameStateManager gsm;
private OrthographicCamera camera;
private FitViewport vp;
private SpriteBatch batch;
private Stage stage;
private float w, h;
#Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
batch = new SpriteBatch();
camera = new OrthographicCamera();
camera.setToOrtho(true, w / SCALE, h / SCALE);
vp = new FitViewport(w, h, camera);
stage = new Stage(vp, batch);
gsm = new GameStateManager(this);
}
#Override
public void render() {
gsm.update(Gdx.graphics.getDeltaTime());
gsm.render();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
gsm.dispose();
batch.dispose();
}
#Override
public void resize(int width, int height) {
gsm.resize((int)(width / SCALE), (int)(height / SCALE));
}
public SpriteBatch getBatch() {
return this.batch;
}
public OrthographicCamera getCamera(){
return this.camera;
}
public Stage getStage(){
return this.stage;
}
}
GameStateManager.java
public class GameStateManager {
private final Application app;
private Stack<GameState> states;
public enum State {
SPLASH, MAINMENU, GAME
}
public GameStateManager(final Application app){
this.app = app;
this.states = new Stack<GameState>();
this.setState(State.GAME);
}
public Application getApp(){
return app;
}
public void update(float delta){
states.peek().update(delta);
}
public void render(){
states.peek().render();
}
public void dispose(){
for(GameState gs : states){
gs.dispose();
}
states.clear();
}
public void resize(int w, int h){
states.peek().resize(w, h);
}
public void setState(State state){
if(states.size() >= 1) {
states.pop().dispose();
}
states.push(getState(state));
}
public GameState getState(State state){
return new GameLoop(this);
}
}
GameState.java
public abstract class GameState {
protected GameStateManager gsm;
protected Application app;
protected SpriteBatch batch;
protected OrthographicCamera camera;
public GameState(GameStateManager gsm){
this.gsm = gsm;
this.app = gsm.getApp();
batch = app.getBatch();
camera = app.getCamera();
}
public void resize(int w, int h){
camera.setToOrtho(true, w, h);
camera.update();
}
public abstract void update(float delta);
public abstract void render();
public abstract void dispose();
}
GameLoop.java
public class GameLoop extends GameState {
private boolean DEV_MODE = true;
private int WIDTH, HEIGHT;
private float mouseX, mouseY;
private Stage stage;
private Game game;
private Application app;
Texture hexture, water, island, robber, highlight, scoreboard;
BitmapFont font = new BitmapFont(Gdx.files.internal("core/assets/Playbill.fnt"));
BitmapFont devFont = new BitmapFont(Gdx.files.internal("core/assets/Arial.fnt"));
public GameLoop(GameStateManager gsm){
super(gsm);
app = gsm.getApp();
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
stage = app.getStage();
System.out.println(stage.getCamera().position.x);
Gdx.input.setInputProcessor(stage);
water = new Texture(Gdx.files.internal("core/assets/img/water.png"));
island = new Texture(Gdx.files.internal("core/assets/img/island.png"));
highlight = new Texture(Gdx.files.internal("core/assets/img/highlightVertex.png"));
scoreboard = new Texture(Gdx.files.internal("core/assets/img/scoreboard.png"));
Image waterImg = new Image(new TextureRegionDrawable(new TextureRegion(water)));
waterImg.setWidth(WIDTH);
waterImg.setHeight(HEIGHT);
stage.addActor(waterImg);
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
app.getStage().act(Gdx.graphics.getDeltaTime());
app.getStage().draw();
}
#Override
public void update(float delta) {
}
#Override
public void dispose() {
}
#Override
public void resize(int w, int h){
camera.setToOrtho(true, w, h);
app.getStage().getViewport().update(w, h, true);
}

libgdx world to screen pos and factors

I want to draw a texture on a body, which is a box.
How do I convert the coordinates of the body to screen coordinates?
I know that the other way around is with camera.unproject(pos), is it similar to this?
I see a lot of people using constants such as WORLD_TO_SCREEN = 32, but I currently don't have that in my game. Is that a problem, and how can I implement it now? Because it seems like people that are using these factors can convert world to screen positions easily. I currently have a camera and an ExtendViewport
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
viewport = new ExtendViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, camera);
camera.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0f);
Viewport width and height are set to this
public static final int VIEWPORT_WIDTH = 20;
public static final int VIEWPORT_HEIGHT = 22;
I don't really know what I'm doing, I read documentation and read some tutorials, but if someone could give some explanation about these variables and the world_to_screen factor that would really help me out.
I also set APP_WIDTH=1280, APP_HEIGHT = 720
Do viewport width and height mean that for box2d my screen is 20 meters wide and 22 meters high?
(asking it again because I added a lot the question and I would really like to know these things)
[EDIT]
So I'm trying to draw a ground picture on the ground body
float x = stage.getGround().getX();
float y = stage.getGround().getY();
float w = stage.getGround().getWidth();
float h = stage.getGround().getHeight();
stage.act(delta);
stage.draw();
stage.updateCamera();
Texture texture = new Texture(Gdx.files.internal("ground.png"));
Sprite sprite = new Sprite(texture);
sprite.setSize(w, h);
sprite.setPosition(x-sprite.getWidth()/2, y-sprite.getHeight()/2);
But I don't see it anywhere
[EDIT 2]
Stage Class
public class Mission1Stage extends Stage{
public static final int VIEWPORT_WIDTH = 20;
public static final int VIEWPORT_HEIGHT = 22;
private World world;
private Ground ground;
private LeftWall leftWall;
private Rocket rocket;
private static final float TIME_STEP = 1 / 300f;
private float accumulator = 0f;
private OrthographicCamera camera;
private Box2DDebugRenderer renderer;
private Viewport viewport;
private SpriteBatch spriteBatch = new SpriteBatch();
private Vector3 touchPoint;
private ShapeRenderer shapeRenderer;
private Button boostButton;
private Skin boostSkin;
private Button boostLeftButton;
private Skin boostLeftSkin;
private Button boostRightButton;
private Skin boostRightSkin;
private Button resetButton;
private Skin resetSkin;
private Game game;
private boolean isTouched = false;
public Mission1Stage(Game game) {
setUpWorld();
renderer = new Box2DDebugRenderer();
shapeRenderer = new ShapeRenderer();
setupCamera();
setUpButtons();
addActor(new Background(ground));
}
private void setUpWorld() {
world = WorldUtils.createWorld();
setUpGround();
setUpRocket();
}
private void setUpGround() {
ground = new Ground(WorldUtils.createGround(world));
addActor(ground);
}
private void setUpLeftWall() {
leftWall = new LeftWall(WorldUtils.createLeftWall(world));
}
private void setUpRocket() {
rocket = new Rocket(WorldUtils.createRocket(world));
addActor(rocket);
}
private void setupCamera() {
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
viewport = new ExtendViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, camera);
camera.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0f);
camera.update();
}
private void setUpButtons() {
boostSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
boostButton = new Button(boostSkin);
boostButton.setSize(80,80);
boostButton.setPosition(Gdx.graphics.getWidth()-boostButton.getWidth()*2,0);
boostButton.setTransform(true);
boostButton.scaleBy(0.5f);
Gdx.input.setInputProcessor(this);
addActor(boostButton);
boostLeftSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
boostLeftButton = new Button(boostLeftSkin);
boostLeftButton.setSize(100, 100);
boostLeftButton.setPosition(0, 0);
addActor(boostLeftButton);
boostRightSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
boostRightButton = new Button(boostRightSkin);
boostRightButton.setSize(100, 100);
boostRightButton.setPosition(boostLeftButton.getWidth(), 0);
addActor(boostRightButton);
resetSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
resetButton = new Button(resetSkin);
resetButton.setSize(100, 100);
resetButton.setPosition(Gdx.graphics.getWidth()-100, Gdx.graphics.getHeight()-100);
addActor(resetButton);
}
#Override
public void act(float delta) {
super.act(delta);
handleInput();
accumulator += delta;
while(accumulator >= delta) {
world.step(TIME_STEP, 6, 2);
accumulator -= TIME_STEP;
}
}
#Override
public void draw() {
super.draw();
renderer.render(world, camera.combined);
float x = getGround().getBody().getPosition().x;
float y = getGround().getBody().getPosition().y;
float w = getGround().getWidth() * 2;
float h = getGround().getHeight() * 2;
spriteBatch.setProjectionMatrix(getCamera().combined);
Texture texture = new Texture(Gdx.files.internal("ground.png"));
Sprite sprite = new Sprite(texture);
sprite.setSize(w, h);
sprite.setPosition(x-sprite.getWidth()/2, y-sprite.getHeight()/2);
spriteBatch.begin();
sprite.draw(spriteBatch);
spriteBatch.end();
}
public void handleInput() {
if(boostButton.isPressed()) {
rocket.boost();
}
if(boostLeftButton.isPressed()) {
rocket.turnLeft();
}
if(boostRightButton.isPressed()) {
rocket.turnRight();
}
if(resetButton.isPressed()) {
}
}
public boolean resetScreen() {
if(resetButton.isPressed()) return true;
return false;
}
public void updateCamera() {
}
public Ground getGround() {
return ground;
}
public void resize(int width, int height) {
viewport.update(width, height);
camera.position.x = VIEWPORT_WIDTH / 2;
camera.position.y = VIEWPORT_HEIGHT /2;
}
private void translateScreenToWorldCoordinates(int x, int y) {
getCamera().unproject(touchPoint.set(x, y, 0));getCamera();
}
}
Screen class
public class Mission1Screen implements Screen{
private Game game;
private Mission1Stage stage;
private SpriteBatch spriteBatch = new SpriteBatch();
private Skin boostSkin;
private Button boostButton;
public Mission1Screen(Game game) {
this.game = game;
stage = new Mission1Stage(game);
}
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(stage.resetScreen()) {
game.setScreen(new Mission1Screen(game));
}
stage.act(delta);
stage.draw();
stage.updateCamera();
}
#Override
public void resize(int width, int height) {
stage.resize(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
[EDIT 3]
public class Main extends Game {
#Override
public void create () {
this.setScreen(new Mission1Screen(this));
}
#Override
public void render () {
super.render();
}
#Override
public void dispose () {
}
}
We mostly use Pixel to meter conversion because box2d best works in meters (0-10) but you can avoid this conversion by using small worldwidth and height of your viewport. I mostly prefer 48 and 80 as viewport width and height.
You can use unproject(vector3) method of camera that translate a point given in screen coordinates to world space. I am using this method in touchdown because I get screen coordinate as parameter then I need to convert it into camera world space so that I can generate object at a particular position in world.
public class MyGdxTest extends Game implements InputProcessor {
private SpriteBatch batch;
private ExtendViewport extendViewport;
private OrthographicCamera cam;
private float w=20;
private float h=22;
private World world;
private Box2DDebugRenderer debugRenderer;
private Array<Body> array;
private Vector3 vector3;
#Override
public void create() {
cam=new OrthographicCamera();
extendViewport=new ExtendViewport(w,h,cam);
batch =new SpriteBatch();
Gdx.input.setInputProcessor(this);
world=new World(new Vector2(0,-9.8f),true);
array=new Array<Body>();
debugRenderer=new Box2DDebugRenderer();
vector3=new Vector3();
BodyDef bodyDef=new BodyDef();
bodyDef.type= BodyDef.BodyType.StaticBody;
bodyDef.position.set(0,0);
Body body=world.createBody(bodyDef);
ChainShape chainShape=new ChainShape();
chainShape.createChain(new float[]{1,1,55,1});
FixtureDef fixtureDef=new FixtureDef();
fixtureDef.shape=chainShape;
fixtureDef.restitution=.5f;
body.createFixture(fixtureDef);
chainShape.dispose();
}
#Override
public void render() {
super.render();
Gdx.gl.glClearColor(0,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
world.step(1/60f,6,2);
batch.setProjectionMatrix(cam.combined);
batch.begin();
world.getBodies(array);
for (Body body:array){
if(body.getUserData()!=null) {
Sprite sprite = (Sprite) body.getUserData();
sprite.setPosition(body.getPosition().x-sprite.getWidth()/2, body.getPosition().y-sprite.getHeight()/2);
sprite.setRotation(body.getAngle()*MathUtils.radDeg);
sprite.draw(batch);
}
}
batch.end();
debugRenderer.render(world,cam.combined);
}
#Override
public void resize(int width, int height) {
super.resize(width,height);
extendViewport.update(width,height);
cam.position.x = w /2;
cam.position.y = h/2;
cam.update();
}
private void createPhysicsObject(float x,float y){
float sizeX=2,sizeY=2;
BodyDef bodyDef=new BodyDef();
bodyDef.position.set(x,y);
bodyDef.type= BodyDef.BodyType.DynamicBody;
Body body=world.createBody(bodyDef);
PolygonShape polygonShape=new PolygonShape();
polygonShape.setAsBox(sizeX,sizeY);
FixtureDef fixtureDef=new FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.restitution=.2f;
fixtureDef.density=2;
body.createFixture(fixtureDef);
body.setFixedRotation(false);
polygonShape.dispose();
Sprite sprite=new Sprite(new Texture("badlogic.jpg"));
sprite.setSize(2*sizeX,2*sizeY);
sprite.setPosition(x-sprite.getWidth()/2,y-sprite.getHeight()/2);
sprite.setOrigin(sizeX,sizeY);
body.setUserData(sprite);
}
#Override
public void dispose() {
batch.dispose();
debugRenderer.dispose();
world.dispose();
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
vector3.set(screenX,screenY,0);
Vector3 position=cam.unproject(vector3);
createPhysicsObject(vector3.x,vector3.y);
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return 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();
}
}

Issue with sounds in libgdx

I am having issues with libgdx sound inside a ClickListener for my button.
I am getting the error Syntax error on token "PlayButtonSound", Identifier expected after this token
SoundManager:
import utils.Constants;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
public class SoundManager {
private static Sound buttonSound = Gdx.audio.newSound(Constants.buttonSound);
private static Music song = Gdx.audio.newMusic(Constants.song);
public static void PlayMusic() {
song.setLooping(true);
song.setVolume(0.2f);
song.play();
}
public static void PlayButtonSound() {
buttonSound.play();
}
public void DestryoAudio() {
buttonSound.dispose();
song.dispose();
}
}
And MainMenu:
public class MainMenu implements Screen {
private Stage stage;
private Sprite sprite;
private TextureRegion menuBackgroundImg;
private TextureAtlas menuButton;
private Skin skin;
private BitmapFont font;
private Table table;
private TextButton playButton;
#Override
public void show() {
// Set stage
stage = new Stage();
Gdx.input.setInputProcessor(stage);
SoundManager.PlayMusic();
// Set menu baggrund
menuBackgroundImg = new TextureRegion(new Texture(Gdx.files.internal(Constants.menuBackgroundImg)));
sprite = new Sprite(menuBackgroundImg);
sprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
// Set menuknapper
menuButton = new TextureAtlas(Constants.menuButton);
skin = new Skin(menuButton);
// Set font
font = new BitmapFont(Constants.font, false);
// Set table
table = new Table(skin);
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
// Create button styling
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("menuButtonUp");
textButtonStyle.down = skin.getDrawable("menuButtonPressed");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.pressedOffsetY = -1;
textButtonStyle.font = font;
textButtonStyle.fontColor = Color.BLACK;
// Create buttons
playButton = new TextButton(Constants.play, textButtonStyle);
playButton.addListener(new InputListener() {
SoundManager.PlayButtonSound(); // This is the error
});
// Button padding
playButton.pad(20, 100, 20, 100);
// Setting the table
table.add(playButton).row();
// Setting stage actor
stage.addActor(table);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// Tegn baggrund
stage.getBatch().begin();
sprite.draw(stage.getBatch());
stage.getBatch().end();
// Tegn resten
stage.act(delta);
stage.draw();
}
...
#Override
public void dispose() {
stage.dispose();
menuButton.dispose();
skin.dispose();
font.dispose();
}
}
I can't really grasp what I am doing wrong and a search for the error gives vague answers that dosen't really solves my issue.
P.S. I have imported SoundManager but left it out due to length of the code snippet.
You need to implement one of the InputListener interface methods, most likely touchDown(InputEvent event, float x, float y, int pointer, int button).
Check out the API for InputListener. It lists all the methods and gives a pretty good example.
playButton.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
SoundManager.PlayButtonSound();
return false;
}
});

How to zoom in and out on a stage in libgdx Scene2d?

I am making a 2d game using libgdx and am adding hexagon shaped actors to a group which is then added to a stage. For a normal camera you can use camera.zoom in the render method to zoom in and out along with camera.translate to pan around the world.
I have been getting the camera used by the stage using stage.getCamera() and I can still call stage.getcamera().translate however there is no stage.getCamera().zoom option.
Here is my code:
//import statements
public class HexGame implements ApplicationListener{
private Stage stage;
private Texture hexTexture;
private Group hexGroup;
private int screenWidth;
private int screenHeight;
#Override
public void create() {
hexTexture = new Texture(Gdx.files.internal("hex.png"));
screenHeight = Gdx.graphics.getHeight();
screenWidth = Gdx.graphics.getWidth();
stage = new Stage(new ScreenViewport());
hexGroup = new HexGroup(screenWidth,screenHeight,hexTexture);
stage.addActor(hexGroup);
}
#Override
public void dispose() {
stage.dispose();
hexTexture.dispose();
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
handleInput();
stage.getCamera().update();
}
private void handleInput() {
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
stage.getCamera().translate(-3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
stage.getCamera().translate(3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
stage.getCamera().translate(0, -3, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
stage.getCamera().translate(0, 3, 0);
}
//This is the part that doesn't work
/*
if (Gdx.input.isKeyPressed(Input.Keys.Z)) {
stage.getCamera().zoom += 0.02;
}
*/
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
Any help is appreciated, and if there is anything else wrong with my code please let me know, I'm new to libgdx. Thanks
Zoom is available in OrthographicCamera class and by default Stage class create a OrthographicCamera
/** Creates a stage with a {#link ScalingViewport} set to {#link Scaling#stretch}. The stage will use its own {#link Batch}
* which will be disposed when the stage is disposed. */
public Stage () {
this(new ScalingViewport(Scaling.stretch, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), new OrthographicCamera()),
new SpriteBatch());
ownsBatch = true;
}
So what you need is to cast your camera to OrthographicCamera:
((OrthographicCamera)stage.getCamera()).zoom += 0.02f;

Categories

Resources