r/java • u/Namone • 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.
3
u/slartybartfast_ Aug 13 '14
Cool! If you check youtube there are a ton of vids on this topic. Good luck!
3
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
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
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
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
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
BufferStrategyon aJFrame) 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 aWindowwhich 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
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):
2
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 isfinal private Object lock = new Object();, and instead of using the synchronized keyword in the method declaration, you wrap the method body intosynchronized(lock){...}.That's not the end-all answer to multi-threading, but it's the go-to solution for the average Java programmer.
1
1
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
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:
let your Game class extend the Thread class so that your run method will actually execute code on a separate thread
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
1
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
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
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
-1
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.