Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Sync from next
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Mar 4, 2021
1 parent b80d1de commit 5bfcf23
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ group=com.soywiz.korlibs.korim
version=2.0.0-SNAPSHOT

# kotlinx
korioVersion=2.0.8
kormaVersion=2.0.7
korioVersion=2.0.9
kormaVersion=2.0.8

# bintray location
project.bintray.org=korlibs
Expand Down
6 changes: 5 additions & 1 deletion korim/src/commonMain/kotlin/com/soywiz/korim/atlas/Atlas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.soywiz.korim.atlas
import com.soywiz.korim.bitmap.*
import com.soywiz.korim.format.*
import com.soywiz.korio.file.*
import com.soywiz.korma.geom.RectangleInt

class Atlas(val textures: Map<String, BitmapSlice<Bitmap>>, val info: AtlasInfo = AtlasInfo()) {
constructor(texture: BitmapSlice<Bitmap>, info: AtlasInfo = AtlasInfo()) : this(mapOf(info.pages.first().fileName to texture), info)
Expand All @@ -14,7 +15,10 @@ class Atlas(val textures: Map<String, BitmapSlice<Bitmap>>, val info: AtlasInfo
val texture = textures[page.fileName]
?: error("Can't find '${page.fileName}' in ${textures.keys}")
val slice = texture.slice(info.frame.rect).let {
BitmapSlice(it.bmpBase, it.bounds, info.name, info.rotated)
// Define virtual frame with offsets "info.spriteSourceSize.x" and "info.spriteSourceSize.y"
// and original texture size "info.sourceSize.width" and "info.sourceSize.height"
BitmapSlice(it.bmpBase, it.bounds, info.name, info.rotated,
virtFrame = if (info.trimmed) RectangleInt(info.spriteSourceSize.x, info.spriteSourceSize.y, info.sourceSize.width, info.sourceSize.height) else null)
}
val name get() = info.name
// @TODO: Use name instead
Expand Down
25 changes: 22 additions & 3 deletions korim/src/commonMain/kotlin/com/soywiz/korim/bitmap/BitmapSlice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import com.soywiz.kmem.*
import com.soywiz.korio.resources.*
import com.soywiz.korma.geom.*

/**
* @property virtFrame This defines a virtual frame [RectangleInt] which surrounds the bounds [RectangleInt] of the [Bitmap].
* It is used in a trimmed texture atlas to specify the original size of a single texture.
* X and y of virtFrame is the offset of the virtual frame to the top left edge of
* the bounds rectangle. Width and height defines the size of the virtual frame.
*/
abstract class BmpSlice(
val bmpBase: Bitmap,
val bounds: RectangleInt,
val name: String? = null,
val rotated: Boolean = false
val rotated: Boolean = false,
val virtFrame: RectangleInt? = null
) : Extra, Resourceable<BmpSlice> {
override fun getOrNull() = this
override suspend fun get() = this
Expand Down Expand Up @@ -37,6 +44,12 @@ abstract class BmpSlice(
val right get() = bounds.right
val bottom get() = bounds.bottom

val trimmed: Boolean = virtFrame != null
val frameOffsetX: Int = virtFrame?.x ?: 0
val frameOffsetY: Int = virtFrame?.y ?: 0
val frameWidth: Int = virtFrame?.width ?: bounds.width
val frameHeight : Int = virtFrame?.height ?: bounds.height

var parent: Any? = null

val tl_x = p0.x.toFloat()
Expand All @@ -61,7 +74,13 @@ fun BmpSlice.getIntBounds(out: RectangleInt = RectangleInt()) = out.setTo(left,

fun BmpSlice.extract(): Bitmap = bmpBase.extract(left, top, width, height)

class BitmapSlice<out T : Bitmap>(override val bmp: T, bounds: RectangleInt, name: String? = null, rotated: Boolean = false) : BmpSlice(bmp, bounds, name, rotated), Extra by Extra.Mixin() {
class BitmapSlice<out T : Bitmap>(
override val bmp: T,
bounds: RectangleInt,
name: String? = null,
rotated: Boolean = false,
virtFrame: RectangleInt? = null
) : BmpSlice(bmp, bounds, name, rotated, virtFrame), Extra by Extra.Mixin() {
val premultiplied get() = bmp.premultiplied

fun extract(): T = bmp.extract(bounds.x, bounds.y, bounds.width, bounds.height)
Expand All @@ -86,7 +105,7 @@ class BitmapSlice<out T : Bitmap>(override val bmp: T, bounds: RectangleInt, nam
}
}

fun withName(name: String? = null) = BitmapSlice<T>(bmp, bounds, name, rotated)
fun withName(name: String? = null) = BitmapSlice<T>(bmp, bounds, name, rotated, virtFrame)

override fun toString(): String = "BitmapSlice($name:${SizeInt(bounds.width, bounds.height)})"
}
Expand Down

0 comments on commit 5bfcf23

Please sign in to comment.