I'm trying to get a png file to be pasted onto the screen as a backdrop for my game, so far I can't seem to get this to work and I'm not too sure.
I have created a map class and have it create the Texture like so:
public static Texture backgroundTexture;
public static Sprite backgroundSprite;
public Maps(){
try{
backgroundTexture = new Texture(Gdx.files.internal("background.png"));
backgroundSprite = new Sprite(backgroundTexture);
}catch (Exception e){
e.printStackTrace();
}
}
public void renderBackground(SpriteBatch batch){
batch.draw(backgroundTexture, 1000, 800);
}
Then in my main class I have it called like so in the create method:
batch = new SpriteBatch();
map = new Maps();
batch.begin();
map.renderBackground(batch);
batch.end();
But this doesn't work? Should it be in render method cos it needs to refresh the background each turn? I've tried it in render but still didn't work. My screen size is WXH 1000x800
Try this:
OrthographicCamera cam = new OrthographicCamera(1000, 800);
SpriteBatch batch = new SpriteBatch();
Maps map = new Maps();
in your render/update method:
batch.setProjectionMatrix(cam.combined);
batch.begin();
map.renderBackground(batch);
batch.end();
in your Maps render method:
batch.draw(backgroundTexture, 0, 0, 1000, 800); //x, y, width, height
Related
The specific tools.png texture is not displaying
Tools.png and TopBanner.png are both in my assets folder and are valid files
Here is my resize function, which works on every other png except tool and my create and render functions
public Texture resize(String path, int width1, int height1){
Texture final_texture;
Pixmap TL1 = new Pixmap(Gdx.files.internal(path));
Pixmap TL2 = new Pixmap(width1, height1, TL1.getFormat());
TL2.drawPixmap(TL1,
0, 0, TL1.getWidth(), TL1.getHeight(),
0, 0, TL2.getWidth(), TL2.getHeight()
);
final_texture = new Texture(TL2);
TL1.dispose();
TL2.dispose();
return final_texture;
}
public void create () {
//there are other things here too but dont pertain to tools
tools = resize("tools.png", 200, 200);
TLArrow = resize("TLArrow.png", width*5/4, height/8);
}
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(0/255f, 255/255f, 140/255f, 1);
batch.begin();
batch.draw(tools,500, 500);
batch.draw(TLArrow,500, 200);
batch.end();
}
The TLArrow draws fine, but the tools does not. I logged tools.toString() and it returned a (seemingly) valid storage location
I'm trying to use a TiledMap in a test game but I'm having issues with the size. I'm using an ExtendViewport with width 160 and height 90. I guess the problem is that the tiled map is drawing using the screen size, because it's zoomed in. Do I need 2 seperate cameras for the tiled map and the rest of the game (players, enemies, ...)?
This is all of my code:
public class Main extends Game {
private OrthographicCamera camera;
private ExtendViewport viewport;
private TiledMap tiledMap;
TiledMapRenderer tiledMapRenderer;
#Override
public void create () {
camera = new OrthographicCamera(160, 90);
camera.setToOrtho(false, 160, 90);
camera.update();
viewport = new ExtendViewport(160, 90, camera);
viewport.apply();
tiledMap = new TmxMapLoader().load("map1.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
tiledMapRenderer.setView(camera);
tiledMapRenderer.render();
}
#Override
public void resize(int width, int height) {
viewport.update(width, height, false);
}
#Override
public void dispose () {
}
}
Thanks
OrthogonalTiledMapRenderer's second parameter is unitScale. The unit scale tells the renderer how many pixels map to a single world unit. And it defaults to 1.
So in your case one pixel is equals to one unit size in tiled map. Try changing the unitScale parameter.
https://github.com/libgdx/libgdx/wiki/Tile-maps#rendering-tiled-maps
I have a shader and I want to run the shader on the whole rendered image at the end instead of each individual sprite that I render on screen. For this purpose I created an FBO, I render the all my images to the FBO and then I render to colorBufferTexture generated by the FBO. There is just one problem, the shader does not work when I render the FBO texture. The withoutFbo() method render the image with the shader working just fine, but the withFbo() method only renders the image without the shader. Also I know that the shader in withFbo() is working because if I try to render something other then the colorBufferTexture from the FBO the shader will work. So why isn't the shader working with the texture generated by the FBO?
Note: The shader is working with this code, the problem is that it doesn't work when I use the fboTexture. If I call batch.draw(sprite, ...) then the shader will work in both the withFbo() and withoutFbo() methods, but I call batch.draw(fboTexture, ...) then it will simply render the FBO texture without applying the shader.
public class Application3 extends ApplicationAdapter
{
float time;
Sprite sprite;
FrameBuffer fbo;
SpriteBatch batch;
Texture noiseTexture;
TextureRegion fboTexture;
ShaderProgram shaderProgram;
#Override public void create()
{
ShaderProgram.pedantic = false;
batch = new SpriteBatch();
sprite = new Sprite(new Texture("game.png"));
sprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
shaderProgram = new ShaderProgram(Gdx.files.internal(Shader.GLITCH2.getVertex()), Gdx.files.internal(Shader.GLITCH2.getFragment()));
noiseTexture = new Texture(Gdx.files.internal(Shader.GLITCH2.getPath() + "noise.png"));
noiseTexture.bind(0);
fbo = new FrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
fboTexture = new TextureRegion(fbo.getColorBufferTexture());
fboTexture.flip(false, true);
}
#Override public void render()
{
//withFbo();
withoutFbo();
}
void withoutFbo()
{
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
time += Gdx.graphics.getDeltaTime();
shaderProgram.setUniformi("u_noise", 0);
shaderProgram.setUniformf("u_time", time);
batch.setShader(shaderProgram);
batch.begin();
batch.draw(sprite, sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
batch.setShader(null);
batch.end();
}
void withFbo()
{
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
time += Gdx.graphics.getDeltaTime();
fbo.begin();
batch.begin();
batch.draw(sprite, sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
batch.end();
fbo.end();
shaderProgram.setUniformi("u_noise", 0);
shaderProgram.setUniformf("u_time", time);
batch.setShader(shaderProgram);
batch.begin();
batch.draw(fboTexture, sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
batch.setShader(null);
batch.end();
}
}
I have a GameScreen with a SpriteBatch to draw all my textures on, but I want to add a pause button on the screen now. I've tried to use a stage on top of my SpriteBatch, but for some reason it won't show up.
Here's some of the code:
The constructor:
public GameScreen(MyGame game) {
stage = new Stage();
table = new Table();
pause_texture = new Texture("path_to_texture.png");
pause_image = new Image(pause_texture);
pause_image.setPosition(1920 - pause_image.getWidth() - 20, 20);
table.add(pause_image);
stage.addActor(table);
}
The render method:
public void render(float delta) {
Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
game.camera.update();
game.batch.setProjectionMatrix(game.camera.combined);
game.batch.begin();
//rendering stuff here
game.batch.end();
}
What am I doing wrong?
Don't use absolute positions on a Table, just do something like this:
public GameScreen(MyGame game){
stage = new Stage();
table = new Table();
pause_texture = new Texture("path_to_texture.png");
pause_image = new Image(pause_texture);
table.add(pause_image);
table.bottom().right();
stage.addActor(table);
}
For further reference look at Table on the Libgdx Wiki
Don't forget to move stage.act(delta); and stage.draw(); to after batch.end(), the GUI is the last thing that should be rendered so it can be in the front of everything.
When I tried draw a Texture using SpriteBatch it was result flip like this:
Here what I do:
I create MyRect object which draw bounding rectangle and an image.
Here MyRect class preview:
public class MyRect {
private Vector2 position;
private int width;
private float height;
private Texture img;
private Sprite sprite;
public MyRect(int x, int y, int width, int height){
img = new Texture("badlogic.jpg");
sprite = new Sprite(img);
position = new Vector2(x,y);
this.width = width;
this.height = height;
}
public void draw(ShapeRenderer shapeRenderer, SpriteBatch batch){
// Gdx.gl.glClearColor(0, 0, 0, 1);
// Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shapeRenderer.begin(ShapeType.Line);
// Draw Background color
shapeRenderer.setColor(55 / 255.0f, 80 / 255.0f, 100 / 255.0f, 1);
shapeRenderer.rect(position.x, position.y, width, height);
shapeRenderer.end();
batch.begin();
// batch.disableBlending();
batch.draw(img, position.x, position.y,
width, height);
batch.end();
}
}
Parameters ShapeRenderer and SpriteBatch pass by GameScreen class.
GameScreen preview:
public class GameScreen implements Screen{
private MyRect myRect;
private ShapeRenderer shapeRenderer;
private SpriteBatch batch;
private OrthographicCamera cam;
public GameScreen(){
myRect = new MyRect(10,10,50,50);
int gameHeight=100;
cam = new OrthographicCamera();
cam.setToOrtho(true, 136, gameHeight);
batch = new SpriteBatch();
batch.setProjectionMatrix(cam.combined);
shapeRenderer = new ShapeRenderer();
shapeRenderer.setProjectionMatrix(cam.combined);
}
#Override
public void render(float delta) {
// TODO Auto-generated method stub
myRect.draw(shapeRenderer, batch);
}
}
Why is this happen? Am I doing it wrong?
Your camera is upside down because you called setToOrtho(true, ...) on it instead of setToOrtho(false, ...)
It is valid to use an upside-down camera (which might be more comfortable if you've used Flash or some other Y-down system before), but then you need to flip all your TextureRegions (aka Sprites): sprite.flip(false, true). Alternatively, you can create a TextureAtlas using TexturePacker (look it up in libgdx documentation) and set the flipY option, so it flips them ahead of time for you. Eventually, for performance you will need to use a TextureAtlas anyway.
By the way, when you start drawing multiple instances of MyRect, you are going to need to move the spriteBatch.begin() and end() and shapeRenderer.begin() and end() out of the individual MyRect's draw method or you will run into a performance problem. And as such, there will need to be two draw methods (one for sprite batch and one for shape renderer).
apparently, the only thing I see different than how I would do, is try to change:
this meybe if it works"for test"
cam.setToOrtho(false, 136, gameHeight);
this is how I use the camera.
I do not know whether to use true, you have to do some different way, to draw the batch, in anyway. if the camera false, it looks good, I think it has to flip the image before to draw uv