Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  [SOLVED]Keyboard Input Lag  (Read 1816 times)
0 Members and 2 Guests are viewing this topic.
Offline duce

JGO n00b
*

Posts: 47



« on: 2012-01-27 12:43:08 »

I'm trying to make a game in Java. I decided to be stubborn, and not use LWJGL or Slick. Everything went fine until input. The keyboard arrow presses are delayed, and I don't know what is causing it.

http://pastebin.com/0LTdKr21

EDIT:
Solved. Needed to convert my images during initialization.
http://pastebin.com/9S8QMXby
Online ReBirth

JGO Wizard
****

Posts: 1279
Medals: 19



« Reply #1 on: 2012-01-27 16:54:00 »

That's not stubborn, otherwise is Grin
try to let the JFrame to implement listener.

Offline duce

JGO n00b
*

Posts: 47



« Reply #2 on: 2012-01-27 19:56:39 »

Didn't help at all Sad It worked perfectly in my games made in Slick. Maybe something is wrong with my game loop? What is the purpose of SwingUtilities.invokeLater()?
Games published by our own members! Go get 'em!
Offline sproingie

JGO Strike Force
***

Posts: 899
Medals: 55



« Reply #3 on: 2012-01-27 20:22:47 »

What is the purpose of SwingUtilities.invokeLater()?

"Many thousands are in want of common answers; hundreds of thousands are in want of common knowledge, sir."

"Are there no search engines?"

"Plenty of search engines, said the gentleman, laying down his keyboard again.

"And the javadoc on Oracle's website", demanded Sproingie, "is it still in operation?"

"Both very busy sir."

"Oh. I was afraid, from what you said at first, that something had occurred to stop them in their useful course," said Sproingie.  "I'm very glad to hear it."


Humbug!  Cranky

Offline GabrielBailey74

Full Member
**

Posts: 157
Medals: 2


Owner of Elite Demons R.I.P


« Reply #4 on: 2012-01-27 20:23:00 »

Believe it's something like:

1  
2  
3  
4  
5  
public void init() {
SwingUtilities.invokeLater() {
//Than add the action listener for the event performed here?
};
}


You'd want it in your main loop of course, you wouldn't want to call it upon just start of the program.

Offline Spudgun

JGO n00b
*

Posts: 5



« Reply #5 on: 2012-01-27 21:27:54 »

I came across 'delays' in my early game efforts which seemed to be related to how key repeats are handled. The way to demonstrate the effect is to open a text editor and hold down a letter key.  After the first letter is displayed there is a noticeable delay after which the letter then repeats quickly (in Windows at least).  I would get the same responses in my game.

My way round it is rather than respond to action directly in the overridden keylistener methods,  I use them to set booleans which eliminate the 'lag' as the game movement logic now relies on the boolean values rather than the raw keypress data.

I'm not sure if this is the same problem you are experiencing, but from what you are describing it sounds very familiar to my experiences.
Offline Cero

JGO Neuromancer
****

Posts: 1050
Medals: 18



« Reply #6 on: 2012-01-27 21:34:11 »

Didn't we just had Christmas =D

Offline duce

JGO n00b
*

Posts: 47



« Reply #7 on: 2012-01-27 22:29:00 »

I tried having the key listeners set flags, but it still delays. I had my listeners do println()'s, and the message doesn't delay, but the movement does.
Online ReBirth

JGO Wizard
****

Posts: 1279
Medals: 19



« Reply #8 on: 2012-01-28 07:06:51 »

Then something happend on your framerate, which can be caused by busy logic or drawing lack. It's pretty scary you did duo run method there, plus I saw nested ArrayList called blocks. I suggest you to narrow down the main class. Also, you use singleton Textures, so post it may help.

Offline duce

JGO n00b
*

Posts: 47



« Reply #9 on: 2012-01-28 08:19:32 »

http://pastebin.com/mdEDW6Tx

Here's what fills my ArrayList
http://pastebin.com/AZLKQV63
Games published by our own members! Go get 'em!
Offline duce

JGO n00b
*

Posts: 47



« Reply #10 on: 2012-01-28 08:24:57 »

Okay, so to test the FPS of my game, I loaded Fraps up to see the counter. The game counts up from 0 to eight frames per second, then stops there. I have no idea why.
Online ReBirth

JGO Wizard
****

Posts: 1279
Medals: 19



« Reply #11 on: 2012-01-28 10:08:49 »

I see. Your code is too mess. Even on singleton you call another singleton just to load image. Nothing wrong on LevelLoader class. So you can take this first aid: change the structure. avoid nested loop. you actually no need to use thread, since your gameloop method acts similiar. For example:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public class Yours extends Canvas{
public Yours(){
//init the Canvas
//add listener (below class)
//call JFrame, init and add(this)
}
void gameLoop(){
while(gameRunning){
//update, render, sleep, FPS calculation
}
}
public static void main(String[] args) {
Yours y = new Yours();
y.gameLoop();
}
...
class X implements KeyListener{
//do it
}
}

