1 (* coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 3 of the License ONLY.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
17 {$DEFINE FUI_TEXT_ICONS}
21 {$FATAL Desktop OpenGL required for current flexui implementation}
29 fui_common
, fui_events
;
32 // ////////////////////////////////////////////////////////////////////////// //
41 function charWidth (const ch
: AnsiChar): Integer; virtual; abstract;
42 function textWidth (const s
: AnsiString): Integer; virtual; abstract;
45 property name
: AnsiString read mName
;
46 property height
: Integer read mHeight
;
47 property baseLine
: Integer read mBaseLine
;
50 TGxContext
= class (fui_gfx
.TGxContext
)
54 // for active contexts
60 protected (* internal *)
61 procedure realizeClip (); // setup scissoring
62 procedure setClipOfs (const aofs
: TGxOfs
); // !!!
65 function setOffset (constref aofs
: TGxOfs
): TGxOfs
; // returns previous offset
66 function setClip (constref aclip
: TGxRect
): TGxRect
; // returns previous clip
67 procedure glSetScale (ascale
: Single);
68 procedure glSetTrans (ax
, ay
: Single);
69 procedure glSetScaleTrans (ascale
, ax
, ay
: Single);
72 function getFont (): AnsiString; override;
73 procedure setFont (const aname
: AnsiString); override;
75 procedure onActivate (); override;
76 procedure onDeactivate (); override;
78 function getColor (): TGxRGBA
; override;
79 procedure setColor (const clr
: TGxRGBA
); override;
81 function getClipRect (): TGxRect
; override;
82 procedure setClipRect (const aclip
: TGxRect
); override;
84 procedure setScale (a
: Single); override;
87 constructor Create ();
88 destructor Destroy (); override;
90 procedure line (x1
, y1
, x2
, y2
: Integer); override;
91 procedure hline (x
, y
, len
: Integer); override;
92 procedure vline (x
, y
, len
: Integer); override;
93 procedure rect (x
, y
, w
, h
: Integer); override;
94 procedure fillRect (x
, y
, w
, h
: Integer); override;
95 procedure darkenRect (x
, y
, w
, h
: Integer; a
: Integer); override;
97 function charWidth (const ch
: AnsiChar): Integer; override;
98 function charHeight (const ch
: AnsiChar): Integer; override;
99 function textWidth (const s
: AnsiString): Integer; override;
100 function textHeight (const s
: AnsiString): Integer; override;
101 function drawChar (x
, y
: Integer; const ch
: AnsiChar): Integer; override; // returns char width
102 function drawText (x
, y
: Integer; const s
: AnsiString): Integer; override; // returns text width
104 procedure resetClip (); override;
105 function combineClip (constref aclip
: TGxRect
): TGxRect
; override; // returns previous clip
108 property color
: TGxRGBA read mColor write setColor
;
109 property offset
: TGxOfs read mClipOfs write setClipOfs
;
110 property clip
: TGxRect read mClipRect write setClipRect
; // clipping is unaffected by offset
114 // setup 2D OpenGL mode; will be called automatically in `glInit()`
115 //procedure oglSetup2D (winWidth, winHeight: Integer; upsideDown: Boolean=false);
116 //procedure oglSetup2DState (); // don't modify viewports and matrices
118 //procedure oglDrawCursor ();
119 //procedure oglDrawCursorAt (msX, msY: Integer);
122 //procedure fuiGfxLoadFont (const fontname: AnsiString; const fontFile: AnsiString; proportional: Boolean=false);
123 //procedure fuiGfxLoadFont (const fontname: AnsiString; st: TStream; proportional: Boolean=false);
126 // ////////////////////////////////////////////////////////////////////////// //
128 gGfxDoClear
: Boolean = true;
134 {$INCLUDE ../nogl/noGLuses.inc}
140 // ////////////////////////////////////////////////////////////////////////// //
141 // returns `false` if the color is transparent
142 // returns `false` if the color is transparent
143 function setupGLColor (constref clr
: TGxRGBA
): Boolean;
145 if (clr
.a
< 255) then
148 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
154 glColor4ub(clr
.r
, clr
.g
, clr
.b
, clr
.a
);
155 result
:= (clr
.a
<> 0);
158 function isScaled (): Boolean;
160 mt
: packed array [0..15] of GLfloat
;
162 glGetFloatv(GL_MODELVIEW_MATRIX
, @mt
[0]);
163 result
:= (mt
[0] <> 1.0) or (mt
[1*4+1] <> 1.0);
167 // ////////////////////////////////////////////////////////////////////////// //
168 //TODO: OpenGL framebuffers and shaders state
170 TSavedGLState
= record
173 gltextbinding
: GLint
;
175 //oldfbr, oldfbw: GLint;
176 glvport
: packed array [0..3] of GLint
;
180 constructor Create (dosave
: Boolean);
182 procedure restore ();
185 constructor TSavedGLState
.Create (dosave
: Boolean);
187 FillChar(self
, sizeof(self
), 0);
188 if (dosave
) then save();
191 procedure TSavedGLState
.save ();
193 if (saved
) then raise Exception
.Create('cannot save into already saved OpenGL state');
194 glGetIntegerv(GL_MATRIX_MODE
, @glmatmode
);
195 glGetIntegerv(GL_TEXTURE_BINDING_2D
, @gltextbinding
);
196 glGetIntegerv(GL_VIEWPORT
, @glvport
[0]);
197 //glGetIntegerv(GL_CURRENT_PROGRAM, &oldprg);
198 //glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &oldfbr);
199 //glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &oldfbw);
200 glMatrixMode(GL_PROJECTION
); glPushMatrix();
201 glMatrixMode(GL_MODELVIEW
); glPushMatrix();
202 glMatrixMode(GL_TEXTURE
); glPushMatrix();
203 glMatrixMode(GL_COLOR
); glPushMatrix();
204 glPushAttrib({GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_CURRENT_BIT}GL_ALL_ATTRIB_BITS
); // let's play safe
208 procedure TSavedGLState
.restore ();
210 if (not saved
) then raise Exception
.Create('cannot restore unsaved OpenGL state');
211 glPopAttrib({GL_ENABLE_BIT});
212 glMatrixMode(GL_PROJECTION
); glPopMatrix();
213 glMatrixMode(GL_MODELVIEW
); glPopMatrix();
214 glMatrixMode(GL_TEXTURE
); glPopMatrix();
215 glMatrixMode(GL_COLOR
); glPopMatrix();
216 glMatrixMode(glmatmode
);
217 //if (glHasFunc!"glBindFramebufferEXT") glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, oldfbr);
218 //if (glHasFunc!"glBindFramebufferEXT") glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, oldfbw);
219 glBindTexture(GL_TEXTURE_2D
, gltextbinding
);
220 //if (glHasFunc!"glUseProgram") glUseProgram(oldprg);
221 glViewport(glvport
[0], glvport
[1], glvport
[2], glvport
[3]);
227 savedGLState
: TSavedGLState
;
229 procedure gxGLPreSetContextCallback
;
231 if (savedGLState
.saved
) then savedGLState
.restore();
234 function gxGLCreateContextCallback (): fui_gfx
.TGxContext
;
236 result
:= TGxContext
.Create();
239 // ////////////////////////////////////////////////////////////////////////// //
241 TScissorSave
= record
244 scxywh
: packed array[0..3] of GLint
;
249 procedure save (enableScissoring
: Boolean);
250 procedure restore ();
252 // set new scissor rect, bounded by the saved scissor rect
253 procedure combineRect (x
, y
, w
, h
: Integer);
257 procedure TScissorSave
.save (enableScissoring
: Boolean);
259 wassc
:= (glIsEnabled(GL_SCISSOR_TEST
) <> 0);
260 if wassc
then glGetIntegerv(GL_SCISSOR_BOX
, @scxywh
[0]) else glGetIntegerv(GL_VIEWPORT
, @scxywh
[0]);
261 //conwritefln('(%d,%d)-(%d,%d)', [scxywh[0], scxywh[1], scxywh[2], scxywh[3]]);
262 if enableScissoring
and (not wassc
) then glEnable(GL_SCISSOR_TEST
);
265 procedure TScissorSave
.restore ();
267 glScissor(scxywh
[0], scxywh
[1], scxywh
[2], scxywh
[3]);
268 if wassc
then glEnable(GL_SCISSOR_TEST
) else glDisable(GL_SCISSOR_TEST
);
271 procedure TScissorSave
.combineRect (x
, y
, w
, h
: Integer);
272 //var ox, oy, ow, oh: Integer;
274 if (w
< 1) or (h
< 1) then begin glScissor(0, 0, 0, 0); exit
; end;
275 y
:= fuiScrHgt
-(y
+h
);
276 //ox := x; oy := y; ow := w; oh := h;
277 if not intersectRect(x
, y
, w
, h
, scxywh
[0], scxywh
[1], scxywh
[2], scxywh
[3]) then
279 //writeln('oops: COMBINE: old=(', ox, ',', oy, ')-(', ox+ow-1, ',', oy+oh-1, '); sci: (', scxywh[0], ',', scxywh[1], ')-(', scxywh[0]+scxywh[2]-1, ',', scxywh[1]+scxywh[3]-1, ')');
280 //writeln('oops: COMBINE: oldx=<', ox, '-', ox+ow-1, '>; oldy=<', oy, ',', oy+oh-1, '> : scix=<', scxywh[0], '-', scxywh[0]+scxywh[2]-1, '>; sciy=<', scxywh[1], '-', scxywh[1]+scxywh[3]-1, '>');
281 glScissor(0, 0, 0, 0);
285 glScissor(x
, y
, w
, h
);
290 // ////////////////////////////////////////////////////////////////////////// //
292 TGxBmpFont
= class(TGxFont
)
294 mTexId
: GLuint
; // OpenGL texture id
295 mWidth
: Integer; // <=0: proportional
298 mFreeFontWdt
: Boolean;
299 mFreeFontBmp
: Boolean;
302 procedure oglCreateTexture ();
303 procedure oglDestroyTexture ();
305 procedure initDrawText ();
306 procedure doneDrawText ();
307 function drawCharInterim (x
, y
: Integer; const ch
: AnsiChar): Integer; // return width (not including last empty pixel)
308 function drawCharInternal (x
, y
: Integer; const ch
: AnsiChar): Integer; // return width (not including last empty pixel)
309 function drawTextInternal (x
, y
: Integer; const s
: AnsiString): Integer; // return width (not including last empty pixel)
312 constructor Create (const aname
: AnsiString; st
: TStream
; proportional
: Boolean);
313 destructor Destroy (); override;
315 function charWidth (const ch
: AnsiChar): Integer; override;
316 function textWidth (const s
: AnsiString): Integer; override;
320 constructor TGxBmpFont
.Create (const aname
: AnsiString; st
: TStream
; proportional
: Boolean);
322 sign
: packed array [0..7] of AnsiChar;
323 enc
: packed array [0..16] of AnsiChar;
325 wdt
, hgt
, elen
: Integer;
330 mFreeFontBmp
:= true;
331 mFreeFontWdt
:= true;
335 st
.ReadBuffer(sign
[0], 8);
336 if (sign
<> 'FUIFONT0') then raise Exception
.Create('FlexUI: invalid font file signature');
337 // encoding length and width
339 wdt
:= (b
and $0f)+1; // 16 is not supported
340 if (wdt
= 16) then raise Exception
.Create('FlexUI: 16-wdt fonts aren''t supported yet');
341 elen
:= ((b
shr 4) and $0f);
342 if (elen
= 0) then raise Exception
.CreateFmt('FlexUI: invalid font encoding length: %d', [elen
]);
346 if (hgt
< 2) then raise Exception
.CreateFmt('FlexUI: invalid font height: %d', [hgt
]);
348 st
.ReadBuffer(enc
[0], elen
);
349 // check for 'cp1251' here (it can also be 'koi8')
350 if (wdt
<= 8) then fntbwdt
:= 1 else fntbwdt
:= 2;
351 // shift and width table (hi nibble: left shift for proportional print; lo nibble: shifted character width for proportional print)
352 GetMem(mFontWdt
, 256);
353 st
.ReadBuffer(mFontWdt
^, 256);
355 GetMem(mFontBmp
, (hgt
*fntbwdt
)*256);
356 st
.ReadBuffer(mFontBmp
^, (hgt
*fntbwdt
)*256);
359 mBaseLine
:= hgt
-1; //FIXME
360 if (proportional
) then
363 for ch
:= 0 to 255 do
365 for dy
:= 0 to hgt
-1 do
367 if (fntbwdt
= 1) then
369 mFontBmp
[ch
*hgt
+dy
] := mFontBmp
[ch
*hgt
+dy
] shl (mFontWdt
[ch
] shr 4);
373 wrd
:= mFontBmp
[ch
*(hgt
*2)+(dy
*2)]+256*mFontBmp
[ch
*(hgt
*2)+(dy
*2)+1];
374 wrd
:= wrd
shl (mFontWdt
[ch
] shr 4);
375 mFontBmp
[ch
*(hgt
*2)+(dy
*2)+0] := (wrd
and $ff);
376 mFontBmp
[ch
*(hgt
*2)+(dy
*2)+1] := ((wrd
shr 16) and $ff);
383 FillChar(mFontWdt
^, 256, wdt
);
388 destructor TGxBmpFont
.Destroy ();
390 if (mFreeFontBmp
) and (mFontBmp
<> nil) then FreeMem(mFontBmp
);
391 if (mFreeFontWdt
) and (mFontWdt
<> nil) then FreeMem(mFontWdt
);
398 mFreeFontWdt
:= false;
399 mFreeFontBmp
:= false;
405 procedure TGxBmpFont
.oglCreateTexture ();
413 x
, y
, dx
, dy
: Integer;
415 GetMem(tex
, TxWidth
*TxHeight
*4);
416 FillChar(tex
^, TxWidth
*TxHeight
*4, 0);
418 for cc
:= 0 to 255 do
422 for dy
:= 0 to mHeight
-1 do
424 if (mWidth
<= 8) then b
:= mFontBmp
[cc
*mHeight
+dy
] else b
:= mFontBmp
[cc
*(mHeight
*2)+(dy
*2)+1];
425 //if prop then b := b shl (fontwdt[cc] shr 4);
426 tpp
:= tex
+((y
+dy
)*(TxWidth
*4))+x
*4;
429 if ((b
and $80) <> 0) then
431 tpp
^ := 255; Inc(tpp
);
432 tpp
^ := 255; Inc(tpp
);
433 tpp
^ := 255; Inc(tpp
);
434 tpp
^ := 255; Inc(tpp
);
443 b
:= (b
and $7f) shl 1;
447 b
:= mFontBmp
[cc
*(mHeight
*2)+(dy
*2)+0];
450 if ((b
and $80) <> 0) then
452 tpp
^ := 255; Inc(tpp
);
453 tpp
^ := 255; Inc(tpp
);
454 tpp
^ := 255; Inc(tpp
);
455 tpp
^ := 255; Inc(tpp
);
464 b
:= (b
and $7f) shl 1;
470 glGenTextures(1, @mTexId
);
471 if (mTexId
= 0) then raise Exception
.Create('can''t create FlexUI font texture');
473 glBindTexture(GL_TEXTURE_2D
, mTexId
);
474 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
475 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
476 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
477 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
479 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, TxWidth
, TxHeight
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex
);
482 glBindTexture(GL_TEXTURE_2D
, 0);
487 procedure TGxBmpFont
.oglDestroyTexture ();
489 if (mTexId
<> 0) then
491 glDeleteTextures(1, @mTexId
);
497 function TGxBmpFont
.charWidth (const ch
: AnsiChar): Integer;
499 result
:= (mFontWdt
[Byte(ch
)] and $0f);
503 function TGxBmpFont
.textWidth (const s
: AnsiString): Integer;
507 if (Length(s
) > 0) then
510 for ch
in s
do result
+= (mFontWdt
[Byte(ch
)] and $0f)+1;
519 procedure TGxBmpFont
.initDrawText ();
521 glEnable(GL_ALPHA_TEST
);
522 glAlphaFunc(GL_NOTEQUAL
, 0.0);
523 glEnable(GL_TEXTURE_2D
);
524 glBindTexture(GL_TEXTURE_2D
, mTexId
);
528 procedure TGxBmpFont
.doneDrawText ();
530 glDisable(GL_ALPHA_TEST
);
531 glDisable(GL_TEXTURE_2D
);
532 glBindTexture(GL_TEXTURE_2D
, 0);
536 function TGxBmpFont
.drawCharInterim (x
, y
: Integer; const ch
: AnsiChar): Integer;
540 tx
:= (Integer(ch
) mod 16)*16;
541 ty
:= (Integer(ch
) div 16)*16;
543 glTexCoord2f((tx
+0)/256.0, (ty
+0)/256.0); glVertex2i(x
+0, y
+0); // top-left
544 glTexCoord2f((tx
+mWidth
)/256.0, (ty
+0)/256.0); glVertex2i(x
+mWidth
, y
+0); // top-right
545 glTexCoord2f((tx
+mWidth
)/256.0, (ty
+mHeight
)/256.0); glVertex2i(x
+mWidth
, y
+mHeight
); // bottom-right
546 glTexCoord2f((tx
+0)/256.0, (ty
+mHeight
)/256.0); glVertex2i(x
+0, y
+mHeight
); // bottom-left
548 result
:= (mFontWdt
[Byte(ch
)] and $0f);
552 function TGxBmpFont
.drawCharInternal (x
, y
: Integer; const ch
: AnsiChar): Integer;
555 result
:= drawCharInterim(x
, y
, ch
);
560 function TGxBmpFont
.drawTextInternal (x
, y
: Integer; const s
: AnsiString): Integer;
565 if (Length(s
) = 0) then begin result
:= 0; exit
; end;
570 wdt
:= drawCharInterim(x
, y
, ch
)+1;
578 // ////////////////////////////////////////////////////////////////////////// //
580 fontList
: array of TGxBmpFont
= nil;
581 defaultFontName
: AnsiString = 'win14';
584 function strEquCI (const s0
, s1
: AnsiString): Boolean;
589 result
:= (Length(s0
) = Length(s1
));
592 for f
:= 1 to Length(s0
) do
595 if (c0
>= 'a') and (c0
<= 'z') then Dec(c0
, 32); // poor man's `toupper()`
597 if (c1
>= 'a') and (c1
<= 'z') then Dec(c1
, 32); // poor man's `toupper()`
598 if (c0
<> c1
) then begin result
:= false; exit
; end;
604 function getFontByName (const aname
: AnsiString): TGxBmpFont
;
609 if (Length(fontList
) = 0) then raise Exception
.Create('font subsystem not initialized');
610 if (Length(aname
) = 0) or (strEquCI(aname
, 'default')) then fname
:= defaultFontName
else fname
:= aname
;
611 for f
:= 0 to High(fontList
) do
613 result
:= fontList
[f
];
614 if (result
= nil) then continue
;
615 if (strEquCI(result
.name
, fname
)) then exit
;
617 if (fontList
[0] = nil) then raise Exception
.Create('font subsystem not properly initialized');
618 result
:= fontList
[0];
623 procedure deleteFonts ();
627 for f := 0 to High(fontList) do freeAndNil(fontList[f]);
633 procedure fuiGfxLoadFont (const fontname
: AnsiString; st
: TStream
; proportional
: Boolean=false);
635 fnt
: TGxBmpFont
= nil;
638 if (Length(fontname
) = 0) then raise Exception
.Create('FlexUI: cannot load nameless font');
639 fnt
:= TGxBmpFont
.Create(fontname
, st
, proportional
);
641 for f
:= 0 to High(fontList
) do
643 if (strEquCI(fontList
[f
].name
, fontname
)) then
645 if (fontList
[f
].mTexId
<> 0) then raise Exception
.Create('FlexUI: cannot reload generated font named '''+fontname
+'''');
646 FreeAndNil(fontList
[f
]);
651 SetLength(fontList
, Length(fontList
)+1);
652 fontList
[High(fontList
)] := fnt
;
660 procedure fuiGfxLoadFont (const fontname
: AnsiString; const fontFile
: AnsiString; proportional
: Boolean=false);
664 if (Length(fontname
) = 0) then raise Exception
.Create('FlexUI: cannot load nameless font '''+fontFile
+'''');
665 st
:= fuiOpenFile(fontFile
);
666 if (st
= nil) then raise Exception
.Create('FlexUI: cannot load font '''+fontFile
+'''');
668 fuiGfxLoadFont(fontname
, st
, proportional
);
669 except on e
: Exception
do
671 writeln('FlexUI font loadin error: ', e
.message);
673 raise Exception
.Create('FlexUI: cannot load font '''+fontFile
+'''');
682 procedure oglInitFonts ();
686 for f
:= 0 to High(fontList
) do if (fontList
[f
] <> nil) then fontList
[f
].oglCreateTexture();
690 procedure oglDeinitFonts ();
694 for f
:= 0 to High(fontList
) do if (fontList
[f
] <> nil) then fontList
[f
].oglDestroyTexture();
698 // ////////////////////////////////////////////////////////////////////////// //
699 procedure oglSetup2DState ();
702 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
703 glDisable(GL_LINE_SMOOTH
);
704 glDisable(GL_POLYGON_SMOOTH
);
705 glDisable(GL_POINT_SMOOTH
);
706 glDisable(GL_DEPTH_TEST
);
707 glDisable(GL_TEXTURE_2D
);
708 glDisable(GL_LIGHTING
);
709 glDisable(GL_DITHER
);
710 glDisable(GL_STENCIL_TEST
);
711 glDisable(GL_SCISSOR_TEST
);
712 glDisable(GL_CULL_FACE
);
713 glDisable(GL_ALPHA_TEST
);
715 glClearColor(0, 0, 0, 0);
716 glColor4f(1, 1, 1, 1);
720 procedure oglSetup2D (winWidth
, winHeight
: Integer; upsideDown
: Boolean=false);
722 glViewport(0, 0, winWidth
, winHeight
);
726 glMatrixMode(GL_TEXTURE
);
729 glMatrixMode(GL_COLOR
);
732 glMatrixMode(GL_PROJECTION
);
736 glOrtho(0, winWidth
, 0, winHeight
, -1, 1); // set origin to bottom left
740 glOrtho(0, winWidth
, winHeight
, 0, -1, 1); // set origin to top left
743 glMatrixMode(GL_MODELVIEW
);
748 // ////////////////////////////////////////////////////////////////////////// //
749 {$INCLUDE r_fui_gfx_gl_cursor.inc}
751 procedure oglDrawCursor (); begin oglDrawCursorAt(fuiMouseX
, fuiMouseY
); end;
754 // ////////////////////////////////////////////////////////////////////////// //
755 constructor TGxContext
.Create ();
758 mColor
:= TGxRGBA
.Create(255, 255, 255);
759 mFont
:= getFontByName('default');
762 mClipRect
:= TGxRect
.Create(0, 0, 8192, 8192);
763 mClipOfs
:= TGxOfs
.Create(0, 0);
767 destructor TGxContext
.Destroy ();
769 if self
.active
then gxSetContext(nil);
774 function TGxContext
.getFont (): AnsiString;
776 result
:= mFont
.name
;
779 procedure TGxContext
.setFont (const aname
: AnsiString);
781 mFont
:= getFontByName(aname
);
785 procedure TGxContext
.onActivate ();
788 setupGLColor(mColor
);
792 procedure TGxContext
.onDeactivate ();
797 function TGxContext
.getColor (): TGxRGBA
;
802 procedure TGxContext
.setColor (const clr
: TGxRGBA
);
805 if self
.active
then setupGLColor(mColor
);
809 procedure TGxContext
.realizeClip ();
811 sx
, sy
, sw
, sh
: Integer;
813 if not self
.active
then exit
; // just in case
814 if (mClipRect
.w
<= 0) or (mClipRect
.h
<= 0) then
816 glEnable(GL_SCISSOR_TEST
);
817 glScissor(0, 0, 0, 0);
823 sx
:= trunc(mClipRect
.x
*mScale
);
824 sy
:= trunc(mClipRect
.y
*mScale
);
825 sw
:= trunc(mClipRect
.w
*mScale
);
826 sh
:= trunc(mClipRect
.h
*mScale
);
835 if (not intersectRect(sx
, sy
, sw
, sh
, 0, 0, fuiScrWdt
, fuiScrHgt
)) then
837 glEnable(GL_SCISSOR_TEST
);
838 glScissor(0, 0, 0, 0);
840 else if (sx
= 0) and (sy
= 0) and (sw
= fuiScrWdt
) and (sh
= fuiScrHgt
) then
842 glDisable(GL_SCISSOR_TEST
);
846 glEnable(GL_SCISSOR_TEST
);
847 sy
:= fuiScrHgt
-(sy
+sh
);
848 glScissor(sx
, sy
, sw
, sh
);
854 procedure TGxContext
.resetClip ();
856 mClipRect
:= TGxRect
.Create(0, 0, 8192, 8192);
857 if self
.active
then realizeClip();
861 procedure TGxContext
.setClipOfs (const aofs
: TGxOfs
);
867 function TGxContext
.getClipRect (): TGxRect
;
872 procedure TGxContext
.setClipRect (const aclip
: TGxRect
);
875 if self
.active
then realizeClip();
879 function TGxContext
.setOffset (constref aofs
: TGxOfs
): TGxOfs
;
886 function TGxContext
.setClip (constref aclip
: TGxRect
): TGxRect
;
890 if self
.active
then realizeClip();
894 function TGxContext
.combineClip (constref aclip
: TGxRect
): TGxRect
;
897 mClipRect
.intersect(aclip
);
898 if self
.active
then realizeClip();
902 procedure TGxContext
.line (x1
, y1
, x2
, y2
: Integer);
904 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
906 if (not mScaled
) then
910 glVertex2f(x1
+0.375, y1
+0.375);
911 glVertex2f(x2
+0.375, y2
+0.375);
914 if (x1
<> x2
) or (y1
<> y2
) then
918 glVertex2f(x2
+0.375, y2
+0.375);
930 glVertex2i(x2
+1, y2
+1);
936 procedure TGxContext
.hline (x
, y
, len
: Integer);
938 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
939 if (len
< 1) then exit
;
940 if (not mScaled
) then
944 glVertex2f(x
+0.375, y
+0.375);
945 glVertex2f(x
+len
+0.375, y
+0.375);
948 else if (mScale
> 1.0) then
952 glVertex2i(x
+len
, y
);
953 glVertex2i(x
+len
, y
+1);
961 while (len
> 0) do begin glVertex2i(x
, y
); Inc(x
); Dec(len
); end;
967 procedure TGxContext
.vline (x
, y
, len
: Integer);
969 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
970 if (len
< 1) then exit
;
971 if (not mScaled
) then
975 glVertex2f(x
+0.375, y
+0.375);
976 glVertex2f(x
+0.375, y
+len
+0.375);
979 else if (mScale
> 1.0) then
983 glVertex2i(x
, y
+len
);
984 glVertex2i(x
+1, y
+len
);
992 while (len
> 0) do begin glVertex2i(x
, y
); Inc(y
); Dec(len
); end;
998 procedure TGxContext
.rect (x
, y
, w
, h
: Integer);
1000 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1001 if (w
< 0) or (h
< 0) then exit
;
1002 if (w
= 1) and (h
= 1) then
1006 if mScaled
then glVertex2i(x
, y
) else glVertex2f(x
+0.375, y
+0.375);
1011 if (not mScaled
) then
1015 glVertex2i(x
, y
); glVertex2i(x
+w
, y
); // top
1016 glVertex2i(x
, y
+h
-1); glVertex2i(x
+w
, y
+h
-1); // bottom
1017 glVertex2f(x
+0.375, y
+1); glVertex2f(x
+0.375, y
+h
-1); // left
1018 glVertex2f(x
+w
-1+0.375, y
+1); glVertex2f(x
+w
-1+0.375, y
+h
-1); // right
1026 vline(x
+w
-1, y
+1, h
-2);
1032 procedure TGxContext
.fillRect (x
, y
, w
, h
: Integer);
1034 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1035 if (w
< 0) or (h
< 0) then exit
;
1039 glVertex2f(x
+w
, y
+h
);
1045 procedure TGxContext
.darkenRect (x
, y
, w
, h
: Integer; a
: Integer);
1047 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (a
>= 255) then exit
;
1048 if (w
< 0) or (h
< 0) then exit
;
1049 if (a
< 0) then a
:= 0;
1051 glBlendFunc(GL_ZERO
, GL_SRC_ALPHA
);
1052 glColor4f(0.0, 0.0, 0.0, a
/255.0);
1056 glVertex2i(x
+w
, y
+h
);
1059 setupGLColor(mColor
);
1063 function TGxContext
.charWidth (const ch
: AnsiChar): Integer;
1065 result
:= mFont
.charWidth(ch
);
1068 function TGxContext
.charHeight (const ch
: AnsiChar): Integer;
1070 result
:= mFont
.height
;
1074 function TGxContext
.textWidth (const s
: AnsiString): Integer;
1076 result
:= mFont
.textWidth(s
);
1079 function TGxContext
.textHeight (const s
: AnsiString): Integer;
1081 result
:= mFont
.height
;
1085 function TGxContext
.drawChar (x
, y
: Integer; const ch
: AnsiChar): Integer; // returns char width
1087 result
:= mFont
.charWidth(ch
);
1088 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) then exit
;
1089 TGxBmpFont(mFont
).drawCharInternal(x
, y
, ch
);
1092 function TGxContext
.drawText (x
, y
: Integer; const s
: AnsiString): Integer; // returns text width
1094 result
:= mFont
.textWidth(s
);
1095 if (not self
.active
) or (mClipRect
.w
< 1) or (mClipRect
.h
< 1) or (mColor
.a
= 0) or (Length(s
) = 0) then exit
;
1096 TGxBmpFont(mFont
).drawTextInternal(x
, y
, s
);
1100 procedure TGxContext
.setScale (a
: Single);
1105 procedure TGxContext
.glSetScale (ascale
: Single);
1107 if (ascale
< 0.01) then ascale
:= 0.01;
1109 glScalef(ascale
, ascale
, 1.0);
1111 mScaled
:= (ascale
<> 1.0);
1114 procedure TGxContext
.glSetTrans (ax
, ay
: Single);
1117 glScalef(mScale
, mScale
, 1.0);
1118 glTranslatef(ax
, ay
, 0);
1122 procedure TGxContext
.glSetScaleTrans (ascale
, ax
, ay
: Single);
1125 glTranslatef(ax
, ay
, 0);
1129 // ////////////////////////////////////////////////////////////////////////// //
1131 procedure oglRestoreMode (doClear: Boolean);
1133 oglSetup2D(fuiScrWdt, fuiScrHgt);
1134 glScissor(0, 0, fuiScrWdt, fuiScrHgt);
1136 glBindTexture(GL_TEXTURE_2D, 0);
1137 glDisable(GL_BLEND);
1138 glDisable(GL_TEXTURE_2D);
1139 glDisable(GL_STENCIL_TEST);
1140 glDisable(GL_SCISSOR_TEST);
1141 glDisable(GL_LIGHTING);
1142 glDisable(GL_DEPTH_TEST);
1143 glDisable(GL_CULL_FACE);
1144 glDisable(GL_LINE_SMOOTH);
1145 glDisable(GL_POINT_SMOOTH);
1148 glColor4f(1, 1, 1, 1);
1152 glClearColor(0, 0, 0, 0);
1153 glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT or GL_ACCUM_BUFFER_BIT or GL_STENCIL_BUFFER_BIT);
1157 glMatrixMode(GL_MODELVIEW);
1159 //glScalef(4, 4, 1);
1164 //procedure onWinFocus (); begin uiFocus(); end;
1165 //procedure onWinBlur (); begin fuiResetKMState(true); uiBlur(); end;
1167 //procedure onPreRender (); begin oglRestoreMode(gGfxDoClear); end;
1168 procedure onPostRender (); begin oglDrawCursor(); end;
1170 procedure onInit ();
1172 //oglSetup2D(fuiScrWdt, fuiScrHgt);
1173 createCursorTexture();
1177 procedure onDeinit ();
1179 fuiResetKMState(false);
1180 if (curtexid
<> 0) then glDeleteTextures(1, @curtexid
);
1190 // ////////////////////////////////////////////////////////////////////////// //
1192 savedGLState
:= TSavedGLState
.Create(false);
1194 //winFocusCB := onWinFocus;
1195 //winBlurCB := onWinBlur;
1196 //prerenderFrameCB := onPreRender;
1197 postrenderFrameCB
:= onPostRender
;
1198 oglInitCB
:= onInit
;
1199 oglDeinitCB
:= onDeinit
;
1201 gxPreSetContextCallback
:= gxGLPreSetContextCallback
;
1202 gxCreateContextCallback
:= gxGLCreateContextCallback
;
1203 gxFuiGfxLoadFontCallback
:= fuiGfxLoadFont
;