hedgewars/uTextures.pas
author Wolfgang Steffens <WolfgangSteff@gmail.com>
Tue, 10 Jul 2012 16:38:50 +0200
changeset 7377 1aceade403ba
parent 7304 8b3575750cd2
child 11317 62287d4044e7
permissions -rw-r--r--
Debug commit Added code to replace sprites with a checkerboard pattern in order to identify filtering issues. This version only renders DrawSprite()'s with the checkerboard pattern for debugging purposes cleaned up some unneeded stuff
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 
7377
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   212
    texelOffsetPos = 0.5;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   213
    texelOffsetNeg = 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
   214
begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   215
    aw:=texture^.atlas^.w;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   216
    ah:=texture^.atlas^.h;
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   217
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   218
    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
   219
    begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   220
        w:=r^.h;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   221
        h:=r^.w;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   222
    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
   223
    begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   224
        w:=r^.w;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   225
        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
   226
    end;
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   227
7377
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   228
    x0:= (texture^.x + {r^.x} +     texelOffsetPos)/aw;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   229
    x1:= (texture^.x + {r^.x} + w + texelOffsetNeg)/aw;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   230
    y0:= (texture^.y + {r^.y} +     texelOffsetPos)/ah;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   231
    y1:= (texture^.y + {r^.y} + h + texelOffsetNeg)/ah;
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   232
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   233
    if (texture^.isRotated) then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   234
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   235
        tb^[0].X:= x0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   236
        tb^[0].Y:= y0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   237
        tb^[3].X:= x1;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   238
        tb^[3].Y:= y0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   239
        tb^[2].X:= x1;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   240
        tb^[2].Y:= y1;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   241
        tb^[1].X:= x0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   242
        tb^[1].Y:= y1
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   243
    end else
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   244
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   245
        tb^[0].X:= x0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   246
        tb^[0].Y:= y0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   247
        tb^[1].X:= x1;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   248
        tb^[1].Y:= y0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   249
        tb^[2].X:= x1;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   250
        tb^[2].Y:= y1;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   251
        tb^[3].X:= x0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   252
        tb^[3].Y:= y1;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   253
    end;
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   254
end;
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   255
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   256
procedure ResetVertexArrays(texture: PTexture);
7377
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   257
var 
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   258
    rect: TSDL_Rect;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   259
    l, t, r, b: Real;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   260
const
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   261
    halfTexelOffsetPos = 1.0;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   262
    halfTexelOffsetNeg = -0.0;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   263
begin
7377
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   264
    l:= texture^.cropInfo.l + halfTexelOffsetPos;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   265
    r:= texture^.cropInfo.l + texture^.w + halfTexelOffsetNeg;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   266
    t:= texture^.cropInfo.t + halfTexelOffsetPos;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   267
    b:= texture^.cropInfo.t + texture^.h + halfTexelOffsetNeg;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   268
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   269
    with texture^ do
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   270
    begin
7377
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   271
        vb[0].X:= l;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   272
        vb[0].Y:= t;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   273
        vb[1].X:= r;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   274
        vb[1].Y:= t;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   275
        vb[2].X:= r;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   276
        vb[2].Y:= b;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   277
        vb[3].X:= l;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   278
        vb[3].Y:= b;
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   279
    end;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   280
7377
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   281
    rect.x:= 0;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   282
    rect.y:= 0;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   283
    rect.w:= texture^.w;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   284
    rect.h:= texture^.h;
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   285
    ComputeTexcoords(texture, @rect, @texture^.tb);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   286
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   287
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   288
function NewTexture(width, height: Longword; buf: Pointer): PTexture;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   289
begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   290
new(NewTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   291
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
   292
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   293
// 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
   294
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
   295
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
   296
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
   297
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
   298
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
   299
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
   300
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
   301
NewTexture^.isRotated:=false;
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   302
NewTexture^.shared:=false;
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   303
NewTexture^.surface:=nil;
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   304
NewTexture^.nextFrame:=nil;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   305
NewTexture^.cropInfo.l:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   306
NewTexture^.cropInfo.r:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   307
NewTexture^.cropInfo.t:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   308
NewTexture^.cropInfo.b:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   309
NewTexture^.cropInfo.x:= width div 2;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   310
NewTexture^.cropInfo.y:= height div 2;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   311
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   312
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   313
ResetVertexArrays(NewTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   314
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   315
glGenTextures(1, @NewTexture^.atlas^.id);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   316
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   317
glBindTexture(GL_TEXTURE_2D, NewTexture^.atlas^.id);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   318
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   319
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   320
SetTextureParameters(true);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   321
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   322
6303
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   323
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
   324
var tw, x, y: Longword;
6303
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   325
    fromP4: PLongWordArray;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   326
begin
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   327
fromP4:= Surf^.pixels;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   328
for y:= 0 to Pred(Surf^.h) do
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   329
    begin
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   330
    for x:= 0 to Pred(Surf^.w) do 
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   331
        begin
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   332
        tw:= fromP4^[x];
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   333
        tw:= round((tw shr RShift and $FF) * RGB_LUMINANCE_RED +  
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   334
              (tw shr GShift and $FF) * RGB_LUMINANCE_GREEN + 
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   335
              (tw shr BShift and $FF) * RGB_LUMINANCE_BLUE);
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   336
        if tw > 255 then tw:= 255;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   337
        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
   338
        fromP4^[x]:= tw;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   339
        end;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   340
    fromP4:= @(fromP4^[Surf^.pitch div 4])
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   341
    end;
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   342
end;
6467
090269e528df - Improve parsing of prefix operators
unc0rr
parents: 6394
diff changeset
   343
090269e528df - Improve parsing of prefix operators
unc0rr
parents: 6394
diff changeset
   344
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   345
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
   346
var
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   347
    subSurface: PSDL_Surface;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   348
    framesX, framesY: Integer;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   349
    last, current: PTexture;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   350
    r: TSDL_Rect;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   351
    x, y: Integer;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   352
begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   353
    SurfaceSheet2Atlas:= nil;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   354
    r.x:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   355
    r.y:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   356
    r.w:= spriteWidth;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   357
    r.h:= spriteHeight;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   358
    last:= nil;
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
    framesX:= surf^.w div spriteWidth;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   361
    framesY:= surf^.h div spriteHeight;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   362
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   363
    for x:=0 to Pred(framesX) do
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   364
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   365
        r.y:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   366
        for y:=0 to Pred(framesY) do
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   367
        begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   368
            subSurface:= CropSurface(surf, @r);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   369
            current:= Surface2Atlas(subSurface, false);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   370
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   371
            if last = nil then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   372
            begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   373
                SurfaceSheet2Atlas:= current;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   374
                last:= current;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   375
            end else
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   376
            begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   377
                last^.nextFrame:= current;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   378
                last:= current;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   379
            end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   380
            inc(r.y, spriteHeight);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   381
        end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   382
        inc(r.x, spriteWidth);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   383
    end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   384
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   385
    SDL_FreeSurface(surf);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   386
end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   387
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   388
function Surface2Atlas(surf: PSDL_Surface; enableClamp: boolean): PTexture;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   389
var tw, th, x, y: Longword;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   390
    tmpp: pointer;
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   391
    cropped: PSDL_Surface;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   392
    fromP4, toP4: PLongWordArray;
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   393
    cropInfo: TCropInformation;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   394
begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   395
    cropped:= AutoCrop(surf, cropInfo);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   396
    if cropped <> surf then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   397
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   398
        SDL_FreeSurface(surf);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   399
        surf:= cropped;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   400
    end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   401
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   402
    if surf = nil then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   403
    begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   404
        new(Surface2Atlas);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   405
        Surface2Atlas^.w:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   406
        Surface2Atlas^.h:= 0;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   407
        Surface2Atlas^.x:=0 ;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   408
        Surface2Atlas^.y:=0 ;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   409
        Surface2Atlas^.isRotated:= false;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   410
        Surface2Atlas^.surface:= nil;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   411
        Surface2Atlas^.shared:= false;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   412
        Surface2Atlas^.nextFrame:= nil;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   413
        Surface2Atlas^.cropInfo:= cropInfo;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   414
        exit;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   415
    end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   416
7377
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   417
    //if (surf^.w <= 512) and (surf^.h <= 512) then
1aceade403ba Debug commit
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7304
diff changeset
   418
    // nothing should use the old codepath anymore once we are done!
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   419
    begin
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   420
        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
   421
        Surface2Atlas^.cropInfo:= cropInfo;
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   422
        ResetVertexArrays(Surface2Atlas);
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   423
        exit;
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   424
    end;
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   425
new(Surface2Atlas);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   426
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   427
// 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
   428
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
   429
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   430
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
   431
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
   432
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
   433
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
   434
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
   435
Surface2Atlas^.surface:= surf;
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   436
Surface2Atlas^.shared:= false;
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   437
Surface2Atlas^.nextFrame:= nil;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   438
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
   439
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   440
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   441
if (surf^.format^.BytesPerPixel <> 4) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   442
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   443
    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
   444
    Surface2Atlas^.atlas^.id:= 0;
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   445
    exit;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   446
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   447
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   448
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   449
glGenTextures(1, @Surface2Atlas^.atlas^.id);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   450
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   451
glBindTexture(GL_TEXTURE_2D, Surface2Atlas^.atlas^.id);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   452
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   453
if SDL_MustLock(surf) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   454
    SDLTry(SDL_LockSurface(surf) >= 0, true);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   455
5441
39962b855540 Add grayscale option for 3d, helps with colour clashing
nemo
parents: 4976
diff changeset
   456
fromP4:= Surf^.pixels;
39962b855540 Add grayscale option for 3d, helps with colour clashing
nemo
parents: 4976
diff changeset
   457
6982
8d41d22a291d breaking news, we don't support typed consts anymore
koda
parents: 6700
diff changeset
   458
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
   459
    Surface2GrayScale(Surf);
6303
3edb3c857995 Add missing grayscale conversions
nemo
parents: 5441
diff changeset
   460
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   461
if (not SupportNPOTT) and (not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h))) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   462
    begin
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   463
    tw:= toPowerOf2(Surf^.w);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   464
    th:= toPowerOf2(Surf^.h);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   465
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   466
    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
   467
    Surface2Atlas^.atlas^.h:=th;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   468
