CPImageSprite

class CPImageSprite(id: String, x: Int, y: Int, z: Int, img: CPImage, collidable: Boolean, shaders: Seq[CPShader], tags: String*) extends CPSceneObject

Scene object tailor-made for rendering images.

This sprite can be used to render static or moving image, the same image or changing images. For static unchanging images see CPStaticImageSprite for even simpler API.

Dynamic Image Position

If a game is intended to work with adaptive scene sizing than all of its scene objects must adapt to changing size of the scene. Image sprite provides simple and convenient way to support this logic. Here's an example of using image sprite to display an image at the center of the screen dynamically adjusting its position on each frame to the actual canvas size:

   val img = ...
   val spr = new CPImageSprite("logo", 0, 0, 0, img):
       override def update(ctx: CPSceneObjectContext): Unit =
           val canv = ctx.getCanvas
           setX((canv.dim.width - getImage.getWidth) / 2)
           setY((canv.dim.height - getImage.getHeight) / 2)

Notes:

  • All we have to do is override CPSceneObject.update method to update XY-coordinate on each frame.
  • Canvas instance will always have the current size of scene: whether it is defined explicitly or implicitly as the current size of the terminal.
  • Initial (0,0) position is not used as we override the actual position on each frame.
  • We use methods setX and setY to set current XY-coordinates.
  • Note that we don't need to override any other methods. Specifically, we don't need to override render method since it relies on getX and getY method in its implementation.

Sprites

CosPlay provides number of built-in sprites. A sprite is a scene objects, visible or off-screen, that is custom designed for a particular use case. Built-in sprites provide concrete implementations for the abstract methods in the base CPSceneObject class. Most non-trivial games will use combination of the built-in sprites and their own ones. Here's the list of the built-in sprites:

Value parameters:
collidable

Whether or not this sprite has a collision shape. Default is false.

id

Optional ID of the sprite. By default, a random ID will be used.

img

The image to render. It can be changed later.

shaders

Optional sequence of shaders for this sprite. Default value is an empty sequence.

tags

Optional set of organizational or grouping tags. By default, the empty set is used.

x

Initial X-coordinate of the sprite.

y

Initial Y-coordinate of the sprite.

z

Z-index at which to render the image.

See also:
Example:

See CPImageCarouselExample class for the example of using images.

See CPImageFormatsExample class for the example of using images.

Companion:
object
Source:
CPImageSprite.scala
class Object
trait Matchable
class Any

Value members

Concrete methods

override def getCollisionRect: Option[CPRect]

Gets optional collision shape or hit box for this sprite.

Gets optional collision shape or hit box for this sprite.

Definition Classes
Source:
CPImageSprite.scala
override def getDim: CPDim

Gets current dimension (width and height) of this object.

Gets current dimension (width and height) of this object.

Definition Classes
Source:
CPImageSprite.scala

Gets current image to render.

Gets current image to render.

Returns:

Current image to render.

Source:
CPImageSprite.scala
override def getRect: CPRect

Gets rectangular shape of this sprite. It is basically a combination of its top-left corner XY-coordinate and sprite's dimension.

Gets rectangular shape of this sprite. It is basically a combination of its top-left corner XY-coordinate and sprite's dimension.

Definition Classes
Source:
CPImageSprite.scala
override def getShaders: Seq[CPShader]

Gets the optional list of shaders attached to this scene object. By default, returns an empty list. Note that shaders are called regardless of whether the object visible, in camera frame or invisible.

Gets the optional list of shaders attached to this scene object. By default, returns an empty list. Note that shaders are called regardless of whether the object visible, in camera frame or invisible.

Definition Classes
Source:
CPImageSprite.scala
override def getX: Int

Gets current X-coordinate of this object within dimensions of its scene. Note that returned value is allowed to be outside scene's dimension (e.g. negative value). In such cases, the clipping of the scene rendering will result in showing only portion or none of the object.

Gets current X-coordinate of this object within dimensions of its scene. Note that returned value is allowed to be outside scene's dimension (e.g. negative value). In such cases, the clipping of the scene rendering will result in showing only portion or none of the object.

Definition Classes
Source:
CPImageSprite.scala
override def getY: Int

Gets current Y-coordinate of this object within dimensions of its scene. Note that returned value is allowed to be outside scene's dimension (e.g. negative value). In such cases, the clipping of the scene rendering will result in showing only portion or none of the object.

Gets current Y-coordinate of this object within dimensions of its scene. Note that returned value is allowed to be outside scene's dimension (e.g. negative value). In such cases, the clipping of the scene rendering will result in showing only portion or none of the object.

Definition Classes
Source:
CPImageSprite.scala
override def getZ: Int

Gets Z-index or order to use in rendering of this object. A pixel with higher Z-index visually overrides the overlapping pixel in the same XY-coordinate with equal or smaller Z-index.

Gets Z-index or order to use in rendering of this object. A pixel with higher Z-index visually overrides the overlapping pixel in the same XY-coordinate with equal or smaller Z-index.

Definition Classes
Source:
CPImageSprite.scala
override def render(ctx: CPSceneObjectContext): Unit

Called to render this scene object. Only visible and in camera frame objects will receive this callback. This callback is called on scene object after all scene objects received update callback. Note that unlike update callbacks and shaders that are called for all scene objects on each frame, this callback is only called for scene objects that are visible and, at least partially, in camera frame.

Called to render this scene object. Only visible and in camera frame objects will receive this callback. This callback is called on scene object after all scene objects received update callback. Note that unlike update callbacks and shaders that are called for all scene objects on each frame, this callback is only called for scene objects that are visible and, at least partially, in camera frame.

Definition Classes
Source:
CPImageSprite.scala
def reset(): Unit

Resets this sprite to its initial XYZ-coordinates and the initial image.

