Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  JGO Frequently Asked Questions  (Read 2088 times)
0 Members and 1 Guest are viewing this topic.
Offline sproingie

JGO Kernel


Medals: 202



« Posted 2012-04-25 02:27:38 »

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
General Java Development
Q: How do I load images/sounds/textures/configs from the classpath?

Use the getResource() method to return a URL that can be opened later, or getResourceAsStream() to open the resource directly as an InputStream.  There are several different objects where this method can be used, and which you should use depends on many subtle aspects of java classloading, but for 99% of cases there are two approaches, depending on whether you want an absolute or relative resource path:

1  
2  
3  
4  
5  
6  
// APPROACH 1: To load from the root of jars in your classpath
Thread.currentThread().getContextClassLoader().getResourceAsStream("images/backgrounds/sky.jpg");

// APPROACH 2: To load resources relative to the package of this class (the class of 'this')
// e.g. if your class is org.stuff.Game, this will look for /org/stuff/config/settings.xml
getClass().getResourceAsStream("config/settings.xml");
    Notice that neither path uses a leading "/", such as "/config/settings.xml".  Using a leading slash in the first example simply won't work much of the time, while it will cause the second example to behave similarly to the first (searching from the root).  Finally, there are environments such as OSGi where
neither of these approaches is correct, so the best advice is to use a utility class to hide all this behind, so you can change it out if and when you need to.

Q: How do I access the user's home directory

On the surface, it's pretty simple:
System.getProperty("user.home")
.  Except it does completely the wrong thing on Windows, so you can fall back on using System.getEnv() and reading the USERPROFILE variable on Windows or HOME on Unix.  You don't usually even have to detect the OS if you just look for those variables and bypass the problematic user.home property altogether.  

The real problem is, what are you actually trying to do?  If you're trying to read or save settings, then read on to see just how deep the rabbit hole goes: First off, seriously consider using the Preferences API for application settings -- it takes care of all this stuff for you.   If that's not an option for whatever reason, then your problem becomes figuring out the appropriate directory to use.  Most users don't really mind all that much when you drop folders into their home directory, and a lot of applications do so, whether Unix or Windows, but if you really want to be a proper citizen, that's where things get really hairy.

  • Windows: The environment variable you're looking is APPDATA for application prefs, but LOCALAPPDATA for stuff you don't want synchronized on roaming profiles.  Throw a directory under there and it's good enough for government work ... but the frightening whole story can be seen in this SO thread.

  • Linux and other Unixoids: You think preferences live in $HOME?  The freedesktop.org people have different news for you with the XDG Base Directory Spec.  It not only splits app data into "config" and "data" dirs ($XDG_CONFIG_HOME and $XDG_DATA_HOME) but adds to the "fun" by adding path search environment variables for each!  Short story: use a hidden folder in $HOME, or use the Preferences API.

  • OSX: TODO -- I'm going to need someone to fill this in.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Game Development
Q: How do run I my game at a constant framerate?
It's not anything special: after every frame you draw, simply call a method that measures the time between now and the last frame you drew, and pause for however long it takes to make up the difference.  How you do this pause can be as simple as a single Thread.sleep or as complex as multiple sleeps and yields, depending on how precise you need to be.  Obviously you can only slow down your game this way to reach the desired framerate.  If you need constant-rate motion in your game, you will still need to adjust for the actual time each frame is taking, not just the ideal frame rate.

As with any utility, it's best to use an existing utility class that accounts for things like timer inaccuracy on different platforms and adapt it to your needs.  Some implementations of this:


Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2012-04-25 03:11:45 »

This post is to collect draft questions that don't have answers yet.   Feel free to reply if you want to answer one or submit a question (remember, it should be actually frequently asked here on JGO, not just be a "good question")

Q: What's an Entity System?  Should I use one?
A: (blah blah various definitions yadda yadda link to articles and implementations.  composition is good, entity systems are a maybe, but have lots of tradeoffs)

Q: I'm new to learning Java.  Where do I start?
A: (links to tutorials, books, etc, use mid-level toolkit like libgdx.  read javadoc, ctrl-click stuff)

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 757
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2012-04-25 03:25:37 »

Q: How to load resources from a JAR, relative to classes, relative to their classloaders and relative to the current thread's context classloader?
A: Knock yourself out. (contributions appreciated)

Most of the time, it's best to load resources from the current thread's context class loader:
1  
Thread.currentThread().getContextClassLoader().getResourceAsStream("path/relative/to/roots/of/jars.jpg")

... preferably implemented as a utility method.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #3 - Posted 2012-04-25 04:35:44 »

I used the thread context classloader answer you gave, but I'm kind of wondering about it:  is the context classloader even relevant outside of JavaEE?  I'm pretty sure it doesn't work properly in OSGi, and you have to use the current classloader instead, though I'm not going to sweat it since I doubt anyone's going to be using OSGi anyway...
Offline MrDodo

Senior Newbie





« Reply #4 - Posted 2012-04-25 08:20:07 »

thanks for this sproingie.
I cannot help with the FAQ because I will probably be target audience for it, but I can say I will read it and appreciate very much your efforts!

thanks everybody!

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 757
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2012-04-25 15:52:43 »

I used the thread context classloader answer you gave, but I'm kind of wondering about it:  is the context classloader even relevant outside of JavaEE?  I'm pretty sure it doesn't work properly in OSGi, and you have to use the current classloader instead, though I'm not going to sweat it since I doubt anyone's going to be using OSGi anyway...

According to kevglass it's the only reliable way to access resources within the sandbox (applets, webstart). In an environment where no security manager is active, it's not really any better than your average classloader, but that's no reason not to use it in the general case.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
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.

CogWheelz (14 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (15 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

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
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!