Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  First Post - Bufferstrategy, jpane and canvas?  (Read 1636 times)
0 Members and 1 Guest are viewing this topic.
Offline BrassApparatus

Junior Member





« Posted 2013-04-15 11:14:03 »

Hey, everybody, this is going to be my first post here so here's a little background.
I'm self taught and only moderately new to java. I have prior experience with C++ up to about the intermediate level but decided to learn something else. I've been reading books, posts and sourcecode to help me get the hang of JG basics. I've been reading through Mojang's Catacomb Snatch while following JGO for a little while but I'm beginning to run into some questions I  can't seem to answer.

TLDR: I'm fairly new and I don't understand some junk.

1: I feel like a have a fair grasp of how a game loop "should" be structured;  I definitely like the fixed timestep version.. But I don't don't really understand the difference between two of the posts I've seen here.
Eli's fixed timestep loop here: http://www.java-gaming.org/topics/game-loops/24220/view.html
and ra4king's timestep loop here: http://www.java-gaming.org/topics/about-on-my-main-loop/26222/msg/229028/view.html#msg229028

I understand some of the differences. What I'm really hung up on is the purpose of Eli's interpolation. I don't fully understand why it is necessary. And to clarify, I'm going with the assumption in this case that Eli's code is more precise   because both of these loops seem to be well respected  and Eli's is longer. Smiley

2: I am not completely clear on some of the ways I've seen BufferStrategy implemented. I have followed TheNewBoston's turorials on Youtube but they seem somewhat lax in the Java Gaming section. http://www.youtube.com/user/thenewboston?feature=watch
I have read  Oracle's documents on BufferStrategy, JFrame, Canvas etc. and I understand how BS works and why it's great but I am not clear on the differences between JPanel, Canvas and how they act when working with BS. I have seen a BS created from a Window, JFrame, Canvas, and JPanel but I don't understand why one would be preferred over the other. In Catacomb Snatch Notch uses a JFrame packed with a JPanel yet I see many examples of people creating a BS from a JFrame directly or more often from a Canvas. I am assuming that when you call Canvas.createBufferStrategy(2) it is really calling .createBufferStrategy(2) on the frame it's packed in but idk. I'm beginning to have dreams related to this question so some clarity would REALLY help.
P.S. I have seen code where .pack() is not used. Why is it preferred or vice versa?

3: While it all seems both concise and clever (imho) I would really love an explanation of why ra4king uses a canvas in the link in number 1. I keep seeing "Oh god DON'T use swing and awt together!" but I'm not seeing a consensus on the issue so I'm at a loss as to why you would use Canvas over JPanel. What I think I understand is that JPanel is best used when using swing components like JButton and that awt has little conflict outside of interactions with those components. Tell me if I'm right: JPanel is more efficient for using components and since most of us on JGO wouldn't be using them we prefer Canvas because it's not at the mercy of the EDT.  Is that even close?
In addition, I'm assuming that the purpose of ra4king's daemon thread is because .sleep(int) is more accurate if it's in constant use.

Lastly, thanks for reading and feel free to help me with forum etiquette cause this is my first real forum post, ever. And I'd love input from either ra4king or Eli here.

TLDR: Read the underlined junk. Also, I'm new, thanks.
Offline StumpyStrust
« Reply #1 - Posted 2013-04-15 14:34:41 »

I will let someone else get to the gameloop but I can help with the BS (no pun intended)

JPanel and most Swing components are double buffered automatically and I do not think canvas is. They also use passive rendering which is not what we want. Normally, you use a canvas because it is meant to be used to draw stuff with where a JPanel is not. I have used both and with a few extra settings in JPanel, they both work fine. They whole thing is like you said, don't mux up AWT and Swing. The reason why we use BS is to keep things actively rendering and not passive which is what java2D wants to do. You also need to double buffer when you want animations to be smooth. pack() will automatically shrink all components into the smallest size in a JFrame. So if your frame is 1600x900 and your canvas is 800x600 it will make the JFrame 800x600. I may be wrong on this one.

Now comes what you probably don't want to hear. Drop learning any rendering in Java2D. It is crap. Use libgdx or slick2D. No need to wonder about all the black magic java2D is doing and the reasons why.

If you insist on java2D then before you wonder all the magical ways of getting java2D to perform right, here is this.

http://www.java-gaming.org/topics/java2d-clearing-up-the-air/27506/msg/246444/view.html

If you would like, I can post my old classes that I used to get java2D to perform well but really you should just drop java2d.

Offline BrassApparatus

Junior Member





« Reply #2 - Posted 2013-04-15 20:15:26 »

Thanks for the help!

Now it makes sense, pack re sizes to fit the smallest piece.

Really, I don't mind hearing that. I've seen that before on here quite often. The only reason I haven't been looking into libraries is that I want to begin working on my projects the "right" way. I don't mind the idea of a library, I just wonder, are they really more efficient? I'm afraid of using them as a crutch simply because its easier. If they're actually better then I'm on board. Smiley
If that's the case, is there a pro/con page for the popular ones?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Grunnt

JGO Wizard


Medals: 64
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #3 - Posted 2013-04-15 20:36:05 »

Welcome!

Really, I don't mind hearing that. I've seen that before on here quite often. The only reason I haven't been looking into libraries is that I want to begin working on my projects the "right" way. I don't mind the idea of a library, I just wonder, are they really more efficient? I'm afraid of using them as a crutch simply because its easier. If they're actually better then I'm on board. Smiley
If that's the case, is there a pro/con page for the popular ones?

