Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
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  
  movement slow until window resized?  (Read 886 times)
0 Members and 1 Guest are viewing this topic.
Offline pifreak

Senior Newbie





« Posted 2009-03-07 06:01:11 »

This is weird:

I thought my key input and/or image refreshing was bad, but I should have it perfect now. The movement engine/demo is very laggy.

When you move around, it gets about 2-3 fps. If you maximize the screen, it magically fixes it giving it the normal fps which is not laggy at all and appears extremely smooth.


Could someone please help me find out why it is a very nice movement engine...only after the JFrame is resized in any way?

Offline pjt33
« Reply #1 - Posted 2009-03-07 15:19:52 »

I don't think anyone can help you without a few more details. How are you rendering (AWT? JOGL directly? LWJGL directly? Something built on top of one of those?) How does your game loop work? (JOGL Animator? Active rendering? Handled by a library?)
Offline pifreak

Senior Newbie





« Reply #2 - Posted 2009-03-07 23:43:49 »

okay, sorry.

packages used:


key input class:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


img class:

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;


structure:
body[main] class:

main
make windows,
make objects for images,
implement key listener,


then it loops just like this:

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  
41  
for(;;)
           {
               //optimize
              try {
                Thread.currentThread();
                Thread.sleep(7);
             } catch (InterruptedException e1) {
                e1.printStackTrace();
             }

             //move
            if (down)
              {
                 character.setXY(character.getX(),character.getY()+s);  
              }
              if (up)
              {
                 character.setXY(character.getX(),character.getY()-s);
              }
              if (right)
              {
                 floor.setXY(floor.getX()-s,floor.getY());
                character.setImg("character_r.png");
              }
              if (left)
              {
                 floor.setXY(floor.getX()+s, floor.getY());
                character.setImg("character_l.png");
              }
             
              if(System.currentTimeMillis() - lastReload > 17)
              {
              lastReload = System.currentTimeMillis();
                 if (left || right || up || down)
                 {
                    //character.repaint();
                   //floor.repaint();
                  f.repaint();
                 }
              }
           }//for;;



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

Senior Member





« Reply #3 - Posted 2009-03-08 06:08:55 »

First of all, if you're character.setImg calls are loading the image each time, that's the source of your problem.  I doubt that is the problem though because it wouldn't get faster when maximized.

1  
Thread.currentThread();

That does absolutely nothing.  Just call Thread.sleep.  That isn't the problem though.

You're doing it backwards.  You should slow down updating the game so that it occurs a certain number of times per second regardless of the machine's processing power.  And then draw the screen as often as possible.  You're doing the opposite.  You want to change your code to the following:

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  
for(;;) {
        //optimize
       try {
      Thread.sleep(7);
   } catch (InterruptedException e1) {
      e1.printStackTrace();
   }

            if(System.currentTimeMillis() - lastReload > 17)
             {
              lastReload = System.currentTimeMillis();
             //move
            if (down)
              {
                 character.setXY(character.getX(),character.getY()+s);  
              }
              if (up)
              {
                 character.setXY(character.getX(),character.getY()-s);
              }
              if (right)
              {
                 floor.setXY(floor.getX()-s,floor.getY());
                character.setImg("character_r.png");
              }
              if (left)
              {
                 floor.setXY(floor.getX()+s, floor.getY());
                character.setImg("character_l.png");
              }
                  }
             
      f.repaint();
          }//for;;


Still, that probably won't solve your problem.

Just so you know, checking whether the elapsed milliseconds is more than 17 or sleeping for 7 milliseconds is likely equivalent to 20 and 10 milliseconds on Windows computers because that's as precise as the timer is. .

You should look into active rendering and use the BufferStrategy class (see the API).  Switching to full-screen exclusive mode is a way to speed things up, but it seems clear that isn't the problem for you.  Your program's simple enough that it shouldn't run so slow.

Is your main loop being executed on the Event Queue thread?  If so, that's something you should avoid.
Offline pifreak

Senior Newbie





« Reply #4 - Posted 2009-03-10 03:26:41 »

something interesting:

I added a map loader, and when I go into the new map and after it loads, it goes back to "lagg mode" until I play with the window size by dragging it around Tongue

I took the floor image out and then it is less laggy at startup, but still magically fixable for some reason into the laggless state when you play with the JFrame.


I'll look into the stuff you recommended, but any other ideas?
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.

CogWheelz (16 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

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

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

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

Riven (31 views)
2014-07-23 20:56:16
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!