Skip to content

Commit

Permalink
Use next higher integral factor as well.
Browse files Browse the repository at this point in the history
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 fda9344
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
14 changes: 8 additions & 6 deletions core/src/playn/core/Scale.java
Expand Up @@ -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)));
}
Expand Down
38 changes: 38 additions & 0 deletions core/tests/playn/core/ScaleTest.java
@@ -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.