Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (577)
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  
  Main loop for rendering and updating logic or spawn new threads  (Read 541 times)
0 Members and 1 Guest are viewing this topic.
Offline jh62

Senior Newbie





« Posted 2013-11-23 09:12:48 »

Hi!

I want to know if this approach is correct:

I have the main thread, as always, and currently it does all the work. I want to try and use the 2 cores my computer has (i always relied on one core only for simplicity) so I'm thinking to use the main loop for the calculations and other stuff not related to rendering graphics and spawn a new thread for this.

My Display class (JFrame) uses different states (JPanels) to change levels and menus, so my first idea is to make the state's superclass extend Thread and every new state I create will spawn a new thread that would run concurrently with the main loop, since there can be only 1 state present, no more than 2 threads would be present at all times.

Do you think this is a good approach?

Another question: If I have two threads, one for each core, do I need to put a sleep in the second thread? I have a sleeping timer for the rendering (main loop) to adjusting the frames so this are constant, but do I need to put the logic thread to sleep even for 1 ms or not?
Offline trollwarrior1
« Reply #1 - Posted 2013-11-23 10:14:34 »

Unless you're making some kind of super large AAA game, you should be fine with 1 thread.

There is just no way that your game needs more than 1 thread. Besides, rendering happens on the GPU, not CPU. Unless you're going through all the pixels on the screen.

The only time you should be having a second thread, is if your game has a lot of stuff to load. Than you could add another thread to load stuff and nothing more. That would make user experience better, because they wouldn't have to wait for stuff to load.
Offline Damocles
« Reply #2 - Posted 2013-11-23 10:19:01 »

Using seperate Threats shure can give you a boost in performance.
BUT, dont digg into that unless you create some very complex and demanding game simulation (using physics for example).

The effort to get it working right and flawless might not be worth it, when just developing a small indy game.
Suggesting: Stick to a single Thread for logic and rendering.

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

Senior Newbie





« Reply #3 - Posted 2013-11-23 10:38:13 »

Using seperate Threats shure can give you a boost in performance.
BUT, dont digg into that unless you create some very complex and demanding game simulation (using physics for example).

The effort to get it working right and flawless might not be worth it, when just developing a small indy game.
Suggesting: Stick to a single Thread for logic and rendering.

Thanks for the replies.

Anyway, I'm just doing this to get a better understanding of threads and concurrency while learning how to develop games.

I'll stick to one thread for now then.
Offline jh62

Senior Newbie





« Reply #4 - Posted 2013-11-23 10:40:54 »

Unless you're making some kind of super large AAA game, you should be fine with 1 thread.

There is just no way that your game needs more than 1 thread. Besides, rendering happens on the GPU, not CPU. Unless you're going through all the pixels on the screen.

The only time you should be having a second thread, is if your game has a lot of stuff to load. Than you could add another thread to load stuff and nothing more. That would make user experience better, because they wouldn't have to wait for stuff to load.

Thanks.

I'm using Java2D to make the game. I know how to draw 2D with LWJGL basically. I have to understand it better for me to be comfortable to make a game.

I'm using BufferedImages with createCompatibleImage so I guess I should be fine... Maybe if I get stuck at some point with the performance i'd switch to OpenGL.
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.

TehJavaDev (33 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (59 views)
2014-10-16 15:22:06

Norakomi (47 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!