Ive been learning to code games in java and libgdx. My most hated part of any language Ive attempted is the Level Editor (.TMX , tiled) parsing. The Github for libgdx says there is a helpful class made for this which Ive been trying to follow the instructions but Im stuck.
Basically if I comment out the following line of code, the app runs fine but of course without any level elements included. When I have this line in code it crashes my phone instantly whilst loading app:
TiledMap map
public Level(OrthographicCamera camera) {
map = new TmxMapLoader().load("level1.tmx"); /// THIS LINE CRASHES IT
}
I have checked the file 'level1.tmx' definitely exists in my 'assets' folder inside the Android folders of the game hierarchy.
If you do need the rest of my code Im happy to put it up but there is lots of garbage there not related to the problem. Thanks for any help
EDIT: One thing i think it may be is the type of file Im making in Tiled Editor. (ie. I chose zlib compression) other options were XML CSV and i think gzip
ANOTHER EDIT -- i managed to view an error message at the bottom of Android Studio saying unable to load file "....tiles-brown.png" -- I was unaware I had to load those textures, the two tiles png files used are also in the assets folder anyway already but it says Couldn't Load on the errors. PLEASE HELP
If your error is "unable to load file "....tiles-brown.png" then go to the folder in windows where you've stored the .tmx map. Right click and open in notepad. There you will see a reference path for each asset used on top of the file. Make sure the asset exists in the specified path in your project. If it doesn't or is different change it to where you store the assets for this map. Then save and refresh the android project. This should solve the problem.
You do not have to load the textures yourself. The loader will load all referenced textures for you, but you will have to put them in your assets folder at the same location which was used when creating the map with TilEd. Since you didn't do that right, you get the exception.
A good way to do this is creating a folder levels and a folder textures in your assets directory. Put your .tmx files in levels and the tilesets in textures. Now when you import a tileset from TilEd, it will be referenced as ../textures/tileset.png. To make sure this is correct, you can check the .tmx file. Do not use absolute file paths!
Related
I am trying to build a game engine from scratch by following various tutorials using Java, LWJGL, and OpenGL. My current plan, is to follow each tutorial, generating the code they teach, until I have working code. Step 2 is to re-organize, rename, document, and otherwise move stuff around until it matches my specific coding style.
Each new subject, is then packaged up nice and neat in its own project, which is then turned into a library .jar file.
That way I can create a new program, add a few specific library .jar files for "2D Text" or "particle effects" to add new features to my game as needed without a ton of copy and pasting between projects, or importing a super large library into a program that isn't going to use all aspects of it.
Now, here is the part I can't figure out. Most OpenGL rendering with the programmable pipeline uses shaders. If I locate the shaders within my new program, everything is fine. I reference the shaders using the "src/Shaders/shaderEntity.vert" file path.
However, If I want everything nice and neat, and reusable, I should locate those shaders within the library .jar file with the rest of the classes for that feature.
How do I reference a shader file, within another .jar file? The library .jar file itself is located in "dist/lib/myJarFile.jar" and would be in the "/Shaders/shaderEntity.vert" location within the .jar file.
Or is this a bad idea and I should continue locating my shaders within the new programs?
A shader doesn't have to be read from a file. OpenGL, or more specifically glLoadShader takes the source code of your shader as a string.
I'm don't know much about .jar files (and have limited experience with the rest of Java) but the goal is to store it in the .jar in some way that you can read it out as a string to pass to OpenGL. That might mean changing your class to take in the source code string and loading that string from the .jar elsewhere, or taking in 2 paths (or one string with a divider, e.g. dist/lib/myJarFile.jar:/Shaders/shaderEntity.vert) that provide the exact path to the file, and the class will then open up the file within the .jar and read from it.
I am a beginner Java developer and I have created an application in Java (Netbeans).
I have used buttons icons, backgrouds for jframes etc. When I built the project, they can easily accessible using WinRAR. Anyone can extract my jar file and see all the images that I have used in my program. Even the images used in jpanel that is not accessible without password.
How can I hide or protect these images?
I have created a .exe file also using Launch4j but still facing same problem. When I right click on the .exe file, its easy to extract my whole program.
To be honest, if someone wants your picture, they are going to be able to get it. One can simply do a print screen. However you can still do somethings.
Remove the extensions for the picture files and give them crazy names - this will make it harder for people to find your pictures
Encript the files then have the application decript them when loading - I don't know how to do this but shouldn't be too hard to find, for instance you could save the pictures as a byte stream with some alterations then reload it.
Save the picture in some archive which only your application knows how to read.
But anyway even with all these things, I still know how one could get a handle to an open JFrame, look through the components, and then could get your picture. IMHO trying to prevent people for getting your pictures is not worth doing.
I'm creating a game with LWJGL, and I want to display the LWJGL logo when the game starts.
For that, I need to load the image from the jar file.
You can see how I set up my resources here: http://imgur.com/HKHLDnM
I have been searching, and it appears that you have to use a '/' in front of the path.
That didn't work for me, also without the '/' in front.
I also tried putting 'jar:' before resources, but it doesn't work either.
Instead of using ResourceLoader.getResourceAsStream() use getClass.getResourceAsStream. This will start the path from where your main class is, so you may have to use .. (it goes to the file above). Sorry if this isn't clear or I misunderstood, but this is what I did to load images in a jar.
I need to load "configuration" type files for my program in Android, they are both .bin files containing dictionary data for the NLP library. I'm a bit new to Android still, and I'm having trouble finding a folder to place the files in so I can access them when the activity starts.
I also need to create/save/load a filetype specific to my program, and I don't know where to put it either. All I've been able to find on here is people using the getAssetManager() function to fetch input streams, but I explicitly need File objects for me to be able to load them into my pre-existing desktop software code I'd like to reuse (plus the libraries require them anyway)
I've also seen people using a "res/raw" folder, however the ADT did not generate this "raw" file when I made the project - so I'm not sure what to do there either.
Here is how I usually start the software in the desktop version, but I need to fetch these files in an Android environment:
brain.start(new File("memboric.core"), new File("en_pos_maxent.bin"), new File("en_sent.bin"));
core = brain.getInterpreter().getCore();
The memboric.core file can be generated, but I need to know WHERE and HOW to do so.
Thank you very much for your time, feel free to direct me to other resources if you feel this question is inadequate.
TLDR; how do I load "static" files for the software to function (needs to be included with software), and how to create/load/save "personal" files into an appropriate area of the device?
Use Context.getFilesDir(). Your application can read and write files in that folder and they'll automatically get deleted if your application gets uninstalled.
From that point forward, you can create, delete and read from files like any other Java application.
the "raw"-folder you can create it on your own. So check this out, which shows how to handle files in Android: http://developer.android.com/training/basics/data-storage/files.html
I'm new to Java. I'm simply trying to build a .jar file of my applet so I can run it from my browser. This is what my directory structure looks like:
C:\java\pacman\src
contains all of the .java class files.
C:\java\pacman\assets
contains about 4-5 images and audio files.
If I try to use the following code:
Image someFile=getCodeBase().toString() + "file.png";
The result of getCodeBase() is
file:/C:/java/pacman/bin/
However the following code fails to load:
img=new ImgHelper(getCodeBase().toString() + "assets/");
ImageIO.read(new File(img.getPath("pacman.png")));
Moving my 'assets' folder to the 'bin' folder didn't fix this either. It tries loading:
file:/C:/java/pacman/bin/assets/pacman.png
saying:
Can't read input file!
But the url it gave opens fine if I paste it into run and hit enter:
So to avoid myself a lot of headache i commented out the code in my ImgHelper class and did this:
public ImgHelper(String dir)
{
//this.imgDir=dir;
imgDir="C:\\java\\pacman\\assets\\";
}
Which works perfectly. But I want to put this on a web server, and I have no idea how/what I should do to make all the images and sounds work. Any ideas?
Thanks...
Why not put it all in a JAR file and then call Class.getResourceAsStream?
A JAR file is better as it is a single HTTP connection rather than one HTTP connection per file. It is also much more flexible to use a Stream than a File.
getResourceAsStream will work when the files are not in a JAR as well, they need to be relative to the class file.
EDIT:
Another thing, the File method won't work if the applet is on a server as it will be trying to open the file from the local machine (I think, I haven't tried it) rather then from the server. Even if it tried to create a file path to the server that won't work.
I agree with tofubeer about the JAR, but if you want to put the image on your server, see the tutorial on Applet images here. The codebase will be whatever location your applet is on the server, and you can put images relative to that on the server as well. Use a media tracker along with the Applet.getImage() method to retrive the url. From the example:
my_gif = getImage(getDocumentBase(),"imageExample.gif");
There are two possible solutions that would work:
The images could be present outside the applet JAR. The applet could then be initialized with the location of the directory where the images are present. Once you have that information you could then load images from the server. The Sun Java tutorial provides an example usage of the applet parameter to pass the image source directory.
The applet class loader could be utilized to load the images from the applet's JAR, using the getResourceAsStream() method.
PS: It would be helpful if you referred to the section in the Java tutorials to load icons for your application. The same section discusses a lot of the points brought forth by TofuBeer and John.
EDIT : The usage of the File API is not recommended because it ends up reading off the local file system. That is unacceptable for most users on the internet.