Offline duce

JGO n00b
*

Posts: 47



« Reply #12 on: 2012-01-28 11:23:10 »

I tried what you said, but nothing changed.

http://pastebin.com/33reqdRy
Offline duce

JGO n00b
*

Posts: 47



« Reply #13 on: 2012-01-28 14:41:10 »

The FPS is capped at 8. If I set it to update slower, it will. But higher than 8 will just result in 8.
Offline duce

JGO n00b
*

Posts: 47



« Reply #14 on: 2012-01-28 14:48:11 »

When turning off the drawing of the level, it significantly improves the FPS, and the listener responds quickly. Here is the Level class and its draw function.

http://pastebin.com/TmuFW3te
Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #15 on: 2012-01-28 21:38:43 »

1  
g2d.scale(gameScale, gameScale);

Don't do this.

There is no god.
Offline Shazer2

Jr. Member
**

Posts: 66
Medals: 3



« Reply #16 on: 2012-01-28 21:45:23 »

Do this for the loop too, instead of doing blocks.get(i) everytime.
1  
2  
3  
for (Block block : blocks) {

}


That way you can access it by doing
1  
2  
3  
switch(block.get(j)) {

}


Just a bit of a cleaner way.

~Shazer2 Smiley

"When you want to be successful as bad as you want to breathe, then you will be successful." - Eric Thomas
Online ReBirth

JGO Wizard
****

Posts: 1279
Medals: 19



« Reply #17 on: 2012-01-28 22:35:14 »

Like theagentd said. Also, if your blocks has limited size (not growing on fly) or you know the max size that never will be exceeded, I suggest to use 2D array.

Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #18 on: 2012-01-28 22:57:35 »

Do this for the loop too, instead of doing blocks.get(i) everytime.
1  
2  
3  
for (Block block : blocks) {

}


~Shazer2 Smiley
That's generally slower since this creates an Iterator instance every frame. Since when did accessing array elements become slow?

There is no god.
Offline Shazer2

Jr. Member
**

Posts: 66
Medals: 3



« Reply #19 on: 2012-01-28 22:59:18 »

It is what I was told in #lwjgl by sproingie.

~Shazer2 Smiley

"When you want to be successful as bad as you want to breathe, then you will be successful." - Eric Thomas
Offline duce

JGO n00b
*

Posts: 47



« Reply #20 on: 2012-01-29 02:46:56 »

Thanks for the help, everyone! I managed to find the problem. I needed to use createCompatibleImage() to convert my images to the required format.

Here is my new Texture class:
http://pastebin.com/9S8QMXby
Offline sproingie

JGO Strike Force
***

Posts: 899
Medals: 55



« Reply #21 on: 2012-01-30 12:47:32 »

That's generally slower since this creates an Iterator instance every frame. Since when did accessing array elements become slow?

This is like telling someone driving a fully loaded 18-wheeler to take the bumper stickers off to shed weight.  The point is to get familiar with programming idioms that are actually clear.
Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #22 on: 2012-01-30 14:08:36 »

That's generally slower since this creates an Iterator instance every frame. Since when did accessing array elements become slow?

This is like telling someone driving a fully loaded 18-wheeler to take the bumper stickers off to shed weight.  The point is to get familiar with programming idioms that are actually clear.

That depends on how much you use it. Tell someone that it's okay to do use those for-loops and he/she is going to use them for nested for-loops. I've saved milliseconds by changing it to a normal for-loop, and I don't have many of those you know.

There is no god.
Offline pitbuller

Sr. Member
**

Posts: 340
Medals: 9



« Reply #23 on: 2012-01-30 15:32:34 »

That's generally slower since this creates an Iterator instance every frame. Since when did accessing array elements become slow?

This is like telling someone driving a fully loaded 18-wheeler to take the bumper stickers off to shed weight.  The point is to get familiar with programming idioms that are actually clear.

That depends on how much you use it. Tell someone that it's okay to do use those for-loops and he/she is going to use them for nested for-loops. I've saved milliseconds by changing it to a normal for-loop, and I don't have many of those you know.
Just for additional notes that on android for:each loop is faster that indexed loop and do not produce iterator but only for normal array. For collections that cannot be indexed in constant time they are faster and create garbage but  ArrayList they are slower. I used lot of time with timers and byte/dex code to get this conclusion.

Ps. Don't say its does not matter much. I know it allready.

Offline sproingie

JGO Strike Force
***

Posts: 899
Medals: 55



« Reply #24 on: 2012-01-30 15:45:17 »

"Optimizing" the iterator out of a brute-force algorithm.  Bikeshedding at its finest.
Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #25 on: 2012-01-31 00:02:02 »

"Optimizing" the iterator out of a brute-force algorithm.  Bikeshedding at its finest.

-_-

Let's just say I'm not using iterators in my particle engine that runs 1 million particles, or in the ray-casting method I have to check visibility between two points on a tile-map.

There is no god.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.136 seconds with 19 queries.