Debug commit
authorWolfgang Steffens <WolfgangSteff@gmail.com>
Tue, 10 Jul 2012 16:38:50 +0200
changeset 7377 1aceade403ba
parent 7374 514138949c76
child 7380 8bf3ad5a6f13
child 7381 ce3586dca0ed
Debug commit Added code to replace sprites with a checkerboard pattern in order to identify filtering issues. This version only renders DrawSprite()'s with the checkerboard pattern for debugging purposes cleaned up some unneeded stuff
hedgewars/uAtlas.pas
hedgewars/uRender.pas
hedgewars/uTextures.pas
--- a/hedgewars/uAtlas.pas	Tue Jul 10 11:09:38 2012 +0200
+++ b/hedgewars/uAtlas.pas	Tue Jul 10 16:38:50 2012 +0200
@@ -128,18 +128,8 @@
 
 
 procedure writefunc(png: png_structp; buffer: png_bytep; size: QWord); cdecl;
-var
-    p: Pbyte;
-    i: Integer;
 begin
-  //TStream(png_get_io_ptr(png)).Write(buffer^, size);
     BlockWrite(DumpFile, buffer^, size);
-{    p:= PByte(buffer^);
-    for i:=0 to pred(size) do
-    begin
-        Write(DumpFile, p^);
-        inc(p);
-    end;}
 end;
 
 function IntToStrPad(i: Integer): string;
@@ -397,9 +387,6 @@
 begin
     sz:= surf^.w * surf^.h;
     p:= surf^.pixels;
-    //randr:=Random;
-    //randg:=Random;
-    //randb:=1 - min(randr, randg);
     randh:=Random;
     HSVToRGB(randh, 1.0, 1.0, randr, randg, randb);
     for i:=0 to pred(sz) do
@@ -422,6 +409,50 @@
     end;
 end;
 
+procedure DebugChecker(surf: PSDL_Surface);
+var
+    sz: Integer;
+    p, q: PByte;
+    randr, randg, randb: Single;
+    randrb, randgb, randbb: Byte;
+    randh: Single;
+    x, y: Integer;
+begin
+    sz:= surf^.w * surf^.h;
+    p:= surf^.pixels;
+    randh:=Random;
+    HSVToRGB(randh, 1.0, 1.0, randr, randg, randb);
+    randrb:= Trunc(255*randr);
+    randgb:= Trunc(255*randg);
+    randbb:= Trunc(255*randb);
+
+    p:= surf^.pixels;
+    for y:=0 to Pred(surf^.h) do
+    begin
+        q:= p;
+        for x:=0 to Pred(surf^.w) do
+        begin
+            if ((x xor y) and 1) = 1 then
+            begin
+                q[0]:= randrb;
+                q[1]:= randgb;
+                q[2]:= randbb;
+                q[3]:= 255;
+            end else
+            begin
+                q[0]:= 0;
+                q[1]:= 0;
+                q[2]:= 0;
+                q[3]:= 255;
+            end;
+            inc(q, 4);
+        end;
+        inc(p, surf^.pitch);
+    end;
+    
+end;
+
+
 procedure Upload(var info: AtlasInfo; sprite: Rectangle; surf: PSDL_Surface);
 var
     sp: PTexture;
@@ -442,6 +473,7 @@
     //if GrayScale then
     //    Surface2GrayScale(surf);
     //DebugColorize(surf);
+    DebugChecker(surf);
 
     glBindTexture(GL_TEXTURE_2D, info.TextureInfo.id);
     if (sp^.isRotated) then
--- a/hedgewars/uRender.pas	Tue Jul 10 11:09:38 2012 +0200
+++ b/hedgewars/uRender.pas	Tue Jul 10 16:38:50 2012 +0200
@@ -122,14 +122,14 @@
 
 procedure DrawSpriteFromRect(Sprite: TSprite; r: TSDL_Rect; X, Y, Height, Position: LongInt);
 begin
