hedgewars/uRender.pas
branchwebgl
changeset 8026 4a4f21070479
parent 7069 bcf9d8e64e92
child 8330 aaefa587e277
equal deleted inserted replaced
8023:7de85783b823 8026:4a4f21070479
    15  * along with this program; if not, write to the Free Software
    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
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  *)
    17  *)
    18 
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
       
    20 {$IF GLunit = GL}{$DEFINE GLunit:=GL,GLext}{$ENDIF}
    20 
    21 
    21 unit uRender;
    22 unit uRender;
    22 
    23 
    23 interface
    24 interface
    24 
    25 
    25 uses SDLh, uTypes, GLunit, uConsts;
    26 uses SDLh, uTypes, GLunit, uConsts, uStore, uMatrix;
    26 
    27 
    27 procedure DrawSprite            (Sprite: TSprite; X, Y, Frame: LongInt);
    28 procedure DrawSprite            (Sprite: TSprite; X, Y, Frame: LongInt);
    28 procedure DrawSprite            (Sprite: TSprite; X, Y, FrameX, FrameY: LongInt);
    29 procedure DrawSprite            (Sprite: TSprite; X, Y, FrameX, FrameY: LongInt);
    29 procedure DrawSpriteFromRect    (Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt);
    30 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 DrawSpriteClipped     (Sprite: TSprite; X, Y, TopY, RightX, BottomY, LeftX: LongInt);
    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;
    53 
    54 
    54 
       
    55 implementation
    55 implementation
    56 uses uVariables;
    56 uses uVariables;
    57 
    57 
    58 var LastTint: LongWord = 0;
    58 var LastTint: LongWord = 0;
    59 
    59 
    66 
    66 
    67 procedure DrawTextureFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
    67 procedure DrawTextureFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
    68 begin
    68 begin
    69 DrawTextureFromRect(X, Y, r^.w, r^.h, r, SourceTexture)
    69 DrawTextureFromRect(X, Y, r^.w, r^.h, r, SourceTexture)
    70 end;
    70 end;
    71 
    71 {
    72 procedure DrawTextureFromRect(X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
    72 procedure DrawTextureFromRect(X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
    73 var rr: TSDL_Rect;
    73 var rr: TSDL_Rect;
    74     _l, _r, _t, _b: real;
    74     _l, _r, _t, _b: real;
    75     VertexBuffer, TextureBuffer: array [0..3] of TVertex2f;
    75     VertexBuffer, TextureBuffer: array [0..3] of TVertex2f;
    76 begin
    76 begin
   113 TextureBuffer[3].X:= _l;
   113 TextureBuffer[3].X:= _l;
   114 TextureBuffer[3].Y:= _b;
   114 TextureBuffer[3].Y:= _b;
   115 
   115 
   116 glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
   116 glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
   117 glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]);
   117 glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]);
       
   118 glDrawArrays(GL_TRIANGLE_FAN, 0, High(VertexBuffer) - Low(VertexBuffer) + 1);
       
   119 end;
       
   120 }
       
   121 
       
   122 procedure DrawTextureFromRect(X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
       
   123 var 
       
   124     rr: TSDL_Rect;
       
   125     VertexBuffer, TextureBuffer: array [0..3] of TVertex2f;
       
   126     //VertexBuffer, TextureBuffer: TVertexRect;
       
   127     _l, _r, _t, _b: GLfloat;
       
   128 begin
       
   129 if (SourceTexture^.h = 0) or (SourceTexture^.w = 0) then
       
   130     exit;
       
   131 
       
   132 // do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs)
       
   133 if (abs(X) > W) and ((abs(X + W / 2) - W / 2) > cScreenWidth / cScaleFactor) then
       
   134     exit;
       
   135 if (abs(Y) > H) and ((abs(Y + H / 2 - (0.5 * cScreenHeight)) - H / 2) > cScreenHeight / cScaleFactor) then
       
   136     exit;
       
   137 
       
   138 rr.x:= X;
       
   139 rr.y:= Y;
       
   140 rr.w:= W;
       
   141 rr.h:= H;
       
   142 
       
   143 _l:= r^.x / SourceTexture^.w * SourceTexture^.rx;
       
   144 _r:= (r^.x + r^.w) / SourceTexture^.w * SourceTexture^.rx;
       
   145 _t:= r^.y / SourceTexture^.h * SourceTexture^.ry;
       
   146 _b:= (r^.y + r^.h) / SourceTexture^.h * SourceTexture^.ry;
       
   147 
       
   148 glBindTexture(GL_TEXTURE_2D, SourceTexture^.id);
       
   149 
       
   150 VertexBuffer[0].X:= X;
       
   151 VertexBuffer[0].Y:= Y;
       
   152 VertexBuffer[1].X:= rr.w + X;
       
   153 VertexBuffer[1].Y:= Y;
       
   154 VertexBuffer[2].X:= rr.w + X;
       
   155 VertexBuffer[2].Y:= rr.h + Y;
       
   156 VertexBuffer[3].X:= X;
       
   157 VertexBuffer[3].Y:= rr.h + Y;
       
   158 
       
   159 TextureBuffer[0].X:= _l;
       
   160 TextureBuffer[0].Y:= _t;
       
   161 TextureBuffer[1].X:= _r;
       
   162 TextureBuffer[1].Y:= _t;
       
   163 TextureBuffer[2].X:= _r;
       
   164 TextureBuffer[2].Y:= _b;
       
   165 TextureBuffer[3].X:= _l;
       
   166 TextureBuffer[3].Y:= _b;
       
   167 
       
   168 SetVertexPointer(@VertexBuffer[0], Length(VertexBuffer));
       
   169 SetTexCoordPointer(@TextureBuffer[0], Length(VertexBuffer));
       
   170 
       
   171 {$IFDEF GL2}
       
   172 UpdateModelviewProjection;
       
   173 {$ENDIF}
       
   174 
   118 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   175 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   119 end;
   176 end;
   120 
   177 
   121 procedure DrawTexture(X, Y: LongInt; Texture: PTexture); inline;
   178 procedure DrawTexture(X, Y: LongInt; Texture: PTexture); inline;
   122 begin
   179 begin
   124 end;
   181 end;
   125 
   182 
   126 procedure DrawTexture(X, Y: LongInt; Texture: PTexture; Scale: GLfloat);
   183 procedure DrawTexture(X, Y: LongInt; Texture: PTexture; Scale: GLfloat);
   127 begin
   184 begin
   128 
   185 
       
   186 {$IFDEF GL2}
       
   187 hglPushMatrix;
       
   188 hglTranslatef(X, Y, 0);
       
   189 hglScalef(Scale, Scale, 1);
       
   190 {$ELSE}
   129 glPushMatrix;
   191 glPushMatrix;
   130 glTranslatef(X, Y, 0);
   192 glTranslatef(X, Y, 0);
   131 glScalef(Scale, Scale, 1);
   193 glScalef(Scale, Scale, 1);
       
   194 {$ENDIF}
   132 
   195 
   133 glBindTexture(GL_TEXTURE_2D, Texture^.id);
   196 glBindTexture(GL_TEXTURE_2D, Texture^.id);
   134 
   197 
   135 glVertexPointer(2, GL_FLOAT, 0, @Texture^.vb);
   198 SetVertexPointer(@Texture^.vb, Length(Texture^.vb));
   136 glTexCoordPointer(2, GL_FLOAT, 0, @Texture^.tb);
   199 SetTexCoordPointer(@Texture^.tb, Length(Texture^.vb));
       
   200 
       
   201 {$IFDEF GL2}
       
   202 UpdateModelviewProjection;
   137 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(Texture^.vb));
   203 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(Texture^.vb));
   138 
   204 hglPopMatrix;
   139 glPopMatrix
   205 {$ELSE}
       
   206 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(Texture^.vb));
       
   207 glPopMatrix;
       
   208 {$ENDIF}
       
   209 
   140 end;
   210 end;
   141 
   211 
   142 procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt);
   212 procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt);
   143 begin
   213 begin
   144     DrawTextureRotatedF(Texture, Scale, 0, 0, X, Y, Frame, Dir, w, h, 0)
   214     DrawTextureRotatedF(Texture, Scale, 0, 0, X, Y, Frame, Dir, w, h, 0)
   153 if (abs(X) > W) and ((abs(X + dir * OffsetX) - W / 2) * cScaleFactor > cScreenWidth) then
   223 if (abs(X) > W) and ((abs(X + dir * OffsetX) - W / 2) * cScaleFactor > cScreenWidth) then
   154     exit;
   224     exit;
   155 if (abs(Y) > H) and ((abs(Y + OffsetY - (0.5 * cScreenHeight)) - W / 2) * cScaleFactor > cScreenHeight) then
   225 if (abs(Y) > H) and ((abs(Y + OffsetY - (0.5 * cScreenHeight)) - W / 2) * cScaleFactor > cScreenHeight) then
   156     exit;
   226     exit;
   157 
   227 
       
   228 {$IFDEF GL2}
       
   229 hglPushMatrix;
       
   230 hglTranslatef(X, Y, 0);
       
   231 {$ELSE}
   158 glPushMatrix;
   232 glPushMatrix;
   159 glTranslatef(X, Y, 0);
   233 glTranslatef(X, Y, 0);
       
   234 {$ENDIF}
       
   235 
   160 if Dir = 0 then Dir:= 1;
   236 if Dir = 0 then Dir:= 1;
   161 
   237 
       
   238 {$IFDEF GL2}
       
   239 hglRotatef(Angle, 0, 0, Dir);
       
   240 hglTranslatef(Dir*OffsetX, OffsetY, 0);
       
   241 hglScalef(Scale, Scale, 1);
       
   242 {$ELSE}
   162 glRotatef(Angle, 0, 0, Dir);
   243 glRotatef(Angle, 0, 0, Dir);
   163 
       
   164 glTranslatef(Dir*OffsetX, OffsetY, 0);
   244 glTranslatef(Dir*OffsetX, OffsetY, 0);
   165 glScalef(Scale, Scale, 1);
   245 glScalef(Scale, Scale, 1);
       
   246 {$ENDIF}
   166 
   247 
   167 // Any reason for this call? And why only in t direction, not s?
   248 // Any reason for this call? And why only in t direction, not s?
   168 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   249 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   169 
   250 
   170 hw:= w div (2 div Dir);
   251 hw:= w div (2 div Dir);
   195 TextureBuffer[2].X:= fr;
   276 TextureBuffer[2].X:= fr;
   196 TextureBuffer[2].Y:= fb;
   277 TextureBuffer[2].Y:= fb;
   197 TextureBuffer[3].X:= fl;
   278 TextureBuffer[3].X:= fl;
   198 TextureBuffer[3].Y:= fb;
   279 TextureBuffer[3].Y:= fb;
   199 
   280 
   200 glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
   281 SetVertexPointer(@VertexBuffer[0], Length(VertexBuffer));
   201 glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]);
   282 SetTexCoordPointer(@TextureBuffer[0], Length(VertexBuffer));
       
   283 
       
   284 {$IFDEF GL2}
       
   285 UpdateModelviewProjection;
       
   286 {$ENDIF}
       
   287 
   202 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   288 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   203 
   289 
   204 glPopMatrix
   290 {$IFDEF GL2}
       
   291 hglPopMatrix;
       
   292 {$ELSE}
       
   293 glPopMatrix;
       
   294 {$ENDIF}
       
   295 
   205 end;
   296 end;
   206 
   297 
   207 procedure DrawSpriteRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real);
   298 procedure DrawSpriteRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real);
   208 begin
   299 begin
   209     DrawTextureRotated(SpritesData[Sprite].Texture,
   300     DrawTextureRotated(SpritesData[Sprite].Texture,
   212         X, Y, Dir, Angle)
   303         X, Y, Dir, Angle)
   213 end;
   304 end;
   214 
   305 
   215 procedure DrawSpriteRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real);
   306 procedure DrawSpriteRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real);
   216 begin
   307 begin
       
   308 
       
   309 {$IFDEF GL2}
       
   310 hglPushMatrix;
       
   311 hglTranslatef(X, Y, 0);
       
   312 {$ELSE}
   217 glPushMatrix;
   313 glPushMatrix;
   218 glTranslatef(X, Y, 0);
   314 glTranslatef(X, Y, 0);
       
   315 {$ENDIF}
   219 
   316 
   220 if Dir < 0 then
   317 if Dir < 0 then
       
   318 {$IFDEF GL2}
       
   319     hglRotatef(Angle, 0, 0, -1)
       
   320 {$ELSE}
   221     glRotatef(Angle, 0, 0, -1)
   321     glRotatef(Angle, 0, 0, -1)
       
   322 {$ENDIF}
   222 else
   323 else
       
   324 {$IFDEF GL2}
       
   325     hglRotatef(Angle, 0, 0,  1);
       
   326 {$ELSE}
   223     glRotatef(Angle, 0, 0,  1);
   327     glRotatef(Angle, 0, 0,  1);
       
   328 {$ENDIF}
   224 if Dir < 0 then
   329 if Dir < 0 then
       
   330 {$IFDEF GL2}
       
   331     hglScalef(-1.0, 1.0, 1.0);
       
   332 {$ELSE}
   225     glScalef(-1.0, 1.0, 1.0);
   333     glScalef(-1.0, 1.0, 1.0);
       
   334 {$ENDIF}
   226 
   335 
   227 DrawSprite(Sprite, -SpritesData[Sprite].Width div 2, -SpritesData[Sprite].Height div 2, Frame);
   336 DrawSprite(Sprite, -SpritesData[Sprite].Width div 2, -SpritesData[Sprite].Height div 2, Frame);
   228 
   337 
   229 glPopMatrix
   338 {$IFDEF GL2}
       
   339 hglPopMatrix;
       
   340 {$ELSE}
       
   341 glPopMatrix;
       
   342 {$ENDIF}
       
   343 
   230 end;
   344 end;
   231 
   345 
   232 procedure DrawTextureRotated(Texture: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real);
   346 procedure DrawTextureRotated(Texture: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real);
   233 var VertexBuffer: array [0..3] of TVertex2f;
   347 var VertexBuffer: array [0..3] of TVertex2f;
   234 begin
   348 begin
   236 if (abs(X) > 2 * hw) and ((abs(X) - hw) > cScreenWidth / cScaleFactor) then
   350 if (abs(X) > 2 * hw) and ((abs(X) - hw) > cScreenWidth / cScaleFactor) then
   237     exit;
   351     exit;
   238 if (abs(Y) > 2 * hh) and ((abs(Y - 0.5 * cScreenHeight) - hh) > cScreenHeight / cScaleFactor) then
   352 if (abs(Y) > 2 * hh) and ((abs(Y - 0.5 * cScreenHeight) - hh) > cScreenHeight / cScaleFactor) then
   239     exit;
   353     exit;
   240 
   354 
       
   355 {$IFDEF GL2}
       
   356 hglPushMatrix;
       
   357 hglTranslatef(X, Y, 0);
       
   358 {$ELSE}
   241 glPushMatrix;
   359 glPushMatrix;
   242 glTranslatef(X, Y, 0);
   360 glTranslatef(X, Y, 0);
       
   361 {$ENDIF}
   243 
   362 
   244 if Dir < 0 then
   363 if Dir < 0 then
   245     begin
   364     begin
   246     hw:= - hw;
   365     hw:= - hw;
       
   366 {$IFDEF GL2}
       
   367     hglRotatef(Angle, 0, 0, -1);
       
   368 {$ELSE}
   247     glRotatef(Angle, 0, 0, -1);
   369     glRotatef(Angle, 0, 0, -1);
       
   370 {$ENDIF}
   248     end
   371     end
   249 else
   372 else
   250     glRotatef(Angle, 0, 0,  1);
   373 {$IFDEF GL2}
   251 
   374     hglRotatef(Angle, 0, 0,  1);
       
   375 {$ELSE}
       
   376     glRotatef(Angle, 0, 0, 1);
       
   377 {$ENDIF}
   252 
   378 
   253 glBindTexture(GL_TEXTURE_2D, Texture^.id);
   379 glBindTexture(GL_TEXTURE_2D, Texture^.id);
   254 
   380 
   255 VertexBuffer[0].X:= -hw;
   381 VertexBuffer[0].X:= -hw;
   256 VertexBuffer[0].Y:= -hh;
   382 VertexBuffer[0].Y:= -hh;
   259 VertexBuffer[2].X:= hw;
   385 VertexBuffer[2].X:= hw;
   260 VertexBuffer[2].Y:= hh;
   386 VertexBuffer[2].Y:= hh;
   261 VertexBuffer[3].X:= -hw;
   387 VertexBuffer[3].X:= -hw;
   262 VertexBuffer[3].Y:= hh;
   388 VertexBuffer[3].Y:= hh;
   263 
   389 
   264 glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
   390 SetVertexPointer(@VertexBuffer[0], Length(VertexBuffer));
   265 glTexCoordPointer(2, GL_FLOAT, 0, @Texture^.tb);
   391 SetTexCoordPointer(@Texture^.tb, Length(VertexBuffer));
       
   392 
       
   393 {$IFDEF GL2}
       
   394 UpdateModelviewProjection;
       
   395 {$ENDIF}
       
   396 
   266 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   397 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   267 
   398 
   268 glPopMatrix
   399 {$IFDEF GL2}
       
   400 hglPopMatrix;
       
   401 {$ELSE}
       
   402 glPopMatrix;
       
   403 {$ENDIF}
       
   404 
   269 end;
   405 end;
   270 
   406 
   271 procedure DrawSprite(Sprite: TSprite; X, Y, Frame: LongInt);
   407 procedure DrawSprite(Sprite: TSprite; X, Y, Frame: LongInt);
   272 var row, col, numFramesFirstCol: LongInt;
   408 var row, col, numFramesFirstCol: LongInt;
   273 begin
   409 begin
   322         scale:= 1.0;
   458         scale:= 1.0;
   323     DrawTexture(X - round(Source^.w * scale) div 2, Top, Source, scale)
   459     DrawTexture(X - round(Source^.w * scale) div 2, Top, Source, scale)
   324 end;
   460 end;
   325 
   461 
   326 procedure DrawLine(X0, Y0, X1, Y1, Width: Single; r, g, b, a: Byte);
   462 procedure DrawLine(X0, Y0, X1, Y1, Width: Single; r, g, b, a: Byte);
   327 var VertexBuffer: array [0..3] of TVertex2f;
   463 var VertexBuffer: array [0..1] of TVertex2f;
   328 begin
   464 begin
       
   465     glEnable(GL_LINE_SMOOTH);
       
   466 {$IFNDEF GL2}
   329     glDisable(GL_TEXTURE_2D);
   467     glDisable(GL_TEXTURE_2D);
   330     glEnable(GL_LINE_SMOOTH);
       
   331 
   468 
   332     glPushMatrix;
   469     glPushMatrix;
   333     glTranslatef(WorldDx, WorldDy, 0);
   470     glTranslatef(WorldDx, WorldDy, 0);
   334     glLineWidth(Width);
   471     glLineWidth(Width);
   335 
   472 
   337     VertexBuffer[0].X:= X0;
   474     VertexBuffer[0].X:= X0;
   338     VertexBuffer[0].Y:= Y0;
   475     VertexBuffer[0].Y:= Y0;
   339     VertexBuffer[1].X:= X1;
   476     VertexBuffer[1].X:= X1;
   340     VertexBuffer[1].Y:= Y1;
   477     VertexBuffer[1].Y:= Y1;
   341 
   478 
   342     glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
   479     SetVertexPointer(@VertexBuffer[0], Length(VertexBuffer));
   343     glDrawArrays(GL_LINES, 0, Length(VertexBuffer));
   480     glDrawArrays(GL_LINES, 0, Length(VertexBuffer));
   344     Tint($FF, $FF, $FF, $FF);
   481     Tint($FF, $FF, $FF, $FF);
   345     
   482     
   346     glPopMatrix;
   483     glPopMatrix;
   347     
   484     
   348     glEnable(GL_TEXTURE_2D);
   485     glEnable(GL_TEXTURE_2D);
       
   486 
       
   487 {$ELSE}
       
   488     EnableTexture(False);
       
   489 
       
   490     hglPushMatrix;
       
   491     hglTranslatef(WorldDx, WorldDy, 0);
       
   492     glLineWidth(Width);
       
   493 
       
   494     UpdateModelviewProjection;
       
   495 
       
   496     Tint(r, g, b, a);
       
   497     VertexBuffer[0].X:= X0;
       
   498     VertexBuffer[0].Y:= Y0;
       
   499     VertexBuffer[1].X:= X1;
       
   500     VertexBuffer[1].Y:= Y1;
       
   501 
       
   502     SetVertexPointer(@VertexBuffer[0], Length(VertexBuffer));
       
   503     glDrawArrays(GL_LINES, 0, Length(VertexBuffer));
       
   504     Tint($FF, $FF, $FF, $FF);
       
   505 
       
   506     hglPopMatrix;
       
   507     EnableTexture(True);
       
   508 
       
   509 {$ENDIF}
   349     glDisable(GL_LINE_SMOOTH);
   510     glDisable(GL_LINE_SMOOTH);
   350 end;
   511 end;
   351 
   512 
   352 procedure DrawFillRect(r: TSDL_Rect);
   513 procedure DrawFillRect(r: TSDL_Rect);
   353 var VertexBuffer: array [0..3] of TVertex2f;
   514 var VertexBuffer: array [0..3] of TVertex2f;
   354 begin
   515 begin
   355 // do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs)
   516 // do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs)
       
   517 
   356 if (abs(r.x) > r.w) and ((abs(r.x + r.w / 2) - r.w / 2) * cScaleFactor > cScreenWidth) then
   518 if (abs(r.x) > r.w) and ((abs(r.x + r.w / 2) - r.w / 2) * cScaleFactor > cScreenWidth) then
   357     exit;
   519     exit;
   358 if (abs(r.y) > r.h) and ((abs(r.y + r.h / 2 - (0.5 * cScreenHeight)) - r.h / 2) * cScaleFactor > cScreenHeight) then
   520 if (abs(r.y) > r.h) and ((abs(r.y + r.h / 2 - (0.5 * cScreenHeight)) - r.h / 2) * cScaleFactor > cScreenHeight) then
   359     exit;
   521     exit;
   360 
   522 
       
   523 {$IFDEF GL2}
       
   524 EnableTexture(False);
       
   525 {$ELSE}
   361 glDisable(GL_TEXTURE_2D);
   526 glDisable(GL_TEXTURE_2D);
       
   527 {$ENDIF}
   362 
   528 
   363 Tint($00, $00, $00, $80);
   529 Tint($00, $00, $00, $80);
   364 
   530 
   365 VertexBuffer[0].X:= r.x;
   531 VertexBuffer[0].X:= r.x;
   366 VertexBuffer[0].Y:= r.y;
   532 VertexBuffer[0].Y:= r.y;
   369 VertexBuffer[2].X:= r.x + r.w;
   535 VertexBuffer[2].X:= r.x + r.w;
   370 VertexBuffer[2].Y:= r.y + r.h;
   536 VertexBuffer[2].Y:= r.y + r.h;
   371 VertexBuffer[3].X:= r.x;
   537 VertexBuffer[3].X:= r.x;
   372 VertexBuffer[3].Y:= r.y + r.h;
   538 VertexBuffer[3].Y:= r.y + r.h;
   373 
   539 
   374 glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
   540 SetVertexPointer(@VertexBuffer[0], Length(VertexBuffer));
   375 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   541 glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   376 
   542 
   377 Tint($FF, $FF, $FF, $FF);
   543 Tint($FF, $FF, $FF, $FF);
       
   544 
       
   545 {$IFDEF GL2}
       
   546 EnableTexture(True);
       
   547 {$ELSE}
   378 glEnable(GL_TEXTURE_2D)
   548 glEnable(GL_TEXTURE_2D)
       
   549 {$ENDIF}
       
   550 
   379 end;
   551 end;
   380 
   552 
   381 procedure DrawCircle(X, Y, Radius, Width: LongInt; r, g, b, a: Byte); 
   553 procedure DrawCircle(X, Y, Radius, Width: LongInt; r, g, b, a: Byte); 
   382 begin
   554 begin
   383     Tint(r, g, b, a);
   555     Tint(r, g, b, a);
   392 begin
   564 begin
   393     for i := 0 to 59 do begin
   565     for i := 0 to 59 do begin
   394         CircleVertex[i].X := X + Radius*cos(i*pi/30);
   566         CircleVertex[i].X := X + Radius*cos(i*pi/30);
   395         CircleVertex[i].Y := Y + Radius*sin(i*pi/30);
   567         CircleVertex[i].Y := Y + Radius*sin(i*pi/30);
   396     end;
   568     end;
       
   569 
       
   570 {$IFNDEF GL2}
       
   571 
   397     glDisable(GL_TEXTURE_2D);
   572     glDisable(GL_TEXTURE_2D);
   398     glEnable(GL_LINE_SMOOTH);
   573     glEnable(GL_LINE_SMOOTH);
   399     glPushMatrix;
   574     glPushMatrix;
   400     glLineWidth(Width);
   575     glLineWidth(Width);
   401     glVertexPointer(2, GL_FLOAT, 0, @CircleVertex[0]);
   576     glVertexPointer(2, GL_FLOAT, 0, @CircleVertex[0]);
   402     glDrawArrays(GL_LINE_LOOP, 0, 60);
   577     glDrawArrays(GL_LINE_LOOP, 0, 60);
   403     glPopMatrix;
   578     glPopMatrix;
   404     glEnable(GL_TEXTURE_2D);
   579     glEnable(GL_TEXTURE_2D);
   405     glDisable(GL_LINE_SMOOTH);
   580     glDisable(GL_LINE_SMOOTH);
       
   581 
       
   582 {$ELSE}
       
   583     EnableTexture(False);
       
   584     glEnable(GL_LINE_SMOOTH);
       
   585     hglPushMatrix;
       
   586     glLineWidth(Width);
       
   587     SetVertexPointer(@CircleVertex[0], 60);
       
   588     glDrawArrays(GL_LINE_LOOP, 0, 60);
       
   589     hglPopMatrix;
       
   590     EnableTexture(True);
       
   591     glDisable(GL_LINE_SMOOTH);
       
   592 {$ENDIF}
   406 end;
   593 end;
   407 
   594 
   408 
   595 
   409 procedure DrawHedgehog(X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Angle: real);
   596 procedure DrawHedgehog(X, Y: LongInt; Dir: LongInt; Pos, Step: LongWord; Angle: real);
   410 const VertexBuffer: array [0..3] of TVertex2f = (
   597 const VertexBuffer: array [0..3] of TVertex2f = (
   433         begin
   620         begin
   434         l:= Step * 32 / HHTexture^.w;
   621         l:= Step * 32 / HHTexture^.w;
   435         r:= (Step + 1) * 32 / HHTexture^.w
   622         r:= (Step + 1) * 32 / HHTexture^.w
   436     end;
   623     end;
   437 
   624 
   438 
   625 {$IFDEF GL2}
       
   626     hglPushMatrix();
       
   627     hglTranslatef(X, Y, 0);
       
   628     hglRotatef(Angle, 0, 0, 1);
       
   629 {$ELSE}
   439     glPushMatrix();
   630     glPushMatrix();
   440     glTranslatef(X, Y, 0);
   631     glTranslatef(X, Y, 0);
   441     glRotatef(Angle, 0, 0, 1);
   632     glRotatef(Angle, 0, 0, 1);
       
   633 {$ENDIF}
   442 
   634 
   443     glBindTexture(GL_TEXTURE_2D, HHTexture^.id);
   635     glBindTexture(GL_TEXTURE_2D, HHTexture^.id);
   444 
   636 
   445     TextureBuffer[0].X:= l;
   637     TextureBuffer[0].X:= l;
   446     TextureBuffer[0].Y:= t;
   638     TextureBuffer[0].Y:= t;
   449     TextureBuffer[2].X:= r;
   641     TextureBuffer[2].X:= r;
   450     TextureBuffer[2].Y:= b;
   642     TextureBuffer[2].Y:= b;
   451     TextureBuffer[3].X:= l;
   643     TextureBuffer[3].X:= l;
   452     TextureBuffer[3].Y:= b;
   644     TextureBuffer[3].Y:= b;
   453 
   645 
   454     glVertexPointer(2, GL_FLOAT, 0, @VertexBuffer[0]);
   646     SetVertexPointer(@VertexBuffer[0], Length(VertexBuffer));
   455     glTexCoordPointer(2, GL_FLOAT, 0, @TextureBuffer[0]);
   647     SetTexCoordPointer(@TextureBuffer[0], Length(VertexBuffer));
       
   648 
       
   649 {$IFDEF GL2}
       
   650     UpdateModelviewProjection;
       
   651 {$ENDIF}
       
   652 
   456     glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   653     glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
   457 
   654 
   458     glPopMatrix
   655 {$IFDEF GL2}
       
   656     hglPopMatrix;
       
   657 {$ELSE}
       
   658     glPopMatrix;
       
   659 {$ENDIF}
   459 end;
   660 end;
   460 
   661 
   461 procedure DrawScreenWidget(widget: POnScreenWidget);
   662 procedure DrawScreenWidget(widget: POnScreenWidget);
   462 {$IFDEF USE_TOUCH_INTERFACE}
   663 {$IFDEF USE_TOUCH_INTERFACE}
   463 var alpha: byte = $FF;
   664 var alpha: byte = $FF;
   499         Tint($FF, $FF, $FF, $FF);
   700         Tint($FF, $FF, $FF, $FF);
   500         end;
   701         end;
   501     end;
   702     end;
   502 {$ELSE}
   703 {$ELSE}
   503 begin
   704 begin
   504 widget:= widget; // avoid hint
   705 {widget:= widget; // avoid hint}
   505 {$ENDIF}
   706 {$ENDIF}
   506 end;
   707 end;
   507 
   708 
   508 procedure Tint(r, g, b, a: Byte); inline;
   709 procedure Tint(r, g, b, a: Byte); inline;
   509 var nc, tw: Longword;
   710 var
       
   711     nc, tw: Longword;
       
   712     scale:Real = 1.0/255.0;
   510 begin
   713 begin
   511     nc:= (a shl 24) or (b shl 16) or (g shl 8) or r;
   714     nc:= (a shl 24) or (b shl 16) or (g shl 8) or r;
   512 
   715 
   513     if nc = lastTint then
   716     if nc = lastTint then
   514         exit;
   717         exit;
   521         r:= tw;
   724         r:= tw;
   522         g:= tw;
   725         g:= tw;
   523         b:= tw
   726         b:= tw
   524         end;
   727         end;
   525 
   728 
       
   729     {$IFDEF GL2}
       
   730     glUniform4f(uMainTintLocation, r*scale, g*scale, b*scale, a*scale);
       
   731     //glColor4ub(r, g, b, a);
       
   732     {$ELSE}
   526     glColor4ub(r, g, b, a);
   733     glColor4ub(r, g, b, a);
       
   734     {$ENDIF}
   527     lastTint:= nc;
   735     lastTint:= nc;
   528 end;
   736 end;
   529 
   737 
   530 procedure Tint(c: Longword); inline;
   738 procedure Tint(c: Longword); inline;
   531 begin
   739 begin
   532     Tint(((c shr 24) and $FF), ((c shr 16) and $FF), (c shr 8) and $FF, (c and $FF))
   740     Tint(((c shr 24) and $FF), ((c shr 16) and $FF), (c shr 8) and $FF, (c and $FF))
   533 end;
   741 end;
   534 
   742 
       
   743 
   535 end.
   744 end.