Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.serdjuk.retro.game.palette.v3
- import com.badlogic.gdx.graphics.Color
- import com.badlogic.gdx.graphics.Pixmap
- import com.badlogic.gdx.graphics.Texture
- import com.badlogic.gdx.graphics.g2d.TextureRegion
- import com.badlogic.gdx.math.MathUtils
- import com.badlogic.gdx.math.Rectangle
- import com.badlogic.gdx.math.Vector2
- class Bitmap(width: Int, height: Int, format: Pixmap.Format) : Pixmap(width, height, format) {
- private val errorMessage = "No more space in pixmap."
- val atlas = HashMap<String, TextureRegion>()
- fun insertRectangle(color: Color, fill: Boolean, area: Pair<Int, Int>) {
- val position = getFreePosition(area.first, area.second, color.toIntBits())
- if (position != null) {
- if (fill) fillRectangle(position.x.toInt(), position.y.toInt(), area.first, area.second)
- drawRectangle(position.x.toInt(), position.y.toInt(), area.first, area.second)
- } else error(errorMessage)
- }
- fun insertRectangleNamed(name: String, color: Color, fill: Boolean, area: Pair<Int, Int>, texture: Texture) {
- val position = getFreePosition(area.first, area.second, color.toIntBits())
- if (position != null) {
- if (fill) fillRectangle(position.x.toInt(), position.y.toInt(), area.first, area.second)
- drawRectangle(position.x.toInt(), position.y.toInt(), area.first, area.second)
- atlas[name] = TextureRegion(texture, position.x.toInt(), position.y.toInt(), area.first, area.second)
- } else error(errorMessage)
- }
- fun insertAreas(color: Color, fill: Boolean, vararg areas: Pair<Int, Int>) {
- areas.forEachIndexed { index, area ->
- insertRectangle(color, fill, area)
- }
- }
- /*
- insertColoredAres (width,height,Color)
- */
- fun insertColorAres(fill: Boolean, vararg coloredAreas: Triple<Int, Int, Color>) {
- coloredAreas.forEach {
- insertRectangle(it.third, fill, Pair(it.first, it.second))
- }
- }
- fun insert(pixmap: Pixmap) {
- val position = getFreePosition(pixmap.width, pixmap.height)
- if (position != null) {
- drawPixmap(pixmap, position.x.toInt(), position.y.toInt())
- } else error(errorMessage)
- }
- /*
- pixmap new pixmap well need added
- name - name for atlas
- texture - this. pixmap texture
- */
- fun insertNamed(pixmap: Pixmap, name: String, texture: Texture) {
- val position = getFreePosition(pixmap.width, pixmap.height)
- if (position != null) {
- drawPixmap(pixmap, position.x.toInt(), position.y.toInt())
- atlas[name] = TextureRegion(texture, position.x.toInt(), position.y.toInt(), pixmap.width, pixmap.height)
- } else error(errorMessage)
- }
- fun getFreePosition(soughtWidth: Int, soughtHeight: Int, clearPixelColor: Int = 0): Vector2? {
- val position = Vector2()
- while (true) {
- var fit = true
- var h = 0
- getClearPixel(position, clearPixelColor, soughtWidth)
- val x = position.x
- val y = position.y
- while (h++ != soughtHeight) {
- if (position.y > (height - soughtHeight)) return null
- repeat(soughtWidth) w@{
- if (getPixel((position.x + it).toInt(), position.y.toInt()) != clearPixelColor) {
- fit = false
- position.x += it + 1
- return@w
- }
- }
- if (!fit) {
- position.y = y
- break
- } else {
- position.y++
- }
- }
- if (fit) {
- position.set(x, y)
- break
- }
- }
- return position
- }
- private fun getClearPixel(position: Vector2, clearPixel: Int, soughtWidth: Int): Vector2 {
- while (getPixel(position.x.toInt(), position.y.toInt()) != clearPixel) {
- if (++position.x >= (width - soughtWidth)) {
- position.x = 0f
- position.y++
- }
- }
- return position
- }
- }
Add Comment
Please, Sign In to add comment