-r.y:= r.y + Height * Position;
-r.h:= Height;
-DrawTextureFromRect(X, Y, @r, SpritesData[Sprite].Texture)
+    r.y:= r.y + Height * Position;
+    r.h:= Height;
+    DrawTextureFromRect(X, Y, @r, SpritesData[Sprite].Texture)
 end;
 
 procedure DrawTextureFromRect(X, Y: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
 begin
-DrawTextureFromRect(X, Y, r^.w, r^.h, r, SourceTexture)
+    DrawTextureFromRect(X, Y, r^.w, r^.h, r, SourceTexture)
 end;
 
 procedure DrawTextureFromRect(X, Y, W, H: LongInt; r: PSDL_Rect; SourceTexture: PTexture);
@@ -174,7 +174,7 @@
     SetVertexPointer(@VertexBuffer[0]);
     //SetTexCoordPointer(@TextureBuffer[0]);
     SetTexCoordPointer(@SourceTexture^.tb[0]);
-    glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
+    //glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
 end;
 
 procedure DrawTexture(X, Y: LongInt; Texture: PTexture); inline;
@@ -184,17 +184,18 @@
 
 procedure DrawTexture(X, Y: LongInt; Texture: PTexture; Scale: GLfloat);
 begin
-SetOffset(X, Y);
-ResetRotation;
-SetScale(Scale);
-UpdateModelview;
+    SetOffset(X, Y);
+    ResetRotation;
+    SetScale(Scale);
+    UpdateModelview;
 
-glBindTexture(GL_TEXTURE_2D, Texture^.atlas^.id);
+    glBindTexture(GL_TEXTURE_2D, Texture^.atlas^.id);
+    SetVertexPointer(@Texture^.vb);
+    SetTexCoordPointer(@Texture^.tb);
+    //glDrawArrays(GL_TRIANGLE_FAN, 0, Length(Texture^.vb));
+    glBindTexture(GL_TEXTURE_2D, 0); // DEBUG
 
-SetVertexPointer(@Texture^.vb);
-SetTexCoordPointer(@Texture^.tb);
-glDrawArrays(GL_TRIANGLE_FAN, 0, Length(Texture^.vb));
-ResetModelview;
+    ResetModelview;
 end;
 
 procedure DrawTextureF(Texture: PTexture; Scale: GLfloat; X, Y, Frame, Dir, w, h: LongInt);
@@ -218,53 +219,57 @@
     if Texture = nil then
         exit;
 
-// do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs)
-if (abs(X) > W) and ((abs(X + dir * OffsetX) - W / 2) * cScaleFactor > cScreenWidth) then
-    exit;
-if (abs(Y) > H) and ((abs(Y + OffsetY - (0.5 * cScreenHeight)) - W / 2) * cScaleFactor > cScreenHeight) then
-    exit;
+    // do not draw anything outside the visible screen space (first check fixes some sprite drawing, e.g. hedgehogs)
+    if (abs(X) > W) and ((abs(X + dir * OffsetX) - W / 2) * cScaleFactor > cScreenWidth) then
+        exit;
+    if (abs(Y) > H) and ((abs(Y + OffsetY - (0.5 * cScreenHeight)) - W / 2) * cScaleFactor > cScreenHeight) then
+        exit;
+
 
-SetOffset(X, Y);
-if Dir = 0 then Dir:= 1;
+    SetOffset(X, Y);
+    if Dir = 0 then
+        Dir:= 1;
 
-SetRotation(Angle, Dir);
-AddOffset(Dir*OffsetX, OffsetY);
-AddScale(Scale);
-UpdateModelview;
+    SetRotation(Angle, Dir);
+    AddOffset(Dir*OffsetX, OffsetY);
+    AddScale(Scale);
+    UpdateModelview;
 
 // Any reason for this call? And why only in t direction, not s?
 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
-hw:= w div (2 div Dir);
+    hw:= w div (2 div Dir);
 
-r.y:=0;
-r.x:=0;
-r.w:=w;
-r.h:=h;
-ComputeTexcoords(Texture, @r, @TextureBuffer);
+    r.y:=0;
+    r.x:=0;
+    r.w:=w;
+    r.h:=h;
+    ComputeTexcoords(Texture, @r, @TextureBuffer);
 
-glBindTexture(GL_TEXTURE_2D, Texture^.atlas^.id);
+    glBindTexture(GL_TEXTURE_2D, Texture^.atlas^.id);
 
-_l:= -hw + Texture^.cropInfo.l;
-_t:= w/-2 + Texture^.cropInfo.t;
-_r:= hw - Texture^.cropInfo.l - Texture^.cropInfo.r;
-_b:= w/2 - Texture^.cropInfo.t - Texture^.cropInfo.b;
+    _l:= -hw + Texture^.cropInfo.l;
+    _t:= w/-2 + Texture^.cropInfo.t;
+    _r:= hw - Texture^.cropInfo.l - Texture^.cropInfo.r;
+    _b:= w/2 - Texture^.cropInfo.t - Texture^.cropInfo.b;
 
-VertexBuffer[0].X:= _l;
-VertexBuffer[0].Y:= _t;
-VertexBuffer[1].X:= _r;
-VertexBuffer[1].Y:= _t;
-VertexBuffer[2].X:= _r;
-VertexBuffer[2].Y:= _b;
-VertexBuffer[3].X:= _l;
-VertexBuffer[3].Y:= _b;
+    VertexBuffer[0].X:= _l;
+    VertexBuffer[0].Y:= _t;
+    VertexBuffer[1].X:= _r;
+    VertexBuffer[1].Y:= _t;
+    VertexBuffer[2].X:= _r;
+    VertexBuffer[2].Y:= _b;
+    VertexBuffer[3].X:= _l;
+    VertexBuffer[3].Y:= _b;
 
-SetVertexPointer(@VertexBuffer[0]);
-//SetTexCoordPointer(@TextureBuffer[0]);
-SetTexCoordPointer(@Texture^.tb[0]);
-glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
+    //SetVertexPointer(@VertexBuffer[0]);
+    //SetTexCoordPointer(@TextureBuffer[0]);
 
-ResetModelview;
+    SetVertexPointer(@Texture^.vb[0]);
+    SetTexCoordPointer(@Texture^.tb[0]);
+    //glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
+
+    ResetModelview;
 end;
 
 procedure DrawSpriteRotated(Sprite: TSprite; X, Y, Dir: LongInt; Angle: real);
@@ -275,22 +280,6 @@
         X, Y, Dir, Angle)
 end;
 
