-
Notifications
You must be signed in to change notification settings - Fork 0
Image Module
As with all other modules, you can access the image module with a simple import door;
, but if you only want the image module, it's import door.images;
. The Image Module is comprised of 4 sub-modules:
- Image (
door.images.image
) - Clickable Image (
door.images.clickable
) - Clickable Image with Delegate (
door.images.clickable_delegate
) - Texture (
door.images.texture
) All of these modules translate and extend raylib images into an object oriented paradigm. Each module contains a single class which itself has multiple methods.
The Texture module contains a class called Tx2d. The name was chosen to prevent conflicts with raylib's Texture type. Textures are usually faster than images because they are stored and drawn by the GPU rather than the CPU. When you initialize a Tx2d, the class will essentially send all the image data to the GPU.
Tx2d contains only one field, which is only accessible within the door.images module and which is for internal use only.
Tx2d has 3 constructors:
-
this(string fileName)
This constructor takes a string which contains the path to an image file.
Usage:
// png is used in this example, but all image formats accepted by raylib are valid.
auto mytexture = new Tx2d("path/to/image.png");
-
this(Img image)
This constructor takes an Img, a type defined in door.images.image.
Usage:
// png is used in this example, but all image formats accepted by raylib are valid.
auto myimage = new Img("path/to/image.png");
auto mytexture = new Tx2d(myimage);
-
this(Image image)
This constructor takes an Image, a type defined in raylib. raylib's types are considerably lower level and harder to use than the typical DOOR class. It is recommended not to use this unless you are transitioning from pure raylib-d to DOOR and/or you know what you are doing.
Usage:
// png is used in this example, but all image formats accepted by raylib are valid.
auto myimage = LoadImageFromFile("path/to/image.png");
auto mytexture = new Tx2d(myimage);
The update method comes in two flavours:
void update(Img image)
and void update(Img image, Rectangle rectangle)
The former changes the image of the texture on the GPU. The latter also does that in addition to confining the new image data to the dimensions of the rectangle. Do note that using an image smaller than the rectangle will yield potentially undesirable effects.
The following examples start with this image:
Take for example this image that we want to update the original texture with:
Now using a smaller rectangle this is what we get:
Playing with the rectangle's settings should yield various results. The original raylib documentation on this feature is not very explicit as to the purpose of updating with a rectangle, so it is recommended that one be careful and test often when using that variant.
This method only has one variant: void generateMipmaps()
.
This method generates mipmaps of the texture on the GPU. It's helpful for reducing aliasing (grainy, overly sharp visual artifacts) when the texture is viewed from a rendered from a large distance.
void setFilter(int filter)
This method apply a filter on the texture.
The filter
parameter can take in any of these values from the TextureFilter
enum from the raylib
module:
// Texture parameters: filter mode
// NOTE 1: Filtering considers mipmaps if available in the texture
// NOTE 2: Filter is accordingly set for minification and magnification
enum TextureFilter
{
TEXTURE_FILTER_POINT = 0, // No filter, just pixel aproximation
TEXTURE_FILTER_BILINEAR = 1, // Linear filtering
TEXTURE_FILTER_TRILINEAR = 2, // Trilinear filtering (linear with mipmaps)
TEXTURE_FILTER_ANISOTROPIC_4X = 3, // Anisotropic filtering 4x
TEXTURE_FILTER_ANISOTROPIC_8X = 4, // Anisotropic filtering 8x
TEXTURE_FILTER_ANISOTROPIC_16X = 5 // Anisotropic filtering 16x
}
void SetWrap(int wrap)
This method sets the texture wrapping mode. This is controls how the GPU wraps the texture around a model.
The wrap
parameter can take in one of the following values values from the TextureWrap
enum from the raylib
module:
// Texture parameters: wrap mode
enum TextureWrap
{
TEXTURE_WRAP_REPEAT = 0, // Repeats texture in tiled mode
TEXTURE_WRAP_CLAMP = 1, // Clamps texture to edge pixel in tiled mode
TEXTURE_WRAP_MIRROR_REPEAT = 2, // Mirrors and repeats the texture in tiled mode
TEXTURE_WRAP_MIRROR_CLAMP = 3 // Mirrors and clamps to border the texture in tiled mode
}
There are are 8 variants of draw
as well as 4 explicit methods prefixed with "draw":
Variants:
void draw(int x, int y, Color tint = white)
void draw(Vector2 position, Color tint = white)
void draw(int x, int y, float rotation, float scale, Color tint = white)
void draw(Vector2 position, float rotation, float scale, Color tint = white)
void draw(Rectangle source, int x, int y, Color tint = white)
void draw(Rectangle source, Vector2 position, Color tint = white)
void draw(Vector2 tiling, Vector2 offset, Rectangle quad, Color tint = white)
void draw(Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint = white)
Prefixed with "draw":
void drawTiled(Rectangle source, Rectangle dest, Vector2 origin, float rotation, float scale, Color tint = white)
void drawNPatch(NPatchInfo nPatchInfo, Rectangle dest, Vector2 origin, float rotation, Color tint = white)
void drawPolygon(Vector2 center, Vector2[] points, Vector2[] texcoords, Color tint = white)
These are wrappers for various DrawTexture*
functions in raylib.
You get various degrees over how the texture is drawn based on which method you use.
Here's an example of how to use the simplest draw
method:
import door;
import raylib;
void main()
{
auto window = Window.getInstance(640, 480, "A raylib game");
SetTargetFPS(60);
auto myTexture = new Tx2d("/path/to/image.png");
while (!window.shouldClose)
{
BeginDrawing;
scope (exit) EndDrawing;
green.ClearBackground;
myTexture.draw(0, 0);
}
}
The result will be the loaded texture being drawn at the top left of the window: