hedgewars/uTextures.pas
author Wolfgang Steffens <WolfgangSteff@gmail.com>
Tue, 10 Jul 2012 11:08:35 +0200
changeset 7304 8b3575750cd2
parent 7297 af64b509725c
child 7377 1aceade403ba
permissions -rw-r--r--
Added auto cropping to atlasing Added splitting of animation sheets to frames and auto crop the frames. Fixed some atlas blitting issues. Vertex coords are still improper tho for auto cropped frames
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     1
(*
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     2
 * Hedgewars, a free turn based strategy game
6700
e04da46ee43c the most important commit of the year
koda
parents: 6580
diff changeset
     3
 * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     4
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     8
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    12
 * GNU General Public License for more details.
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    13
 *
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    14
 * You should have received a copy of the GNU General Public License
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    15
 * along with this program; if not, write to the Free Software
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    17
 *)
088d40d8aba2 Happy 2011 :)
koda
parents: 4901
diff changeset
    18
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    19
{$INCLUDE "options.inc"}
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    20
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    21
unit uTextures;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    22
interface
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    23
uses SDLh, uTypes;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    24
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    25
function  NewTexture(width, height: Longword; buf: Pointer): PTexture;
6303
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
    26
procedure Surface2GrayScale(surf: PSDL_Surface);
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    27
function SurfaceSheet2Atlas(surf: PSDL_Surface; spriteWidth: Integer; spriteHeight: Integer): PTexture;
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
    28
function  Surface2Atlas(surf: PSDL_Surface; enableClamp: boolean): PTexture;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    29
procedure FreeTexture(tex: PTexture);
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
    30
procedure ComputeTexcoords(texture: PTexture; r: PSDL_Rect; tb: PVertexRect);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    31
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    32
procedure initModule;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    33
procedure freeModule;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    34
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
    35
implementation
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    36
uses GLunit, uUtils, uVariables, uConsts, uDebug, uConsole, uAtlas, SysUtils;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    37
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    38
var
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    39
  logFile: TextFile;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    40
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    41
function CropSurface(source: PSDL_Surface; rect: PSDL_Rect): PSDL_Surface;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    42
var
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    43
    fmt: PSDL_PixelFormat;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    44
    srcP, dstP: PByte;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    45
    copySize: Integer;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    46
    i: Integer;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    47
const
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    48
    pixelSize = 4;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    49
begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    50
    //writeln(stdout, 'Cropping from ' + IntToStr(source^.w) + 'x' + IntToStr(source^.h) + ' -> ' + IntToStr(rect^.w) + 'x' + IntToStr(rect^.h));
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    51
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    52
    fmt:= source^.format;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    53
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    54
    CropSurface:= SDL_CreateRGBSurface(source^.flags, rect^.w, rect^.h, 
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    55
        fmt^.BitsPerPixel, fmt^.Rmask, fmt^.Gmask, fmt^.Bmask, fmt^.Amask);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    56
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    57
    if SDL_MustLock(source) then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    58
        SDLTry(SDL_LockSurface(source) >= 0, true);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    59
    if SDL_MustLock(CropSurface) then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    60
        SDLTry(SDL_LockSurface(CropSurface) >= 0, true);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    61
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    62
    srcP:= source^.pixels;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    63
    dstP:= CropSurface^.pixels;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    64
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    65
    inc(srcP, pixelSize * rect^.x);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    66
    inc(srcP, source^.pitch * rect^.y);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    67
    copySize:= rect^.w * pixelSize;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    68
    for i:= 0 to Pred(rect^.h) do
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    69
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    70
        Move(srcP^, dstP^, copySize);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    71
        inc(srcP, source^.pitch);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    72
        inc(dstP, CropSurface^.pitch);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    73
    end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    74
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    75
    if SDL_MustLock(source) then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    76
        SDL_UnlockSurface(source);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    77
    if SDL_MustLock(CropSurface) then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    78
        SDL_UnlockSurface(CropSurface);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    79
end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    80
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    81
function TransparentLine(p: PByte; stride: Integer; length: Integer): boolean;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    82
var
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    83
    i: Integer;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    84
begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    85
    TransparentLine:= false;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    86
    for i:=0 to pred(length) do
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    87
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    88
        if p^ <> 0 then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    89
            exit;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    90
        inc(p, stride);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    91
    end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    92
    TransparentLine:= true;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    93
