Defines rectangular shape and its content as a collection of pixels.
Image is one of the key types in CosPlay. Almost everything that is drawn on the screen is represented by an image: FIGLet font string, sprites, video and animation frames, etc. Image itself is just a container for pixels. It can be created in-code or loaded and saved from and to the external file or resource. To render the image on the screen you can use CPCanvas.drawImage method.
Image is an asset. Just like other assets such as fonts, sounds, animations or videos they are not managed or governed by the CosPlay game engine unlike scenes and scene objects that are managed and governed by the game engine. Assets are typically created outside the game loop and managed by the developer, they can be freely shared between scenes or scene objects as any other standard Scala objects.
In-Code Images
One of the convenient features of CosPlay is that images can be created & painted (a.k.a. skinned) right in code with very minimal gestalt. The easiest way is to use CPArrayImage class that provides utility methods to convert a margin-based Scala string into an image. For example:
import org.cosplay.*
import org.cosplay.CPColor.*
import org.cosplay.CPArrayImage.*
import org.cosplay.CPPixel.*
object CPAlienImage extends CPArrayImage(
prepSeq("""
| . .
| \/
| (@@)
| g/\_)(_/\e
|g/\(=--=)/\e
| //\\
| _| |_
"""),
(ch, _, _) => ch&C_LIME
)
Another example with more sophisticated skinning:
import org.cosplay.*
import org.cosplay.CPColor.*
import org.cosplay.CPArrayImage.*
import org.cosplay.CPPixel.*
object CPAmigaImage extends CPArrayImage(
prepSeq("""
| .---------.
| |.-------.|
| ||>run#xx||
| ||xxxxxxx||
| |"-------'|
|.-^---------^-.
||x---~xxxAMiGA|
|"-------------'
"""),
(ch, _, _) => ch match
case ' ' => XRAY
case c @ ('A' | 'M' | 'i' | 'G') => CPPixel(c, C_NAVY, C_WHITE)
case c @ ('r' | 'u' | 'n' | '#') => c&C_GREEN_YELLOW
case 'x' => CPPixel(' ', C_BLACK)
case '>' => '>'&C_GREEN_YELLOW
case '~' => '~'&C_ORANGE_RED
case c => c&C_WHITE
)
Loading Images
You can load images from the external source like URL or file path in one of the following formats:
*.xp
REXPaint XP format. This is a native binary format supported by REXPaint ASCII editor. This format supports full color information.*.csv
REXPaint CSV format. This format is natively exported by REXPaint ASCII editor and also supported by CosPlay to save an image with. This format supports full color information.*.txt
format. Image in this format is a simple *.txt file and it does not provide or store any color information.
Use one of the following methods from the companion object to load the image from file path, resources
folder or URL:
- load()
- loadRexCsv()
- loadRexXp()
- loadTxt()
For example:
val speckImg = CPImage.load(
"prefab/images/speck.xp",
(px, _, _) => px.withBg(None) // Make background transparent.
)
Saving Images
You can save image to the local file path in the following format:
*.csv
REXPaint CSV format. This format is natively exported by REXPaint ASCII editor and also supported by CosPlay to save an image with. This format supports full color information.*.xp
REXPaint XP format. This is a native format used by REXPaint ASCII editor and can be loaded by the REXPaint.This format supports full color information.*.txt
Text format. This format does not retain color information.
Use the following methods to save the image to the file path:
ASCII Art Online
There's a vast collection of existing ASCII art imagery online. Here's some of the main resources where you can find it:
- http://www.asciiworld.com/
- https://www.asciiart.eu/
- http://www.ascii-art.de/
- https://asciiart.website
- https://www.incredibleart.org/links/ascii.html
- http://blocktronics.org/
- http://ansiart.com/
- https://llizardakaejm.wordpress.com/ascii-animations/
Prefabs
CosPlay comes with a list of prefab image, animations and video clips. All of them are shipped with CosPlay and can be found in org.cosplay.prefabs
package and its sub-packages.
ASCII Art Editors
REXPaint ASCII editor is an excellent free editor for ASCII art from the creator of the Cogmind game. REXPaint editor is highly recommended for images with non-trivial coloring.
Value parameters
- origin
-
The origin of the image like file path or URL.
Attributes
- Example
-
See CPImageCarouselExample class for the example of using images.
See CPImageFormatsExample class for the example of using images.
- Companion
- object
- Source
- CPImage.scala
- Graph
-
- Supertypes
- Known subtypes
-
class CPArrayImageobject CPBikingAniImage.typeobject CPBirdAniImage.typeobject CPCubeAniImage.typeobject CPCurveAniImage.typeobject CPDancerAniImage.typeobject CPHandAniImage.typeobject CPLogoCatAniImage.typeobject CPMacarena1AniImage.typeobject CPMacarena2AniImage.typeobject CPMacarena3AniImage.typeobject CPMacarena4AniImage.typeobject CPMacarena5AniImage.typeobject CPPointManAniImage.typeobject CPRunnerAniImage.typeobject CPSomersaultAniImage.typeobject CPSpinningGlobeAniImage.typeobject CPCircle1aImage.typeobject CPCircle1bImage.typeobject CPCircle1cImage.typeobject CPCircle2aImage.typeobject CPCircle2bImage.typeobject CPCircle3Image.typeobject CPCircle4Image.typeobject CPCircle5Image.typeobject CPCircle6Image.typeobject CPCircle9Image.typeobject CPAardvarkImage.typeobject CPAlienImage.typeobject CPAlienPlanetImage.typeobject CPAmigaImage.typeobject CPArrowImage.typeobject CPAstronaut1Image.typeobject CPAstronaut2Image.typeobject CPAtari2080STImage.typeobject CPBananaImage.typeobject CPBatImage.typeobject CPBearImage.typeobject CPBedImage.typeobject CPBeetleImage.typeobject CPBrickNImage.typeobject CPBrickWImage.typeobject CPCactusImage.typeobject CPCalculatorImage.typeobject CPCapsuleImage.typeobject CPCarImage.typeobject CPCastleImage.typeobject CPCloudImage.typeobject CPCrownImage.typeobject CPCubesImage.typeobject CPDogImage.typeobject CPDolphinImage.typeobject CPFlamingoImage.typeobject CPGameBoyImage.typeobject CPGingerbreadImage.typeobject CPGlobeImage.typeobject CPGrimReaperImage.typeobject CPHelicopterImage.typeobject CPIceCreamImage.typeobject CPKnifeImage.typeobject CPLanderImage.typeobject CPMoon1Image.typeobject CPMoon2Image.typeobject CPMotorcycleImage.typeobject CPMouseImage.typeobject CPOceanLinerImage.typeobject CPPlaneImage.typeobject CPRocket1Image.typeobject CPRocket2Image.typeobject CPSatellite1Image.typeobject CPSatellite2Image.typeobject CPSatellite3Image.typeobject CPSaturnImage.typeobject CPShieldImage.typeobject CPSkullImage.typeobject CPSpaceShipImage.typeobject CPSunGlassesImage.typeobject CPSwordImage.typeobject CPTelescopeImage.typeobject CPTntImage.typeobject CPTornadoImage.typeobject CPTruckImage.typeobject CPUmbrellaImage.type