-procedure DrawSpriteRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real);
-begin
-SetOffset(X, Y);
-if Dir < 0 then
-    SetRotation(Angle, -1.0)
-else
-    SetRotation(Angle, 1.0);
-if Dir < 0 then
-    AddScale(-1.0, 1.0);
-UpdateModelview;
-
-DrawSprite(Sprite, -SpritesData[Sprite].Width div 2, -SpritesData[Sprite].Height div 2, Frame);
-
-ResetModelview;
-end;
-
 procedure DrawTextureRotated(Texture: PTexture; hw, hh, X, Y, Dir: LongInt; Angle: real);
 var VertexBuffer: array [0..3] of TVertex2f;
     _l, _r, _t, _b: GLfloat;
@@ -330,14 +319,15 @@
 
 SetVertexPointer(@VertexBuffer[0]);
 SetTexCoordPointer(@Texture^.tb);
-glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
+//glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
 
 ResetModelview;
 end;
 
-procedure DrawSprite(Sprite: TSprite; X, Y, Frame: LongInt);
-var 
-    r: TSDL_Rect;
+
+
+procedure DrawSpriteRotatedF(Sprite: TSprite; X, Y, Frame, Dir: LongInt; Angle: real);
+var
     tex: PTexture;
 begin
     if SpritesData[Sprite].imageHeight = 0 then
@@ -354,11 +344,31 @@
     if (tex = nil) or (tex^.w = 0) or (tex^.h = 0) then
         exit;
 
-    r.x:= 0;
-    r.w:= SpritesData[Sprite].Width;
-    r.y:= 0;
-    r.h:= SpritesData[Sprite].Height;
-    DrawTextureFromRect(X, Y, @r, tex)
+    SetOffset(X, Y);
+
+    if Dir < 0 then
+    begin
+        SetRotation(Angle, -1.0);
+        AddScale(-1.0, 1.0);
+    end 
+    else
+        SetRotation(Angle, 1.0);
+        
+    UpdateModelview;
+
+    glBindTexture(GL_TEXTURE_2D, tex^.atlas^.id);
+    SetVertexPointer(@tex^.vb);
+    SetTexCoordPointer(@tex^.tb);
+    glDrawArrays(GL_TRIANGLE_FAN, 0, Length(tex^.vb));
+    glBindTexture(GL_TEXTURE_2D, 0); // DEBUG
+
+    ResetModelview;
+end;
+
+
+procedure DrawSprite(Sprite: TSprite; X, Y, Frame: LongInt);
+begin
+    DrawSpriteRotatedF(Sprite, X, Y, Frame, 1, 0.0);
 end;
 
 procedure DrawSpriteClipped(Sprite: TSprite; X, Y, TopY, RightX, BottomY, LeftX: LongInt);
@@ -413,7 +423,7 @@
     VertexBuffer[1].Y:= Y1;
 
     SetVertexPointer(@VertexBuffer[0]);
-    glDrawArrays(GL_LINES, 0, Length(VertexBuffer));
+    //glDrawArrays(GL_LINES, 0, Length(VertexBuffer));
     Tint($FF, $FF, $FF, $FF);
     
     ResetModelview;
@@ -449,7 +459,7 @@
 VertexBuffer[3].Y:= r.y + r.h;
 
 SetVertexPointer(@VertexBuffer[0]);
-glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
+//glDrawArrays(GL_TRIANGLE_FAN, 0, Length(VertexBuffer));
 
 Tint($FF, $FF, $FF, $FF);
 glEnable(GL_TEXTURE_2D);
@@ -480,7 +490,7 @@
     UpdateModelview;
     glLineWidth(Width);
     SetVertexPointer(@CircleVertex[0]);
-    glDrawArrays(GL_LINE_LOOP, 0, 60);
+    //glDrawArrays(GL_LINE_LOOP, 0, 60);
     glEnable(GL_TEXTURE_2D);
     glDisable(GL_LINE_SMOOTH);
     ResetModelview;
@@ -522,7 +532,7 @@
     else
         SetVertexPointer(@VertexBuffers[0][0]);
     SetTexCoordPointer(@TextureBuffer[0]);
-    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+    //glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
 
     ResetModelview;
 end;
--- a/hedgewars/uTextures.pas	Tue Jul 10 11:09:38 2012 +0200
+++ b/hedgewars/uTextures.pas	Tue Jul 10 16:38:50 2012 +0200
@@ -209,7 +209,8 @@
     w, h, aw, ah: LongInt;
     p: PChar;
 const 
-    texelOffset = 0.0;
+    texelOffsetPos = 0.5;
+    texelOffsetNeg = 0.0;
 begin
     aw:=texture^.atlas^.w;
     ah:=texture^.atlas^.h;
@@ -224,10 +225,10 @@
         h:=r^.h;        
     end;
 
-    x0:= (texture^.x + {r^.x} +     texelOffset)/aw;
-    x1:= (texture^.x + {r^.x} + w - texelOffset)/aw;
-    y0:= (texture^.y + {r^.y} +     texelOffset)/ah;
-    y1:= (texture^.y + {r^.y} + h - texelOffset)/ah;
+    x0:= (texture^.x + {r^.x} +     texelOffsetPos)/aw;
+    x1:= (texture^.x + {r^.x} + w + texelOffsetNeg)/aw;
+    y0:= (texture^.y + {r^.y} +     texelOffsetPos)/ah;
+    y1:= (texture^.y + {r^.y} + h + texelOffsetNeg)/ah;
 
     if (texture^.isRotated) then
     begin
@@ -253,25 +254,35 @@
 end;
 
 procedure ResetVertexArrays(texture: PTexture);
-var r: TSDL_Rect;
+var 
+    rect: TSDL_Rect;
+    l, t, r, b: Real;
+const
+    halfTexelOffsetPos = 1.0;
+    halfTexelOffsetNeg = -0.0;
 begin
+    l:= texture^.cropInfo.l + halfTexelOffsetPos;
+    r:= texture^.cropInfo.l + texture^.w + halfTexelOffsetNeg;
+    t:= texture^.cropInfo.t + halfTexelOffsetPos;
+    b:= texture^.cropInfo.t + texture^.h + halfTexelOffsetNeg;
+
     with texture^ do
     begin
-        vb[0].X:= texture^.cropInfo.l;
-        vb[0].Y:= texture^.cropInfo.t;
-        vb[1].X:= texture^.cropInfo.l + w;
-        vb[1].Y:= texture^.cropInfo.t;
-        vb[2].X:= texture^.cropInfo.l + w;
-        vb[2].Y:= texture^.cropInfo.t + h;
-        vb[3].X:= texture^.cropInfo.l;
-        vb[3].Y:= texture^.cropInfo.t + h;
+        vb[0].X:= l;
+        vb[0].Y:= t;
+        vb[1].X:= r;
+        vb[1].Y:= t;
+        vb[2].X:= r;
+        vb[2].Y:= b;
+        vb[3].X:= l;
+        vb[3].Y:= b;
     end;
 
-    r.x:= 0;
-    r.y:= 0;
-    r.w:= texture^.w;
-    r.h:= texture^.h;
-    ComputeTexcoords(texture, @r, @texture^.tb);
+    rect.x:= 0;
+    rect.y:= 0;
+    rect.w:= texture^.w;
+    rect.h:= texture^.h;
+    ComputeTexcoords(texture, @rect, @texture^.tb);
 end;
 
 function NewTexture(width, height: Longword; buf: Pointer): PTexture;
@@ -403,7 +414,8 @@
         exit;
     end;
 
-    if (surf^.w <= 512) and (surf^.h <= 512) then
+    //if (surf^.w <= 512) and (surf^.h <= 512) then
+    // nothing should use the old codepath anymore once we are done!
     begin
         Surface2Atlas:= Surface2Tex_(surf, enableClamp); // run the atlas side by side for debugging
         Surface2Atlas^.cropInfo:= cropInfo;