Skip to content

Commit

Permalink
[spine-android] Clean-up, batching renderer, clipping TBD
Browse files Browse the repository at this point in the history
  • Loading branch information
badlogic committed Apr 26, 2024
1 parent f8ebef5 commit 4f32c5b
Show file tree
Hide file tree
Showing 6 changed files with 486 additions and 393 deletions.
Expand Up @@ -34,7 +34,6 @@ fun AppContent() {
color = MaterialTheme.colorScheme.background
) {
Box {
BackgroundImage()
SpineViewComposable()
}
}
Expand All @@ -52,13 +51,3 @@ fun SpineViewComposable(modifier: Modifier = Modifier.fillMaxSize()) {
modifier = modifier
)
}

@Composable
fun BackgroundImage() {
val image: Painter = painterResource(id = com.esotericsoftware.spine.R.drawable.img) // Replace with your image resource
Image(
painter = image,
contentDescription = null,
modifier = Modifier.fillMaxSize()
)
}
Expand Up @@ -29,11 +29,9 @@

package com.esotericsoftware.spine.android;

import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Null;

import com.esotericsoftware.spine.Skin;
import com.esotericsoftware.spine.attachments.AttachmentLoader;
import com.esotericsoftware.spine.attachments.BoundingBoxAttachment;
Expand All @@ -50,61 +48,61 @@
* Spine Runtimes Guide. */
@SuppressWarnings("javadoc")
public class AndroidAtlasAttachmentLoader implements AttachmentLoader {
private AndroidTextureAtlas atlas;
private AndroidTextureAtlas atlas;

public AndroidAtlasAttachmentLoader (AndroidTextureAtlas atlas) {
if (atlas == null) throw new IllegalArgumentException("atlas cannot be null.");
this.atlas = atlas;
}
public AndroidAtlasAttachmentLoader (AndroidTextureAtlas atlas) {
if (atlas == null) throw new IllegalArgumentException("atlas cannot be null.");
this.atlas = atlas;
}

private void loadSequence (String name, String basePath, Sequence sequence) {
TextureRegion[] regions = sequence.getRegions();
for (int i = 0, n = regions.length; i < n; i++) {
String path = sequence.getPath(basePath, i);
regions[i] = atlas.findRegion(path);
if (regions[i] == null) throw new RuntimeException("Region not found in atlas: " + path + " (sequence: " + name + ")");
}
}
private void loadSequence (String name, String basePath, Sequence sequence) {
TextureRegion[] regions = sequence.getRegions();
for (int i = 0, n = regions.length; i < n; i++) {
String path = sequence.getPath(basePath, i);
regions[i] = atlas.findRegion(path);
if (regions[i] == null) throw new RuntimeException("Region not found in atlas: " + path + " (sequence: " + name + ")");
}
}

public RegionAttachment newRegionAttachment (Skin skin, String name, String path, @Null Sequence sequence) {
RegionAttachment attachment = new RegionAttachment(name);
if (sequence != null)
loadSequence(name, path, sequence);
else {
AtlasRegion region = atlas.findRegion(path);
if (region == null)
throw new RuntimeException("Region not found in atlas: " + path + " (region attachment: " + name + ")");
attachment.setRegion(region);
}
return attachment;
}
public RegionAttachment newRegionAttachment (Skin skin, String name, String path, @Null Sequence sequence) {
RegionAttachment attachment = new RegionAttachment(name);
if (sequence != null)
loadSequence(name, path, sequence);
else {
AtlasRegion region = atlas.findRegion(path);
if (region == null)
throw new RuntimeException("Region not found in atlas: " + path + " (region attachment: " + name + ")");
attachment.setRegion(region);
}
return attachment;
}

public MeshAttachment newMeshAttachment (Skin skin, String name, String path, @Null Sequence sequence) {
MeshAttachment attachment = new MeshAttachment(name);
if (sequence != null)
loadSequence(name, path, sequence);
else {
AtlasRegion region = atlas.findRegion(path);
if (region == null)
throw new RuntimeException("Region not found in atlas: " + path + " (mesh attachment: " + name + ")");
attachment.setRegion(region);
}
return attachment;
}
public MeshAttachment newMeshAttachment (Skin skin, String name, String path, @Null Sequence sequence) {
MeshAttachment attachment = new MeshAttachment(name);
if (sequence != null)
loadSequence(name, path, sequence);
else {
AtlasRegion region = atlas.findRegion(path);
if (region == null)
throw new RuntimeException("Region not found in atlas: " + path + " (mesh attachment: " + name + ")");
attachment.setRegion(region);
}
return attachment;
}

public BoundingBoxAttachment newBoundingBoxAttachment (Skin skin, String name) {
return new BoundingBoxAttachment(name);
}
public BoundingBoxAttachment newBoundingBoxAttachment (Skin skin, String name) {
return new BoundingBoxAttachment(name);
}

public ClippingAttachment newClippingAttachment (Skin skin, String name) {
return new ClippingAttachment(name);
}
public ClippingAttachment newClippingAttachment (Skin skin, String name) {
return new ClippingAttachment(name);
}

public PathAttachment newPathAttachment (Skin skin, String name) {
return new PathAttachment(name);
}
public PathAttachment newPathAttachment (Skin skin, String name) {
return new PathAttachment(name);
}

public PointAttachment newPointAttachment (Skin skin, String name) {
return new PointAttachment(name);
}
public PointAttachment newPointAttachment (Skin skin, String name) {
return new PointAttachment(name);
}
}
@@ -1,45 +1,70 @@

package com.esotericsoftware.spine.android;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.ObjectMap;
import com.esotericsoftware.spine.BlendMode;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.TextureData;

public class AndroidTexture extends Texture {
private Bitmap bitmap;
private Paint paint;

protected AndroidTexture(Bitmap bitmap) {
super();
this.bitmap = bitmap;
this.paint = new Paint();
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);
}

public Bitmap getBitmap() {
return bitmap;
}

public Paint getPaint() {
return paint;
}

@Override
public int getWidth() {
return bitmap.getWidth();
}

@Override
public int getHeight() {
return bitmap.getHeight();
}

@Override
public void dispose() {
bitmap.recycle();
}
private Bitmap bitmap;
private ObjectMap<BlendMode, Paint> paints = new ObjectMap<>();

protected AndroidTexture (Bitmap bitmap) {
super();
this.bitmap = bitmap;
for (BlendMode blendMode : BlendMode.values()) {
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);

switch (blendMode) {
case normal:
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
break;
case multiply:
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
break;
case additive:
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
break;
case screen:
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
break;
default:
break;
}

paints.put(blendMode, paint);
}
}

public Bitmap getBitmap () {
return bitmap;
}

public Paint getPaint (BlendMode blendMode) {
return paints.get(blendMode);
}

@Override
public int getWidth () {
return bitmap.getWidth();
}

@Override
public int getHeight () {
return bitmap.getHeight();
}

@Override
public void dispose () {
bitmap.recycle();
}
}

0 comments on commit 4f32c5b

Please sign in to comment.