Zweihander - a SHMUP in Java...

A place for people with an interest in developing new shmups.
Post Reply
relminator
Posts: 55
Joined: Sat Jun 05, 2010 1:48 pm

Zweihander - a SHMUP in Java...

Post by relminator »

Now runs smooth on my laptop but still stutters a little on my netbook.


Updated DL:
http://rel.phatcode.net/junk.php?id=139

No sound version:
http://www.rel.phatcode.net/Temp/%5bJav ... oSound.zip


Image

I didn't even use OpenGL just straight Java2D.

BTW,

1. Does it still stutter?
2. Does the no sound version still stutter?

Also improvements on code are welcome.

Update: (Decided I'll make a game)

1. Now named zweihander (unless that name is taken)
2. Not a single heap allocation when in gameplay mode (thanks to mystran for reporting the GC stuff)
3. Added a single type enemy with lotsa bullets.
4. Uploaded a no sound version for testing
Last edited by relminator on Fri Mar 22, 2013 5:15 am, edited 5 times in total.
User avatar
BPzeBanshee
Posts: 4859
Joined: Sun Feb 08, 2009 3:59 am

Re: SHMUPS in Java...

Post by BPzeBanshee »

Framerate seems to vary but it's around ~900 on an i3 here.

Gimme a while and I'll test this thing on my Pentium III 1ghz legacy PC which no doubt will probably suffer performance issues.
relminator
Posts: 55
Joined: Sat Jun 05, 2010 1:48 pm

Re: SHMUPS in Java...

Post by relminator »

Thanks for testing! It sure would run like a snail on a p3. LOL

BTW added a bullet manager and a ship...

http://rel.phatcode.net/junk.php?id=138
User avatar
AntiFritz
Posts: 1630
Joined: Sun Apr 03, 2011 2:34 am
Location: Australia

Re: SHMUPS in Java...

Post by AntiFritz »

Runs at 900 - 930 fps on my core i5. Seems to be fairly smooth.
RegalSin wrote:Rape is very shakey subject. It falls into the catergory of Womens right, Homosexaul rights, and Black rights.
relminator
Posts: 55
Joined: Sat Jun 05, 2010 1:48 pm

Re: SHMUPS in Java...

Post by relminator »

Did you try the newest one? With the controllable ship and a moving BG?

On my netbook(intel atom with intel gfx) there's a stutter every now and then.
User avatar
AntiFritz
Posts: 1630
Joined: Sun Apr 03, 2011 2:34 am
Location: Australia

Re: SHMUPS in Java...

Post by AntiFritz »

The newer one ran at almost 1000 fps.
RegalSin wrote:Rape is very shakey subject. It falls into the catergory of Womens right, Homosexaul rights, and Black rights.
mystran
Posts: 77
Joined: Tue Mar 12, 2013 11:59 pm
Location: Helsinki, FI

Re: SHMUPS in Java...

Post by mystran »

relminator wrote:Did you try the newest one? With the controllable ship and a moving BG?

On my netbook(intel atom with intel gfx) there's a stutter every now and then.
In (compiled) managed languages the biggest problem tends to be unpredictable garbage collection. Java is especially bad, since it's quite hard to avoid producing garbage and I suspect you might be getting stutters when the garbage collector runs and takes too long on the slow CPU (on a faster CPU it might run fast enough that you won't notice). C# is actually a bit better if you're just trying to get a nicer (than C++) environment; at least in C# you can put structs in the stack to help reduce garbage production.

I would test on my (cheapish) laptop, but I don't want to install JRE.
relminator
Posts: 55
Joined: Sat Jun 05, 2010 1:48 pm

Re: SHMUPS in Java...

Post by relminator »

mystran wrote:
relminator wrote:Did you try the newest one? With the controllable ship and a moving BG?

On my netbook(intel atom with intel gfx) there's a stutter every now and then.
In (compiled) managed languages the biggest problem tends to be unpredictable garbage collection. Java is especially bad, since it's quite hard to avoid producing garbage and I suspect you might be getting stutters when the garbage collector runs and takes too long on the slow CPU (on a faster CPU it might run fast enough that you won't notice). C# is actually a bit better if you're just trying to get a nicer (than C++) environment; at least in C# you can put structs in the stack to help reduce garbage production.