Using Java2D when there's LibGDX around is less like not using a crutch and more like breaking your leg intentionally :-) Java2D is a o.k. place for beginners to start, as it does not introduce some complexities relating to the use of libraries (e.g. some classpath stuff) and allows you to start at a very basic level, but it is generally lacking in features and performance compared to the OpenGL-based alternatives such as LibGDX, Slick2D or LWJGL (which is more of a basic wrapper that underlies both). Especially since you seem to have quite some programming experience just starting using one of these libraries may be a good idea. Or just try some extremely simple games using Java2D to get the hang of it.

With regards to 1: I never used interpolation in this way (too complex for my little brain), but kind of understood how it works after reading this brief explanation.

Offline BrassApparatus

Junior Member





« Reply #4 - Posted 2013-04-15 22:00:57 »

Thanks Grunnt.

I'm looking into LWJGL and libGDX. I have pretty much decided that for my purposes interpolation is overkill.
At this point, some advice / tutorial suggestions on the above would be really helpful.

Thanks again guys
EDIT: I am thinking I'm going lidGDX. This post seems to have confirmed what I was suspecting -->http://www.java-gaming.org/topics/lwjgl-vs-libgdx/28840/view.html

Also, would someone explain how to post a link as "link" instead of "http://"?
Offline Grunnt

JGO Wizard


Medals: 64
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #5 - Posted 2013-04-15 23:02:37 »

You can post as a link using [ url = http://stuff]title[ / url ] without the spaces ofcourse.

Tutorials and getting started info on LibGDX is available in the wiki. Good luck!

Offline Z-Man
« Reply #6 - Posted 2013-04-16 01:03:03 »

pack() will automatically shrink all components into the smallest size in a JFrame. So if your frame is 1600x900 and your canvas is 800x600 it will make the JFrame 800x600. I may be wrong on this one.
The JFrame will be a bit bigger than 800x600 to allow for an internal area (the Canvas) of 800x600 plus the window's border.

The only reason I haven't been looking into libraries is that I want to begin working on my projects the "right" way. I don't mind the idea of a library, I just wonder, are they really more efficient? I'm afraid of using them as a crutch simply because its easier. If they're actually better then I'm on board. Smiley
If that's the case, is there a pro/con page for the popular ones?
The OpenGL to java bindings are all more efficient simply because OpenGL is a native, hardware accelerated, library. I really wouldn't call LWJGL or LibGDX a crutch but, of the two LWJGL is definitely lower level as it's mostly a direct binding to the native OpenGL libraries. I switched from Java2D to LWJGL simply because I like learning how things work and writing more things for myself as a learning experience instead of using more of a game-dev library like LibGDX. Whichever you choose good luck Smiley
Offline BrassApparatus

Junior Member





« Reply #7 - Posted 2013-04-16 01:29:38 »

I knew in the back of my head that JFrame would be larger but I'd kind of forgotten it.

I tend towards that "Do it myself" mentality but I tihnk im going to try libGDX then maybe next time LWJGL. Thanks for the advice Smiley
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2013-04-16 06:46:40 »

Well.....err.....since everything else has been covered, I'll just drop in and say hi! Smiley

Also, here is an earlier post of mine that explains Passive vs. Active rendering.

Offline BrassApparatus

Junior Member





« Reply #9 - Posted 2013-04-16 07:16:11 »

Well.....err.....since everything else has been covered, I'll just drop in and say hi! Smiley

Actually there is one thing you could help explain. -See end of #3-  I  am assuming that you make a daemon thread and have it sleep simply because it makes the timing mechanisms more efficient if they're running constantly. Or am  I missing something? 
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2013-04-16 08:01:48 »

The daemon thread forces the system to use the high precision timer. This will cause sleep(int) to be more accurate Smiley

Offline SHC
« Reply #11 - Posted 2013-04-16 08:09:05 »

You can see my game engine. (It's totally Java2D)

GEJ - Game Engine 4 Java

Offline BrassApparatus

Junior Member





« Reply #12 - Posted 2013-04-17 00:38:28 »

Thanks all! This has really cleared up a few things for me. Cheesy And its good to see that someone out there is doing Java2D seriously too.
tyty
Offline sproingie

JGO Kernel


Medals: 201



« Reply #13 - Posted 2013-04-23 18:09:26 »

The daemon thread forces the system to use the high precision timer. This will cause sleep(int) to be more accurate Smiley

A little semi-necro since I'm behind: Just to clarify, you only need this daemon thread trick on Windows.  The precision of the scheduler timers on Linux and OSX are always pretty good and can be made better with system calls (involving JNI stuff you probably don't want to mess with)
Offline BrassApparatus

Junior Member





« Reply #14 - Posted 2013-04-23 23:00:07 »

Thats what I was thinking but its really nice to have all the clarification. Ty Smiley
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.

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

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

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

Riven (20 views)
2014-07-23 20:56:16

ctomni231 (48 views)
2014-07-18 06:55:21

Zero Volt (44 views)
2014-07-17 23:47:54

danieldean (35 views)
2014-07-17 23:41:23

MustardPeter (38 views)
2014-07-16 23:30:00

Cero (53 views)
2014-07-16 00:42:17

Riven (52 views)
2014-07-14 18:02:53
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!