Create project files using LibGDX-Setup-UI as
Next Import them to a workspace
Next delete implements ApplicationListener and put extends Game as
After adding import statement and method using Eclipse, I also entered the methods for render and dispose as
Here I just log some details. When I close the window, I get
We can see the frame rate is a little more than 60 frames per second.
Any real program depends on implenting different Screens. For the simplest case, my Screens are 2 static images. The first one is called Splash Screen which will last for 5 seconds. The next is Main Screen which will exist after Splash Screen.
This is the Splash image:
This is the Main image:
First I have to add a class for implementing Screen
The log output after running the program (which is listed afterwards):
MySplashGame.java
package com.tutorials.mysplashgame;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.TimeUtils;
public class MySplashGame extends Game {
private int rendCount; //** render count **//
private long startTime; //** time app started **//
private long endTime; //** time app ended **//
@Override
public void create() {
Gdx.app.log("my Splash Game","App created");
startTime = TimeUtils.millis();
setScreen(new MySplashScreen(this)); //** start SpashSreen, with Game parameter **//
}
public void render() {
super.render();
rendCount++; //** another render - inc count **//
}
public void dispose() {
Gdx.app.log("my Splash Game", "App rendered " + rendCount + " times");
Gdx.app.log("my Splash Game", "App ended");
endTime = TimeUtils.millis();
Gdx.app.log("my Splash Game", "App running for " + (endTime-startTime)/1000 + " seconds.");
}
}
MySplashScreen.java
package com.tutorials.mysplashgame;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.TimeUtils;
public class MySplashScreen implements Screen{
private SpriteBatch batch;
private Game myGame;
private Texture texture;
private OrthographicCamera camera;
private long startTime;
private int rendCount;
public MySplashScreen(Game g) // ** constructor called initially **//
{
Gdx.app.log("my Spash Screen", "constructor called");
myGame = g; // ** get Game parameter **//
camera = new OrthographicCamera();
camera.setToOrtho(false, 512, 512);
batch = new SpriteBatch();
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(texture, 0, 0);
batch.end();
rendCount++;
if (TimeUtils.millis()>(startTime+5000)) myGame.setScreen(new MyMainScreen(myGame));
}
@Override
public void resize(int width, int height) {
}
@Override
public void show() {
Gdx.app.log("my Splash Screen", "show called");
texture = new Texture(Gdx.files.internal("splash.png")); //** texture is now the splash image **//
startTime = TimeUtils.millis();
}
@Override
public void hide() {
Gdx.app.log("my Splash Screen", "hide called");
Gdx.app.log("my Splash Screen", "rendered " + rendCount + " times.");
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
texture.dispose();
batch.dispose();
}
}
MyMainScreen.java
package com.tutorials.mysplashgame;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MyMainScreen implements Screen {
private SpriteBatch batch;
private Game myGame;
private Texture texture;
private OrthographicCamera camera;
public MyMainScreen(Game g) // ** constructor called initially **//
{
Gdx.app.log("my Main Screen", "constructor called");
myGame = g; // ** get Game parameter **//
camera = new OrthographicCamera();
camera.setToOrtho(false, 512, 512);
batch = new SpriteBatch();
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(texture, 0, 0);
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void show() {
Gdx.app.log("my Main Screen", "show called");
texture = new Texture(Gdx.files.internal("main.png")); //** texture is now the main image **//
}
@Override
public void hide() {
Gdx.app.log("my Main Screen", "hide called");
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
Gdx.app.log("my Main Screen", "dispose called");
texture.dispose();
batch.dispose();
}
}
Very good, thank you
ReplyDeletethat was very use full..thanks a lot..
ReplyDeleteHow about adding to the overall programming picture- that you unfortunately have 0 musical talent Whatsoever!! LoLoL
ReplyDeleteHi,
ReplyDeleteA very good tutorial.
Including the png-images would make it even better for us beginners. It is good to see the pictures and their size.
Regards
It works, but I changed it (of course)
ReplyDeleteWouldn't it make more sense for a splash screen to change to the main game when a button is pressed, instead of after a certain delay?