Permalink
Browse files

Use next higher integral factor as well.

When seeking scaled resources, try scaling a slightly higher factor resource
down before attempting to scale a slightly lower factor resource up.
  • Loading branch information...
samskivert committed Sep 19, 2018
1 parent 6608782 commit fda9344841fc8f864a0de0c9e2dbcf37ae560b7c
Showing with 46 additions and 6 deletions.
  1. +8 −6 core/src/playn/core/Scale.java
  2. +38 −0 core/tests/playn/core/ScaleTest.java
@@ -105,16 +105,18 @@ public float roundToNearestPixel(float length) {
}
/**
* Returns an ordered series of scaled resources to try when loading an asset. The highest
* (presumably native) resolution will be tried first, then that will be stepped down to all
* whole integers less than the native resolution. Often this is simply {@code 2, 1}, but on a
* Retina iPad, it could be {@code 4, 3, 2, 1}, and on Android devices it may often be something
* like {@code 3, 2, 1} or {@code 2.5, 2, 1}.
* Returns an ordered series of scaled resources to try when loading an asset. The native
* resolution will be tried first, then that will be rounded up to the nearest whole integer and
* stepped down through all whole integers to {@code 1}. If the native scale factor is {@code 2},
* this will yield {@code 2, 1}. If the native scale factor is {@code 4}, this will yield
* {@code 4, 3, 2, 1}. Android devices often have fractional scale factors, which demonstrates
* the rounding up then back down approach: a native scale factor of {@code 2.5} would yield
* {@code 2.5, 3, 2, 1}.
*/
public List<ScaledResource> getScaledResources(String path) {
List<ScaledResource> rsrcs = new ArrayList<ScaledResource>();
rsrcs.add(new ScaledResource(this, computePath(path, factor)));
for (float rscale = MathUtil.ifloor(factor); rscale > 1; rscale -= 1) {
for (float rscale = MathUtil.iceil(factor); rscale > 1; rscale -= 1) {
if (rscale != factor) rsrcs.add(
new ScaledResource(new Scale(rscale), computePath(path, rscale)));
}
@@ -0,0 +1,38 @@
/**
* Copyright 2010-2015 The PlayN Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package playn.core;
import java.util.List;
import org.junit.Test;
import pythagoras.f.MathUtil;
import static org.junit.Assert.*;
public class ScaleTest {
private void assertScalesMatch (List<Scale.ScaledResource> rsrcs, float... scales) {
int ii = 0;
assertEquals("Scale count does not match resource count", scales.length, rsrcs.size());
for (Scale.ScaledResource rsrc : rsrcs) {
assertEquals(rsrc.scale.factor, scales[ii++], MathUtil.EPSILON);
}
}
@Test
public void testScaledResources () {
assertScalesMatch(new Scale(2).getScaledResources("test.png"), 2, 1);
assertScalesMatch(new Scale(4).getScaledResources("test.png"), 4, 3, 2, 1);
assertScalesMatch(new Scale(2.5f).getScaledResources("test.png"), 2.5f, 3, 2, 1);
assertScalesMatch(new Scale(1.5f).getScaledResources("test.png"), 1.5f, 2, 1);
}
}

0 comments on commit fda9344

Please sign in to comment.