An empty texture is a 16 × 16 pixel yellow image that can be treated like a valid texture
An empty texture also results when loading emojis, icons, or image files fails
To check if a texture is empty, use if (texture.isEmpty()), if (texture), or if (not texture)
#include<Siv3D.hpp>voidMain(){Scene::SetBackground(ColorF{0.6,0.8,0.7});Texturetexture1;Print<<texture1.isEmpty();// Assign a texturetexture1=Texture{U"🐈"_emoji};// Specify a non-existent image fileconstTexturetexture2{U"example/aaa.png"};if(nottexture2){Print<<U"Failed to load a texture";}while(System::Update()){// Draw empty texture (16x16 yellow image)texture2.drawAt(400,300);}}
.draw() and .drawAt() allow you to specify a color to multiply with the texture
When drawing a texture pixel ColorF{ sr, sg, sb } with color multiplication ColorF{ r, g, b }, the drawn color becomes ColorF{ (sr * r), (sg * g), (sb * b) } (in normal blend mode)
By default, Palette::White (ColorF{ 1.0 }) is used as the multiplication color
When drawing a texture pixel ColorF{ sr, sg, sb } onto a destination pixel ColorF{ dr, dg, db }, the drawn color becomes ColorF{ (sr * a + dr * (1 - a)), (sg * a + dg * (1 - a)), (sb * a + db * (1 - a)) } (in normal blend mode)
To draw a texture as large as possible within a certain size, use the following member functions to create a TextureRegion with scaling applied:
Code
Description
.fitted(size)
Return a TextureRegion that preserves the texture's aspect ratio while fitting within width size.x and height size.y, scaled to be as large as possible
.fitted(width, height)
Return a TextureRegion that preserves the texture's aspect ratio while fitting within width width and height height, scaled to be as large as possible
TextureRegion can be drawn just like Texture
The cost of creating a TextureRegion from an existing Texture is small, so it can be executed within the main loop without problems
To draw only a rectangular region of a texture, use the following member functions to create a TextureRegion with partial extraction applied:
Code
Description
(x, y, w, h)
Create a TextureRegion that extracts width w and height h from the texture starting at (x, y)
(rect)
Create a TextureRegion that extracts the rect region from the texture
.uv(u, v, w, h)
Create a TextureRegion that extracts width w and height h from UV coordinate (u, v) of the texture
.uv(rect)
Create a TextureRegion that extracts the UV coordinate rect region from the texture
The first two use pixel coordinates, while the latter two use UV coordinates
UV coordinates are coordinates where the top-left of the texture is (0.0, 0.0) and the bottom-right is (1.0, 1.0), always ranging from 0.0 to 1.0 regardless of image size
When texture texture has size 400 × 200, texture(0.5, 0.0, 0.5, 1.0) is the same as texture(200, 0, 200, 200)
TextureRegion can be drawn just like Texture
The cost of creating a TextureRegion from an existing Texture is small, so it can be executed within the main loop without problems
#include<Siv3D.hpp>voidMain(){Scene::SetBackground(ColorF{0.6,0.8,0.7});constTexturetexture{U"example/windmill.png"};constTextureemoji{U"🍎"_emoji};while(System::Update()){// Draw a portion of the image starting from (250, 100) with width 200 and height 150texture(250,100,200,150).draw(40,40);// Draw a portion from UV coordinate (0.5, 0.0) with width 0.5 and height 0.75emoji.uv(0.5,0.0,0.5,0.75).drawAt(400,300);}}
To draw textures repeatedly in a tiled pattern, use the following member functions to create a TextureRegion with tiling applied, then draw with the appropriate texture address mode:
Code
Description
.mapped(width, height)
Create a TextureRegion that tiles the texture with the specified width and height
.mapped(size)
Create a TextureRegion that tiles the texture with the specified size
.repeated(X_count, Y_count)
Create a TextureRegion that tiles the texture X times horizontally and Y times vertically
TextureRegion can be drawn just like Texture
The cost of creating a TextureRegion from an existing Texture is small, so it can be executed within the main loop without problems
The default texture address mode for 2D drawing is Clamp
When trying to draw outside the texture range, that area is filled with the edge color of the texture
When UV coordinates specify values smaller than 0.0 or larger than 1.0, they are treated as 0.0 and 1.0 respectively
It's like a clock hand trying to point to 13 but stopping at 12
Alternatively, when a clock hand tries to point to 13, it can continue past 12, return to 0, and become 1
When UV coordinates specify values like 1.1, 2.3, or -0.3, they are treated as 0.1, 0.3, and 0.7 respectively
This texture address mode is called Repeat
The texture address mode can be changed by setting the sampler state as follows:
See Tutorial 48 for more about sampler states
#include<Siv3D.hpp>voidMain(){Scene::SetBackground(ColorF{0.6,0.8,0.7});constTextureemoji{U"🌳"_emoji};while(System::Update()){{// Set texture address mode to repeatconstScopedRenderStates2Dsampler{SamplerState::RepeatLinear};emoji.mapped(300,400).draw();emoji.repeated(2.5,4).draw(400,0);}}}
You can apply all or part of a texture to Rect, RectF, Circle, Quad, or RoundRect and draw it
Use the following member functions to create objects that fit the shape:
Code
Description
rect(texture)
Create a TexturedQuad by applying a texture (Texture or TextureRegion) to a rectangle (Rect or RectF)
circle(texture)
Create a TexturedCircle by applying a texture (Texture or TextureRegion) to a Circle
quad(texture)
Create a TexturedQuad by applying a texture (Texture or TextureRegion) to a Quad
roundRect(texture)
Create a TexturedRoundRect by applying a texture (Texture or TextureRegion) to a rounded rectangle (RoundRect)
TexturedQuad, TexturedCircle, and TexturedRoundRect can be drawn like Texture
The cost of creating TexturedQuad, TexturedCircle, and TexturedRoundRect from an existing Texture is small, so it can be executed within the main loop without problems
When applying a texture to a Polygon, use functions like the following to create a Buffer2D from the Polygon, then use the Buffer2D drawing function to draw the texture:
| Code | Description |
| polygon.toBuffer2D(offset, size) | Create a Buffer2D that arranges textures of size size with offset as the origin |
| polygon.toBuffer2D(Arg::center = offset, size) | Create a Buffer2D that arranges textures of size size with offset as the center |
offset controls where the texture is applied in screen coordinates
size is the size of the texture to apply
If size is smaller than the original texture size, the texture is reduced; if larger, it's enlarged
Creating a Buffer2D has a small cost, so if possible, create it before the main loop and reuse the created object
With Buffer2D object b and Texturet, draw the texture with b.draw(t)
#include<Siv3D.hpp>voidMain(){Scene::SetBackground(ColorF{0.6,0.8,0.7});constTexturetexture1{U"example/windmill.png",TextureDesc::Mipped};constTexturetexture2{U"example/siv3d-kun.png",TextureDesc::Mipped};constPolygonstar=Shape2D::Star(180,Vec2{200,200});constPolygonhexagon=Shape2D::Hexagon(60,Vec2{480,380});while(System::Update()){constdoublexOffset=(200+Periodic::Sine1_1(5s)*80.0);// Apply texture to star with (xOffset, 200) as the image center and drawstar.toBuffer2D(Arg::center(xOffset,200),texture1.size()).draw(texture1);hexagon.draw(HSV{240,0.5,1.0});// Apply texture to hexagon with (515, 562) as the image center and drawhexagon.toBuffer2D(Arg::center=Vec2{515,562},texture2.size()).draw(texture2);}}
Loading high-resolution image files can increase memory usage and degrade runtime performance
In such cases, you can scale down the image before creating a texture to save memory and improve drawing speed
Load the image file into an Image, scale it down with .scaled(), then create a texture from the resulting Image
#include<Siv3D.hpp>voidMain(){Scene::SetBackground(ColorF{0.6,0.8,0.7});// Create a texture by scaling the image down to 1/4constTexturetexture{Image{U"example/bay.jpg"}.scaled(0.25)};Print<<texture.size();while(System::Update()){texture.draw();}}
31.24.2 Colors from Surrounding Pixels Bleed in Map Tiles¶
When extracting specific map tiles from images containing arranged map tiles and enlarging them or drawing at floating-point coordinates, colors from adjacent map tiles can bleed through
This occurs because surrounding pixels are included during the interpolation process
There are several countermeasures:
Change the sampler state to Nearest
Add 1-pixel padding around textures to minimize the impact of bleeding
Draw at integer coordinates instead of floating-point coordinates
Use Texture2DArray (a feature available from Siv3D v0.8) to treat each map tile as an independent texture
31.24.3 Images Surrounded by Transparency Show Black Outlines When Enlarged¶
When enlarging images surrounded by transparent pixels, like emojis, the surrounding black can bleed through
There are several countermeasures:
Change the sampler state to Nearest
See Tutorial 48 for more about sampler states
Use premultiplied alpha rendering
Premultiplied alpha rendering will be supported as standard in Siv3D v0.8. The current version requires code like the following: