1 module gfm.sdl2.texture; 2 3 import std.string; 4 5 import derelict.sdl2.sdl; 6 7 import gfm.core.log; 8 9 import gfm.sdl2.sdl, 10 gfm.sdl2.surface, 11 gfm.sdl2.renderer; 12 13 /// SDL Texture wrapper. 14 final class SDL2Texture 15 { 16 public 17 { 18 /// Creates a SDL Texture for a specific renderer. 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 this(SDL2Renderer renderer, SDL2Surface surface) 29 { 30 _handle = SDL_CreateTextureFromSurface(renderer._renderer, surface._surface); 31 if (_handle is null) 32 _sdl2.throwSDL2Exception("SDL_CreateTextureFromSurface"); 33 } 34 35 ~this() 36 { 37 close(); 38 } 39 40 /// Releases the SDL resource. 41 void close() 42 { 43 if (_handle !is null) 44 { 45 SDL_DestroyTexture(_handle); 46 _handle = null; 47 } 48 } 49 50 void setBlendMode(SDL_BlendMode blendMode) 51 { 52 if (SDL_SetTextureBlendMode(_handle, blendMode) != 0) 53 _sdl2.throwSDL2Exception("SDL_SetTextureBlendMode"); 54 } 55 56 void setColorMod(ubyte r, ubyte g, ubyte b) 57 { 58 if (SDL_SetTextureColorMod(_handle, r, g, b) != 0) 59 _sdl2.throwSDL2Exception("SDL_SetTextureColorMod"); 60 } 61 62 void setAlphaMod(ubyte a) 63 { 64 if (SDL_SetTextureAlphaMod(_handle, a) != 0) 65 _sdl2.throwSDL2Exception("SDL_SetTextureAlphaMod"); 66 } 67 68 uint format() 69 { 70 uint res; 71 int err = SDL_QueryTexture(_handle, &res, null, null, null); 72 if (err != 0) 73 _sdl2.throwSDL2Exception("SDL_QueryTexture"); 74 75 return res; 76 } 77 78 int access() 79 { 80 int res; 81 int err = SDL_QueryTexture(_handle, null, &res, null, null); 82 if (err != 0) 83 _sdl2.throwSDL2Exception("SDL_QueryTexture"); 84 85 return res; 86 } 87 88 /// Returns: Width of texture. 89 int width() 90 { 91 int res; 92 int err = SDL_QueryTexture(_handle, null, null, &res, null); 93 if (err != 0) 94 _sdl2.throwSDL2Exception("SDL_QueryTexture"); 95 return res; 96 } 97 98 /// Returns: Height of texture. 99 int height() 100 { 101 int res; 102 int err = SDL_QueryTexture(_handle, null, null, null, &res); 103 if (err != 0) 104 _sdl2.throwSDL2Exception("SDL_QueryTexture"); 105 return res; 106 } 107 } 108 109 package 110 { 111 SDL_Texture* _handle; 112 } 113 114 private 115 { 116 SDL2 _sdl2; 117 } 118 } 119