Skip to content

Commit

Permalink
Fixed SampleTextureAtlasPacker ordering
Browse files Browse the repository at this point in the history
Imrpoved burst sample
Removed rain sample
  • Loading branch information
shchurov committed Apr 12, 2017
1 parent 33c04eb commit 61b309e
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 297 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'

// NOTE: Do not place your application dependencies here; they belong
Expand Down
2 changes: 1 addition & 1 deletion particleview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdkVersion 14
targetSdkVersion 25
versionCode 5
versionName "0.9.8"
versionName "0.9.9"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ public TextureAtlas pack(List<Integer> drawableIds, Resources res, int atlasWidt
}

public TextureAtlas pack(List<Bitmap> bitmaps, int atlasWidth, int atlasHeight) {
TextureAtlas atlas = new TextureAtlas(atlasWidth, atlasHeight);
freeRects.add(new Rect(0, 0, atlasWidth, atlasHeight));
List<Bitmap> sortedBitmaps = sortBitmaps(bitmaps);
Rect out = new Rect();
List<Region> regions = new ArrayList<>();
for (Bitmap bmp : sortedBitmaps) {
int index = bitmaps.indexOf(bmp);
boolean rotate = findPositionForBitmap(bmp.getWidth() + 2 * PADDING, bmp.getHeight() + 2 * PADDING, out);
if (rotate) {
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), rotationMatrix, false);
}
atlas.addRegion(out.left + PADDING, out.top + PADDING, rotate, bmp);
regions.add(new Region(index, out.left + PADDING, out.top + PADDING, rotate, bmp));
for (int i = 0; i < freeRects.size(); i++) {
if (splitRect(freeRects.get(i), out)) {
freeRects.remove(i--);
Expand All @@ -52,6 +53,11 @@ public TextureAtlas pack(List<Bitmap> bitmaps, int atlasWidth, int atlasHeight)
cleanUpFreeRects();
}
freeRects.clear();
TextureAtlas atlas = new TextureAtlas(atlasWidth, atlasHeight);
List<Region> sortedRegions = sortRegionsToOriginalOrder(regions);
for (Region r : sortedRegions) {
atlas.addRegion(r.x, r.y, r.cwRotated, r.bitmap);
}
return atlas;
}

Expand All @@ -66,6 +72,17 @@ public int compare(Bitmap b1, Bitmap b2) {
return sorted;
}

private List<Region> sortRegionsToOriginalOrder(List<Region> regions) {
List<Region> sorted = new ArrayList<>(regions);
Collections.sort(sorted, new Comparator<Region>() {
@Override
public int compare(Region o1, Region o2) {
return o1.index - o2.index;
}
});
return sorted;
}

private boolean findPositionForBitmap(int bmpWidth, int bmpHeight, Rect out) {
out.setEmpty();
int minShortExtra = Integer.MAX_VALUE;
Expand Down Expand Up @@ -143,4 +160,20 @@ private void cleanUpFreeRects() {
}
}

private class Region {
final int index;
final int x;
final int y;
final boolean cwRotated;
final Bitmap bitmap;

Region(int index, int x, int y, boolean cwRotated, Bitmap bitmap) {
this.index = index;
this.x = x;
this.y = y;
this.cwRotated = cwRotated;
this.bitmap = bitmap;
}
}

}
4 changes: 0 additions & 4 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
android:name=".spinner.SpinnerActivity"
android:screenOrientation="portrait"/>

<activity
android:name=".rain.RainActivity"
android:screenOrientation="portrait"/>

<activity
android:name=".getting_started.GettingStartedActivity"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import com.github.shchurov.particleview.sample.burst.BurstActivity;
import com.github.shchurov.particleview.sample.getting_started.GettingStartedActivity;
import com.github.shchurov.particleview.sample.rain.RainActivity;
import com.github.shchurov.particleview.sample.spinner.SpinnerActivity;

public class MainActivity extends AppCompatActivity {
Expand All @@ -35,11 +34,5 @@ public void onClick(View view) {
SpinnerActivity.start(MainActivity.this);
}
});
findViewById(R.id.btnRain).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RainActivity.start(MainActivity.this);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ class BurstParticle extends Particle {
float vr;
double timeLeft;

BurstParticle(float x, float y, int texture, float vx, float vy, float vr, double timeLeft) {
super(SIZE, SIZE, x, y, texture);
BurstParticle() {
super(SIZE, SIZE, 0, 0, 0);
}

void setup(float x, float y, int textureIndex, float vx, float vy, float vr, double timeLeft) {
setX(x);
setY(y);
setTextureIndex(textureIndex);
this.vx = vx;
this.vy = vy;
this.vr = vr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.shchurov.particleview.ParticleSystem;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Random;
Expand All @@ -26,6 +27,7 @@ class BurstParticleSystem implements ParticleSystem {
private List<BurstParticle> particles = new ArrayList<>();
private Queue<PointF> originsQueue = new ConcurrentLinkedQueue<>();
private Random random = new Random();
private ParticlesPool pool = new ParticlesPool();

@Override
public int getMaxCount() {
Expand All @@ -52,6 +54,7 @@ private void updateExistingParticles(double timeDelta) {
BurstParticle p = particles.get(i);
if ((p.timeLeft -= timeDelta) < 0) {
particles.remove(i--);
pool.recycle(p);
continue;
}
p.setX(p.getX() + (float) (p.vx * timeDelta));
Expand All @@ -69,10 +72,30 @@ private void pollOrigins() {
float vx = (random.nextBoolean() ? 1 : -1) * MAX_VX * random.nextFloat();
float vy = (random.nextBoolean() ? 1 : -1) * MAX_VY * random.nextFloat();
float vr = (random.nextBoolean() ? 1 : -1) * MAX_VR * random.nextFloat();
particles.add(new BurstParticle(origin.x, origin.y, random.nextInt(TEXTURE_COUNT), vx, vy, vr,
BURST_DURATION));
BurstParticle p = pool.obtain(origin.x, origin.y, random.nextInt(TEXTURE_COUNT), vx, vy, vr,
BURST_DURATION);
particles.add(p);
}
}
}

private class ParticlesPool {

Queue<BurstParticle> pool = new LinkedList<>();

BurstParticle obtain(float x, float y, int textureIndex, float vx, float vy, float vr, double timeLeft) {
BurstParticle p = pool.poll();
if (p == null) {
p = new BurstParticle();
}
p.setup(x, y, textureIndex, vx, vy, vr, timeLeft);
return p;
}

void recycle(BurstParticle p) {
pool.add(p);
}

}

}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 61b309e

Please sign in to comment.