Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Timer or new Thread?  (Read 1805 times)
0 Members and 1 Guest are viewing this topic.
Offline wanner

Senior Newbie




Java games rock!


« Posted 2005-10-20 22:13:21 »

Im making a simple game.
And i wonder what you recomend to handle the game loop.
A timer that updates and calls repaint.
Or a new thread?.

The timer im using is javax.swing.
Is there trouble if i use swing timer and other components from awt?

Thanks.
Offline weston

Junior Member





« Reply #1 - Posted 2005-10-21 00:46:47 »

just stick it in a new Thread this way you can change the way you want to pause the loop or skip frames and that sort of thing since your not reliant on the swing timer.

...
Thread myThread = new Thread(this);
myThread.start();
}

public void run()
{
    running = true;

    while(running)
       //handle input
       //update game
       //render game
       //do pause
}

for(int i = 1; i > 0; i++)
{
System.out.println(i+" cups of java downed");
}
Offline wanner

Senior Newbie




Java games rock!


« Reply #2 - Posted 2005-10-21 07:42:17 »

Ok so i go with thread.
How do i calculate how long the thread should sleep before updating?
if i just make it sleep a fix value the performance will be different on different systems right?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline weston

Junior Member





« Reply #3 - Posted 2005-10-21 14:15:29 »

Well there are lots of ways you can do that, a simple scheme would be to measure how much time passed while you were updating/rendering (i.e. during the current frame) and subtract it from your usual pause time. You could probably find something more advanced if you looked around (or came up with your own Smiley).

for(int i = 1; i > 0; i++)
{
System.out.println(i+" cups of java downed");
}
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #4 - Posted 2005-10-22 02:47:29 »

I've used a swing Timer for that kind of stuff, and it's fine! It's much easier to avoid bugs using a swing Timer than when you write your own multithreading applications, especially if you don't have loads of experience. However, if you rely on active rendering and that kind of stuff, it might be appropriate with some more control of what threads code is being executed from, and then a swing Timer probably won't do the job.
Offline wanner

Senior Newbie




Java games rock!


« Reply #5 - Posted 2005-10-22 12:33:06 »

How does this look to you?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public void run() {
        while(gameisrunning) {
         
      starttime = System.nanoTime();
         
      //Perform operations
       
      estimatedtime = (System.nanoTime() - starttime) / 1000000;
         
      sleeptime = 15 - estimatedtime;
         
      if(sleeptime > 0) {
         try  { thread.sleep(sleeptime); }
         catch (InterruptedException e) { System.out.println(e); }
      }
   }
}
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #6 - Posted 2005-10-22 13:15:54 »

It is not guaranteed that the game will hold any specified average framerate using the above code. For example, if one particularly expensive "//perform operations" takes longer than sleep time, there will never be any compensation for this, and thus the game will drift out of sync if you're networking. It's okay for single player though as far as I can see. I guess it's fine for what you're trying to do.

Note that in java, we normally prefer to capitalize identifiers like this: "startTime", "estimatedTime", "gameIsRunning", etc.
Offline wanner

Senior Newbie




Java games rock!


« Reply #7 - Posted 2005-10-22 13:29:36 »

Ok. How would you do to compensate for an operation that takes long time?
Its only going to be singleplayer but no reason not to do it correct Smiley
Offline JAW

Senior Member


Medals: 2



« Reply #8 - Posted 2005-10-22 16:11:48 »

Hi

A fixed framerate would be

int fps = 30; //whatever you want
int timeperframe = 1000/fps;

while(running)
{
  long start = System.nanoTime();

  Do decisions for this frame, AI
  Update all Objects / Move
  Check Collisions and stuff
  Paint the frame

  long end = System.nanoTime();
  long duration = (end - start) / 1000000;
  long sleeptime = Math.max( timeperframe-duration, 5);
  try
  {
    Thread.sleep(sleeptime);
  } catch(Exception e)
 {
  }
}


This would be a fixed framerate. After each frame it sleeps just as long so that the frames all take
the same time. You must specify your framerate. It should take a minimum sleep time of at least a few ms so that your game loop doesnt starve out other java threads.
When the pc is not fast enough to do the frames in the target time, the game will slow down.


to conquer this, you can make a dynamic framerate with

long timesincelastframe = lastframestart - thisframestart;

and do all updating based on this time. If more time passed, things will move a greater distance.
be sure to make a good collision detection, because fast things might jump quite some distance if the fps is low,
and they might just overjump each other while they should have collided.


If you have very expensive tasks, either make another thread, let them run there and use the results when its done, or
divide it into small packages and do one package per frame until all are done.
pathfinding for example might be restricted to search 100 tiles per frame. you wont notice when it takes 3 or 4 frames to make
a path.

-JAW
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #9 - Posted 2005-10-22 16:58:07 »

What I meant was that the logical framerate must be held constant in order to make a network game. Not perfectly constant, but as hours pass, computers must not drift away from each other. The graphical framerate may, of course, vary freely.

I would store the amount of frames executed since the beginning of the game. Think of each frame as predestined to be executed "reasonably close" to the time [frame count] * [time per frame] . You will just have to call Thread.sleep such that the sleep time aims toward this number (instead of the per-frame time). When the timer is "late", i.e. when the last update took too long, you can always perform the next update without e.g. drawing, thus conserving time and eventually (if the computer is not an 80286) keep up with any other computer without desyncing issues.

You don't have to do any of this though.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!