I would test on my (cheapish) laptop, but I don't want to install JRE.
I need to so this in Java (not my choice since I'd rather do things in C++).
mystran
Posts: 77
Joined: Tue Mar 12, 2013 11:59 pm
Location: Helsinki, FI

Re: SHMUPS in Java...

Post by mystran »

relminator wrote:
mystran wrote:
relminator wrote:Did you try the newest one? With the controllable ship and a moving BG?

On my netbook(intel atom with intel gfx) there's a stutter every now and then.
In (compiled) managed languages the biggest problem tends to be unpredictable garbage collection. Java is especially bad, since it's quite hard to avoid producing garbage and I suspect you might be getting stutters when the garbage collector runs and takes too long on the slow CPU (on a faster CPU it might run fast enough that you won't notice). C# is actually a bit better if you're just trying to get a nicer (than C++) environment; at least in C# you can put structs in the stack to help reduce garbage production.

I would test on my (cheapish) laptop, but I don't want to install JRE.
I need to so this in Java (not my choice since I'd rather do things in C++).
Oh I see. If it seems GC is indeed the fault and you can't reduce allocations, then forcing a GC at "known good moments" might also work... but then again running at 900-1000fps is kinda sensitive to everything. On Windows for example, granularity below 10ms intervals is pretty much "let's pray" unless you have a system optimized for low latency work (eg audio). I wouldn't worry about it if you get average near 1000fps and it runs smooth at 60 (which is about 17ms which is quite reasonable even if a background task decides to wake up).
relminator
Posts: 55
Joined: Sat Jun 05, 2010 1:48 pm

Re: SHMUPS in Java...

Post by relminator »

Oh, the FPS varies since that's the amount of loop the gameloop executes per second.

The game logic on the other hand runs at a steady 60 FPS since I use a fixed timepstep via delta-timing.
mystran
Posts: 77
Joined: Tue Mar 12, 2013 11:59 pm
Location: Helsinki, FI

Re: SHMUPS in Java...

Post by mystran »

Ok, so I installed latest JDK7 (x64) on my desktop (W7x64, i7-2600k, GTX570, so not netbook) and the speedtest only gets me about 880 typical (varies between 875-900) and I get VERY slight stutters at times (every 5-10 seconds really) when I move around. In the "shmup" I get solid 999-1000 obviously (disabling the sleep it goes from 1300-3500 or so.. which seems a bit weird to be honest), but even then I get some stutter that feels like a GC.. but testing with -verbosegc actually seems to indicate that (1) I get stutters (small ones) much more often than the GC actually runs and (2) GC doesn't actually cause any noticeable stutter on this system..

So I did a bit of debugging:

Code: Select all

	private void update() 
	{

		counter++;
		
                // When this branch is taken, I get stutter for few frames
                // it's a bit random how bad it is, but it's noticeable
		if( (counter & 255) == 0 )
		{
        
			TurretBehavior turretRotateBehavior;

Note that I also tried running 3 of them next to each other, and they all run 1000fps next to each other, and all stutter on that branch (independent of each other).

Looking at the code, there's some allocations going. You might want to avoid creating turret behaviors (and turrets and enemies and so on) as separate classes and use static data arrays as much as possible, also for turrets and enemies and everything; yes it does make code less OOP and uglier, but it would avoid having to call "new" which is only "obvious" red flag that I see there (or in the methods called). I checked (well tried at least) with test code against Math.random() doing something stupid, and it seems fine and safe. Still, GC collections actually don't cause a stutter, so maybe it's not allocs but something else hiding there that didn't catch my eyes (rest seem like rather reasonable state setup code).

Anyway, depending on how fast you're planning to go with the bullet speeds, it's not necessarily terrible for a slower game. With current bullet speeds though, it's certainly annoying. Also no idea if this is the stutter that you are talking about, but.. it's certainly weird (pretty much only sensible explanation would be heap lock during Java2D's D3D backend thread JNI calls, or some such similar thing, but even that seems a bit far fetched). Tried various heap tuning command line options too, but couldn't get any improvement. "jstack" shows that's using D3D for rendering here.

Well, don't know if this was of any use.
User avatar
Reiko
Posts: 47
Joined: Mon Dec 24, 2012 2:29 pm
Location: Australia

Re: SHMUPS in Java...

Post by Reiko »

Looks cool so far!

Don't know if the info is still useful to you, but on my comp:

The speed test shows about 780-820 or so, though it's only rendering at 60fps.
The shmup shows 994-1000 and also renders at that frame rate.

I did not notice any stutter.

CPU: i7-3770k @ 4.5ghz
Video: gtx670 @ 1200mhz/6900mhz
relminator
Posts: 55
Joined: Sat Jun 05, 2010 1:48 pm

Re: SHMUPS in Java...

Post by relminator »

mystran wrote:Ok, so I installed latest JDK7 (x64) on my desktop (W7x64, i7-2600k, GTX570, so not netbook) and the speedtest only gets me about 880 typical (varies between 875-900) and I get VERY slight stutters at times (every 5-10 seconds really) when I move around. In the "shmup" I get solid 999-1000 obviously (disabling the sleep it goes from 1300-3500 or so.. which seems a bit weird to be honest), but even then I get some stutter that feels like a GC.. but testing with -verbosegc actually seems to indicate that (1) I get stutters (small ones) much more often than the GC actually runs and (2) GC doesn't actually cause any noticeable stutter on this system..

So I did a bit of debugging:

Code: Select all

	private void update() 
	{

		counter++;
		
                // When this branch is taken, I get stutter for few frames
                // it's a bit random how bad it is, but it's noticeable
		if( (counter & 255) == 0 )
		{
        
			TurretBehavior turretRotateBehavior;

Note that I also tried running 3 of them next to each other, and they all run 1000fps next to each other, and all stutter on that branch (independent of each other).

Looking at the code, there's some allocations going. You might want to avoid creating turret behaviors (and turrets and enemies and so on) as separate classes and use static data arrays as much as possible, also for turrets and enemies and everything; yes it does make code less OOP and uglier, but it would avoid having to call "new" which is only "obvious" red flag that I see there (or in the methods called). I checked (well tried at least) with test code against Math.random() doing something stupid, and it seems fine and safe. Still, GC collections actually don't cause a stutter, so maybe it's not allocs but something else hiding there that didn't catch my eyes (rest seem like rather reasonable state setup code).

Anyway, depending on how fast you're planning to go with the bullet speeds, it's not necessarily terrible for a slower game. With current bullet speeds though, it's certainly annoying. Also no idea if this is the stutter that you are talking about, but.. it's certainly weird (pretty much only sensible explanation would be heap lock during Java2D's D3D backend thread JNI calls, or some such similar thing, but even that seems a bit far fetched). Tried various heap tuning command line options too, but couldn't get any improvement. "jstack" shows that's using D3D for rendering here.

Well, don't know if this was of any use.
Oh, it is very useful info! Thanks!!

I've updated the engine(decided I'll make a game out of it)
See first post for more detail...
http://rel.phatcode.net/junk.php?id=139

Anyways,

1. I've recoded almost everything regarding the entity managers so that they don't allocate memory on the fly (all allocs are done at startup so GC should not kick-in)
2. Used "recycling" instead of allocs to manage all my entities so no heap allocs are done midplay.
3. Does not stutter on my laptop(ubuntu) but stutters now and then on my netbook(win7/xp).


Ideas on stutter:

1. System.nanotime() does not use queryperformancecounter in windows thus, frames are missed.
2. Ram latency(behavior and animator classes are not batched) stalls the CPU but timer works independenty, so frames "jump".


BTW, can you teach me how you profiled the code to point to where the stutter is?

Thanks!

Reiko: Thanks for testing. Is your setup Linux or Windows?
mystran
Posts: 77
Joined: Tue Mar 12, 2013 11:59 pm
Location: Helsinki, FI

Re: SHMUPS in Java...

Post by mystran »

relminator wrote: I've updated the engine(decided I'll make a game out of it)
See first post for more detail...
http://rel.phatcode.net/junk.php?id=139
Curiously I still get some hickups (though different I guess), including audio [edit: a few] drop-outs (the audio lib isn't included so can't really debug that one). All I can say is it seems to happen more when I shoot so could be sound or maybe the sparks or.. I don't know. Still happens randomly at other times though, so .. I think there might be something wrong with the JDK I have, could try an older version later (or the separate JRE downloads, etc) to see if it works better. Right now I'm too lazy.
1. System.nanotime() does not use queryperformancecounter in windows thus, frames are missed.
Well, normally Windows timing accuracy is 10ms (on default desktop configuration at least), unless some program has requested higher accuracy (timeBeginPeriod() API.. best you can get that way is 1ms)... but I think Java might do that automatically anyway if you're getting 1000fps anyway (plus running another application just for that doesn't make any difference).
2. Ram latency(behavior and animator classes are not batched) stalls the CPU but timer works independenty, so frames "jump".
I don't think "ram latency" as such is a problem. Cache-trashing problems tend to just cause more uniform slowdown most of the time, since the actual "stalls" are still pretty short in the grand scale of things. Now, without VSync you do get weird timing variations sometimes, but.. then again that doesn't exactly explain audio drop-outs.
BTW, can you teach me how you profiled the code to point to where the stutter is?
I didn't really profile at all, rather I just applied two basic debugging techniques, namely "printf debugging" (mostly to familiarize myself with what's going on) and "temporarily comment out stuff until it works" then made a few modifications to try to convince myself that I was indeed looking at the right part (though I could be wrong; you never know until it's fixed for real).
relminator
Posts: 55
Joined: Sat Jun 05, 2010 1:48 pm

Re: Zweihander - a SHMUP in Java...

Post by relminator »

Thanks mystran!
Curiously I still get some hickups (though different I guess), including audio [edit: a few] drop-outs (the audio lib isn't included so can't really debug that one). All I can say is it seems to happen more when I shoot so could be sound or maybe the sparks or.. I don't know. Still happens randomly at other times though, so .. I think there might be something wrong with the JDK I have, could try an older version later (or the separate JRE downloads, etc) to see if it works better. Right now I'm too lazy.
Don't think it's the JRE itself since 70% of the people tested reports "smooth as silk" while the other 30% (sadly, that includes both you and me) reports stutter. I really have no idea why this happens. Some things I'm considering are:

1. Bad GFX card driver
2. OS
3. Timer implementation
4. Sound (runs from the JVM and which I have no control over its implementation)
5. Lack of vsynch ( actually call sync() via toolkit but no effect somehow. I'll try if the stutter disappears when in fullscreen mode.)

You could compile this w/o sounds by removing the Sonics.java class (a singleton) and removing all the references (not that many).

or...

DL the tinysound lib here: https://github.com/finnkuusisto/TinySound


What do you mean by audio "drop-outs"?
User avatar
BPzeBanshee
Posts: 4859
Joined: Sun Feb 08, 2009 3:59 am

Re: Zweihander - a SHMUP in Java...

Post by BPzeBanshee »

I updated Java the other day and the FPS display stopped reporting into the 900+ range, and it seemed to be stuttering a bit too. Should try and redownload now with the changes when I get the chance. :)
relminator
Posts: 55
Joined: Sat Jun 05, 2010 1:48 pm

Re: Zweihander - a SHMUP in Java...

Post by relminator »

BPzeBanshee wrote:I updated Java the other day and the FPS display stopped reporting into the 900+ range, and it seemed to be stuttering a bit too. Should try and redownload now with the changes when I get the chance. :)
Thanks for testing.

I've updated the engine.

Version with sound:
http://rel.phatcode.net/junk.php?id=139

No sound version:
http://www.rel.phatcode.net/Temp/%5bJav ... oSound.zip

Image


Please tell me if it still stutters.
User avatar
BPzeBanshee
Posts: 4859
Joined: Sun Feb 08, 2009 3:59 am

Re: Zweihander - a SHMUP in Java...

Post by BPzeBanshee »

It's now displaying an FPS of ~300, but it still looks like it's stuttering like it's missing a few frames out of 60. :?
User avatar
Reiko
Posts: 47
Joined: Mon Dec 24, 2012 2:29 pm
Location: Australia

Re: SHMUPS in Java...

Post by Reiko »

relminator wrote:Reiko: Thanks for testing. Is your setup Linux or Windows?
I'm using Windows 7.
User avatar
BPzeBanshee
Posts: 4859
Joined: Sun Feb 08, 2009 3:59 am

Re: Zweihander - a SHMUP in Java...

Post by BPzeBanshee »

I'm also using Windows 7. Sorry for mentioning it before, I've not been all that well.
User avatar
Reiko
Posts: 47
Joined: Mon Dec 24, 2012 2:29 pm
Location: Australia

Re: Zweihander - a SHMUP in Java...

Post by Reiko »

New version runs fine on my comp as well, though it probably doesn't mean much since my comp is probably a bit overpowered for the purpose.

I have two slower computers with AMD cpus in my house I can try it on though. I'll test it out on them and see how it goes.

By the way, again, according to Fraps, the game is actually being rendered at 1000 fps. I'm not sure if that's intentional or if it's supposed to only be rendering at 60 fps. Cause originally, your test version only rendered at 60 fps.

Edit: Also, probably the last thing on your mind right now :P but I'd set a limit on the number of sounds playing at once when you hit stuff, since it gets a bit noisy when a lot of shots hit at once.
http://youtu.be/WAyLr9D4w4w
Post Reply