Resets this sprite to its initial XYZ-coordinates and the initial image.

Source:
CPImageSprite.scala
def setImage(img: CPImage): Unit

Changes the image this sprite is rendering.

Changes the image this sprite is rendering.

Value parameters:
img

New image to render.

Source:
CPImageSprite.scala
def setX(d: Int): Unit

Sets current X-coordinate. This coordinate will be returned from getX method.

Sets current X-coordinate. This coordinate will be returned from getX method.

Value parameters:
d

X-coordinate to set.

Source:
CPImageSprite.scala
def setXY(a: Int, b: Int): Unit

Sets both current XY-coordinates.

Sets both current XY-coordinates.

Value parameters:
a

X-coordinate to set.

b

Y-coordinate to set.

Source:
CPImageSprite.scala
def setY(d: Int): Unit

Sets current Y-coordinate. This coordinate will be returned from getY method.

Sets current Y-coordinate. This coordinate will be returned from getY method.

Value parameters:
d

Y-coordinate to set.

Source:
CPImageSprite.scala
def setZ(d: Int): Unit

Sets current Z-index. This index will be returned from getZ method.

Sets current Z-index. This index will be returned from getZ method.

Value parameters:
d

Z-index to set.

Source:
CPImageSprite.scala

Inherited methods

override def equals(obj: Any): Boolean
Definition Classes
Inherited from:
CPGameObject
Source:
CPGameObject.scala
inline def getHeight: Int

Gets current height of this object.

Gets current height of this object.

See also:
Inherited from:
CPSceneObject
Source:
CPSceneObject.scala
def getId: String

Gets unique ID of this game object.

Gets unique ID of this game object.

Inherited from:
CPGameObject
Source:
CPGameObject.scala
final def getState: State

Gets current lifecycle state.

Gets current lifecycle state.

Inherited from:
CPLifecycle
Source:
CPLifecycle.scala
def getTags: Set[String]

Gets optional set of organizational tags. Note that by default the set of tags is empty.

Gets optional set of organizational tags. Note that by default the set of tags is empty.

See also:
Inherited from:
CPGameObject
Source:
CPGameObject.scala
inline def getWidth: Int

Gets current width of this object.

Gets current width of this object.

See also:
Inherited from:
CPSceneObject
Source:
CPSceneObject.scala
def hide(): Unit

Shortcut method for hiding this object. Note that by default all scene objects are visible.

Shortcut method for hiding this object. Note that by default all scene objects are visible.

See also:
Inherited from:
CPSceneObject
Source:
CPSceneObject.scala
def isVisible: Boolean

Checks the visibility flag.

Checks the visibility flag.

If object is invisible than only update method will be called on each frame. If object is visible and in camera frame - method render will be called as well to render itself. Note that shaders are called regardless of whether the object visible, in camera frame or invisible.

See also:
Inherited from:
CPSceneObject
Source:
CPSceneObject.scala
def onActivate(): Unit

Callback on lifecycle object activation. Default implementation is no-op.

Callback on lifecycle object activation. Default implementation is no-op.

See also:

CPLifecycle.State.LF_ACTIVE

Inherited from:
CPLifecycle
Source:
CPLifecycle.scala
def onDeactivate(): Unit

Callback on lifecycle object deactivation. Default implementation is no-op.

Callback on lifecycle object deactivation. Default implementation is no-op.

See also:

CPLifecycle.State.LF_INACTIVE

Inherited from:
CPLifecycle
Source:
CPLifecycle.scala
def onStart(): Unit

Callback on lifecycle object start. Default implementation is no-op.

Callback on lifecycle object start. Default implementation is no-op.

See also:

CPLifecycle.State.LF_STARTED

Inherited from:
CPLifecycle
Source:
CPLifecycle.scala
def onStop(): Unit

Callback on lifecycle object stop. Default implementation is no-op.

Callback on lifecycle object stop. Default implementation is no-op.

See also:

CPLifecycle.State.LF_STOPPED

Inherited from:
CPLifecycle
Source:
CPLifecycle.scala
def setVisible(vis: Boolean): Unit

Sets visibility flag. Note that by default all scene objects are visible.

Sets visibility flag. Note that by default all scene objects are visible.

Value parameters:
vis

true to make this object visible, false otherwise.

See also:
Inherited from:
CPSceneObject
Source:
CPSceneObject.scala
def show(): Unit

Shortcut method for showing this object. Note that by default all scene objects are visible.

Shortcut method for showing this object. Note that by default all scene objects are visible.

See also:
Inherited from:
CPSceneObject
Source:
CPSceneObject.scala

Called to update the internal state of this scene object. This callback is called each frame on every object in the scene and it is called before any render callback. Note that all scene object will receive this callback before first render callback.

Called to update the internal state of this scene object. This callback is called each frame on every object in the scene and it is called before any render callback. Note that all scene object will receive this callback before first render callback.

Value parameters:
ctx

Frame context. This context provides bulk of functionality that a scene object can do in a game, e.g. interact with other scene objects, check collisions, read input events and manage input focus, add or remove scene objects, add new and switch between scenes, etc.

See also:
Inherited from:
CPSceneObject
Source:
CPSceneObject.scala

Concrete fields

final val initImg: CPImage

Initial image of the sprite.

Initial image of the sprite.

See also:
Source:
CPImageSprite.scala
final val initX: Int

Initial X-coordinate of the sprite.

Initial X-coordinate of the sprite.

See also:
Source:
CPImageSprite.scala
final val initY: Int

Initial Y-coordinate of the sprite.

Initial Y-coordinate of the sprite.

See also:
Source:
CPImageSprite.scala
final val initZ: Int

Initial Z-index of the sprite.

Initial Z-index of the sprite.

See also:
Source:
CPImageSprite.scala