hedgewars/uTextures.pas
changeset 7297 af64b509725c
parent 7295 e70b81854fb9
child 7304 8b3575750cd2
equal deleted inserted replaced
7295:e70b81854fb9 7297:af64b509725c
    47     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    47     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    48     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    48     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    49 end;
    49 end;
    50 
    50 
    51 procedure ComputeTexcoords(texture: PTexture; r: PSDL_Rect; tb: PVertexRect);
    51 procedure ComputeTexcoords(texture: PTexture; r: PSDL_Rect; tb: PVertexRect);
    52 var x0, y0, x1, y1: Real;
    52 var x0, y0, x1, y1, tmp: Real;
    53     w, h, aw, ah: LongInt;
    53     w, h, aw, ah: LongInt;
    54 const texelOffset = 0.0;
    54 const texelOffset = 0.0;
    55 begin
    55 begin
    56 aw:=texture^.atlas^.w;
    56 aw:=texture^.atlas^.w;
    57 ah:=texture^.atlas^.h;
    57 ah:=texture^.atlas^.h;
       
    58 
    58 if texture^.isRotated then
    59 if texture^.isRotated then
    59     begin
    60     begin
    60     w:=r^.h;
    61     w:=r^.h;
    61     h:=r^.w;
    62     h:=r^.w;
    62     end 
    63     end 
    64     begin
    65     begin
    65     w:=r^.w;
    66     w:=r^.w;
    66     h:=r^.h;        
    67     h:=r^.h;        
    67     end;
    68     end;
    68 
    69 
    69 x0:= (r^.x +     texelOffset)/aw;
    70 x0:= (texture^.x + r^.x +     texelOffset)/aw;
    70 x1:= (r^.x + w - texelOffset)/aw;
    71 x1:= (texture^.x + r^.x + w - texelOffset)/aw;
    71 y0:= (r^.y +     texelOffset)/ah;
    72 y0:= (texture^.y + r^.y +     texelOffset)/ah;
    72 y1:= (r^.y + h - texelOffset)/ah;
    73 y1:= (texture^.y + r^.y + h - texelOffset)/ah;
    73 
    74 
    74 tb^[0].X:= x0;
    75 if (texture^.isRotated) then
    75 tb^[0].Y:= y0;
    76 begin
    76 tb^[1].X:= x1;
    77   tb^[0].X:= x0;
    77 tb^[1].Y:= y0;
    78   tb^[0].Y:= y0;
    78 tb^[2].X:= x1;
    79   tb^[3].X:= x1;
    79 tb^[2].Y:= y1;
    80   tb^[3].Y:= y0;
    80 tb^[3].X:= x0;
    81   tb^[2].X:= x1;
    81 tb^[3].Y:= y1
    82   tb^[2].Y:= y1;
       
    83   tb^[1].X:= x0;
       
    84   tb^[1].Y:= y1
       
    85 end else
       
    86 begin
       
    87   tb^[0].X:= x0;
       
    88   tb^[0].Y:= y0;
       
    89   tb^[1].X:= x1;
       
    90   tb^[1].Y:= y0;
       
    91   tb^[2].X:= x1;
       
    92   tb^[2].Y:= y1;
       
    93   tb^[3].X:= x0;
       
    94   tb^[3].Y:= y1;
       
    95 end;
    82 end;
    96 end;
    83 
    97 
    84 procedure ResetVertexArrays(texture: PTexture);
    98 procedure ResetVertexArrays(texture: PTexture);
    85 var r: TSDL_Rect;
    99 var r: TSDL_Rect;
    86 begin
   100 begin
   124 NewTexture^.x:=0;
   138 NewTexture^.x:=0;
   125 NewTexture^.y:=0;
   139 NewTexture^.y:=0;
   126 NewTexture^.w:=width;
   140 NewTexture^.w:=width;
   127 NewTexture^.h:=height;
   141 NewTexture^.h:=height;
   128 NewTexture^.isRotated:=false;
   142 NewTexture^.isRotated:=false;
       
   143 NewTexture^.shared:=false;
       
   144 NewTexture^.surface:=nil;
   129 
   145 
   130 ResetVertexArrays(NewTexture);
   146 ResetVertexArrays(NewTexture);
   131 
   147 
   132 glGenTextures(1, @NewTexture^.atlas^.id);
   148 glGenTextures(1, @NewTexture^.atlas^.id);
   133 
   149 
   162 function Surface2Atlas(surf: PSDL_Surface; enableClamp: boolean): PTexture;
   178 function Surface2Atlas(surf: PSDL_Surface; enableClamp: boolean): PTexture;
   163 var tw, th, x, y: Longword;
   179 var tw, th, x, y: Longword;
   164     tmpp: pointer;
   180     tmpp: pointer;
   165     fromP4, toP4: PLongWordArray;
   181     fromP4, toP4: PLongWordArray;
   166 begin
   182 begin
   167     if (surf^.w <= 128) and (surf^.h <= 128) then
   183     if (surf^.w <= 256) and (surf^.h <= 256) then
   168         Surface2Tex_(surf, enableClamp); // run the atlas side by side for debugging
   184     begin
       
   185         Surface2Atlas:= Surface2Tex_(surf, enableClamp); // run the atlas side by side for debugging
       
   186         ResetVertexArrays(Surface2Atlas);
       
   187         exit;
       
   188     end;
   169 new(Surface2Atlas);
   189 new(Surface2Atlas);
   170 Surface2Atlas^.PrevTexture:= nil;
   190 Surface2Atlas^.PrevTexture:= nil;
   171 Surface2Atlas^.NextTexture:= nil;
   191 Surface2Atlas^.NextTexture:= nil;
   172 if TextureList <> nil then
   192 if TextureList <> nil then
   173     begin
   193     begin
   183 Surface2Atlas^.h:= surf^.h;
   203 Surface2Atlas^.h:= surf^.h;
   184 Surface2Atlas^.x:=0;
   204 Surface2Atlas^.x:=0;
   185 Surface2Atlas^.y:=0;
   205 Surface2Atlas^.y:=0;
   186 Surface2Atlas^.isRotated:=false;
   206 Surface2Atlas^.isRotated:=false;
   187 Surface2Atlas^.surface:= surf;
   207 Surface2Atlas^.surface:= surf;
       
   208 Surface2Atlas^.shared:= false;
   188 
   209 
   189 
   210 
   190 if (surf^.format^.BytesPerPixel <> 4) then
   211 if (surf^.format^.BytesPerPixel <> 4) then
   191     begin
   212     begin
   192     TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true);
   213     TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true);
   258 
   279 
   259 // deletes texture and frees the memory allocated for it.
   280 // deletes texture and frees the memory allocated for it.
   260 // if nil is passed nothing is done
   281 // if nil is passed nothing is done
   261 procedure FreeTexture(tex: PTexture);
   282 procedure FreeTexture(tex: PTexture);
   262 begin
   283 begin
   263     FreeTexture_(tex); // run atlas side by side for debugging
       
   264 if tex <> nil then
   284 if tex <> nil then
   265     begin
   285     begin
       
   286         if tex^.shared then
       
   287         begin
       
   288             FreeTexture_(tex); // run atlas side by side for debugging
       
   289             SDL_FreeSurface(tex^.surface);
       
   290             exit;
       
   291         end;
       
   292 
   266     // Atlas cleanup happens here later on. For now we just free as each sprite has one atlas
   293     // Atlas cleanup happens here later on. For now we just free as each sprite has one atlas
   267     Dispose(tex^.atlas);
   294     Dispose(tex^.atlas);
   268 
   295 
   269     if tex^.NextTexture <> nil then
   296     if tex^.NextTexture <> nil then
   270         tex^.NextTexture^.PrevTexture:= tex^.PrevTexture;
   297         tex^.NextTexture^.PrevTexture:= tex^.PrevTexture;
   272         tex^.PrevTexture^.NextTexture:= tex^.NextTexture
   299         tex^.PrevTexture^.NextTexture:= tex^.NextTexture
   273     else
   300     else
   274         TextureList:= tex^.NextTexture;
   301         TextureList:= tex^.NextTexture;
   275     glDeleteTextures(1, @tex^.atlas^.id);
   302     glDeleteTextures(1, @tex^.atlas^.id);
   276 
   303 
   277     SDL_FreeSurface(tex^.surface);
   304     if (tex^.surface <> nil) then
       
   305         SDL_FreeSurface(tex^.surface);
   278     Dispose(tex);
   306     Dispose(tex);
   279     end
   307     end
   280 end;
   308 end;
   281 
   309 
   282 procedure initModule;
   310 procedure initModule;