author | Wolfgang Steffens <WolfgangSteff@gmail.com> |
Mon, 14 May 2012 19:26:50 +0200 | |
changeset 7080 | dbf43c07a507 |
parent 7069 | bcf9d8e64e92 |
child 7111 | 5ba5a92d74fb |
permissions | -rw-r--r-- |
4976 | 1 |
(* |
2 |
* Hedgewars, a free turn based strategy game |
|
6700 | 3 |
* Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
4976 | 4 |
* |
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 2 of the License |
|
8 |
* |
|
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. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*) |
|
18 |
||
4378 | 19 |
{$INCLUDE "options.inc"} |
4976 | 20 |
|
4378 | 21 |
unit uRender; |
22 |
||
23 |
interface |
|
24 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
25 |
uses SDLh, uTypes, GLunit, uConsts, uTextures; |
4378 | 26 |
|
6999 | 27 |
procedure DrawSprite (Sprite: TSprite; X, Y, Frame: LongInt); |
28 |
procedure DrawSprite (Sprite: TSprite; X, Y, FrameX, FrameY: LongInt); |
|
29 |
procedure DrawSpriteFromRect (Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt); |
|
30 |
procedure DrawSpriteClipped (Sprite: TSprite; X, Y, TopY, RightX, BottomY, LeftX: LongInt); |
|
31 |
procedure DrawSpriteRotated (Sprite: TSprite; X, Y, Dir: LongInt; Angle: real); |
|
32 |
procedure DrawSpriteRotatedF (Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real); |
|
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
33 |
|
6999 | 34 |
procedure DrawTexture (X, Y: LongInt; Texture: PTexture); inline; |
35 |
procedure DrawTexture (X, Y: LongInt; Texture: PTexture; Scale: GLfloat); |
|
36 |
procedure DrawTextureFromRect (X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture); |
|
37 |
procedure DrawTextureFromRect (X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture); |
|
38 |
procedure DrawTextureCentered (X, Top: LongInt; Source: PTexture); |
|
39 |
procedure DrawTextureF (Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt); |
|
40 |
procedure DrawTextureRotated (Texture: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real); |
|
41 |
procedure DrawTextureRotatedF (Texture: PTexture; Scale, OffsetX, OffsetY: GLfloat; X, Y, Frame, Dir, w, h: LongInt; Angle: real); |
|
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
42 |
|
6999 | 43 |
procedure DrawCircle (X, Y, Radius, Width: LongInt); |
44 |
procedure DrawCircle (X, Y, Radius, Width: LongInt; r, g, b, a: Byte); |
|
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
45 |
|
6999 | 46 |
procedure DrawLine (X0, Y0, X1, Y1, Width: Single; r, g, b, a: Byte); |
47 |
procedure DrawFillRect (r: TSDL_Rect); |
|
48 |
procedure DrawHedgehog (X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Angle: real); |
|
49 |
procedure DrawScreenWidget (widget: POnScreenWidget); |
|
50 |
||
51 |
procedure Tint (r, g, b, a: Byte); inline; |
|
52 |
procedure Tint (c: Longword); inline; |
|
4378 | 53 |
|
4385 | 54 |
|
4378 | 55 |
implementation |
56 |
uses uVariables; |
|
57 |
||
7028 | 58 |
var LastTint: LongWord = 0; |
59 |
||
4378 | 60 |
procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt); |
61 |
begin |
|
62 |
r.y:= r.y + Height * Position; |
|
63 |
r.h:= Height; |
|
6999 | 64 |
DrawTextureFromRect(X, Y, @r, SpritesData[Sprite].Texture) |
4378 | 65 |
end; |
66 |
||
6999 | 67 |
procedure DrawTextureFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture); |
4378 | 68 |
begin |
6999 | 69 |
DrawTextureFromRect(X, Y, r^.w, r^.h, r, SourceTexture) |
4378 | 70 |
end; |
71 |
||
6999 | 72 |
procedure DrawTextureFromRect(X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture); |
4378 | 73 |
var rr: TSDL_Rect; |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
74 |
VertexBuffer, TextureBuffer: TVertexRect; |
4378 | 75 |
begin |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6394
diff
changeset
|
76 |
if (SourceTexture^.h = 0) or (SourceTexture^.w = 0) then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6394
diff
changeset
|
77 |
exit; |
4378 | 78 |
|
5565 | 79 |
// do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs) |
4378 | 80 |
if (abs(X) > W) and ((abs(X + W / 2) - W / 2) > cScreenWidth / cScaleFactor) then |
81 |
exit; |
|
82 |
if (abs(Y) > H) and ((abs(Y + H / 2 - (0.5 * cScreenHeight)) - H / 2) > cScreenHeight / cScaleFactor) then |
|
83 |
exit; |
|
84 |
||
85 |
rr.x:= X; |
|
86 |
rr.y:= Y; |
|
87 |
rr.w:= W; |
|
88 |
rr.h:= H; |
|
89 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
90 |
glBindTexture(GL_TEXTURE_2D, SourceTexture^.atlas^.id); |
4378 | 91 |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
92 |
ComputeTexcoords(SourceTexture, r, @TextureBuffer); |
4378 | 93 |
|
94 |
VertexBuffer[0].X:= X; |
|
95 |
VertexBuffer[0].Y:= Y; |
|
96 |
VertexBuffer[1].X:= rr.w + X; |
|
97 |
VertexBuffer[1].Y:= Y; |
|
98 |
VertexBuffer[2].X:= rr.w + X; |
|
99 |
VertexBuffer[2].Y:= rr.h + Y; |
|
100 |
VertexBuffer[3].X:= X; |
|
101 |
VertexBuffer[3].Y:= rr.h + Y; |
|
102 |
||
103 |
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]); |
|
104 |
glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]); |
|
105 |
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
|
106 |
end; |
|
107 |
||
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
108 |
procedure DrawTexture(X, Y: LongInt; Texture: PTexture); inline; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
109 |
begin |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
110 |
DrawTexture(X, Y, Texture, 1.0); |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
111 |
end; |
4378 | 112 |
|
113 |
procedure DrawTexture(X, Y: LongInt; Texture: PTexture; Scale: GLfloat); |
|
114 |
begin |
|
115 |
||
116 |
glPushMatrix; |
|
117 |
glTranslatef(X, Y, 0); |
|
118 |
glScalef(Scale, Scale, 1); |
|
119 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
120 |
glBindTexture(GL_TEXTURE_2D, Texture^.atlas^.id); |
4378 | 121 |
|
122 |
glVertexPointer(2, GL_FLOAT, 0, @Texture^.vb); |
|
123 |
glTexCoordPointer(2, GL_FLOAT, 0, @Texture^.tb); |
|
124 |
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(Texture^.vb)); |
|
125 |
||
126 |
glPopMatrix |
|
127 |
end; |
|
128 |
||
129 |
procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt); |
|
130 |
begin |
|
6999 | 131 |
DrawTextureRotatedF(Texture, Scale, 0, 0, X, Y, Frame, Dir, w, h, 0) |
4378 | 132 |
end; |
133 |
||
6999 | 134 |
procedure DrawTextureRotatedF(Texture: PTexture; Scale, OffsetX, OffsetY: GLfloat; X, Y, Frame, Dir, w, h: LongInt; Angle: real); |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
135 |
var hw, nx, ny: LongInt; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
136 |
r: TSDL_Rect; |
4378 | 137 |
VertexBuffer, TextureBuffer: array [0..3] of TVertex2f; |
138 |
begin |
|
5565 | 139 |
// do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs) |
4378 | 140 |
if (abs(X) > W) and ((abs(X + dir * OffsetX) - W / 2) * cScaleFactor > cScreenWidth) then |
141 |
exit; |
|
142 |
if (abs(Y) > H) and ((abs(Y + OffsetY - (0.5 * cScreenHeight)) - W / 2) * cScaleFactor > cScreenHeight) then |
|
143 |
exit; |
|
144 |
||
145 |
glPushMatrix; |
|
146 |
glTranslatef(X, Y, 0); |
|
6323
c1aa6a3c84a7
Dir should not be 0, but set it to 1 if 0 in case I missed some other place this was done. Also correct cloud scaling.
nemo
parents:
6322
diff
changeset
|
147 |
if Dir = 0 then Dir:= 1; |
4378 | 148 |
|
6323
c1aa6a3c84a7
Dir should not be 0, but set it to 1 if 0 in case I missed some other place this was done. Also correct cloud scaling.
nemo
parents:
6322
diff
changeset
|
149 |
glRotatef(Angle, 0, 0, Dir); |
4378 | 150 |
|
151 |
glTranslatef(Dir*OffsetX, OffsetY, 0); |
|
152 |
glScalef(Scale, Scale, 1); |
|
153 |
||
154 |
// Any reason for this call? And why only in t direction, not s? |
|
155 |
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
|
156 |
||
6322
b310f0bc8dde
If I'm going to be arbitrary about it, might as well go for the more minimal arbitrariness
nemo
parents:
6318
diff
changeset
|
157 |
hw:= w div (2 div Dir); |
4378 | 158 |
|
159 |
nx:= round(Texture^.w / w); // number of horizontal frames |
|
160 |
ny:= round(Texture^.h / h); // number of vertical frames |
|
161 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
162 |
r.y:=(Frame mod ny) * h; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
163 |
r.x:=(Frame div ny) * w; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
164 |
r.w:=w; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
165 |
r.h:=h; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
166 |
ComputeTexcoords(Texture, @r, @TextureBuffer); |
4378 | 167 |
|
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
168 |
glBindTexture(GL_TEXTURE_2D, Texture^.atlas^.id); |
4378 | 169 |
|
170 |
VertexBuffer[0].X:= -hw; |
|
171 |
VertexBuffer[0].Y:= w / -2; |
|
172 |
VertexBuffer[1].X:= hw; |
|
173 |
VertexBuffer[1].Y:= w / -2; |
|
174 |
VertexBuffer[2].X:= hw; |
|
175 |
VertexBuffer[2].Y:= w / 2; |
|
176 |
VertexBuffer[3].X:= -hw; |
|
177 |
VertexBuffer[3].Y:= w / 2; |
|
178 |
||
179 |
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]); |
|
180 |
glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]); |
|
181 |
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
|
182 |
||
183 |
glPopMatrix |
|
184 |
end; |
|
185 |
||
6999 | 186 |
procedure DrawSpriteRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real); |
4378 | 187 |
begin |
6999 | 188 |
DrawTextureRotated(SpritesData[Sprite].Texture, |
4378 | 189 |
SpritesData[Sprite].Width, |
190 |
SpritesData[Sprite].Height, |
|
191 |
X, Y, Dir, Angle) |
|
192 |
end; |
|
193 |
||
6999 | 194 |
procedure DrawSpriteRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real); |
4378 | 195 |
begin |
196 |
glPushMatrix; |
|
197 |
glTranslatef(X, Y, 0); |
|
198 |
||
199 |
if Dir < 0 then |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6394
diff
changeset
|
200 |
glRotatef(Angle, 0, 0, -1) |
4378 | 201 |
else |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6394
diff
changeset
|
202 |
glRotatef(Angle, 0, 0, 1); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6394
diff
changeset
|
203 |
if Dir < 0 then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6394
diff
changeset
|
204 |
glScalef(-1.0, 1.0, 1.0); |
4378 | 205 |
|
206 |
DrawSprite(Sprite, -SpritesData[Sprite].Width div 2, -SpritesData[Sprite].Height div 2, Frame); |
|
207 |
||
208 |
glPopMatrix |
|
209 |
end; |
|
210 |
||
6999 | 211 |
procedure DrawTextureRotated(Texture: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real); |
4378 | 212 |
var VertexBuffer: array [0..3] of TVertex2f; |
213 |
begin |
|
5565 | 214 |
// do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs) |
4378 | 215 |
if (abs(X) > 2 * hw) and ((abs(X) - hw) > cScreenWidth / cScaleFactor) then |
216 |
exit; |
|
217 |
if (abs(Y) > 2 * hh) and ((abs(Y - 0.5 * cScreenHeight) - hh) > cScreenHeight / cScaleFactor) then |
|
218 |
exit; |
|
219 |
||
220 |
glPushMatrix; |
|
221 |
glTranslatef(X, Y, 0); |
|
222 |
||
223 |
if Dir < 0 then |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6394
diff
changeset
|
224 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6394
diff
changeset
|
225 |
hw:= - hw; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6394
diff
changeset
|
226 |
glRotatef(Angle, 0, 0, -1); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6394
diff
changeset
|
227 |
end |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6394
diff
changeset
|
228 |
else |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6394
diff
changeset
|
229 |
glRotatef(Angle, 0, 0, 1); |
4378 | 230 |
|
231 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
232 |
glBindTexture(GL_TEXTURE_2D, Texture^.atlas^.id); |
4378 | 233 |
|
234 |
VertexBuffer[0].X:= -hw; |
|
235 |
VertexBuffer[0].Y:= -hh; |
|
236 |
VertexBuffer[1].X:= hw; |
|
237 |
VertexBuffer[1].Y:= -hh; |
|
238 |
VertexBuffer[2].X:= hw; |
|
239 |
VertexBuffer[2].Y:= hh; |
|
240 |
VertexBuffer[3].X:= -hw; |
|
241 |
VertexBuffer[3].Y:= hh; |
|
242 |
||
243 |
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]); |
|
6999 | 244 |
glTexCoordPointer(2, GL_FLOAT, 0, @Texture^.tb); |
4378 | 245 |
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
246 |
||
247 |
glPopMatrix |
|
248 |
end; |
|
249 |
||
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
250 |
procedure DrawSprite(Sprite: TSprite; X, Y, Frame: LongInt); |
4378 | 251 |
var row, col, numFramesFirstCol: LongInt; |
252 |
begin |
|
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
253 |
if SpritesData[Sprite].imageHeight = 0 then |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
254 |
exit; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
255 |
numFramesFirstCol:= SpritesData[Sprite].imageHeight div SpritesData[Sprite].Height; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
256 |
row:= Frame mod numFramesFirstCol; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
257 |
col:= Frame div numFramesFirstCol; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
258 |
DrawSprite(Sprite, X, Y, col, row); |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
259 |
end; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
260 |
|
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
261 |
procedure DrawSprite(Sprite: TSprite; X, Y, FrameX, FrameY: LongInt); |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
262 |
var r: TSDL_Rect; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
263 |
begin |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
264 |
r.x:= FrameX * SpritesData[Sprite].Width; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
265 |
r.w:= SpritesData[Sprite].Width; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
266 |
r.y:= FrameY * SpritesData[Sprite].Height; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
267 |
r.h:= SpritesData[Sprite].Height; |
6999 | 268 |
DrawTextureFromRect(X, Y, @r, SpritesData[Sprite].Texture) |
4378 | 269 |
end; |
270 |
||
271 |
procedure DrawSpriteClipped(Sprite: TSprite; X, Y, TopY, RightX, BottomY, LeftX: LongInt); |
|
272 |
var r: TSDL_Rect; |
|
273 |
begin |
|
274 |
r.x:= 0; |
|
275 |
r.y:= 0; |
|
276 |
r.w:= SpritesData[Sprite].Width; |
|
277 |
r.h:= SpritesData[Sprite].Height; |
|
278 |
||
279 |
if (X < LeftX) then |
|
280 |
r.x:= LeftX - X; |
|
281 |
if (Y < TopY) then |
|
282 |
r.y:= TopY - Y; |
|
283 |
||
284 |
if (Y + SpritesData[Sprite].Height > BottomY) then |
|
285 |
r.h:= BottomY - Y + 1; |
|
286 |
if (X + SpritesData[Sprite].Width > RightX) then |
|
287 |
r.w:= RightX - X + 1; |
|
288 |
||
289 |
dec(r.h, r.y); |
|
290 |
dec(r.w, r.x); |
|
291 |
||
6999 | 292 |
DrawTextureFromRect(X + r.x, Y + r.y, @r, SpritesData[Sprite].Texture) |
4378 | 293 |
end; |
294 |
||
6999 | 295 |
procedure DrawTextureCentered(X, Top: LongInt; Source: PTexture); |
4378 | 296 |
var scale: GLfloat; |
297 |
begin |
|
298 |
if (Source^.w + 20) > cScreenWidth then |
|
299 |
scale:= cScreenWidth / (Source^.w + 20) |
|
300 |
else |
|
301 |
scale:= 1.0; |
|
302 |
DrawTexture(X - round(Source^.w * scale) div 2, Top, Source, scale) |
|
303 |
end; |
|
304 |
||
305 |
procedure DrawLine(X0, Y0, X1, Y1, Width: Single; r, g, b, a: Byte); |
|
306 |
var VertexBuffer: array [0..3] of TVertex2f; |
|
307 |
begin |
|
308 |
glDisable(GL_TEXTURE_2D); |
|
309 |
glEnable(GL_LINE_SMOOTH); |
|
310 |
||
311 |
glPushMatrix; |
|
312 |
glTranslatef(WorldDx, WorldDy, 0); |
|
313 |
glLineWidth(Width); |
|
314 |
||
315 |
Tint(r, g, b, a); |
|
316 |
VertexBuffer[0].X:= X0; |
|
317 |
VertexBuffer[0].Y:= Y0; |
|
318 |
VertexBuffer[1].X:= X1; |
|
319 |
VertexBuffer[1].Y:= Y1; |
|
320 |
||
321 |
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]); |
|
322 |
glDrawArrays(GL_LINES, 0, Length(VertexBuffer)); |
|
323 |
Tint($FF, $FF, $FF, $FF); |
|
324 |
||
325 |
glPopMatrix; |
|
326 |
||
327 |
glEnable(GL_TEXTURE_2D); |
|
328 |
glDisable(GL_LINE_SMOOTH); |
|
329 |
end; |
|
330 |
||
331 |
procedure DrawFillRect(r: TSDL_Rect); |
|
332 |
var VertexBuffer: array [0..3] of TVertex2f; |
|
333 |
begin |
|
5565 | 334 |
// do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs) |
4378 | 335 |
if (abs(r.x) > r.w) and ((abs(r.x + r.w / 2) - r.w / 2) * cScaleFactor > cScreenWidth) then |
336 |
exit; |
|
337 |
if (abs(r.y) > r.h) and ((abs(r.y + r.h / 2 - (0.5 * cScreenHeight)) - r.h / 2) * cScaleFactor > cScreenHeight) then |
|
338 |
exit; |
|
339 |
||
340 |
glDisable(GL_TEXTURE_2D); |
|
341 |
||
342 |
Tint($00, $00, $00, $80); |
|
343 |
||
344 |
VertexBuffer[0].X:= r.x; |
|
345 |
VertexBuffer[0].Y:= r.y; |
|
346 |
VertexBuffer[1].X:= r.x + r.w; |
|
347 |
VertexBuffer[1].Y:= r.y; |
|
348 |
VertexBuffer[2].X:= r.x + r.w; |
|
349 |
VertexBuffer[2].Y:= r.y + r.h; |
|
350 |
VertexBuffer[3].X:= r.x; |
|
351 |
VertexBuffer[3].Y:= r.y + r.h; |
|
352 |
||
353 |
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]); |
|
354 |
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
|
355 |
||
356 |
Tint($FF, $FF, $FF, $FF); |
|
357 |
glEnable(GL_TEXTURE_2D) |
|
358 |
end; |
|
359 |
||
4420
6be946bcd17a
Add a visual gear for drawing circles. Intent is to allow specifying areas on map for lua scripts (such as to indicate a location to go to). Could also be used to, say, circle a hog in CTF. Also add a "Critical" flag for visual gears so a gear flagges as such will always be created.
nemo
parents:
4385
diff
changeset
|
360 |
procedure DrawCircle(X, Y, Radius, Width: LongInt; r, g, b, a: Byte); |
4451
1c342980b4aa
trying to get tint to behave more as I expect. need to ask Smaxx what he intended here, to avoid too many pointless colour calls
nemo
parents:
4420
diff
changeset
|
361 |
begin |
1c342980b4aa
trying to get tint to behave more as I expect. need to ask Smaxx what he intended here, to avoid too many pointless colour calls
nemo
parents:
4420
diff
changeset
|
362 |
Tint(r, g, b, a); |
1c342980b4aa
trying to get tint to behave more as I expect. need to ask Smaxx what he intended here, to avoid too many pointless colour calls
nemo
parents:
4420
diff
changeset
|
363 |
DrawCircle(X, Y, Radius, Width); |
1c342980b4aa
trying to get tint to behave more as I expect. need to ask Smaxx what he intended here, to avoid too many pointless colour calls
nemo
parents:
4420
diff
changeset
|
364 |
Tint($FF, $FF, $FF, $FF); |
1c342980b4aa
trying to get tint to behave more as I expect. need to ask Smaxx what he intended here, to avoid too many pointless colour calls
nemo
parents:
4420
diff
changeset
|
365 |
end; |
1c342980b4aa
trying to get tint to behave more as I expect. need to ask Smaxx what he intended here, to avoid too many pointless colour calls
nemo
parents:
4420
diff
changeset
|
366 |
|
1c342980b4aa
trying to get tint to behave more as I expect. need to ask Smaxx what he intended here, to avoid too many pointless colour calls
nemo
parents:
4420
diff
changeset
|
367 |
procedure DrawCircle(X, Y, Radius, Width: LongInt); |
4378 | 368 |
var |
369 |
i: LongInt; |
|
5561
dfbe55237c64
Shrink number of circle points to 60, reenable seduction circle (no longer crashes)
nemo
parents:
5559
diff
changeset
|
370 |
CircleVertex: array [0..59] of TVertex2f; |
4378 | 371 |
begin |
5561
dfbe55237c64
Shrink number of circle points to 60, reenable seduction circle (no longer crashes)
nemo
parents:
5559
diff
changeset
|
372 |
for i := 0 to 59 do begin |
dfbe55237c64
Shrink number of circle points to 60, reenable seduction circle (no longer crashes)
nemo
parents:
5559
diff
changeset
|
373 |
CircleVertex[i].X := X + Radius*cos(i*pi/30); |
dfbe55237c64
Shrink number of circle points to 60, reenable seduction circle (no longer crashes)
nemo
parents:
5559
diff
changeset
|
374 |
CircleVertex[i].Y := Y + Radius*sin(i*pi/30); |
4378 | 375 |
end; |
376 |
glDisable(GL_TEXTURE_2D); |
|
377 |
glEnable(GL_LINE_SMOOTH); |
|
378 |
glPushMatrix; |
|
379 |
glLineWidth(Width); |
|
380 |
glVertexPointer(2, GL_FLOAT, 0, @CircleVertex[0]); |
|
5561
dfbe55237c64
Shrink number of circle points to 60, reenable seduction circle (no longer crashes)
nemo
parents:
5559
diff
changeset
|
381 |
glDrawArrays(GL_LINE_LOOP, 0, 60); |
4378 | 382 |
glPopMatrix; |
383 |
glEnable(GL_TEXTURE_2D); |
|
384 |
glDisable(GL_LINE_SMOOTH); |
|
385 |
end; |
|
386 |
||
387 |
||
4385 | 388 |
procedure DrawHedgehog(X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Angle: real); |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
389 |
const VertexBuffers: array[0..1] of TVertexRect = ( |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
390 |
((x: -16; y: -16), |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
391 |
(x: 16; y: -16), |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
392 |
(x: 16; y: 16), |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
393 |
(x: -16; y: 16)), |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
394 |
((x: 16; y: -16), |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
395 |
(x: -16; y: -16), |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
396 |
(x: -16; y: 16), |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
397 |
(x: 16; y: 16))); |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
398 |
var r: TSDL_Rect; |
4385 | 399 |
TextureBuffer: array [0..3] of TVertex2f; |
400 |
begin |
|
5565 | 401 |
// do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs) |
4385 | 402 |
if (abs(X) > 32) and ((abs(X) - 16) * cScaleFactor > cScreenWidth) then |
403 |
exit; |
|
404 |
if (abs(Y) > 32) and ((abs(Y - 0.5 * cScreenHeight) - 16) * cScaleFactor > cScreenHeight) then |
|
405 |
exit; |
|
406 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
407 |
r.x:=Step * 32; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
408 |
r.y:=Pos * 32; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
409 |
r.w:=32; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
410 |
r.h:=32; |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
411 |
ComputeTexcoords(HHTexture, @r, @TextureBuffer); |
4385 | 412 |
|
413 |
glPushMatrix(); |
|
414 |
glTranslatef(X, Y, 0); |
|
415 |
glRotatef(Angle, 0, 0, 1); |
|
416 |
||
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
417 |
glBindTexture(GL_TEXTURE_2D, HHTexture^.atlas^.id); |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
418 |
if Dir = -1 then |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
419 |
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffers[1][0]) |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
420 |
else |
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
421 |
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffers[0][0]); |
4385 | 422 |
glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]); |
7080
dbf43c07a507
Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents:
7069
diff
changeset
|
423 |
glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
4385 | 424 |
|
425 |
glPopMatrix |
|
426 |
end; |
|
427 |
||
6688 | 428 |
procedure DrawScreenWidget(widget: POnScreenWidget); |
6992 | 429 |
{$IFDEF USE_TOUCH_INTERFACE} |
6688 | 430 |
var alpha: byte = $FF; |
431 |
begin |
|
432 |
with widget^ do |
|
433 |
begin |
|
434 |
if (fadeAnimStart <> 0) then |
|
435 |
begin |
|
436 |
if RealTicks > (fadeAnimStart + FADE_ANIM_TIME) then |
|
437 |
fadeAnimStart:= 0 |
|
438 |
else |
|
439 |
if show then |
|
440 |
alpha:= Byte(trunc((RealTicks - fadeAnimStart)/FADE_ANIM_TIME * $FF)) |
|
441 |
else |
|
442 |
alpha:= Byte($FF - trunc((RealTicks - fadeAnimStart)/FADE_ANIM_TIME * $FF)); |
|
443 |
end; |
|
444 |
||
445 |
with moveAnim do |
|
446 |
if animate then |
|
447 |
if RealTicks > (startTime + MOVE_ANIM_TIME) then |
|
448 |
begin |
|
449 |
startTime:= 0; |
|
6710
42504695122d
fixed the active region on widgets when they are animated, added active.x:= in uWorld too
Xeli
parents:
6700
diff
changeset
|
450 |
animate:= false; |
6695
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6689
diff
changeset
|
451 |
frame.x:= target.x; |
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6689
diff
changeset
|
452 |
frame.y:= target.y; |
6710
42504695122d
fixed the active region on widgets when they are animated, added active.x:= in uWorld too
Xeli
parents:
6700
diff
changeset
|
453 |
active.x:= active.x + (target.x - source.x); |
42504695122d
fixed the active region on widgets when they are animated, added active.x:= in uWorld too
Xeli
parents:
6700
diff
changeset
|
454 |
active.y:= active.y + (target.y - source.y); |
6688 | 455 |
end |
456 |
else |
|
457 |
begin |
|
6695
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6689
diff
changeset
|
458 |
frame.x:= source.x + Round((target.x - source.x) * ((RealTicks - startTime) / MOVE_ANIM_TIME)); |
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6689
diff
changeset
|
459 |
frame.y:= source.y + Round((target.y - source.y) * ((RealTicks - startTime) / MOVE_ANIM_TIME)); |
6688 | 460 |
end; |
461 |
||
462 |
if show or (fadeAnimStart <> 0) then |
|
463 |
begin |
|
464 |
Tint($FF, $FF, $FF, alpha); |
|
6695
32de8965c62c
refactored a few types involved in the touch interface and corrected a few invisible mistakes
koda
parents:
6689
diff
changeset
|
465 |
DrawTexture(frame.x, frame.y, spritesData[sprite].Texture, buttonScale); |
6688 | 466 |
Tint($FF, $FF, $FF, $FF); |
467 |
end; |
|
468 |
end; |
|
6992 | 469 |
{$ELSE} |
470 |
begin |
|
471 |
widget:= widget; // avoid hint |
|
6689 | 472 |
{$ENDIF} |
6688 | 473 |
end; |
4385 | 474 |
|
4378 | 475 |
procedure Tint(r, g, b, a: Byte); inline; |
6982 | 476 |
var nc, tw: Longword; |
4378 | 477 |
begin |
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
478 |
nc:= (a shl 24) or (b shl 16) or (g shl 8) or r; |
5559 | 479 |
|
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
480 |
if nc = lastTint then |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
481 |
exit; |
5559 | 482 |
|
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
483 |
if GrayScale then |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
484 |
begin |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
485 |
tw:= round(r * RGB_LUMINANCE_RED + g * RGB_LUMINANCE_GREEN + b * RGB_LUMINANCE_BLUE); |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
486 |
if tw > 255 then |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
487 |
tw:= 255; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
488 |
r:= tw; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
489 |
g:= tw; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
490 |
b:= tw |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
491 |
end; |
5441
39962b855540
Add grayscale option for 3d, helps with colour clashing
nemo
parents:
5235
diff
changeset
|
492 |
|
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
493 |
glColor4ub(r, g, b, a); |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
494 |
lastTint:= nc; |
4378 | 495 |
end; |
496 |
||
497 |
procedure Tint(c: Longword); inline; |
|
498 |
begin |
|
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
499 |
Tint(((c shr 24) and $FF), ((c shr 16) and $FF), (c shr 8) and $FF, (c and $FF)) |
4378 | 500 |
end; |
501 |
||
502 |
end. |