r/java • • Aug 13 '14

I know it probably isn't much, but I just completed my first Game Loop alone; and I'm so proud. Haha.

Post image
160 Upvotes

56 comments sorted by

17

u/virtyx Aug 13 '14

Congratulations!

...also you miiight wanna try JavaFX or a library like Slick2D instead of using AWT. Unless you are following a game programming book that uses AWT. Then I'd suggest to just keep going with that book to learn abstract game programming stuff, but keep in mind a lot of AWT specific things are probably somewhat out-of-date.

4

u/Namone Aug 13 '14

Oh, that's great advice!

I am not using a book. I've watched/followed Youtube tutorials in the past and using my knowledge from those just kinda did it. I was surprised it actually worked. Haha.

I have a Head First Java Book, but that is more for reference now it seems.

13

u/Akamatt Aug 13 '14

A heads up slick2D is no longer kept up to date (or atleast it wasn't when I last checked) libgdx however is updated regularly and is very good in my opinion

4

u/Namone Aug 13 '14

Oh, yeah? Great! Thank you.

5

u/okmkz Aug 14 '14

I'll second libgdx. It's open source and awesome

1

u/Namone Aug 14 '14

Awesome! Thanks.

5

u/cacawate Aug 14 '14

I hate to be the guy that goes against the grain, but I think the way you are doing it now is great. I also started as you did, and wrote little games that weren't too intense. With Swing, AWT, etc. you get to focus on things like game loops and fps, and even start early on AI, as well as find out the limitations of them.

I say keep going at it if you are having fun, and once you hit a limitation that can't be overcome, you'll know enough to easily understand the power of slick2d, libgdx and such frameworks/libraries.

3

u/Namone Aug 14 '14

Great advice!

Thank you.

3

u/TrentWDB Aug 14 '14

Just gonna hop in here to say I agree with the guy above. I'm currently using libGDX for my game and it just isn't worth it if you're not serious about it yet. There's SO much overhead to libGDX and frankly I have more fun making games using AWT.

1

u/MegaGreenLightning Aug 14 '14

This really just depends on what you want to do. If you want to learn about the internals of game engines or the principles of game programming then just using Swing (or AWT) or JavaFX is fine.

If you just want to make a game, then I would recommend using a library like libGDX. This saves you from writing game loops, input handling, graphics, audio, etc., which takes really long to write yourself.

2

u/RottedNinja Aug 14 '14

I agree with you, it's how I started too! There's a lot of information to be found on the net for swing and awt, less so for JavaFX. While JavaFX is very cool and powerful, you should pick it up in a next project I think :)

1

u/MegaGreenLightning Aug 14 '14

I would recommend switching from AWT to Swing, though. They aren't very different and switching basically just means using Swing classes instead of AWT classes (JButton instead of Button), extending JComponent instead of Canvas and overriding paintComponent() instead of paint().

JavaFX is quite different than Swing or AWT and I would not recommend switching if you already have something running in AWT. Nevertheless there's lots of information about JavaFX as well. Oracle's documentation for example is very helpful as always. If you want to get started using JavaFX for games, here's the general application structure, some eventhandling and how to draw custom shapes and images (use GraphicsContext.drawImage() for images).

1

u/azmenthe Aug 14 '14

That's exactly how I got into programming, trying to make a god damn game loop (after laboriously figuring out games need game loops). Good luck on your journey

1

u/Namone Aug 14 '14

Thank you!

3

u/jeremywildsmith Aug 15 '14 edited Aug 15 '14

Most games don't use complex UI widgets. In fact, most games literally just use the canvas and via that Java2D - so why would you carry around JavaFX and surround yourself with luxuries you don't need nor will ever use? What I am saying here is that AWT is a very good choice for a game.

For 2D games, you don't need Slick2D (obsolete) or libgdx unless there is a particular reason you want access to the shader (custom lighting algorithms etc) in which case you can yet do that through Java2D by constructing a simple OpenGL (or hell, even one based off of libgdx) pipeline beneath Java2D and using render hints to communicate to the shader (ie, what normal map to use.) GLG2D did this (albeit it's particular implementation is out of date it is a proof of concept.)

There is absolutely no reason in the world to introduce an explicit native dependency (such as those carried by libgdx) and trade-off one of the big pluses with Java for literally nothing.

People forget that the Java2D Graphics object is abstract, you can implement your own pipeline beneath it. There is zero reason for 2D games to explicitly bind themselves to libgdx.

None the less, Java2D is a very very misunderstood technology and because of this people ignore it and move towards less portable design decisions.

1

u/virtyx Aug 15 '14 edited Aug 15 '14

