55d9dad6432db0bc2d1832aa6a3fc8af3fee831c
1 (* Copyright (C) Doom 2D: Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 {$INCLUDE ../../../shared/a_modes.inc}
25 procedure r_Draw_Texture (img
: TGLTexture
; x
, y
, w
, h
: Integer; flip
: Boolean; r
, g
, b
, a
: Byte; blend
: Boolean);
26 procedure r_Draw_TextureRepeat (img
: TGLTexture
; x
, y
, w
, h
: Integer; flip
: Boolean; r
, g
, b
, a
: Byte; blend
: Boolean);
27 procedure r_Draw_TextureRepeatRotate (img
: TGLTexture
; x
, y
, w
, h
: Integer; flip
: Boolean; r
, g
, b
, a
: Byte; blend
: Boolean; rx
, ry
, angle
: Integer);
29 procedure r_Draw_MultiTextureRepeat (m
: TGLMultiTexture
; const anim
: TAnimState
; backanim
: Boolean; x
, y
, w
, h
: Integer; flip
: Boolean; r
, g
, b
, a
: Byte; blend
: Boolean);
30 procedure r_Draw_MultiTextureRepeatRotate (m
: TGLMultiTexture
; const anim
: TAnimState
; backanim
: Boolean; x
, y
, w
, h
: Integer; flip
: Boolean; r
, g
, b
, a
: Byte; blend
: Boolean; rx
, ry
, angle
: Integer);
32 procedure r_Draw_Filter (l
, t
, r
, b
: Integer; rr
, gg
, bb
, aa
: Byte);
33 procedure r_Draw_Rect (l
, t
, r
, b
: Integer; rr
, gg
, bb
, aa
: Byte);
34 procedure r_Draw_FillRect (l
, t
, r
, b
: Integer; rr
, gg
, bb
, aa
: Byte);
35 procedure r_Draw_InvertRect (l
, t
, r
, b
: Integer; rr
, gg
, bb
, aa
: Byte);
37 procedure r_Draw_Text (const text: AnsiString; x
, y
: Integer; r
, g
, b
, a
: Byte; f
: TGLFont
);
38 procedure r_Draw_GetTextSize (const text: AnsiString; f
: TGLFont
; out w
, h
: Integer);
40 procedure r_Draw_Setup (w
, h
: Integer);
41 procedure r_Draw_SetRect (l
, t
, r
, b
: Integer);
42 procedure r_Draw_GetRect (out l
, t
, r
, b
: Integer);
52 SysUtils
, Classes
, Math
,
63 sl
, st
, sr
, sb
: Integer;
64 ScreenWidth
, ScreenHeight
: Integer;
66 procedure r_Draw_Setup (w
, h
: Integer);
72 glScissor(0, 0, w
, h
);
73 glViewport(0, 0, w
, h
);
74 glMatrixMode(GL_PROJECTION
);
76 glOrtho(0, w
, h
, 0, 0, 1);
77 glMatrixMode(GL_MODELVIEW
);
79 glEnable(GL_SCISSOR_TEST
);
80 r_Draw_SetRect(0, 0, w
- 1, h
- 1);
83 procedure DrawQuad (x
, y
, w
, h
: Integer);
89 glVertex2i(x
+ w
, y
+ h
);
93 procedure DrawTile (tile
: TGLAtlasNode
; x
, y
, w
, h
: Integer; flip
: Boolean; rr
, gg
, bb
, aa
: Byte; blend
: Boolean);
94 var nw
, nh
, ax
, bx
, ay
, by
: GLfloat
; l
, t
, r
, b
: Integer;
98 glColor4ub(rr
, gg
, bb
, aa
);
99 if blend
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
) else glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
100 glDisable(GL_TEXTURE_2D
);
102 DrawQuad(x
, y
, w
, h
);
108 ax
:= IfThen(flip
, tile
.l
, tile
.r
+ 1) / nw
;
109 bx
:= IfThen(flip
, tile
.r
+ 1, tile
.l
) / nh
;
111 by
:= (tile
.b
+ 1) / nh
;
112 l
:= x
; t
:= y
; r
:= x
+ w
; b
:= y
+ h
;
113 glBindTexture(GL_TEXTURE_2D
, tile
.id
);
114 glColor4ub(rr
, gg
, bb
, aa
);
115 if blend
then glBlendFunc(GL_SRC_ALPHA
, GL_ONE
) else glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
116 glEnable(GL_TEXTURE_2D
);
119 glTexCoord2f(ax
, ay
); glVertex2i(r
, t
);
120 glTexCoord2f(bx
, ay
); glVertex2i(l
, t
);
121 glTexCoord2f(bx
, by
); glVertex2i(l
, b
);
122 glTexCoord2f(ax
, by
); glVertex2i(r
, b
);
124 glDisable(GL_TEXTURE_2D
);
125 glBindTexture(GL_TEXTURE_2D
, 0);
129 procedure r_Draw_Texture (img
: TGLTexture
; x
, y
, w
, h
: Integer; flip
: Boolean; r
, g
, b
, a
: Byte; blend
: Boolean);
130 var i
, j
, offx
, offy
: Integer; n
: TGLAtlasNode
;
135 DrawTile(nil, x
, y
, w
, h
, flip
, NTR
, NTB
, NTG
, NTA
, blend
)
140 for j
:= 0 to img
.lines
- 1 do
142 for i
:= 0 to img
.cols
- 1 do
144 n
:= img
.GetTile(i
, j
);
147 glTranslatef(x
+ offx
, y
+ offy
, 0);
148 glScalef(w
/ img
.width
, h
/ img
.height
, 1);
149 DrawTile(n
, 0, 0, n
.width
, n
.height
, flip
, r
, g
, b
, a
, blend
);
151 offx
:= offx
+ n
.width
;
154 offy
:= offy
+ n
.height
;
159 procedure r_Draw_TextureRepeat (img
: TGLTexture
; x
, y
, w
, h
: Integer; flip
: Boolean; r
, g
, b
, a
: Byte; blend
: Boolean);
165 r_Draw_Texture(nil, x
, y
, w
, h
, flip
, NTR
, NTG
, NTB
, NTB
, blend
)
167 for j
:= 0 to (h
- 1) div img
.height
do
168 for i
:= 0 to (w
- 1) div img
.width
do
169 r_Draw_Texture(img
, x
+ i
* img
.width
, y
+ j
* img
.height
, img
.width
, img
.height
, flip
, r
, g
, b
, a
, blend
);
172 procedure r_Draw_TextureRepeatRotate (img
: TGLTexture
; x
, y
, w
, h
: Integer; flip
: Boolean; r
, g
, b
, a
: Byte; blend
: Boolean; rx
, ry
, angle
: Integer);
179 glTranslatef(x
+ rx
, y
+ ry
, 0);
180 glRotatef(angle
, 0, 0, 1);
181 glTranslatef(-(x
+ rx
), -(y
+ ry
), 0);
182 r_Draw_TextureRepeat(img
, x
, y
, w
, h
, flip
, r
, g
, b
, a
, blend
);
186 r_Draw_TextureRepeat(img
, x
, y
, w
, h
, flip
, r
, g
, b
, a
, blend
);
189 procedure r_Draw_MultiTextureRepeat (m
: TGLMultiTexture
; const anim
: TAnimState
; backanim
: Boolean; x
, y
, w
, h
: Integer; flip
: Boolean; r
, g
, b
, a
: Byte; blend
: Boolean);
190 var img
: TGLTexture
; frame
: LongInt;
192 ASSERT(anim
.IsValid());
194 r_Draw_TextureRepeat(nil, x
, y
, w
, h
, flip
, NTR
, NTG
, NTB
, NTB
, blend
)
197 g_Anim_GetFrameFromState(anim
, backanim
, frame
);
199 ASSERT(frame
< m
.count
);
200 img
:= m
.GetTexture(frame
);
201 r_Draw_TextureRepeat(img
, x
, y
, w
, h
, flip
, r
, g
, b
, a
, blend
);
205 procedure r_Draw_MultiTextureRepeatRotate (m
: TGLMultiTexture
; const anim
: TAnimState
; backanim
: Boolean; x
, y
, w
, h
: Integer; flip
: Boolean; r
, g
, b
, a
: Byte; blend
: Boolean; rx
, ry
, angle
: Integer);
212 glTranslatef(x
+ rx
, y
+ ry
, 0);
213 glRotatef(angle
, 0, 0, 1);
214 glTranslatef(-(x
+ rx
), -(y
+ ry
), 0);
215 r_Draw_MultiTextureRepeat(m
, anim
, backanim
, x
, y
, w
, h
, flip
, r
, g
, b
, a
, blend
);
219 r_Draw_MultiTextureRepeat(m
, anim
, backanim
, x
, y
, w
, h
, flip
, r
, g
, b
, a
, blend
);
222 procedure r_Draw_Filter (l
, t
, r
, b
: Integer; rr
, gg
, bb
, aa
: Byte);
227 glBlendFunc(GL_ZERO
, GL_SRC_COLOR
);
228 glDisable(GL_TEXTURE_2D
);
229 glColor4ub(rr
, gg
, bb
, aa
);
238 procedure r_Draw_Rect (l
, t
, r
, b
: Integer; rr
, gg
, bb
, aa
: Byte);
243 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
244 glDisable(GL_TEXTURE_2D
);
245 glColor4ub(rr
, gg
, bb
, aa
);
246 glBegin(GL_LINE_LOOP
);
253 glVertex2f(l
+ 0.5, t
+ 0.5);
254 glVertex2f(r
- 0.5, t
+ 0.5);
255 glVertex2f(r
- 0.5, b
- 0.5);
256 glVertex2f(l
+ 0.5, b
- 0.5);
260 procedure r_Draw_FillRect (l
, t
, r
, b
: Integer; rr
, gg
, bb
, aa
: Byte);
265 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
266 glDisable(GL_TEXTURE_2D
);
267 glColor4ub(rr
, gg
, bb
, aa
);
276 glVertex2f(l + 0.5, t + 0.5);
277 glVertex2f(r - 0.5, t + 0.5);
278 glVertex2f(r - 0.5, b - 0.5);
279 glVertex2f(l + 0.5, b - 0.5);
281 glVertex2f(l
+ 0, t
+ 0);
282 glVertex2f(r
+ 0.75, t
+ 0);
283 glVertex2f(r
+ 0.75, b
+ 0.75);
284 glVertex2f(l
+ 0, b
+ 0.75);
288 procedure r_Draw_InvertRect (l
, t
, r
, b
: Integer; rr
, gg
, bb
, aa
: Byte);
293 glBlendFunc(GL_ONE_MINUS_DST_COLOR
, GL_ZERO
);
294 glDisable(GL_TEXTURE_2D
);
295 glColor4ub(rr
, gg
, bb
, aa
);
304 procedure r_Draw_Text (const text: AnsiString; x
, y
: Integer; r
, g
, b
, a
: Byte; f
: TGLFont
);
305 var i
, xoff
, spc
: Integer; t
: TGLTexture
; ch
: AnsiChar;
307 xoff
:= x
; spc
:= MAX(0, f
.GetSpace());
308 for i
:= 1 to Length(text) do
313 r_Draw_Texture(t
, xoff
, y
, t
.width
, t
.height
, false, r
, g
, b
, a
, false);
314 Inc(xoff
, f
.GetWidth(ch
) + spc
);
318 procedure r_Draw_GetTextSize (const text: AnsiString; f
: TGLFont
; out w
, h
: Integer);
319 var i
, spc
, len
: Integer;
322 h
:= f
.GetMaxHeight();
326 spc
:= MAX(0, f
.GetSpace());
327 for i
:= 1 to len
- 1 do
328 Inc(w
, f
.GetWidth(text[i
]) + spc
);
329 Inc(w
, f
.GetWidth(text[len
]));
333 procedure r_Draw_SetRect (l
, t
, r
, b
: Integer);
340 glScissor(l
, ScreenHeight
- h
- t
, w
, h
);
341 sl
:= l
; st
:= t
; sr
:= r
; sb
:= b
;
344 procedure r_Draw_GetRect (out l
, t
, r
, b
: Integer);
346 l
:= sl
; t
:= st
; r
:= sr
; b
:= sb
;