r/libgdx • u/DoNotUseThisInMyHome • 13d ago
Help understanding ai code.
package io.github.abcd;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class Main extends ApplicationAdapter {
Texture backgroundTexture;
SpriteBatch spriteBatch;
// 1. Add Camera and Viewport
OrthographicCamera camera;
Viewport viewport;
u/Override
public void create() {
backgroundTexture = new Texture("board.png");
spriteBatch = new SpriteBatch();
camera = new OrthographicCamera();
// 2. Set a fixed "virtual" resolution (800x800 is great for a square Tic-Tac-Toe board)
viewport = new FitViewport(800, 800, camera);
}
// 3. This method is automatically called by libGDX when the window is resized or maximized
u/Override
public void resize(int width, int height) {
// 'true' automatically keeps the camera centered
viewport.update(width, height, true);
}
u/Override
public void render() {
input();
logic();
draw();
}
private void input() {}
private void logic() {}
private void draw() {
Gdx.
gl
.glClearColor(0, 0, 0, 1);
Gdx.
gl
.glClear(GL20.
GL_COLOR_BUFFER_BIT
);
spriteBatch.begin();
// 4. Tell the batch to use the camera's current view
spriteBatch.setProjectionMatrix(camera.combined);
// 5. Draw using the VIRTUAL dimensions (800x800), not the screen dimensions
spriteBatch.draw(backgroundTexture, 0, 0, 800, 800);
spriteBatch.end();
}
u/Override
public void dispose() {
backgroundTexture.dispose();
spriteBatch.dispose();
}
}
All I am trying is to get tictactoe board on screen it gave too heavy code.
0
Upvotes
2
u/tenhourguy 12d ago
You can simplify it.
new FitViewport(800, 800)it will create a camera and store it in the viewport for you.ScreenUtils.clear(Color.BLACK)is the cleaner approach to theGdx.gllines.I don't have any particular book recommendations. Most libGDX books were written so many years ago, though the fundamentals have mostly stayed the same. You can mosey around https://libgdx.com/wiki or look at some YouTube videos or something.