1 module grape.surface; 2 3 import derelict.sdl2.sdl; 4 import derelict.sdl2.ttf; 5 import std.exception : enforce; 6 7 import std.stdio; 8 9 enum SurfaceFormat { 10 unknown = SDL_PIXELFORMAT_UNKNOWN, 11 index1lsb = SDL_PIXELFORMAT_INDEX1LSB, 12 index1msb = SDL_PIXELFORMAT_INDEX1MSB, 13 index4lsb = SDL_PIXELFORMAT_INDEX4LSB, 14 index4msb = SDL_PIXELFORMAT_INDEX4MSB, 15 index8 = SDL_PIXELFORMAT_INDEX8, 16 rgb332 = SDL_PIXELFORMAT_RGB332, 17 rgb444 = SDL_PIXELFORMAT_RGB444, 18 rgb555 = SDL_PIXELFORMAT_RGB555, 19 bgr555 = SDL_PIXELFORMAT_BGR555, 20 argb444 = SDL_PIXELFORMAT_ARGB4444, 21 rgba4444 = SDL_PIXELFORMAT_RGBA4444, 22 abgr4444 = SDL_PIXELFORMAT_ABGR4444, 23 bgra4444 = SDL_PIXELFORMAT_BGRA4444, 24 argb1555 = SDL_PIXELFORMAT_ARGB1555, 25 rgba5551 = SDL_PIXELFORMAT_RGBA5551, 26 abgr1555 = SDL_PIXELFORMAT_ABGR1555, 27 bgra5551 = SDL_PIXELFORMAT_BGRA5551, 28 rgb565 = SDL_PIXELFORMAT_RGB565, 29 bgr565 = SDL_PIXELFORMAT_BGR565, 30 rgb24 = SDL_PIXELFORMAT_RGB24, 31 bgr24 = SDL_PIXELFORMAT_BGR24, 32 rgb888 = SDL_PIXELFORMAT_RGB888, 33 rgbx8888 = SDL_PIXELFORMAT_RGBX8888, 34 bgr888 = SDL_PIXELFORMAT_BGR888, 35 bgrx8888 = SDL_PIXELFORMAT_BGRX8888, 36 argb8888 = SDL_PIXELFORMAT_ARGB8888, 37 rgba8888 = SDL_PIXELFORMAT_RGBA8888, 38 abgr8888 = SDL_PIXELFORMAT_ABGR8888, 39 bgra8888 = SDL_PIXELFORMAT_BGRA8888, 40 argb2101010 = SDL_PIXELFORMAT_ARGB2101010, 41 42 yy12 = SDL_PIXELFORMAT_YV12, 43 iyuv = SDL_PIXELFORMAT_IYUV, 44 yuy2 = SDL_PIXELFORMAT_YUY2, 45 uyyy = SDL_PIXELFORMAT_UYVY, 46 yyyu = SDL_PIXELFORMAT_YVYU 47 } 48 49 final class Surface { 50 public: 51 ~this() { 52 debug(dtor) writeln("Surface dtor"); 53 // free(); 54 } 55 56 void create(in SDL_Surface* delegate() dg) { 57 free(); 58 _surf = dg(); 59 enforce(_surf !is null, "Surface.create() failed."); 60 } 61 62 void convert(in SurfaceFormat flag) { 63 auto tmp = SDL_ConvertSurfaceFormat(_surf, flag, 0); 64 free(); 65 _surf = tmp; 66 enforce(_surf !is null, "SDL_ConvertSurfaceFormat() failed."); 67 } 68 69 @property { 70 int w() { 71 return _surf.w; 72 } 73 74 int h() { 75 return _surf.h; 76 } 77 78 void* pixels() { 79 return _surf.pixels; 80 } 81 82 int bytes_per_pixel() { 83 return _surf.format.BytesPerPixel; 84 } 85 } 86 87 private: 88 void free() { 89 if (_surf !is null) 90 SDL_FreeSurface(_surf); 91 } 92 93 SDL_Surface* _surf; 94 } 95