I'm currently reading a book on LibGDX Game Development - Learning LigbGDX Game Development.
In the book, we use TexturePacker2 to generate a .png and .pack file to pack a half dozen textures into a texture atlas. I followed the code exactly, but it does not generate anything into the destination folder.
Here is the code in my desktop project folder:
package com.packtpub.libgdx.canyonbunny;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.tools.imagepacker.TexturePacker2;
import com.badlogic.gdx.tools.imagepacker.TexturePacker2.Settings;
public class Main {
private static boolean rebuildAtlas = true;
private static boolean drawDebugOutline = false;
public static void main (String[] args) {
if (rebuildAtlas) {
Settings settings = new Settings();
settings.maxWidth = 1024;
settings.maxHeight = 1024;
settings.debug = drawDebugOutline;
TexturePacker2.process(settings, "assets-raw/images", "../CanyonBunny-android/assets/images", "canyonbunny.pack");
TexturePacker2.process(settings, "assets-raw/images-ui", "../CanyonBunny-android/assets/images", "canyonbunny-ui.pack");
rebuildAtlas = false;
}
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "CanyonBunny";
cfg.useGL20 = false;
cfg.width = 800;
cfg.height = 480;
new LwjglApplication(new CanyonBunnyMain(), cfg);
}
}
But when I run this, nothing is generated into my CanyonBunny-android/assets/images folder. The only way I can get my textures to load is to download the sources for the book and put the already generated .png and .pack folder in the correct folder. I followed the book exactly, so I'm not sure what the issue is. I get the logging message that it is generating a 1024x1024 texture atlas, so the code is being executed. Is there something I'm missing?
Here is what the console says when I start the application(with the already-generated files)
Packing.........
Writing 1024x1024: ../CanyonBunny-android/assets/images/canyonbunny.png
Packing......
Writing 1024x1024: ../CanyonBunny-android/assets/images/canyonbunny-ui.png
game.Assets: # of assets loaded: 2
game.Assets: asset: images/canyonbunny.pack
game.Assets: asset: images/canyonbunny.png
Without the already-generated files:
Packing.........
Writing 1024x1024: ../CanyonBunny-android/assets/images/canyonbunny.png
Packing......
Writing 1024x1024: ../CanyonBunny-android/assets/images/canyonbunny-ui.png
game.Assets: Couldn't load asset 'images/canyonbunny.pack'
com.badlogic.gdx.utils.GdxRuntimeException: File not found: images/canyonbunny.pack (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:127)
at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:97)
at com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:59)
at com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:34)
at com.badlogic.gdx.assets.AssetLoadingTask.handleSyncLoader(AssetLoadingTask.java:103)
at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:92)
at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:399)
at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:314)
at com.badlogic.gdx.assets.AssetManager.finishLoading(AssetManager.java:337)
at game.Assets.init(Assets.java:38)
at com.packtpub.libgdx.canyonbunny.CanyonBunnyMain.create(CanyonBunnyMain.java:17)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:125)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:108)
game.Assets: # of assets loaded: 0
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.NullPointerException
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:111)
Caused by: java.lang.NullPointerException
at com.badlogic.gdx.utils.ObjectMap.get(ObjectMap.java:278)
at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:103)
at game.Assets.init(Assets.java:45)
at com.packtpub.libgdx.canyonbunny.CanyonBunnyMain.create(CanyonBunnyMain.java:17)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:125)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:108)
Check if the files are actually created in the correct folder using file explorer (not eclipse).
If you can't find, try searching it and make it appear in the correct folder first.
The log suggests that the file is being generated, I suppose it should be.
However, eclipse does not recognize the newly generated files. You have to hit refresh in the eclipse project folder before using them.
Check it it works first.
Now you won't be able to pack them at each run. I had the same issue, so I created a different project (to be used as a tool) which would do packing (and automate many other little things). Whenever I changed assets, I'd run the tool once before testing.
Hope this helps.
Good luck.
Looks like you are using an old version of libgdx — right? TexturePacker2 isn't there anymore, there's just the TexturePacker now (and it works flawlessly, by the way). I'd recommend you updating first.
I had the same problem. It worked with drawDebugOutline = true, but when I changed it to false it crashed.
It is an Eclipse thing...
The solution as hinted at above is to refresh the project. Eclipse is not seeing the new packed texture file until you refresh. Right click on the Android project and click "Refresh" which will cause Eclipse to find the new file.
Related
ok i am a new one here and tried to write an awesome program:
package f;
import javax.swing.*;
public class dasMain {
public static void main(String[] args) {
ImageIcon img = new ImageIcon("pics/daFaq.png");
JOptionPane.showMessageDialog(null, img, "u r heck", JOptionPane.PLAIN_MESSAGE);
}
}
the thing is that if I run the program from Intellij Idea, then everything works fine, but after compilation the picture disappears
here are the source files of the project:
https://i.ibb.co/Njc8jYp/screen.png
i want to run this awesome code with pictures on other computers, but i only know this way and it doesn't work :(
You probably do not know where your program expects the picture to be. If you modify your code slightly, this information would be evident. Make use of
ImageIcon(URL)
File.toURI()
URI.toURL()
With that your code can look like this:
package f;
import javax.swing.*;
import java.io.File;
public class dasMain {
public static void main(String[] args) {
File png = new File("pics/daFaq.png");
System.out.println("Loading image from " + png.getAbsolutePath());
ImageIcon img = new ImageIcon(png.toURI().toURL());
JOptionPane.showMessageDialog(null, img, "u r heck", JOptionPane.PLAIN_MESSAGE);
}
}
Also I am pretty sure you intend to ship the png together with your code, so you better load it as a resource from the classpath. Here is an example.
I also investigated a bit why you would not see any error message or exception. This is documented in ImageIcon. So you might want to verify the image was loaded using getImageLoadStatus().
If you access the resource with the path like pics/file_name.png, then the pics - is the package name. And it must be located in the directory, marked as resource type. For example, create the directory, named resources in your project root, mark this directory as resource type, and move the pics there:
P. S. I would advise to use Maven or Gradle build system for managing project builds. As it is commonly accepted build management systems for the JVM projects. IDE New Project Wizard has the option to create Maven or Gradle based projects.
I'm trying to load a video file in OpenCV Java, and have narrowed down my issue to the following code:
import org.opencv.core.Core;
import org.opencv.videoio.VideoCapture;
public class OpenCVTest {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.loadLibrary("opencv_videoio_ffmpeg455_64");
VideoCapture capture = new VideoCapture("myVideoFile.avi");
System.out.println(capture.isOpened());
}
}
Of course, this always prints out "false". Accessing my computer's camera with new VideoCapture(0) works fine. After scouring the internet, I'm thoroughly confused as to why loading a video won't work. I followed guides that suggested I needed to add "opencv_videoio_ffmpeg455_64.dll" to my path variable and call System.loadLibrary. I'm new to this, and to be honest, I don't understand what loadLibrary does, or what could be wrong with my setup and code. Any ideas? Thanks in advance.
Here is the answer from similar problem
load the ffmpeg library System.loadLibrary("opencv_ffmpeg300_64");
Open the file:
VideoCapture vC = new VideoCapture("res/video.mp4");
Copy opencv_ffmpeg300_64.dll from opencv\build\x64\vc11\bin to
opencv\build\java\x64
Please note that 64 and .dll may differ from an OS to another, those are for Windows x64
As it turns out, I was using the wrong file path. I (wrongly) assumed that new VideoCapture("my file") would search for "my file" in the directory where the compiled .class files are placed, when in fact it searches in the project root directory. There is no need to call System.loadLibrary("opencv_videoio_ffmpeg455_64");
Afterburner.fx has broken for me and I can't seem to get it working again.
In my main method I have this:
#Override
public void start(Stage primaryStage) throws Exception{
Map<Object, Object> customProperties = new HashMap<>();
Injector.setConfigurationSource(customProperties::get);
final StackPane views = new StackPane();
customProperties.put("views", views);
BattleView battleView = new BattleView();
Parent view = battleView.getView();
view.setVisible( false );
view.setId("battle");
views.getChildren().add(view);
...
}
Yet I seem to hit an exception when I get to the BattleView battleView = new BattleView(); line. Afterburner fx seems to be trying to evaluate toString() on ui.battle.BattleView and it doesn't like it, see the below picture:
And in the terminal results in:
I haven't found any help from similiar questions so was hoping someone could point me in the right direction! Help!
Edit: Same error after moving battle.css and battle.fxml to resources/ui/battle:
Edit2:
Since you're using Java 11 and module system, your resource output directory have to be in the same output directory as the module they belong to.
Judging by the images in your post, you're using gradle and you build your project directly from IDEA.
You need to tell IDE to copy your resources to the same output directory:
//build.gradle
plugins {
...
id 'idea' //add idea plugin
}
...
//put compiled classes and resources in the same directory (change the path if needed)
idea.module.outputDir file("out/production/classes")
If you plan to build with gradle too:
//put compiled classes and resources in the same directory (change the paths if needed)
sourceSets.main.output.resourcesDir = "build/classes/java/main"
sourceSets.test.output.resourcesDir = "build/classes/java/test"
and you'll probably need to open package with resource files, so that the framework can access them:
//module-info.java
module YourModuleName {
...
opens ui.battle to afterburner.fx; //add this
}
edit:
The 'idea' in 'idea.module.outputDir' is greyed out with the error 'Cannot resolve symbol 'idea''.
It's not really an error, but you can get rid of it easily:
//use this format instead of the previous one
idea {
module.outputDir file("out/production/classes")
}
Im getting an nullPointer error from input streams where I am trying to load resources, such as: setImage(new Image(getClass().getClassLoader().getResourceAsStream("images/ui/mineStartButton.png")));
Do not call getClassLoader() or you'll need to open the package with the image resource - even if the calling code is in the same module (check Javadoc):
//you need to add '/' at the beginning of the path string to make it absolute
getClass().getResourceAsStream("/images/ui/mineStartButton.png")
I'm trying to teach myself java syntax and using minecraft as a platform for diving in. I'm having a problem though because none of my textures are being loaded. For that matter neither are my localizations. Here is the code for my block
package net.richbaird.testtutorial.blocks;
import cpw.mods.fml.common.registry.GameRegistry;
//import cpw.mods.fml.common.registry.LanguageRegistry;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.richbaird.testtutorial.lib.constants;
public class OrangeBlock extends Block {
private String blockName = "orangeBlock";
public OrangeBlock() {
super(Material.rock);
this.setBlockName(constants.MODID + "_" + blockName);
this.setCreativeTab(CreativeTabs.tabBlock);
GameRegistry.registerBlock(this,blockName);
this.setBlockTextureName(constants.MODID + ":" + blockName);
//LanguageRegistry.addName(this,"tutorial block");
}
}
here is my constants class
package net.richbaird.testtutorial.lib;
public class constants {
public static final String MODID = "testtutorial";
public static final String MODNAME = "Test Tutorial";
public static final String VERSION = "1.0";
}
I have my texture saved at
~/IdeaProjects/testmod/src/main/resources/assets/testtutorial/textures/blocks/orangeBlock.png
According to the log it is unable to find my texture. Here's the message I'm getting
[08:08:14] [Client thread/ERROR]:
Using missing texture, unable to load
testtutorial:textures/blocks/orangeBlock.png
java.io.FileNotFoundException: testtutorial:textures/blocks/orangeBlock.png
The client loads and my item shows up but with a default black and purple texture. What have I done wrong? I'm thinking it might have to do with my naming conventions, since the .lang file never gets read either, and the only way I can give my block a friendly name is with the now depreciated LanguageRegistry.addName() method
For those who are curious, it's a bug with intellij 14 looks like. Adding this line to the bottom of the build.gradle that comes with forge
sourceSets {
main { output.resourcesDir = output.classesDir }
}
And running gradle setupDecompWorkspace idea --refresh-dependencies
fixed the problem.
I recently ran into this bug after updating IntelliJ, and while richbai90's solution did fix the immediate issue, it also broke compiling the mod into a jar (the assets folder gets included twice). After some digging around, I eventually found the root of the issue: IntelliJ was delegating the build task to Gradle, which put the assets and classes in separate folders, and Forge didn't know they belong to the same mod. The solution that worked for me was to build and run using the IDE, which is in the Settings dialog under Build, Execution, Deployment | Build Tools | Gradle (the help page has more detailed instructions). On older versions of IntelliJ, this was called "Delegate IDE build/run actions to gradle" (see the help page).
I've got Gradle projects and 2 IDE's: NetBeans and IntelliJ. NetBeans works perfectly with this project, no errors found.
But IntelliJ in the same project throws exception:
[Assets] error: com.badlogic.gdx.utils.GdxRuntimeException: File not found: ./images/enemy.atlas (Internal)
Here is Java class:
public class Assets {
public static AssetManager assetManager;
public Assets() {
assetManager = new AssetManager();
try {
assetManager.load("./images/enemy.atlas", TextureAtlas.class);
assetManager.load("./images/buttons.atlas", TextureAtlas.class);
assetManager.load("./fonts/000.fnt", BitmapFont.class);
assetManager.load("./fonts/111.fnt", BitmapFont.class);
assetManager.setLoader(TiledMap.class, new TmxMapLoader(new InternalFileHandleResolver()));
assetManager.load("./world/level1/map.tmx", TiledMap.class);
assetManager.finishLoading();
} catch (GdxRuntimeException e) { if (Variables.isDebug) System.err.println("\n[Assets] error: " + e + "\n"); }
}
}
It is simple, right? So, I've looked through *.gradle files and noticed that gradle knows where to search for my assets:
project.ext.assetsDir = new File("../android/assets");
I've remade project especially for IDEA (using gdx-setup.jar), but same problem arises. I'm confused and asking for advise!
P.S. AssetManager is a class of LibGDX 1.1.0 library.
Have you tried ommiting the ./ at the beginning of the path? See: https://github.com/libgdx/libgdx/wiki/Managing-your-assets#loading-assets
./ usually references the current directory, but I don't know how libgdx handles it. I can't post this as a comment somehow.
I had similar problem. After 3 hours of being angry i realized that path is case sensitive.
Example for tile map:
Android assets file structure:
assets/Maps/Desert.tmx
//Work as desktop luncher but not on android
TiledMap tiledMap = new TmxMapLoader().load("maps\\Desert.tmx");
//Works on android and dektop
TiledMap tiledMap = new TmxMapLoader().load("Maps\\Desert.tmx");
Second thing which I noticed is that path shouldn't start with
./
../
.\\
..\\
//Enough
"Maps\\Desert.tmx"
//Too much
".\\Maps\\Desert.tmx"
One of the possible way outs for IDEA is to set the Working directory for the Desktop run configuration to your android/assets/ folder.
You need to tell the "desktop module" to look for assets in the "android module." I forget the exact steps to do this...