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