Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
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  
  LWJGL for my 2D Game  (Read 5215 times)
0 Members and 1 Guest are viewing this topic.
Offline Malohkan

Senior Duke




while (true) System.out.println("WOO!!!!");


« Posted 2004-07-25 13:23:33 »

I've decided I want to use LWJGL for my game, but I need some help doing it.  The game is a top-down 2D game, and well it's really about finished, but for it to stand on its own as a PC game I need it to work quite a bit faster as Java2D just can't cut it when it comes to translating translucent images all over the place.  Even just plain tiling opaque images is too slow for my hopes.

Also, I want good sound, so I'm currently working through the OpenAL tutorials for this library and developing a tool to help me do that and it's going very well.

What ISN'T going well is me figuring out how to use OpenGL for my game.  Any set of example code I can find to use LWJGL for 2D is overwhelming to me, and I feel funny going through tutorials at NeHe because I'm never going to use anything I learn there with drawing 3D objects and wave transforms for them and all that.  I just want a faster way to draw my sprites.

I have my game done in Java2D and Graphics2D.drawImage() calls all over the place.  Now I want to be able to flip a toggle and go from that rendering method to an OpenGL rendering method, where I can draw all my sprites like I do but using all the speed advantages OpenGL can give me.  Can someone help me out?  I'm afraid I'm going to have to write a separate game just to get OpenGL rendering to work, when I just want it to be an augmentation of the working game I already have.

Thanks for any help or advice you guys can give me.  If you want to check out the current stage of the game in an Applet form, go here (controls are listed in the Options menu):
http://www.gamelizard.com/test/rimscape.html

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline Middy

Junior Duke




Java games rock!


« Reply #1 - Posted 2004-07-25 14:07:22 »

ava.lang.ClassNotFoundException: com.gamelizard.util.GLApplet

     at sun.applet.AppletClassLoader.findClass(Unknown Source)

     at java.lang.ClassLoader.loadClass(Unknown Source)

     at sun.applet.AppletClassLoader.loadClass(Unknown Source)

     at java.lang.ClassLoader.loadClass(Unknown Source)

     at sun.applet.AppletClassLoader.loadCode(Unknown Source)

     at sun.applet.AppletPanel.createApplet(Unknown Source)

     at sun.plugin.AppletViewer.createApplet(Unknown Source)

     at sun.applet.AppletPanel.runLoader(Unknown Source)

     at sun.applet.AppletPanel.run(Unknown Source)

     at java.lang.Thread.run(Unknown Source)

Caused by: java.io.IOException: open HTTP connection failed.

     at sun.applet.AppletClassLoader.getBytes(Unknown Source)

     at sun.applet.AppletClassLoader.access$100(Unknown Source)

     at sun.applet.AppletClassLoader$1.run(Unknown Source)

     at java.security.AccessController.doPrivileged(Native Method)

     ... 10 more


When do I get my makeMyGameAsILike() extension?
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #2 - Posted 2004-07-25 15:51:07 »

You *cannot* run LWJGL in an applet - you have to use Java Webstart. As for using LWJGL in 2D mode, look at org.lwjgl.examples.spaceinvaders

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

Junior Newbie




Java games rock!


« Reply #3 - Posted 2004-07-25 15:52:35 »

It worked fine for me!  Plus it runs really smooth.

To do 2D graphics you need to learn all the 3D stuff because for 2D you would draw all your sprites onto quads with no depth.  Your game is still in the 3D world.  There are some orthographic functions that you might want to look into though.
Offline Malohkan

Senior Duke




while (true) System.out.println("WOO!!!!");


« Reply #4 - Posted 2004-07-31 02:12:51 »

I checked out the LWJGL Space Invaders game and understood the source fine, so that was very helpful to learn about how LWJGL would work in comparison to how I have my game set up.  I also found a tutorial that illustrated using Sprites that would be created and drawn using whatever the selected rendering option was, with the choices being JOGL, LWJGL, and Java2D.  It was very insightful and made great sense, but still, for me to implement the idea felt like an overwhelming project.

I make a LOT of calls to the Graphics2D class throughout my game, only about half of them being to the drawImage() method.  So while I saw how I can draw sprites using LWJGL, there is a lot more going on my game that I don't know how to compensate for.  I really want to be able to toggle between Java2D mode and OpenGL mode, but I don't know what to do because I have so much I'm doing with the Graphics2D class and making my code work for both Java2D and OpenGL is just overwhelming.  Especially since for the majority of what I do I don't know of the GL options I have for doing the same thing.

