22 |
22 |
23 interface |
23 interface |
24 |
24 |
25 uses SDLh, uTypes, GLunit, uConsts; |
25 uses SDLh, uTypes, GLunit, uConsts; |
26 |
26 |
27 procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt); |
27 procedure DrawSprite (Sprite: TSprite; X, Y, Frame: LongInt); |
28 procedure DrawFromRect(X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture); |
28 procedure DrawSprite (Sprite: TSprite; X, Y, FrameX, FrameY: LongInt); |
29 procedure DrawFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture); |
29 procedure DrawSpriteFromRect (Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt); |
30 procedure DrawSprite (Sprite: TSprite; X, Y, Frame: LongInt); |
30 procedure DrawSpriteClipped (Sprite: TSprite; X, Y, TopY, RightX, BottomY, LeftX: LongInt); |
31 procedure DrawSprite2(Sprite: TSprite; X, Y, FrameX, FrameY: LongInt); |
31 procedure DrawSpriteRotated (Sprite: TSprite; X, Y, Dir: LongInt; Angle: real); |
32 procedure DrawSpriteClipped(Sprite: TSprite; X, Y, TopY, RightX, BottomY, LeftX: LongInt); |
32 procedure DrawSpriteRotatedF (Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real); |
33 procedure DrawTexture(X, Y: LongInt; Texture: PTexture; Scale: GLfloat = 1.0); |
33 |
34 procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt); |
34 procedure DrawTexture (X, Y: LongInt; Texture: PTexture); inline; |
35 procedure DrawRotatedTextureF(Texture: PTexture; Scale, OffsetX, OffsetY: GLfloat; X, Y, Frame, Dir, w, h: LongInt; Angle: real); |
35 procedure DrawTexture (X, Y: LongInt; Texture: PTexture; Scale: GLfloat); |
36 procedure DrawRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real); |
36 procedure DrawTextureFromRect (X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture); |
37 procedure DrawRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real); |
37 procedure DrawTextureFromRect (X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture); |
38 procedure DrawRotatedTex(Tex: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real); |
38 procedure DrawTextureCentered (X, Top: LongInt; Source: PTexture); |
39 procedure DrawCentered(X, Top: LongInt; Source: PTexture); |
39 procedure DrawTextureF (Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt); |
40 procedure DrawLine(X0, Y0, X1, Y1, Width: Single; r, g, b, a: Byte); |
40 procedure DrawTextureRotated (Texture: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real); |
41 procedure DrawFillRect(r: TSDL_Rect); |
41 procedure DrawTextureRotatedF (Texture: PTexture; Scale, OffsetX, OffsetY: GLfloat; X, Y, Frame, Dir, w, h: LongInt; Angle: real); |
42 procedure DrawCircle(X, Y, Radius, Width: LongInt; r, g, b, a: Byte); |
42 |
43 procedure DrawCircle(X, Y, Radius, Width: LongInt); |
43 procedure DrawCircle (X, Y, Radius, Width: LongInt); |
44 procedure DrawHedgehog(X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Angle: real); |
44 procedure DrawCircle (X, Y, Radius, Width: LongInt; r, g, b, a: Byte); |
45 procedure Tint(r, g, b, a: Byte); inline; |
45 |
46 procedure Tint(c: Longword); inline; |
46 procedure DrawLine (X0, Y0, X1, Y1, Width: Single; r, g, b, a: Byte); |
47 |
47 procedure DrawFillRect (r: TSDL_Rect); |
48 var |
48 procedure DrawHedgehog (X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Angle: real); |
49 HHTexture: PTexture; |
49 procedure DrawScreenWidget (widget: POnScreenWidget); |
|
50 |
|
51 procedure Tint (r, g, b, a: Byte); inline; |
|
52 procedure Tint (c: Longword); inline; |
|
53 |
50 |
54 |
51 implementation |
55 implementation |
52 uses uVariables; |
56 uses uVariables; |
53 |
57 |
|
58 var LastTint: LongWord = 0; |
|
59 |
54 procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt); |
60 procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt); |
55 begin |
61 begin |
56 r.y:= r.y + Height * Position; |
62 r.y:= r.y + Height * Position; |
57 r.h:= Height; |
63 r.h:= Height; |
58 DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture) |
64 DrawTextureFromRect(X, Y, @r, SpritesData[Sprite].Texture) |
59 end; |
65 end; |
60 |
66 |
61 procedure DrawFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture); |
67 procedure DrawTextureFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture); |
62 begin |
68 begin |
63 DrawFromRect(X, Y, r^.w, r^.h, r, SourceTexture) |
69 DrawTextureFromRect(X, Y, r^.w, r^.h, r, SourceTexture) |
64 end; |
70 end; |
65 |
71 |
66 procedure DrawFromRect(X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture); |
72 procedure DrawTextureFromRect(X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture); |
67 var rr: TSDL_Rect; |
73 var rr: TSDL_Rect; |
68 _l, _r, _t, _b: real; |
74 _l, _r, _t, _b: real; |
69 VertexBuffer, TextureBuffer: array [0..3] of TVertex2f; |
75 VertexBuffer, TextureBuffer: array [0..3] of TVertex2f; |
70 begin |
76 begin |
71 if (SourceTexture^.h = 0) or (SourceTexture^.w = 0) then exit; |
77 if (SourceTexture^.h = 0) or (SourceTexture^.w = 0) then |
|
78 exit; |
72 |
79 |
73 // do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs) |
80 // do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs) |
74 if (abs(X) > W) and ((abs(X + W / 2) - W / 2) > cScreenWidth / cScaleFactor) then |
81 if (abs(X) > W) and ((abs(X + W / 2) - W / 2) > cScreenWidth / cScaleFactor) then |
75 exit; |
82 exit; |
76 if (abs(Y) > H) and ((abs(Y + H / 2 - (0.5 * cScreenHeight)) - H / 2) > cScreenHeight / cScaleFactor) then |
83 if (abs(Y) > H) and ((abs(Y + H / 2 - (0.5 * cScreenHeight)) - H / 2) > cScreenHeight / cScaleFactor) then |
129 glPopMatrix |
139 glPopMatrix |
130 end; |
140 end; |
131 |
141 |
132 procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt); |
142 procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt); |
133 begin |
143 begin |
134 DrawRotatedTextureF(Texture, Scale, 0, 0, X, Y, Frame, Dir, w, h, 0) |
144 DrawTextureRotatedF(Texture, Scale, 0, 0, X, Y, Frame, Dir, w, h, 0) |
135 end; |
145 end; |
136 |
146 |
137 procedure DrawRotatedTextureF(Texture: PTexture; Scale, OffsetX, OffsetY: GLfloat; X, Y, Frame, Dir, w, h: LongInt; Angle: real); |
147 procedure DrawTextureRotatedF(Texture: PTexture; Scale, OffsetX, OffsetY: GLfloat; X, Y, Frame, Dir, w, h: LongInt; Angle: real); |
138 var ft, fb, fl, fr: GLfloat; |
148 var ft, fb, fl, fr: GLfloat; |
139 hw, nx, ny: LongInt; |
149 hw, nx, ny: LongInt; |
140 VertexBuffer, TextureBuffer: array [0..3] of TVertex2f; |
150 VertexBuffer, TextureBuffer: array [0..3] of TVertex2f; |
141 begin |
151 begin |
142 // do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs) |
152 // do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs) |
192 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
202 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
193 |
203 |
194 glPopMatrix |
204 glPopMatrix |
195 end; |
205 end; |
196 |
206 |
197 procedure DrawRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real); |
207 procedure DrawSpriteRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real); |
198 begin |
208 begin |
199 DrawRotatedTex(SpritesData[Sprite].Texture, |
209 DrawTextureRotated(SpritesData[Sprite].Texture, |
200 SpritesData[Sprite].Width, |
210 SpritesData[Sprite].Width, |
201 SpritesData[Sprite].Height, |
211 SpritesData[Sprite].Height, |
202 X, Y, Dir, Angle) |
212 X, Y, Dir, Angle) |
203 end; |
213 end; |
204 |
214 |
205 procedure DrawRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real); |
215 procedure DrawSpriteRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real); |
206 begin |
216 begin |
207 glPushMatrix; |
217 glPushMatrix; |
208 glTranslatef(X, Y, 0); |
218 glTranslatef(X, Y, 0); |
209 |
219 |
210 if Dir < 0 then |
220 if Dir < 0 then |
211 glRotatef(Angle, 0, 0, -1) |
221 glRotatef(Angle, 0, 0, -1) |
212 else |
222 else |
213 glRotatef(Angle, 0, 0, 1); |
223 glRotatef(Angle, 0, 0, 1); |
214 if Dir < 0 then glScalef(-1.0, 1.0, 1.0); |
224 if Dir < 0 then |
|
225 glScalef(-1.0, 1.0, 1.0); |
215 |
226 |
216 DrawSprite(Sprite, -SpritesData[Sprite].Width div 2, -SpritesData[Sprite].Height div 2, Frame); |
227 DrawSprite(Sprite, -SpritesData[Sprite].Width div 2, -SpritesData[Sprite].Height div 2, Frame); |
217 |
228 |
218 glPopMatrix |
229 glPopMatrix |
219 end; |
230 end; |
220 |
231 |
221 procedure DrawRotatedTex(Tex: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real); |
232 procedure DrawTextureRotated(Texture: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real); |
222 var VertexBuffer: array [0..3] of TVertex2f; |
233 var VertexBuffer: array [0..3] of TVertex2f; |
223 begin |
234 begin |
224 // do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs) |
235 // do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs) |
225 if (abs(X) > 2 * hw) and ((abs(X) - hw) > cScreenWidth / cScaleFactor) then |
236 if (abs(X) > 2 * hw) and ((abs(X) - hw) > cScreenWidth / cScaleFactor) then |
226 exit; |
237 exit; |
248 VertexBuffer[2].Y:= hh; |
260 VertexBuffer[2].Y:= hh; |
249 VertexBuffer[3].X:= -hw; |
261 VertexBuffer[3].X:= -hw; |
250 VertexBuffer[3].Y:= hh; |
262 VertexBuffer[3].Y:= hh; |
251 |
263 |
252 glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]); |
264 glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]); |
253 glTexCoordPointer(2, GL_FLOAT, 0, @Tex^.tb); |
265 glTexCoordPointer(2, GL_FLOAT, 0, @Texture^.tb); |
254 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
266 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
255 |
267 |
256 glPopMatrix |
268 glPopMatrix |
257 end; |
269 end; |
258 |
270 |
259 procedure DrawSprite (Sprite: TSprite; X, Y, Frame: LongInt); |
271 procedure DrawSprite(Sprite: TSprite; X, Y, Frame: LongInt); |
260 var row, col, numFramesFirstCol: LongInt; |
272 var row, col, numFramesFirstCol: LongInt; |
261 begin |
273 begin |
262 if SpritesData[Sprite].imageHeight = 0 then exit; |
274 if SpritesData[Sprite].imageHeight = 0 then |
263 numFramesFirstCol:= SpritesData[Sprite].imageHeight div SpritesData[Sprite].Height; |
275 exit; |
264 row:= Frame mod numFramesFirstCol; |
276 numFramesFirstCol:= SpritesData[Sprite].imageHeight div SpritesData[Sprite].Height; |
265 col:= Frame div numFramesFirstCol; |
277 row:= Frame mod numFramesFirstCol; |
266 DrawSprite2 (Sprite, X, Y, col, row); |
278 col:= Frame div numFramesFirstCol; |
|
279 DrawSprite(Sprite, X, Y, col, row); |
|
280 end; |
|
281 |
|
282 procedure DrawSprite(Sprite: TSprite; X, Y, FrameX, FrameY: LongInt); |
|
283 var r: TSDL_Rect; |
|
284 begin |
|
285 r.x:= FrameX * SpritesData[Sprite].Width; |
|
286 r.w:= SpritesData[Sprite].Width; |
|
287 r.y:= FrameY * SpritesData[Sprite].Height; |
|
288 r.h:= SpritesData[Sprite].Height; |
|
289 DrawTextureFromRect(X, Y, @r, SpritesData[Sprite].Texture) |
267 end; |
290 end; |
268 |
291 |
269 procedure DrawSpriteClipped(Sprite: TSprite; X, Y, TopY, RightX, BottomY, LeftX: LongInt); |
292 procedure DrawSpriteClipped(Sprite: TSprite; X, Y, TopY, RightX, BottomY, LeftX: LongInt); |
270 var r: TSDL_Rect; |
293 var r: TSDL_Rect; |
271 begin |
294 begin |
442 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
456 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer)); |
443 |
457 |
444 glPopMatrix |
458 glPopMatrix |
445 end; |
459 end; |
446 |
460 |
|
461 procedure DrawScreenWidget(widget: POnScreenWidget); |
|
462 {$IFDEF USE_TOUCH_INTERFACE} |
|
463 var alpha: byte = $FF; |
|
464 begin |
|
465 with widget^ do |
|
466 begin |
|
467 if (fadeAnimStart <> 0) then |
|
468 begin |
|
469 if RealTicks > (fadeAnimStart + FADE_ANIM_TIME) then |
|
470 fadeAnimStart:= 0 |
|
471 else |
|
472 if show then |
|
473 alpha:= Byte(trunc((RealTicks - fadeAnimStart)/FADE_ANIM_TIME * $FF)) |
|
474 else |
|
475 alpha:= Byte($FF - trunc((RealTicks - fadeAnimStart)/FADE_ANIM_TIME * $FF)); |
|
476 end; |
|
477 |
|
478 with moveAnim do |
|
479 if animate then |
|
480 if RealTicks > (startTime + MOVE_ANIM_TIME) then |
|
481 begin |
|
482 startTime:= 0; |
|
483 animate:= false; |
|
484 frame.x:= target.x; |
|
485 frame.y:= target.y; |
|
486 active.x:= active.x + (target.x - source.x); |
|
487 active.y:= active.y + (target.y - source.y); |
|
488 end |
|
489 else |
|
490 begin |
|
491 frame.x:= source.x + Round((target.x - source.x) * ((RealTicks - startTime) / MOVE_ANIM_TIME)); |
|
492 frame.y:= source.y + Round((target.y - source.y) * ((RealTicks - startTime) / MOVE_ANIM_TIME)); |
|
493 end; |
|
494 |
|
495 if show or (fadeAnimStart <> 0) then |
|
496 begin |
|
497 Tint($FF, $FF, $FF, alpha); |
|
498 DrawTexture(frame.x, frame.y, spritesData[sprite].Texture, buttonScale); |
|
499 Tint($FF, $FF, $FF, $FF); |
|
500 end; |
|
501 end; |
|
502 {$ELSE} |
|
503 begin |
|
504 widget:= widget; // avoid hint |
|
505 {$ENDIF} |
|
506 end; |
447 |
507 |
448 procedure Tint(r, g, b, a: Byte); inline; |
508 procedure Tint(r, g, b, a: Byte); inline; |
449 const |
509 var nc, tw: Longword; |
450 lastTint: Longword = 0; |
510 begin |
451 var |
511 nc:= (a shl 24) or (b shl 16) or (g shl 8) or r; |
452 nc, tw: Longword; |
512 |
453 begin |
513 if nc = lastTint then |
454 nc:= (a shl 24) or (b shl 16) or (g shl 8) or r; |
514 exit; |
455 |
515 |
456 if nc = lastTint then |
516 if GrayScale then |
457 exit; |
517 begin |
458 |
518 tw:= round(r * RGB_LUMINANCE_RED + g * RGB_LUMINANCE_GREEN + b * RGB_LUMINANCE_BLUE); |
459 if cGrayScale then |
519 if tw > 255 then |
460 begin |
520 tw:= 255; |
461 tw:= round(r * RGB_LUMINANCE_RED + g * RGB_LUMINANCE_GREEN + b * RGB_LUMINANCE_BLUE); |
521 r:= tw; |
462 if tw > 255 then tw:= 255; |
522 g:= tw; |
463 r:= tw; |
523 b:= tw |
464 g:= tw; |
524 end; |
465 b:= tw |
525 |
466 end; |
526 glColor4ub(r, g, b, a); |
467 |
527 lastTint:= nc; |
468 glColor4ub(r, g, b, a); |
|
469 lastTint:= nc; |
|
470 end; |
528 end; |
471 |
529 |
472 procedure Tint(c: Longword); inline; |
530 procedure Tint(c: Longword); inline; |
473 begin |
531 begin |
474 Tint(((c shr 24) and $FF), ((c shr 16) and $FF), (c shr 8) and $FF, (c and $FF)) |
532 Tint(((c shr 24) and $FF), ((c shr 16) and $FF), (c shr 8) and $FF, (c and $FF)) |
475 end; |
533 end; |
476 |
534 |
477 end. |
535 end. |