2025-01-31 12:14:27 +03:00

62 lines
2.4 KiB
Scala

package ocelot.desktop.graphics_v2
import ocelot.desktop.color.Color
import java.nio.ByteBuffer
trait Graphics {
/** Type of [[Image.Static]] used by this graphics backend.
*
* Created with [[createStaticImage()]] and must be destroyed with [[destroyStaticImage()]]
*/
type StaticImage <: Image.Static
/** Type of [[Image.Surface]] used by this graphics backend.
*
* This is used for off-screen rendering and for rendering to the window. Though the latter requires you to obtain
* a window-bound surface using implementation-specific methods defined outside of this trait.
*
* Created with [[createSurface()]] and must be destroyed with [[destroySurface()]]
*/
type Surface <: Image.Surface
/** Defines various passes that happen during the rendering process in sequence. */
sealed class Pass
object Pass {
/** Clears the surface with a solid color. */
case class Clear(surface: Surface, color: Color) extends Pass
/** Draws 2D geometry to a surface using the callback to populate it. */
case class Draw2D(surface: Surface, callback: Encoder2D => Unit) extends Pass
/** Downloads the surface contents, invoking the callback on completion. */
case class Download(surface: Surface, callback: ByteBuffer => Unit) extends Pass
}
/** Creates a static image with given dimensions, format, and data (row-major, packed without alignment or padding).
*
* @throws IllegalArgumentException if width or height is zero or negative,
* if data is too short or too long for given format and dimensions
*/
def createStaticImage(width: Int, height: Int, format: Image.Format, packedData: ByteBuffer): StaticImage
/** Destroys a static image.
* @throws IllegalArgumentException if the image does not belong to this [[Graphics]] instance or is already destroyed.
*/
def destroyStaticImage(image: StaticImage): Unit
/** Creates a drawable off-screen surface with given dimensions. */
def createSurface(width: Int, height: Int, format: Image.Format): Surface
/** Destroys a surface.
* @throws IllegalArgumentException if the surface does not belong to this [[Graphics]] instance or is already destroyed.
*/
def destroySurface(surface: Surface): Unit
/** Performs all rendering passes from the specified array in sequence. */
def process(passes: Array[Pass]): Unit
}