Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  FPS goes crazy after manualy restarting the game  (Read 2189 times)
0 Members and 1 Guest are viewing this topic.
Offline stef569

Junior Member





« Posted 2007-09-28 00:26:00 »

Hi,

I'm having an fps issue when i restart my game manualy. It goes insane! hitting 5000 fps?

Normaly I hit 95 on level1
going to 110 when the level increase...

I looked at all my vars and they all get reset.
The function that does the restart is used with the button and with the r key

The button only can restart if the game has ended.

the r key can be pushed anytime.
I first set quit = true so that the loop stops if it was still running then startGame()

you can download the game @ http://www.kwbbz.be/Downloads/start.jnlp and see for yourself
what can be the cause of this?

The code involved:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
  public synchronized void startGame() {
    eventHandler.fireGameEvent(GameEvent.Status.RUNNING);
    if( pauzed ) {
      pauzed = false;
    }
    else {
      System.out.println("Restart!");
      GameThread = null;
      board.clearBoard();
      quit = false;
      score = 0;
      level = 0;
      update_Timer = 0;
      move_Timer = 0;
      moveDelay = NORMAL_MOVE_DELAY;
      updateDelay = NORMAL_UPDATE_DELAY;
      sleepDelay = NORMAL_GAME_SLEEP_DELAY;
      curBlock = null;
      nextBlock = null;
      left = false;
      right = false;
      up = false;
      down = false;
      spaceBar = false;
      pauzed = false;
      startThread();
    }
  }

  private void startThread() {
    /* Initialize the game thread. */
    GameThread = new GameThread();
    GameThread.start();
  }

  private class GameThread extends Thread {
    public void run() {
      runGame();
    }
  }
Offline broumbroum

Junior Member





« Reply #1 - Posted 2007-09-28 02:32:30 »

This seems to be a ThreadStack overflow you make when restarting with "R" key. You should add a GameThread member to the Main class so that you can interrupt the previous running Thread before to start again. This is as easy.

BTW your jnlp file is ok, but you filled it somehow to make it working at all, didn't you ? You may change the tag jnlp and add the original start.jnlp stored on the server then if you make change to the jnlp (if the description changes or the icon etc.), JWS will update directly from the base jnlp. That be :   
1  
2  
3  
<jnlp spec="1.0+"
  codebase="http://www.kwbbz.be/Downloads/" href="start.jnlp"
>(...)</jnlp>


However, I love to play Tetris... Cheesy

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline stef569

Junior Member





« Reply #2 - Posted 2007-09-28 15:38:38 »

broumbroum, I don't understand your first sentence. If you mean placing the Thread in the first Main object i make, then i don't see how I could do that... I never use the Main object as a reference anywhere. so i can't call the function startThread...
when I run the function startGame with the r key somethimes parts of blocks remain on the screen???

and yes i followed a tut on how to make a jnlp file and it seems to be doing what it is intented to do, so i rather not change it Grin
Thanks for checking the game it out  Wink

btw insert to toggle fps on or off.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline stef569

Junior Member





« Reply #3 - Posted 2007-09-28 16:48:16 »

ok bear with me i'm trying to understand what's going on when I restart.
1. Start Game, Start GameThread
2. Game is looping
2. User pushed button => added to Swing event Thread
3. actionEvent handles the action and asks the game to restart(acording to the game state ofcourse)
4. public synchronized void restart() {
    System.out.println("restart!");
    quit = true;
    startGame();
  }
5. that means that we wait until we can lock the object? and then set quit to true, and then start the game again with the default values
or does restart get called somewhere in the middle of the loop?
Offline broumbroum

Junior Member





« Reply #4 - Posted 2007-09-28 22:34:50 »

OK ! The correct operations are :
1. Start Game, Start GameThread
2. Game is looping
2. User pushed button => added to Swing event Thread
3. actionEvent handles the action and asks the game to restart(acording to the game state ofcourse)
4. public synchronized void restart() {
    System.out.println("restart!");
    quit = true;
    startGame();
  }
4.a) started the gameThread with startGame(), the next call to startGame() cannot launch the same Thread, right ?
4.b) make a new Thread when the first one exited or caught interruption: that is, to make the running Thread with the current game stop and start the new Thread for a clean restart().
4.c) next time I'll wait on the lock....
5. that means that we wait until we can lock the object? and then set quit to true, and then start the game again with the default values
or does restart get called somewhere in the middle of the loop?

There's the (Thread).interrupt() method to get one Thread interrupted. That Thread will then return false to (Thread).isAlive(). Then you can restart a new one for the game, that's fair enough for the game reset switch. Cheesy do you understand ?

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline stef569

Junior Member





« Reply #5 - Posted 2007-09-29 17:27:15 »

