So some of you noticed that I am trying to find my way around game development.

I first tried to comprehend game loops deeply enough to write my own. The same goes for the "best" way of active rendering (If there is one).

Like anyone who reads this, you guess that I keep failing and am getting sick and tired of that.

So first of all I wanted to ask a question; How much Java 2D should we know? I did not plan to ask this, but I saw some libgdx tutorials and image drawing was nothing like Java 2D. The other reason of this question is that I am getting tired of Java 2D, not because it's slow etc., I still get very frustrating errors

Anyway, I have a code snippet to be used as a skeleton (or let's say a template) for my practices. I figured that if I keep staying on these two topics and don't go further, I'd get bored. So for now I am going to use this to make something 'visible' and I am hoping to learn and understand why there are so many methods to do these things.

But I have some questions regarding the code snippet. It is not written by me. I guess I took it from Killer Game Programming in Java book. It uses active rendering and has a basic game loop.

The first two question is about active rendering. In the book, he does that like this (Comments are written by me to understand what's going on):

Why did he use two methods to do the drawing? Is this correct? And why do we dispose the g Graphics object?

Second is about game loop. In the book he says this one might cause problems, but this is as good as I can understand, nothing further. But still there is something...There is a "period" variable (actually I don't know what it is) and he never defined it anywhere. But he explains it before the code:

Quote

A popular measure of how fast an animation progresses is frames per second (FPS). For GamePanel, a frame corresponds to a single pass through the update-render-sleep loop inside run(). Therefore, the desired 100 FPS imply that each iteration of the loop should take 1000/100 == 10 ms. This iteration time is stored in the periodvariable inGamePanel.

I could not understand what he means. Can anyone please explain it? And how does he plan to use that?

And here is the full code (Not the finished one - he says it is to be improved, but I just couldn't understand, so that is it )

First of all: I think that you are doing the right thing, the way you try to learn everything and understand it as deeply as possible. Doing it like that you will be able to find your way through all this on your own very soon. It always takes a little while, but as you go further in learning you will get a better overview of what is going on. So, you are on the right path.

Now to your questions:

Quote

How much Java 2D should we know? I did not plan to ask this, but I saw some libgdx tutorials and image drawing was nothing like Java 2D.

That's right, libgdx uses OpenGL. With OpenGL, 2D graphics work very much different than Java2D. I mean, it is not from outer space and works with buffers full of pixel color and alpha definitions and all this, but you handle it all in a very different way. With OpenGL you can not like in Java2D simply say: "Hey, let's put an Image here!". It's more like this:"Hmm, I'll set it up like this with all the right states and the ortho-perspective set up, then I have to load the image file and make a texture out of it. After that I need to create a quad out of four vertices to have a primitive to stick the texture on. This will make it possible to see it, if I have defined the vertices in the correct order or culling is off. And if the texture coordinates are alright, too."This should explain it the way I have at first experienced it. When I went from Java2D to OpenGL I thought: "Will I ever get this thing working?" Eventually I did. But it took some time to learn. It was worth it.

The good part is: you could try to put all this OpenGL stuff in a shell and create an interface that works pretty much like Java2D if you want that. Or you could just use some library that does it for you.

Quote

The other reason of this question is that I am getting tired of Java 2D, not because it's slow etc., I still get very frustrating errors

Yeah, it is not easy. I had the same problems. Sometimes I asked myself: if I am not able to manage this, how will I ever be able to do some of the really complex things? After experiencing this, I would say: take it slow, learn from your mistakes and errors. There can be some very nasty errors but you shouldn't give up. Even though you maybe won't need Java2D later, it is a good place to start (not everyone would agree).

Quote

I figured that if I keep staying on these two topics and don't go further, I'd get bored.

If that's the case, maybe you should try to go to more advanced topics or more fun ones. If you have problems motivating yourself to learn thoroughly, you will soon be stuck somewhere. This is mostly meant to be fun, so jump to any topic you think can handle (or handle to learn). And if you run into problems and think you need to take a step back, maybe it gives you some new motivation to learn the tough parts.

Quote

Why did he use two methods to do the drawing? Is this correct?And why do we dispose the g Graphics object?

You mean gameRender and paintScreen?GameRender is there to render your visible game objects on your backBuffer. You know, doubleBuffering. After that the Image will be copied to the panel. It is faster copying a finished image than drawing many objects (with calculations between) directly on the panel. This is why you do that.

The reason for disposing the graphics object

1

g.dispose(); //Why do we dispose that?

is surely because of the JVM problem the book has mentioned. If you always tell your image to create a new graphics context every frame and don't dispose it, then you will end up with a lot of useless garbage in your memory, I think. For example BufferedImage will call createGraphics() when you call getGraphics(). So I think it literally always creates a new context. Disposing might be a good idea then.

Quote

There is a "period" variable (actually I don't know what it is) and he never defined it anywhere.

You know that you need a value to specify your desired frames per second. So if you want 60 fps, this means that every second your engine has to generate 60 images to display on the screen. This gives your engine about 16,67 milliseconds for every frame to update and render. And exactly THAT value is the period. The amount of time you give your gameloop to calculate a frame. (and sleep when work is done before time is up)

ActiveRendering in the context of Java2D means that you won't rely on the JVM to do the things when you want them to be done, btw. You just say: I will take care of this, instead of sticking it into the queue to be managed by the JVM.

Bored? Ok. Make a particle engine. Instant visual feedback and it is what will make your game look super pro

Stuff in java2d is very similar to libgdx. Java2D does all the opengl stuff for you like when you do ImageIO.blablalbah and load an image, it creates the texture. When you say g.drawImage(blah) it binds the image, and renders it to a quad.

Libgdx is a little tough to get loading images if you have never down anything before but rendering is as easy as java2D.

Instead of g.draw it is batch.draw. You still call batch.begin and end much like you call to get the graphics and dispose of it. Big difference is that libgdx is 10x faster and lets you do more advanced thing as you learn. Try getting additive blending in java2D or even tinting images on the fly. Not going to happen. I think that if you are going to spend the time getting into all the tech for game making do NOT do it in java2D as you are learning things that become rather useless later on. At least the code is. If you really want to learn fun stuff, dive into opengl. Get a image to load. Put it on a quad. Put it on a triangle strip. Look into batching.

I got a rather fast java2D image going and it worked very well, but due to the horrible inconsistency and how hard it is to do anything more then sprite rendering I dropped it.

Right now you are going along the rendering a little wrong. Look into Bufferstrategy to do proper double buffering. You say you do calc after rendering? Should be update() render() update() render() etc. FPS is how many frames a sec. The 1000/60 is because you pass in the sleep time in milisec to the Thread.sleep(). 1000 milliseconds is 1 second. Java has horrid sleep accuracy so search up ways to get an actuate sleep time. (libgdx and opengl will do this for you)

I know this is said alot but stay away from java2D. I know there are a whole lot of tutorials for it out there but unless you are doing 4k, you are shooting yourself in the foot with it. If you are scared of opengl, try slick2D very much like java2D but not as hard to load images as libgdx.

Sorry for the long post but this basically will setup a window with everything being properly draw. Here is the unstripped version.http://pastebin.java-gaming.org/793779a8b43It basically abstracts all rendering junk away from you. If you look it is like opengl. Clear the screen, drawstuff, show buffer and swap to another. As long as all rendering is called between clearScrean() and render(), you are fine. This is basically how libgdx is but with opengl speed.

I know this is said alot but stay away from java2D. I know there are a whole lot of tutorials for it out there but unless you are doing 4k, you are shooting yourself in the foot with it.

I wouldn't say it this way. It is true that Java2D isn't of much use after switching to OpenGL. But learning game loops with Java2D is ok and you get a good idea of what working with images is like. And my problem always was, that I wanted to do the things myself because I wanted to learn. Java2D helped me a lot to get a good start. So it won't hurt to start with it and your feet will stay healthy, mine are.And of course, I benefit from Java2D because it comes in handy when programming applications for my computer science course.

Let me say that I like the community here. People actually explain stuff instead of saying "You're wrong" and dumping the right code without explanation.

@Sparky83Considering doing the right thing, I have decided to learn game loop as much as I can and leave Java 2D after figuring out basic things.Let's be honest, even if you won't ever release a game, you'd want to learn stuff like libgdx instead of settling with Java2D.

So when I cover the basic Java 2D, I'll try to get my hands on libgdx. But it might take veeeeery long.

And about getting bored, I am not bored yet, but close to it. So while I am trying to learn these, I'll use a good template to experiment some fun stuff.

About using two methods to implement double buffering, I actually wanted to ask that could he not do that in one method? Or is he just 'simplifying' things?

Okay, I am about to pass another threshold (Does this sound normal in English?) about game loops. I guess I did not know how to calculate period until now.So, is this the math behind FPS and the period in my code?

1 second/period(from my code example) = Desired FPSSo, it is actually ==> period = 1 sec/Desired FPSBecause 1 sec = 1000 ms it translates to;period = 1000ms/Desired FPSAnd if we wanted to calculate it in nanosecond;period = 1000000ns/Desired FPSHence, period = time in which a single frame should be drawn

@StumpyStrustHaha, I am not bored yet, but I am about to, if I cannot figure out these stuff. I am kinda obsessed.I mean, dammit my old school uses my program to keep student information locally (Government gave app to do it online) and they have not complaint once (Maybe it might be working a little slow ) but I cannot draw some images and move them?

When I add your comment on top of Sparky83's, I feel like I should just stop with Java 2D. But for the sake of learning mechanism and design of a simple game (hence at least dreaming of a complex one), I think I should stick with Java2D a little more. So, I'll look into BufferStrategy. Actually, expect a question soon

I did not look at any OpenGL codes, but I read a tutorial for libgdx (Actually just glanced at the code). It looked fun and I guess it's based on OpenGL. So, maybe because I haven't tried anything yet, but it does not look scary for now.

I see Sparky83 posted another reply And I think it supports my decision about Java2D

Let me say that I like the community here. People actually explain stuff instead of saying "You're wrong" and dumping the right code without explanation.

It is great, isn't it. I like this site too.

Quote

Let's be honest, even if you won't ever release a game, you'd want to learn stuff like libgdx instead of settling with Java2D.

That is totally true.

Quote

Okay, I am about to pass another threshold (Does this sound normal in English?) about game loops. I guess I did not know how to calculate period until now.So, is this the math behind FPS and the period in my code?

What would be your native language btw? Just curious.It seems you know the trick with period now. But there is one mistake:

Quote

And if we wanted to calculate it in nanosecond;period = 1000000ns/Desired FPS

Thanks Vermeer. I'll look into it. Videos seem like they do not teach but make me memorize stuff, so not my first choice. But I'd be lying if I told I never used videos to learn something.

On the other hand, ClickerMonkey has a nice tutorial (http://www.gameprogblog.com/generic-game-loop/) he linked in the second post. I think I will have to translate into Turkish first, then try to read once again. It feels like this is one of the difficult stuff you should read in your own language. I'm gonna PM him if he is okay with it. Not planning to post it somewhere since I do not have a blog or website but still

And is render.setFocusable(false) needed? Is it something you do to prevent canvas getting events and to let JPanel (Or any other class) worry about events?

@Sparky83I didn't see your last post, I am sorry. I am from Turkey

That period variable (actually it turns out to be a constant, if I am trying to keep FPS same?) still bugs me. Now, since I read tons of tutorials but only got a little from them, I remember vaguely that I should pass it to the game updating methods, and use it with any time dependent jobs. Like moving an object (locationX += (velocityX + period) maybe?). But why?Or was it completely something else?

Considering doing the right thing, I have decided to learn game loop as much as I can and leave Java 2D after figuring out basic things.

What's the point in learning a game loop and sync? As long as you understand the basic concept -- your render() method is being called N times per second, how to use the update(delta) method, etc -- then you will be fine. There is no need to waste your time struggling with BufferStrategy, GraphicsConfiguration, Thread.yield, etc.

The best use of your time is to skip "how to write the perfect game loop" and instead start learning how to develop games. For a beginner, using a library like LibGDX is generally a better idea than using LWJGL.

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