0
\$\begingroup\$

I know that the project generator in libGDX finds the render(deltatime) method in my Game project and make it into a game loop so that it repeats itself on an average of 60 times per second. But I want to know how exactly the project generator finds the render() method.

Let me give you an example:

In my game, in the Main class, I declared a SecondClass with its own render() method using its constructor in the create() of the Main class. Then, I didn't have any render() method in the MainClass. When I did this, the render() in the SecondClass worked. However, when I created the render() method in the MainClass, the render() from the MainClass worked but not the render() method in the SecondClass. Does that mean that the project generator access the first render() it can see as the GameLoop?

\$\endgroup\$
  • \$\begingroup\$ oh by the way the SecondClass implemented Screen \$\endgroup\$ – chanu19 Feb 8 '17 at 17:11
3
\$\begingroup\$

The project generator isn't doing any work here, this is all handled by the source code, using dynamic dispatch (virtual functions) and basic programming logic. Reading the source code is a good way to understand how things like this work.

The Game base class you inherit from in libGDX is itself an implementation of ApplicationListener, which mandates the render() function.

The Game class implements render() by checking to see if the game has a screen, and if it does, it calls that screen's render() method.

If you inherit from Game and provide render() there, you override the Game class's render method and yours gets called instead. If you don't override render, the Game class's method gets called, and it forwards along a call to the render method of any active screen object.

This is just how dynamic dispatch/virtual functions work in Java. There's no magic examination of your source code by any project generator going on to implement this.

The generator is only used to generate a stub set of files that just replace your class and package names into the source code at the right points to ensure an instance of your class is created. It doesn't do much else relevant to this operation, and it's entirely unnecessary; you could write that bootstrapping code yourself quite easily if you wanted.

\$\endgroup\$
  • \$\begingroup\$ How do you know LibGDX so well? You seem to be knowing every ins and outs of LibGDX. Can you tell me what tutorials or books you used to learn LibGDX? I think your tips will help accelerate my learning progress \$\endgroup\$ – chanu19 Feb 8 '17 at 17:58
  • 1
    \$\begingroup\$ @ChanWooKim I have never even downloaded libGDX. I just look at the source code on GitHub when people ask questions about it. \$\endgroup\$ – Josh Feb 8 '17 at 18:00
  • \$\begingroup\$ So then you are just really good at java and opengl? Good enough to understand the source code just by lookin at it once? \$\endgroup\$ – chanu19 Feb 8 '17 at 18:02
  • 1
    \$\begingroup\$ I've never written any Java either, but I know enough to read it and understand the basic concepts. \$\endgroup\$ – Josh Feb 8 '17 at 18:02
  • 1
    \$\begingroup\$ Mostly it takes practice, just keep trying to make stuff and keep trying to read stuff to understand how it works. \$\endgroup\$ – Josh Feb 8 '17 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.