Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (711)
Games in Android Showcase (213)
games submitted by our members
Games in WIP (785)
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 2783 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?

Offline weston
« 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);

public void run()
    running = true;

       //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
« 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 Devvie

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?

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 Devvie

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 Devvie

Medals: 2

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


A fixed framerate would be

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

  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);
  } 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.

Offline Ask_Hjorth_Larsen

Junior Devvie

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.

numerical (240 views)
2017-02-21 07:32:16

numerical (239 views)
2017-02-21 07:31:46

theagentd (351 views)
2017-02-18 13:42:33

theagentd (351 views)
2017-02-18 13:35:16

h.pernpeintner (1516 views)
2017-01-24 22:39:11

h.pernpeintner (1505 views)
2017-01-24 22:38:32

Galdo (2069 views)
2017-01-12 13:44:09

Archive (2087 views)
2017-01-02 05:31:41

0AndrewShepherd0 (2624 views)
2016-12-16 03:58:39

0AndrewShepherd0 (2341 views)
2016-12-15 21:50:57
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 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‑
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!