hedgewars/uAtlas.pas
changeset 7304 8b3575750cd2
parent 7301 bea42438a2ec
child 7377 1aceade403ba
equal deleted inserted replaced
7301:bea42438a2ec 7304:8b3575750cd2
    10 procedure initModule;
    10 procedure initModule;
    11 
    11 
    12 function Surface2Tex_(surf: PSDL_Surface; enableClamp: boolean): PTexture;
    12 function Surface2Tex_(surf: PSDL_Surface; enableClamp: boolean): PTexture;
    13 procedure FreeTexture_(sprite: PTexture);
    13 procedure FreeTexture_(sprite: PTexture);
    14 procedure DebugAtlas;
    14 procedure DebugAtlas;
       
    15 procedure DumpInfo(tex: PTexture);
    15 
    16 
    16 implementation
    17 implementation
    17 
    18 
    18 uses GLunit, uBinPacker, uDebug, png, sysutils, uTextures;
    19 uses GLunit, uBinPacker, uDebug, png, sysutils, uTextures;
    19 
    20 
    26 type
    27 type
    27     AtlasInfo = record
    28     AtlasInfo = record
    28         PackerInfo: Atlas;     // Rectangle packer context
    29         PackerInfo: Atlas;     // Rectangle packer context
    29         TextureInfo: TAtlas;   // OpenGL texture information
    30         TextureInfo: TAtlas;   // OpenGL texture information
    30         Allocated: boolean;    // indicates if this atlas is in use
    31         Allocated: boolean;    // indicates if this atlas is in use
       
    32         DumpID: Integer;
    31     end;
    33     end;
    32 
    34 
    33 var
    35 var
    34     Info: array[0..MaxAtlases-1] of AtlasInfo;
    36     Info: array[0..MaxAtlases-1] of AtlasInfo;
    35 
    37 
    36 
    38 
    37 ////////////////////////////////////////////////////////////////////////////////
    39 ////////////////////////////////////////////////////////////////////////////////
    38 // Debug routines
    40 // Debug routines
       
    41 
       
    42 procedure DumpInfo(tex: PTexture);
       
    43 var
       
    44     frame: Integer;
       
    45     i, atlasID: Integer;
       
    46     aw, ah: Integer;
       
    47 begin
       
    48     if tex = nil then
       
    49         exit;
       
    50 
       
    51     frame:= 0;
       
    52     writeln(stdout, 'Texture: ' + IntToHex(Integer(tex), 8));
       
    53 
       
    54     while tex <> nil do
       
    55     begin
       
    56         atlasID:= -1;
       
    57         for i:= 0 to Pred(MaxAtlases) do
       
    58             if tex^.atlas = @Info[i].TextureInfo then
       
    59                 atlasID:=i;
       
    60 
       
    61         aw:= tex^.atlas^.w;
       
    62         ah:= tex^.atlas^.h;   
       
    63  
       
    64         writeln(stdout, 'Frame   : ' + IntToStr(frame));
       
    65         writeln(stdout, 'Size    : ' + IntToStr(tex^.w) + 'x' + IntToStr(tex^.h));
       
    66         writeln(stdout, 'Atlas   : ' + IntToStr(atlasID));
       
    67         writeln(stdout, 'Location: ' + IntToStr(tex^.x) + 'x' + IntToStr(tex^.y));
       
    68         writeln(stdout, 'TB      : ' + '(' + FloatToStrF(tex^.tb[0].X, ffFixed, 15, 4) + ',' + FloatToStrF(tex^.tb[0].Y, ffFixed, 15, 4) + ') '
       
    69                                      + '(' + FloatToStrF(tex^.tb[1].X, ffFixed, 15, 4) + ',' + FloatToStrF(tex^.tb[1].Y, ffFixed, 15, 4) + ') '
       
    70                                      + '(' + FloatToStrF(tex^.tb[2].X, ffFixed, 15, 4) + ',' + FloatToStrF(tex^.tb[2].Y, ffFixed, 15, 4) + ') '
       
    71                                      + '(' + FloatToStrF(tex^.tb[3].X, ffFixed, 15, 4) + ',' + FloatToStrF(tex^.tb[3].Y, ffFixed, 15, 4) + ')');
       
    72 
       
    73         writeln(stdout, 'TB.ABS  : ' + '(' + FloatToStrF(tex^.tb[0].X * aw, ffFixed, 15, 4) + ',' + FloatToStrF(tex^.tb[0].Y * ah, ffFixed, 15, 4) + ') '
       
    74                                      + '(' + FloatToStrF(tex^.tb[1].X * aw, ffFixed, 15, 4) + ',' + FloatToStrF(tex^.tb[1].Y * ah, ffFixed, 15, 4) + ') '
       
    75                                      + '(' + FloatToStrF(tex^.tb[2].X * aw, ffFixed, 15, 4) + ',' + FloatToStrF(tex^.tb[2].Y * ah, ffFixed, 15, 4) + ') '
       
    76                                      + '(' + FloatToStrF(tex^.tb[3].X * aw, ffFixed, 15, 4) + ',' + FloatToStrF(tex^.tb[3].Y * ah, ffFixed, 15, 4) + ')');
       
    77 
       
    78         inc(frame);
       
    79         tex:= tex^.nextFrame;
       
    80     end;
       
    81     halt(0);
       
    82 end;
    39 
    83 
    40 procedure AssertCount(tex: PTexture; count: Integer);
    84 procedure AssertCount(tex: PTexture; count: Integer);
    41 var
    85 var
    42     i, j: Integer;
    86     i, j: Integer;
    43     found: Integer;
    87     found: Integer;
    70         halt(-2);
   114         halt(-2);
    71     end;
   115     end;
    72 end;
   116 end;
    73 
   117 
    74 var
   118 var
    75     DumpID: Integer;
       
    76     DumpFile: File of byte;
   119     DumpFile: File of byte;
    77 
   120 
    78 const
   121 const
    79     PNG_COLOR_TYPE_RGBA = 6;
   122     PNG_COLOR_TYPE_RGBA = 6;
    80     PNG_COLOR_TYPE_RGB = 2;
   123     PNG_COLOR_TYPE_RGB = 2;
   104   s: string;
   147   s: string;
   105 begin
   148 begin
   106    s:= IntToStr(i);
   149    s:= IntToStr(i);
   107    if (i < 10) then s:='0' + s;
   150    if (i < 10) then s:='0' + s;
   108    if (i < 100) then s:='0' + s;
   151    if (i < 100) then s:='0' + s;
       
   152    if (i < 1000) then s:='0' + s;
   109 
   153 
   110    IntToStrPad:=s;
   154    IntToStrPad:=s;
   111 end;
   155 end;
   112 
   156 
   113 // GL1 ATLAS DEBUG ONLY CODE!
   157 // GL1 ATLAS DEBUG ONLY CODE!
   114 procedure DebugAtlas;
   158 procedure DebugAtlas;
       
   159 {$IFDEF DEBUG_ATLAS}
   115 var
   160 var
   116     vp: array[0..3] of GLint;
   161     vp: array[0..3] of GLint;
   117     prog: GLint;
   162     prog: GLint;
   118     i: Integer;
   163     i: Integer;
   119     x, y: Integer;
   164     x, y: Integer;
   166 {$IFDEF GL2}
   211 {$IFDEF GL2}
   167         glUseProgram(prog);
   212         glUseProgram(prog);
   168 {$ENDIF}
   213 {$ENDIF}
   169     end;
   214     end;
   170 end;
   215 end;
   171 
   216 {$ELSE}
   172 procedure DumpAtlas(var info: AtlasInfo);
   217 begin;
       
   218 end;
       
   219 {$ENDIF}
       
   220 
       
   221 procedure DumpAtlas(var dinfo: AtlasInfo);
   173 var
   222 var
   174     png: png_structp;
   223     png: png_structp;
   175     png_info: png_infop;
   224     png_info: png_infop;
   176     w, h, sz: Integer;
   225     w, h, sz: Integer;
   177     filename: string;
   226     filename: string;
   178     rows: array of png_bytep;
   227     rows: array of png_bytep;
   179     size: Integer;
   228     size: Integer;
   180     i, j: Integer;
   229     i, j: Integer;
       
   230     idx: Integer;
   181     mem, p, pp: PByte;
   231     mem, p, pp: PByte;
   182 begin
   232 begin
   183     filename:= '/home/wolfgangst/hedgewars/dump/atlas_' + IntToStrPad(DumpID) + '.png';
   233     idx:= -1;
       
   234     for i:= 0 to Pred(MaxAtlases) do
       
   235         if @dinfo = @Info[i] then
       
   236             idx:=i;
       
   237 
       
   238     filename:= '/home/wolfgangst/hedgewars/dump/atlas_' + IntToStr(idx) + '_' + IntToStrPad(dinfo.DumpID) + '.png';
   184     Assign(DumpFile, filename);
   239     Assign(DumpFile, filename);
   185     inc(DumpID);
   240     inc(dinfo.DumpID);
   186     Rewrite(DumpFile);
   241     Rewrite(DumpFile);
   187 
   242 
   188     w:= info.TextureInfo.w;
   243     w:= dinfo.TextureInfo.w;
   189     h:= info.TextureInfo.h;
   244     h:= dinfo.TextureInfo.h;
   190     size:= w * h * 4;
   245     size:= w * h * 4;
   191     SetLength(rows, h);
   246     SetLength(rows, h);
   192     GetMem(mem, size);
   247     GetMem(mem, size);
   193 
   248 
   194     glBindTexture(GL_TEXTURE_2D, info.TextureInfo.id);
   249     glBindTexture(GL_TEXTURE_2D, dinfo.TextureInfo.id);
   195 
   250 
   196     glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, mem);
   251     glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, mem);
   197 
   252 
   198     p:= mem;
   253     p:= mem;
   199     for i:= 0 to pred(h) do
   254     for i:= 0 to pred(h) do
   372     sp: PTexture;
   427     sp: PTexture;
   373     i, j, stride: Integer;
   428     i, j, stride: Integer;
   374     scanline: PByte;
   429     scanline: PByte;
   375     r: TSDL_Rect;
   430     r: TSDL_Rect;
   376 begin
   431 begin
   377     writeln('Uploading sprite to ', sprite.x, ',', sprite.y, ',', sprite.width, ',', sprite.height);
   432     //writeln('Uploading sprite to ', sprite.x, ',', sprite.y, ',', sprite.width, ',', sprite.height);
   378     sp:= PTexture(sprite.UserData);
   433     sp:= PTexture(sprite.UserData);
   379     sp^.x:= sprite.x;
   434     sp^.x:= sprite.x;
   380     sp^.y:= sprite.y;
   435     sp^.y:= sprite.y;
   381     sp^.isRotated:= sp^.w <> sprite.width;
   436     sp^.isRotated:= sp^.w <> sprite.width;
   382     sp^.atlas:= @info.TextureInfo;
   437     sp^.atlas:= @info.TextureInfo;
   558     sprite^.x:= 0;
   613     sprite^.x:= 0;
   559     sprite^.y:= 0;
   614     sprite^.y:= 0;
   560     sprite^.isRotated:= false;
   615     sprite^.isRotated:= false;
   561     sprite^.surface:= surf;
   616     sprite^.surface:= surf;
   562     sprite^.shared:= true;
   617     sprite^.shared:= true;
       
   618     sprite^.nextFrame:= nil;
   563 
   619 
   564     sz:= SizeForSprite(sprite);
   620     sz:= SizeForSprite(sprite);
   565 
   621 
   566     // STEP 1
   622     // STEP 1
   567     // try to allocate the new sprite in one of the existing atlases
   623     // try to allocate the new sprite in one of the existing atlases
   696 
   752 
   697 procedure initModule;
   753 procedure initModule;
   698 var
   754 var
   699     i: Integer;
   755     i: Integer;
   700 begin
   756 begin
   701     DumpID:=0;
       
   702     for i:= 0 to pred(MaxAtlases) do
   757     for i:= 0 to pred(MaxAtlases) do
       
   758     begin
   703         Info[i].Allocated:= false;
   759         Info[i].Allocated:= false;
       
   760         Info[i].DumpID:=0;
       
   761     end;
   704 end;
   762 end;
   705 
   763 
   706 end.
   764 end.