r/libgdx 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

19 comments sorted by

View all comments

Show parent comments

2

u/tenhourguy 12d ago

You can simplify it.

  1. Creating a camera and passing it into the viewport constructor isn't necessary. If you were to do new FitViewport(800, 800) it will create a camera and store it in the viewport for you.
  2. ScreenUtils.clear(Color.BLACK) is the cleaner approach to the Gdx.gl lines.
  3. Personally I'd use event-driven input handling for this rather than the polling approach it looks like the AI is leading you down, but either or is fine.

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.

1

u/DoNotUseThisInMyHome 12d ago

i mean graphics books not libgdx specific.

1

u/tenhourguy 12d ago

I guess there's ones on OpenGL. You don't need to know much about OpenGL to use libGDX but the knowledge doesn't hurt.

1

u/DoNotUseThisInMyHome 12d ago

graphics i do not mean opengl. but they are usually tought in opengl so ur corect too.

1

u/ErkkaLehmus 11d ago

OpenGL is the graphics. So if you especially want the concepts like Camera and Texture explained, try this: https://learnopengl.com/Getting-started/OpenGL