I need help Tongue  Can someone give me some hope on this project?  I've spent two full days of code crunching trying to match ideas to make both methods work, and I've had almost completely 0 success.  I'm in need of some serious help.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline kevglass

JGO Kernel


Medals: 186
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #5 - Posted 2004-07-31 06:09:27 »

Funny thing is, what you need to complete this is something like J2DA (http://j2da.sf.net). However, I'd pretty mcuh decided this was a dead idea. The problem is if you try to support Java 2D and OpenGL you end up supporting only the lowest common denominator. While this is fine with most types of abstraction (daabases etc) for visuals this doesn't cut the mustard. If you want nice effects just choose the rendering technology you're going to use and stick to that.

Just my opinion of course,

Kev

Offline Malohkan

Senior Duke




while (true) System.out.println("WOO!!!!");


« Reply #6 - Posted 2004-07-31 11:17:57 »

the effects I pretty much already have.  All of the requests of my designer have been plenty possible with Java 2D, and they're done.  The game looks good, and it's clean looking.  I don't need any other effects, I just want the speed advantage.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline tom
« Reply #7 - Posted 2004-07-31 16:15:12 »

Quote
I make a LOT of calls to the Graphics2D class throughout my game, only about half of them being to the drawImage() method.  So while I saw how I can draw sprites using LWJGL, there is a lot more going on my game that I don't know how to compensate for.

What else do you need?

You can draw lines by passing GL_LINES to glBegin().
1  
2  
3  
glBegin(GL_LINES)
//line vertices
glEnd()


Other shapes, like sircles, you have to draw yourself by using line segments.

Offline Malohkan

Senior Duke




while (true) System.out.println("WOO!!!!");


« Reply #8 - Posted 2004-07-31 18:19:40 »

can I create Sprites on the fly and store them?  I do quite a bit of work onto BufferedImages, and I'd like to be able to create one, hold onto it, and draw it as much as I want on the screen.  That would let me do my Graphics2D tricks (primitive drawing and GradientPaint usage) and not have to worry about finding OpenGL ways to do it as well.  So far the only management I've found with Sprites has been by letting the library construct it from a file name and never letting you have any more access than that.  Even still, would it be at all efficient to be creating a Sprite object very often with its Texture components when all I really want to do is blit a new BufferedImage to the screen?

I also do a lot of work in terms of drawing with the AffineTransform class.  I assume there are fast counterparts to the transformation work I'd need to do for OpenGL right?

Also, what about regular computers without 3D graphics cards?  Will the OpenGL drivers still be able to draw my sprites faster than Java2D can?

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline tom
« Reply #9 - Posted 2004-07-31 18:48:04 »

Quote
can I create Sprites on the fly and store them?

Yes you can. You have to read up on textures. What you are after are GLU.gluBuild2DMipmaps(...) or GL11.glTexImage2D(...).

Quote
Even still, would it be at all efficient to be creating a Sprite object very often with its Texture components when all I really want to do is blit a new BufferedImage to the screen?

Not sure I understand, can you explain more. Creating or updating textures are slow, if thats what you are asking.

Quote
I also do a lot of work in terms of drawing with the AffineTransform class.  I assume there are fast counterparts to the transformation work I'd need to do for OpenGL right?

Of course. In opengl there is a matrix stack that contains the current transform.

Quote
Also, what about regular computers without 3D graphics cards?  Will the OpenGL drivers still be able to draw my sprites faster than Java2D can?

LWJGL won't work unless the computer has hardware accelerated opengl. I think JOGL supports software mode, but I don't think it will be faster than Java2D.

You need to read the red book. (There is a link to a pdf and html version at the bottom of the page).

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

Senior Duke




while (true) System.out.println("WOO!!!!");


« Reply #10 - Posted 2004-08-01 02:58:43 »

In the interest of a slow and steady conversion to being able to use LWJGL, I was thinking I'd take it one step at a time, just converting what I needed to, instead of spending half an hour altering code and hoping it would all work at the same time.

So I made a Sprite constructor that would take a BufferedImage and do the same operations to create the Sprite as the regular Sprite constructor that takes a string for the file name.

I kept my game the same, except each loop I rendered my game to a BufferedImage, and I created a Sprite using it, and let LWJGL render that to the screen.  It took about half a second to do every frame.  That wasn't cool.

I know when I have the rest of it working I'll be mostly using pre-made Sprites, but still, I would really like to keep drawing my BufferedImages.  All the menu's are just fine and dandy and really don't have a need for converting.  I could just draw them to a BufferedImage, and have whatever active renderer that is working draw the BufferedImage on top of the game.  The actual game, away from menu's, is where I care about speed and FPS.

Do I have any good options for doing what I'm trying to do?  I couldn't figure out how to use glTexImage2D() and the Red Book didn't help give me any ideas.  It's like me looking up a Spanish word in a Spanish dictionary.  I just get more Spanish words Smiley

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline elias

Senior Duke





« Reply #11 - Posted 2004-08-01 05:59:31 »

Per default, LWJGL refuses to run on software OpenGL, but if you use

1  
 -Dorg.lwjgl.opengl.Window.allowSoftwareOpenGL=true


on the command line, you can override this behaviour. As for your BufferedImage troubles, I can't help you (other than saying "convert it all to OpenGL", but that won't help you).

