1 /++
2 
3 
4 Macros:
5     LREF = <a href="#$1">$1</a>
6     HREF = <a href="$1">$2</a>
7 
8 Authors: $(HREF https://github.com/TodNaz,TodNaz)
9 Copyright: Copyright (c) 2020 - 2021, TodNaz.
10 License: $(HREF https://github.com/TodNaz/Tida/blob/master/LICENSE,MIT)
11 +/
12 module tida.softimage;
13 
14 import tida.render;
15 
16 /++
17 Object to draw objects on the image.
18 +/
19 class SoftImage : ICanvas
20 {
21     import tida.image, tida.color, tida.shape, tida.vector, tida.window;
22 
23 private:
24     Image image;
25     ubyte[] buffer;
26 
27     uint _width;
28     uint _height;
29 
30     uint _pwidth;
31     uint _pheight;
32 
33     int xput;
34     int yput;
35 
36     BlendMode bmode;
37     BlendFactor[2] sdfactor;
38 
39 public @safe:
40     this(Image img)
41     {
42         image = img;
43 
44         allocatePlace(img.width,img.height);
45         viewport(img.width,img.height);
46 
47         bmode = BlendMode.withBlend;
48         sdfactor = [BlendFactor.SrcAlpha, BlendFactor.OneMinusSrcAlpha];
49     }
50 
51     /++
52     Returns the rendered surface.
53     +/
54     Image done()
55     {
56         return image;
57     }
58 
59 override:
60     void allocatePlace(uint width, uint height)
61     {
62         buffer = new ubyte[](width * height * 4);
63 
64         _width = width;
65         _height = height;
66     }
67 
68     void clearPlane(Color!ubyte color)
69     {
70         for(size_t i = 0; i < _width * _height * 4; i += 4)
71         {
72             buffer[i] = color.r;
73             buffer[i+1] = color.g;
74             buffer[i+2] = color.b;
75             buffer[i+3] = color.a;
76         }
77     }
78 
79     void drawTo()
80     {
81         image.bytes!(PixelFormat.RGBA)(buffer);
82     }
83 
84     void blendMode(BlendMode mode) @safe @property
85     {
86         bmode = mode;
87     }
88     
89     @property BlendMode blendMode()
90     {
91         return bmode;
92     }
93 
94     void blendOperation(BlendFactor sfactor, BlendFactor dfactor)
95     {
96         sdfactor = [sfactor, dfactor];
97     }
98 
99     BlendFactor[2] blendOperation()
100     {
101         return sdfactor;
102     }
103 
104     @property ref ubyte[] data()
105     {
106         return buffer;
107     }
108 
109     mixin PointToImpl!(PixelFormat.RGBA, 4);
110 
111     void viewport(uint w, uint h) @safe
112     {
113         _pwidth = w;
114         _pheight = h;
115         allocatePlace(_width,_height);
116     }
117 
118     void move(int x,int y)
119     {
120         xput = x;
121         yput = y;
122     }
123     
124     @property uint[2] size()
125     {
126         return [_width, _height];
127     }
128     
129     @property uint[2] portSize()
130     {
131         return [_pwidth, _pheight];
132     }
133     
134     @property int[2] cameraPosition()
135     {
136         return [xput, yput];
137     }
138 }