I am trying to draw some lines for debugging using libgdx but I failed. this is my code
public class MainMenu extends Screen implements InputProcessor {
private OrthographicCamera camera;
SpriteBatch myBatch;
ShapeRenderer shapeDebugger;
public MainMenu(Game game) {
super(game);
camera= new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.update();}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
myBatch.begin();
stage.draw();
Gdx.gl10.glLineWidth(10);
shapeDebugger.setProjectionMatrix(camera.combined);
shapeDebugger.begin(ShapeType.Line);
shapeDebugger.setColor(1, 1, 1, 1);
shapeDebugger.line(2, 2, 5, 5);
myBatch.end();
}
}
I get an error from line
shapeDebugger.setProjectionMatrix(camera.combined);
#Pranav008
thank you very much. I didn't expect that I need to initiate it. but I've got a true problem. I draw the line to center of the game screen like this.
Gdx.gl10.glLineWidth(2);
shapeDebugger.setProjectionMatrix(camera.combined);
shapeDebugger.begin(ShapeType.Line);
shapeDebugger.setColor(1, 1, 1, 1);
shapeDebugger.line(Gdx.graphics.getWidth()/2, 0,Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight());
shapeDebugger.end();
when I try to resize the screen it doesn't update the to center it goes far away to right.
You must have got nullpointerException because you haven't made any object of ShapeRenderer.
Insert this line in your constructor.
shapeDebugger=new ShapeRenderer();
Remember that nesting Shaperender with a SpriteBatch may cause problems.
Check this link.
My answer is mayby too late for you, but for people how have the same positioning problems.
Your second question about the position after a resize is because the viewport did not change.
Aldo your window size changed your application stil is using the same pixel size set on create by the function camera.setToOrtho
Update the view port on a resize!
//-----------------------------------------------------------------
#Override
public void resize (int width, int height) {
camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.update();
}
Define & initialize ShapeRenderer
ShapeRenderer shapeDebugger;
#Override
public void create() {
shapeDebugger = new ShapeRenderer();
...
Draw line in render callback
#Override
public void render() {
//render scene
Gdx.gl.glClearColor(69f / 255, 90f / 255, 100f / 255, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
...
shapeDebugger.setProjectionMatrix(camera.combined);
shapeDebugger.begin(ShapeRenderer.ShapeType.Line);
Gdx.gl.glLineWidth(10 / camera.zoom);
shapeDebugger.setColor(1, 0, 0, 1);
shapeDebugger.line(screenWidth / 2, 0, screenWidth / 2, screenHeight);
shapeDebugger.line(0, screenHeight / 2, screenWidth, screenHeight / 2);
shapeDebugger.end();
Related
Ok, I'll just come out with it. I'm extremely new to Android coding. I'm also doing it solely with AIDE on my phone.
I want to open another class labeled aboutgame.java
I'm not sure how to post my code, but here goes.
package com.bernco.screenoff;
import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
public class MyGdxGame implements ApplicationListener
{
Texture texture;
Texture pwrdby;
Texture about;
SpriteBatch batch;
SpriteBatch pwrbtch;
SpriteBatch abtbtch;
#Override
public void create()
{
texture = new Texture(Gdx.files.internal("dark-android.jpg"));
about = new Texture(Gdx.files.internal("about.png"));
pwrdby = new Texture(Gdx.files.internal("powered-by.png"));
batch = new SpriteBatch();
abtbtch = new SpriteBatch();
pwrbtch = new SpriteBatch();
}
#Override
public void render()
{
Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
abtbtch.begin();
abtbtch.draw(about, Gdx.graphics.getWidth() - about.getWidth() / 2, Gdx.graphics.getHeight() - about.getHeight() / 2,
Gdx.graphics.getWidth() / 8, Gdx.graphics.getWidth() / 8);
abtbtch.end();
batch.begin();
batch.draw(texture, Gdx.graphics.getWidth() / 80, 0,
Gdx.graphics.getWidth() / 1, Gdx.graphics.getWidth() / 2);
batch.end();
pwrbtch.begin();
pwrbtch.draw(pwrdby, Gdx.graphics.getWidth() / 80, 0,
Gdx.graphics.getWidth() / 4, Gdx.graphics.getWidth() / 3);
pwrbtch.end();
}
#Override
public void dispose()
{
}
#Override
public void resize(int width, int height)
{
}
#Override
public void pause()
{
}
#Override
public void resume()
{
}
}
welcome to StackOverflow!
At a high level, what you need for a multi-screen game is one class to represent your application, and a bunch of other classes to represent the different screens in your game.
In libGDX, this means that instead of your MyGdxGame class implementing ApplicationListener, you want to have it extend the Game class. This is a utility class that libGDX provides that gives you some of the framework code needed to manage screens. Instead of drawing itself, this class will tell one of your screen classes to draw itself instead. Then when you want to change screens, you call Game#setScreen(...) to point it towards a new screen.
Each of your screen classes then will need to implement the Screen interface, which will show you the methods you need to implement for them to work with your game.
I recommend you take a look at two tutorials on the official libGDX wiki: A Simple Game and Extending the Simple Game. They give examples for both Desktop and Android, and the latter walks you through examples of how to use Game and Screen.
I try to implement a custom Actor which displays contents of a FrameBuffer by extending Image. Inside I'd like to draw using a ShapeRenderer.
public class CustomActor extends Image {
private FrameBuffer frameBuffer = null;
private ShapeRenderer shapeRenderer = new ShapeRenderer();
private int width;
private int height;
public CustomActor() {
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
shapeRenderer.setAutoShapeType(true);
}
#Override
public void draw(Batch batch, float parentAlpha) {
if (frameBuffer == null) {
frameBuffer = new FrameBuffer(Pixmap.Format.RGB565, width / 2, height / 2, false);
TextureRegion textureRegion = new TextureRegion(frameBuffer.getColorBufferTexture());
textureRegion.flip(false, true);
setDrawable(new TextureRegionDrawable(textureRegion));
}
frameBuffer.begin();
// render content
Gdx.gl.glClearColor(0, 1, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.WHITE);
shapeRenderer.rect(0, 0, frameBuffer.getWidth() / 2, frameBuffer.getHeight() / 2);
shapeRenderer.end();
if (frameBuffer != null) {
frameBuffer.end();
}
super.draw(batch, parentAlpha);
}
}
When deleting the ShapeRenderer part I can see the green background in the scene. But as soon as I use the ShapeRenderer the content is black. There is no white rectangle visible.
Any ideas what I am doing wrong?
Just found out that using my own SpriteBatch (with begin() and end()) after the FrameBuffer end() it works. So the problem seems to have to do with the calling order of the FrameBuffers methods and the SpriteBatches methods. Ending the given actor batch before beginning the FrameBuffer and "re"-beginning it to draw the actor is also working (see comment above).
I've tried to scale down a 512 x 256 image of a rocket by setting the width and height directly. When I launch it the rocket image is pixely and very bad quality even though it is supposed a crisp vector image. I wanted to know how I would properly scale this down to still retain the quality.
public GameScreen(){
cam = new OrthographicCamera();
cam.setToOrtho(false, 600, 800);
batch = new SpriteBatch();
img = new Texture(Gdx.files.internal("data/rocket.png"));
batch.setProjectionMatrix(cam.combined);
}
#Override
public void show() {
}
#Override
public void render(float delta) {
cam.update();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img,10, 10, 100, 60);
batch.end();
}
As Tenfour04 mentioned in comments you need to initialize Texture class with mipmaps enabled. Then set texture filters using setFilter method.
img = new Texture(Gdx.files.internal("data/rocket.png"), true);
img.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.Nearest);
I'm new to LibGDX and I am trying to get my screen resolution sizes set up first before I get into the actual game itself. Before I added extendViewport I had an orthographic Camera and an Image that displayed. But when I got rid of the orthographic camera and added the Viewport the image disappeared. Here is the code i have for the the screen.
public class GameScreen implements Screen {
SlingshotSteve game;
private ExtendViewport viewport;
PerspectiveCamera camera;
public void Menu(SlingshotSteve game){
this.game = game;
}
SpriteBatch batch;
TextureRegion backgroundTexture;
Texture texture;
GameScreen(final SlingshotSteve gam) {
this.game = gam;
batch = new SpriteBatch();
Texture texture = new Texture(Gdx.files.internal("background.jpg"));
backgroundTexture = new TextureRegion(texture, 0, 0, 500, 500);
Music mp3Sound = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
mp3Sound.setLooping(true);
mp3Sound.play();
}
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(backgroundTexture, 0, 0, SlingshotSteve.WIDTH, SlingshotSteve.HEIGHT);
batch.end();
}
#Override
public void resize(int width, int height) {
viewport.update(width, height);
}
#Override
public void dispose() {
batch.dispose();
texture.dispose();
}
#Override
public void show() {
camera = new PerspectiveCamera();
viewport = new ExtendViewport(800, 480, camera);
}
}
The other question I have is that this screen is the main game screen where Level 1 is going to occur. Since I added a main menu I had to switch from the "Application Listener" to the "Implement Screen". Is this correct, or do I have to go back to the Application Listener to get the Create() method? I'm not completely sure what everything means so If someone could please explain this to me that would be great!
I'm trying to rendering smooth scalable bitmap fonts. After checking this question one of the answers mentioned using distance field fonts.
I'm doing exactly as mentioned in LibGDX wiki article about distance filed fonts. However I can't get it working. Fonts are rendered hazy.
Here's the code I used to generate this output
public class FontRenderTest implements ApplicationListener {
private Texture texture;
private SpriteBatch spriteBatch;
private BitmapFont font;
#Override
public void create() {
spriteBatch = new SpriteBatch();
Texture texture = new Texture(Gdx.files.internal("Raleway.png"), true); // true enables mipmaps
texture.setFilter(TextureFilter.MipMapLinearNearest, TextureFilter.Linear); // linear filtering in nearest mipmap image
font = new BitmapFont(Gdx.files.internal("Raleway.fnt"), new TextureRegion(texture), false);
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
font.draw(spriteBatch, "This is hazy !!", 100, 150);
spriteBatch.end();
}
}
I'm not sure if I properly understand the function of distance field font. If anyone could explain how to render font smooth.
I think it needs a shader and if I recall right the shaders require GL20. As it said in the wiki you would need .frag and .vert files. I modified your code with the help from this Libgdx test: http://git.io/-yAmNg .
It looks like this with different smoothing.
public class FontRenderTest implements ApplicationListener {
private Texture texture;
private SpriteBatch spriteBatch;
private BitmapFont font;
private DistanceFieldShader distanceFieldShader;
private static class DistanceFieldShader extends ShaderProgram {
public DistanceFieldShader () {
// The vert and frag files are copied from http://git.io/yK63lQ (vert) and http://git.io/hAcw9Q (the frag)
super(Gdx.files.internal("data/shaders/distancefield.vert"), Gdx.files.internal("data/shaders/distancefield.frag"));
if (!isCompiled()) {
throw new RuntimeException("Shader compilation failed:\n" + getLog());
}
}
/** #param smoothing a value between 0 and 1 */
public void setSmoothing (float smoothing) {
float delta = 0.5f * MathUtils.clamp(smoothing, 0, 1);
setUniformf("u_lower", 0.5f - delta);
setUniformf("u_upper", 0.5f + delta);
}
}
#Override
public void create() {
spriteBatch = new SpriteBatch();
Texture texture = new Texture(Gdx.files.internal("hiero.png"), true); // true enables mipmaps
texture.setFilter(TextureFilter.MipMapLinearNearest, TextureFilter.Linear); // linear filtering in nearest mipmap image
font = new BitmapFont(Gdx.files.internal("hiero.fnt"), new TextureRegion(texture), false);
distanceFieldShader = new DistanceFieldShader();
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
spriteBatch.setShader(distanceFieldShader);
font.draw(spriteBatch, "This is pretty sharp !!", 100, 120);
distanceFieldShader.setSmoothing(0f);
spriteBatch.setShader(distanceFieldShader);
font.draw(spriteBatch, "This is hazy !!", 100, 150);
distanceFieldShader.setSmoothing(1f);
spriteBatch.setShader(distanceFieldShader);
font.draw(spriteBatch, "This is pretty smooth !!", 100, 180);
distanceFieldShader.setSmoothing(1/2f);
spriteBatch.end();
}
Use the shader created by DistanceFieldFont.createDistanceFieldShader.