I am only using one thread and the cpu usage is pretty low when running. I dont know how it will be when I have a much more complex engine. Stability might become an issue. I am not sure wether running a loop that checks every tick is effifcient and safe.
I had a really good reason for doing it. Unfortunately, I can’t remember it so it must not be that good of a reason!
Seriously, I think I had needed to find out exactly what the time is and for whatever reason getClockTicks() was not acting as either I expected, or else there’s something really convoluted and wrong about my code. At this point since the loop was changed I think I no longer needed.
What about the wisdom of not using sleep at all in this case… is that cool? I see a potential flaw if your draw code takes longer than the ai’s next tick… which means I guess you skip it if it’s taken too long to do any one operation.
Yeah I am concerned about that. I dont think that not hjaving sleep and building in all these safety checks is not the smoothest way. Hey GergisKhan what does your loop look like? How do you solve the problem of having different updates at different specified intervals during a second of time?
Actually, I didn’t really solve it. I just update everything clientside whenever the frame is supposed to refresh. That’s because very little should really be going on clientside that requires more than a 1/8th of a second refresh.
All of my AI work is server-side. Had I been doing this as a standalone (which is my NEXT project for demo purposes) I would still resort to a single call. The reason for this is that I feel more comfortable knowing that I’ll get in one redraw and one update, and a few ticks left over to sleep in.
Sample, not production code:
public void run()
{
long startTime;
long endTime;
boolean contentsLost = false;
try
{
while ( doRender )
{
if ( !pauseRender )
{
startTime = timer.getTime();
if ( doRender( null ) )
{
bufferStrategy.show();
}
updateSprites();
endTime = timer.getTime();
timer.sleep( renderLoopSpeed - ( endTime - startTime) );
}
}
}
catch ( Exception x )
{
}
}
I had the same implementation where I would do all the updates during the interval where I refreshed my frame. The problem is that after a while If I have my character running through a bar and there are 20 npc’s in the bar then there are a lot of things going on that need to be updated. I have a bouning box collision setup, but since my world is iso I also have collision that relys on the geometry of the tiles. So I have a bar drawn, then all the objects like tables and such, then the keyboard and mouse events are taken care of, then collision detection happens between all the sprites and the environment, then movement. It gets really complex as I go along and I dont think updating everything 60 times a second is a viable option (or a good one). Especially since alot of the things like movement and character animations do not need to be updated every frame.
I was wonedering how else I should handle this situation. What kind of loops do some of you guys use that work ell for a complex game?
I highly suggest you look at the way GAGE handles collisions. There’s no need to test every object against every object, just test possible types of collisions. 60 FPS should be no problem then.
As for your loop, as long as you have your AI and Character stuff as multiples of your time between frames (or at least close), you can do this:
long nextDraw = timer.getClockTicks()+16;
long nextChar = timer.getClockTicks()+50;
long nextAI = timer.getClockTicks()+300;
while(true)
{
drawStuff();
nextDraw = nextDraw+16;
if(nextChar < timer.getClockTicks())
{
doCharacterStuff();
nextChar = nextChar+50;
}
if(nextAI < timer.getClockTicks())
{
doAIStuff();
nextAI = nextAI+300;
}
timer.sleepUntil(nextDraw);
}
I did my own collision detection so I can learn how to do it properly. It was the first time I wrote fast effecient collision detection between objects other than simple rectangles. I am actually now looking at the GAGE code so as to see what areas I can improve.
The loop you have is the one that I am currently using and experimenting with. I was wondering what do you mean by my other updates being multiples of my time between frames. Edit: Nevermind I got it.
I will continue to use the loop you suggest but am still intrested in other people’s implementations on this matter. I guess I am still asking for solutions to doing different updates during different time intervals within a second of game time.
Hey jbanes, I was wondering how does your collision detection work, theoretically? In the Javadocs it says “Currently, this code uses brute force comparing to decide whether a collision has occurred”. In my collision detection I have bounding box check that when violated I then do calculations on the geometry. But I still have to do bounding box between a sprite and all the other sprites that it can collide with every update.
If you look above I only put it in their for a quick test. after that didn’t fix it I removed it and looked at the java classes that came with timer and saw that it was loaded in nativeTimer. But it didn’t throw any new errors including it in there a 2nd time.
Also I’ve got the timer.dll is in my program directory and I still get the error- I’ve pretty much put the timer.dll everywhere possible after this didn’t work-still nothing. Only thing I can think of is maybe the jar file is older then the .dll and something was changed recently in the timer .dll? I didn’t get the jar file with the latest download of the timer.zip but found it somewhere else. If this isn’t the problem I’m seeing no other reason why it shouldn’t be working. jar file is being imported and dll is being loaded…
Quick question, whats the proper algorithm for displaying the framerate? Shoud I just have a counter inside my redraw() and have that printed to the screen once every second? Or is there a more accurate way of getting the actual fps?
Vydias, can you post your code here? Also, do a search on your system to make sure that you have no other files called “timer.dll”.
Kommi:
long frametime = 0;
long fps;
while(true)
{
frametime = timer.getClockTicks();
...
fps = timer.getTickPerSecond()/(timer.getClockTicks()-frametime);
}