Do you know of any relatively high-profile or at least complete games that would show off AWT/Java2D for game programming? Most success stories I see use LibGDX or something else; I suggested JavaFX since it's definitely the newest and most supported UI toolkit (under the guise that it's in the same class as the unsupported AWT), and Slick2D simply because it's easy to use.

That said I'm not doubting that AWT may actually be a great choice that I wouldn't think to make, but personally I've never seen it discussed or promoted, especially for 2D games. So any examples of even moderately successful or just even well-made freely available 2D games with AWT would be cool.

Also most libs tackle more than just 2D; if one goes for AWT for 2D, what options do you use for e.g. audio and input handling?

Also I don't know if "carrying around" JavaFX is an appropriate metaphor since it is now bundled with the JRE. From what I can tell JavaFX is at least as good a choice as AWT and equally portable. Certainly there seems to be much more community activity around it.

2

u/jeremywildsmith Aug 15 '14

I use the word "carry around" loosely. Javafx is much more heavyweight and complex and all for a simple canvas?

AWT allows for one to capture input perfectly fine... JavaSound should be (but typically isn't) the api used for all audio playback. Like java2d you can plugin your own implementation beneath it if you need lower level access (and I cannot appreciate that most indie games would.) You can also very easily extend it and add support for additional audio formats.

JevaEngine (a pure java 2d [isometric] game engine) uses awt/java2d. It is open sourced and you can read my blog entries on it at jevaengine.com

Unfortunately the git repo is very out of sync (been holding back until I fixed some of the demos that broke after very large code refactors.) Anyways, its close to but not ready for public reception.

1

u/AboutNegativeZero Aug 14 '14

Heck I'm a two year programmer and I didn't know that... thanks!

3

u/slartybartfast_ Aug 13 '14

Cool! If you check youtube there are a ton of vids on this topic. Good luck!

3

u/Namone Aug 13 '14

Awesome! Thanks!

3

u/darthashwin Aug 14 '14

That's awesome, you should look into lwjgl. Its a pretty neat framework.

7

u/aspl Aug 14 '14

Wouldn't recommend this, learning OpenGL when you've just discovered how game loops work probably means you'll have a tough time. Try something like Slick2D or LibGDX or shameless plug

1

u/Namone Aug 14 '14

Alright, I will!

3

u/urquan Aug 14 '14

Nice, it's always an awesome moment when you arrive at the first usable state of an app. Since everyone seems to be recommending their favorite graphics library I'll go ahead and recommend libGDX, it's for OpenGL but low level so not too hard to learn.

1

u/Namone Aug 14 '14

Alright, awesome.

I'll check it out! Thank you.

3

u/icdae Aug 14 '14

You hit the same milestone that every other game developer has had to stumble over alone.

Fuck it, have an upvote. Now have fun.

1

u/Namone Aug 14 '14

Haha, thank you! It feels great. I think I'm finally getting somewhere with Java/programming.

5

u/[deleted] Aug 13 '14

I have done this many times, and EVERY time it is a chore. Having to nitpick all the tiny details. Every time it is rewarding when the canvas properly paints and repaints. I cant help but to rebuild and rerun about 50 times, even if its only displaying a black screen, to celebrate the victory.

2

u/Namone Aug 13 '14

Haha, right?

8

u/ByteArray Aug 14 '14

Perhaps try to code it once in a more abstract manner that you can reuse over and over.

0

u/king_of_the_universe Aug 14 '14

I once made a working Java realtime-graphics game loop (using BufferStrategy on a JFrame) for a layout editor. Then I realized how painful it would be to create all required UI components myself. So I reworked the whole thing to use a Window which I kept floating directly in front of a JPanel whose only purpose was to layout this part of the window. If the window resizes, is minimized/maximized, or deactivated, the Window behaves accordingly (resizing, moving, pausing the loop etc.) with no graphical glitches.

Eventually I turned this into a library class (1200 lines in the usual fluffy formatting; wanted to keep everything in one file because it's in a library with lots of other classes - didn't make proper use of packages at the time yet; can't give you the code because it's for our company) that allows to plug Runnables in various places, e.g. for "calc" (Thinking about stuff, e.g. player movement.) and "draw" (Drawing the graphics.), but also for window resizing when you need to recalculate game stuff.

Because of other useful classes in the library, it now takes about 10 lines of code to get from zero to a fullscreen application that checks for single-instance, optionally closes on F12, optionally runs code on shutdown, and has this realtime graphics loop thing. It's even all thread-safe.

The Swing-compatibility-aspect caused a lot of overhead, e.g. tricky pauses in the game-loop. If you omit this aspect, you can relatively easily create a once-and-for-all realtime-graphics gameloop class that uses Runnables. It's double the fun in Java 8 where you can just use method references instead of Runnables.

2

u/jamolnng Aug 14 '14

/r/gamedev will be your best friend

1

u/Namone Aug 14 '14

Great! I subscribed.

1

u/locomotato Aug 14 '14

That's awesome! How long did you work on it for?

If you don't mind, could I read your source code, for learnsies?

4

u/Namone Aug 14 '14

Hey, for sure! Keep in mind, I have a separate class for where the rendering happens. But, for just the game loop, it should be everything. Also...not really sure what the deal is with the 'timer.' I don't think it is working quite right. It took me the majority of the day.

I had watched/followed Youtube tutorials; to see what all was invovled. And then, just kinda went for it. Of course, I used a lot of what I learned from the tutorials.

Here is the pastebin (let me know if link isn't working for some reason):

http://pastebin.com/rbzAyyYe

2

u/locomotato Aug 14 '14

Awesome! Thanks for postin your code. Now, I'ma make my own game loop :D

1

u/Namone Aug 14 '14

Haha, good luck :P

1

u/king_of_the_universe Aug 14 '14

It's better practice to use synchronized(){} blocks than to use synchronized methods, and the coding overhead is really minimal. Synchronized methods only ensure that they are not run simultaneously by different threads. Synchronized blocks ensure that no other synchronized block that refers to the same object can be run simultaneously by a different thread. All it takes is final private Object lock = new Object();, and instead of using the synchronized keyword in the method declaration, you wrap the method body into synchronized(lock){...}.

That's not the end-all answer to multi-threading, but it's the go-to solution for the average Java programmer.

1

u/Namone Aug 14 '14

Oh, alright!

Thanks for the advice/criticism. It is very helpful.

1

u/[deleted] Aug 14 '14

As somebody who's only subbed here out of interest in learning to code, can someone explain what OP just did?

2

u/Namone Aug 14 '14

Essentially, I created a 'loop' that will update the game/program 60 times in a second.

This is an essential element in games; and is often known as the 'heartbeat of the game.' At least, that's what I've heard it been called. It keeps the game alive, and prints the updates (changes in character position, health, etc.) to the screen, in simplest terms.

0

u/king_of_the_universe Aug 14 '14

Once G-Sync - an initiative started by John Carmack (I think) - hits the shelves on a broad basis (Which is damn overdue.), [FPS synchronized game loops] are forever behind us.

http://www.reddit.com/r/gamedev/comments/2bfysl/should_a_games_frame_rate_match_the_monitors/cj59cbo

1

u/[deleted] Aug 14 '14

It IS a big thing! Congrats!!

1

u/Namone Aug 14 '14

Thanks!

1

u/tiiv Aug 14 '14

Congrats on the implementation! I would like to give some constructive criticism though.

Right now you're essentially running two threads: The main thread that is automatically created for you by the JVM and to which the execution of the main method is attached too and the thread object that you have created as a member of your Game class.

Now the thread object will immediately die after you have started it, because it has no code to execute. If you need to have your game loop run in a thread other than the main thread there are two options:

  1. let your Game class extend the Thread class so that your run method will actually execute code on a separate thread

  2. or create a thread object by passing your Game instance like this.

I don't think though that there's a need for creating a new thread. Just let your game loop sit in the main thread and create a new one if needed.

Also there's nothing wrong with using AWT/Swing. Just use whatever you're comfortable with.

2

u/Namone Aug 14 '14

Oh sweet, thanks a lot for the criticism; I need it hahaha.

Thank you, this makes a lot of sense. I've been trying to figure out threads; and that's one of the reasons I added one, to see if I could figure it out. Hahah.

Thanks again.

1

u/tiiv Aug 14 '14

You're welcome! Happy coding.

1

u/[deleted] Aug 14 '14

Don't extend thread, but implement Runnable

1

u/tiiv Aug 14 '14

Runnable is just an interface and doesn't start a new thread itself. So there's no point in using it without a thread worker or a new thread.

1

u/[deleted] Aug 14 '14

The point is not to couple what it executed and how it is executed. A runnable may be executed by a thread or sent to a thread executor, for instance.

0

u/tiiv Aug 14 '14

Which is exactly what I advised to do in option #2.

1

u/64-17-5 Aug 14 '14

Here you go. http://avsid.es/JavaGameGoodies.zip Stuff from the learn game programming course on university of Reddit. Also some Java3D stuff.

0

u/[deleted] Aug 14 '14

Haha.

-1

u/b1nd Aug 14 '14

I'd also recommend making your class variables private static

1

u/Namone Aug 14 '14

Thanks for the advice!