Kapparino
Kapparino

Reputation: 988

doubts about implementing different approach of render method in libgdx

Recently, I decided to make a new game and give that chance to libgdx framework. Long time ago, I used to make some games in Slick2D, and I remember that their approach when it comes to update(delta) and draw() object, was different, then now in libgdx which has only render(delta). So I came to idea, to abstract game, and make my own class Game and Screen with update & draw. However, now I'm in doubt regarding parameter delta from render method. Watching some examples on the internet regarding libgdx with implementation concept update and draw, people were passing delta parameter from render method to both update and draw methods. From this description, my questions are:

  1. Is it ok to implement concept of update and draw methods ?
  2. Should I pass delta parametar to draw method?
  3. If in update(delta) metod I do computations, what is purpose or use cases of delta parametar in draw method ?

Upvotes: 0

Views: 263

Answers (2)

Tekkerue
Tekkerue

Reputation: 1517

You could utilize LibGDX's Scene2d package:

https://github.com/libgdx/libgdx/wiki/Scene2d

The Stage class does provide separate act and draw methods. So the render() method in the Game class ends up looking like this:

@Override
public void render() {      
    Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);   
    stage.act();
    stage.draw();
}

Upvotes: 0

Xoppa
Xoppa

Reputation: 8113

  1. Yes, it's usually a good idea to separate concerns. If your game loop only exists of updating game logic and drawing game state, then those methods would be good. When your game grows, you might need to add additional steps (like multiple render passes, synchronizing multiplayers, etc.).
  2. You typically only need the delta time when updating game logic, not when rendering it. But if you do need it then it's fine to pass it as argument.
  3. It is de time, in seconds, since the previous time the render method was called. You use it to update your game logic in a frame rate independent matter. E.g. calculate the correct position of your character, the correct frame of an animation, etc.

Note that just taking the delta time as-is can cause e.g. teleporting on hick-ups and is not discrete. Therefor it's usually a good idea to limit the value (e.g. delta = Math.min(delta, 1/30f);). Using a fixed time step is often used when you need your game logic to be discrete.

Upvotes: 1

Related Questions