- elias

Offline Malohkan

Senior Duke




while (true) System.out.println("WOO!!!!");


« Reply #12 - Posted 2004-08-01 15:58:59 »

if I really did just convert all my Graphics2D calls into OpenGL calls, and my game ran on a computer that didn't support OpenGL and had to have it work in software mode, would lwjgl handle that and still make it run as fast as it does now with Java2D?

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline elias

Senior Duke





« Reply #13 - Posted 2004-08-01 16:10:27 »

Quote
if I really did just convert all my Graphics2D calls into OpenGL calls, and my game ran on a computer that didn't support OpenGL and had to have it work in software mode, would lwjgl handle that and still make it run as fast as it does now with Java2D?


I highly doubt it - OpenGL is dog-slow in software mode (at least the standard Windows implementation).

Offline darkprophet

Senior Duke




Go Go Gadget Arms


« Reply #14 - Posted 2004-08-01 16:19:08 »

if your clever enough, you could abstract the rendering bits from your code. That way, you can check if they have an OpenGL able card and use OpenGL, if not, use Java2D.

So you would have the exact same methods in your renderer for both Java2D and OpenGL, e.g. draw(TriMesh t). In java2D, that would be a polygon made up of triangles using the drawPolygon method. in OpenGL you would use a triangleStrip.

Hope that helps, DP

Friends don't let friends make MMORPGs.

Blog | Volatile-Engine
Offline Malohkan

Senior Duke




while (true) System.out.println("WOO!!!!");


« Reply #15 - Posted 2004-08-01 16:35:02 »

true, but one thing I do now is create BufferedImages and hold on to them and only change them when stuff in the menu changes.  This makes the speed MUCH faster when menu's are working (important for key response as well as the game running in the background).  If I take out that optimization, the Java2D version will be very slow.  However, if I leave it, the OpenGL version will be very slow.  Even abstracting the Graphics class wouldn't be able to handle that.

The only way I can think to manage that one would just be a whole bunch of if statements checking the draw method and deciding whether or not to do something.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline Malohkan

Senior Duke




while (true) System.out.println("WOO!!!!");


« Reply #16 - Posted 2004-08-02 12:48:37 »

ok as far as how much of the CPU is involved in rendering, and considering the advantage I have of storing BufferedImages for menu portions so I don't have to re-create them, OpenGL isn't going to be THAT helpful.  A lot of the advantage of my storing them is that I don't have to recalculate a lot of stuff.  In that area of the game it's not all rendering speed, it's a lot of other calculation work.  If I can't store those pieces as BufferedImages and use them efficiently, then I guess OpenGL is out for me.

That's really confusing though... I can recreate a BufferedImage every frame if I wanted to and draw it with unaccelerated translucency and it's still SOOO much faster than creating a Sprite and drawing it with OpenGL.  Why is that?  It doesn't make sense to me that Java2D would be so much faster at just drawing a BufferedImage to the screen.  Is there NO other option for having OpenGL do that?

How about the new Java 5 feature for enabling the OpenGL pipeline for Java2D calls?  Is there any way to use OpenGL drivers to take advantage of that?  I can run AlienFlux just fine because my graphics card supports OpenGL.  However it doesn't support all the exact picky things Java needs to enable the OpenGL pipeline with the System property -Djava.sun.opengl.  It seems like if some extra drivers is all I need to get AlienFlux doing all its crazy 3D things with OpenGL, that I could use them for the same purpose in enabling this pipeline.  Any ideas there?  It seems like I'm coming to the end of my options with OpenGL here and I might just have to toss it and stick with Java2D.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Online princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #17 - Posted 2004-08-02 14:02:10 »

I'm not sure why you need to render anything to a bufferedimage and cache it in the first place?

Under GL, just draw everything - every frame.

