Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (780)
Games in Android Showcase (233)
games submitted by our members
Games in WIP (857)
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 3768 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  

hadezbladez (1165 views)
2018-11-16 13:46:03

hadezbladez (525 views)
2018-11-16 13:41:33

hadezbladez (1185 views)
2018-11-16 13:35:35

hadezbladez (272 views)
2018-11-16 13:32:03

EgonOlsen (2567 views)
2018-06-10 19:43:48

EgonOlsen (2786 views)
2018-06-10 19:43:44

EgonOlsen (1564 views)
2018-06-10 19:43:20

DesertCoockie (2268 views)
2018-05-13 18:23:11

nelsongames (2121 views)
2018-04-24 18:15:36

nelsongames (2815 views)
2018-04-24 18:14:32
Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20 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!