i'm trying to understand it broum, I used interupt and this is the code i got now, and... it works 90% of the time.
See the System output at the end of this post.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
  public synchronized void startGame() {
    eventHandler.fireGameEvent(GameEvent.Status.RUNNING);
    if( pauzed ) {
      pauzed = false;
    }

    if( GameThread == null) {
      createThread(ThreadNumber++);
    }
    else{
      if( GameThread.isInterrupted() ) {
        createThread(ThreadNumber++);
      }
      else
        System.out.println("GameThread " + GameThread.getName() +" is not interupted? yet..." );
      }
  }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
  private void createThread(int threatNumb) {
    /* Initialize the game thread. */
    GameThread = new GameThread();
    GameThread.setName("GameThread " + threatNumb);
    GameThread.start();
    System.out.println(GameThread.getName() + " Started");
  }

  private class GameThread extends Thread {
    public void run() {
      System.out.println("starting game.runGame()");
      runGame();
    }
  }


Note: skipped logic
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
  public void runGame() {
    boolean gameRunning = true;
    while (gameRunning) {
      /* Exits the game,
      * Placed above the other functions Because we need to draw one last String */

      if( quit ) {
        gameRunning = false;
        eventHandler.fireGameEvent(GameEvent.Status.END);
      }

      // sleep
     try {
        Thread.sleep(sleepDelay);
      } catch (InterruptedException e) {
        System.out.println("InterruptedException: " + e.getMessage());
        gameRunning = false;
      }
    }
    System.out.println("This Game has ended!");
  }


1  
2  
3  
4  
5  
6  
7  
8  
9  
  public void restart() {
    System.out.println("RESTARTED!");
    if( GameThread != null && !pauzed) {
      GameThread.interrupt();
      startGame();
    }
    else
      System.out.println("Cannot Restart!");
  }


The console prints:

GameThread 15 Started
InterruptedException: sleep interrupted
This Game has ended!
starting game.runGame()
Create 2 Random Blocks:
New Random Block 5 Starts at java.awt.Point[x=5,y=0]
New Random Block 2 Starts at java.awt.Point[x=3,y=0]
RESTARTED!
GameThread 16 Started
InterruptedException: sleep interrupted
This Game has ended!
starting game.runGame()
Create 2 Random Blocks:
New Random Block 7 Starts at java.awt.Point[x=4,y=0]
New Random Block 7 Starts at java.awt.Point[x=7,y=0]
RESTARTED!
GameThread 17 Started
InterruptedException: sleep interrupted
This Game has ended!
starting game.runGame()
Create 2 Random Blocks:
New Random Block 6 Starts at java.awt.Point[x=5,y=0]
New Random Block 6 Starts at java.awt.Point[x=4,y=0]
RESTARTED!
InterruptedException: sleep interrupted
This Game has ended!
GameThread GameThread 17 is not interupted? yet...


if I use isalive in       

1  
2  
3  
if( GameThread.isInterrupted() ) {
        createThread(ThreadNumber++);
      }


then I have to push 2 times before it detects that it is not alive anymore.

GameThread 0 Started
starting game.runGame()
Create 2 Random Blocks:
New Random Block 5 Starts at java.awt.Point[x=5,y=0]
New Random Block 5 Starts at java.awt.Point[x=5,y=0]
RESTARTED!
GameThread GameThread 0 is still alive.
InterruptedException: sleep interrupted

This Game has ended!
RESTARTED!
GameThread 1 Started
starting game.runGame()
Create 2 Random Blocks:
New Random Block 3 Starts at java.awt.Point[x=5,y=0]
New Random Block 7 Starts at java.awt.Point[x=2,y=0]
RESTARTED!
GameThread GameThread 1 is still alive.
InterruptedException: sleep interrupted
This Game has ended!
RESTARTED!
GameThread 2 Started
starting game.runGame()
Create 2 Random Blocks:
New Random Block 5 Starts at java.awt.Point[x=5,y=0]
New Random Block 4 Starts at java.awt.Point[x=4,y=0]
RESTARTED!
GameThread GameThread 2 is still alive.
InterruptedException: sleep interrupted
This Game has ended!
Offline broumbroum

Junior Member





« Reply #6 - Posted 2007-09-29 23:06:27 »

Thank you for reporting! This is right, then your game doesn't crash on restart, does it ? Now I'd simply change the sleep() function to a more accurate GameThread.wait(delay) to release the Thread lock.  Cheesy

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline keldon85

Senior Member


Medals: 1



« Reply #7 - Posted 2007-09-30 10:13:22 »

Despite the recorded framerate you're getting a lot of flicker and the pad is very unresponsive. Not sure what you're doing there, and sometimes it does not respond at all. Are you updating the game regularly?

Offline stef569

Junior Member





« Reply #8 - Posted 2007-09-30 21:26:39 »

I think this might be the problem:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
    /* Default Action: Move the Block Down */
    update_Timer++;
    if( update_Timer % updateDelay == 0 ) {
      curBlock.move(Block.MOVE_DOWN);
      update_Timer = 0;
      step = true;
      moved = true;
    }

    /* Let the Player Move the Block in a Direction */
    move_Timer++;
    if( move_Timer % moveDelay == 0 ) {
      playerMove();
      move_Timer = 0;
      moved = true;
    }


I put this inside my game loop, i don't know how to do it on another way.
alsoo the frames are counted for every loop, but not when a move occurs  Roll Eyes
and if you need more info I uploaded all my classes to http://www.kwbbz.be/Downloads/src2/be/JTetris/model/ since if I keep on posting code this topic is going to grow too big...
Offline keldon85

Senior Member


Medals: 1



« Reply #9 - Posted 2007-09-30 22:50:47 »

Well first of all try to keep the game speed as constant as you can. The gameboy Tetris runs at 60fps without fail, within this loop you check for the pad input and update the frame accordingly before generating the frame for output. Also consult the Tetris Concept wiki and forum for Tetris related topics, etc.

See the StrollDriver.java for how I manage keyboard input in Stroll. I have all of the code available on line at k:one labs.org. Generally if you stick to a typical game loop and try to work within common working practices you will find that things "just work".

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline stef569

Junior Member





« Reply #10 - Posted 2007-10-02 21:44:56 »

ok now I have fixed fps (not realy it floats around 40 - 60)
and after looking into my code I think I found where it goes wrong.

Note: moved = true means i do a repaint()

When a block get's added to a board i set moved = true
When a block moves i set moved =true

But not for every loop only when the block actualy moved or when the player moved it....
alsoo blocks shouldn't be falling straight to the buttom so this gives me this code inside the game logic.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
    /* Default Action: Move the Block Down */
    update_Timer++;
    if( update_Timer % updateDelay == 0 ) {
      curBlock.move(Block.MOVE_DOWN);
      update_Timer = 0;
      step = true;
      moved = true;
    }

    /* Let the Player Move the Block in a Direction */
    move_Timer++;
    if( move_Timer % moveDelay == 0 ) {
      playerMove();
      move_Timer = 0;
      moved = true;
    }


When running with that code I get: http://www.kwbbz.be/Downloads/JTetris.jar
if I remove all the timing around playerMove() I get this: http://www.kwbbz.be/Downloads/JTetris1.1.jar
Can you please report back if you see a difference in flicker?
and if you do, when should I be repainting at another interval?
Offline keldon85

Senior Member


Medals: 1



« Reply #11 - Posted 2007-10-03 00:30:18 »

It is still missing frames, also your input is over sampling some how. I am still seeing flicker. As for the input problem, you need to sort out the delayed auto shift

Offline stef569

Junior Member





« Reply #12 - Posted 2007-10-04 12:27:36 »

ok, is it better to removed the playermove function out of my loop? and move Blocks directly?

I'm alsoo going to use the canvas instead of the JPanel. I'll just start again from nothing.
Offline keldon85

Senior Member


Medals: 1



« Reply #13 - Posted 2007-10-04 16:57:02 »

I'll see if I can look over your code on Saturday. But one thing to note is that Tetris, though simple, features many - if not most - elements present in a typical game. In something like Windows I go for the approach of having a "virtual pad", and the keyboard input just toggles that. That way my game logic is simplistic and I can write boolean statements, such as:
1  
int pressed_keys = ~last_keys & current_keys;


You might be doing that already, and your problems may be somewhere where you least expect it. For all you know it could be something as trivial as incorrectly managing the state and just not updating it as often as you think you are - and believe me, you'd be surprised how often things like that happen. That is not really a *bug*, more of a flaw; so in general try to pre-empt flaws and make them become big-bugs as soon as you can, because at least with a big-bug you see it right away.

Offline stef569

Junior Member





« Reply #14 - Posted 2007-10-05 16:34:42 »

I found the bug  Grin

I was so focused on my loop that I didn't even think it might be somwhere else.
In the board the updateblock function was sending events to update the board for every square in one Tetris piece Hence the flickering  Shocked and moving slowness :/
Alsoo seperating the keyboard inputs from the game loop was a good thing to do i think.

Now I only have blocks falling down too fast on fast cpu's because I use a counter instead of a time but that's another story  Cheesy

thx for the help guys.
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.

pw (22 views)
2014-07-24 01:59:36

Riven (20 views)
2014-07-23 21:16:32

Riven (17 views)
2014-07-23 21:07:15

Riven (20 views)
2014-07-23 20:56:16

ctomni231 (48 views)
2014-07-18 06:55:21

Zero Volt (44 views)
2014-07-17 23:47:54

danieldean (35 views)
2014-07-17 23:41:23

MustardPeter (38 views)
2014-07-16 23:30:00

Cero (53 views)
2014-07-16 00:42:17

Riven (52 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!