Skip to content

Commit

Permalink
2021.5 added pixmap asset loader
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Aug 3, 2021
1 parent 282305c commit d88cf8d
Show file tree
Hide file tree
Showing 16 changed files with 417 additions and 46 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.terefang</groupId>
<artifactId>gdx-ddsdxt</artifactId>
<version>2021.4</version>
<version>2021.5</version>

<build>
<plugins>
Expand All @@ -25,6 +25,7 @@
<configuration>
<services>
<param>com.github.terefang.gdx.ddsdxt.GenericTextureDataLoaderFactory</param>
<param>com.github.terefang.gdx.ddsdxt.GenericPixmapLoaderFactory</param>
</services>
</configuration>
<executions>
Expand Down Expand Up @@ -61,6 +62,12 @@
<version>${gdx.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx-backend-headless</artifactId>
<version>${gdx.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.terefang.gdx.ddsdxt;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;

public interface GenericPixmapLoader
{
public Pixmap loadPixmap(String _fileName, FileHandle _file);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.terefang.gdx.ddsdxt;

import com.badlogic.gdx.files.FileHandle;

import java.util.Iterator;
import java.util.ServiceLoader;

public interface GenericPixmapLoaderFactory
{
public static GenericPixmapLoader findLoader(String _fileName, FileHandle _file)
{
ServiceLoader<GenericPixmapLoaderFactory> _sl = ServiceLoader.load(GenericPixmapLoaderFactory.class);
Iterator<GenericPixmapLoaderFactory> _it = _sl.iterator();
while(_it.hasNext())
{
GenericPixmapLoaderFactory _impl = _it.next();
if(_impl.canLoadPixmap(_fileName,_file))
{
return _impl.getInstance();
}
}
return null;
}

public boolean canLoadPixmap(String _fileName, FileHandle _file);

public GenericPixmapLoader getInstance();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

public interface GenericTextureDataLoader
{
public TextureData load(String _fileName, FileHandle _file, TextureLoader.TextureParameter _param);
public TextureData loadTexture(String _fileName, FileHandle _file, TextureLoader.TextureParameter _param);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public static GenericTextureDataLoader findLoader(String _fileName, FileHandle _
while(_it.hasNext())
{
GenericTextureDataLoaderFactory _impl = _it.next();
if(_impl.canLoad(_fileName,_file))
if(_impl.canLoadTexture(_fileName,_file))
{
return _impl.getInstance();
}
}
return null;
}

public boolean canLoad(String _fileName, FileHandle _file);
public boolean canLoadTexture(String _fileName, FileHandle _file);

public GenericTextureDataLoader getInstance();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.github.terefang.gdx.ddsdxt.assets;

import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetLoaderParameters;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.SynchronousAssetLoader;
import com.badlogic.gdx.assets.loaders.TextureLoader;
import com.badlogic.gdx.assets.loaders.resolvers.AbsoluteFileHandleResolver;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.TextureData;
import com.badlogic.gdx.utils.Array;
import com.github.terefang.gdx.ddsdxt.GenericPixmapLoader;
import com.github.terefang.gdx.ddsdxt.GenericPixmapLoaderFactory;
import com.github.terefang.gdx.ddsdxt.GenericTextureDataLoader;
import com.github.terefang.gdx.ddsdxt.GenericTextureDataLoaderFactory;
import com.github.terefang.gdx.ddsdxt.load.DDSLoader;
import com.github.terefang.gdx.ddsdxt.load.DXTNLoader;

/** {@link DxtnPixmapAssetLoader} for {@link Texture} instances. The pixel data is loaded synchronously. The texture is then created on the
* rendering thread, synchronously. Passing a {@link TextureLoader.TextureParameter} to
* {@link AssetManager#load(String, Class, AssetLoaderParameters)} allows one to specify parameters as can be passed to the
* various Texture constructors, e.g. filtering, whether to generate mipmaps and so on.
*/
public class DxtnPixmapAssetLoader extends SynchronousAssetLoader<Pixmap, AssetLoaderParameters<Pixmap>>
{
public static DxtnPixmapAssetLoader register(AssetManager _mgr, FileHandleResolver _resolver)
{
DxtnPixmapAssetLoader _tl = new DxtnPixmapAssetLoader(_resolver);
_mgr.setLoader(Pixmap.class, ".dds", _tl);
_mgr.setLoader(Pixmap.class, ".dds.gz", _tl);
_mgr.setLoader(Pixmap.class, ".dxtn", _tl);
_mgr.setLoader(Pixmap.class, ".dxtn.gz", _tl);
return _tl;
}

public static void register(AssetManager _mgr)
{
register(_mgr, new AbsoluteFileHandleResolver());
}

boolean useServiceLoader = false;
public DxtnPixmapAssetLoader(FileHandleResolver _resolver)
{
super(_resolver);
}


public DxtnPixmapAssetLoader(FileHandleResolver _resolver, boolean _sl)
{
super(_resolver);
this.useServiceLoader = _sl;
}

@Override
public Pixmap load(AssetManager _manager, String _fileName, FileHandle _file, AssetLoaderParameters<Pixmap> _parameter)
{
if(this.useServiceLoader)
{
GenericPixmapLoader _loader = GenericPixmapLoaderFactory.findLoader(_fileName, _file);
if(_loader!=null)
{
return _loader.loadPixmap(_fileName, _file);
}
}
else
if(_fileName.endsWith(".dds") || _fileName.endsWith(".dds.gz"))
{
return DDSLoader.fromDdsToPixmap(_fileName, _file);
}
else
if(_fileName.endsWith(".dxtn") || _fileName.endsWith(".dxtn.gz"))
{
return DXTNLoader.fromDxtnToPixmap(_fileName, _file);
}

return null;
}

@Override
public Array<AssetDescriptor> getDependencies(String _fileName, FileHandle _file, AssetLoaderParameters<Pixmap> _parameter)
{
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
*/
public class DxtnTextureAssetLoader extends SynchronousAssetLoader<Texture, TextureLoader.TextureParameter>
{
public static void register(AssetManager _mgr, FileHandleResolver _resolver)
public static DxtnTextureAssetLoader register(AssetManager _mgr, FileHandleResolver _resolver)
{
DxtnTextureAssetLoader _tl = new DxtnTextureAssetLoader(_resolver);
_mgr.setLoader(Texture.class, ".dds", _tl);
_mgr.setLoader(Texture.class, ".dds.gz", _tl);
_mgr.setLoader(Texture.class, ".dxtn", _tl);
_mgr.setLoader(Texture.class, ".dxtn.gz", _tl);
return _tl;
}

public static void register(AssetManager _mgr)
Expand All @@ -40,12 +41,14 @@ public static void register(AssetManager _mgr)
}

boolean useServiceLoader = false;
public DxtnTextureAssetLoader(FileHandleResolver _resolver) {
public DxtnTextureAssetLoader(FileHandleResolver _resolver)
{
super(_resolver);
}


public DxtnTextureAssetLoader(FileHandleResolver _resolver, boolean _sl) {
public DxtnTextureAssetLoader(FileHandleResolver _resolver, boolean _sl)
{
super(_resolver);
this.useServiceLoader = _sl;
}
Expand All @@ -54,7 +57,8 @@ public DxtnTextureAssetLoader(FileHandleResolver _resolver, boolean _sl) {
public Texture load(AssetManager _manager, String _fileName, FileHandle _file, TextureLoader.TextureParameter _parameter)
{
boolean _genMipMaps = false;
if (_parameter != null) {
if (_parameter != null)
{
_genMipMaps = _parameter.genMipMaps;
}

Expand All @@ -64,24 +68,25 @@ public Texture load(AssetManager _manager, String _fileName, FileHandle _file, T
GenericTextureDataLoader _loader = GenericTextureDataLoaderFactory.findLoader(_fileName, _file);
if(_loader!=null)
{
_textureData = _loader.load(_fileName, _file, _parameter);
_textureData = _loader.loadTexture(_fileName, _file, _parameter);
}
}
else
if(_fileName.endsWith(".dds") || _fileName.endsWith(".dds.gz"))
{
_textureData = DDSLoader.fromDDS(_file, _genMipMaps);
_textureData = DDSLoader.fromDdsToTexture(_file, _genMipMaps);
}
else
if(_fileName.endsWith(".dxtn") || _fileName.endsWith(".dxtn.gz"))
{
_textureData = DXTNLoader.fromDXTN(_file, _genMipMaps);
_textureData = DXTNLoader.fromDxtnToTexture(_file, _genMipMaps);
}

if(_textureData==null) return null;

Texture _texture = new Texture(_textureData);
if (_parameter != null) {
if (_parameter != null)
{
_texture.setFilter(_parameter.minFilter, _parameter.magFilter);
_texture.setWrap(_parameter.wrapU, _parameter.wrapV);
}
Expand Down
Loading

0 comments on commit d88cf8d

Please sign in to comment.