hedgewars/uRender.pas
changeset 6999 486db9d26e4b
parent 6992 b8f3d8991e92
child 7028 0f60591f3a16
equal deleted inserted replaced
6998:f2524a80eea2 6999:486db9d26e4b
    22 
    22 
    23 interface
    23 interface
    24 
    24 
    25 uses SDLh, uTypes, GLunit, uConsts;
    25 uses SDLh, uTypes, GLunit, uConsts;
    26 
    26 
    27 procedure DrawFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
    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 
    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 DrawSprite(Sprite: TSprite; X, Y, FrameX, FrameY: LongInt);
    31 procedure DrawSpriteRotated     (Sprite: TSprite; X, Y, Dir: LongInt; Angle: real);
    32 procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt);
    32 procedure DrawSpriteRotatedF    (Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real);
    33 procedure DrawSpriteClipped(Sprite: TSprite; X, Y, TopY, RightX, BottomY, LeftX: LongInt);
    33 
    34 
    34 procedure DrawTexture           (X, Y: LongInt; Texture: PTexture); inline;
    35 procedure DrawTexture(X, Y: LongInt; Texture: PTexture); inline;
    35 procedure DrawTexture           (X, Y: LongInt; Texture: PTexture; Scale: GLfloat);
    36 procedure DrawTexture(X, Y: LongInt; Texture: PTexture; Scale: GLfloat);
    36 procedure DrawTextureFromRect   (X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
    37 procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt);
    37 procedure DrawTextureFromRect   (X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
    38 
    38 procedure DrawTextureCentered   (X, Top: LongInt; Source: PTexture);
    39 procedure DrawRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real);
    39 procedure DrawTextureF          (Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt);
    40 procedure DrawRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real);
    40 procedure DrawTextureRotated    (Texture: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real);
    41 procedure DrawRotatedTex(Tex: 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);
    42 procedure DrawRotatedTextureF(Texture: PTexture; Scale, OffsetX, OffsetY: GLfloat; X, Y, Frame, Dir, w, h: LongInt; Angle: real);
    42 
    43 
    43 procedure DrawCircle            (X, Y, Radius, Width: LongInt);
    44 procedure DrawCentered(X, Top: LongInt; Source: PTexture);
    44 procedure DrawCircle            (X, Y, Radius, Width: LongInt; r, g, b, a: Byte);
    45 procedure DrawLine(X0, Y0, X1, Y1, Width: Single; r, g, b, a: Byte);
    45 
    46 procedure DrawFillRect(r: TSDL_Rect);
    46 procedure DrawLine              (X0, Y0, X1, Y1, Width: Single; r, g, b, a: Byte);
    47 procedure DrawCircle(X, Y, Radius, Width: LongInt);
    47 procedure DrawFillRect          (r: TSDL_Rect);
    48 procedure DrawCircle(X, Y, Radius, Width: LongInt; r, g, b, a: Byte);
    48 procedure DrawHedgehog          (X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Angle: real);
    49 procedure DrawHedgehog(X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Angle: real);
    49 procedure DrawScreenWidget      (widget: POnScreenWidget);
    50 procedure DrawScreenWidget(widget: POnScreenWidget);
    50 
    51 
    51 procedure Tint                  (r, g, b, a: Byte); inline;
    52 procedure Tint(r, g, b, a: Byte); inline;
    52 procedure Tint                  (c: Longword); inline;
    53 procedure Tint(c: Longword); inline;
       
    54 
    53 
    55 
    54 
    56 implementation
    55 implementation
    57 uses uVariables;
    56 uses uVariables;
    58 
    57 
    59 procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt);
    58 procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt);
    60 begin
    59 begin
    61 r.y:= r.y + Height * Position;
    60 r.y:= r.y + Height * Position;
    62 r.h:= Height;
    61 r.h:= Height;
    63 DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture)
    62 DrawTextureFromRect(X, Y, @r, SpritesData[Sprite].Texture)
    64 end;
    63 end;
    65 
    64 
    66 procedure DrawFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
    65 procedure DrawTextureFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
    67 begin
    66 begin
    68 DrawFromRect(X, Y, r^.w, r^.h, r, SourceTexture)
    67 DrawTextureFromRect(X, Y, r^.w, r^.h, r, SourceTexture)
    69 end;
    68 end;
    70 
    69 
    71 procedure DrawFromRect(X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
    70 procedure DrawTextureFromRect(X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
    72 var rr: TSDL_Rect;
    71 var rr: TSDL_Rect;
    73     _l, _r, _t, _b: real;
    72     _l, _r, _t, _b: real;
    74     VertexBuffer, TextureBuffer: array [0..3] of TVertex2f;
    73     VertexBuffer, TextureBuffer: array [0..3] of TVertex2f;
    75 begin
    74 begin
    76 if (SourceTexture^.h = 0) or (SourceTexture^.w = 0) then
    75 if (SourceTexture^.h = 0) or (SourceTexture^.w = 0) then
   110 TextureBuffer[2].X:= _r;
   109 TextureBuffer[2].X:= _r;
   111 TextureBuffer[2].Y:= _b;
   110 TextureBuffer[2].Y:= _b;
   112 TextureBuffer[3].X:= _l;
   111 TextureBuffer[3].X:= _l;
   113 TextureBuffer[3].Y:= _b;
   112 TextureBuffer[3].Y:= _b;
   114 
   113 
   115 
       
   116 glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
   114 glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
   117 glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]);
   115 glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]);
   118 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   116 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   119 end;
   117 end;
   120 
   118 
   139 glPopMatrix
   137 glPopMatrix
   140 end;
   138 end;
   141 
   139 
   142 procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt);
   140 procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt);
   143 begin
   141 begin
   144     DrawRotatedTextureF(Texture, Scale, 0, 0, X, Y, Frame, Dir, w, h, 0)
   142     DrawTextureRotatedF(Texture, Scale, 0, 0, X, Y, Frame, Dir, w, h, 0)
   145 end;
   143 end;
   146 
   144 
   147 procedure DrawRotatedTextureF(Texture: PTexture; Scale, OffsetX, OffsetY: GLfloat; X, Y, Frame, Dir, w, h: LongInt; Angle: real);
   145 procedure DrawTextureRotatedF(Texture: PTexture; Scale, OffsetX, OffsetY: GLfloat; X, Y, Frame, Dir, w, h: LongInt; Angle: real);
   148 var ft, fb, fl, fr: GLfloat;
   146 var ft, fb, fl, fr: GLfloat;
   149     hw, nx, ny: LongInt;
   147     hw, nx, ny: LongInt;
   150     VertexBuffer, TextureBuffer: array [0..3] of TVertex2f;
   148     VertexBuffer, TextureBuffer: array [0..3] of TVertex2f;
   151 begin
   149 begin
   152 // do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs)
   150 // do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs)
   202 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   200 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   203 
   201 
   204 glPopMatrix
   202 glPopMatrix
   205 end;
   203 end;
   206 
   204 
   207 procedure DrawRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real);
   205 procedure DrawSpriteRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real);
   208 begin
   206 begin
   209     DrawRotatedTex(SpritesData[Sprite].Texture,
   207     DrawTextureRotated(SpritesData[Sprite].Texture,
   210         SpritesData[Sprite].Width,
   208         SpritesData[Sprite].Width,
   211         SpritesData[Sprite].Height,
   209         SpritesData[Sprite].Height,
   212         X, Y, Dir, Angle)
   210         X, Y, Dir, Angle)
   213 end;
   211 end;
   214 
   212 
   215 procedure DrawRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real);
   213 procedure DrawSpriteRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real);
   216 begin
   214 begin
   217 glPushMatrix;
   215 glPushMatrix;
   218 glTranslatef(X, Y, 0);
   216 glTranslatef(X, Y, 0);
   219 
   217 
   220 if Dir < 0 then
   218 if Dir < 0 then
   227 DrawSprite(Sprite, -SpritesData[Sprite].Width div 2, -SpritesData[Sprite].Height div 2, Frame);
   225 DrawSprite(Sprite, -SpritesData[Sprite].Width div 2, -SpritesData[Sprite].Height div 2, Frame);
   228 
   226 
   229 glPopMatrix
   227 glPopMatrix
   230 end;
   228 end;
   231 
   229 
   232 procedure DrawRotatedTex(Tex: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real);
   230 procedure DrawTextureRotated(Texture: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real);
   233 var VertexBuffer: array [0..3] of TVertex2f;
   231 var VertexBuffer: array [0..3] of TVertex2f;
   234 begin
   232 begin
   235 // do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs)
   233 // do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs)
   236 if (abs(X) > 2 * hw) and ((abs(X) - hw) > cScreenWidth / cScaleFactor) then
   234 if (abs(X) > 2 * hw) and ((abs(X) - hw) > cScreenWidth / cScaleFactor) then
   237     exit;
   235     exit;
   248     end
   246     end
   249 else
   247 else
   250     glRotatef(Angle, 0, 0,  1);
   248     glRotatef(Angle, 0, 0,  1);
   251 
   249 
   252 
   250 
   253 glBindTexture(GL_TEXTURE_2D, Tex^.id);
   251 glBindTexture(GL_TEXTURE_2D, Texture^.id);
   254 
   252 
   255 VertexBuffer[0].X:= -hw;
   253 VertexBuffer[0].X:= -hw;
   256 VertexBuffer[0].Y:= -hh;
   254 VertexBuffer[0].Y:= -hh;
   257 VertexBuffer[1].X:= hw;
   255 VertexBuffer[1].X:= hw;
   258 VertexBuffer[1].Y:= -hh;
   256 VertexBuffer[1].Y:= -hh;
   260 VertexBuffer[2].Y:= hh;
   258 VertexBuffer[2].Y:= hh;
   261 VertexBuffer[3].X:= -hw;
   259 VertexBuffer[3].X:= -hw;
   262 VertexBuffer[3].Y:= hh;
   260 VertexBuffer[3].Y:= hh;
   263 
   261 
   264 glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
   262 glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
   265 glTexCoordPointer(2, GL_FLOAT, 0, @Tex^.tb);
   263 glTexCoordPointer(2, GL_FLOAT, 0, @Texture^.tb);
   266 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   264 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   267 
   265 
   268 glPopMatrix
   266 glPopMatrix
   269 end;
   267 end;
   270 
   268 
   284 begin
   282 begin
   285     r.x:= FrameX * SpritesData[Sprite].Width;
   283     r.x:= FrameX * SpritesData[Sprite].Width;
   286     r.w:= SpritesData[Sprite].Width;
   284     r.w:= SpritesData[Sprite].Width;
   287     r.y:= FrameY * SpritesData[Sprite].Height;
   285     r.y:= FrameY * SpritesData[Sprite].Height;
   288     r.h:= SpritesData[Sprite].Height;
   286     r.h:= SpritesData[Sprite].Height;
   289     DrawFromRect(X, Y, @r, SpritesData[Sprite].Texture)
   287     DrawTextureFromRect(X, Y, @r, SpritesData[Sprite].Texture)
   290 end;
   288 end;
   291 
   289 
   292 procedure DrawSpriteClipped(Sprite: TSprite; X, Y, TopY, RightX, BottomY, LeftX: LongInt);
   290 procedure DrawSpriteClipped(Sprite: TSprite; X, Y, TopY, RightX, BottomY, LeftX: LongInt);
   293 var r: TSDL_Rect;
   291 var r: TSDL_Rect;
   294 begin
   292 begin
   308     r.w:= RightX - X + 1;
   306     r.w:= RightX - X + 1;
   309 
   307 
   310 dec(r.h, r.y);
   308 dec(r.h, r.y);
   311 dec(r.w, r.x);
   309 dec(r.w, r.x);
   312 
   310 
   313 DrawFromRect(X + r.x, Y + r.y, @r, SpritesData[Sprite].Texture)
   311 DrawTextureFromRect(X + r.x, Y + r.y, @r, SpritesData[Sprite].Texture)
   314 end;
   312 end;
   315 
   313 
   316 procedure DrawCentered(X, Top: LongInt; Source: PTexture);
   314 procedure DrawTextureCentered(X, Top: LongInt; Source: PTexture);
   317 var scale: GLfloat;
   315 var scale: GLfloat;
   318 begin
   316 begin
   319     if (Source^.w + 20) > cScreenWidth then
   317     if (Source^.w + 20) > cScreenWidth then
   320         scale:= cScreenWidth / (Source^.w + 20)
   318         scale:= cScreenWidth / (Source^.w + 20)
   321     else
   319     else