vito-Z80

find free space (rectangle) in bitmap (LibGDX Pixmap)

Aug 25th, 2020 (edited)
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.18 KB | None | 0 0
  1. package ru.serdjuk.retro.game.palette.v3
  2.  
  3. import com.badlogic.gdx.graphics.Color
  4. import com.badlogic.gdx.graphics.Pixmap
  5. import com.badlogic.gdx.graphics.Texture
  6. import com.badlogic.gdx.graphics.g2d.TextureRegion
  7. import com.badlogic.gdx.math.MathUtils
  8. import com.badlogic.gdx.math.Rectangle
  9. import com.badlogic.gdx.math.Vector2
  10.  
  11. class Bitmap(width: Int, height: Int, format: Pixmap.Format) : Pixmap(width, height, format) {
  12.  
  13.     private val errorMessage = "No more space in pixmap."
  14.     val atlas = HashMap<String, TextureRegion>()
  15.  
  16.     fun insertRectangle(color: Color, fill: Boolean, area: Pair<Int, Int>) {
  17.         val position = getFreePosition(area.first, area.second, color.toIntBits())
  18.         if (position != null) {
  19.             if (fill) fillRectangle(position.x.toInt(), position.y.toInt(), area.first, area.second)
  20.             drawRectangle(position.x.toInt(), position.y.toInt(), area.first, area.second)
  21.         } else error(errorMessage)
  22.     }
  23.  
  24.     fun insertRectangleNamed(name: String, color: Color, fill: Boolean, area: Pair<Int, Int>, texture: Texture) {
  25.         val position = getFreePosition(area.first, area.second, color.toIntBits())
  26.         if (position != null) {
  27.             if (fill) fillRectangle(position.x.toInt(), position.y.toInt(), area.first, area.second)
  28.             drawRectangle(position.x.toInt(), position.y.toInt(), area.first, area.second)
  29.             atlas[name] = TextureRegion(texture, position.x.toInt(), position.y.toInt(), area.first, area.second)
  30.         } else error(errorMessage)
  31.     }
  32.  
  33.  
  34.     fun insertAreas(color: Color, fill: Boolean, vararg areas: Pair<Int, Int>) {
  35.         areas.forEachIndexed { index, area ->
  36.             insertRectangle(color, fill, area)
  37.         }
  38.     }
  39.  
  40.     /*
  41.     insertColoredAres (width,height,Color)
  42.      */
  43.     fun insertColorAres(fill: Boolean, vararg coloredAreas: Triple<Int, Int, Color>) {
  44.         coloredAreas.forEach {
  45.             insertRectangle(it.third, fill, Pair(it.first, it.second))
  46.         }
  47.     }
  48.  
  49.     fun insert(pixmap: Pixmap) {
  50.         val position = getFreePosition(pixmap.width, pixmap.height)
  51.         if (position != null) {
  52.             drawPixmap(pixmap, position.x.toInt(), position.y.toInt())
  53.         } else error(errorMessage)
  54.     }
  55.  
  56.     /*
  57.     pixmap  new pixmap well need added
  58.     name - name for atlas
  59.     texture - this. pixmap texture
  60.      */
  61.     fun insertNamed(pixmap: Pixmap, name: String, texture: Texture) {
  62.         val position = getFreePosition(pixmap.width, pixmap.height)
  63.         if (position != null) {
  64.             drawPixmap(pixmap, position.x.toInt(), position.y.toInt())
  65.             atlas[name] = TextureRegion(texture, position.x.toInt(), position.y.toInt(), pixmap.width, pixmap.height)
  66.         } else error(errorMessage)
  67.     }
  68.  
  69.  
  70.     fun getFreePosition(soughtWidth: Int, soughtHeight: Int, clearPixelColor: Int = 0): Vector2? {
  71.         val position = Vector2()
  72.         while (true) {
  73.             var fit = true
  74.             var h = 0
  75.             getClearPixel(position, clearPixelColor, soughtWidth)
  76.             val x = position.x
  77.             val y = position.y
  78.             while (h++ != soughtHeight) {
  79.                 if (position.y > (height - soughtHeight)) return null
  80.  
  81.                 repeat(soughtWidth) w@{
  82.                     if (getPixel((position.x + it).toInt(), position.y.toInt()) != clearPixelColor) {
  83.                         fit = false
  84.                         position.x += it + 1
  85.                         return@w
  86.                     }
  87.                 }
  88.                 if (!fit) {
  89.                     position.y = y
  90.                     break
  91.                 } else {
  92.                     position.y++
  93.                 }
  94.             }
  95.             if (fit) {
  96.                 position.set(x, y)
  97.                 break
  98.             }
  99.         }
  100.         return position
  101.     }
  102.  
  103.  
  104.     private fun getClearPixel(position: Vector2, clearPixel: Int, soughtWidth: Int): Vector2 {
  105.         while (getPixel(position.x.toInt(), position.y.toInt()) != clearPixel) {
  106.             if (++position.x >= (width - soughtWidth)) {
  107.                 position.x = 0f
  108.                 position.y++
  109.             }
  110.         }
  111.         return position
  112.     }
  113.  
  114.  
  115. }
Add Comment
Please, Sign In to add comment