7151
ec15d9e1a7e3 pas2c stuff
unc0rr
parents: 6982
diff changeset
   469
    tmpp:= GetMem(tw * th * surf^.format^.BytesPerPixel);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   470
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   471
    fromP4:= Surf^.pixels;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   472
    toP4:= tmpp;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   473
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   474
    for y:= 0 to Pred(Surf^.h) do
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   475
        begin
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   476
        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
   477
            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
   478
        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
   479
            toP4^[x]:= 0;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   480
        toP4:= @(toP4^[tw]);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   481
        fromP4:= @(fromP4^[Surf^.pitch div 4])
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   482
        end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   483
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   484
    for y:= Surf^.h to Pred(th) do
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   485
        begin
6580
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   486
        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
   487
            toP4^[x]:= 0;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   488
        toP4:= @(toP4^[tw])
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   489
        end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   490
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   491
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmpp);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   492
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   493
    FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel)
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   494
    end
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   495
else
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   496
    begin
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   497
    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
   498
    Surface2Atlas^.atlas^.h:=Surf^.h;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   499
    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
   500
    end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   501
7291
ad4b6c2b09e8 retaining SDL surfaces in order to allow recreating atlases from scratch without
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7186
diff changeset
   502
ResetVertexArrays(Surface2Atlas);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   503
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   504
if SDL_MustLock(surf) then
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   505
    SDL_UnlockSurface(surf);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   506
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   507
SetTextureParameters(enableClamp);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   508
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   509
4901
d1e2d82d9ccc Thou shalt not leak!
sheepluva
parents: 4403
diff changeset
   510
// deletes texture and frees the memory allocated for it.
d1e2d82d9ccc Thou shalt not leak!
sheepluva
parents: 4403
diff changeset
   511
// if nil is passed nothing is done
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   512
procedure FreeTexture(tex: PTexture);
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   513
begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   514
    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
   515
    begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   516
        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
   517
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   518
        if tex^.surface = nil then
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   519
        begin
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   520
            Dispose(tex);
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   521
            exit;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   522
        end;
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   523
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   524
        if tex^.shared then
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   525
        begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   526
            SDL_FreeSurface(tex^.surface);
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   527
            FreeTexture_(tex); // run atlas side by side for debugging
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   528
            exit;
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   529
        end;
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   530
7080
dbf43c07a507 Refactored TTexture to allow encoding sprite(s) at an arbitrary location within
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 6982
diff changeset
   531
    // 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
   532
    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
   533
    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
   534
7297
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   535
    if (tex^.surface <> nil) then
af64b509725c using atlas for rendering now
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7295
diff changeset
   536
        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
   537
    Dispose(tex);
6155187bf599 A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents: 6467
diff changeset
   538
    end
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   539
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   540
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   541
procedure initModule;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   542
begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   543
assign(logFile, 'out.log');
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   544
rewrite(logFile);
7295
e70b81854fb9 made surface retaining mandatory
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7292
diff changeset
   545
uAtlas.initModule;
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   546
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   547
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   548
procedure freeModule;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   549
begin
7304
8b3575750cd2 Added auto cropping to atlasing
Wolfgang Steffens <WolfgangSteff@gmail.com>
parents: 7297
diff changeset
   550
close(logFile);
4375
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   551
end;
ae5507ddb989 Introduce uTextures
unC0Rr
parents:
diff changeset
   552
4901
d1e2d82d9ccc Thou shalt not leak!
sheepluva
parents: 4403
diff changeset
   553
end.