2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 module egfx
.backgl
/*is aliced*/;
20 import arsd
.simpledisplay
;
24 //import iv.glbinds : glTexParameterfv; // rdmd hack
30 // ////////////////////////////////////////////////////////////////////////// //
31 enum GLTexType
= GL_BGRA
; // GL_RGBA;
34 // ////////////////////////////////////////////////////////////////////////// //
35 public __gshared
uint vglTexId
; // OpenGL texture id
36 public __gshared
uint vArrowTextureId
= 0;
39 // ////////////////////////////////////////////////////////////////////////// //
40 shared static this () {
41 import core
.stdc
.stdlib
: malloc
;
42 vglTexBuf
= cast(uint*)malloc((VBufWidth
*VBufHeight
+4)*4);
43 if (vglTexBuf
is null) { import core
.exception
: onOutOfMemoryErrorNoGC
; onOutOfMemoryErrorNoGC(); }
44 vglTexBuf
[0..VBufWidth
*VBufHeight
+4] = 0;
48 // ////////////////////////////////////////////////////////////////////////// //
49 public void vglCreateArrowTexture () {
52 enum wrapOpt
= GL_REPEAT
;
53 enum filterOpt
= GL_NEAREST
; //GL_LINEAR;
54 enum ttype
= GL_UNSIGNED_BYTE
;
56 if (vArrowTextureId
) glDeleteTextures(1, &vArrowTextureId
);
58 glGenTextures(1, &vArrowTextureId
);
59 if (vArrowTextureId
== 0) assert(0, "can't create arrow texture");
61 //GLint gltextbinding;
62 //glGetIntegerv(GL_TEXTURE_BINDING_2D, &gltextbinding);
63 //scope(exit) glBindTexture(GL_TEXTURE_2D, gltextbinding);
65 glBindTexture(GL_TEXTURE_2D
, vArrowTextureId
);
66 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, wrapOpt
);
67 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, wrapOpt
);
68 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, filterOpt
);
69 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, filterOpt
);
70 //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
71 //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
73 //GLfloat[4] bclr = 0.0;
74 //glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, bclr.ptr);
76 uint[16*8] pmap
= 0x00_000000U;
77 // sprite,sprite,mask,mask
78 static immutable ushort[$] spx
= [
79 0b11111111_10000000, 0b00000000_01111111,
80 0b01000000_10000000, 0b10000000_01111111,
81 0b00100000_10000000, 0b11000000_01111111,
82 0b00010000_01100000, 0b11100000_00011111,
83 0b00001001_10011000, 0b11110000_00000111,
84 0b00000110_01100110, 0b11111001_10000001,
85 0b00000000_00011001, 0b11111111_11100000,
86 0b00000000_00000110, 0b11111111_11111001,
89 foreach (immutable dy
; 0..8) {
90 ushort spr
= spx
[dy
*2+0];
91 ushort msk
= spx
[dy
*2+1];
92 foreach (immutable dx
; 0..16) {
93 if ((msk
&0x8000) == 0) {
94 pmap
[dy
*16+dx
] = (spr
&0x8000 ?
0xff_ffffffU
: 0xff_000000U);
100 //pmap = 0xff_ff0000U;
103 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 16, 8, 0, GLTexType
, GL_UNSIGNED_BYTE
, pmap
.ptr
);
107 // ////////////////////////////////////////////////////////////////////////// //
108 public void vglBlitArrow (int px
, int py
) {
109 if (vArrowTextureId
!= 0) {
110 glMatrixMode(GL_PROJECTION
); // for ortho camera
112 // left, right, bottom, top, near, far
113 //glViewport(0, 0, w*vbufEffScale, h*vbufEffScale);
114 //glOrtho(0, w, h, 0, -1, 1); // top-to-bottom
115 glViewport(0, 0, VBufWidth
*vbufEffScale
, VBufHeight
*vbufEffScale
);
116 glOrtho(0, VBufWidth
, VBufHeight
, 0, -1, 1); // top-to-bottom
117 glMatrixMode(GL_MODELVIEW
);
120 glEnable(GL_TEXTURE_2D
);
121 glDisable(GL_LIGHTING
);
122 glDisable(GL_DITHER
);
123 glDisable(GL_DEPTH_TEST
);
126 //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
127 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
128 //glColor4f(1, 1, 1, 1);
129 glBindTexture(GL_TEXTURE_2D
, vArrowTextureId
);
130 //scope(exit) glBindTexture(GL_TEXTURE_2D, 0);
132 glTexCoord2f(0.0f, 0.0f); glVertex2i(px
, py
); // top-left
133 glTexCoord2f(1.0f, 0.0f); glVertex2i(px
+16*2, py
); // top-right
134 glTexCoord2f(1.0f, 1.0f); glVertex2i(px
+16*2, py
+8*2); // bottom-right
135 glTexCoord2f(0.0f, 1.0f); glVertex2i(px
, py
+8*2); // bottom-left
141 // ////////////////////////////////////////////////////////////////////////// //
142 // resize buffer, reinitialize OpenGL texture
143 public void vglResizeBuffer (int wdt
, int hgt
, int ascale
=1) {
146 if (wdt
< 1) wdt
= 1;
147 if (hgt
< 1) hgt
= 1;
149 if (wdt
> 8192) wdt
= 8192;
150 if (hgt
> 8192) hgt
= 8192;
152 bool sizeChanged
= (wdt
!= VBufWidth || hgt
!= VBufHeight
);
156 if (vglTexBuf
is null || sizeChanged
) {
157 import core
.stdc
.stdlib
: realloc
;
158 vglTexBuf
= cast(uint*)realloc(vglTexBuf
, (VBufWidth
*VBufHeight
+4)*vglTexBuf
[0].sizeof
);
159 if (vglTexBuf
is null) { import core
.exception
: onOutOfMemoryErrorNoGC
; onOutOfMemoryErrorNoGC(); }
160 vglTexBuf
[0..VBufWidth
*VBufHeight
+4] = 0;
163 if (vglTexId
== 0 || sizeChanged
) {
164 enum wrapOpt
= GL_REPEAT
;
165 enum filterOpt
= GL_NEAREST
; //GL_LINEAR;
166 enum ttype
= GL_UNSIGNED_BYTE
;
168 if (vglTexId
) glDeleteTextures(1, &vglTexId
);
170 glGenTextures(1, &vglTexId
);
171 if (vglTexId
== 0) assert(0, "can't create OpenGL texture");
173 //GLint gltextbinding;
174 //glGetIntegerv(GL_TEXTURE_BINDING_2D, &gltextbinding);
175 //scope(exit) glBindTexture(GL_TEXTURE_2D, gltextbinding);
177 glBindTexture(GL_TEXTURE_2D
, vglTexId
);
178 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, wrapOpt
);
179 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, wrapOpt
);
180 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, filterOpt
);
181 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, filterOpt
);
182 //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
183 //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
185 //GLfloat[4] bclr = 0.0;
186 //glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, bclr.ptr);
188 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, VBufWidth
, VBufHeight
, 0, GLTexType
, GL_UNSIGNED_BYTE
, vglTexBuf
);
191 if (ascale
< 1) ascale
= 1;
192 if (ascale
> 32) ascale
= 32;
193 vbufEffScale
= cast(ubyte)ascale
;
197 // ////////////////////////////////////////////////////////////////////////// //
198 public void vglUpdateTexture () {
200 glBindTexture(GL_TEXTURE_2D
, vglTexId
);
201 glTexSubImage2D(GL_TEXTURE_2D
, 0, 0/*x*/, 0/*y*/, VBufWidth
, VBufHeight
, GLTexType
, GL_UNSIGNED_BYTE
, vglTexBuf
);
202 //glBindTexture(GL_TEXTURE_2D, 0);
207 // ////////////////////////////////////////////////////////////////////////// //
208 public void vglBlitTexture () {
210 glMatrixMode(GL_PROJECTION
); // for ortho camera
212 // left, right, bottom, top, near, far
213 //glViewport(0, 0, w*vbufEffScale, h*vbufEffScale);
214 //glOrtho(0, w, h, 0, -1, 1); // top-to-bottom
215 glViewport(0, 0, VBufWidth
*vbufEffScale
, VBufHeight
*vbufEffScale
);
216 glOrtho(0, VBufWidth
, VBufHeight
, 0, -1, 1); // top-to-bottom
217 glMatrixMode(GL_MODELVIEW
);
220 glEnable(GL_TEXTURE_2D
);
221 glDisable(GL_LIGHTING
);
222 glDisable(GL_DITHER
);
223 //glDisable(GL_BLEND);
224 glDisable(GL_DEPTH_TEST
);
225 //glEnable(GL_BLEND);
226 //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
227 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
229 //glDisable(GL_STENCIL_TEST);
232 immutable w
= VBufWidth
;
233 immutable h
= VBufHeight
;
235 glColor4f(1, 1, 1, 1);
236 glBindTexture(GL_TEXTURE_2D
, vglTexId
);
237 //scope(exit) glBindTexture(GL_TEXTURE_2D, 0);
239 glTexCoord2f(0.0f, 0.0f); glVertex2i(0, 0); // top-left
240 glTexCoord2f(1.0f, 0.0f); glVertex2i(w
, 0); // top-right
241 glTexCoord2f(1.0f, 1.0f); glVertex2i(w
, h
); // bottom-right
242 glTexCoord2f(0.0f, 1.0f); glVertex2i(0, h
); // bottom-left