Hey everyone, I've just gotten started with java game development and i though that I'd create a simple space invaders clone to start off with. I've created an update and render loop and managed to render a sprite to the screen.

While i was first creating the game loop it updated 60 times per second as i was expecting and i was getting a bunch of fps. But now when I've started adding stuff to the render function of my game it only updates at around 55-59 times a second.

Sure this isn't huge performance hit but i would like to know what causes this.

Well, you're currently rendering as fast as you possibly can. I would recommended rendering 60 times a second as well, since rendering is usually pretty costly. I would also suggest using Thread.sleep() to eat up the time between updates instead of just letting the loop run and eat up CPU cycles. To see what I'm talking about, have a look at this article on game loops, especially the fixed timestep loop that Yields/Sleeps for the left over time.

Well, you're currently rendering as fast as you possibly can. I would recommended rendering 60 times a second as well, since rendering is usually pretty costly. I would also suggest using Thread.sleep() to eat up the time between updates instead of just letting the loop run and eat up CPU cycles. To see what I'm talking about, have a look at this article on game loops, especially the fixed timestep loop that Yields/Sleeps for the left over time.

When you say I should render 60 times per frame as well, do you mean I should just call tick() and then render()? or is there a better way of doing it?

When you say I should render 60 times per frame as well, do you mean I should just call tick() and then render()? or is there a better way of doing it?

Yes. If you looked at the two best examples from the article I linked to you updating and rendering are both done before the time wasting (or waiting for the next cycle) is. The article gets a little more advanced and compensates for running slow by "dropping" frames. It does this by updating without rendering until it catches up, and then it renders.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org