1 module gfm.sdl2.texture;
2 
3 import std..string;
4 
5 import derelict.sdl2.sdl;
6 
7 import gfm.sdl2.sdl,
8        gfm.sdl2.surface,
9        gfm.sdl2.renderer;
10 
11 /// SDL Texture wrapper.
12 final class SDL2Texture
13 {
14     public
15     {
16         /// Creates a SDL Texture for a specific renderer.
17         /// See_also: $(LINK http://wiki.libsdl.org/SDL_CreateTexture)
18         /// Throws: $(D SDL2Exception) on error.
19         this(SDL2Renderer renderer, uint format, uint access, int width, int height)
20         {
21             _sdl2 = renderer._sdl2;
22             _handle = SDL_CreateTexture(renderer._renderer, format, access, width, height);
23             if (_handle is null)
24                 _sdl2.throwSDL2Exception("SDL_CreateTexture");
25         }
26 
27         /// Creates a SDL Texture for a specific renderer, from an existing surface.
28         /// See_also: $(LINK http://wiki.libsdl.org/SDL_CreateTextureFromSurface)
29         /// Throws: $(D SDL2Exception) on error.
30         this(SDL2Renderer renderer, SDL2Surface surface)
31         {
32             _handle = SDL_CreateTextureFromSurface(renderer._renderer, surface._surface);
33             if (_handle is null)
34                 _sdl2.throwSDL2Exception("SDL_CreateTextureFromSurface");
35         }
36 
37         ~this()
38         {
39             close();
40         }
41 
42         /// Releases the SDL resource.
43         void close() 
44         {
45             if (_handle !is null)
46             {
47                 SDL_DestroyTexture(_handle);
48                 _handle = null;
49             }
50         }
51 
52         /// See_also: $(LINK http://wiki.libsdl.org/SDL_SetTextureBlendMode)
53         /// Throws: $(D SDL2Exception) on error.
54         void setBlendMode(SDL_BlendMode blendMode)
55         {
56             if (SDL_SetTextureBlendMode(_handle, blendMode) != 0)
57                 _sdl2.throwSDL2Exception("SDL_SetTextureBlendMode");
58         }
59 
60         /// See_also: $(LINK http://wiki.libsdl.org/SDL_SetTextureColorMod)
61         /// Throws: $(D SDL2Exception) on error.
62         void setColorMod(int r, int g, int b)
63         {
64             if (SDL_SetTextureColorMod(_handle, cast(ubyte)r, cast(ubyte)g, cast(ubyte)b) != 0)
65                 _sdl2.throwSDL2Exception("SDL_SetTextureColorMod");
66         }
67 
68         /// See_also: $(LINK http://wiki.libsdl.org/SDL_SetTextureAlphaMod)
69         /// Throws: $(D SDL2Exception) on error.
70         void setAlphaMod(int a)
71         {
72             if (SDL_SetTextureAlphaMod(_handle, cast(ubyte)a) != 0)
73                 _sdl2.throwSDL2Exception("SDL_SetTextureAlphaMod");
74         }
75 
76         /// Returns: Texture format.
77         /// See_also: $(LINK http://wiki.libsdl.org/SDL_QueryTexture)
78         /// Throws: $(D SDL2Exception) on error.
79         uint format()
80         {
81             uint res;
82             int err = SDL_QueryTexture(_handle, &res, null, null, null);
83             if (err != 0)
84                 _sdl2.throwSDL2Exception("SDL_QueryTexture");
85 
86             return res;
87         }
88 
89         /// Returns: Texture access.
90         /// See_also: $(LINK http://wiki.libsdl.org/SDL_QueryTexture)
91         /// Throws: $(D SDL2Exception) on error.
92         int access()
93         {
94             int res;
95             int err = SDL_QueryTexture(_handle, null, &res, null, null);
96             if (err != 0)
97                 _sdl2.throwSDL2Exception("SDL_QueryTexture");
98 
99             return res;
100         }
101 
102         /// Returns: Width of texture.
103         /// See_also: $(LINK http://wiki.libsdl.org/SDL_QueryTexture)
104         /// Throws: $(D SDL2Exception) on error.
105         int width()
106         {
107             int res;
108             int err = SDL_QueryTexture(_handle, null, null, &res, null);
109             if (err != 0)
110                 _sdl2.throwSDL2Exception("SDL_QueryTexture");
111             return res;
112         }
113 
114         /// Returns: Height of texture.
115         /// See_also: $(LINK http://wiki.libsdl.org/SDL_QueryTexture)
116         /// Throws: $(D SDL2Exception) on error.
117         int height()
118         {
119             int res;
120             int err = SDL_QueryTexture(_handle, null, null, null, &res);
121             if (err != 0)
122                 _sdl2.throwSDL2Exception("SDL_QueryTexture");
123             return res;
124         }
125 
126         /// Updates the whole texture with new pixel data.
127         /// See_also: $(LINK http://wiki.libsdl.org/SDL_UpdateTexture)
128         /// Throws: $(D SDL2Exception) on error.
129         void updateTexture(const(void)* pixels, int pitch)
130         {
131             int err = SDL_UpdateTexture(_handle, null, pixels, pitch);
132             if (err != 0)
133                 _sdl2.throwSDL2Exception("SDL_UpdateTexture");
134         }
135 
136         /// Updates a part of a texture with new pixel data.
137         /// See_also: $(LINK http://wiki.libsdl.org/SDL_UpdateTexture)
138         /// Throws: $(D SDL2Exception) on error.
139         void updateTexture(const(SDL_Rect)* rect, const(void)* pixels, int pitch)
140         {
141             int err = SDL_UpdateTexture(_handle, rect, pixels, pitch);
142             if (err != 0)
143                 _sdl2.throwSDL2Exception("SDL_UpdateTexture");
144         }
145 
146         /// Update a planar YV12 or IYUV texture with new pixel data. 
147         /// See_also: $(LINK http://wiki.libsdl.org/SDL_UpdateYUVTexture)
148         /// Throws: $(D SDL2Exception) on error.
149         void updateYUVTexture(const(ubyte)* Yplane, int Ypitch, const(ubyte)* Uplane, int Upitch, const Uint8* Vplane, int Vpitch)
150         {
151             int err = SDL_UpdateYUVTexture(_handle, null, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
152             if (err != 0)
153                 _sdl2.throwSDL2Exception("SDL_UpdateYUVTexture");
154         }
155 
156         /// Update a part of a planar YV12 or IYUV texture with new pixel data.
157         /// See_also: $(LINK http://wiki.libsdl.org/SDL_UpdateYUVTexture)
158         /// Throws: $(D SDL2Exception) on error.
159         void updateYUVTexture(const(SDL_Rect)* rect, const(ubyte)* Yplane, int Ypitch, const(ubyte)* Uplane, int Upitch, const Uint8* Vplane, int Vpitch)
160         {
161             int err = SDL_UpdateYUVTexture(_handle, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
162             if (err != 0)
163                 _sdl2.throwSDL2Exception("SDL_UpdateYUVTexture");
164         }
165 
166     }
167 
168     package
169     {
170         SDL_Texture* _handle;
171     }
172 
173     private
174     {
175         SDL2 _sdl2;
176     }
177 }
178