end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    94
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    95
function AutoCrop(source: PSDL_Surface; var cropinfo: TCropInformation): PSDL_Surface;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    96
var
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    97
    l,r,t,b, i: Integer;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    98
    pixels, p: PByte;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
    99
    scanlineSize: Integer;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   100
    rect: TSDL_Rect;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   101
const
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   102
    pixelSize = 4;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   103
begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   104
    l:= source^.w; 
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   105
    r:= 0; 
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   106
    t:= source^.h;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   107
    b:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   108
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   109
    if SDL_MustLock(source) then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   110
        SDLTry(SDL_LockSurface(source) >= 0, true);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   111
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   112
    pixels:= source^.pixels;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   113
    scanlineSize:= source^.pitch;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   114
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   115
    inc(pixels, 3); // advance to alpha value
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   116
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   117
    // check top
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   118
    p:= pixels;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   119
    for i:= 0 to Pred(source^.h) do
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   120
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   121
        if not TransparentLine(p, pixelSize, source^.w) then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   122
        begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   123
            t:= i;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   124
            break;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   125
        end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   126
        inc(p, scanlineSize);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   127
    end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   128
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   129
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   130
    // check bottom
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   131
    p:= pixels;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   132
    inc(p, scanlineSize * source^.h);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   133
    for i:= 0 to Pred(source^.h - t) do
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   134
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   135
        dec(p, scanlineSize);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   136
        if not TransparentLine(p, pixelSize, source^.w) then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   137
        begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   138
            b:= i;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   139
            break;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   140
        end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   141
    end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   142
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   143
    // check left
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   144
    p:= pixels;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   145
    for i:= 0 to Pred(source^.w) do
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   146
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   147
        if not TransparentLine(p, scanlineSize, source^.h) then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   148
        begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   149
            l:= i;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   150
            break;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   151
        end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   152
        inc(p, pixelSize);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   153
    end;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   154
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   155
    // check right
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   156
    p:= pixels;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   157
    inc(p, scanlineSize);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   158
    for i:= 0 to Pred(source^.w - l) do
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   159
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   160
        dec(p, pixelSize);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   161
        if not TransparentLine(p, scanlineSize, source^.h) then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   162
        begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   163
            r:= i;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   164
            break;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   165
        end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   166
    end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   167
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   168
    if SDL_MustLock(source) then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   169
        SDL_UnlockSurface(source);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   170
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   171
    rect.x:= l;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   172
    rect.y:= t;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   173
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   174
    rect.w:= source^.w - r - l;    
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   175
    rect.h:= source^.h - b - t;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   176
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   177
    cropInfo.l:= l;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   178
    cropInfo.r:= r;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   179
    cropInfo.t:= t;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   180
    cropInfo.b:= b;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   181
    cropInfo.x:= Trunc(source^.w / 2 - l + r);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   182
    cropInfo.y:= Trunc(source^.h / 2 - t + b);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   183
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   184
    if (l = source^.w) or (t = source^.h) then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   185
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   186
        result:= nil;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   187
        exit;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   188
    end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   189
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   190
    if (l <> 0) or (r <> 0) or (t <> 0) or (b <> 0) then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   191
        result:= CropSurface(source, @rect)
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   192
    else result:= source;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   193
end;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   194
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   195
procedure SetTextureParameters(enableClamp: Boolean);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   196
begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   197
    if enableClamp and ((cReducedQuality and rqClampLess) = 0) then
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   198
        begin
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   199
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   200
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   201
        end;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   202
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   203
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   204
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   205
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   206
procedure ComputeTexcoords(texture: PTexture; r: PSDL_Rect; tb: PVertexRect);
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   207
var 
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   208
    x0, y0, x1, y1, tmp: Real;
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   209
    w, h, aw, ah: LongInt;
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   210
    p: PChar;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   211
