hedgewars/uRender.pas
author unC0Rr
Thu, 18 Nov 2010 15:45:35 +0300
changeset 4378 91655618a510
child 4385 f679ffa2dc8c
permissions -rw-r--r--
Introduce uRender
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4378
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
     1
{$INCLUDE "options.inc"}
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
     2
unit uRender;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
     3
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
     4
interface
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
     5
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
     6
uses SDLh, uTypes, GLunit;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
     7
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
     8
procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
     9
procedure DrawFromRect(X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    10
procedure DrawFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    11
procedure DrawSprite (Sprite: TSprite; X, Y, Frame: LongInt);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    12
procedure DrawSprite2(Sprite: TSprite; X, Y, FrameX, FrameY: LongInt);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    13
procedure DrawSpriteClipped(Sprite: TSprite; X, Y, TopY, RightX, BottomY, LeftX: LongInt);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    14
procedure DrawTexture(X, Y: LongInt; Texture: PTexture; Scale: GLfloat = 1.0);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    15
procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    16
procedure DrawRotatedTextureF(Texture: PTexture; Scale, OffsetX, OffsetY: GLfloat; X, Y, Frame, Dir, w, h: LongInt; Angle: real);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    17
procedure DrawRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    18
procedure DrawRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    19
procedure DrawRotatedTex(Tex: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    20
procedure DrawCentered(X, Top: LongInt; Source: PTexture);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    21
procedure DrawLine(X0, Y0, X1, Y1, Width: Single; r, g, b, a: Byte);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    22
procedure DrawFillRect(r: TSDL_Rect);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    23
procedure DrawCircle(X, Y, Radius: LongInt; Width: Single; r, g, b, a: Byte);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    24
procedure Tint(r, g, b, a: Byte); inline;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    25
procedure Tint(c: Longword); inline;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    26
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    27
implementation
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    28
uses uVariables;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    29
var
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    30
    lastTint: Longword;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    31
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    32
procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    33
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    34
r.y:= r.y + Height * Position;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    35
r.h:= Height;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    36
DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    37
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    38
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    39
procedure DrawFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    40
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    41
DrawFromRect(X, Y, r^.w, r^.h, r, SourceTexture)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    42
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    43
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    44
procedure DrawFromRect(X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    45
var rr: TSDL_Rect;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    46
    _l, _r, _t, _b: real;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    47
    VertexBuffer, TextureBuffer: array [0..3] of TVertex2f;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    48
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    49
if (SourceTexture^.h = 0) or (SourceTexture^.w = 0) then exit;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    50
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    51
// don't draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    52
if (abs(X) > W) and ((abs(X + W / 2) - W / 2) > cScreenWidth / cScaleFactor) then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    53
    exit;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    54
if (abs(Y) > H) and ((abs(Y + H / 2 - (0.5 * cScreenHeight)) - H / 2) > cScreenHeight / cScaleFactor) then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    55
    exit;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    56
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    57
rr.x:= X;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    58
rr.y:= Y;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    59
rr.w:= W;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    60
rr.h:= H;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    61
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    62
_l:= r^.x / SourceTexture^.w * SourceTexture^.rx;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    63
_r:= (r^.x + r^.w) / SourceTexture^.w * SourceTexture^.rx;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    64
_t:= r^.y / SourceTexture^.h * SourceTexture^.ry;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    65
_b:= (r^.y + r^.h) / SourceTexture^.h * SourceTexture^.ry;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    66
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    67
glBindTexture(GL_TEXTURE_2D, SourceTexture^.id);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    68
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    69
VertexBuffer[0].X:= X;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    70
VertexBuffer[0].Y:= Y;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    71
VertexBuffer[1].X:= rr.w + X;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    72
VertexBuffer[1].Y:= Y;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    73
VertexBuffer[2].X:= rr.w + X;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    74
VertexBuffer[2].Y:= rr.h + Y;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    75
VertexBuffer[3].X:= X;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    76
VertexBuffer[3].Y:= rr.h + Y;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    77
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    78
TextureBuffer[0].X:= _l;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    79
TextureBuffer[0].Y:= _t;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    80
TextureBuffer[1].X:= _r;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    81
TextureBuffer[1].Y:= _t;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    82
TextureBuffer[2].X:= _r;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    83
TextureBuffer[2].Y:= _b;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    84
TextureBuffer[3].X:= _l;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    85
TextureBuffer[3].Y:= _b;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    86
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    87
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    88
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    89
glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    90
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    91
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    92
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    93
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    94
procedure DrawTexture(X, Y: LongInt; Texture: PTexture; Scale: GLfloat);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    95
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    96
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    97
glPushMatrix;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    98
glTranslatef(X, Y, 0);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
    99
glScalef(Scale, Scale, 1);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   100
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   101
glBindTexture(GL_TEXTURE_2D, Texture^.id);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   102
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   103
glVertexPointer(2, GL_FLOAT, 0, @Texture^.vb);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   104
glTexCoordPointer(2, GL_FLOAT, 0, @Texture^.tb);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   105
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(Texture^.vb));
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   106
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   107
glPopMatrix
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   108
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   109
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   110
procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   111
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   112
    DrawRotatedTextureF(Texture, Scale, 0, 0, X, Y, Frame, Dir, w, h, 0)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   113
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   114
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   115
procedure DrawRotatedTextureF(Texture: PTexture; Scale, OffsetX, OffsetY: GLfloat; X, Y, Frame, Dir, w, h: LongInt; Angle: real);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   116
var ft, fb, fl, fr: GLfloat;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   117
    hw, nx, ny: LongInt;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   118
    VertexBuffer, TextureBuffer: array [0..3] of TVertex2f;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   119
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   120
// don't draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   121
if (abs(X) > W) and ((abs(X + dir * OffsetX) - W / 2) * cScaleFactor > cScreenWidth) then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   122
    exit;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   123
if (abs(Y) > H) and ((abs(Y + OffsetY - (0.5 * cScreenHeight)) - W / 2) * cScaleFactor > cScreenHeight) then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   124
    exit;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   125
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   126
glPushMatrix;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   127
glTranslatef(X, Y, 0);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   128
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   129
if Dir < 0 then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   130
   glRotatef(Angle, 0, 0, -1)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   131
else
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   132
   glRotatef(Angle, 0, 0,  1);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   133
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   134
glTranslatef(Dir*OffsetX, OffsetY, 0);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   135
glScalef(Scale, Scale, 1);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   136
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   137
// Any reason for this call? And why only in t direction, not s?
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   138
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   139
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   140
if Dir < 0 then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   141
    hw:= w div -2
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   142
else
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   143
    hw:= w div 2;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   144
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   145
nx:= round(Texture^.w / w); // number of horizontal frames
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   146
ny:= round(Texture^.h / h); // number of vertical frames
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   147
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   148
ft:= (Frame mod ny) * Texture^.ry / ny;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   149
fb:= ((Frame mod ny) + 1) * Texture^.ry / ny;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   150
fl:= (Frame div ny) * Texture^.rx / nx;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   151
fr:= ((Frame div ny) + 1) * Texture^.rx / nx;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   152
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   153
glBindTexture(GL_TEXTURE_2D, Texture^.id);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   154
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   155
VertexBuffer[0].X:= -hw;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   156
VertexBuffer[0].Y:= w / -2;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   157
VertexBuffer[1].X:= hw;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   158
VertexBuffer[1].Y:= w / -2;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   159
VertexBuffer[2].X:= hw;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   160
VertexBuffer[2].Y:= w / 2;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   161
VertexBuffer[3].X:= -hw;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   162
VertexBuffer[3].Y:= w / 2;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   163
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   164
TextureBuffer[0].X:= fl;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   165
TextureBuffer[0].Y:= ft;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   166
TextureBuffer[1].X:= fr;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   167
TextureBuffer[1].Y:= ft;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   168
TextureBuffer[2].X:= fr;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   169
TextureBuffer[2].Y:= fb;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   170
TextureBuffer[3].X:= fl;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   171
TextureBuffer[3].Y:= fb;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   172
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   173
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   174
glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   175
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   176
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   177
glPopMatrix
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   178
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   179
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   180
procedure DrawRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   181
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   182
    DrawRotatedTex(SpritesData[Sprite].Texture,
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   183
        SpritesData[Sprite].Width,
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   184
        SpritesData[Sprite].Height,
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   185
        X, Y, Dir, Angle)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   186
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   187
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   188
procedure DrawRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   189
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   190
glPushMatrix;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   191
glTranslatef(X, Y, 0);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   192
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   193
if Dir < 0 then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   194
   glRotatef(Angle, 0, 0, -1)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   195
else
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   196
   glRotatef(Angle, 0, 0,  1);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   197
if Dir < 0 then glScalef(-1.0, 1.0, 1.0);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   198
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   199
DrawSprite(Sprite, -SpritesData[Sprite].Width div 2, -SpritesData[Sprite].Height div 2, Frame);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   200
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   201
glPopMatrix
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   202
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   203
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   204
procedure DrawRotatedTex(Tex: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   205
var VertexBuffer: array [0..3] of TVertex2f;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   206
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   207
// don't draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   208
if (abs(X) > 2 * hw) and ((abs(X) - hw) > cScreenWidth / cScaleFactor) then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   209
    exit;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   210
if (abs(Y) > 2 * hh) and ((abs(Y - 0.5 * cScreenHeight) - hh) > cScreenHeight / cScaleFactor) then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   211
    exit;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   212
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   213
glPushMatrix;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   214
glTranslatef(X, Y, 0);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   215
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   216
if Dir < 0 then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   217
   begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   218
   hw:= - hw;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   219
   glRotatef(Angle, 0, 0, -1);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   220
   end else
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   221
   glRotatef(Angle, 0, 0,  1);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   222
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   223
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   224
glBindTexture(GL_TEXTURE_2D, Tex^.id);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   225
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   226
VertexBuffer[0].X:= -hw;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   227
VertexBuffer[0].Y:= -hh;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   228
VertexBuffer[1].X:= hw;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   229
VertexBuffer[1].Y:= -hh;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   230
VertexBuffer[2].X:= hw;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   231
VertexBuffer[2].Y:= hh;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   232
VertexBuffer[3].X:= -hw;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   233
VertexBuffer[3].Y:= hh;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   234
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   235
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   236
glTexCoordPointer(2, GL_FLOAT, 0, @Tex^.tb);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   237
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   238
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   239
glPopMatrix
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   240
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   241
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   242
procedure DrawSprite (Sprite: TSprite; X, Y, Frame: LongInt);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   243
var row, col, numFramesFirstCol: LongInt;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   244
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   245
numFramesFirstCol:= SpritesData[Sprite].imageHeight div SpritesData[Sprite].Height;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   246
row:= Frame mod numFramesFirstCol;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   247
col:= Frame div numFramesFirstCol;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   248
DrawSprite2 (Sprite, X, Y, col, row);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   249
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   250
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   251
procedure DrawSpriteClipped(Sprite: TSprite; X, Y, TopY, RightX, BottomY, LeftX: LongInt);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   252
var r: TSDL_Rect;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   253
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   254
r.x:= 0;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   255
r.y:= 0;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   256
r.w:= SpritesData[Sprite].Width;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   257
r.h:= SpritesData[Sprite].Height;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   258
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   259
if (X < LeftX) then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   260
    r.x:= LeftX - X;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   261
if (Y < TopY) then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   262
    r.y:= TopY - Y;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   263
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   264
if (Y + SpritesData[Sprite].Height > BottomY) then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   265
    r.h:= BottomY - Y + 1;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   266
if (X + SpritesData[Sprite].Width > RightX) then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   267
    r.w:= RightX - X + 1;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   268
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   269
dec(r.h, r.y);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   270
dec(r.w, r.x);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   271
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   272
DrawFromRect(X + r.x, Y + r.y, @r, SpritesData[Sprite].Texture)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   273
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   274
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   275
procedure DrawSprite2(Sprite: TSprite; X, Y, FrameX, FrameY: LongInt);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   276
var r: TSDL_Rect;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   277
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   278
    r.x:= FrameX * SpritesData[Sprite].Width;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   279
    r.w:= SpritesData[Sprite].Width;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   280
    r.y:= FrameY * SpritesData[Sprite].Height;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   281
    r.h:= SpritesData[Sprite].Height;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   282
    DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   283
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   284
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   285
procedure DrawCentered(X, Top: LongInt; Source: PTexture);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   286
var scale: GLfloat;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   287
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   288
    if (Source^.w + 20) > cScreenWidth then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   289
        scale:= cScreenWidth / (Source^.w + 20)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   290
    else
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   291
        scale:= 1.0;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   292
    DrawTexture(X - round(Source^.w * scale) div 2, Top, Source, scale)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   293
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   294
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   295
procedure DrawLine(X0, Y0, X1, Y1, Width: Single; r, g, b, a: Byte);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   296
var VertexBuffer: array [0..3] of TVertex2f;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   297
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   298
    glDisable(GL_TEXTURE_2D);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   299
    glEnable(GL_LINE_SMOOTH);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   300
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   301
    glPushMatrix;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   302
    glTranslatef(WorldDx, WorldDy, 0);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   303
    glLineWidth(Width);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   304
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   305
    Tint(r, g, b, a);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   306
    VertexBuffer[0].X:= X0;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   307
    VertexBuffer[0].Y:= Y0;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   308
    VertexBuffer[1].X:= X1;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   309
    VertexBuffer[1].Y:= Y1;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   310
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   311
    glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   312
    glDrawArrays(GL_LINES, 0, Length(VertexBuffer));
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   313
    Tint($FF, $FF, $FF, $FF);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   314
    
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   315
    glPopMatrix;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   316
    
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   317
    glEnable(GL_TEXTURE_2D);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   318
    glDisable(GL_LINE_SMOOTH);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   319
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   320
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   321
procedure DrawFillRect(r: TSDL_Rect);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   322
var VertexBuffer: array [0..3] of TVertex2f;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   323
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   324
// don't draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   325
if (abs(r.x) > r.w) and ((abs(r.x + r.w / 2) - r.w / 2) * cScaleFactor > cScreenWidth) then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   326
    exit;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   327
if (abs(r.y) > r.h) and ((abs(r.y + r.h / 2 - (0.5 * cScreenHeight)) - r.h / 2) * cScaleFactor > cScreenHeight) then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   328
    exit;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   329
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   330
glDisable(GL_TEXTURE_2D);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   331
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   332
Tint($00, $00, $00, $80);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   333
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   334
VertexBuffer[0].X:= r.x;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   335
VertexBuffer[0].Y:= r.y;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   336
VertexBuffer[1].X:= r.x + r.w;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   337
VertexBuffer[1].Y:= r.y;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   338
VertexBuffer[2].X:= r.x + r.w;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   339
VertexBuffer[2].Y:= r.y + r.h;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   340
VertexBuffer[3].X:= r.x;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   341
VertexBuffer[3].Y:= r.y + r.h;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   342
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   343
glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   344
glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   345
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   346
Tint($FF, $FF, $FF, $FF);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   347
glEnable(GL_TEXTURE_2D)
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   348
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   349
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   350
procedure DrawCircle(X, Y, Radius: LongInt; Width: Single; r, g, b, a: Byte); 
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   351
var
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   352
    i: LongInt;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   353
    CircleVertex: array [0..359] of TVertex2f;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   354
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   355
    for i := 0 to 359 do begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   356
        CircleVertex[i].X := X + Radius*cos(i*pi/180);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   357
        CircleVertex[i].Y := Y + Radius*sin(i*pi/180);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   358
    end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   359
    glDisable(GL_TEXTURE_2D);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   360
    glEnable(GL_LINE_SMOOTH);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   361
    glPushMatrix;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   362
    glLineWidth(Width);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   363
    Tint(r, g, b, a);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   364
    glVertexPointer(2, GL_FLOAT, 0, @CircleVertex[0]);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   365
    glDrawArrays(GL_LINE_LOOP, 0, 360);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   366
    Tint($FF, $FF, $FF, $FF);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   367
    glPopMatrix;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   368
    glEnable(GL_TEXTURE_2D);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   369
    glDisable(GL_LINE_SMOOTH);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   370
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   371
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   372
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   373
procedure Tint(r, g, b, a: Byte); inline;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   374
var nc: Longword;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   375
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   376
nc:= (a shl 24) or (b shl 16) or (g shl 8) or r;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   377
if nc = lastTint then
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   378
    exit;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   379
glColor4ub(r, g, b, a);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   380
lastTint:= nc;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   381
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   382
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   383
procedure Tint(c: Longword); inline;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   384
begin
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   385
Tint(((c shr 16) and $FF), ((c shr 8) and $FF), (c and $FF), $FF);
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   386
end;
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   387
91655618a510 Introduce uRender
unC0Rr
parents:
diff changeset
   388
end.