Cas Smiley

Offline Malohkan

Senior Duke




while (true) System.out.println("WOO!!!!");


« Reply #18 - Posted 2004-08-02 14:30:51 »

What I was trying to say was that some times deciding what needs to be drawn and how takes a lot of decision making and calculations and then scaling and color saturation before doing a draw.  Even if I'm doing this every frame with OpenGL, the biggest expense here is all of the calculating.  That's why I store a BufferedImage, so that I don't have to redo all of the calculations every frame.  I want to still have this advantage

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Online princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #19 - Posted 2004-08-02 15:03:50 »

"What to draw" and "how to draw it" are two different concepts. Work out what you want to draw once and stash that somewhere. Then when asked to draw it, it should be a simple matter of blitting quads and such. It's a bit like optimising the drawing of a quadtree landscape. The quadtree is what you want to render; whenever the viewpoint changes you work out what to draw and stuff that in an intermediate structure that knows how to draw itself fast. Same here with your menus.

GL even has a few tricks up its sleeve which might even make it easier, such as display lists.

Cas Smiley

Offline Malohkan

Senior Duke




while (true) System.out.println("WOO!!!!");


« Reply #20 - Posted 2004-08-03 13:18:25 »

yuck.  I'm really wanting to be able to switch between the modes.  It sounds like I'm going to have to have a completely separate set of code for my game set aside for the OpenGL implementation.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Online princec

JGO Kernel


Medals: 404
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #21 - Posted 2004-08-03 14:29:11 »

No, that's exactly not what to do!

Try and imagine for a moment that a menu system is really just another bit of the universe you're trying to draw - let's pretend it's a bit of "World State".

You need an intermediary structure then that describes the "Drawable World State" which you work out only when necessary.

Then you need to create an instance of the thing that can draw the drawable world state using whatever renderer you decide upon. You've got two choices: a Java2D renderer and an OpenGL renderer. Largely the way they work will be very similar.

The sequence of operations is thus:

1. When anything changes, use the World State to create a new instance of the Drawable World State (or update an existing one, when you're at the tuning stage)

2. Use the Drawable World State to create an instance of a Renderer that can draw it using the appropriate API.

3. Draw using the Renderer whenever you need to.

Yes, this is complicated. No, it's probably not worth it. You really ought to stick to one rendering API and put all your effort into making it work the best.

Cas Smiley

Offline dsellars

Junior Duke




Need to write more games


« Reply #22 - Posted 2004-08-03 17:42:36 »

I've not much to add technically (sorry).

But you've got a nice looking game there.  Not sure what I was supposed to do though...

I feel using lwjgl should give you a big speed increase though.

Hope you get somewhere with this.

Dan.
Offline Malohkan

Senior Duke




while (true) System.out.println("WOO!!!!");


« Reply #23 - Posted 2004-08-03 19:59:38 »

The game was origionally designed for a phone, so I have a version of the game that can run at like 150x150 resolution and be plenty playable and attractive.  So there's a whole 'nother art set for that.  Also, I have the game running as an Applet game, so that I can expand the size to about 500x500 with the lower (or higher) resolution art to hold on to 60fps easy.  Similarly to having it on phones, I can have it on pda's.  Now I also want the portion that grabs this and sticks it on a Canvas in full screen mode to be able to use the high resolution graphics at 1024x768 resolution easy.

So when I change code in the games, I don't want to have to change that code in more than one place otherwise I'll go out of my mind.

Ideally I could just toss in lwjgl drivers and have the Java 1.5 OpenGL pipeline work, but as far as I've read up on it, that feature doesn't seem to work on much of ANYTHING.

Cas, you tried to explain something abstract... but you got a little too abstract for me.  I lost you there and I'm not sure what your idea was Smiley  With that in mind... your "it's probably not worth it" comment seems to make sense Smiley

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline tentons

Innocent Bystander





« Reply #24 - Posted 2004-08-22 19:19:45 »

Just curoius, but would the changes/additions in Java 1.5 make a difference without learning LWJGL?

Offline Malohkan

Senior Duke




while (true) System.out.println("WOO!!!!");


« Reply #25 - Posted 2004-08-22 22:41:46 »

Well they haven't for me.  My video card claims to support OpenGL of a more recent version than is required, but the Java requirements to enable the OpenGL pipeline for Java2D actually are pretty complicated and my card, a 64 meg 3D graphics card (I think that's good) can't handle it.  Which means.... many cards won't work with it even if they support LWJGL or JOGL.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
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.

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

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

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

lcass (34 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (54 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
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!