HOLD YOUR HORSES, I haven’t discovered anything yet! Ranting beware.
I have been hacking both AWT and SWING for awhile to get the best performance out at the same time as I learn OpenGL.
My problem initially was that there were too many useless threads going on at the same time along with my rendering thread and they were all unneccessary.
Now Sun seems to have very user friendly way of treating developers: http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html#why
However, I’d really like to be incontrol of the things that I do. To me this is crap news, if Swing is supposed to eventually take over the position of AWT it must be fully customizable and I want to be in control of every single thread running. And yes this means the hidden threads that might phuck up the program. If my program freezes, it is clearly my fault, not suns fault and thats why they should leave as much room for me.
Now, there is tons and metric tons of air inside of swing, we are all aware of that, thats why it is slow and people on slower computers can see delays when they click things. For game programming this is not necessarily so. I admit that the hidden threads make my life 1000% easier, but the problem is that I don’t want to do it the easy way. I want it the fast way.
Now here is the possible sollution:
-
invokeLater(): Requests that some code be executed in the event-dispatching thread. This method returns immediately, without waiting for the code to execute.
- invokeAndWait(): Acts like invokeLater(), except that this method waits for the code to execute. As a rule, you should use invokeLater() instead of this method.
As you can clearly understand where I’m going with this. As a matter of a fact, I haven’t found any reasonable way to access the inner threads in AWT and assuming that a lot of air could be squeezed out if we could access them this article explains everything better:
http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html
Although things are made easy for us, it doesn’t mean that they are necessarily slow. By accessing the swing thread, this saves us one thread, which can be many milliseconds and it will also save the GC from extra work.
I’m not saying swing is the way to go. I’m saying that swing has potential. If it just gave me greater degree of freedom. http://billharlan.com/pub/papers/Improving_Swing_Performance.html.
Now some people would say that omg all this for just one thread? Well, to me even one thread is enough. When I know that my code potentially runs faster in theory, it is enough for me and isn’t everyone saying that Java is too slow? It is slow, certainly slower than C++, thats why to match we must counter the speed gap.
I know we are still light years away, but every single bit helps. I’m not trusting the Sun to make it faster with those policies. I don’t care if an idiot can make a game if it is slow.
The practical uses for hacking the event thread? In theory, you could create game events, that would be handled among the input events or directed into another thread which is the actualy game thread.
The way I categorize things:
Under the hood threads:
-The engine
–Rendering
–Input
–Media(Sound)
These are currently all different threads, except for the sound. If they could be combined, that would be already 1 thread less.
Game threads
-events in game
-physics
-interaction
-Usually combined into one. Now this is what it should be.
The ideal way to render in games: The engine pumps out as much FPS as it is possible, the game thread and the input thread corresponds with the engine, and handles everything so that it goes maximium 72 fps(or whatever the eye/monitor relationship is). Now if the FPS is over 1000, which I’m having right now with my small OpenGL app, the animation is not good looking. Thats why the animation speed must be limited to 72 fps, at the same time with the input thead. Now one lower end computers, the engine must keep count on the FPS part, since if the FPS goes lower than 72fps, the other threads must be adjusted too, so that the game maintains its playability.
This is certainly better design than having everything set to whatever FPS or the infinite fps loop.
So threads required would be:
1.) The graphics engine thread
-This would adjust the engine speed and everything else
2.) The game thread
-follows the engine thread.and keeps the game playable even on lower end systems.
This would help in 2d, but I’m aiming into the third dimensions, be the API opengl or java3d, that doesn’t matter. To get the most speed out of the APIS we must have as secure base as possible, this means we must have go as lowlevel as possible so that the higherlevel api is able to take the full advatange of the environment.
Now when using Java2D or other java-based API, we don’t have to worry about heavyweight/lightweight relationship since on swing, if done correctly, hardware acceleration is easy to do.
Now if we don’t want the lightweight rendering, what now? I will continue when I find out more.
If you read this, please correct all the mistakes I have made in the concepts. Do you have anything to add, howabout comparsion to AWT?