const 
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   212
    texelOffset = 0.0;
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   213
begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   214
    aw:=texture^.atlas^.w;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   215
    ah:=texture^.atlas^.h;
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   216
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   217
    if texture^.isRotated then
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   218
    begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   219
        w:=r^.h;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   220
        h:=r^.w;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   221
    end else
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   222
    begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   223
        w:=r^.w;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   224
        h:=r^.h;        
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   225
    end;
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   226
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   227
    x0:= (texture^.x + {r^.x} +     texelOffset)/aw;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   228
    x1:= (texture^.x + {r^.x} + w - texelOffset)/aw;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   229
    y0:= (texture^.y + {r^.y} +     texelOffset)/ah;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   230
    y1:= (texture^.y + {r^.y} + h - texelOffset)/ah;
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   231
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   232
    if (texture^.isRotated) then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   233
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   234
        tb^[0].X:= x0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   235
        tb^[0].Y:= y0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   236
        tb^[3].X:= x1;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   237
        tb^[3].Y:= y0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   238
        tb^[2].X:= x1;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   239
        tb^[2].Y:= y1;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   240
        tb^[1].X:= x0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   241
        tb^[1].Y:= y1
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   242
    end else
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   243
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   244
        tb^[0].X:= x0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   245
        tb^[0].Y:= y0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   246
        tb^[1].X:= x1;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   247
        tb^[1].Y:= y0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   248
        tb^[2].X:= x1;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   249
        tb^[2].Y:= y1;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   250
        tb^[3].X:= x0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   251
        tb^[3].Y:= y1;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   252
    end;
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   253
end;
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   254
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   255
procedure ResetVertexArrays(texture: PTexture);
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   256
var r: TSDL_Rect;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   257
begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   258
    with texture^ do
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   259
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   260
        vb[0].X:= texture^.cropInfo.l;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   261
        vb[0].Y:= texture^.cropInfo.t;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   262
        vb[1].X:= texture^.cropInfo.l + w;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   263
        vb[1].Y:= texture^.cropInfo.t;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   264
        vb[2].X:= texture^.cropInfo.l + w;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   265
        vb[2].Y:= texture^.cropInfo.t + h;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   266
        vb[3].X:= texture^.cropInfo.l;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   267
        vb[3].Y:= texture^.cropInfo.t + h;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   268
    end;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   269
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   270
    r.x:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   271
    r.y:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   272
    r.w:= texture^.w;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   273
    r.h:= texture^.h;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   274
    ComputeTexcoords(texture, @r, @texture^.tb);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   275
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   276
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   277
function NewTexture(width, height: Longword; buf: Pointer): PTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   278
begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   279
new(NewTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   280
NewTexture^.Scale:= 1;
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   281
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   282
// Atlas allocation happens here later on. For now we just allocate one exclusive atlas per sprite
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   283
new(NewTexture^.atlas);
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   284
NewTexture^.atlas^.w:=width;
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   285
NewTexture^.atlas^.h:=height;
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   286
NewTexture^.x:=0;
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   287
NewTexture^.y:=0;
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   288
NewTexture^.w:=width;
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   289
NewTexture^.h:=height;
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   290
NewTexture^.isRotated:=false;
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   291
NewTexture^.shared:=false;
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   292
NewTexture^.surface:=nil;
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   293
NewTexture^.nextFrame:=nil;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   294
NewTexture^.cropInfo.l:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   295
NewTexture^.cropInfo.r:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   296
NewTexture^.cropInfo.t:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   297
NewTexture^.cropInfo.b:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   298
NewTexture^.cropInfo.x:= width div 2;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   299
NewTexture^.cropInfo.y:= height div 2;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   300
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   301
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   302
ResetVertexArrays(NewTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   303
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   304
glGenTextures(1, @NewTexture^.atlas^.id);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   305
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   306
glBindTexture(GL_TEXTURE_2D, NewTexture^.atlas^.id);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   307
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   308
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   309
SetTextureParameters(true);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   310
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   311
6303
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   312
procedure Surface2GrayScale(surf: PSDL_Surface);
6305
5f7480c2a08d Set default water colours in greyscale mode in case the theme does not define them, decrement piano weapon on use
nemo
parents: 6303
diff changeset
   313
var tw, x, y: Longword;
6303
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   314
    fromP4: PLongWordArray;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   315
begin
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   316
fromP4:= Surf^.pixels;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   317
for y:= 0 to Pred(Surf^.h) do
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   318
    begin
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   319
    for x:= 0 to Pred(Surf^.w) do 
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   320
        begin
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   321
        tw:= fromP4^[x];
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   322
        tw:= round((tw shr RShift and $FF) * RGB_LUMINANCE_RED +  
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   323
              (tw shr GShift and $FF) * RGB_LUMINANCE_GREEN + 
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   324
              (tw shr BShift and $FF) * RGB_LUMINANCE_BLUE);
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   325
        if tw > 255 then tw:= 255;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   326
        tw:= (tw and $FF shl RShift) or (tw and $FF shl BShift) or (tw and $FF shl GShift) or (fromP4^[x] and AMask);
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   327
        fromP4^[x]:= tw;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   328
        end;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   329
    fromP4:= @(fromP4^[Surf^.pitch div 4])
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   330
    end;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   331
end;
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents: 6394
diff changeset
   332
090269e528df - Improve parsing of prefix operators
unc0rr
parents: 6394
diff changeset
   333
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   334
function SurfaceSheet2Atlas(surf: PSDL_Surface; spriteWidth: Integer; spriteHeight: Integer): PTexture;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   335
var
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   336
    subSurface: PSDL_Surface;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   337
    framesX, framesY: Integer;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   338
    last, current: PTexture;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   339
    r: TSDL_Rect;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   340
    x, y: Integer;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   341
begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   342
    SurfaceSheet2Atlas:= nil;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   343
    r.x:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   344
    r.y:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   345
    r.w:= spriteWidth;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   346
    r.h:= spriteHeight;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   347
    last:= nil;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   348
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   349
    framesX:= surf^.w div spriteWidth;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   350
    framesY:= surf^.h div spriteHeight;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   351
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   352
    for x:=0 to Pred(framesX) do
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   353
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   354
        r.y:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   355
        for y:=0 to Pred(framesY) do
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   356
        begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   357
            subSurface:= CropSurface(surf, @r);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   358
            current:= Surface2Atlas(subSurface, false);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   359
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   360
            if last = nil then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   361
            begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   362
                SurfaceSheet2Atlas:= current;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   363
                last:= current;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   364
            end else
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   365
            begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   366
                last^.nextFrame:= current;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   367
                last:= current;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   368
            end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   369
            inc(r.y, spriteHeight);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   370
        end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   371
        inc(r.x, spriteWidth);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   372
    end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   373
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   374
    SDL_FreeSurface(surf);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   375
end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   376
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   377
function Surface2Atlas(surf: PSDL_Surface; enableClamp: boolean): PTexture;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   378
var tw, th, x, y: Longword;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   379
    tmpp: pointer;
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   380
    cropped: PSDL_Surface;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   381
    fromP4, toP4: PLongWordArray;
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   382
    cropInfo: TCropInformation;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   383
begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   384
    cropped:= AutoCrop(surf, cropInfo);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   385
    if cropped <> surf then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   386
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   387
        SDL_FreeSurface(surf);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   388
        surf:= cropped;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   389
    end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   390
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   391
    if surf = nil then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   392
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   393
        new(Surface2Atlas);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   394
        Surface2Atlas^.w:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   395
        Surface2Atlas^.h:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   396
        Surface2Atlas^.x:=0 ;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   397
        Surface2Atlas^.y:=0 ;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   398
        Surface2Atlas^.isRotated:= false;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   399
        Surface2Atlas^.surface:= nil;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   400
        Surface2Atlas^.shared:= false;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   401
        Surface2Atlas^.nextFrame:= nil;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   402
        Surface2Atlas^.cropInfo:= cropInfo;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   403
        exit;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   404
    end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   405
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   406
    if (surf^.w <= 512) and (surf^.h <= 512) then
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   407
    begin
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   408
        Surface2Atlas:= Surface2Tex_(surf, enableClamp); // run the atlas side by side for debugging
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   409
        Surface2Atlas^.cropInfo:= cropInfo;
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   410
        ResetVertexArrays(Surface2Atlas);
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   411
        exit;
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   412
    end;
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   413
new(Surface2Atlas);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   414
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   415
// Atlas allocation happens here later on. For now we just allocate one exclusive atlas per sprite
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   416
new(Surface2Atlas^.atlas);
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   417
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   418
Surface2Atlas^.w:= surf^.w;
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   419
Surface2Atlas^.h:= surf^.h;
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   420
Surface2Atlas^.x:=0;
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   421
Surface2Atlas^.y:=0;
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   422
Surface2Atlas^.isRotated:=false;
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   423
Surface2Atlas^.surface:= surf;
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   424
Surface2Atlas^.shared:= false;
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   425
Surface2Atlas^.nextFrame:= nil;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   426
Surface2Atlas^.cropInfo:= cropInfo;
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   427
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   428
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   429
if (surf^.format^.BytesPerPixel <> 4) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   430
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   431
    TryDo(false, 'Surface2Tex failed, expecting 32 bit surface', true);
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   432
    Surface2Atlas^.atlas^.id:= 0;
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   433
    exit;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   434
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   435
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   436
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   437
glGenTextures(1, @Surface2Atlas^.atlas^.id);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   438
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   439
glBindTexture(GL_TEXTURE_2D, Surface2Atlas^.atlas^.id);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   440
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   441
if SDL_MustLock(surf) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   442
    SDLTry(SDL_LockSurface(surf) >= 0, true);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   443
5441
39962b855540 Add grayscale option for 3d, helps with colour clashing
nemo
parents: 4976
diff changeset
   444
fromP4:= Surf^.pixels;
39962b855540 Add grayscale option for 3d, helps with colour clashing
nemo
parents: 4976
diff changeset
   445
6982
8d41d22a291d breaking news, we don't support typed consts anymore
koda
parents: 6700
diff changeset
   446
if GrayScale then
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   447
    Surface2GrayScale(Surf);
6303
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   448
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   449
if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   450
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   451
    tw:= toPowerOf2(Surf^.w);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   452
    th:= toPowerOf2(Surf^.h);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   453
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   454
    Surface2Atlas^.atlas^.w:=tw;
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   455
    Surface2Atlas^.atlas^.h:=th;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   456
7151
ec15d9e1a7e3 pas2c stuff
unc0rr
parents: 6982
diff changeset
   457
    tmpp:= GetMem(tw * th * surf^.format^.BytesPerPixel);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   458
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   459
    fromP4:= Surf^.pixels;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   460
    toP4:= tmpp;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   461
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   462
    for y:= 0 to Pred(Surf^.h) do
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   463
        begin
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   464
        for x:= 0 to Pred(Surf^.w) do
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   465
            toP4^[x]:= fromP4^[x];
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   466
        for x:= Surf^.w to Pred(tw) do
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   467
            toP4^[x]:= 0;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   468
        toP4:= @(toP4^[tw]);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   469
        fromP4:= @(fromP4^[Surf^.pitch div 4])
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   470
        end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   471
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   472
    for y:= Surf^.h to Pred(th) do
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   473
        begin
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   474
        for x:= 0 to Pred(tw) do
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   475
            toP4^[x]:= 0;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   476
        toP4:= @(toP4^[tw])
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   477
        end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   478
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   479
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   480
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   481
    FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel)
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   482
    end
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   483
else
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   484
    begin
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   485
    Surface2Atlas^.atlas^.w:=Surf^.w;
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   486
    Surface2Atlas^.atlas^.h:=Surf^.h;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   487
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf^.w, surf^.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surf^.pixels);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   488
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   489
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   490
ResetVertexArrays(Surface2Atlas);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   491
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   492
if SDL_MustLock(surf) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   493
    SDL_UnlockSurface(surf);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   494
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   495
SetTextureParameters(enableClamp);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   496
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   497
4901
d1e2d82d9ccc Thou shalt not leak!
sheepluva
parents: 4403
diff changeset
   498
// deletes texture and frees the memory allocated for it.
d1e2d82d9ccc Thou shalt not leak!
sheepluva
parents: 4403
diff changeset
   499
// if nil is passed nothing is done
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   500
procedure FreeTexture(tex: PTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   501
begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   502
    if tex <> nil then
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   503
    begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   504
        FreeTexture(tex^.nextFrame); // free all frames linked to this animation
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   505
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   506
        if tex^.surface = nil then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   507
        begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   508
            Dispose(tex);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   509
            exit;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   510
        end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   511
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   512
        if tex^.shared then
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   513
        begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   514
            SDL_FreeSurface(tex^.surface);
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   515
            FreeTexture_(tex); // run atlas side by side for debugging
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   516
            exit;
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   517
        end;
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   518
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   519
    // Atlas cleanup happens here later on. For now we just free as each sprite has one atlas
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   520
    glDeleteTextures(1, @tex^.atlas^.id);
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   521
    Dispose(tex^.atlas);
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   522
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   523
    if (tex^.surface <> nil) then
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   524
        SDL_FreeSurface(tex^.surface);
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   525
    Dispose(tex);
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   526
    end
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   527
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   528
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   529
procedure initModule;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   530
begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   531
assign(logFile, 'out.log');
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   532
rewrite(logFile);
7295
e70b81854fb9 made surface retaining mandatory
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7292
diff changeset
   533
uAtlas.initModule;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   534
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   535
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   536
procedure freeModule;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   537
begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   538
close(logFile);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   539
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   540
4901
d1e2d82d9ccc Thou shalt not leak!
sheepluva
parents: 4403
diff changeset
   541
end.