2

I was wondering if it is possible to make two threads, one of them to handle the graphics and drawing of the objects, and the other one to update position and logic.

Now the main problem I think would happen is that they will get out of sync? like one thread would be ahead of the other thread and the drawing or game updating will be behind?

I want to do that mainly because i have a lot of arrays that i am trying to handle, and loading each array and initializing it takes about 1-5 seconds each, and i have many of them (each array consists of 500 objects), so the main idea is that as the player moves, the next array will initialize itself without the need to pause the game or make it slower.

Pero P.
  • 25,813
  • 9
  • 61
  • 85
Baruch
  • 1,618
  • 5
  • 23
  • 42
  • How would it get more than one frame "out of sync" if it always draws the most recent data? – tilpner May 29 '13 at 16:58
  • When it draws, I use time of the system to update and create delta time, both for drawing and updating, and since there is much more to initialize than there is to draw and sometimes vice versa I thought it wouldn't draw in sync and in the exact frame rate as it updates... maybe im way off?.. – Baruch May 29 '13 at 17:07
  • [Here is an answer I wrote to a similar question](http://stackoverflow.com/a/16099607/119114). It has some discussion of game loops, shows an example using `AsyncTask` (which uses a 2nd thread), and some links to more source of info. – Nate May 29 '13 at 21:54

1 Answers1

0

The common way to solve such problem is double buffering. One thread works with game logic, one thread draws data into a buffer in background, and the main thread updates the buffer to screen, as well as swaps the buffer.

kyriosli
  • 333
  • 1
  • 6
  • What happens if the rendering thread renders its view of the object on to the buffer and the logic thread destroys the object by shooting it? You might end up rendering a dead cat as alive, though not noticeable at high fps. – arynaq May 29 '13 at 17:20
  • that's exactly what I was wondering and what caused me to write the question, since for now i am using only one thread, everything works ok since it goes from task to task and doesnt jump around, and if you use two threads they both work simultaneously and can cause problems as you mentioned. – Baruch May 29 '13 at 17:25