Before adding a sprite to the scene, a texture atlas containing the sprite’s image needs to be created. A texture atlas is a series of small images combined into a single larger image. They are often referred to as spritesheets. Chilli Source uses its own texture atlas format: csatlas.
To build a csatlas file, the CSAtlasBuilder java command line tool is used. First place all images that should be included in the atlas in a directory then run the following with the correct directory and file paths:
java -jar CSAtlasBuilder.jar --input --output
This will output three files: a csatlasid file, the csatlas file, and a csimage file. The csatlasid file contains a list of the image frame names, each named after the associated input image. These Ids are used lookup the original image in the texture atlas. The csatlas file contains the image frame data. The csimage file is an image containing all of the input images, in Chilli Source’s own format. All of these files should be added to the project by copying them to
Now that the texture atlas has been created, adding a sprite to the scene is easy. A sprite is an Entity with a Sprite Component attached to it, which is created through the Render Component Factory.
Sprite Components are created with a world space size and a Size Policy. The size policy describes how the sprite will be sized if the world space size and actual image size are different. For example, if using SizePolicy::k_fitMaintainingAspect the sprite will be sized such that it fits inside the given world space size without changing the source image aspect ratio.
//load the texture and texture atlas.
CSCore::ResourcePool* resourcePool = CSCore::Application::Get()->GetResourcePool();
CSRendering::TextureCSPtr texture = resourcePool->LoadResource(CSCore::StorageLocation::k_package, "Textures/TextureAtlas.csimage");
CSRendering::TextureAtlasCSPtr textureAtlas = resourcePool->LoadResource(CSCore::StorageLocation::k_package, "Textures/TextureAtlas.csatlas");
//create the material
CSRendering::MaterialFactory* materialFactory = CSCore::Application::Get()->GetSystem();
CSRendering::MaterialSPtr material = materialFactory->CreateSprite("SpriteMaterial", texture);
//Create the sprite component
CSRendering::RenderComponentFactory* renderComponentFactory = CSCore::Application::Get()->GetSystem();
CSRendering::SpriteComponentSPtr spriteComponent = renderComponentFactory->CreateSpriteComponent(CSCore::Vector2::k_one, textureAtlas, "ImageId", material, CSRendering::SpriteComponent::SizePolicy::k_fitMaintainingAspect);
//create the sprite entity and add the sprite component
CSCore::EntitySPtr spriteEntity = CSCore::Entity::Create();
spriteEntity->AddComponent(spriteComponent);
//add the sprite to the scene
GetScene()->Add(spriteEntity);