Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
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  
  Game development with Swing: Using threads.  (Read 804 times)
0 Members and 1 Guest are viewing this topic.
Offline AvivC

Junior Member


Projects: 1



« Posted 2014-03-15 14:12:40 »

I admit to have corss-posted this to another site, but I'm getting no help there, so I'll try my luck here Smiley

(Note: Even if you don't recommend Swing for game development, please try to answer my question as it is).

For those of you who use Swing to develop their Java games.

As I see it, there are three main possible approaches to designing games in Swing, in relation to threads:

1- Have the entire game (except for especially long-running tasks) run on the EDT.

2- Have the entire game run on a non-EDT thread.

3- Have clear seperation between a thread to constantly run game logic (a non EDT thread), and a thread to constantly update the screen (the EDT).

By your experience and knowledge, which approach is the most common, and/or the most efficient, and/or the most reasonable programming-wise (aka isn't relativley very complex to design)? Which approach should I use?

Help would be appreciated Smiley (I really want to pick up good habits).
Offline Drenius
« Reply #1 - Posted 2014-03-15 14:16:03 »

You can use threads, but there is less advantage than you might think...
But you could or will get problems with accessing lists using it.
Offline SHC
« Reply #2 - Posted 2014-03-15 14:19:56 »

Though I don't recommend using Swing for games anymore, I'd say option #2 is the best way to use swing for games, since you cannot do too much work on the EDT and using two threads for both rendering and logic results in a mess.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2014-03-15 14:37:53 »

You can use threads, but there is less advantage than you might think...
But you could or will get problems with accessing lists using it.
What I think is that on an N core machine I can get more than a N times increase in through put by having a well chosen number of active threads that's greater than N.  And I'm consistently correct.
Online princec

JGO Kernel


Medals: 379
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #4 - Posted 2014-03-15 16:45:04 »

Hm, YMMV, but I don't get quite that sort of scaling in throughput.

In other news though... simplest solution is to run the game all on the EDT (if it an arcade game) using active rendering. If it is a turnbased game (ie. UI led) then run it on the EDT as well, but be aware that long-running tasks will freeze the UI completely, and these should be punted to their own thread and the UI updated with progress indication.

Cas Smiley

Offline Roquen
« Reply #5 - Posted 2014-03-15 18:03:14 »

Absolutely YMMV.  It going to depend on your experience, how well you can design the separation of tasks, what they are, when they have to run, etc. etc.  My point being there's this mistaken notion that there isn't much to be gained by multi-threading...don't believe it.
Offline AvivC

Junior Member


Projects: 1



« Reply #6 - Posted 2014-03-15 22:51:19 »

Hm, YMMV, but I don't get quite that sort of scaling in throughput.

In other news though... simplest solution is to run the game all on the EDT (if it an arcade game) using active rendering. If it is a turnbased game (ie. UI led) then run it on the EDT as well, but be aware that long-running tasks will freeze the UI completely, and these should be punted to their own thread and the UI updated with progress indication.

Cas Smiley

If using one thread, I think it better be a non-EDT thread. Why are you suggesting the EDT?
Online princec

JGO Kernel


Medals: 379
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #7 - Posted 2014-03-16 10:40:46 »

If you're making an arcade game your game loop has to execute in 17ms anyway (well, it'd be nice if it did at any rate) and thus it doesn't matter if it's on the EDT.
If you're making a turnbased event-driven game, if your game logic executes in just a couple of hundred ms, then no-one's going to notice, and it'll be easier to code.

Interacting with Swing when you're not on the EDT is a bit ugly and annoying, so it's just easier. By all means run it all in a separate thread, but be aware you can do absolutely *nothing* to the UI in that thread, and that thread can't even listen for events on the EDT either. The only way you can communicate with the EDT is to use SwingUtilities.invokeLater() (or SwingUtilities.invokeAndWait()). That is a pain.

Cas Smiley

Offline SHC
« Reply #8 - Posted 2014-03-16 11:25:14 »

I object that. When I'm learning swing, I've used to just override the
paintComponent()
method. But when my game progressed enough to make a scrolling maps, when all the objects (2-3 enemies and the player on screen and floor is made as tiles), the game started to lag. I've then read Brackeen's Java Game Book and it suggested that using Threads, one could solve this and by moving game loop to the
run()
and implementing the
Runnable
, it ran very smooth. Later, I've made all those tiles as dynamic objects as well, and with almost 30-40 objects on the screen, and the map is scrolling, I've achieved 70-90 fps.

Offline AvivC

Junior Member


Projects: 1



« Reply #9 - Posted 2014-03-17 07:58:51 »

I object that. When I'm learning swing, I've used to just override the
paintComponent()
method. But when my game progressed enough to make a scrolling maps, when all the objects (2-3 enemies and the player on screen and floor is made as tiles), the game started to lag. I've then read Brackeen's Java Game Book and it suggested that using Threads, one could solve this and by moving game loop to the
run()
and implementing the
Runnable
, it ran very smooth. Later, I've made all those tiles as dynamic objects as well, and with almost 30-40 objects on the screen, and the map is scrolling, I've achieved 70-90 fps.

Does that mean that your game lagged on the EDT, and moving it to a different thread solved the lags? (The entire game run on a non-EDT thread?)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online princec

JGO Kernel


Medals: 379
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #10 - Posted 2014-03-17 10:00:06 »

Wrong on so many levels  Emo

Active rendering in Swing is where you run the EDT at 60Hz. You paint every 17ms. If you can't run your game loop in 17ms, rethink your game loop, because something is probably broken. Do not resort to threads until you know how to get this bit right. (See also: make Pong before your first MMO)

Cas Smiley

Offline SHC
« Reply #11 - Posted 2014-03-17 14:09:07 »

@AvivC

Yes. Now my game is entirely on a new thread.

@princec

I've started programming games with this framework tutorial. It said a Game thread and I've followed it simply. Also can you say why are you opposing the use of threads for the gameloop by a beginner? Even the first thing mentioned in the newboston's tutorials or brackeens book is Threads.

Offline nsigma
« Reply #12 - Posted 2014-03-17 14:25:15 »

Active rendering in Swing is where you run the EDT at 60Hz.

That's not what's usually meant by active rendering.  I've yet to see a tutorial, including Oracle's (well Sun's!  Wink ) own on this, that talks about doing it in the EDT, and the same for the BufferStrategy JavaDoc.  I'd also be highly suspect about what the OP means by "Swing".  Are we talking actual UI components, or just drawing on a big canvas?

Usually I'd recommend ignoring the Swing stuff, and use a Frame and a Canvas from the awt package.  Create a BufferStrategy from the Canvas, and directly draw into that from your rendering loop, outside the EDT.  You can also get a BufferStrategy direct from the Frame / Window, but I prefer the flexibility of using Canvas.  It's also best to set to ignore repaint requests (setIgnoreRepaint()) to avoid any possible conflict with the EDT, and beware of any events fired by the window, etc. which will still be in the EDT.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
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.

BurntPizza (28 views)
2014-09-21 02:42:18

BurntPizza (18 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (46 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (101 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56

mitcheeb (71 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!